Skip to content

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

In review

Description

@JunkaiWang-TheoPhy

Describe the bug

When a caller updates a factor tensor in place, FactoredMatrix.AB sees the new weights but FactoredMatrix.S continues reporting the singular values from the first inspection. This can affect comparisons before and after an intervention if the same object is used for both measurements.

Code example

import torch
from transformer_lens.FactoredMatrix import FactoredMatrix

a = torch.eye(2)
m = FactoredMatrix(a, torch.eye(2))
print(m.S.tolist())                           # [1.0, 1.0]
a.mul_(3)
print(m.S.tolist())                           # [1.0, 1.0]
print(torch.linalg.svdvals(m.AB).tolist())     # [3.0, 3.0]
print(FactoredMatrix(*m.pair).S.tolist())      # [3.0, 3.0]

System info

Source 402647f, macOS arm64, Python 3.12, PyTorch 2.15.0 CPU. Tested the source modules directly, without loading a model.

Additional context

The constructor keeps broadcast views of the factors, while the first SVD result is cached for the instance lifetime. Reconstructing the object from the same factors restores agreement. If mutable factor tensors are supported, both access patterns should report the current spectrum. If immutable inputs are intended, documenting that constraint and the reconstruction workaround would help users avoid stale measurements.

  • Checked for a similar issue.

Activity

  1. added
    bug
    help wanted
    good first issue
    complexity-moderate
    TransformerBridge
    on Nov 30, 2026
  2. outerheaven199X commented on Dec 1, 2026

    @outerheaven199X

    Hey Thomas, hope your week's going alright.

    I checked FactoredMatrix.py and that matches what the code is doing. _svd_cached uses @cached_property (originally moved off lru_cache to keep the cache on the instance), so S, U, V, Vh, svd(), norm(), make_even(), collapse_l(), and collapse_r() all read self.__dict__["_svd_cached"] once it is populated, while AB, BA, T, eigenvalues, get_corner(), and pair recompute from self.A and self.B on every access.

    In transformer_bridge.py, QK and OV build a new FactoredMatrix on each property read, so callers usually only hit this when holding a FactoredMatrix instance across an in-place edit. If maintainers decide to invalidate the cache on mutation rather than treating factors as immutable, two details in FactoredMatrix are worth keeping in mind:

    • FactoredMatrix.__init__ also accepts TensorLike duck types whose protocol doesn't include _version, so any version check needs a fallback like getattr(..., "_version", None) (though svd() itself requires torch.Tensor).
    • Storing (self.A, self.B) identity alongside (self.A._version, self.B._version) also covers callers that reassign m.A or m.B directly, not just in-place ops.
  3. added a commit that references this issue on Dec 1, 2026
    7a87a60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    TransformerBridgebugcomplexity-moderategood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions