[Bug Report] `remove_batch_dim=True` with batch `size > 1`: the three caching paths disagree
メンテナーはふだん 1 日以内に返信
@Mudassiruddin7 がすでに取り組んでいます。
2026年10月6日 から。
評価
この issue はまだ評価されていません。
説明
Describe the bug
The docstrings say remove_batch_dim "only makes sense with batch_size=1 inputs", but only one of the three caching paths enforces that; the other two silently do different wrong things on batch > 1:
run_with_cache(..., remove_batch_dim=True)(ActivationCache path): raisesAssertionError: Cannot remove batch dimension from cache with batch size 2. This is loud and arguably the right behavior.run_with_cache(..., remove_batch_dim=True, return_cache_object=False): silently ignoresremove_batch_dim, activations keep their batch dim.get_caching_hooks(remove_batch_dim=True)/add_caching_hooks:save_hookdoesstored[0], silently discarding every example after the first. This is bad, as it quietly abandons data.
Code example
import torch
from transformer_lens.config import TransformerBridgeConfig
from transformer_lens.model_bridge import TransformerBridge
cfg = TransformerBridgeConfig(
d_model=32, d_head=16, n_heads=2, n_layers=1, n_ctx=8, d_vocab=16,
d_mlp=64, act_fn="gelu", normalization_type="LN", seed=0,
)
bridge = TransformerBridge.boot_native(cfg)
tokens = torch.randint(0, cfg.d_vocab, (2, 6))
name = "blocks.0.hook_out"
with torch.no_grad():
try:
bridge.run_with_cache(tokens, names_filter=name, remove_batch_dim=True)
except AssertionError as e:
print("ActivationCache:", e)
_, plain = bridge.run_with_cache(
tokens, names_filter=name, remove_batch_dim=True, return_cache_object=False
)
print("plain dict:", tuple(plain[name].shape))
cache, fwd_hooks, _ = bridge.get_caching_hooks(names_filter=name, remove_batch_dim=True)
with torch.no_grad(), bridge.hooks(fwd_hooks=fwd_hooks):
bridge.forward(tokens)
print("get_caching_hooks:", tuple(cache[name].shape))
Output on dev (1012730f):
ActivationCache: Cannot remove batch dimension from cache with batch size 2
plain dict: (2, 6, 32)
get_caching_hooks: (6, 32)
Expected: one behavior across all three – the ActivationCache assert is the obvious candidate (an assert tensor.size(0) == 1 in save_hook and in the return_cache_object=False squeeze). EDIT: We have made some slight adjustments to this expected behavior, see discussion below
System Info
- Source checkout installed with
uv sync,devat1012730f. - macOS (Apple Silicon), CPU only; Python 3.12.12; torch 2.11.0; transformers 5.13.0.
- No model downloads needed: reproduced with a random-init
TransformerBridge.boot_nativemodel.
Additional context
Found while verifying #1856. The silent [0] is inherited from the legacy HookedRootModule.get_caching_hooks, which has the same behavior. A fix should probably touch both.
Checklist
- I have checked that there is no similar issue in the repo (required)
- 主要言語
- Python
- スター
- 3.9k
- フォーク
- 708
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 65
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
TransformerLensOrg/TransformerLens のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
TransformerLensOrg/TransformerLens#1868 ·
メンテナーはふだん 1 日以内に返信
-
[Bug Report] run_with_hooks(remove_batch_dim=True) raises AttributeError when a hook returns None対応中かも @Mudassiruddin7 が 1 日前に担当しました。 オープンbug complexity-simple TransformerBridge
TransformerLensOrg/TransformerLens#1863 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
[Bug Report] `get_caching_hooks` fires for alias names in `names_filter` but caches only under the canonical key対応中かも @Mudassiruddin7 が 2 日前に担当しました。 オープンbug complexity-moderate TransformerBridge
TransformerLensOrg/TransformerLens#1857 · コメント 2 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
[Bug Report] get_caching_hooks slices the wrong axis when remove_batch_dim=True is combined with pos_slice対応中かも @JoeyTan21 が 2 日前に担当しました。 オープン
TransformerLensOrg/TransformerLens#1856 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
[Bug Report] Inspect RemoteBridge silently ignores attention_mask対応中かも @emerardd が 3 日前に担当しました。 オープンbug complexity-moderate TransformerBridge
TransformerLensOrg/TransformerLens#1851 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
TransformerLensOrg/TransformerLens の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
UKGovernmentBEIS/inspect_ai#5781 ·
メンテナーはふだん 2 日以内に返信
-
Bump .cicd to wamp-cicd 4c2f9ac: `just land` refuses open A18 decisions, `just where` lists themオープン
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
crossbario/cfxdb#139 ·
-
Bump .cicd to wamp-cicd 4c2f9ac: `just land` refuses open A18 decisions, `just where` lists themオープン
難易度 1/5 1時間未満 初心者へのやさしさ 84/100
crossbario/txaio#241 ·
-
UX
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
mediajunkie/piper-morgan-product#1963 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100