Skip to content

fix: preserve selected J-space directions across atom scales - #1942

Merged
jlarson4 merged 1 commit into
TransformerLensOrg:devfrom
emerardd:fix/jspace-span-projection
Oct 23, 2026
Merged

jlarson4 merged 1 commit into
TransformerLensOrg:devfrom
emerardd:fix/jspace-span-projection

Conversation

@emerardd

Copy link
Copy Markdown
Contributor

Description

The float32 default torch.linalg.pinv rank cutoff in sparse J-space projections depends on the ambient dimension and raw atom scales. It can discard a genuinely independent selected direction even when the normalized atoms are orthogonal.

A model-free example uses two orthogonal atoms with norms 1 and 3000 and a target with coordinates [2, 1] in 4096 dimensions. NNLS selects both atoms and reconstructs [2, 1], but the reported J-space component is [0, 1]. Its supposedly orthogonal residual still has a dot product of 2 with the first atom. Occupancy reports gains [0.8, -0.6] instead of [0.8, 0.2]. The same problem in a low-dimensional space, or with unit-norm atoms, works correctly.

This change shares one selected-span projection between decomposition, occupancy and precomputed coordinate-patch validation. It normalizes atom columns and uses a float64 SVD with an explicit sqrt(eps64) relative rank tolerance. Numerical rank no longer depends on zero-padding the ambient dimension or rescaling an atom. Exactly dependent and numerically indistinguishable directions are removed under the documented tolerance. Output dtype/device are preserved, using the existing CPU fallback for MPS linear algebra.

This does not change pursuit selection, NNLS coefficient fitting or the public API. It corrects the selected-span projection and its reuse validation. The reproductions establish the mathematical failure; its prevalence in pretrained lens dictionaries has not been measured.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Validation

Regression coverage includes orthogonal reconstruction/projection identity, residual orthogonality, occupancy gains, atom-rescaling and zero-padding invariance, explicit near-dependency behavior, redundant/empty spans, valid/invalid precomputed projections, and the public JacobianLens.fraction_of_variance path.

  • Before the fix, the initial regression file had 3 failed / 6 passed.
  • The five affected Jacobian-lens test files pass: 420 passed.
  • Final regression-file rerun after the additional invalid-projection assertion: 16 passed.
  • Pinned pycln/isort/Black checks on the three changed files and git diff --check: passed.
  • Full mypy .: 389 source files passed.

The complete unit suite and GPU/MPS hardware checks were not run.

Checklist

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (API docstrings)
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (affected tests only; full suite not run)
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

@jlarson4
jlarson4 merged commit 34c5d77 into TransformerLensOrg:dev Oct 23, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants