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
[Question] Desire for support for SPDA attention + compilation? #2032
TransformerBridgeBug specific to the new TransformerBridge systemenhancementNew feature or requesthigh-priorityMaintainers are interested in these issues being solved before others
I maintain the ARENA material and we use TransformerLens for several days of material. Often we switch from eager to SPDA attention, or compile the model for performance reasons to get a day of material to run as fast as possible on meagre hardware to give short feedback loops to participants.
I've got a somewhat hacky entirely vibecoded patch to add both SPDA attention support and the ability to compile TransformerLens models to speed things up. For now I plan to just have this as either an extra file in the ARENA material or atop a fork of TransformerLens.
Would there be any interest from you to support either of these? If so, I can try to PR a cleaner version.
Pleasure to hear from you, and we are happy to support anything that helps ARENA run more smoothly. If you've got the time, you are more than welcome to put together a PR. If you'd rather hand it off, I can take it on myself or pass it off to one of our excellent contributors who I know will be able to tackle it. Let me know what works best for you.
Hi @davidquarel, happy to help with this if it's useful. I'm on an M1 with 8 GB, which is pretty close to the "meagre hardware" case, so I could test your SDPA and compile patches on MPS and report eager vs SDPA vs compiled speed, plus whether logits and hooks still match eager. If you'd rather hand the PR off, I'd also be glad to take it on, building on your branches.
Hi @adityasingh-ops that would be great, from prelim tests I think that SDPA might not be worth the trouble (and the logits will be slightly different which might be undesirable), but the ability to compile is a much bigger deal. Currently I have to have a kind of awkward juggle between HuggingFace and TransformerLens models, TransformerBridge in TL4 helps (still in the process of porting over to TL4) but compilation that brings performance to ~near HF would be fantastic!
Thanks @davidquarel , makes sense to drop SDPA then. I had a first look at torch.compile on TransformerBridge (dev, gpt2, torch 2.11, transformers 5.13):
It already compiles and the outputs match eager (max logit diff ~1.6e-4), but torch._dynamo.explain gives 22 graphs / 21 graph breaks for the bridge vs 1 graph / 0 breaks for the plain HF model.
Most of the breaks come from GeneralizedComponent.__getattr__ (base.py:475). Dynamo can't trace object.__getattribute__(self, "__dict__"), so it falls back to eager for the whole wrapped call. The rest are at the hook points that end up just after it (hook_attn_scores, hook_pattern, hook_scale).
Because of that split, _reconstruct_attention gets compiled as its own frame and guards on the KV cache state for each layer index, so it hits dynamo's recompile limit (8) and the later layers run eager.
On the first call HF's output-capturing hooks take a threading.Lock (transformers/utils/output_capturing.py), which dynamo can't enter. A warm-up eager call avoids that one.
Plan I'd try:
Make __getattr__ (and anything else on the forward path) dynamo-friendly, aiming for 0 graph breaks on a no-hook forward for a few architectures (gpt2, llama, gemma), with a test that asserts the graph break count
Check that hooks still fire under compile (run_with_hooks / run_with_cache) and that the outputs match eager
Benchmark eager vs compiled vs HF on a GPU, since CPU timings don't say much here
Before I start: for ARENA, is the main case plain forward passes, or do you need compiled runs with hooks attached (e.g. run_with_cache inside a loop)? Hooks that run arbitrary Python will probably always cause some breaks, so I'd like to know which path matters most.
I agree with the decision to drop SDPA. Ideally, we can take what @davidquarel has already done in this PR and merge it into dev in the main repo. @adityasingh-ops can then start from there handling the additional aspects of this task. No reason to redo what has already been done.
Once that is taken care of, these are the items I have identified that need addressing:
The threading.Lock break survives the patch on a model booted by name, on the first call only. A model passed through hf_model= doesn't hit it, which is why the branch's tests don't see it.
A backward hook added after compiling never fires. len(self.bwd_hooks) next to the fwd_hooks line in HookPoint.forward fixes it.
The graph-break tests should compare against the same HF model unbridged rather than assert exactly 0, so a transformers release can't fail our CI.
MPS and low-memory test runs in addition to the GPT-2 check I did.
Let me know if you have any questions, would like to adjust who is in charge of what, or if there is anything I missed.
Thanks @jlarson4, sounds good, happy to take these four. For the lock, my plan is to install HF's output-capturing hooks when the bridge is booted, so the first compiled call doesn't hit maybe_install_capturing_hooks, and to add a boot-by-name test that catches it. I can run everything on an M1 with 8 GB for the MPS and low-memory checks.
Should I branch off @davidquarel's PR now and rebase once it lands in dev, or wait for it to be merged first?
@adityasingh-ops apologies for the delayed response. Feel free to branch off @davidquarel's PR, if you PR the main repo before he does, I will make sure he is credited as a co-author
Before I start: for ARENA, is the main case plain forward passes, or do you need compiled runs with hooks attached (e.g. run_with_cache inside a loop)?
If I'm only doing forward passes, then there's no real reason to use TransformerLens at all and I can just default to using the HF model. It's runs with attached hooks that are sometimes the expensive part (especially on the day where we train an SAE against a TransformerLens model)
Thanks both! Starting from @davidquarel's branch now. I'll focus on the hooked path (run_with_cache / run_with_hooks under compile), since that's what matters for ARENA, along with the four items above, and post numbers for compiled hooked runs on MPS.
@davidquarel your PR is now merged! @adityasingh-ops, whenever your PR is placed on dev, it may need a rebase to properly merge. Let me know if you run into any issues. Thanks!
Opened #2070 for this: your compile commit plus fixes for the four items, and run_with_cache now compiles with 0 graph breaks on GPT-2. MPS numbers are in the description.
TransformerBridgeBug specific to the new TransformerBridge systemenhancementNew feature or requesthigh-priorityMaintainers are interested in these issues being solved before others
Activity
jlarson4 commentedon Nov 19, 2026
Hi @davidquarel!
Pleasure to hear from you, and we are happy to support anything that helps ARENA run more smoothly. If you've got the time, you are more than welcome to put together a PR. If you'd rather hand it off, I can take it on myself or pass it off to one of our excellent contributors who I know will be able to tackle it. Let me know what works best for you.
Thank you,
Jonah Larson
adityasingh-ops commentedon Nov 21, 2026
Hi @davidquarel, happy to help with this if it's useful. I'm on an M1 with 8 GB, which is pretty close to the "meagre hardware" case, so I could test your SDPA and compile patches on MPS and report eager vs SDPA vs compiled speed, plus whether logits and hooks still match eager. If you'd rather hand the PR off, I'd also be glad to take it on, building on your branches.
davidquarel commentedon Nov 23, 2026
Hi @adityasingh-ops that would be great, from prelim tests I think that SDPA might not be worth the trouble (and the logits will be slightly different which might be undesirable), but the ability to compile is a much bigger deal. Currently I have to have a kind of awkward juggle between HuggingFace and TransformerLens models, TransformerBridge in TL4 helps (still in the process of porting over to TL4) but compilation that brings performance to ~near HF would be fantastic!
adityasingh-ops commentedon Nov 23, 2026
Thanks @davidquarel , makes sense to drop SDPA then. I had a first look at
torch.compileonTransformerBridge(dev, gpt2, torch 2.11, transformers 5.13):torch._dynamo.explaingives 22 graphs / 21 graph breaks for the bridge vs 1 graph / 0 breaks for the plain HF model.GeneralizedComponent.__getattr__(base.py:475). Dynamo can't traceobject.__getattribute__(self, "__dict__"), so it falls back to eager for the whole wrapped call. The rest are at the hook points that end up just after it (hook_attn_scores,hook_pattern,hook_scale)._reconstruct_attentiongets compiled as its own frame and guards on the KV cache state for each layer index, so it hits dynamo's recompile limit (8) and the later layers run eager.threading.Lock(transformers/utils/output_capturing.py), which dynamo can't enter. A warm-up eager call avoids that one.Plan I'd try:
__getattr__(and anything else on the forward path) dynamo-friendly, aiming for 0 graph breaks on a no-hook forward for a few architectures (gpt2, llama, gemma), with a test that asserts the graph break countrun_with_hooks/run_with_cache) and that the outputs match eagerBefore I start: for ARENA, is the main case plain forward passes, or do you need compiled runs with hooks attached (e.g.
run_with_cacheinside a loop)? Hooks that run arbitrary Python will probably always cause some breaks, so I'd like to know which path matters most.jlarson4 commentedon Nov 24, 2026
Hi @davidquarel & @adityasingh-ops!
I agree with the decision to drop SDPA. Ideally, we can take what @davidquarel has already done in this PR and merge it into
devin the main repo. @adityasingh-ops can then start from there handling the additional aspects of this task. No reason to redo what has already been done.Once that is taken care of, these are the items I have identified that need addressing:
threading.Lockbreak survives the patch on a model booted by name, on the first call only. A model passed throughhf_model=doesn't hit it, which is why the branch's tests don't see it.len(self.bwd_hooks)next to thefwd_hooksline inHookPoint.forwardfixes it.Let me know if you have any questions, would like to adjust who is in charge of what, or if there is anything I missed.
adityasingh-ops commentedon Nov 24, 2026
Thanks @jlarson4, sounds good, happy to take these four. For the lock, my plan is to install HF's output-capturing hooks when the bridge is booted, so the first compiled call doesn't hit
maybe_install_capturing_hooks, and to add a boot-by-name test that catches it. I can run everything on an M1 with 8 GB for the MPS and low-memory checks.Should I branch off @davidquarel's PR now and rebase once it lands in
dev, or wait for it to be merged first?jlarson4 commentedon Nov 26, 2026
@adityasingh-ops apologies for the delayed response. Feel free to branch off @davidquarel's PR, if you PR the main repo before he does, I will make sure he is credited as a co-author
davidquarel commentedon Nov 26, 2026
@adityasingh-ops
If I'm only doing forward passes, then there's no real reason to use TransformerLens at all and I can just default to using the HF model. It's runs with attached hooks that are sometimes the expensive part (especially on the day where we train an SAE against a TransformerLens model)
adityasingh-ops commentedon Nov 26, 2026
Thanks both! Starting from @davidquarel's branch now. I'll focus on the hooked path (
run_with_cache/run_with_hooksunder compile), since that's what matters for ARENA, along with the four items above, and post numbers for compiled hooked runs on MPS.jlarson4 commentedon Nov 27, 2026
@davidquarel your PR is now merged! @adityasingh-ops, whenever your PR is placed on
dev, it may need a rebase to properly merge. Let me know if you run into any issues. Thanks!adityasingh-ops commentedon Nov 30, 2026
Opened #2070 for this: your compile commit plus fixes for the four items, and
run_with_cachenow compiles with 0 graph breaks on GPT-2. MPS numbers are in the description.