[Bug Report] NormMatchedPlacebo silently loses norm matching and produces float16 NaNs

已关闭
#1,958 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@emerardd 已经在做这个了。

开始于 2026年11月2日。

  • #1959 来自 @emerardd —— 已合并

评估

难度
3/5
预计耗时
1-2 天
新手友好度
75/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python, pytorch
领域
tooling

调研方向

Start in transformer_lens/tools/analysis/interchange.py at NormMatchedPlacebo.__call__. Reproduce with the provided torch script to see float16 NaNs and zero-projection silence. Edit to preserve zero-reference rows, normalize at safe precision, and raise on zero placebo projection with nonzero reference. Run the focused regression tests (8 failing) until they pass and new identity/error cases hold.

由索引模型根据 Issue 内容生成。

描述

Describe the bug

NormMatchedPlacebo promises to match the reference intervention's displacement in a different subspace, but a zero projection silently produces no displacement in float32 even when the reference moves. In float16, the 1e-8 denominator clamp rounds to zero and produces NaNs, including when source and base are identical. This can occur with the documented random-neuron control excluding the reference neurons, not just a degenerate dense random basis.

Code example

import torch
from transformer_lens.tools.analysis.interchange import (
    NormMatchedPlacebo, SubspaceInterchange, neuron_basis, random_neuron_basis,
)

for dtype in (torch.float32, torch.float16):
    base = torch.zeros(2, 4, dtype=dtype)
    source = base.clone()
    source[0, 0] = 2
    reference = SubspaceInterchange(neuron_basis(4, [0]))
    control_basis = random_neuron_basis(4, 1, seed=0, exclude=[0])
    result = NormMatchedPlacebo(reference, control_basis)(base, source)
    print(dtype, (reference(base, source) - base).norm(dim=-1), result)

The reference displacement norms are [2, 0]. Float32 returns two unchanged rows, so the first row is not norm-matched. Float16 returns NaNs in both rows; the second row should simply be an identity control. The current implementation also forms a potentially overflowing low-precision scale factor before multiplying by the direction.

Expected behavior: safely preserve rows with zero reference displacement, normalize nonzero projected directions at safe precision, and explicitly reject a zero placebo projection when the reference displacement is nonzero rather than silently returning an unmatched control. This keeps the existing projected-direction definition without inventing a fallback direction.

System Info

Source checkout at dev commit a7b3213a, Windows, Python 3.12, existing uv project environment, CPU. No model loading or downloads are required. Focused regression tests on this base: 8 failed, 14 passed, 38 deselected, covering float16, bfloat16, float32 and float64. Four failures assert the proposed explicit-error contract; the other four expose current float16 NaNs.

Additional context

The affected implementation is transformer_lens/tools/analysis/interchange.py, NormMatchedPlacebo.__call__. Existing random dense float32 tests cover ordinary nonzero projections, but not the zero-projection or float16 identity cases. This concerns the numerical validity of the control; no pretrained-model accuracy claim is involved.

Checklist
  • I have checked that there is no similar issue in the repo.
主要语言
Python
星标
3.9k
派生
708
平均合并
1 天 4 小时
30 天内合并 PR
87

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

  • 没有 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TransformerLensOrg/TransformerLens 的其他 Issue

查看 TransformerLensOrg/TransformerLens 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。