Repository navigation
fix: per-head attention results on JetMoE's mixture of attention heads - #2007
Merged
jlarson4 merged 1 commit intoNov 9, 2026
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
On JetMoE, per-head attention results and every API built on them raise:
JetMoE's attention is a mixture of attention heads (
JetMoeMoA). Each token routes totop_kattention experts, and each expert has its own query and output projection:Expert slot
kowns headsk * n_kv .. (k + 1) * n_kv - 1. Its output projection and gate depend on the token's routing. There is noo_projforhook_zto alias, and no staticW_OforActivationCacheto multiplyhook_zby.Fix.
_JetMoeAttentionBridge).hook_zis a real hook point on the input ofexperts.reduce,[batch, pos, n_heads, d_head]. It is patchable: the reduce call reads the hooked value.gate · W_out[expert][:, head] @ z_head, with each slot's expert and gate from the attention router.head_results_from_cachecomputes them from the cachedhook_zand the router logits (attn.experts.router.hook_out), with the same top-k + softmax asJetMoeTopKGating.use_attn_result, the wrapped reduce computes them from HF's routing topology, fireshook_result, and returns their sum plus the MoA bias.supports_attn_result = True.b_Ois the MoA bias, so the decompositions'biasrow includes it.ActivationCache._ensure_head_results. If an attention bridge defineshead_results_from_cache, use it instead ofhook_z @ W_O. No other bridge defines it.On a tiny random JetMoE (3 layers, 4 attention experts, top-2, non-zero MoA bias), max abs errors:
devb_Ovshook_attn_outKeyErrorstack_head_results(incl_remainder=True)vsresid_postKeyErrorget_full_resid_decomposition(expand_neurons=False)KeyErroruse_attn_resultvs HFNotImplementedErrorLogits are unchanged without
use_attn_result(still exactly HF's), and greedygeneratematches HF.expand_neurons=Truestill fails on JetMoE's MoE MLP, as on the otherMoEBridgemodels.Type of change
Checklist:
I have commented my code, particularly in hard-to-understand areas
I have made corresponding changes to the documentation
My changes generate no new warnings
I have added tests that prove my fix is effective or that my feature works
New and existing unit tests pass locally with my changes
I have not rewritten tests relating to key interfaces which would affect backward compatibility
New
tests/integration/model_bridge/test_jetmoe_head_results.py(tiny randomJetMoeForCausalLM, built locally) checks that:hook_zhas the per-head shape;hook_zchanges the output, and an identity hook does not;b_Osum tohook_attn_out;resid_post;get_full_resid_decompositionsums to its target;use_attn_result, logits match HF andhook_resultequals the cache-computed results;generatematches HF.test_jetmoe_adapter.py:hook_zandsupports_attn_resulton the MoA bridge. A second test checks that_slot_routingrecovers each slot's top-k expert and gate from HF's expert-sorted routing topology, using a realJetMoeTopKGating.On
dev, 9 of these tests fail.Local runs (Windows, CPU, torch 2.9.1, transformers 5.20.2,
--jaxtyping-packages=transformer_lens,beartype.beartype,PYTHONUTF8=1):test_jetmoe_adapter.py: 16 passed.tests/unit -m "not slow": 8175 passed, 14 failed. The same 14 fail ondevin my environment (ten Gemma 3n adapter tests need the optionaltimm, threetest_sparse_probing.pytests check optimizer stop reasons, andtest_faithfulness_recovery_is_graded_not_binaryis [Bug Report] test_faithfulness_recovery_is_graded_not_binary passes or fails on the sign of float32 rounding #1993).tests/integration/model_bridge -k "resid or decompos or head_result or attn_result or post_norm": 388 passed. The 2 failures also fail ondevhere:test_jamba_adapter.py::test_uses_post_norm_B_C(nomixer.conv1dhooks in my transformers version) and an Ouro test whose remote code callscreate_causal_mask(input_embeds=...). The Gemma 3n errors needtimm. Three Raven tests errored on downloading the checkpoint and pass on a rerun.black(the locked 23.12.1),isort --check-onlyandmypyon the changed files pass. In my environment mypy reports three errors in other, unchanged files (_model_state.py,moe.py,olmo_hybrid.py); they also appear ondev.This was prepared with AI assistance (Claude); I reviewed and tested the change.