Defuser 0.0.32__tar.gz → 0.0.34__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.32 → defuser-0.0.34}/Defuser.egg-info/PKG-INFO +2 -1
- {defuser-0.0.32 → defuser-0.0.34}/Defuser.egg-info/SOURCES.txt +2 -0
- {defuser-0.0.32 → defuser-0.0.34}/PKG-INFO +2 -1
- {defuser-0.0.32 → defuser-0.0.34}/README.md +1 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/model_registry.py +49 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/runtime_defusion.py +6 -0
- {defuser-0.0.32 → defuser-0.0.34}/pyproject.toml +1 -1
- {defuser-0.0.32 → defuser-0.0.34}/tests/test_candidate_coverage.py +30 -1
- {defuser-0.0.32 → defuser-0.0.34}/tests/test_convert_model.py +77 -1
- defuser-0.0.34/tests/test_decoder_expert_registrations.py +44 -0
- defuser-0.0.34/tests/test_decoder_family_defusion.py +42 -0
- {defuser-0.0.32 → defuser-0.0.34}/tests/test_meta_model_defusion.py +77 -3
- {defuser-0.0.32 → defuser-0.0.34}/Defuser.egg-info/dependency_links.txt +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/Defuser.egg-info/requires.txt +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/Defuser.egg-info/top_level.txt +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/LICENSE +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/__init__.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/checkpoint_ops.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/defuser.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/__init__.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/glm4v.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/model_patches.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/moe_experts_interface.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/replace_modules.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/unfused_moe/__init__.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/unfused_moe/common.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/unfused_moe/glm4_moe.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/unfused_moe/glm4_moe_lite.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/unfused_moe/mixtral.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/unfused_moe/qwen2_moe.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/unfused_moe/qwen3_moe.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/unfused_moe/qwen3_next.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/unfused_moe/qwen3_omni_moe.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/modeling/update_module.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/utils/__init__.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/utils/common.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/utils/device.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/utils/hf.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/defuser/utils/model.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/setup.cfg +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/setup.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/tests/test_device_utils.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/tests/test_filter_rules.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/tests/test_moe_experts_interface.py +0 -0
- {defuser-0.0.32 → defuser-0.0.34}/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.34
|
|
4
4
|
Summary: Model defuser helper for HF Transformers.
|
|
5
5
|
Author-email: ModelCloud <qubitium@modelcloud.ai>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -79,6 +79,7 @@ Defuser currently supports the following `transformers>=5.3.0` `model_type` valu
|
|
|
79
79
|
|
|
80
80
|
| Model type | Defused op performed ⚙️ |
|
|
81
81
|
| --- | --- |
|
|
82
|
+
| `dots3_note`, `dots3_note_text`, `hy_v4`, `hy_v4_vl`, `hy_v4_vl_text`, `inkling_text`, `inkling2`, `inkling2_text`, `inkling2_loop_text`, `minimax_m3_vl_text`, `zaya` | Exposes separate gate, up and down projections for each routed expert in the text decoder. |
|
|
82
83
|
| `glm4_moe` | Replaces `Glm4MoeMoE` with a defused per-expert linear MoE block. |
|
|
83
84
|
| `glm4_moe_lite` | Replaces `Glm4MoeLiteMoE` with a defused per-expert linear MoE block.|
|
|
84
85
|
| `glm4v` | Replaces the fused text MLP with split `gate_proj`, `up_proj`, and `down_proj` layers. Also splits fused checkpoint `mlp.gate_up_proj.weight` into `mlp.gate_proj.weight` + `mlp.up_proj.weight`. |
|
|
@@ -34,6 +34,8 @@ defuser/utils/hf.py
|
|
|
34
34
|
defuser/utils/model.py
|
|
35
35
|
tests/test_candidate_coverage.py
|
|
36
36
|
tests/test_convert_model.py
|
|
37
|
+
tests/test_decoder_expert_registrations.py
|
|
38
|
+
tests/test_decoder_family_defusion.py
|
|
37
39
|
tests/test_device_utils.py
|
|
38
40
|
tests/test_filter_rules.py
|
|
39
41
|
tests/test_meta_model_defusion.py
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: Defuser
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.34
|
|
4
4
|
Summary: Model defuser helper for HF Transformers.
|
|
5
5
|
Author-email: ModelCloud <qubitium@modelcloud.ai>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -79,6 +79,7 @@ Defuser currently supports the following `transformers>=5.3.0` `model_type` valu
|
|
|
79
79
|
|
|
80
80
|
| Model type | Defused op performed ⚙️ |
|
|
81
81
|
| --- | --- |
|
|
82
|
+
| `dots3_note`, `dots3_note_text`, `hy_v4`, `hy_v4_vl`, `hy_v4_vl_text`, `inkling_text`, `inkling2`, `inkling2_text`, `inkling2_loop_text`, `minimax_m3_vl_text`, `zaya` | Exposes separate gate, up and down projections for each routed expert in the text decoder. |
|
|
82
83
|
| `glm4_moe` | Replaces `Glm4MoeMoE` with a defused per-expert linear MoE block. |
|
|
83
84
|
| `glm4_moe_lite` | Replaces `Glm4MoeLiteMoE` with a defused per-expert linear MoE block.|
|
|
84
85
|
| `glm4v` | Replaces the fused text MLP with split `gate_proj`, `up_proj`, and `down_proj` layers. Also splits fused checkpoint `mlp.gate_up_proj.weight` into `mlp.gate_proj.weight` + `mlp.up_proj.weight`. |
|
|
@@ -53,6 +53,7 @@ Defuser currently supports the following `transformers>=5.3.0` `model_type` valu
|
|
|
53
53
|
|
|
54
54
|
| Model type | Defused op performed ⚙️ |
|
|
55
55
|
| --- | --- |
|
|
56
|
+
| `dots3_note`, `dots3_note_text`, `hy_v4`, `hy_v4_vl`, `hy_v4_vl_text`, `inkling_text`, `inkling2`, `inkling2_text`, `inkling2_loop_text`, `minimax_m3_vl_text`, `zaya` | Exposes separate gate, up and down projections for each routed expert in the text decoder. |
|
|
56
57
|
| `glm4_moe` | Replaces `Glm4MoeMoE` with a defused per-expert linear MoE block. |
|
|
57
58
|
| `glm4_moe_lite` | Replaces `Glm4MoeLiteMoE` with a defused per-expert linear MoE block.|
|
|
58
59
|
| `glm4v` | Replaces the fused text MLP with split `gate_proj`, `up_proj`, and `down_proj` layers. Also splits fused checkpoint `mlp.gate_up_proj.weight` into `mlp.gate_proj.weight` + `mlp.up_proj.weight`. |
|
|
@@ -16,6 +16,10 @@ class PATCH(str, Enum):
|
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
MODEL_CONFIG = {
|
|
19
|
+
"aria_text": {"min_transformers_version": "5.25.1"},
|
|
20
|
+
"axk1": {"min_transformers_version": "5.25.1"},
|
|
21
|
+
"beam": {"min_transformers_version": "5.25.1"},
|
|
22
|
+
"mellum": {"min_transformers_version": "5.25.1"},
|
|
19
23
|
"afmoe": {
|
|
20
24
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
21
25
|
PATCH.EXPERTS_DEFUSE: [
|
|
@@ -52,6 +56,18 @@ MODEL_CONFIG = {
|
|
|
52
56
|
"inkling_mm_model": {
|
|
53
57
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
54
58
|
},
|
|
59
|
+
"inkling2_loop": {
|
|
60
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
61
|
+
PATCH.EXPERTS_DEFUSE: [
|
|
62
|
+
{
|
|
63
|
+
# Inkling 2 Mini keeps routed experts as 3-D gate_up_proj /
|
|
64
|
+
# down_proj parameters. Expand them into per-expert Linear
|
|
65
|
+
# modules so quantizers can discover and quantize each expert.
|
|
66
|
+
"module_class": "transformers.models.inkling2_loop.modeling_inkling2_loop.Inkling2LoopExperts",
|
|
67
|
+
"forward_impl": "linear_loop",
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
},
|
|
55
71
|
"deepseek_v3": {
|
|
56
72
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
57
73
|
},
|
|
@@ -362,6 +378,39 @@ MODEL_CONFIG = {
|
|
|
362
378
|
"unlimited_ocr": {
|
|
363
379
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
364
380
|
},
|
|
381
|
+
"hy_v4": {
|
|
382
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
383
|
+
},
|
|
384
|
+
"hy_v4_vl": {
|
|
385
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
386
|
+
},
|
|
387
|
+
"hy_v4_vl_text": {
|
|
388
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
389
|
+
},
|
|
390
|
+
"inkling2": {
|
|
391
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
392
|
+
},
|
|
393
|
+
"inkling2_text": {
|
|
394
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
395
|
+
},
|
|
396
|
+
"inkling2_loop_text": {
|
|
397
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
398
|
+
},
|
|
399
|
+
"minimax_m3_vl_text": {
|
|
400
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
401
|
+
},
|
|
402
|
+
"dots3_note": {
|
|
403
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
404
|
+
},
|
|
405
|
+
"dots3_note_text": {
|
|
406
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
407
|
+
},
|
|
408
|
+
"inkling_text": {
|
|
409
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
410
|
+
},
|
|
411
|
+
"zaya": {
|
|
412
|
+
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
413
|
+
},
|
|
365
414
|
"zamba2": {
|
|
366
415
|
"min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION,
|
|
367
416
|
},
|
|
@@ -192,6 +192,12 @@ def patch_longcat_flash_experts(module: nn.Module) -> bool:
|
|
|
192
192
|
if getattr(module, "_defuser_longcat_runtime", False):
|
|
193
193
|
return False
|
|
194
194
|
|
|
195
|
+
# Newer Transformers routes identity experts in the parent MoE. Its
|
|
196
|
+
# decorated expert bank contains learned experts only and is converted
|
|
197
|
+
# by the generic pass that follows runtime patching.
|
|
198
|
+
if not hasattr(module, "total_experts"):
|
|
199
|
+
return False
|
|
200
|
+
|
|
195
201
|
gate_up_proj = getattr(module, "gate_up_proj", None)
|
|
196
202
|
down_proj = getattr(module, "down_proj", None)
|
|
197
203
|
if gate_up_proj is None and down_proj is None and hasattr(module, "0"):
|
|
@@ -438,6 +438,8 @@ STANDARD_MOE_CASES = [
|
|
|
438
438
|
"route_weights": [[0.6, 0.4], [0.7, 0.3], [0.5, 0.5], [0.8, 0.2], [0.9, 0.1]],
|
|
439
439
|
"expert_attrs": ("gate_proj", "up_proj", "down_proj"),
|
|
440
440
|
"identity_expert_index": 4,
|
|
441
|
+
"atol": 2e-4,
|
|
442
|
+
"rtol": 2e-5,
|
|
441
443
|
},
|
|
442
444
|
]
|
|
443
445
|
|
|
@@ -679,10 +681,37 @@ def test_standard_moe_candidates_convert_and_preserve_forward(case):
|
|
|
679
681
|
rtol=case.get("rtol", 1.3e-6),
|
|
680
682
|
)
|
|
681
683
|
|
|
682
|
-
if case.get("identity_expert_index") is not None:
|
|
684
|
+
if case.get("identity_expert_index") is not None and hasattr(patched, "total_experts"):
|
|
683
685
|
assert hasattr(getattr(patched, str(case["identity_expert_index"])), "identity")
|
|
684
686
|
|
|
685
687
|
|
|
688
|
+
def test_longcat_identity_routes_survive_public_conversion():
|
|
689
|
+
from transformers import LongcatFlashConfig
|
|
690
|
+
from transformers.models.longcat_flash.modeling_longcat_flash import LongcatFlashMoE
|
|
691
|
+
|
|
692
|
+
config = LongcatFlashConfig(hidden_size=8, expert_ffn_hidden_size=4, n_routed_experts=2,
|
|
693
|
+
zero_expert_num=2, moe_topk=2)
|
|
694
|
+
layer = LongcatFlashMoE(config).eval()
|
|
695
|
+
wrapper = _DummyModel("longcat_flash", layer)
|
|
696
|
+
wrapper.config = config
|
|
697
|
+
# Positive tokens select two identity routes; negative tokens select
|
|
698
|
+
# learned routes whose weights are all zero. Both routes share one batch.
|
|
699
|
+
inputs = torch.ones(1, 2, config.hidden_size)
|
|
700
|
+
inputs[:, 1].neg_()
|
|
701
|
+
with torch.no_grad():
|
|
702
|
+
for parameter in layer.parameters():
|
|
703
|
+
parameter.zero_()
|
|
704
|
+
layer.router.classifier.weight[:2, 0] = -2
|
|
705
|
+
layer.router.classifier.weight[2:, 0] = 2
|
|
706
|
+
layer.router.e_score_correction_bias.zero_()
|
|
707
|
+
expected = layer(inputs)
|
|
708
|
+
assert expected[:, 0].abs().min() > 0
|
|
709
|
+
assert not expected[:, 1].any()
|
|
710
|
+
assert convert_model(wrapper, cleanup_original=True)
|
|
711
|
+
torch.testing.assert_close(layer(inputs), expected)
|
|
712
|
+
assert isinstance(getattr(layer.experts, "0").gate_proj, nn.Linear)
|
|
713
|
+
|
|
714
|
+
|
|
686
715
|
@pytest.mark.parametrize("case", PARALLEL_CASES, ids=[case["model_type"] for case in PARALLEL_CASES])
|
|
687
716
|
def test_parallel_expert_candidates_convert_and_preserve_forward(case):
|
|
688
717
|
"""Parallel expert containers should convert without changing outputs."""
|
|
@@ -1292,7 +1292,9 @@ def test_defused_glm4_model_forward_accepts_router_tuple(model_type, config_fact
|
|
|
1292
1292
|
assert torch.isfinite(logits).all()
|
|
1293
1293
|
|
|
1294
1294
|
|
|
1295
|
-
@pytest.mark.parametrize(
|
|
1295
|
+
@pytest.mark.parametrize(
|
|
1296
|
+
"model_type", ["kimi_k25", "kimi_linear", "mimo_v2_flash", "unlimited_ocr", "inkling2_loop"]
|
|
1297
|
+
)
|
|
1296
1298
|
def test_native_moe_model_types_are_registered(model_type):
|
|
1297
1299
|
"""Native Transformers MoE backbones must not fall through the registry.
|
|
1298
1300
|
|
|
@@ -1335,6 +1337,80 @@ def test_native_moe_defusion_preserves_forward(model_type, config_factory, model
|
|
|
1335
1337
|
torch.testing.assert_close(actual, expected, atol=1e-6, rtol=0)
|
|
1336
1338
|
|
|
1337
1339
|
|
|
1340
|
+
def test_inkling2_loop_experts_defuse_preserves_forward():
|
|
1341
|
+
"""Inkling 2 Mini routed experts are 3-D parameters that must be expanded.
|
|
1342
|
+
|
|
1343
|
+
The implementation only exists on the open huggingface/transformers#51050
|
|
1344
|
+
branch, so this test skips with a precise reason on a released Transformers
|
|
1345
|
+
that has no ``inkling2_loop`` module.
|
|
1346
|
+
"""
|
|
1347
|
+
|
|
1348
|
+
modeling = pytest.importorskip(
|
|
1349
|
+
"transformers.models.inkling2_loop.modeling_inkling2_loop",
|
|
1350
|
+
reason="Inkling2Loop only exists on the open huggingface/transformers#51050 branch.",
|
|
1351
|
+
)
|
|
1352
|
+
configuration = pytest.importorskip(
|
|
1353
|
+
"transformers.models.inkling2_loop.configuration_inkling2_loop",
|
|
1354
|
+
reason="Inkling2Loop only exists on the open huggingface/transformers#51050 branch.",
|
|
1355
|
+
)
|
|
1356
|
+
|
|
1357
|
+
torch.manual_seed(0)
|
|
1358
|
+
config = configuration.Inkling2LoopConfig(
|
|
1359
|
+
text_config={
|
|
1360
|
+
"hidden_size": 32,
|
|
1361
|
+
"num_hidden_layers": 4,
|
|
1362
|
+
"num_attention_heads": 2,
|
|
1363
|
+
"num_key_value_heads": 1,
|
|
1364
|
+
"head_dim": 8,
|
|
1365
|
+
"intermediate_size": 16,
|
|
1366
|
+
"n_routed_experts": 3,
|
|
1367
|
+
"num_experts_per_tok": 2,
|
|
1368
|
+
"moe_intermediate_size": 8,
|
|
1369
|
+
"vocab_size": 64,
|
|
1370
|
+
"unpadded_vocab_size": 64,
|
|
1371
|
+
"n_layers_per_attn_res_block": 2,
|
|
1372
|
+
"d_rel": 2,
|
|
1373
|
+
"rel_extent": 4,
|
|
1374
|
+
"sliding_window_size": 4,
|
|
1375
|
+
"loop_config": {"core_start": 2, "core_end": 4, "num_passes": 2, "gate_rank": 4},
|
|
1376
|
+
},
|
|
1377
|
+
vision_config={
|
|
1378
|
+
"hidden_size": 16,
|
|
1379
|
+
"num_hidden_layers": 1,
|
|
1380
|
+
"num_attention_heads": 2,
|
|
1381
|
+
"projection_hidden_dim": 16,
|
|
1382
|
+
"text_hidden_size": 32,
|
|
1383
|
+
},
|
|
1384
|
+
)
|
|
1385
|
+
model = modeling.Inkling2LoopForConditionalGeneration(config).eval()
|
|
1386
|
+
input_ids = torch.tensor([[1, 2, 3, 4, 5]])
|
|
1387
|
+
|
|
1388
|
+
with torch.no_grad():
|
|
1389
|
+
expected = model(input_ids=input_ids, use_cache=False).logits
|
|
1390
|
+
|
|
1391
|
+
containers = [module for name, module in model.named_modules() if name.endswith(".mlp.experts")]
|
|
1392
|
+
assert len(containers) == config.text_config.num_hidden_layers
|
|
1393
|
+
assert all(hasattr(container, "gate_up_proj") for container in containers)
|
|
1394
|
+
|
|
1395
|
+
assert convert_model(model, cleanup_original=False) is True
|
|
1396
|
+
|
|
1397
|
+
containers = [module for name, module in model.named_modules() if name.endswith(".mlp.experts")]
|
|
1398
|
+
assert len(containers) == config.text_config.num_hidden_layers
|
|
1399
|
+
for container in containers:
|
|
1400
|
+
assert not hasattr(container, "gate_up_proj")
|
|
1401
|
+
assert not hasattr(container, "down_proj")
|
|
1402
|
+
for index in range(config.text_config.n_routed_experts):
|
|
1403
|
+
expert = container.get_submodule(str(index))
|
|
1404
|
+
assert isinstance(expert.gate_proj, nn.Linear)
|
|
1405
|
+
assert isinstance(expert.up_proj, nn.Linear)
|
|
1406
|
+
assert isinstance(expert.down_proj, nn.Linear)
|
|
1407
|
+
|
|
1408
|
+
with torch.no_grad():
|
|
1409
|
+
actual = model(input_ids=input_ids, use_cache=False).logits
|
|
1410
|
+
|
|
1411
|
+
torch.testing.assert_close(actual, expected, atol=1e-6, rtol=0)
|
|
1412
|
+
|
|
1413
|
+
|
|
1338
1414
|
def test_glm4v_checkpoint_mapping_splits_gate_up_proj():
|
|
1339
1415
|
from defuser.defuser import get_checkpoint_conversion_mapping
|
|
1340
1416
|
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Public conversion must split each routed bank and preserve its trained activation."""
|
|
3
|
+
from importlib import import_module
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
import torch
|
|
7
|
+
from torch import nn
|
|
8
|
+
from transformers import AutoConfig
|
|
9
|
+
|
|
10
|
+
from decoder_expert_cases import DECODER_EXPERT_CASES
|
|
11
|
+
from defuser import convert_model
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@pytest.mark.parametrize(("model_type", "family", "config_prefix", "class_name"), DECODER_EXPERT_CASES)
|
|
15
|
+
def test_public_conversion_preserves_routed_outputs(model_type, family, config_prefix, class_name):
|
|
16
|
+
module = pytest.importorskip(f"transformers.models.{family}.modeling_{family}")
|
|
17
|
+
root = nn.Module()
|
|
18
|
+
root.config = AutoConfig.for_model(model_type)
|
|
19
|
+
config = getattr(root.config, "text_config", root.config)
|
|
20
|
+
config.hidden_size = 16
|
|
21
|
+
config.moe_intermediate_size = 12
|
|
22
|
+
config.intermediate_size = 12
|
|
23
|
+
for key in ("n_routed_experts", "num_local_experts", "num_experts"):
|
|
24
|
+
if hasattr(config, key):
|
|
25
|
+
setattr(config, key, 3)
|
|
26
|
+
config.num_experts_per_tok = 1 if model_type == "zaya" else 2
|
|
27
|
+
config._experts_implementation = "eager"
|
|
28
|
+
if hasattr(config, "swiglu_limit"):
|
|
29
|
+
config.swiglu_limit = 1.25
|
|
30
|
+
root.experts = getattr(module, class_name)(config)
|
|
31
|
+
generator = torch.Generator().manual_seed(717)
|
|
32
|
+
with torch.no_grad():
|
|
33
|
+
for p in root.experts.parameters():
|
|
34
|
+
p.normal_(generator=generator)
|
|
35
|
+
hidden = torch.randn(6, 16, generator=generator)
|
|
36
|
+
indices = torch.tensor([[0, 1], [2, 0], [1, 2], [1, 0], [2, 1], [0, 2]])[:, :config.num_experts_per_tok]
|
|
37
|
+
weights = torch.rand(indices.shape, generator=generator)
|
|
38
|
+
expected = root.experts(hidden, indices, weights)
|
|
39
|
+
assert convert_model(root, cleanup_original=True)
|
|
40
|
+
assert not hasattr(root.experts, "gate_up_proj")
|
|
41
|
+
for i in range(3):
|
|
42
|
+
expert = root.experts.get_submodule(str(i))
|
|
43
|
+
assert all(isinstance(getattr(expert, proj), nn.Linear) for proj in ("gate_proj", "up_proj", "down_proj"))
|
|
44
|
+
torch.testing.assert_close(root.experts(hidden, indices, weights), expected, rtol=1e-5, atol=1e-5)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 ModelCloud.ai
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import pytest
|
|
4
|
+
import torch
|
|
5
|
+
from transformers import AutoConfig, AutoModelForCausalLM
|
|
6
|
+
|
|
7
|
+
from defuser import convert_model
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
EXPERT_CONFIGS = {
|
|
11
|
+
"aria_text": {"moe_num_experts": 2, "moe_topk": 2},
|
|
12
|
+
"mellum": {"num_local_experts": 2, "num_experts_per_tok": 2, "head_dim": 8,
|
|
13
|
+
"layer_types": ["full_attention"], "mlp_layer_types": ["sparse"]},
|
|
14
|
+
"axk1": {"n_routed_experts": 2, "num_experts_per_tok": 2, "first_k_dense_replace": 0, "n_group": 1,
|
|
15
|
+
"topk_group": 1, "q_lora_rank": 16, "kv_lora_rank": 16, "qk_rope_head_dim": 8,
|
|
16
|
+
"qk_nope_head_dim": 8, "v_head_dim": 8},
|
|
17
|
+
"beam": {"num_experts": 2, "num_experts_per_tok": 2, "n_group": 1, "topk_group": 1, "head_dim": 8,
|
|
18
|
+
"layer_types": ["full_attention"], "mlp_layer_types": ["sparse"]},
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.mark.parametrize("family", EXPERT_CONFIGS)
|
|
23
|
+
def test_public_convert_preserves_routed_decoder(family):
|
|
24
|
+
torch.manual_seed(63)
|
|
25
|
+
config = AutoConfig.for_model(family, hidden_size=32, num_hidden_layers=1, num_attention_heads=4,
|
|
26
|
+
num_key_value_heads=4, intermediate_size=64, moe_intermediate_size=32,
|
|
27
|
+
vocab_size=64, **EXPERT_CONFIGS[family])
|
|
28
|
+
model = AutoModelForCausalLM.from_config(config, attn_implementation="eager").eval()
|
|
29
|
+
experts = [(n, m) for n, m in model.named_modules() if isinstance(getattr(m, "gate_up_proj", None), torch.Tensor)
|
|
30
|
+
and m.gate_up_proj.ndim == 3]
|
|
31
|
+
assert len(experts) == 1
|
|
32
|
+
ids = torch.tensor([[1, 3, 8, 7]])
|
|
33
|
+
with torch.inference_mode():
|
|
34
|
+
before = model(ids, use_cache=False).logits
|
|
35
|
+
assert convert_model(model, cleanup_original=True)
|
|
36
|
+
name, _ = experts[0]
|
|
37
|
+
converted = model.get_submodule(name)
|
|
38
|
+
for index in range(2):
|
|
39
|
+
for leaf in ("gate_proj", "up_proj", "down_proj"):
|
|
40
|
+
assert isinstance(converted.get_submodule(f"{index}.{leaf}"), torch.nn.Linear)
|
|
41
|
+
assert "gate_up_proj" not in converted._parameters
|
|
42
|
+
torch.testing.assert_close(model(ids, use_cache=False).logits, before, atol=2e-6, rtol=1e-5)
|
|
@@ -10,6 +10,7 @@ from importlib import import_module
|
|
|
10
10
|
import pytest
|
|
11
11
|
import torch
|
|
12
12
|
|
|
13
|
+
from decoder_expert_cases import DECODER_EXPERT_CASES
|
|
13
14
|
from defuser import convert_model, replace_fused_blocks
|
|
14
15
|
from defuser.model_registry import MODEL_CONFIG
|
|
15
16
|
|
|
@@ -180,11 +181,25 @@ def _build_model_config(case: dict):
|
|
|
180
181
|
eoi_token_id=126,
|
|
181
182
|
)
|
|
182
183
|
|
|
183
|
-
|
|
184
|
+
if case["model_type"] in {"dots3_note", "dots3_note_text"}:
|
|
185
|
+
# The released config overrides head widths per layer. Construct an
|
|
186
|
+
# explicit homogeneous tiny decoder so the structural test can resize it.
|
|
187
|
+
tiny_text = {"per_layer_config": {}, "num_hidden_layers": 2, "mlp_layer_types": ["sparse", "sparse"]}
|
|
188
|
+
if case["model_type"] == "dots3_note":
|
|
189
|
+
text_cls = _load(case["config_module"], "Dots3NoteTextConfig")
|
|
190
|
+
return config_cls(text_config=text_cls(**tiny_text))
|
|
191
|
+
config = config_cls(**tiny_text)
|
|
192
|
+
else:
|
|
193
|
+
config = config_cls()
|
|
184
194
|
_mutate_common_config_tree(config)
|
|
185
195
|
|
|
186
196
|
model_type = case["model_type"]
|
|
187
|
-
if model_type
|
|
197
|
+
if model_type in {"beam", "mellum", "axk1"}:
|
|
198
|
+
config.mlp_layer_types = ["sparse"] * config.num_hidden_layers
|
|
199
|
+
config.layer_types = ["full_attention"] * config.num_hidden_layers
|
|
200
|
+
if model_type == "axk1":
|
|
201
|
+
config.first_k_dense_replace = 0
|
|
202
|
+
elif model_type == "dbrx":
|
|
188
203
|
config.max_seq_len = 64
|
|
189
204
|
config.resid_pdrop = 0.0
|
|
190
205
|
config.emb_pdrop = 0.0
|
|
@@ -347,7 +362,11 @@ def _validate_defused_module(case: dict, module) -> None:
|
|
|
347
362
|
assert hasattr(module, "0")
|
|
348
363
|
assert hasattr(getattr(module, "0"), "gate_proj")
|
|
349
364
|
assert not hasattr(module, "gate_up_proj")
|
|
350
|
-
|
|
365
|
+
if hasattr(module, "total_experts"):
|
|
366
|
+
assert any(hasattr(getattr(module, str(idx)), "identity") for idx in range(module.total_experts))
|
|
367
|
+
else:
|
|
368
|
+
assert module.num_experts == module.config.n_routed_experts
|
|
369
|
+
assert not hasattr(module, str(module.num_experts))
|
|
351
370
|
_assert_meta_parameters(getattr(module, "0"))
|
|
352
371
|
return
|
|
353
372
|
|
|
@@ -364,6 +383,46 @@ def _validate_defused_module(case: dict, module) -> None:
|
|
|
364
383
|
|
|
365
384
|
|
|
366
385
|
META_MODEL_CASES = [
|
|
386
|
+
{
|
|
387
|
+
"model_type": "aria_text",
|
|
388
|
+
"mode": "convert",
|
|
389
|
+
"model_module": "transformers.models.aria.modeling_aria",
|
|
390
|
+
"model_class": "AriaTextForCausalLM",
|
|
391
|
+
"config_module": "transformers.models.aria.configuration_aria",
|
|
392
|
+
"config_class": "AriaTextConfig",
|
|
393
|
+
"target_class_paths": ("transformers.models.aria.modeling_aria.AriaExperts",),
|
|
394
|
+
"validator": "experts",
|
|
395
|
+
},
|
|
396
|
+
{
|
|
397
|
+
"model_type": "axk1",
|
|
398
|
+
"mode": "convert",
|
|
399
|
+
"model_module": "transformers.models.axk1.modeling_axk1",
|
|
400
|
+
"model_class": "AXK1ForCausalLM",
|
|
401
|
+
"config_module": "transformers.models.axk1.configuration_axk1",
|
|
402
|
+
"config_class": "AXK1Config",
|
|
403
|
+
"target_class_paths": ("transformers.models.axk1.modeling_axk1.AXK1Experts",),
|
|
404
|
+
"validator": "experts",
|
|
405
|
+
},
|
|
406
|
+
{
|
|
407
|
+
"model_type": "beam",
|
|
408
|
+
"mode": "convert",
|
|
409
|
+
"model_module": "transformers.models.beam.modeling_beam",
|
|
410
|
+
"model_class": "BeamForCausalLM",
|
|
411
|
+
"config_module": "transformers.models.beam.configuration_beam",
|
|
412
|
+
"config_class": "BeamConfig",
|
|
413
|
+
"target_class_paths": ("transformers.models.beam.modeling_beam.BeamExperts",),
|
|
414
|
+
"validator": "experts",
|
|
415
|
+
},
|
|
416
|
+
{
|
|
417
|
+
"model_type": "mellum",
|
|
418
|
+
"mode": "convert",
|
|
419
|
+
"model_module": "transformers.models.mellum.modeling_mellum",
|
|
420
|
+
"model_class": "MellumForCausalLM",
|
|
421
|
+
"config_module": "transformers.models.mellum.configuration_mellum",
|
|
422
|
+
"config_class": "MellumConfig",
|
|
423
|
+
"target_class_paths": ("transformers.models.mellum.modeling_mellum.MellumExperts",),
|
|
424
|
+
"validator": "experts",
|
|
425
|
+
},
|
|
367
426
|
{
|
|
368
427
|
"model_type": "afmoe",
|
|
369
428
|
"mode": "convert",
|
|
@@ -1015,6 +1074,21 @@ META_MODEL_CASES = [
|
|
|
1015
1074
|
]
|
|
1016
1075
|
|
|
1017
1076
|
|
|
1077
|
+
for model_type, family, name, experts in DECODER_EXPERT_CASES:
|
|
1078
|
+
family_module = f"transformers.models.{family}"
|
|
1079
|
+
model_module = f"{family_module}.modeling_{family}"
|
|
1080
|
+
META_MODEL_CASES.append({
|
|
1081
|
+
"model_type": model_type,
|
|
1082
|
+
"mode": "convert",
|
|
1083
|
+
"model_module": model_module,
|
|
1084
|
+
"model_class": name + "Model",
|
|
1085
|
+
"config_module": f"{family_module}.configuration_{family}",
|
|
1086
|
+
"config_class": name + "Config",
|
|
1087
|
+
"target_class_paths": (f"{model_module}.{experts}",),
|
|
1088
|
+
"validator": "experts",
|
|
1089
|
+
})
|
|
1090
|
+
|
|
1091
|
+
|
|
1018
1092
|
def test_meta_model_cases_cover_registered_public_models():
|
|
1019
1093
|
"""Meta-model coverage should stay aligned with the public registry."""
|
|
1020
1094
|
assert {case["model_type"] for case in META_MODEL_CASES} == set(MODEL_CONFIG) - {"qwen3_5_moe_text"}
|
|
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
|
|
File without changes
|