Publish interpretune component concept-direction from interpretune/examples@9a8e683162d0
Browse filesFiles changed (3)
6 unmodified lines789101011121312 unmodified lines2627282929303132336 unmodified lines# used. J-lens stages are added per layer with `jlens_layers` (and `jlens_basis`, default jlens_folded): the lens is# resolved from `neuronpedia/jacobian-lens` by the model name, and the one published for gemma-3-1b-it# (`gemma-3-1b-it/jlens/Salesforce-wikitext`) maps source layers 0-24. Layers 6, 10 and 14 span the store site# (`causal_steering.hook` below is block 10), so the causal column is layer-matched for the layer-10 stage only.# (`causal_steering.site` below is block 10), so the causal column is layer-matched for the layer-10 stage only.# Only the probe examples the model answers as expected enter the store: with every example kept, the two Color examples# the model answers as Fruit would pull the example-weighted directions with weights as large as their errors.ANALYSIS:12 unmodified lines # read the store states at a mid-layer site and steer there; the store-site column above follows the final norm, # so it only reads each direction's alignment with the target unembedding difference causal_steering: hook: blocks.10.hook_out # a venngram site address: the residual leaving block 10 on whichever model the experiment runs site: "point:hook_out & layers:10" # fractions of each stage's own group separation at that site (residual norms there are 300-600, so absolute # scales of 10-100 move little): a stage direction steered by the separation it was read from relative_scales: [0.25, 0.5, 1.0, 2.0]284 unmodified lines285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317389 unmodified lines70770870968368468571071171271371471571671771871972072172269469572372472524 unmodified lines750751752726753754755756757758759760761762284 unmodified lines return [float(scale) for scale in scales]
def causal_steering_hook(causal_cfg: Mapping[str, Any], n_layers: int) -> str: """The one hook ``LATENT_DYNAMICS.causal_steering.site`` addresses on an ``n_layers``-deep stack.
The site is a venngram site address (``point:hook_out & layers:10``), so the configuration names an activation point and a layer rather than one architecture's hook spelling. The causal column reads the store states at that site and steers there, so an address selecting more than one site is refused rather than reduced to its first. A ``hook`` key is refused by name: it was the hook-name form of this setting, and reading it as a site would be a guess about which spelling it used. """ from interpretune.analysis.venngram.sites import resolve_sites
if "hook" in causal_cfg: raise ValueError( "LATENT_DYNAMICS.causal_steering.hook is a hook name; give `site`, a venngram site address such as " f"'point:hook_out & layers:10' (got hook={causal_cfg['hook']!r})" ) if "site" not in causal_cfg: raise ValueError("LATENT_DYNAMICS.causal_steering needs a site: the address whose states it reads and steers") sites = resolve_sites(causal_cfg["site"], n_layers=n_layers).sites if len(sites) != 1: raise ValueError( f"LATENT_DYNAMICS.causal_steering.site {causal_cfg['site']!r} selects {len(sites)} sites {list(sites)}; " "it reads and steers one" ) return sites[0]
def steering_site_config(cfg: Any, hook: str) -> Any: """``cfg`` with its store extraction and its direct-projection intervention both moved to ``hook``.
389 unmodified lines stage's direction record, each direction's steering efficacy at every scale :func:`steering_scales` reads (a list per stage, one record per scale; see :func:`steering_efficacy`), the stage cosine matrix, per-pair diagnostics for every paired store stage, per-example records and the reconstruction check's largest difference. With ``LATENT_DYNAMICS.causal_steering`` (a ``hook`` and its scales), it also reads the store states at that hook, recomputes every stage there and steers at the same hook (``causal_steering``): the store-site column at ``unembed.hook_in`` is only a readout, since that site follows the final norm. largest difference. With ``LATENT_DYNAMICS.causal_steering`` (a ``site`` address and its scales), it also reads the store states at the hook that site resolves to, recomputes every stage there and steers at the same hook (``causal_steering``): the store-site column at ``unembed.hook_in`` is only a readout, since that site follows the final norm. """ from exp.concept_direction import build_notebook_harness_config, maybe_zero_softcap, phase_run_name from interpretune.harness.nb_harness_utils import _resolve_model_layer_count from interpretune.harness.sessions import experiment_session
cfg, cleanup_work_root, payload = build_notebook_harness_config(config_path) latent_cfg = dict(payload.get("LATENT_DYNAMICS") or {}) stages = stages_from_config(latent_cfg) causal_cfg = dict(latent_cfg.get("causal_steering") or {}) if causal_cfg and "hook" not in causal_cfg: raise ValueError("LATENT_DYNAMICS.causal_steering needs a hook: the site whose states it reads and steers") try: with experiment_session(cfg.work_root, phase_run_name(cfg, "latent_dynamics"), **cfg.session_kwargs) as ( _,24 unmodified lines ) causal = None if causal_cfg: hook = str(causal_cfg["hook"]) hook = causal_steering_hook(causal_cfg, _resolve_model_layer_count(module)) site_cfg = steering_site_config(cfg, hook) causal_records, _, causal_diff, _, _ = _stage_records( site_cfg, module, tokenizer, model_backend, stages, latent_cfg ) causal = { "site": str(causal_cfg["site"]), "hook": hook, "reconstruction_max_abs_diff": causal_diff, "raw_norms": {name: rec.get("raw_norm") for name, rec in causal_records.items()},278 unmodified lines279280281282282283284285393 unmodified lines679680681682683684685686687688689690691692693694695696697698699700701278 unmodified lines assert [(s.name, s.basis, s.mode, s.jlens_layer) for s in stages[len(DEFAULT_STAGES) :]] == [ (f"jlens_folded_l{layer}", "jlens_folded", "paired_rejection", layer) for layer in (6, 10, 14) ] assert latent["causal_steering"]["hook"] == "blocks.10.hook_out" assert latent["causal_steering"]["site"] == "point:hook_out & layers:10" accepted = set(inspect.signature(project_embeddings).parameters) - {"values"} unknown = sorted(set(latent["projection"]) - accepted) assert not unknown, f"projection settings project_embeddings does not take: {unknown}"393 unmodified lines lonely = torch.tensor([0] * (int(inputs.group_ids.shape[0]) - 1) + [1]) with pytest.raises(ValueError, match="at least two examples in each group"): state_separation(StageInputs(**{**inputs.__dict__, "group_ids": lonely}))
def test_the_causal_site_address_resolves_to_one_hook_on_each_family_depth(): from exp.analysis.concept_direction_latent_dynamics import causal_steering_hook
# gemma-3-1b-it (the experiment's model) and two further mapped families the address must serve unchanged for n_layers in (26, 16, 12): assert causal_steering_hook({"site": "point:hook_out & layers:10"}, n_layers) == "blocks.10.hook_out" assert causal_steering_hook({"site": "point:hook_out & layers:-1"}, 16) == "blocks.15.hook_out"
def test_the_causal_site_refuses_a_hook_name_a_missing_site_and_several_sites(): from exp.analysis.concept_direction_latent_dynamics import causal_steering_hook
with pytest.raises(ValueError, match=r"causal_steering.hook is a hook name; give `site`"): causal_steering_hook({"hook": "blocks.10.hook_out"}, 26) with pytest.raises(ValueError, match=r"needs a site"): causal_steering_hook({"relative_scales": [1.0]}, 26) with pytest.raises(ValueError, match=r"selects 2 sites"): causal_steering_hook({"site": "point:hook_out & layers:10:12"}, 26)concept_direction/configs/gemma3_1b_it_local_latent_dynamics_color_fruit_orange_fs_l10_n5.yaml+3-2
exp/analysis/concept_direction_latent_dynamics.py+34-6
tests/test_latent_dynamics_stages.py+21-1