Skip to content

fix: MoE router hooks of GraniteMoe and Jamba on transformers >= 5.22 - #2064

Open
XiaoyuanWang111 wants to merge 1 commit into
TransformerLensOrg:devfrom
XiaoyuanWang111:fix/moe-routers-transformers-5-22-v2
Open

XiaoyuanWang111 wants to merge 1 commit into
TransformerLensOrg:devfrom
XiaoyuanWang111:fix/moe-routers-transformers-5-22-v2

Conversation

@XiaoyuanWang111

@XiaoyuanWang111 XiaoyuanWang111 commented Nov 27, 2026 •

Copy link
Copy Markdown

Description

Thanks for maintaining the bridge! transformers 5.22 (huggingface/transformers#49206) reordered the GraniteMoe-family router output to (router_logits, top_k_weights, top_k_index) and made Jamba's router a JambaTopKRouter that returns the same tuple. The Granite adapter still reads the old positions, so the router's hook_out receives the top-k indices and hook_expert_indices the logits; Jamba's router is still a plain LinearBridge, so its hook_out is handed the whole tuple. CI uses transformers 5.13 from the lock, so neither shows there.

On transformers >= 5.22 (a small helper in moe.py), the Granite adapter now uses MoERouterBridge's default positions and Jamba's router becomes a MoERouterBridge; older releases keep the current mapping.

Reproducer (dev, transformers 5.24.1, no download):

import torch
from transformers import GraniteMoeConfig, GraniteMoeForCausalLM

from transformer_lens.model_bridge.sources._bridge_builder import build_bridge_from_module

cfg = GraniteMoeConfig(
    vocab_size=128, hidden_size=64, intermediate_size=32, num_hidden_layers=2, num_attention_heads=4,
    num_key_value_heads=2, num_local_experts=4, num_experts_per_tok=2,
)
torch.manual_seed(0)
bridge = build_bridge_from_module(GraniteMoeForCausalLM(cfg).eval(), "GraniteMoeForCausalLM", hf_config=cfg).eval()
tokens = torch.tensor([[5, 6, 7, 8, 9, 10]])
_, cache = bridge.run_with_cache(tokens)
print("router hook_out:", cache["blocks.0.mlp.gate.hook_out"].dtype, tuple(cache["blocks.0.mlp.gate.hook_out"].shape))
print("hook_expert_indices:", cache["blocks.0.mlp.gate.hook_expert_indices"].dtype)
baseline = bridge(tokens)
bridge.add_hook("blocks.0.mlp.gate.hook_expert_indices", lambda t, hook=None: torch.zeros_like(t))
print("zeroing the indices changes the output:", not torch.equal(baseline, bridge(tokens)))

On dev this prints torch.int64 (6, 2), torch.float32 and False; with this change (and on 5.13) torch.float32 (6, 4), torch.int64 and True.

Tested with a new test_granite_moe_routing_hooks.py (fails on dev with 5.23 and 5.24, passes with this change on 5.13, 5.23 and 5.24) and two Jamba cases. SwitchTransformers' router changed order too; it uses another bridge, so I left it out, but I'm glad to add it.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

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

transformers 5.22 reordered the GraniteMoe-family router output to
(router_logits, top_k_weights, top_k_index) and replaced Jamba's nn.Linear
router with a JambaTopKRouter that returns the same tuple. The Granite
adapter still read the old positions, so the router's hook_out received the
top-k indices and an edit on hook_expert_indices did not re-route; Jamba's
router kept a LinearBridge, whose hook_out was handed the whole tuple.

A helper in moe.py checks for transformers >= 5.22. There the Granite
adapter uses MoERouterBridge's default positions and Jamba's router is a
MoERouterBridge; older releases keep the previous mapping.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant