Defuser 0.0.29__tar.gz → 0.0.31__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {defuser-0.0.29 → defuser-0.0.31}/Defuser.egg-info/PKG-INFO +2 -2
- {defuser-0.0.29 → defuser-0.0.31}/PKG-INFO +2 -2
- {defuser-0.0.29 → defuser-0.0.31}/README.md +1 -1
- {defuser-0.0.29 → defuser-0.0.31}/defuser/model_registry.py +19 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/unfused_moe/common.py +14 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/unfused_moe/glm4_moe.py +13 -4
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/unfused_moe/glm4_moe_lite.py +10 -2
- {defuser-0.0.29 → defuser-0.0.31}/pyproject.toml +1 -1
- {defuser-0.0.29 → defuser-0.0.31}/tests/test_candidate_coverage.py +12 -0
- {defuser-0.0.29 → defuser-0.0.31}/tests/test_convert_model.py +145 -0
- {defuser-0.0.29 → defuser-0.0.31}/tests/test_meta_model_defusion.py +65 -0
- {defuser-0.0.29 → defuser-0.0.31}/Defuser.egg-info/SOURCES.txt +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/Defuser.egg-info/dependency_links.txt +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/Defuser.egg-info/requires.txt +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/Defuser.egg-info/top_level.txt +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/LICENSE +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/__init__.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/checkpoint_ops.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/defuser.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/__init__.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/glm4v.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/model_patches.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/moe_experts_interface.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/replace_modules.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/runtime_defusion.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/unfused_moe/__init__.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/unfused_moe/mixtral.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/unfused_moe/qwen2_moe.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/unfused_moe/qwen3_moe.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/unfused_moe/qwen3_next.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/unfused_moe/qwen3_omni_moe.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/modeling/update_module.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/utils/__init__.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/utils/common.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/utils/device.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/utils/hf.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/defuser/utils/model.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/setup.cfg +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/setup.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/tests/test_device_utils.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/tests/test_filter_rules.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/tests/test_moe_experts_interface.py +0 -0
- {defuser-0.0.29 → defuser-0.0.31}/tests/test_replace_modules_tracker.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: Defuser
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.31
|
|
4
4
|
Summary: Model defuser helper for HF Transformers.
|
|
5
5
|
Author-email: ModelCloud <qubitium@modelcloud.ai>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -93,7 +93,7 @@ Defuser currently supports the following `transformers>=5.3.0` `model_type` valu
|
|
|
93
93
|
| Pattern | Supported model types | Defused op performed ⚙️ |
|
|
94
94
|
| --- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| --- |
|
|
95
95
|
| Standard routed expert tensors 🧱 | `afmoe`, `bailing_hybrid`, `deepseek_v2`, `diffusion_gemma`, `dots1`, `ernie4_5_moe`, `ernie4_5_vl_moe`, `exaone_moe`, `flex_olmo`, `glm4_moe_lite`, `glm4v_moe`, `hunyuan_v1_moe`, `jamba`, `laguna`, `lfm2_moe`, `minimax`, `minimax_m2`, `olmoe`, `qwen3_vl_moe`, `solar_open`, `solar_open2` | Splits fused expert tensors or registered expert buffers into numbered expert `nn.Linear` modules with per-expert `gate_proj`, `up_proj`, and `down_proj`. |
|
|
96
|
-
| Mixed sparse and shared experts | `deepseek_v3`, `deepseek_v32`, `deepseek_v4`, `glm5_next`, `glm_moe_dsa`, `qwen3_5_moe`, `qwen3_5_moe_text`, `qwen4_exp`, `qwen4_exp_text` | Runtime expert tensor defusion for routed experts while preserving the model's shared-expert path. |
|
|
96
|
+
| Mixed sparse and shared experts | `deepseek_v3`, `deepseek_v32`, `deepseek_v4`, `glm5_next`, `glm_moe_dsa`, `granitemoe_swa`, `qwen3_5_moe`, `qwen3_5_moe_text`, `qwen4_exp`, `qwen4_exp_text` | Runtime expert tensor defusion for routed experts while preserving the model's shared-expert path. |
|
|
97
97
|
| Transposed or packed expert tensors | `gpt_oss`, `phimoe` | Splits transposed fused expert `gate_up_proj` tensors into per-expert `gate_proj` + `up_proj`, preserves expert bias when present, and converts expert tensors into numbered expert `nn.Linear` modules. |
|
|
98
98
|
| Flattened expert layout | `dbrx` | Rebuilds the flattened DBRX expert FFN weights into numbered expert `gate_proj`, `up_proj`, and `down_proj` `nn.Linear` modules. |
|
|
99
99
|
| Batched expert-input execution | `llama4` | Runtime expert tensor defusion plus preservation of the llama4 batched expert-input execution contract. |
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: Defuser
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.31
|
|
4
4
|
Summary: Model defuser helper for HF Transformers.
|
|
5
5
|
Author-email: ModelCloud <qubitium@modelcloud.ai>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -93,7 +93,7 @@ Defuser currently supports the following `transformers>=5.3.0` `model_type` valu
|
|
|
93
93
|
| Pattern | Supported model types | Defused op performed ⚙️ |
|
|
94
94
|
| --- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| --- |
|
|
95
95
|
| Standard routed expert tensors 🧱 | `afmoe`, `bailing_hybrid`, `deepseek_v2`, `diffusion_gemma`, `dots1`, `ernie4_5_moe`, `ernie4_5_vl_moe`, `exaone_moe`, `flex_olmo`, `glm4_moe_lite`, `glm4v_moe`, `hunyuan_v1_moe`, `jamba`, `laguna`, `lfm2_moe`, `minimax`, `minimax_m2`, `olmoe`, `qwen3_vl_moe`, `solar_open`, `solar_open2` | Splits fused expert tensors or registered expert buffers into numbered expert `nn.Linear` modules with per-expert `gate_proj`, `up_proj`, and `down_proj`. |
|
|
96
|
-
| Mixed sparse and shared experts | `deepseek_v3`, `deepseek_v32`, `deepseek_v4`, `glm5_next`, `glm_moe_dsa`, `qwen3_5_moe`, `qwen3_5_moe_text`, `qwen4_exp`, `qwen4_exp_text` | Runtime expert tensor defusion for routed experts while preserving the model's shared-expert path. |
|
|
96
|
+
| Mixed sparse and shared experts | `deepseek_v3`, `deepseek_v32`, `deepseek_v4`, `glm5_next`, `glm_moe_dsa`, `granitemoe_swa`, `qwen3_5_moe`, `qwen3_5_moe_text`, `qwen4_exp`, `qwen4_exp_text` | Runtime expert tensor defusion for routed experts while preserving the model's shared-expert path. |
|
|
97
97
|
| Transposed or packed expert tensors | `gpt_oss`, `phimoe` | Splits transposed fused expert `gate_up_proj` tensors into per-expert `gate_proj` + `up_proj`, preserves expert bias when present, and converts expert tensors into numbered expert `nn.Linear` modules. |
|
|
98
98
|
| Flattened expert layout | `dbrx` | Rebuilds the flattened DBRX expert FFN weights into numbered expert `gate_proj`, `up_proj`, and `down_proj` `nn.Linear` modules. |
|
|
99
99
|
| Batched expert-input execution | `llama4` | Runtime expert tensor defusion plus preservation of the llama4 batched expert-input execution contract. |
|
|
@@ -67,7 +67,7 @@ Defuser currently supports the following `transformers>=5.3.0` `model_type` valu
|
|
|
67
67
|
| Pattern | Supported model types | Defused op performed ⚙️ |
|
|
68
68
|
| --- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| --- |
|
|
69
69
|
| Standard routed expert tensors 🧱 | `afmoe`, `bailing_hybrid`, `deepseek_v2`, `diffusion_gemma`, `dots1`, `ernie4_5_moe`, `ernie4_5_vl_moe`, `exaone_moe`, `flex_olmo`, `glm4_moe_lite`, `glm4v_moe`, `hunyuan_v1_moe`, `jamba`, `laguna`, `lfm2_moe`, `minimax`, `minimax_m2`, `olmoe`, `qwen3_vl_moe`, `solar_open`, `solar_open2` | Splits fused expert tensors or registered expert buffers into numbered expert `nn.Linear` modules with per-expert `gate_proj`, `up_proj`, and `down_proj`. |
|
|
70
|
-
| Mixed sparse and shared experts | `deepseek_v3`, `deepseek_v32`, `deepseek_v4`, `glm5_next`, `glm_moe_dsa`, `qwen3_5_moe`, `qwen3_5_moe_text`, `qwen4_exp`, `qwen4_exp_text` | Runtime expert tensor defusion for routed experts while preserving the model's shared-expert path. |
|
|
70
|
+
| Mixed sparse and shared experts | `deepseek_v3`, `deepseek_v32`, `deepseek_v4`, `glm5_next`, `glm_moe_dsa`, `granitemoe_swa`, `qwen3_5_moe`, `qwen3_5_moe_text`, `qwen4_exp`, `qwen4_exp_text` | Runtime expert tensor defusion for routed experts while preserving the model's shared-expert path. |
|
|
71
71
|
| Transposed or packed expert tensors | `gpt_oss`, `phimoe` | Splits transposed fused expert `gate_up_proj` tensors into per-expert `gate_proj` + `up_proj`, preserves expert bias when present, and converts expert tensors into numbered expert `nn.Linear` modules. |
|
|
72
72
|
| Flattened expert layout | `dbrx` | Rebuilds the flattened DBRX expert FFN weights into numbered expert `gate_proj`, `up_proj`, and `down_proj` `nn.Linear` modules. |
|
|
73
73
|
| Batched expert-input execution | `llama4` | Runtime expert tensor defusion plus preservation of the llama4 batched expert-input execution contract. |
|
|
@@ -268,6 +268,9 @@ MODEL_CONFIG = {
|
|
|
268
268
|
"granitemoehybrid": {
|
|
269
269
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
270
270
|
},
|
|
271
|
+
"granitemoe_swa": {
|
|
272
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
273
|
+
},
|
|
271
274
|
"granitemoeshared": {
|
|
272
275
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
273
276
|
},
|
|
@@ -283,6 +286,16 @@ MODEL_CONFIG = {
|
|
|
283
286
|
"jetmoe": {
|
|
284
287
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
285
288
|
},
|
|
289
|
+
# Native Transformers MoE backbones whose fused 3-D expert tensors the
|
|
290
|
+
# generic experts pass already splits. Registering them lets
|
|
291
|
+
# ``convert_model()`` report the conversion instead of returning False and
|
|
292
|
+
# leaving routed experts fused.
|
|
293
|
+
"kimi_k25": {
|
|
294
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
295
|
+
},
|
|
296
|
+
"kimi_linear": {
|
|
297
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
298
|
+
},
|
|
286
299
|
"laguna": {
|
|
287
300
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
288
301
|
},
|
|
@@ -310,6 +323,9 @@ MODEL_CONFIG = {
|
|
|
310
323
|
"longcat_flash": {
|
|
311
324
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
312
325
|
},
|
|
326
|
+
"mimo_v2_flash": {
|
|
327
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
328
|
+
},
|
|
313
329
|
"minimax": {
|
|
314
330
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
315
331
|
},
|
|
@@ -343,6 +359,9 @@ MODEL_CONFIG = {
|
|
|
343
359
|
"solar_open2": {
|
|
344
360
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
345
361
|
},
|
|
362
|
+
"unlimited_ocr": {
|
|
363
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
364
|
+
},
|
|
346
365
|
"zamba2": {
|
|
347
366
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
348
367
|
},
|
|
@@ -7,6 +7,20 @@ import torch
|
|
|
7
7
|
from torch import nn
|
|
8
8
|
|
|
9
9
|
|
|
10
|
+
def router_logits_from_output(router_output):
|
|
11
|
+
"""Return raw router logits from a router tensor or the Transformers 5.22 tuple.
|
|
12
|
+
|
|
13
|
+
Transformers 5.22 routers return ``(router_logits, topk_weights,
|
|
14
|
+
topk_indices)``. Defuser keeps its own grouped top-k selection, so it only
|
|
15
|
+
needs the raw logits; the router's precomputed top-k is intentionally
|
|
16
|
+
discarded so the routing math stays byte-identical across Transformers
|
|
17
|
+
versions.
|
|
18
|
+
"""
|
|
19
|
+
if isinstance(router_output, tuple):
|
|
20
|
+
return router_output[0]
|
|
21
|
+
return router_output
|
|
22
|
+
|
|
23
|
+
|
|
10
24
|
def run_routed_experts(
|
|
11
25
|
experts: nn.ModuleList,
|
|
12
26
|
hidden_states: torch.Tensor,
|
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
import torch
|
|
7
7
|
import torch.nn as nn
|
|
8
8
|
|
|
9
|
+
from .common import router_logits_from_output
|
|
10
|
+
|
|
9
11
|
class LinearGlm4MoeMoE(nn.Module):
|
|
10
12
|
"""
|
|
11
13
|
A mixed expert module containing shared experts.
|
|
@@ -34,9 +36,16 @@ class LinearGlm4MoeMoE(nn.Module):
|
|
|
34
36
|
self.routed_scaling_factor = config.routed_scaling_factor
|
|
35
37
|
self.top_k = config.num_experts_per_tok
|
|
36
38
|
|
|
37
|
-
def route_tokens_to_experts(self, router_logits
|
|
38
|
-
"""Replicate GLM4's grouped expert selection and top-k weight normalization.
|
|
39
|
+
def route_tokens_to_experts(self, router_logits) -> tuple[torch.Tensor, torch.Tensor]:
|
|
40
|
+
"""Replicate GLM4's grouped expert selection and top-k weight normalization.
|
|
41
|
+
|
|
42
|
+
Accepts either the raw logits tensor or the Transformers 5.22 router
|
|
43
|
+
output ``(router_logits, topk_weights, topk_indices)``. Only the logits
|
|
44
|
+
are used: Defuser re-derives the top-k selection below, so the router's
|
|
45
|
+
precomputed weights and indices would change its routing math.
|
|
46
|
+
"""
|
|
39
47
|
# Keep the expert selection identical to the upstream GLM4 MoE router.
|
|
48
|
+
router_logits = router_logits_from_output(router_logits)
|
|
40
49
|
router_logits = router_logits.sigmoid()
|
|
41
50
|
router_logits_for_choice = router_logits + self.gate.e_score_correction_bias
|
|
42
51
|
group_scores = (
|
|
@@ -87,8 +96,8 @@ class LinearGlm4MoeMoE(nn.Module):
|
|
|
87
96
|
"""Apply routed experts plus the shared expert branch, matching upstream GLM4 MoE."""
|
|
88
97
|
residuals = hidden_states
|
|
89
98
|
orig_shape = hidden_states.shape
|
|
90
|
-
|
|
91
|
-
topk_indices, topk_weights = self.route_tokens_to_experts(
|
|
99
|
+
router_output = self.gate(hidden_states)
|
|
100
|
+
topk_indices, topk_weights = self.route_tokens_to_experts(router_output)
|
|
92
101
|
hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
|
|
93
102
|
hidden_states = self.moe(hidden_states, topk_indices, topk_weights).view(*orig_shape)
|
|
94
103
|
hidden_states = hidden_states + self.shared_experts(residuals)
|
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
import torch
|
|
10
10
|
import torch.nn as nn
|
|
11
11
|
|
|
12
|
+
from .common import router_logits_from_output
|
|
13
|
+
|
|
12
14
|
class LinearGlm4MoeLiteMoE(nn.Module):
|
|
13
15
|
"""
|
|
14
16
|
A mixed expert module containing shared experts.
|
|
@@ -65,6 +67,12 @@ class LinearGlm4MoeLiteMoE(nn.Module):
|
|
|
65
67
|
return final_hidden_states
|
|
66
68
|
|
|
67
69
|
def route_tokens_to_experts(self, router_logits):
|
|
70
|
+
"""Accept raw logits or the Transformers 5.22 ``(logits, weights, indices)`` tuple.
|
|
71
|
+
|
|
72
|
+
Defuser re-derives the grouped top-k selection below, so only the logits
|
|
73
|
+
are used; the router's precomputed top-k would change the routing math.
|
|
74
|
+
"""
|
|
75
|
+
router_logits = router_logits_from_output(router_logits)
|
|
68
76
|
router_logits = router_logits.sigmoid()
|
|
69
77
|
router_logits_for_choice = router_logits + self.gate.e_score_correction_bias
|
|
70
78
|
group_scores = (
|
|
@@ -92,8 +100,8 @@ class LinearGlm4MoeLiteMoE(nn.Module):
|
|
|
92
100
|
def forward(self, hidden_states):
|
|
93
101
|
residuals = hidden_states
|
|
94
102
|
orig_shape = hidden_states.shape
|
|
95
|
-
|
|
96
|
-
topk_indices, topk_weights = self.route_tokens_to_experts(
|
|
103
|
+
router_output = self.gate(hidden_states)
|
|
104
|
+
topk_indices, topk_weights = self.route_tokens_to_experts(router_output)
|
|
97
105
|
hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
|
|
98
106
|
hidden_states = self.experts_forward(hidden_states, topk_indices, topk_weights).view(*orig_shape)
|
|
99
107
|
hidden_states = hidden_states + self.shared_experts(residuals)
|
|
@@ -285,6 +285,17 @@ STANDARD_MOE_CASES = [
|
|
|
285
285
|
"route_indices": [[0], [1], [2], [3], [0]],
|
|
286
286
|
"expert_attrs": ("gate_proj", "up_proj", "down_proj"),
|
|
287
287
|
},
|
|
288
|
+
{
|
|
289
|
+
"model_type": "granitemoe_swa",
|
|
290
|
+
"module_path": "transformers.models.granitemoe_swa.modeling_granitemoe_swa",
|
|
291
|
+
"class_name": "GraniteMoeSWAExperts",
|
|
292
|
+
"config_module": "transformers.models.granitemoe_swa.configuration_granitemoe_swa",
|
|
293
|
+
"config_name": "GraniteMoeSWAConfig",
|
|
294
|
+
"config_updates": {"hidden_size": 64, "intermediate_size": 128, "num_local_experts": 4, "hidden_act": "silu"},
|
|
295
|
+
"input_dim": 64,
|
|
296
|
+
"route_indices": [[0], [1], [2], [3], [0]],
|
|
297
|
+
"expert_attrs": ("gate_proj", "up_proj", "down_proj"),
|
|
298
|
+
},
|
|
288
299
|
{
|
|
289
300
|
"model_type": "hunyuan_v1_moe",
|
|
290
301
|
"module_path": "transformers.models.hunyuan_v1_moe.modeling_hunyuan_v1_moe",
|
|
@@ -601,6 +612,7 @@ ALL_CANDIDATE_MODEL_TYPES = {
|
|
|
601
612
|
"gpt_oss",
|
|
602
613
|
"granitemoe",
|
|
603
614
|
"granitemoehybrid",
|
|
615
|
+
"granitemoe_swa",
|
|
604
616
|
"granitemoeshared",
|
|
605
617
|
"hunyuan_v1_moe",
|
|
606
618
|
"jamba",
|
|
@@ -17,6 +17,10 @@ from transformers.models.bailing_hybrid.modeling_bailing_hybrid import (
|
|
|
17
17
|
BailingHybridForCausalLM,
|
|
18
18
|
)
|
|
19
19
|
from transformers.models.glm4_moe.modeling_glm4_moe import Glm4MoeConfig, Glm4MoeForCausalLM, Glm4MoeMoE
|
|
20
|
+
from transformers.models.kimi_linear.configuration_kimi_linear import KimiLinearConfig
|
|
21
|
+
from transformers.models.kimi_linear.modeling_kimi_linear import KimiLinearForCausalLM
|
|
22
|
+
from transformers.models.mimo_v2_flash.configuration_mimo_v2_flash import MiMoV2FlashConfig
|
|
23
|
+
from transformers.models.mimo_v2_flash.modeling_mimo_v2_flash import MiMoV2FlashForCausalLM
|
|
20
24
|
from transformers.models.glm4_moe_lite.configuration_glm4_moe_lite import Glm4MoeLiteConfig
|
|
21
25
|
from transformers.models.glm4_moe_lite.modeling_glm4_moe_lite import (
|
|
22
26
|
Glm4MoeLiteForCausalLM,
|
|
@@ -301,6 +305,49 @@ def _tiny_glm4_moe_config():
|
|
|
301
305
|
)
|
|
302
306
|
|
|
303
307
|
|
|
308
|
+
def _tiny_kimi_linear_config():
|
|
309
|
+
return KimiLinearConfig(
|
|
310
|
+
vocab_size=64,
|
|
311
|
+
hidden_size=32,
|
|
312
|
+
intermediate_size=48,
|
|
313
|
+
moe_intermediate_size=16,
|
|
314
|
+
num_hidden_layers=2,
|
|
315
|
+
num_attention_heads=4,
|
|
316
|
+
num_key_value_heads=2,
|
|
317
|
+
head_dim=8,
|
|
318
|
+
num_experts=4,
|
|
319
|
+
num_experts_per_tok=2,
|
|
320
|
+
linear_num_key_heads=2,
|
|
321
|
+
linear_num_value_heads=4,
|
|
322
|
+
linear_key_head_dim=8,
|
|
323
|
+
linear_value_head_dim=8,
|
|
324
|
+
linear_conv_kernel_dim=4,
|
|
325
|
+
pad_token_id=0,
|
|
326
|
+
bos_token_id=1,
|
|
327
|
+
eos_token_id=2,
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def _tiny_mimo_v2_flash_config():
|
|
332
|
+
return MiMoV2FlashConfig(
|
|
333
|
+
vocab_size=64,
|
|
334
|
+
hidden_size=32,
|
|
335
|
+
intermediate_size=48,
|
|
336
|
+
moe_intermediate_size=16,
|
|
337
|
+
num_hidden_layers=2,
|
|
338
|
+
num_attention_heads=4,
|
|
339
|
+
num_key_value_heads=2,
|
|
340
|
+
head_dim=8,
|
|
341
|
+
n_routed_experts=4,
|
|
342
|
+
num_experts_per_tok=2,
|
|
343
|
+
layer_types=["full_attention", "sliding_attention"],
|
|
344
|
+
mlp_layer_types=["dense", "sparse"],
|
|
345
|
+
pad_token_id=0,
|
|
346
|
+
bos_token_id=1,
|
|
347
|
+
eos_token_id=2,
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
|
|
304
351
|
def _tiny_glm4v_config():
|
|
305
352
|
return Glm4vConfig(
|
|
306
353
|
text_config={
|
|
@@ -1190,6 +1237,104 @@ def test_glm4_moe_lite_defused_forward_matches_fused_math():
|
|
|
1190
1237
|
hidden_states,
|
|
1191
1238
|
)
|
|
1192
1239
|
|
|
1240
|
+
|
|
1241
|
+
@pytest.mark.parametrize(
|
|
1242
|
+
("config_factory", "block_cls"),
|
|
1243
|
+
[
|
|
1244
|
+
(_tiny_glm4_moe_config, LinearGlm4MoeMoE),
|
|
1245
|
+
(lambda: _tiny_moe_config(Glm4MoeLiteConfig), LinearGlm4MoeLiteMoE),
|
|
1246
|
+
],
|
|
1247
|
+
)
|
|
1248
|
+
def test_glm4_route_tokens_accepts_tuple_and_tensor_equivalently(config_factory, block_cls):
|
|
1249
|
+
"""Transformers 5.22 routers return ``(logits, weights, indices)``.
|
|
1250
|
+
|
|
1251
|
+
The replacement block must unwrap the tuple while keeping Defuser's grouped
|
|
1252
|
+
top-k routing math identical to the plain-logits path.
|
|
1253
|
+
"""
|
|
1254
|
+
|
|
1255
|
+
config = config_factory()
|
|
1256
|
+
block = block_cls(config).eval()
|
|
1257
|
+
hidden_states = torch.randn(2, 3, config.hidden_size, dtype=torch.float32)
|
|
1258
|
+
with torch.no_grad():
|
|
1259
|
+
router_output = block.gate(hidden_states)
|
|
1260
|
+
logits = router_output[0] if isinstance(router_output, tuple) else router_output
|
|
1261
|
+
|
|
1262
|
+
with torch.no_grad():
|
|
1263
|
+
tuple_indices, tuple_weights = block.route_tokens_to_experts((logits, None, None))
|
|
1264
|
+
tensor_indices, tensor_weights = block.route_tokens_to_experts(logits)
|
|
1265
|
+
|
|
1266
|
+
torch.testing.assert_close(tuple_indices, tensor_indices, rtol=0, atol=0)
|
|
1267
|
+
torch.testing.assert_close(tuple_weights, tensor_weights, rtol=0, atol=0)
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
@pytest.mark.parametrize(
|
|
1271
|
+
("model_type", "config_factory", "model_cls"),
|
|
1272
|
+
[
|
|
1273
|
+
("glm4_moe", _tiny_glm4_moe_config, Glm4MoeForCausalLM),
|
|
1274
|
+
(
|
|
1275
|
+
"glm4_moe_lite",
|
|
1276
|
+
lambda: _tiny_moe_config(Glm4MoeLiteConfig, num_hidden_layers=2),
|
|
1277
|
+
Glm4MoeLiteForCausalLM,
|
|
1278
|
+
),
|
|
1279
|
+
],
|
|
1280
|
+
)
|
|
1281
|
+
def test_defused_glm4_model_forward_accepts_router_tuple(model_type, config_factory, model_cls):
|
|
1282
|
+
"""The installed Transformers router return shape must not crash a defused model."""
|
|
1283
|
+
|
|
1284
|
+
replace_fused_blocks(model_type)
|
|
1285
|
+
model = model_cls(config_factory()).eval()
|
|
1286
|
+
input_ids = torch.tensor([[1, 2, 3, 4]])
|
|
1287
|
+
|
|
1288
|
+
with torch.no_grad():
|
|
1289
|
+
logits = model(input_ids=input_ids).logits
|
|
1290
|
+
|
|
1291
|
+
assert logits.shape == (1, 4, model.config.vocab_size)
|
|
1292
|
+
assert torch.isfinite(logits).all()
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
@pytest.mark.parametrize("model_type", ["kimi_k25", "kimi_linear", "mimo_v2_flash", "unlimited_ocr"])
|
|
1296
|
+
def test_native_moe_model_types_are_registered(model_type):
|
|
1297
|
+
"""Native Transformers MoE backbones must not fall through the registry.
|
|
1298
|
+
|
|
1299
|
+
Without a registry entry ``convert_model()`` returns False and the routed
|
|
1300
|
+
experts stay fused as 3-D parameters, so downstream quantizers silently
|
|
1301
|
+
leave them in the source dtype.
|
|
1302
|
+
"""
|
|
1303
|
+
|
|
1304
|
+
assert model_type in MODEL_CONFIG
|
|
1305
|
+
assert MODEL_CONFIG[model_type]["min_transformers_version"] == MIN_SUPPORTED_TRANSFORMERS_VERSION
|
|
1306
|
+
|
|
1307
|
+
|
|
1308
|
+
@pytest.mark.parametrize(
|
|
1309
|
+
("model_type", "config_factory", "model_cls"),
|
|
1310
|
+
[
|
|
1311
|
+
("kimi_linear", _tiny_kimi_linear_config, KimiLinearForCausalLM),
|
|
1312
|
+
("mimo_v2_flash", _tiny_mimo_v2_flash_config, MiMoV2FlashForCausalLM),
|
|
1313
|
+
],
|
|
1314
|
+
)
|
|
1315
|
+
def test_native_moe_defusion_preserves_forward(model_type, config_factory, model_cls):
|
|
1316
|
+
"""Registering a native MoE type must defuse it without changing the output."""
|
|
1317
|
+
|
|
1318
|
+
model = model_cls(config_factory()).eval()
|
|
1319
|
+
input_ids = torch.tensor([[1, 2, 3, 4]])
|
|
1320
|
+
|
|
1321
|
+
with torch.no_grad():
|
|
1322
|
+
expected = model(input_ids=input_ids, use_cache=False).logits
|
|
1323
|
+
|
|
1324
|
+
assert convert_model(model, cleanup_original=False) is True
|
|
1325
|
+
|
|
1326
|
+
experts = [module for name, module in model.named_modules() if name.endswith(".mlp.experts")]
|
|
1327
|
+
assert experts, f"{model_type}: no routed-expert containers found"
|
|
1328
|
+
assert hasattr(experts[0], "0")
|
|
1329
|
+
assert hasattr(experts[0].get_submodule("0"), "gate_proj")
|
|
1330
|
+
assert not hasattr(experts[0], "gate_up_proj")
|
|
1331
|
+
|
|
1332
|
+
with torch.no_grad():
|
|
1333
|
+
actual = model(input_ids=input_ids, use_cache=False).logits
|
|
1334
|
+
|
|
1335
|
+
torch.testing.assert_close(actual, expected, atol=1e-6, rtol=0)
|
|
1336
|
+
|
|
1337
|
+
|
|
1193
1338
|
def test_glm4v_checkpoint_mapping_splits_gate_up_proj():
|
|
1194
1339
|
from defuser.defuser import get_checkpoint_conversion_mapping
|
|
1195
1340
|
|
|
@@ -224,6 +224,9 @@ def _build_model_config(case: dict):
|
|
|
224
224
|
config.layer_types = ["attention", "attention"]
|
|
225
225
|
config.shared_intermediate_size = 64
|
|
226
226
|
config.mamba_n_heads = 8
|
|
227
|
+
elif model_type == "granitemoe_swa":
|
|
228
|
+
config.layer_types = ["full_attention", "sliding_attention"]
|
|
229
|
+
config.shared_intermediate_size = 32
|
|
227
230
|
elif model_type == "jamba":
|
|
228
231
|
# Keep this meta-structure test on the attention path. The mamba path
|
|
229
232
|
# lazy-loads optional hub kernels during construction, which is outside
|
|
@@ -235,6 +238,16 @@ def _build_model_config(case: dict):
|
|
|
235
238
|
elif model_type == "lfm2_moe":
|
|
236
239
|
config.layer_types = ["full_attention", "short_conv"]
|
|
237
240
|
config.num_dense_layers = 0
|
|
241
|
+
elif model_type == "kimi_k25":
|
|
242
|
+
# The DeepSeek-V3 text backbone starts with dense layers; force every
|
|
243
|
+
# tiny layer onto the routed-expert path.
|
|
244
|
+
config.text_config.first_k_dense_replace = 0
|
|
245
|
+
elif model_type == "kimi_linear":
|
|
246
|
+
config.mlp_layer_types = ["sparse"] * config.num_hidden_layers
|
|
247
|
+
elif model_type == "mimo_v2_flash":
|
|
248
|
+
config.mlp_layer_types = ["dense", "sparse"]
|
|
249
|
+
elif model_type == "unlimited_ocr":
|
|
250
|
+
config.text_config.mlp_layer_types = ["sparse"] * config.text_config.num_hidden_layers
|
|
238
251
|
elif model_type == "laguna":
|
|
239
252
|
config.layer_types = ["full_attention"] * config.num_hidden_layers
|
|
240
253
|
config.mlp_layer_types = ["dense"] + ["sparse"] * (config.num_hidden_layers - 1)
|
|
@@ -655,6 +668,17 @@ META_MODEL_CASES = [
|
|
|
655
668
|
"validator": "parallel",
|
|
656
669
|
"min_targets": 4,
|
|
657
670
|
},
|
|
671
|
+
{
|
|
672
|
+
"model_type": "granitemoe_swa",
|
|
673
|
+
"mode": "convert",
|
|
674
|
+
"model_module": "transformers.models.granitemoe_swa.modeling_granitemoe_swa",
|
|
675
|
+
"model_class": "GraniteMoeSWAForCausalLM",
|
|
676
|
+
"config_module": "transformers.models.granitemoe_swa.configuration_granitemoe_swa",
|
|
677
|
+
"config_class": "GraniteMoeSWAConfig",
|
|
678
|
+
"target_class_paths": ("transformers.models.granitemoe_swa.modeling_granitemoe_swa.GraniteMoeSWAExperts",),
|
|
679
|
+
"validator": "experts",
|
|
680
|
+
"min_targets": 2,
|
|
681
|
+
},
|
|
658
682
|
{
|
|
659
683
|
"model_type": "granitemoeshared",
|
|
660
684
|
"mode": "convert",
|
|
@@ -699,6 +723,27 @@ META_MODEL_CASES = [
|
|
|
699
723
|
"validator": "parallel",
|
|
700
724
|
"min_targets": 4,
|
|
701
725
|
},
|
|
726
|
+
{
|
|
727
|
+
"model_type": "kimi_k25",
|
|
728
|
+
"mode": "convert",
|
|
729
|
+
"model_module": "transformers.models.kimi_k25.modeling_kimi_k25",
|
|
730
|
+
"model_class": "Kimi_K25ForConditionalGeneration",
|
|
731
|
+
"config_module": "transformers.models.kimi_k25.configuration_kimi_k25",
|
|
732
|
+
"config_class": "Kimi_K25Config",
|
|
733
|
+
"target_class_paths": ("transformers.models.deepseek_v3.modeling_deepseek_v3.DeepseekV3Experts",),
|
|
734
|
+
"validator": "experts",
|
|
735
|
+
"min_targets": 2,
|
|
736
|
+
},
|
|
737
|
+
{
|
|
738
|
+
"model_type": "kimi_linear",
|
|
739
|
+
"mode": "convert",
|
|
740
|
+
"model_module": "transformers.models.kimi_linear.modeling_kimi_linear",
|
|
741
|
+
"model_class": "KimiLinearForCausalLM",
|
|
742
|
+
"config_module": "transformers.models.kimi_linear.configuration_kimi_linear",
|
|
743
|
+
"config_class": "KimiLinearConfig",
|
|
744
|
+
"target_class_paths": ("transformers.models.kimi_linear.modeling_kimi_linear.KimiLinearExperts",),
|
|
745
|
+
"validator": "experts",
|
|
746
|
+
},
|
|
702
747
|
{
|
|
703
748
|
"model_type": "laguna",
|
|
704
749
|
"mode": "convert",
|
|
@@ -742,6 +787,16 @@ META_MODEL_CASES = [
|
|
|
742
787
|
"validator": "longcat",
|
|
743
788
|
"min_targets": 2,
|
|
744
789
|
},
|
|
790
|
+
{
|
|
791
|
+
"model_type": "mimo_v2_flash",
|
|
792
|
+
"mode": "convert",
|
|
793
|
+
"model_module": "transformers.models.mimo_v2_flash.modeling_mimo_v2_flash",
|
|
794
|
+
"model_class": "MiMoV2FlashForCausalLM",
|
|
795
|
+
"config_module": "transformers.models.mimo_v2_flash.configuration_mimo_v2_flash",
|
|
796
|
+
"config_class": "MiMoV2FlashConfig",
|
|
797
|
+
"target_class_paths": ("transformers.models.mimo_v2_flash.modeling_mimo_v2_flash.MiMoV2FlashExperts",),
|
|
798
|
+
"validator": "experts",
|
|
799
|
+
},
|
|
745
800
|
{
|
|
746
801
|
"model_type": "minimax",
|
|
747
802
|
"mode": "convert",
|
|
@@ -937,6 +992,16 @@ META_MODEL_CASES = [
|
|
|
937
992
|
"validator": "experts",
|
|
938
993
|
"min_targets": 2,
|
|
939
994
|
},
|
|
995
|
+
{
|
|
996
|
+
"model_type": "unlimited_ocr",
|
|
997
|
+
"mode": "convert",
|
|
998
|
+
"model_module": "transformers.models.unlimited_ocr.modeling_unlimited_ocr",
|
|
999
|
+
"model_class": "UnlimitedOcrForConditionalGeneration",
|
|
1000
|
+
"config_module": "transformers.models.unlimited_ocr.configuration_unlimited_ocr",
|
|
1001
|
+
"config_class": "UnlimitedOcrConfig",
|
|
1002
|
+
"target_class_paths": ("transformers.models.unlimited_ocr.modeling_unlimited_ocr.UnlimitedOcrTextExperts",),
|
|
1003
|
+
"validator": "experts",
|
|
1004
|
+
},
|
|
940
1005
|
{
|
|
941
1006
|
"model_type": "zamba2",
|
|
942
1007
|
"mode": "convert",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|