You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Defuser converts select Hugging Face Transformers `5.3.0+` fused or stacked MoE and MLP blocks back into plain, per-expert `nn.Linear` modules. It keeps the forward math intact while exposing individual projections again so quantizers, activation capture, debugging hooks, and checkpoint tooling can work against a simple module layout instead of fused expert tensors.
14
+
🧩 Defuser converts select Hugging Face Transformers `5.3.0+` fused or stacked MoE and MLP blocks back into plain, per-expert `nn.Linear` modules. It keeps the forward math intact while exposing individual projections again so quantizers, activation capture, debugging hooks, and checkpoint tooling can work against a simple module layout instead of fused expert tensors.
15
15
16
-
Defuser is designed and CI-tested for `transformers>=5.3.0`, and support is only offered for that version range.
16
+
✅ Defuser is designed and CI-tested for `transformers>=5.3.0`, and support is only offered for that version range.
17
17
18
-
## Purpose
18
+
## 🎯 Purpose
19
19
20
20
Defuser exists for cases where newer Transformers modeling code optimizes model structure in ways that are good for runtime, but harder for tooling that needs direct access to individual projections.
21
21
22
22
Depending on the model family, Defuser can:
23
23
24
-
- patch a supported model class before load so HF instantiates a defused block directly
25
-
- split fused tensors such as `gate_up_proj` into `gate_proj` + `up_proj`
26
-
- convert 3D expert tensors, including registered expert buffers, into numbered expert `nn.Linear` modules
27
-
- preserve the original fused math while presenting a naive module structure again
24
+
-🧵 patch a supported model class before load so HF instantiates a defused block directly
25
+
-✂️ split fused tensors such as `gate_up_proj` into `gate_proj` + `up_proj`
26
+
-🧱 convert 3D expert tensors, including registered expert buffers, into numbered expert `nn.Linear` modules
27
+
-🧮 preserve the original fused math while presenting a naive module structure again
28
28
29
-
Public API:
29
+
🛠️ Public API:
30
30
31
31
```python
32
32
from defuser import convert_model, replace_fused_blocks
33
33
```
34
34
35
-
-`replace_fused_blocks(model_type)` patches supported HF model classes before `from_pretrained()` or direct model construction.
36
-
-`convert_model(model, cleanup_original=False, max_layers=None, filter=None)` converts an already loaded model in place. This is the runtime defusion path for supported post-load expert and MLP conversions, including `qwen3_5_moe` style checkpoints.
37
-
- Defuser is designed and CI-tested for `transformers>=5.3.0`, and support is only offered for that version range. Older versions log a warning on these public APIs and are skipped as unsupported.
38
-
- Some model families appear in both support tables. Full models can be prepatched with `replace_fused_blocks(...)`, while standalone fused expert modules from those same families can still be runtime-defused with `convert_model(...)`.
35
+
-🧰 `replace_fused_blocks(model_type)` patches supported HF model classes before `from_pretrained()` or direct model construction.
36
+
-🔄 `convert_model(model, cleanup_original=False, max_layers=None, filter=None)` converts an already loaded model in place. This is the runtime defusion path for supported post-load expert and MLP conversions, including `qwen3_5_moe` style checkpoints.
37
+
-🧪 Defuser is designed and CI-tested for `transformers>=5.3.0`, and support is only offered for that version range. Older versions log a warning on these public APIs and are skipped as unsupported.
38
+
-🧭 Some model families appear in both support tables. Full models can be prepatched with `replace_fused_blocks(...)`, while standalone fused expert modules from those same families can still be runtime-defused with `convert_model(...)`.
39
39
40
40
`filter` is an optional list of PCRE regex rules evaluated against full module paths such as `model.layers.0.mlp.experts`:
41
41
42
-
-`+:regex` explicitly includes matching candidate module paths
-🛡️ negative rules take priority over positive rules
46
+
-🎯 when `filter` is provided, a candidate module is defused only if it matches at least one positive rule and no negative rules
47
47
48
-
## Supported Models
48
+
## ✅ Supported Models
49
49
50
50
Defuser currently supports the following `transformers>=5.3.0``model_type` values.
51
51
52
-
### `replace_fused_blocks(model_type)` before load
52
+
### 🧰 `replace_fused_blocks(model_type)` before load
53
53
54
-
| Model type | Defused op performed |
54
+
| Model type | Defused op performed ⚙️ |
55
55
| --- | --- |
56
56
|`glm4_moe`| Replaces `Glm4MoeMoE` with a defused per-expert linear MoE block. |
57
57
|`glm4_moe_lite`| Replaces `Glm4MoeLiteMoE` with a defused per-expert linear MoE block.|
@@ -62,11 +62,11 @@ Defuser currently supports the following `transformers>=5.3.0` `model_type` valu
62
62
|`qwen3_next`| Replaces `Qwen3NextSparseMoeBlock` with a defused per-expert linear MoE block. |
63
63
|`qwen3_omni_moe`| Replaces both thinker and talker text sparse MoE blocks with defused per-expert linear blocks and applies small runtime compatibility patches for text `forward()` and `generate()`. |
64
64
65
-
### `convert_model(model)` after load
65
+
### 🔄 `convert_model(model)` after load
66
66
67
-
| Pattern | Supported model types | Defused op performed |
67
+
| Pattern | Supported model types | Defused op performed ⚙️ |
68
68
| --- | --- | --- |
69
-
| Standard routed expert tensors |`deepseek_v2`, `dots1`, `ernie4_5_moe`, `ernie4_5_vl_moe`, `exaone_moe`, `flex_olmo`, `glm4_moe_lite`, `glm4v_moe`, `hunyuan_v1_moe`, `jamba`, `lfm2_moe`, `minimax`, `minimax_m2`, `olmoe`, `qwen3_vl_moe`, `solar_open`| Splits fused expert tensors or registered expert buffers into numbered expert `nn.Linear` modules with per-expert `gate_proj`, `up_proj`, and `down_proj`. |
69
+
| Standard routed expert tensors 🧱 |`deepseek_v2`, `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`| 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
70
| Mixed sparse and shared experts |`deepseek_v3`, `glm_moe_dsa`, `qwen3_5_moe`, `qwen3_5_moe_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. |
@@ -76,7 +76,7 @@ Defuser currently supports the following `transformers>=5.3.0` `model_type` valu
76
76
| Routed experts with identity experts |`longcat_flash`| Defuses routed experts into numbered `gate_proj`, `up_proj`, and `down_proj` modules and preserves zero or identity experts. |
77
77
| Fused dense `gate_up_proj` MLPs |`dia`, `glm`, `glm4`, `glm_image`, `glm_ocr`, `phi3`, `phi4_multimodal`, `zamba2`| Splits fused dense `gate_up_proj` layers into `gate_proj` + `up_proj` and updates the block `forward()` to preserve the original MLP math. |
78
78
79
-
## Workflow Summary
79
+
## 🔁 Workflow Summary
80
80
81
81
Use `replace_fused_blocks()` for model families that Defuser can patch before load:
`convert_model(model)` also preserves meta-device construction for supported meta-initialized models, so structural validation can run without materializing weights.
105
105
106
-
Use `filter` when only specific blocks should be defused:
106
+
Use `filter` when only specific blocks should be defused 🎯:
107
107
108
108
```python
109
109
from defuser import convert_model
@@ -117,11 +117,11 @@ convert_model(
117
117
)
118
118
```
119
119
120
-
## Real Qwen3.5 MoE Example
120
+
## 🧪 Real Qwen3.5 MoE Example
121
121
122
122
The example below is written for the `transformers==5.3.0` public API surface and uses the real Hugging Face model `Qwen/Qwen3.5-35B-A3B-Instruct`. Defuser supports `transformers>=5.3.0`.
0 commit comments