Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
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
| 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: |
There was a problem hiding this comment.
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()?
There was a problem hiding this comment.
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.
|
Thanks for the update @RClaudiuM, approved and merging |
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_randmake_even()kept describing the old matrix whileABwas 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 arebroadcast_toviews, and a view shares its base's version counter, so an edit through the original tensor or throughfm.A/fm.Bis both seen. Edits through.datadon't bump the version and still aren't detected; thesvd()docstring says so. Inference tensors (fromtorch.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, sotest_svd_caches_per_instanceis unchanged.Fixes #2071
Type of change
Checklist:
New tests in
tests/unit/factored_matrix/test_properties.py: the repro from the issue, an edit throughm.B, reassigningm.A,U/V/Vh/collapse_*/make_evenafter 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, anduv run mypy .reports no issues.