Skip to content

fix(FactoredMatrix): recompute cached SVD after in-place factor updates - #2073

Merged
jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
RClaudiuM:fix/2071-factored-matrix-svd-cache
Dec 1, 2026
Merged

jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
RClaudiuM:fix/2071-factored-matrix-svd-cache

Conversation

@RClaudiuM

@RClaudiuM RClaudiuM commented Nov 30, 2026 •

Copy link
Copy Markdown

Description

svd() cached its result for the lifetime of the instance, so after an in-place edit of a factor (a.mul_(3) in the issue) S, U, V, Vh, norm(), collapse_l/collapse_r and make_even() kept describing the old matrix while AB was current.

The cache now also stores the two factor tensors and their Tensor._version, and the SVD is recomputed when a factor is replaced or changed in place. The factors are broadcast_to views, and a view shares its base's version counter, so an edit through the original tensor or through fm.A/fm.B is both seen. Edits through .data don't bump the version and still aren't detected; the svd() docstring says so. Inference tensors (from torch.inference_mode()) have no version counter, so for those the cache only checks that the factor is the same tensor, as before this change. Repeated calls on unchanged factors still reuse the cached result, so test_svd_caches_per_instance is unchanged.

Fixes #2071

Type of change

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

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • 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
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

New tests in tests/unit/factored_matrix/test_properties.py: the repro from the issue, an edit through m.B, reassigning m.A, U/V/Vh/collapse_*/make_even after an edit, eigenvalues after an edit, and cache reuse when nothing changed. Five of them fail without the fix; the eigenvalues one already passed, since eigenvalues aren't cached.

A test under torch.inference_mode() covers factors without a version counter.

uv run pytest tests/unit/factored_matrix tests/unit/test_weight_processing.py: 309 passed, 1 skipped. black, isort and pycln are clean on the changed files, and uv run mypy . reports no issues.

svd() cached its result for the lifetime of the instance, so after an
in-place edit of a factor (e.g. a.mul_(3)) U, S, V, Vh, norm(),
collapse_l/r and make_even() kept returning values for the old matrix
while AB was current.

The cache now records the factor tensors and their Tensor._version and is
recomputed when either factor is replaced or modified in place. Views
created by broadcast_to share the base tensor's version counter, so edits
made through the original tensors or through fm.A / fm.B are both seen.
Edits made through .data bypass the version counter and are not detected.
The public API is unchanged and repeated calls on unchanged factors still
reuse the cached result.

Fixes TransformerLensOrg#2071

@jlarson4 jlarson4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! Two competing PRs for #2071 were submitted at roughly the same time, and we are going to move forward with this one. One comment that will need resolution before we can merge below, let me know if you have any questions

Comment thread transformer_lens/FactoredMatrix.py Outdated
A, B = self.A, self.B
if self._svd_cache is not None:
cA, vA, cB, vB, result = self._svd_cache
if cA is A and cB is B and vA == A._version and vB == B._version:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tensor._version raises on inference tensors, so svd() now fails for any factor created under torch.inference_mode(). bridge.OV.S and bridge.all_composition_scores() inside an inference-mode block both work today and raise with this change. Could you make svd() handle factors that have no version counter, with a test under torch.inference_mode()?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good catch. svd() now reads the version with t.is_inference() checked first: an inference tensor has no version counter, so for those factors the cache only checks that it's still the same tensor, which is how it behaved before this PR. An in-place edit of an inference-mode factor isn't detected; the docstring says so. Normal factors keep the version check, including when only one of the two factors is an inference tensor.

Added test_svd_with_inference_mode_factors: builds the factors under torch.inference_mode(), checks S and the reconstruction against a @ b, that the result is reused, and norm(), then reads m.S again after the block. It fails with the previous version of this PR ("Inference tensors do not track version counter") and passes now.

I also checked your two cases on a small randomly initialised GPT-2 bridge built with build_bridge_from_module: inside torch.inference_mode(), bridge.OV.S and bridge.all_composition_scores("Q") raised before this change and work now.

tests/unit/factored_matrix and test_weight_processing.py: 309 passed, 1 skipped; black, isort, pycln and mypy are clean.

Inference tensors have no version counter and Tensor._version raises on
them, so svd() failed for factors created under torch.inference_mode()
(e.g. OV.S and all_composition_scores() inside an inference-mode block).
For such a factor the cache now only checks that it is the same tensor,
as it did before the version check was added.
@jlarson4

jlarson4 commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the update @RClaudiuM, approved and merging

@jlarson4
jlarson4 merged commit 7a87a60 into TransformerLensOrg:dev Dec 1, 2026
31 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.

[Bug Report] FactoredMatrix.S and AB disagree after an in-place factor update

2 participants