Skip to content

[Question] Desire for support for SPDA attention + compilation? #2032

In review

Description

@davidquarel

Question

Hi TransformerLens maintainers,

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.

Thanks,
David Quarel

Activity

  1. jlarson4 commented on Nov 19, 2026

    @jlarson4
    Collaborator

    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

  2. added
    enhancement
    high-priority
    TransformerBridge
    on Nov 19, 2026
  3. adityasingh-ops commented on Nov 21, 2026

    @adityasingh-ops

    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.

  4. davidquarel commented on Nov 23, 2026

    @davidquarel
    Author

    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!

  5. adityasingh-ops commented on Nov 23, 2026

    @adityasingh-ops

    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:

    1. 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
    2. Check that hooks still fire under compile (run_with_hooks / run_with_cache) and that the outputs match eager
    3. 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.

  6. jlarson4 commented on Nov 24, 2026

    @jlarson4
    Collaborator

    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 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.

  7. adityasingh-ops commented on Nov 24, 2026

    @adityasingh-ops

    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?

  8. jlarson4 commented on Nov 26, 2026

    @jlarson4
    Collaborator

    @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

  9. davidquarel commented on Nov 26, 2026

    @davidquarel
    Author

    @adityasingh-ops

    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)

  10. adityasingh-ops commented on Nov 26, 2026

    @adityasingh-ops

    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.

  11. jlarson4 commented on Nov 27, 2026

    @jlarson4
    Collaborator

    @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!

  12. adityasingh-ops commented on Nov 30, 2026

    @adityasingh-ops

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    TransformerBridgeenhancementhigh-priority

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions