feat(jacobian_lens): add occupancy and fraction-of-variance - #1676
Open
janmenjayap wants to merge 1 commit into
Open
feat(jacobian_lens): add occupancy and fraction-of-variance#1676janmenjayap wants to merge 1 commit into
janmenjayap wants to merge 1 commit into
Conversation
Two J-space profiling statistics on top of the sparse decomposition, following Gurnee et al. (2026): - estimate_occupancy / JacobianLens.occupancy: how many J-lens vectors are meaningfully active in an activation, via the step of maximum separation between the real and a random-control cumulative captured-variance curve (deterministic, threshold-free). - JacobianLens.fraction_of_variance / JSpaceVarianceProfile: the J-space share of activation variance over a corpus, per layer as the median and pooled ratio of ||j_space_component||^2 / ||activation||^2 -- the selected-support span projection, not the nonnegative reconstruction. Model-free unit tests (planted-sparsity recovery, determinism, input validation, and the fraction_of_variance no-sample NaN contract) plus gpt2-small integration tests (occupancy seed-determinism, the fraction positions= override, and the corpus median/pooled path); documented in jacobian_lens_fitting.md as an open-weight, shape-only observation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds occupancy and fraction-of-variance to the Jacobian lens: two J-space profiling statistics that sit on top of the sparse decomposition (#1596), following Gurnee et al. (2026), "Verbalizable Representations Form a Global Workspace in Language Models" (Transformer Circuits Thread). They quantify two of the paper's qualitative claims: that "only a small number of J-lens vectors are strongly active at a time" (occupancy) and that the J-space is "never more than about 10%" of activation variance, "a median of 6 to 7%" for concept vectors (fraction of variance).
This is the Tier-A follow-up the maintainer explicitly deferred from #1596; it builds directly on that PR's greedy selection and cached J-lens dictionary. Part of #1539 (Tier-A follow-up to #1596). No new dependencies.
New public surface (TransformerBridge only, matching the rest of the Jacobian lens)
Model-free core in
transformer_lens/tools/analysis/jacobian_lens_decomposition.py:estimate_occupancy(x, dictionary, *, max_atoms=25, num_control_dictionaries=32, seed=0)returns aJSpaceOccupancy(occupancy,marginal_captured_variance,control_captured_variance,support).On
JacobianLens(additive; existing methods unchanged):occupancy(model, activation_or_prompt, layer, *, position=None, max_atoms=25, num_control_dictionaries=32, seed=0)resolves a raw[d_model]activation or a prompt position exactly asdecompose, then callsestimate_occupancyon the cached full-vocabulary dictionary.fraction_of_variance(model, prompts, layers=None, *, k=25, skip_first=16, positions=None, show_progress=False)profiles a prompt corpus and returns aJSpaceVarianceProfile(layers,median,pooled,per_position).JSpaceOccupancy,JSpaceVarianceProfile, andestimate_occupancyare exported fromtransformer_lens.tools.analysis.Occupancy: max-separation versus a random-dictionary control
estimate_occupancyruns the same greedy selection asget_sparse_decompositionup tomax_atoms, recording the per-step captured variance||Pi_S x||² / ||x||²(orthogonal projection onto the selected span). It runs the same greedy onnum_control_dictionariesrandom unit-norm dictionaries of equal size and averages their curves. The occupancy is the step of maximum separation between the real and control cumulative captured variance: the point past which further vectors add no more than random directions would. It is deterministic (seeded) and needs no threshold.An earlier per-step quantile-cutoff variant was calibration-sensitive (planted structure wanted
q ≤ 0.75, a clean noise floor wantedq ≥ 0.9, with no single robust value). The max-separation rule recovers planted sparsity exactly on orthonormal dictionaries (k=1gives 1,k=4gives 4,k=7gives 7) with no tuning, soquantilewas dropped.Fraction of variance: the span-projection operationalization
For each
(layer, position)at or pastskip_first(mirroring the fit's early-position skip),fraction_of_variancerecords||j_space_component||² / ||activation||²and reports, per layer, themedianof those fractions and thepooledratioΣ||j_space||² / Σ||activation||². The numerator is theselected_supportspan projection (the paper's appendix operationalization), not the nonnegativereconstruction; the two coincide only when every selected atom stays active. This is named explicitly in the docstring and docs so the number is not misread. A layer that samples no positions (every prompt shorter thanskip_first, or only zero-norm activations) yieldsNaNmedianandpooledvalues and an emptyper_position. This is a documented, tested contract.Tests
test_jacobian_lens_decomposition.py): planted-sparsity recovery (occupancy == k), ordering by planted density, determinism, and input validation.test_jacobian_lens.py): occupancy raw and prompt paths,fraction_of_variancemedian and pooled over a corpus, unfitted-layer and empty-corpus rejection, and the no-sampleNaNcontract.test_jacobian_lens.py): occupancy is a small positive integer with[0, 1]-bounded cumulative captured-variance curves and is seed-deterministic;fraction_of_variancemedian and pooled land in[0, 1], including thepositions=override path over a multi-prompt corpus.Honesty note
The paper's quantitative occupancy and variance figures are closed-model results (Sonnet, Haiku, Opus). Tests and docs assert shape (occupancy is a small positive integer; the variance fraction is a small ratio in
[0, 1]), not the paper's exact numbers, which are not expected to transfer to open weights.Docs
A new "Occupancy and fraction of variance" subsection in the "Sparse decomposition" section of
jacobian_lens_fitting.md, with runnable snippets and the open-weight, shape-only caveat.Type of change
Screenshots
Not applicable. This adds an analysis API plus a docs page, with no visual output.
Checklist:
Verification run locally
The change is confined to
transformer_lens/tools/analysis/jacobian_lens*, so the local run was scoped to the Jacobian-lens surface; CI re-runs the full suite across Python 3.10, 3.11, and 3.12.check-format: clean under the CI-pinned black 23.12.1 plus isort (profile=black) on all changed files.mypy: Success, no issues in the two analysis modules.tests/unit/tools/test_jacobian_lens.py,test_jacobian_lens_decomposition.py,tests/integration/test_jacobian_lens.py, and (docstring tier,--doctest-modules --doctest-plus) both analysis modules: 246 passed, 4 deselected (the deselected are the slowgemma-2-2b-itcases).build-docs(Sphinx): builds clean (exit 0) with notebooks excluded; the new page and API entries render. The only warnings touching the new dataclasses are the pre-existingduplicate object descriptionre-export pattern (identical toJSpaceDecomposition;-Wis disabled inconf.pyfor exactly this), so no new warning class is introduced. A full localbuild-docsadditionally needspandocfor the demo notebooks, absent on this box and present in CI.occupancyreturns a small positive integer with[0, 1]-bounded captured-variance curves, andfraction_of_variancereturns per-layermedianandpooledratios in[0, 1].