You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug Report] FactoredMatrix.S and AB disagree after an in-place factor update #2071
TransformerBridgeBug specific to the new TransformerBridge systembugSomething isn't workingcomplexity-moderateModerately complicated issues for people who have intermediate experience with the codegood first issueGood for newcomershelp wantedExtra attention is needed
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.
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.
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.
TransformerBridgeBug specific to the new TransformerBridge systembugSomething isn't workingcomplexity-moderateModerately complicated issues for people who have intermediate experience with the codegood first issueGood for newcomershelp wantedExtra attention is needed
Activity
outerheaven199X commentedon Dec 1, 2026
Hey Thomas, hope your week's going alright.
I checked FactoredMatrix.py and that matches what the code is doing.
_svd_cacheduses@cached_property(originally moved offlru_cacheto keep the cache on the instance), soS,U,V,Vh,svd(),norm(),make_even(),collapse_l(), andcollapse_r()all readself.__dict__["_svd_cached"]once it is populated, whileAB,BA,T,eigenvalues,get_corner(), andpairrecompute fromself.Aandself.Bon every access.In transformer_bridge.py,
QKandOVbuild a newFactoredMatrixon each property read, so callers usually only hit this when holding aFactoredMatrixinstance across an in-place edit. If maintainers decide to invalidate the cache on mutation rather than treating factors as immutable, two details inFactoredMatrixare worth keeping in mind:FactoredMatrix.__init__also acceptsTensorLikeduck types whose protocol doesn't include_version, so any version check needs a fallback likegetattr(..., "_version", None)(thoughsvd()itself requirestorch.Tensor).(self.A, self.B)identity alongside(self.A._version, self.B._version)also covers callers that reassignm.Aorm.Bdirectly, not just in-place ops.fix(FactoredMatrix): recompute cached SVD after in-place factor updat…