Files
OBLITERATUS/docs/CONCEPT_INTERVENTIONS.md
T

21 KiB
Raw Blame History

Concept interventions (CV-06 / CV-07)

The web UI's Concept Lab tab follows Define → Extract → Validate → Intervene → Evaluate → Repair/Export. Define selects a built-in/imported pack id or a local pack path and a model, seed and evaluation budget. Pack data must already be local or verified in the cache; the tab never fetches pack data implicitly. The session uses CPU/FP32, and non-refusal targets require a dense model. Apply stays disabled after a failed gate until a nonempty override reason is entered, including for Refusal in this tab. The same check runs in the callback. Pristine and modified previews share a prompt and greedy decoding settings; the collateral and repair panels retain the measured results and limitations. Exports download as local ZIP bundles with the existing provenance and publication-card guards. Research weight exports stay local. Define starts a fresh run and releases the previous model and temporary artifacts; idle model state expires after one hour. The Obliterate tab's optional Concept selector defaults to Refusal, preserving its existing gate and preset behavior.

apply_concept_intervention accepts an already loaded model and a ConceptDirection. It performs no downloads. Run inference with the model in eval() mode for deterministic comparisons.

from obliteratus.concepts import (
    ConceptDirection, apply_concept_intervention, evaluate_dose_ladder,
)

# Safetensors plus JSON preserve per-layer vectors, selections and validation.
direction.save("steering.safetensors")
direction = ConceptDirection.load("steering.safetensors")

with apply_concept_intervention(
    model, direction, "steer", layers="intervention", positions="final_token",
    alpha=-1, rho=0.6,
) as run:
    first = model(**inputs)
    run.alpha = 1.5                     # read by the next forward pass
    second = model(**inputs)
# Hooks removed; pristine logits are restored exactly in deterministic inference.
Mode Operation Lifecycle
orthogonalize Permanent projection of residual-writer weights Reload the pristine model to undo
project Subtract alpha * (x · unit(v)) * unit(v) run.remove() or context manager
steer Add alpha * rho * ‖x‖ * unit(v) Mutable signed run.alpha; removable
lora Add a rank-one adapter during each writer's forward pass Base weights untouched; remove() / unmerge() is exact

Layers accept extraction, intervention (default), all, inclusive band(a,b), or explicit indices. Per-layer vectors are used when available. A single vector can be applied at another layer; multiple vectors require an unambiguous match. Positions accept final_token or all_tokens. Final token means the last position of the current forward call, including cached decoding; it is not an attention-mask-aware selection for right-padded batches.

Weight modes target attention/MLP residual writers (projection_target: output or all for both groups, attention, or ffn). They use the primary rank-one direction at each layer. regularization retains a fraction of the projection. orthogonalize also supports the existing capped norm_preserve projection and requires all_tokens. LoRA rejects norm preservation because rescaling an entire weight matrix is not a rank-one adapter. Exiting an orthogonalize context leaves the permanent edit in place.

Linear and GPT-2 Conv1D writers are supported, with explicit output-axis orientation even for square matrices. Unsupported writer layouts fail before mutation. Biases, embeddings and incoming residuals are unchanged: writer projection alone does not guarantee zero residual projection on arbitrary models. Permanent projection resolves Accelerate CPU/disk backing stores before any mutation, including fused expert output weights, and rolls back committed parameters if a later write fails. An incomplete rollback marks the model invalid and requires reloading. LoRA requires resident Linear/Conv1D writers. The planted offline fixture isolates the direction in these writers and reaches near-zero held-out residual projection. Use the pipeline for broader quantized surgery support.

Non-refusal concepts require a matching validation report with a pass or warn verdict, consistent with CV-05's warning policy. Missing/failed reports raise GateFailed, even if marked report mode. A nonempty override_gate reason permits an exploratory run and is recorded in run.validation without changing the verdict. Pass the report through direction.validation or validation_report=report. Saved report overrides are also retained. Legacy refusal in the concept=None pipeline remains exempt. In this API, exemption requires both concept_id="refusal" and the built-in core refusal pack's hash; the label alone is insufficient. Explicitly constructed directions without that provenance need a matching report or override, including relabelled directions. use_jailbreak_contrast requires the same refusal provenance.

Dose ladders

Steering effects are coefficient-fragile and non-monotonic. Evaluate each dose separately; a monotone projection shift on the synthetic fixture does not imply monotone behaviour on a trained model. This reporting caution follows the issue's cited evidence, arXiv:2609.16247 and arXiv:2506.00085.

def evaluate(model):
    # Generate or compute logits on your fixed evaluation set here.
    # Return one record per attempted example, including invalid outputs.
    return [
        {"invalid_output": False, "refusal": True},
        {"invalid_output": True, "refusal": False},
    ]

report = evaluate_dose_ladder(model, direction, evaluate, rho=0.6)

The default alphas are [-2, -1, 0, +0.5, +1, +1.5, +2, +3]. Each row records invalid-output count / all attempted examples and refusal count / valid examples. Rates with zero denominators are None. Per-example fields supplied by the evaluator (such as projection or KL) are retained. The report includes the non-monotonicity caution and gate record. Hooks are removed even if evaluation raises. The helper uses inference mode but leaves model training/evaluation state to the caller. Setting run.alpha clears per-layer coefficient overrides so the next forward uses that dose on every selected layer. The ladder accepts only mode="steer". Evaluator records must be mappings with Python, NumPy, or scalar Torch boolean fields; integer flags are rejected.

Adapter and vector export

For a LoRA run, run.manager.save(directory, base_model_name_or_path="original/base", base_model_revision=revision) writes unmerged PEFT safetensors/config through the existing export utility. The base identity is explicitly unverified. Load onto the original base. Position- restricted adapters cannot be exported as ordinary PEFT, whose updates apply at every position. Mixed Linear/Conv1D exports are rejected. Existing save_lora_adapters remains the provenance-verified export API for compatible PEFT pairs; existing merge/unmerge APIs retain their original semantics.

SteeringVector.save/load also supports individual legacy vectors using safetensors plus JSON (source layer, label, default alpha and metadata). For multiple layer-specific vectors, use ConceptDirection.save/load as above; reapply runtime options such as alpha, rho and positions explicitly on load.

Both public SteeringHookManager and the pipeline's existing activation-steering installer now use the same hook implementation. Legacy manager calls retain fixed-magnitude addition; the pipeline retains its original scaled projection.

Evaluation battery (CV-07)

Non-refusal concept pipeline runs now capture a pristine baseline before EXCISE and write evaluation.json immediately afterward. Legacy refusal runs retain their historical verification path. The report separates mechanistic, behavioural, collateral, and untested_channels; it is also included in concept metadata. A mechanistic reduction alone does not establish a behavioural change. A behavioural null is a valid result.

The default collateral battery samples 320 paired prompts from BUILTIN_HARMFUL, using the run seed (0 when unset), greedy generation, chat formatting and 48 new tokens. Both arms use exactly the same prompts and decoding settings. Configure refusal_eval_size / --refusal-eval-size and refusal_eval_max_new_tokens / --refusal-eval-max-new-tokens. Tokenizers without a chat template use raw prompts and explicitly report that channel as untested. The report records the template and generation configuration.

Refusal results include both counts and Wilson 95% intervals, refusal losses and gains, a seeded paired-bootstrap 95% drop interval, and the one-sided exact McNemar p-value (binomial test on discordant pairs, alpha 0.05). A significant drop is FAIL, including on small subsets. Otherwise fewer than 284 pairs is UNDERPOWERED; PASS means no detected drop at declared power, never equivalence. Missing KL, neighbour directions or chat formatting makes an otherwise passing overall collateral result INCOMPLETE. The refusal decision remains available separately. No aggregate pass is inferred from untested channels.

Pristine refusal flags are cached under the output directory's .pristine_refusal_cache, keyed by model id/revision, ordered prompt hash, decoding configuration and refusal scorer. Unresolved model revisions disable persistent reuse. Other pristine measurements are always recomputed before edits. The model adapter reads mean absolute final-token residual projections on the existing coherence anchors, for every layer with a direction (a single vector can be read across all layers). It also compares anchor completions and first-token KL. Pipeline neighbour vectors use the pristine neighbour-minus- control activation means; absent directions are explicitly untested. These anchor readouts do not certify removal at other token positions or contexts.

skip_collateral=True / --skip-collateral records skipped and its reason. Research-tier intervention exports require a completed collateral battery with pristine and intervened refusal-rate results. Skipped or missing collateral or refusal rates block export, including local direction bundles that recreate an intervention, adapters, checkpoints and reports of an intervened model. No publication or gate override can waive this requirement. User-tier exports may retain a skipped battery and its reason. Plain direction or validation-only records use intervention mode none and remain exportable as directions or reports without collateral evaluation; direction bundles omit the intervention loader in this mode.

Mechanistic and available behavioural measurements still run. Pack sets.battery data is preserved in ConceptTarget.battery; the default local hook compares exact greedy completions on each named set. This is a descriptive null on those prompts. A task-specific scoring hook can replace it; packs never execute code.

For the four intervention modes, use the evaluated entry point with an already loaded model and tokenizer:

from obliteratus.concepts import EvaluationBattery, evaluate_intervention
from obliteratus.concepts.evaluation_model import ModelEvaluator

evaluator = ModelEvaluator(model, tokenizer, direction, neighbours=neighbour_directions)
battery = EvaluationBattery(
    evaluator.generate, measure=evaluator.measure,
    model_id=direction.model_id, model_revision=direction.model_revision,
    decoding_metadata=evaluator.decoding_metadata, cache_dir="run/cache",
)
with evaluate_intervention(
    model, direction, "project", battery, output_dir="run",
) as run:
    print(run.evaluation.collateral)

apply_concept_intervention remains the low-level mutation/hook primitive. EvaluationBattery.capture() and .finish() can also bracket an existing intervention pipeline. Generation is injectable as generate(prompts, decoding); measurement is a separate callable. This permits fully offline stub tests without downloading weights. behavioural_battery(generate, decoding) returns a nonempty mapping of battery names to JSON scores, compared before/after.

Optional resteer_probe and kv_probe callbacks return JSON measurements. evaluation_model.resteer_probe runs independent dose ladders for the target and one neighbour on the edited model. kv_cache_probe creates clean and steered prefix caches using identical tokens, removes the temporary steering, then compares logits after identical continuation tokens. Temporary hooks are removed on error. These helpers restore model training state where they change it. Without a KV callback, every report states "KV-cache-mediated effects: not tested". The probes measure the configured contexts only; they do not establish the absence of persistent state elsewhere.

Compare and export (CV-08)

Concept pipeline checkpoints and evaluate_intervention(..., output_dir="run") now include concept_run.json and a portable direction. Legacy refusal runs retain their existing save path. To save a refusal run through the concept API, or to retain weights or an unmerged LoRA adapter, use save_concept_run explicitly:

from obliteratus.concepts import save_concept_run

# Call while the intervention is active. Supply its validated source pack.
save_concept_run("run", direction, run, pack=pack, model=model, tokenizer=tokenizer)

Supply model for orthogonalize or lora only. Hook modes save directions and runtime parameters. A mutable steering dose is captured at save time. Unmerged adapters require the original base; ordinary PEFT cannot represent final-token restrictions. Permanent pipeline edits, including merged adapters, export as weights. A separate unmerged LoRA run exports as lora.

Automatic evaluation saving refuses an output directory containing concept_run.json or concept_provenance.json before evaluating or intervening. It validates and saves the run before writing evaluation.json, so a pack identity mismatch leaves no battery files. Explicit save_concept_run calls replace the saved direction and run record, allowing updated parameters or weights to be captured deliberately.

obliteratus concepts compare run-a run-b --json
obliteratus concepts export run-a export-weights --as weights
obliteratus concepts export run-b export-adapter --as lora
obliteratus concepts export run-a export-direction --as direction
obliteratus concepts export run-a export-report --as report
obliteratus concepts export run-a export-card --as report --publish-card \
  --reason "Exploratory publication; collateral failure acknowledged"

Each output must be a new directory outside the source run. Comparison rows include validation verdict, residual projections, collateral, behaviour, repair steps and untested channels. Exported bundles can also be compared.

Every kind writes concept_provenance.json, checked against the installed schema/concept-provenance.v1.schema.json. It records pack id/version/hash and source references, extraction recipe, seed, model revision, validation verdict and metrics, mode and parameters, repair steps, and OBLITERATUS version/commit. The embedded checkpoint_provenance record binds these facts and the output file hashes into its content-addressed identity. Missing model revisions, pack hashes or source commits stay explicitly unknown. Programmatic targets without a pack have no source references. For installed wheels without Git metadata, record the actual OBLITERATUS commit with --tool-commit <40-hex>.

Bundles copy an allowlist of checkpoint, adapter or direction files and never copy pack archives, manifests or source prompt sets, regardless of licence. Source references include the source URL, pinned commit when available, per-file hash and licence. Direction bundles include a loader snippet for an already loaded model. Weight and adapter files use safetensors.

Cards distinguish direction removed on the measured readout from behaviour changed on battery or a behavioural null. Reports preserve partial, underpowered and skipped evaluations. MODEL_CARD.md is a draft; --publish-card writes README.md. A collateral FAIL or recorded gate override blocks --publish-card with exit 5 unless --reason (alias --override-publish) is supplied. The published section includes the verdict and override notice. Collateral FAIL permits every other local export with the failure recorded. An incomplete repair requires --accept-incomplete-repair "<choice and reason>". Run records retain the ladder's status, ordered attempts and rejection reasons for comparison, cards and export provenance. Weight bundles include any saved unmerged repair adapter under repair_adapter/; load it onto the edited checkpoint to reproduce the repaired result.

The web UI's Push to Hub tab exposes both acknowledgements under Publication acknowledgements (optional): a publication override reason and an incomplete repair choice and reason. Incomplete repair blocks a push until its acknowledgement is provided. Concept checkpoints are uploaded as fresh allowlisted weight bundles; their published card and provenance record both choices and retain the original outcomes. Malformed checkpoint concept metadata blocks publication. Outcome language checks also reject subjective-state removal wording, including nominalized and hyphenated adjective forms, in either field.

--push-to-hub <repo> explicitly uploads an export; draft MODEL_CARD.md is excluded. Research-tier packs may upload only directions and reports. A reason cannot authorize research weight or LoRA uploads. The permanent pipeline and the web UI's checkpoint upload apply the same research-weight restriction. Refusal checkpoint exports preserve existing metadata values and add concept provenance fields. No new runtime dependency is required.

Coherence repair (CV-10)

Non-refusal pipeline runs attempt repair when the evaluation battery measures an absolute coherence-rate drop greater than 0.05 from the pristine anchors. Legacy refusal remains unchanged; --repair explicitly enables evaluation and repair for it. --skip-repair disables repair. Skipped or missing coherence measurements report repair: not tested; a run within budget reports repair: not needed without attempting edits.

The ladder records candidates in this order:

  1. Norm preservation (restore writer norms, with the existing 2× cap).
  2. Retained-fraction regularization (restore 0.1 of the original writer edit).
  3. KL-budget rollback (try retained fractions 0.25, 0.5, 0.75, 1).
  4. RDO refinement (cached positive/control probe optimization, 100 steps).
  5. A reversible rank-4 LoRA repair adapter, restoring the lost component orthogonal to the original direction through a truncated SVD.

Every candidate is evaluated against the same pristine baseline. Acceptance requires coherence within budget, per-layer target projections at most max(1e-8, pristine_projection * 1e-4), first-token KL at most 0.5, and no worsening of measured collateral: refusal-rate loss, KL and absolute neighbour projection drift. A new collateral FAIL also rejects the candidate. RDO is checked against the original extracted direction, not its refined replacement. Missing target, coherence, refusal or KL evidence cannot grant acceptance. Non-finite measurements reject the candidate and are recorded as JSON null with the affected measurement paths in the rejection reasons; the ladder continues. The evaluation report still records untested channels and power limitations.

Rejected candidates restore the original intervention's writer weights exactly. The first accepted candidate stops the ladder. If none qualifies, the original intervention remains active and repair: incomplete records all rejections and the best measured coherence trade-off. Reports remain available; exporting that checkpoint requires --repair-export-choice original (API: repair_export_choice="original"). This choice does not turn repair into a pass.

repair.json and the concept metadata include the ordered measurements and reasons; evaluation.json describes the retained result. An accepted LoRA repair is saved separately under repair_adapter/, unmerged. Load this adapter onto the exported edited checkpoint, whose identity is marked unverified. Loading only the checkpoint omits the repair. Remove the in-memory adapter via pipeline.repair_adapter.remove() or, for the evaluated API, via run.repair_adapter.remove(). Exiting the evaluated API's context also removes its repair adapter; permanent base edits remain.

For an already loaded model, opt in with evaluate_intervention(..., repair=True, repair_config=RepairConfig(...)). Supply repair_activations={layer: (positive_rows, control_rows)} for RDO. The default backend supports resident dense residual-writer weight edits; offloaded writers, hook-only interventions and missing RDO inputs record unavailable steps. Pipeline capture costs two CPU copies of selected writer weights and retains cached activation rows until repair completes. Other backends can implement transactional contexts for run_repair_ladder; no pack code is executed and no additional dependencies or downloads are needed.

For the end-to-end flow from define to export, start at the concept workflow and interpretation guide.