Files
OBLITERATUS/docs/neuron-lab.md

329 lines
18 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Neuron Lab
Open **Concept Lab → Neurons** in the local UI to inspect and temporarily change
individual native MLP channels. The **Concepts** tab retains the existing concept
workflow. Neuron Lab owns a separate model for each browser session, so loading
another model also requires its own memory budget.
This first release implements reversible activation experiments. It does not
change parameters, export modified weights, fit normalized doses, establish
behavioral neurons, or reproduce a published model result. The offline tests use
synthetic gated MLPs and a small randomly initialized Llama.
## Supported execution
The adapter accepts native dense Llama, Qwen2, Qwen3 and Qwen3.5 gated MLP layouts,
including the nested Qwen3.5 text backbone. Every layer must expose ordinary
floating-point `gate_proj`, `up_proj` and `down_proj` linear modules with matching
dimensions. The intervention site is the input to `down_proj`: the channel after
the gate activation and multiplication by the up projection.
CPU / FP32 is the tested reference. The UI exposes opt-in MPS / FP32 and CUDA /
FP32, explicitly marked unqualified. Architecture acceptance does not establish
compatibility or performance for every checkpoint. Quantized projections, MoE,
offloading, dispatched or multi-device models, and cached forwards are rejected.
All weights must be resident on one device and the model must be in evaluation
mode. The existing loader's memory-admission checks and warnings still apply.
## Try an experiment
1. Open **Model connection** and load a supported model ID or local directory.
**Use local files only** defaults to on; turn it off to permit download.
Remote code remains disabled. Optionally supply a Hub commit in **Model revision**.
2. Enter a shared prompt and choose a zero-based layer and neuron in the control
rack. **Inspect neuron** captures baseline activity: violet bars are positive,
cyan bars negative. Hover or keyboard-focus a token to read its exact value.
3. Choose an operation. The strength slider and exact numeric input stay in sync;
clamp exposes bounds instead, and flip/zero need no strength. A blank clamp
bound leaves that side unbounded. **Apply to stack** saves the edit. Controls
alone never trigger a model forward.
4. Choose a saved edit to populate its controls and enter **Replace selected**
mode. Move it up/down, remove it, or uncheck **Enable this edit** and apply to
disable it. Choose **New edit** to append instead. The intervention stack shows
execution order, formula, target and enabled state.
5. Select the token scope and apply to the stack to save it. **Run comparison**
produces baseline and recipe response cards with identical prompt formatting
and greedy decoding. Optionally import and enable a separate control condition.
6. Open **Files, data & run details** to import/export recipes or experiments, or
inspect layer geometry, full tables, statistics and admission warnings.
**Reset all neuron controls** clears saved edits, control, inspection, comparison
and the editor. Hooks are already gone after every model action.
## Find a candidate, then test the hypothesis
**Find candidates** compares two prompts at their final valid input token. Leave
the layer field blank to sample up to four evenly spaced layers, or enter up to
eight distinct zero-based layer indices. The scan makes two baseline forwards
and ranks the top twelve channels by `abs(B - A) * norm(down_proj[:, channel])`.
The table exposes both raw activations and this descriptive write-magnitude
proxy. It does not fit a direction, calibrate a dose, or measure a causal trait.
Prompt wording, content and formatting can all drive the ranking.
**Use candidate in controls** copies its address and prompt B into the editor
without running the model or changing the recipe. Inspect the candidate,
choose an intervention and scope, then test on held-out prompts with a separate
control recipe. One successful completion is a lead; check alternate prompts,
unrelated tasks, random targets and reset before attributing a behavior to it.
Scan hooks are removed after success, failure and cancellation. The scan is
bounded to the same 512-token input limit as inspection.
## Talk to both conditions
**Conversation** sends each question to the baseline and your current recipe.
Each condition carries its own assistant history. The first turn freezes the
recipe for the conversation; changing any recipe setting requires **Restart
conversation** before another turn. Both replies must succeed before a turn is
saved, so failure or cancellation cannot leave one history ahead of the other.
Scope is evaluated for each new reply: prior assistant messages become part of
the next turn's prompt, so `prompt_tokens` also applies to those history tokens.
Conversations allow at most eight paired turns, 128 generated tokens per reply,
and 512 rendered input tokens per condition. Histories are never silently
truncated. This is a runtime intervention on the connected checkpoint, not a
newly trained or saved model. Reset clears the conversation together with the
scan and other evidence. Export a conversation before restarting to retain it.
When a tokenizer has a chat template, the prompt becomes one user message with
`add_generation_prompt=True` and `enable_thinking=False`. Templates may ignore
the thinking option; the lab does not promise to disable reasoning for every
model. Without a chat template it uses plain text. Rendered prompts are limited
to 512 tokens and completions to 128 tokens; context limits are checked and input
is never silently truncated.
**Prompt setup** optionally prepends an explicit system instruction. Blank is
the default and adds none. **Apply prompt setup** clears previous captures,
scans, comparisons and conversations when the instruction changes, preserving
the intervention recipe. Prompt formatting can materially change an edit's
effect; record the instruction and template with the result. Default evidence
exports retain its hash, while text-inclusive exports retain the instruction.
## Explore the activation journey
The observatory replays the most recent inspection in an interactive spatial
scene. Token order runs along x; signed activation, normalized to that capture's
largest magnitude, runs along y. Violet is positive and cyan negative. The
selected token readout reports its original raw value. Depth is an illustrative
helix, **not an embedding projection or a model-wide latent map**. Stars, rings
and the entry field are decorative and labeled schematic. Missing values leave
gaps in the trace rather than becoming zero-valued points.
Drag to orbit, use +/− to zoom, and use the scrubber or arrow buttons to travel
through captured tokens. The focused canvas also accepts arrow keys, space to
pause, +/−, and Home to reset the view. **Pause motion** stops the scene and the
ambient hero motion; reduced-motion preferences start the lab paused. Playback
is local to the browser, uses no external assets, and never runs the model.
The layer destinations come from the loaded model's actual geometry. Selecting
one sets the layer, resets the neuron to zero and chooses **New edit**. It leaves
saved recipes and the previous capture untouched; the capture's address remains
visible until **Inspect neuron** runs again. A busy session rejects navigation.
The scene contains at most 512 captured token positions and suspends animation
when hidden or offscreen.
### Listen to the capture
The optional **Listen** control turns measured token activations into short tones,
inspired by [ENTHEA](https://github.com/elder-plinius/ENTHEA). Audio starts only
after a user gesture. It uses the captured values already in the browser and
does not run the model. The mapping legend describes pitch and loudness;
this is activation sonification, not speech or a decoding of thoughts.
Pause, stop, mute and volume controls remain available. Hidden, detached or
replaced scenes release their audio resources. No external audio assets are used.
## Prompt Prism
Open **Prompt Prism** and choose **Refract the prompt** to compare four literal
forms of the shared prompt: original, uppercase, a JSON string, and words on
separate lines. This draws conceptual inspiration from
[P4RS3LT0NGV3](https://github.com/elder-plinius/P4RS3LT0NGV3); it does not import
that project's code or prompt corpus. Transformations can change meaning and
tokenization, so this experiment measures sensitivity, not semantic invariance.
Each form receives baseline, frozen recipe, and fresh unedited baseline trials.
The same system instruction applies throughout. All forms are validated before
inference; each gets at most 512 rendered input tokens and 32 completion tokens.
The source prompt is bounded to 4,096 characters. The maximum budget is twelve
times the completion-token limit in uncached forward calls. There is no automatic
search, sampling or selection of a winning transformation.
Expand a result to inspect the literal input and its actual tokenizer pieces.
The cards show exact output-token changes, first-prediction KL, and whether both
the first-prediction logits and generated tokens matched after removing the
intervention. These do not establish semantic similarity or capability retention.
The edit stack itself stays saved; hooks are removed between trials. Results
commit only after every condition finishes, and failed or cancelled runs clear
the previous Prism result. Edits, model loading, prompt-setup changes and reset
also invalidate the stored Prism evidence.
**Export prompt prism** defaults to hashes, recipe, environment and measurements.
The text checkbox additionally includes original/rendered inputs, system message,
token IDs/pieces and responses. This export follows the same session-owned file
lifetime as other Neuron Lab downloads.
## Operations and scope
For channel activation `a`, the operations are:
| Operation | Result | Parameter units |
|---|---|---|
| `add` | `a + value` | Raw activation |
| `scale` | `value * a` | Dimensionless gain |
| `flip` | `-a` | None |
| `zero` | `0` | None |
| `set` | `value` | Raw activation |
| `clamp` | Bound `a` by `lower` and/or `upper` | Raw activation |
Repeated edits to the same target execute in recipe order. For example, adding
one then flipping differs from flipping then adding one. An arithmetic sign flip
does not imply a semantic opposite. Values must be finite; nonfinite selected
activations abort the action and remove its hooks.
`all_tokens` selects all non-padding positions. `prompt_tokens` selects the
original prompt positions. `generated_tokens` selects already-generated
positions, so the first answer token is unchanged: it is predicted from the
prompt. Boundaries count valid tokens rather than padded offsets. Scope applies
to the entire recipe.
Every comparison starts each condition from the same prompt and uses uncached,
complete-prefix forwards. This simple reference path is slower than ordinary
cached generation. **Cancel neuron action** stops between forwards; a running
device operation must finish before cancellation takes effect.
## Reading the results
Inspection reports per-token activations, mean, standard deviation, range,
positive/negative fractions, the selected down-projection column norm, and the
median norm of the selected transformer layer's output. These are descriptive
measurements for one prompt, not corpus calibration or causal trait labels.
The comparison shows completions, token counts, elapsed time, first-token
`KL(baseline || condition)`, repeated-token fraction, and an invalid-output
heuristic. First-token KL measures the distribution before any generated token
has been consumed; it therefore stays zero for generated-only interventions.
Elapsed time is observational and is not a performance benchmark. Greedy
decoding uses no sampling seed; bitwise parity across devices is not claimed.
These measurements do not evaluate capability retention or trait accuracy.
## Python API
Use an existing compatible `ModelHandle`, whose model is in evaluation mode:
```python
from obliteratus.interventions import NeuronEdit, NeuronRecipe, NeuronSession
recipe = NeuronRecipe(
model_id=handle.model_name,
model_revision=handle.source_model_revision,
edits=(NeuronEdit(layer=0, neuron=0, operation="scale", value=0.5),),
scope="all_tokens",
)
with NeuronSession(handle, recipe):
result = handle.model(**inputs, use_cache=False)
# Hooks are removed, including if the forward raises. Parameters never change.
recipe.save("neuron-recipe.json") # Exclusive creation; does not overwrite.
restored = NeuronRecipe.load("neuron-recipe.json")
```
Use `list_neuron_layers(handle)` for channel bounds and
`inspect_neuron(handle, layer, neuron, inputs)` for a baseline capture. For prompt
or generated scopes, provide `prompt_lengths` to `NeuronSession`, as an integer
for all rows or one integer per batch row. A session permits one positional
argument (`input_ids`); pass attention, position and cache arguments by keyword.
Omitted cache settings are forced to false without changing the model config.
Sessions are single-use and exclusive per model. Forwarding the hooked model
from another thread is rejected. Use the context manager so cleanup is reliable.
## Files and reproducibility
Recipe JSON schema version 1 records the model ID, optional model revision,
scope, and ordered enabled/disabled edits. Imports accept at most 64 edits and
64 KiB, reject unknown or duplicate fields, and validate identities, indices,
types and finite parameters before installing hooks. Recipes are data, never
executable code. A provided revision must match the loaded handle; an absent
revision cannot establish exact checkpoint identity. Local checkpoints without
a resolved revision require separately recording their file hashes for exact
reproduction.
Experiment JSON records the model/tokenizer revisions when available, runtime
versions, device, dtype, recipes, prompt hash, chat-template hash and options,
decoding settings and measurements. By default it omits prompt/output text,
token IDs and inspection token strings. Text is included only when explicitly
selected. Hashes and numerical activations remain potentially informative; the
redacted report is not anonymization. Downloads receive unique filenames so
subsequent exports do not overwrite prior evidence. Files belong to the browser
session and are removed when it expires; download any files you want to keep.
Experiments also include the most recent candidate scan, if any; its prompt text
is omitted by default. Conversation downloads freeze the recipe, condition
histories, formatting and decoding metadata separately. Their default export
omits messages, prompts, response text and token IDs, retaining hashes and
measurements. **Include conversation text in download** explicitly includes the
paired transcript for replay. These exports are evidence, not anonymization.
The experiment schema is an evidence export, not an import/replay protocol.
Replay the separate recipe and supply the same model, prompt and settings.
Corpus-scale discovery, calibration, normalized doses, sweeps, activation patching,
capability evaluation and permanent surgery are follow-on work.
## Operation observatory across the app
The shared **Open operation observatory** control appears above all workspaces.
It is closed by default. Opening it adds no model hooks and does not start a run.
Its first live adapter follows the **Obliterate** pipeline's actual stage events,
status, elapsed time, messages and reported details. The panel remains available
when switching tabs; other operations retain their existing result views.
Orbit nodes represent reported pipeline stages, not measured activations or
layer geometry. Counts show completed reported stages, never an estimated
completion percentage. Failures, cancellation and timeouts retain their real
terminal status. Each run owns a bounded journal; concurrent runs cannot write
into one another's scene. **Pause motion**, reduced-motion preferences,
offscreen detection and page visibility control animation locally. Closing the
panel stops its animation while the underlying operation continues normally.
## Verification
The default tests are deterministic, offline and CPU-safe:
```bash
python -m pytest tests/test_neuron_interventions.py tests/test_neuron_lab.py \
tests/test_ui_neuron_lab_wiring.py tests/test_neuron_lab_visuals.py \
tests/test_neuron_lab_journey.py tests/test_neuron_lab_research.py \
tests/test_ui_theme.py tests/test_ui_observatory.py --cov-fail-under=0
```
With the locked `spaces` extra installed, the conditional UI test drives the
actual Gradio callbacks using a locally generated model and tokenizer:
```bash
python -m pytest tests/conditional/test_operator_ui.py \
-k neuron_lab --cov-fail-under=0
```
Tests check arithmetic against independent expectations, unchanged parameters,
padding and scope boundaries, exceptions and cancellation, model ownership,
strict imports, export privacy, editor synchronization, one-sided clamp round trips,
escaped display text, signed chart geometry, and isolation between two browser sessions.
The journey runtime regressions run with Node.js when it is available; they
verify remount cleanup, paused detach cleanup, and missing-value rendering.
### Verify a reset without losing the comparison
After a paired run, **Clear edits & verify reset** clears the active intervention
and replays the exact stored input tensors and generation limit. The comparison
keeps its baseline and edited answers and adds a third, freshly generated reset
answer. A successful receipt requires exact token IDs, exact first-step logits,
and the same registered forward/pre-forward hooks as before the paired run.
The receipt includes the run ID and UTC verification time. A failed or cancelled
replay clears edits but never displays a successful receipt.
This check applies to this saved input only; it is not an accuracy benchmark or a
weight checksum. Existing external hooks are preserved, and changes to their
registrations cause a mismatch. Changing the recipe or system context invalidates
the saved replay. **Reset all neuron controls** retains its original full-clear
behavior. **Focus on results** hides setup controls while keeping their values,
so comparisons and receipts can use the full workbench width.