Coverage for transformer_lens/model_bridge/supported_architectures/qwen3_next.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.10.1, created at 2026-09-21 19:27 +0000

1"""Qwen3Next architecture adapter. 

2 

3Hybrid linear-attention (GatedDeltaNet) + full-attention with sparse MoE MLP. 

43 linear-attn layers per 1 full-attn layer. Extends Qwen3 base with 

5optional attention mapping, MoE MLP, and fold_ln disabled. 

6""" 

7 

8from typing import Any 

9 

10from transformer_lens.model_bridge.generalized_components import ( 

11 LinearBridge, 

12 MoEBridge, 

13 MoERouterBridge, 

14) 

15from transformer_lens.model_bridge.supported_architectures.qwen3 import ( 

16 Qwen3ArchitectureAdapter, 

17) 

18 

19 

20class Qwen3NextArchitectureAdapter(Qwen3ArchitectureAdapter): 

21 """Hybrid linear-attention + full-attention with sparse MoE MLP. 

22 

23 Same hybrid design as Qwen3.5 but with MoE instead of dense MLP. 

24 """ 

25 

26 def __init__(self, cfg: Any) -> None: 

27 # q_proj stays 2x-wide through weight processing: HF and the attention bridge both 

28 # split [query|gate] per head at forward time; slicing the gate out changes outputs. 

29 setattr(cfg, "gated_q_proj", True) 

30 super().__init__(cfg, hybrid=True) 

31 

32 def _build_mlp_bridge(self): 

33 """Sparse MoE MLP (router + batched experts + shared expert).""" 

34 return MoEBridge( 

35 name="mlp", 

36 config=self.cfg, 

37 sparse_required=("gate",), 

38 submodules={ 

39 # Plain-tensor SparseMoeBlock: router observability comes from 

40 # the gate submodule. Dense fallback layers (mlp_only_layers / 

41 # decoder_sparse_step) bind the projections below (#1645). 

42 "gate": MoERouterBridge(name="gate", optional=True), 

43 "shared_expert": self._gated_mlp(name="shared_expert", optional=True), 

44 "shared_expert_gate": LinearBridge(name="shared_expert_gate", optional=True), 

45 "dense_gate": LinearBridge(name="gate_proj", optional=True), 

46 "dense_in": LinearBridge(name="up_proj", optional=True), 

47 "dense_out": LinearBridge(name="down_proj", optional=True), 

48 }, 

49 )