[Bug Report] FactoredMatrix SVD, composition scores and SVDInterpreter fail on bf16/fp16 bridges

Đã đóng
#1,918 2 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@tivris đang làm issue này rồi.

Từ ngày 20/10/2026.

Đánh giá

Issue này chưa được đánh giá.

Mô tả

Describe the bug

On a bf16 or fp16 bridge, FactoredMatrix.svd() fails, because torch.linalg.svd does not support these dtypes. This affects .S, .U, .V, .norm(), .make_even(), collapse_l() and collapse_r() on OV and QK, all_composition_scores(), and SVDInterpreter.get_singular_vectors(). eigenvalues on the same matrices works, since it casts 16-bit inputs to float32 first (#1155).

Code example

import torch
from transformer_lens import SVDInterpreter
from transformer_lens.model_bridge import TransformerBridge

bridge = TransformerBridge.boot_transformers("roneneldan/TinyStories-1M", device="cpu", dtype=torch.bfloat16)
bridge.OV[0, 0].eigenvalues  # works
bridge.OV[0, 0].S
# NotImplementedError: "linalg_svd_cpu" not implemented for 'BFloat16'
bridge.all_composition_scores("Q")  # same error
SVDInterpreter(bridge).get_singular_vectors("w_in", layer_index=0)  # same error

fp16 fails the same way ('Half'). On a float64 bridge, SVDInterpreter.get_singular_vectors() fails differently: RuntimeError: expected m1 and m2 to have the same dtype, but got: float != double.

System Info

  • Source checkout installed with uv, dev at 2ad49297.
  • macOS 27.2, Apple M5; Python 3.12.14; PyTorch 2.11.0; transformers 5.13.0; CPU.

Additional context

FactoredMatrix._svd_cached passes self.A and self.B to torch.linalg.svd in their stored dtype (transformer_lens/FactoredMatrix.py:228-229). eigenvalues casts bf16/fp16 to float32 before torch.linalg.eig (:281-283). SVDInterpreter runs torch.linalg.svd on the raw w_in and w_out matrices (transformer_lens/SVDInterpreter.py:105, :109), and its readout casts the singular vector with .float() but not the unembedding (:126), which also breaks float64.

Proposed fix: cast 16-bit factors to float32 in _svd_cached, as eigenvalues does; do the same for the two SVDInterpreter SVDs; and compute the readout in the singular vectors' dtype without changing stored weights.

Checklist
  • I have checked that there is no similar issue in the repo.
Ngôn ngữ chính
Python
Star
3.9k
Fork
708
Merge trung bình
1 ngày 12 giờ
Pull request đã merge (30 ngày)
79

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của TransformerLensOrg/TransformerLens

Tất cả issue của TransformerLensOrg/TransformerLens

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.