Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

support qwen3-next/glm4-moe/deepseekv3 monkey patch for transformers 5 (#1367)

Wenhua Cheng0c0a0c1d7f79
diff --git a/README.md b/README.md
index eb47c0286..11edde19b 100644
--- a/README.md
+++ b/README.md
@@ -117,11 +117,14 @@ pip install auto-round-hpu
## Model Quantization (CPU/Intel GPU/Gaudi/CUDA)
+>If you encounter issues during quantization, try using pure RTN mode with iters=0, disable_opt_rtn=True. Additionally, using group_size=32 or mixed bits is recommended for better results..
+
### CLI Usage
The full list of supported arguments is provided by calling `auto-round -h` on the terminal.
> **ModelScope is supported for model downloads, simply set `AR_USE_MODELSCOPE=1`.**
+
```bash
auto-round \
--model Qwen/Qwen3-0.6B \
@@ -267,10 +270,6 @@ ar.quantize_and_save()
### API Usage for VLMs
-If you encounter issues during quantization, try setting iters=0 (to enable RTN) and group_size=32 for better
-results.
-
-
<details>
<summary>Click to expand</summary>
diff --git a/auto_round/compressors/base.py b/auto_round/compressors/base.py
index a1c12631c..9ea366e09 100644
--- a/auto_round/compressors/base.py
+++ b/auto_round/compressors/base.py
@@ -57,8 +57,8 @@ from auto_round.data_type.utils import reshape_pad_tensor_by_group_size
from auto_round.export.export_to_gguf.config import GGUF_INNER_CONFIG
from auto_round.formats import OutputFormat, get_formats
from auto_round.logger import logger
-from auto_round.modeling.fused_moe import apply_model_monkey_patches
-from auto_round.modeling.unfused_moe.replace_modules import materialize_model_, safe_to_cpu_
+from auto_round.modeling.fused_moe.replace_modules import materialize_model_, safe_to_cpu_
+from auto_round.modeling.unfused_moe import apply_model_monkey_patches
from auto_round.schemes import (
QuantizationScheme,
_handle_special_schemes,
diff --git a/auto_round/inference/convert_model.py b/auto_round/inference/convert_model.py
index ced2d3f2b..d83f8bc5c 100644
--- a/auto_round/inference/convert_model.py
+++ b/auto_round/inference/convert_model.py
@@ -30,7 +30,7 @@ from auto_round.inference.backend import (
)
from auto_round.inference.utils import _expand_regex_config
from auto_round.logger import logger
-from auto_round.modeling.fused_moe import apply_modeling_patch
+from auto_round.modeling.unfused_moe import apply_modeling_patch
from auto_round.schemes import QuantizationScheme
from auto_round.special_model_handler import update_module
from auto_round.utils import (
diff --git a/auto_round/modeling/fused_moe/__init__.py b/auto_round/modeling/fused_moe/__init__.py
index 1b50f0e41..14a492441 100644
--- a/auto_round/modeling/fused_moe/__init__.py
+++ b/auto_round/modeling/fused_moe/__init__.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2025 Intel Corporation
+# Copyright (c) 2026 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -11,183 +11,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-import os
-from auto_round import envs
-import transformers
-import importlib
-from packaging import version
-from transformers import AutoConfig
-
-from auto_round.logger import logger
-import torch
-
-MODEL_CONFIG = {
- "qwen3_moe": {
- "min_transformers_version": "5.0.0",
- "max_transformers_version": None,
- "checkpoint_mapping": [],
- "block_patch": [
- (
- "transformers.models.qwen3_moe.modeling_qwen3_moe.Qwen3MoeSparseMoeBlock",
- "auto_round.modeling.fused_moe.qwen3_moe.LinearQwen3MoeSparseMoeBlock",
- )
- ],
- },
- "glm4_moe_lite": {
- "min_transformers_version": "5.0.0",
- "max_transformers_version": None,
- "checkpoint_mapping": [],
- "block_patch": [
- (
- "transformers.models.glm4_moe_lite.modeling_glm4_moe_lite.Glm4MoeLiteMoE",
- "auto_round.modeling.fused_moe.glm_moe_light.LinearGlm4MoeLiteMoE",
- )
- ],
- },
-}
-
-
-def get_checkpoint_conversion_mapping_ar(model_type):
- from transformers import conversion_mapping
-
- if not hasattr(conversion_mapping, "orig_get_checkpoint_conversion_mapping"):
- conversion_mapping.orig_get_checkpoint_conversion_mapping = conversion_mapping.get_checkpoint_conversion_mapping
-
- cfg = MODEL_CONFIG.get(model_type)
- if cfg:
- return cfg.get("checkpoint_mapping", [])
-
- from transformers import conversion_mapping
-
- return conversion_mapping.orig_get_checkpoint_conversion_mapping(model_type)
-
-
-def get_file_path_via_model_name(model_or_path: str, file_name):
- from huggingface_hub import hf_hub_download
-
- # 1) local folder
- if os.path.isdir(model_or_path):
- index_path = os.path.join(model_or_path, file_name)
-
- # 2) HF model name
- elif not envs.AR_USE_MODELSCOPE:
- index_path = hf_hub_download(
- repo_id=model_or_path,
- filename=file_name,
- repo_type="model",
- )
- elif envs.AR_USE_MODELSCOPE:
- from modelscope import snapshot_download # pylint: disable=E0401
-
- # ModelSCOPE is different, it returns the folder path
- folder = snapshot_download(model_or_path, allow_patterns=[file_name])
- index_path = os.path.join(folder, file_name)
- else:
- index_path = None
-
- return index_path
-
-
-def pre_check_config(model_name: str | torch.nn.Module):
- if isinstance(model_name, str):
- config = AutoConfig.from_pretrained(model_name)
- elif isinstance(model_name, torch.nn.Module):
- config = getattr(model_name, "config", None)
- if config is None:
- return False
-
- model_type = getattr(config, "model_type", None)
- if model_type is None or model_type not in MODEL_CONFIG:
- return False
-
- cfg = MODEL_CONFIG[model_type]
-
- min_ver = cfg.get("min_transformers_version")
- max_ver = cfg.get("max_transformers_version")
- tf_ver = version.parse(transformers.__version__)
- if min_ver and tf_ver < version.parse(min_ver):
- return False
- if max_ver and tf_ver > version.parse(max_ver):
- return False
- try:
- file_path = get_file_path_via_model_name(model_name, "model.safetensors.index.json")
- if os.path.exists(file_path):
- import json
-
- with open(file_path, "r") as f:
- index_data = json.load(f)
- model_keys = list(index_data.get("weight_map", {}).keys())
- for key in model_keys:
- if "gate_up_proj" in key:
- return False
- except:
- return True
- return True
-
-
-# This is for model checkpoint with linear definition
-def apply_model_monkey_patches(model_name: str) -> bool:
- res = pre_check_config(model_name)
- if not res:
- return False
- # patch blocks
- config = AutoConfig.from_pretrained(model_name)
- model_type = getattr(config, "model_type")
-
- cfg = MODEL_CONFIG[model_type]
- for orig_path, custom_path in cfg.get("block_patch", []):
- orig_module_path, orig_class_name = orig_path.rsplit(".", 1)
- custom_module_path, custom_class_name = custom_path.rsplit(".", 1)
-
- try:
- orig_module = importlib.import_module(orig_module_path)
- custom_module = importlib.import_module(custom_module_path)
- custom_class = getattr(custom_module, custom_class_name)
- setattr(orig_module, orig_class_name, custom_class)
-
- if version.parse(transformers.__version__) >= version.parse("5.0.0"):
- from transformers import conversion_mapping
-
- if not hasattr(conversion_mapping, "orig_get_checkpoint_conversion_mapping"):
- conversion_mapping.orig_get_checkpoint_conversion_mapping = (
- conversion_mapping.get_checkpoint_conversion_mapping
- )
-
- conversion_mapping.get_checkpoint_conversion_mapping = get_checkpoint_conversion_mapping_ar
- transformers.modeling_utils.get_checkpoint_conversion_mapping = get_checkpoint_conversion_mapping_ar
- logger.info(f"Patched {orig_path} -> {custom_path}")
- return True
-
- except Exception as e:
- logger.warning(f"Failed to patch {orig_path}: {e}")
- return False
- return False
-
-
-def apply_modeling_patch(model: torch.nn.Module) -> bool:
- res = pre_check_config(model)
- if not res:
- return False
- model_type = getattr(model.config, "model_type")
- cfg = MODEL_CONFIG[model_type]
- # patch blocks
- for orig_path, custom_path in cfg.get("block_patch", []):
- orig_module_path, orig_class_name = orig_path.rsplit(".", 1)
- custom_module_path, custom_class_name = custom_path.rsplit(".", 1)
- try:
- orig_module = importlib.import_module(orig_module_path)
- custom_module = importlib.import_module(custom_module_path)
- custom_class = getattr(custom_module, custom_class_name)
- orig_class = getattr(orig_module, orig_class_name)
- names = []
- for n, m in model.named_modules():
- if isinstance(m, orig_class):
- names.append(n)
- for n in names:
- model.set_submodule(n, custom_class(model.config), True)
- logger.info(f"Patched module: {orig_path} -> {custom_path}")
- return True
- except Exception as e:
- logger.warning(f"Failed to patch {orig_path}: {e}")
- return False
- return False
diff --git a/auto_round/modeling/unfused_moe/deepseek_v2.py b/auto_round/modeling/fused_moe/deepseek_v2.py
similarity index 98%
rename from auto_round/modeling/unfused_moe/deepseek_v2.py
rename to auto_round/modeling/fused_moe/deepseek_v2.py
index 0f026aac4..c7d386763 100644
--- a/auto_round/modeling/unfused_moe/deepseek_v2.py
+++ b/auto_round/modeling/fused_moe/deepseek_v2.py
@@ -25,7 +25,7 @@ from transformers.models.deepseek_v2.modeling_deepseek_v2 import (
eager_attention_forward,
)
-from auto_round.modeling.unfused_moe.replace_modules import ReplacementModuleBase
+from auto_round.modeling.fused_moe.replace_modules import ReplacementModuleBase
from auto_round.utils import is_hpex_available
diff --git a/auto_round/modeling/unfused_moe/gpt_oss.py b/auto_round/modeling/fused_moe/gpt_oss.py
similarity index 97%
rename from auto_round/modeling/unfused_moe/gpt_oss.py
rename to auto_round/modeling/fused_moe/gpt_oss.py
index 96caeb71d..998d2dbda 100644
--- a/auto_round/modeling/unfused_moe/gpt_oss.py
+++ b/auto_round/modeling/fused_moe/gpt_oss.py
@@ -27,8 +27,8 @@ else:
from transformers.models.gpt_oss.configuration_gpt_oss import GptOssConfig
from transformers.models.gpt_oss.modeling_gpt_oss import GptOssMLP
-from auto_round.modeling.unfused_moe.replace_modules import ReplacementModuleBase
-from auto_round.modeling.unfused_moe.utils import _update_parameter
+from auto_round.modeling.fused_moe.replace_modules import ReplacementModuleBase
+from auto_round.modeling.fused_moe.utils import _update_parameter
from auto_round.utils import clear_memory, unsupported_meta_device
diff --git a/auto_round/modeling/unfused_moe/llama4.py b/auto_round/modeling/fused_moe/llama4.py
similarity index 96%
rename from auto_round/modeling/unfused_moe/llama4.py
rename to auto_round/modeling/fused_moe/llama4.py
index 5e6adc274..863db80f7 100644
--- a/auto_round/modeling/unfused_moe/llama4.py
+++ b/auto_round/modeling/fused_moe/llama4.py
@@ -23,8 +23,8 @@ else:
from transformers.initialization import no_init_weights
from transformers.models.llama4.modeling_llama4 import Llama4Config, Llama4TextMLP
-from auto_round.modeling.unfused_moe.replace_modules import ReplacementModuleBase
-from auto_round.modeling.unfused_moe.utils import _update_parameter
+from auto_round.modeling.fused_moe.replace_modules import ReplacementModuleBase
+from auto_round.modeling.fused_moe.utils import _update_parameter
from auto_round.utils import clear_memory, unsupported_meta_device
diff --git a/auto_round/modeling/unfused_moe/qwen3_vl_moe.py b/auto_round/modeling/fused_moe/qwen3_vl_moe.py
similarity index 97%
rename from auto_round/modeling/unfused_moe/qwen3_vl_moe.py
rename to auto_round/modeling/fused_moe/qwen3_vl_moe.py
index 1c0d62a29..f39cddc51 100644
--- a/auto_round/modeling/unfused_moe/qwen3_vl_moe.py
+++ b/auto_round/modeling/fused_moe/qwen3_vl_moe.py
@@ -16,13 +16,13 @@ import torch
import transformers
from packaging import version
-from auto_round.modeling.unfused_moe.replace_modules import ReplacementModuleBase
+from auto_round.modeling.fused_moe.replace_modules import ReplacementModuleBase
from auto_round.utils import clear_memory, unsupported_meta_device
transformers_version = version.parse(transformers.__version__)
from typing import TYPE_CHECKING
-from auto_round.modeling.unfused_moe.utils import _update_parameter
+from auto_round.modeling.fused_moe.utils import _update_parameter
if TYPE_CHECKING:
from transformers import Qwen3VLMoeConfig, Qwen3VLMoeTextConfig
diff --git a/auto_round/modeling/unfused_moe/replace_modules.py b/auto_round/modeling/fused_moe/replace_modules.py
similarity index 98%
rename from auto_round/modeling/unfused_moe/replace_modules.py
rename to auto_round/modeling/fused_moe/replace_modules.py
index c62345fa1..66222f414 100644
--- a/auto_round/modeling/unfused_moe/replace_modules.py
+++ b/auto_round/modeling/fused_moe/replace_modules.py
@@ -23,10 +23,10 @@ from transformers import PreTrainedModel
from auto_round.utils import LazyImport, dump_mem_usage, dump_memory_usage_ctx, global_state, logger
BUILTIN_MODULES = {
- "Llama4TextMoe": LazyImport("auto_round.modeling.unfused_moe.llama4"),
- "GptOssMLP": LazyImport("auto_round.modeling.unfused_moe.gpt_oss"),
- "Qwen3VLMoeTextSparseMoeBlock": LazyImport("auto_round.modeling.unfused_moe.qwen3_vl_moe"),
- "DeepseekV2Attention": LazyImport("auto_round.modeling.unfused_moe.deepseek_v2"),
+ "Llama4TextMoe": LazyImport("auto_round.modeling.fused_moe.llama4"),
+ "GptOssMLP": LazyImport("auto_round.modeling.fused_moe.gpt_oss"),
+ "Qwen3VLMoeTextSparseMoeBlock": LazyImport("auto_round.modeling.fused_moe.qwen3_vl_moe"),
+ "DeepseekV2Attention": LazyImport("auto_round.modeling.fused_moe.deepseek_v2"),
}
diff --git a/auto_round/modeling/unfused_moe/utils.py b/auto_round/modeling/fused_moe/utils.py
similarity index 100%
rename from auto_round/modeling/unfused_moe/utils.py
rename to auto_round/modeling/fused_moe/utils.py
diff --git a/auto_round/modeling/unfused_moe/__init__.py b/auto_round/modeling/unfused_moe/__init__.py
index 14a492441..a30e2ae45 100644
--- a/auto_round/modeling/unfused_moe/__init__.py
+++ b/auto_round/modeling/unfused_moe/__init__.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2026 Intel Corporation
+# Copyright (c) 2025 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -11,3 +11,227 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
+import os
+from auto_round import envs
+import transformers
+import importlib
+from packaging import version
+from transformers import AutoConfig
+
+from auto_round.logger import logger
+import torch
+
+MODEL_CONFIG = {
+ "qwen3_moe": {
+ "min_transformers_version": "5.0.0",
+ "max_transformers_version": "6.0.0",
+ "checkpoint_mapping": [],
+ "block_patch": [
+ (
+ "transformers.models.qwen3_moe.modeling_qwen3_moe.Qwen3MoeSparseMoeBlock",
+ "auto_round.modeling.unfused_moe.qwen3_moe.LinearQwen3MoeSparseMoeBlock",
+ )
+ ],
+ },
+ "glm4_moe_lite": {
+ "min_transformers_version": "5.0.0",
+ "max_transformers_version": "6.0.0",
+ "checkpoint_mapping": [],
+ "block_patch": [
+ (
+ "transformers.models.glm4_moe_lite.modeling_glm4_moe_lite.Glm4MoeLiteMoE",
+ "auto_round.modeling.unfused_moe.glm_moe_light.LinearGlm4MoeLiteMoE",
+ )
+ ],
+ },
+ "glm4_moe": {
+ "min_transformers_version": "5.0.0",
+ "max_transformers_version": "6.0.0",
+ "checkpoint_mapping": [],
+ "block_patch": [
+ (
+ "transformers.models.glm4_moe.modeling_glm4_moe.Glm4MoeMoE",
+ "auto_round.modeling.unfused_moe.glm_moe.LinearGlm4MoeMoE",
+ )
+ ],
+ },
+ "qwen3_next": {
+ "min_transformers_version": "5.0.0",
+ "max_transformers_version": "6.0.0",
+ "checkpoint_mapping": [],
+ "block_patch": [
+ (
+ "transformers.models.qwen3_next.modeling_qwen3_next.Qwen3NextSparseMoeBlock",
+ "auto_round.modeling.unfused_moe.qwen3_next.LinearQwen3NextSparseMoeBlock",
+ )
+ ],
+ },
+ "deepseek_v3": {
+ "min_transformers_version": "5.0.0",
+ "max_transformers_version": "6.0.0",
+ "checkpoint_mapping": [],
+ "block_patch": [
+ (
+ "transformers.models.deepseek_v3.modeling_deepseek_v3.DeepseekV3MoE",
+ "auto_round.modeling.unfused_moe.deepseek_v3.LinearDeepseekV3MoE",
+ )
+ ],
+ },
+ "ernie4_5_moe": {
+ "min_transformers_version": "5.0.0",
+ "max_transformers_version": "6.0.0",
+ "checkpoint_mapping": [],
+ "block_patch": [
+ (
+ "transformers.models.ernie4_5_moe.modeling_ernie4_5_moe.Ernie4_5_MoeSparseMoeBlock",
+ "auto_round.modeling.unfused_moe.ernie4_5_moe.LinearErnie4_5_MoeSparseMoeBlock",
+ )
+ ],
+ },
+}
+
+
+def get_checkpoint_conversion_mapping_ar(model_type):
+ from transformers import conversion_mapping
+
+ if not hasattr(conversion_mapping, "orig_get_checkpoint_conversion_mapping"):
+ conversion_mapping.orig_get_checkpoint_conversion_mapping = conversion_mapping.get_checkpoint_conversion_mapping
+
+ cfg = MODEL_CONFIG.get(model_type)
+ if cfg:
+ return cfg.get("checkpoint_mapping", [])
+
+ from transformers import conversion_mapping
+
+ return conversion_mapping.orig_get_checkpoint_conversion_mapping(model_type)
+
+
+def get_file_path_via_model_name(model_or_path: str, file_name):
+ from huggingface_hub import hf_hub_download
+
+ # 1) local folder
+ if os.path.isdir(model_or_path):
+ index_path = os.path.join(model_or_path, file_name)
+
+ # 2) HF model name
+ elif not envs.AR_USE_MODELSCOPE:
+ index_path = hf_hub_download(
+ repo_id=model_or_path,
+ filename=file_name,
+ repo_type="model",
+ )
+ elif envs.AR_USE_MODELSCOPE:
+ from modelscope import snapshot_download # pylint: disable=E0401
+
+ # ModelSCOPE is different, it returns the folder path
+ folder = snapshot_download(model_or_path, allow_patterns=[file_name])
+ index_path = os.path.join(folder, file_name)
+ else:
+ index_path = None
+
+ return index_path
+
+
+def pre_check_config(model_name: str | torch.nn.Module):
+ if isinstance(model_name, str):
+ config = AutoConfig.from_pretrained(model_name)
+ elif isinstance(model_name, torch.nn.Module):
+ config = getattr(model_name, "config", None)
+ if config is None:
+ return False
+
+ model_type = getattr(config, "model_type", None)
+ if model_type is None or model_type not in MODEL_CONFIG:
+ return False
+
+ cfg = MODEL_CONFIG[model_type]
+
+ min_ver = cfg.get("min_transformers_version")
+ max_ver = cfg.get("max_transformers_version")
+ tf_ver = version.parse(transformers.__version__)
+ if min_ver and tf_ver < version.parse(min_ver):
+ return False
+ if max_ver and tf_ver > version.parse(max_ver):
+ return False
+ try:
+ file_path = get_file_path_via_model_name(model_name, "model.safetensors.index.json")
+ if os.path.exists(file_path):
+ import json
+
+ with open(file_path, "r") as f:
+ index_data = json.load(f)
+ model_keys = list(index_data.get("weight_map", {}).keys())
+ for key in model_keys:
+ if "gate_up_proj" in key:
+ return False
+ except:
+ return True
+ return True
+
+
+# This is for model checkpoint with linear definition
+def apply_model_monkey_patches(model_name: str) -> bool:
+ res = pre_check_config(model_name)
+ if not res:
+ return False
+ # patch blocks
+ config = AutoConfig.from_pretrained(model_name)
+ model_type = getattr(config, "model_type")
+
+ cfg = MODEL_CONFIG[model_type]
+ for orig_path, custom_path in cfg.get("block_patch", []):
+ orig_module_path, orig_class_name = orig_path.rsplit(".", 1)
+ custom_module_path, custom_class_name = custom_path.rsplit(".", 1)
+
+ try:
+ orig_module = importlib.import_module(orig_module_path)
+ custom_module = importlib.import_module(custom_module_path)
+ custom_class = getattr(custom_module, custom_class_name)
+ setattr(orig_module, orig_class_name, custom_class)
+
+ if version.parse(transformers.__version__) >= version.parse("5.0.0"):
+ from transformers import conversion_mapping
+
+ if not hasattr(conversion_mapping, "orig_get_checkpoint_conversion_mapping"):
+ conversion_mapping.orig_get_checkpoint_conversion_mapping = (
+ conversion_mapping.get_checkpoint_conversion_mapping
+ )
+
+ conversion_mapping.get_checkpoint_conversion_mapping = get_checkpoint_conversion_mapping_ar
+ transformers.modeling_utils.get_checkpoint_conversion_mapping = get_checkpoint_conversion_mapping_ar
+ logger.info(f"Patched {orig_path} -> {custom_path}")
+ return True
+
+ except Exception as e:
+ logger.warning(f"Failed to patch {orig_path}: {e}")
+ return False
+ return False
+
+
+def apply_modeling_patch(model: torch.nn.Module) -> bool:
+ res = pre_check_config(model)
+ if not res:
+ return False
+ model_type = getattr(model.config, "model_type")
+ cfg = MODEL_CONFIG[model_type]
+ # patch blocks
+ for orig_path, custom_path in cfg.get("block_patch", []):
+ orig_module_path, orig_class_name = orig_path.rsplit(".", 1)
+ custom_module_path, custom_class_name = custom_path.rsplit(".", 1)
+ try:
+ orig_module = importlib.import_module(orig_module_path)
+ custom_module = importlib.import_module(custom_module_path)
+ custom_class = getattr(custom_module, custom_class_name)
+ orig_class = getattr(orig_module, orig_class_name)
+ names = []
+ for n, m in model.named_modules():
+ if isinstance(m, orig_class):
+ names.append(n)
+ for n in names:
+ model.set_submodule(n, custom_class(model.config), True)
+ logger.info(f"Patched module: {orig_path} -> {custom_path}")
+ return True
+ except Exception as e:
+ logger.warning(f"Failed to patch {orig_path}: {e}")
+ return False
+ return False
diff --git a/auto_round/modeling/unfused_moe/deepseek_v3.py b/auto_round/modeling/unfused_moe/deepseek_v3.py
new file mode 100644
index 000000000..749e075b4
--- /dev/null
+++ b/auto_round/modeling/unfused_moe/deepseek_v3.py
@@ -0,0 +1,105 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import torch
+import torch.nn as nn
+
+
+class LinearDeepseekV3MoE(nn.Module):
+ """
+ A mixed expert module containing shared experts.
+ """
+
+ def __init__(self, config):
+ super().__init__()
+ self.config = config
+ from transformers.models.deepseek_v3.modeling_deepseek_v3 import DeepseekV3MLP, DeepseekV3TopkRouter
+
+ self.num_experts = config.num_local_experts # needed
+ self.experts = nn.ModuleList(
+ [DeepseekV3MLP(config, intermediate_size=config.moe_intermediate_size) for _ in range(self.num_experts)]
+ )
+ self.gate = DeepseekV3TopkRouter(config)
+ self.shared_experts = DeepseekV3MLP(
+ config=config, intermediate_size=config.moe_intermediate_size * config.n_shared_experts
+ )
+ self.n_routed_experts = config.n_routed_experts
+ self.n_group = config.n_group
+ self.topk_group = config.topk_group
+ self.norm_topk_prob = config.norm_topk_prob
+ self.routed_scaling_factor = config.routed_scaling_factor
+ self.top_k = config.num_experts_per_tok
+
+ def experts_forward(
+ self,
+ hidden_states: torch.Tensor,
+ top_k_index: torch.Tensor,
+ top_k_weights: torch.Tensor,
+ ) -> torch.Tensor:
+ final_hidden_states = torch.zeros_like(hidden_states)
+ with torch.no_grad():
+ expert_mask = torch.nn.functional.one_hot(top_k_index, num_classes=self.num_experts)
+ expert_mask = expert_mask.permute(2, 1, 0)
+ expert_hit = torch.greater(expert_mask.sum(dim=(-1, -2)), 0).nonzero()
+
+ for expert_idx in expert_hit:
+ expert_idx = expert_idx[0]
+ if expert_idx == self.num_experts:
+ continue
+ top_k_pos, token_idx = torch.where(expert_mask[expert_idx])
+ current_state = hidden_states[token_idx]
+ # gate, up = nn.functional.linear(current_state, self.gate_up_proj[expert_idx]).chunk(2, dim=-1)
+ # current_hidden_states = self.act_fn(gate) * up
+ # current_hidden_states = nn.functional.linear(current_hidden_states, self.down_proj[expert_idx])
+ expert_layer = self.experts[expert_idx]
+ current_hidden_states = expert_layer(current_state)
+ current_hidden_states = current_hidden_states * top_k_weights[token_idx, top_k_pos, None]
+ final_hidden_states.index_add_(0, token_idx, current_hidden_states.to(final_hidden_states.dtype))
+
+ return final_hidden_states
+
+ def route_tokens_to_experts(self, router_logits):
+ router_logits = router_logits.sigmoid()
+ router_logits_for_choice = router_logits + self.gate.e_score_correction_bias
+ group_scores = (
+ router_logits_for_choice.view(-1, self.n_group, self.n_routed_experts // self.n_group)
+ .topk(2, dim=-1)[0]
+ .sum(dim=-1)
+ )
+ group_idx = torch.topk(group_scores, k=self.topk_group, dim=-1, sorted=False)[1]
+ group_mask = torch.zeros_like(group_scores)
+ group_mask.scatter_(1, group_idx, 1)
+ score_mask = (
+ group_mask.unsqueeze(-1)
+ .expand(-1, self.n_group, self.n_routed_experts // self.n_group)
+ .reshape(-1, self.n_routed_experts)
+ )
+ scores_for_choice = router_logits_for_choice.masked_fill(~score_mask.bool(), 0.0)
+ topk_indices = torch.topk(scores_for_choice, k=self.top_k, dim=-1, sorted=False)[1]
+ topk_weights = router_logits.gather(1, topk_indices)
+ if self.norm_topk_prob:
+ denominator = topk_weights.sum(dim=-1, keepdim=True) + 1e-20
+ topk_weights /= denominator
+ topk_weights = topk_weights * self.routed_scaling_factor
+ return topk_indices, topk_weights
+
+ def forward(self, hidden_states):
+ residuals = hidden_states
+ orig_shape = hidden_states.shape
+ router_logits = self.gate(hidden_states)
+ topk_indices, topk_weights = self.route_tokens_to_experts(router_logits)
+ hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
+ hidden_states = self.experts_forward(hidden_states, topk_indices, topk_weights).view(*orig_shape)
+ hidden_states = hidden_states + self.shared_experts(residuals)
+ return hidden_states
diff --git a/auto_round/modeling/unfused_moe/ernie4_5_moe.py b/auto_round/modeling/unfused_moe/ernie4_5_moe.py
new file mode 100644
index 000000000..6fc4d9f4c
--- /dev/null
+++ b/auto_round/modeling/unfused_moe/ernie4_5_moe.py
@@ -0,0 +1,78 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import torch
+import torch.nn as nn
+
+
+class LinearErnie4_5_MoeSparseMoeBlock(nn.Module):
+ def __init__(self, config):
+ super().__init__()
+ from transformers.models.ernie4_5_moe.modeling_ernie4_5_moe import Ernie4_5_MoeMLP, Ernie4_5_MoeTopKRouter
+
+ self.hidden_dim = config.hidden_size
+ self.num_experts = config.moe_num_experts
+ self.top_k = config.moe_k
+ self.gate = Ernie4_5_MoeTopKRouter(config)
+ self.experts = nn.ModuleList(
+ [Ernie4_5_MoeMLP(config, intermediate_size=config.moe_intermediate_size) for _ in range(self.num_experts)]
+ )
+
+ self.shared_experts = None
+ if config.moe_num_shared_experts > 0:
+ self.shared_experts = Ernie4_5_MoeMLP(config, config.moe_intermediate_size * config.moe_num_shared_experts)
+
+ def experts_forward(
+ self,
+ hidden_states: torch.Tensor,
+ top_k_index: torch.Tensor,
+ top_k_weights: torch.Tensor,
+ ) -> torch.Tensor:
+ final_hidden_states = torch.zeros_like(hidden_states)
+ with torch.no_grad():
+ expert_mask = torch.nn.functional.one_hot(top_k_index, num_classes=self.num_experts)
+ expert_mask = expert_mask.permute(2, 1, 0)
+ expert_hit = torch.greater(expert_mask.sum(dim=(-1, -2)), 0).nonzero()
+
+ for expert_idx in expert_hit:
+ expert_idx = expert_idx[0]
+ if expert_idx == self.num_experts:
+ continue
+ top_k_pos, token_idx = torch.where(expert_mask[expert_idx])
+ current_state = hidden_states[token_idx]
+ # gate, up = nn.functional.linear(current_state, self.gate_up_proj[expert_idx]).chunk(2, dim=-1)
+ # current_hidden_states = self.act_fn(gate) * up
+ # current_hidden_states = nn.functional.linear(current_hidden_states, self.down_proj[expert_idx])
+ expert_layer = self.experts[expert_idx]
+ current_hidden_states = expert_layer(current_state)
+ current_hidden_states = current_hidden_states * top_k_weights[token_idx, top_k_pos, None]
+ final_hidden_states.index_add_(0, token_idx, current_hidden_states.to(final_hidden_states.dtype))
+
+ return final_hidden_states
+
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
+ batch_size, sequence_length, _ = hidden_states.shape
+ hidden_states = hidden_states.view(-1, self.hidden_dim)
+
+ if self.shared_experts is not None:
+ shared_output = self.shared_experts(hidden_states)
+
+ _, top_k_index, top_k_weights = self.gate(hidden_states)
+ final_hidden_states = self.experts_forward(hidden_states, top_k_index, top_k_weights)
+
+ if self.shared_experts is not None:
+ final_hidden_states = final_hidden_states + shared_output
+
+ final_hidden_states = final_hidden_states.reshape(batch_size, sequence_length, self.hidden_dim)
+ return final_hidden_states.to(hidden_states.dtype)
diff --git a/auto_round/modeling/unfused_moe/glm_moe.py b/auto_round/modeling/unfused_moe/glm_moe.py
new file mode 100644
index 000000000..e68ac9974
--- /dev/null
+++ b/auto_round/modeling/unfused_moe/glm_moe.py
@@ -0,0 +1,105 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import torch
+import torch.nn as nn
+
+
+class LinearGlm4MoeMoE(nn.Module):
+ """
+ A mixed expert module containing shared experts.
+ """
+
+ def __init__(self, config):
+ super().__init__()
+ from transformers.models.glm4_moe.modeling_glm4_moe import Glm4MoeMLP, Glm4MoeTopkRouter
+
+ self.config = config
+ self.num_experts = config.num_local_experts # needed
+ self.experts = nn.ModuleList(
+ [Glm4MoeMLP(config, intermediate_size=config.moe_intermediate_size) for _ in range(self.num_experts)]
+ )
+ self.gate = Glm4MoeTopkRouter(config)
+ self.shared_experts = Glm4MoeMLP(
+ config=config, intermediate_size=config.moe_intermediate_size * config.n_shared_experts
+ )
+ self.n_routed_experts = config.n_routed_experts
+ self.n_group = config.n_group
+ self.topk_group = config.topk_group
+ self.norm_topk_prob = config.norm_topk_prob
+ self.routed_scaling_factor = config.routed_scaling_factor
+ self.top_k = config.num_experts_per_tok
+
+ def experts_forward(
+ self,
+ hidden_states: torch.Tensor,
+ top_k_index: torch.Tensor,
+ top_k_weights: torch.Tensor,
+ ) -> torch.Tensor:
+ final_hidden_states = torch.zeros_like(hidden_states)
+ with torch.no_grad():
+ expert_mask = torch.nn.functional.one_hot(top_k_index, num_classes=self.num_experts)
+ expert_mask = expert_mask.permute(2, 1, 0)
+ expert_hit = torch.greater(expert_mask.sum(dim=(-1, -2)), 0).nonzero()
+
+ for expert_idx in expert_hit:
+ expert_idx = expert_idx[0]
+ if expert_idx == self.num_experts:
+ continue
+ top_k_pos, token_idx = torch.where(expert_mask[expert_idx])
+ current_state = hidden_states[token_idx]
+ # gate, up = nn.functional.linear(current_state, self.gate_up_proj[expert_idx]).chunk(2, dim=-1)
+ # current_hidden_states = self.act_fn(gate) * up
+ # current_hidden_states = nn.functional.linear(current_hidden_states, self.down_proj[expert_idx])
+ expert_layer = self.experts[expert_idx]
+ current_hidden_states = expert_layer(current_state)
+ current_hidden_states = current_hidden_states * top_k_weights[token_idx, top_k_pos, None]
+ final_hidden_states.index_add_(0, token_idx, current_hidden_states.to(final_hidden_states.dtype))
+
+ return final_hidden_states
+
+ def route_tokens_to_experts(self, router_logits):
+ router_logits = router_logits.sigmoid()
+ router_logits_for_choice = router_logits + self.gate.e_score_correction_bias
+ group_scores = (
+ router_logits_for_choice.view(-1, self.n_group, self.n_routed_experts // self.n_group)
+ .topk(2, dim=-1)[0]
+ .sum(dim=-1)
+ )
+ group_idx = torch.topk(group_scores, k=self.topk_group, dim=-1, sorted=False)[1]
+ group_mask = torch.zeros_like(group_scores)
+ group_mask.scatter_(1, group_idx, 1)
+ score_mask = (
+ group_mask.unsqueeze(-1)
+ .expand(-1, self.n_group, self.n_routed_experts // self.n_group)
+ .reshape(-1, self.n_routed_experts)
+ )
+ scores_for_choice = router_logits_for_choice.masked_fill(~score_mask.bool(), 0.0)
+ topk_indices = torch.topk(scores_for_choice, k=self.top_k, dim=-1, sorted=False)[1]
+ topk_weights = router_logits.gather(1, topk_indices)
+ if self.norm_topk_prob:
+ denominator = topk_weights.sum(dim=-1, keepdim=True) + 1e-20
+ topk_weights /= denominator
+ topk_weights = topk_weights * self.routed_scaling_factor
+ return topk_indices, topk_weights
+
+ def forward(self, hidden_states):
+ residuals = hidden_states
+ orig_shape = hidden_states.shape
+ router_logits = self.gate(hidden_states)
+ topk_indices, topk_weights = self.route_tokens_to_experts(router_logits)
+ hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
+ hidden_states = self.experts_forward(hidden_states, topk_indices, topk_weights).view(*orig_shape)
+ hidden_states = hidden_states + self.shared_experts(residuals)
+ return hidden_states
diff --git a/auto_round/modeling/fused_moe/glm_moe_light.py b/auto_round/modeling/unfused_moe/glm_moe_light.py
similarity index 100%
rename from auto_round/modeling/fused_moe/glm_moe_light.py
rename to auto_round/modeling/unfused_moe/glm_moe_light.py
diff --git a/auto_round/modeling/fused_moe/qwen3_moe.py b/auto_round/modeling/unfused_moe/qwen3_moe.py
similarity index 87%
rename from auto_round/modeling/fused_moe/qwen3_moe.py
rename to auto_round/modeling/unfused_moe/qwen3_moe.py
index 02e1bc62a..e8b9ec673 100644
--- a/auto_round/modeling/fused_moe/qwen3_moe.py
+++ b/auto_round/modeling/unfused_moe/qwen3_moe.py
@@ -26,7 +26,7 @@ class LinearQwen3MoeSparseMoeBlock(nn.Module):
self.top_k = config.num_experts_per_tok
self.norm_topk_prob = config.norm_topk_prob
- # gating
+ # This must be linear for vllm alignment
self.gate = nn.Linear(config.hidden_size, config.num_experts, bias=False)
self.experts = nn.ModuleList(
[Qwen3MoeMLP(config, intermediate_size=config.moe_intermediate_size) for _ in range(self.num_experts)]
@@ -52,11 +52,14 @@ class LinearQwen3MoeSparseMoeBlock(nn.Module):
# One hot encode the selected experts to create an expert mask
# this will be used to easily index which expert is going to be solicited
- expert_mask = torch.nn.functional.one_hot(selected_experts, num_classes=self.num_experts).permute(2, 1, 0)
+ with torch.no_grad():
+ expert_mask = torch.nn.functional.one_hot(selected_experts, num_classes=self.num_experts).permute(2, 1, 0)
- # Loop over all available experts in the model and perform the computation on each expert
- expert_hit = torch.greater(expert_mask.sum(dim=(-1, -2)), 0).nonzero()
+ # Loop over all available experts in the model and perform the computation on each expert
+ expert_hit = torch.greater(expert_mask.sum(dim=(-1, -2)), 0).nonzero()
for expert_idx in expert_hit:
+ if expert_idx == self.num_experts:
+ continue
expert_layer = self.experts[expert_idx]
idx, top_x = torch.where(expert_mask[expert_idx].squeeze(0))
diff --git a/auto_round/modeling/unfused_moe/qwen3_next.py b/auto_round/modeling/unfused_moe/qwen3_next.py
new file mode 100644
index 000000000..dfbda11d7
--- /dev/null
+++ b/auto_round/modeling/unfused_moe/qwen3_next.py
@@ -0,0 +1,83 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import torch
+import torch.nn as nn
+from torch.nn import functional as F
+
+
+class LinearQwen3NextSparseMoeBlock(nn.Module):
+ def __init__(self, config):
+ super().__init__()
+ self.num_experts = config.num_experts
+ self.top_k = config.num_experts_per_tok
+ self.norm_topk_prob = config.norm_topk_prob
+
+ # gating
+ self.gate = nn.Linear(config.hidden_size, config.num_experts, bias=False)
+ from transformers.models.qwen3_next.modeling_qwen3_next import Qwen3NextMLP
+
+ self.experts = nn.ModuleList(
+ [Qwen3NextMLP(config, intermediate_size=config.moe_intermediate_size) for _ in range(self.num_experts)]
+ )
+
+ self.shared_expert = Qwen3NextMLP(config, intermediate_size=config.shared_expert_intermediate_size)
+ self.shared_expert_gate = torch.nn.Linear(config.hidden_size, 1, bias=False)
+
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
+ """ """
+ batch_size, sequence_length, hidden_dim = hidden_states.shape
+ hidden_states = hidden_states.view(-1, hidden_dim)
+ # router_logits: (batch * sequence_length, n_experts)
+ router_logits = self.gate(hidden_states)
+
+ routing_weights = F.softmax(router_logits, dim=1, dtype=torch.float)
+ routing_weights, selected_experts = torch.topk(routing_weights, self.top_k, dim=-1)
+ if self.norm_topk_prob:
+ routing_weights /= routing_weights.sum(dim=-1, keepdim=True)
+ # we cast back to the input dtype
+ routing_weights = routing_weights.to(hidden_states.dtype)
+
+ final_hidden_states = torch.zeros(
+ (batch_size * sequence_length, hidden_dim), dtype=hidden_states.dtype, device=hidden_states.device
+ )
+
+ # One hot encode the selected experts to create an expert mask
+ # this will be used to easily index which expert is going to be sollicitated
+ with torch.no_grad():
+ expert_mask = torch.nn.functional.one_hot(selected_experts, num_classes=self.num_experts).permute(2, 1, 0)
+
+ # Loop over all available experts in the model and perform the computation on each expert
+ expert_hit = torch.greater(expert_mask.sum(dim=(-1, -2)), 0).nonzero()
+ for expert_idx in expert_hit:
+ expert_layer = self.experts[expert_idx]
+ idx, top_x = torch.where(expert_mask[expert_idx].squeeze(0))
+
+ # Index the correct hidden states and compute the expert hidden state for
+ # the current expert. We need to make sure to multiply the output hidden
+ # states by `routing_weights` on the corresponding tokens (top-1 and top-2)
+ current_state = hidden_states[None, top_x].reshape(-1, hidden_dim)
+ current_hidden_states = expert_layer(current_state) * routing_weights[top_x, idx, None]
+
+ # However `index_add_` only support torch tensors for indexing so we'll use
+ # the `top_x` tensor here.
+ final_hidden_states.index_add_(0, top_x, current_hidden_states.to(hidden_states.dtype))
+
+ shared_expert_output = self.shared_expert(hidden_states)
+ shared_expert_output = F.sigmoid(self.shared_expert_gate(hidden_states)) * shared_expert_output
+
+ final_hidden_states = final_hidden_states + shared_expert_output
+
+ final_hidden_states = final_hidden_states.reshape(batch_size, sequence_length, hidden_dim)
+ return final_hidden_states
diff --git a/auto_round/special_model_handler.py b/auto_round/special_model_handler.py
index da02b43b0..15594938d 100644
--- a/auto_round/special_model_handler.py
+++ b/auto_round/special_model_handler.py
@@ -18,7 +18,7 @@ from typing import Any, Callable
import torch
from auto_round.formats import OutputFormat
-from auto_round.modeling.unfused_moe.replace_modules import apply_replacements, release_original_module_
+from auto_round.modeling.fused_moe.replace_modules import apply_replacements, release_original_module_
from auto_round.utils import logger
mllms_with_limited_bs = ("llava", "qwen2_vl", "phi3_v", "mllama") # Limitations on batch_size
@@ -178,6 +178,28 @@ class ArchitectureMatcher:
raise ValueError(f"unsupported mode {self.mode}")
+class ModelTypeMatcher:
+ """match config.architectures"""
+
+ def __init__(self, model_type: str, mode="in"):
+ self.model_type = model_type
+ self.mode = mode
+
+ def __call__(self, model) -> bool:
+ model_type = getattr(model.config, "model_type", None)
+ if model_type is None:
+ return False
+
+ if self.mode == "full":
+ return model_type == self.model_type
+ elif self.mode == "in":
+ return self.model_type in model_type
+ elif self.mode == "regex":
+ return re.search(self.model_type, model_type) is not None
+ else:
+ raise ValueError(f"unsupported mode {self.mode}")
+
+
@dataclass
class PreDefinedIgnoreLayers:
matchers: list[Callable[[Any], bool]]
@@ -233,6 +255,16 @@ register_ignore_layers(
],
)
+# # qwen3_next
+# register_ignore_layers(
+# matchers=[
+# ModelTypeMatcher(r"qwen3_next", mode="full"),
+# ],
+# ignore_layers=[
+# "mlp.gate", # vllm issue
+# ],
+# )
+
def get_predefined_ignore_layers(model: torch.nn.Module) -> list[str]:
layers = []
diff --git a/test/test_cpu/models/test_moe_model.py b/test/test_cpu/models/test_moe_model.py
index f98a5b958..885436686 100644
--- a/test/test_cpu/models/test_moe_model.py
+++ b/test/test_cpu/models/test_moe_model.py
@@ -99,7 +99,7 @@ def test_gptoss(setup_gpt_oss, scheme):
# Ensure the quantized model is not None
assert quantized_model is not None, "Quantized model should not be None."
from auto_round.export.export_to_autoround.qlinear_fp import QuantLinear
- from auto_round.modeling.unfused_moe.gpt_oss import GPTOssSingleExpert
+ from auto_round.modeling.fused_moe.gpt_oss import GPTOssSingleExpert
single_expert_cnt = count_modules_by_type(quantized_model, GPTOssSingleExpert)
quant_linear_cnt = count_modules_by_type(quantized_model, QuantLinear)
diff --git a/test/test_cuda/models/test_moe_model.py b/test/test_cuda/models/test_moe_model.py
index 442d559ea..f6fc5af4b 100644
--- a/test/test_cuda/models/test_moe_model.py
+++ b/test/test_cuda/models/test_moe_model.py
@@ -9,7 +9,7 @@ from transformers.models.qwen3.modeling_qwen3 import Qwen3Config, Qwen3ForCausal
from transformers.models.qwen3_vl_moe.modeling_qwen3_vl_moe import Qwen3VLMoeForConditionalGeneration
from auto_round import AutoRound
-from auto_round.modeling.unfused_moe.replace_modules import ReplacementModuleBase
+from auto_round.modeling.fused_moe.replace_modules import ReplacementModuleBase
@pytest.fixture
@@ -210,7 +210,7 @@ def has_module(model: torch.nn.Module, module_name: str) -> bool:
def test_register_module_out_of_tree_base():
- from auto_round.modeling.unfused_moe.replace_modules import ReplacementModuleBase
+ from auto_round.modeling.fused_moe.replace_modules import ReplacementModuleBase
for name, subclass in ReplacementModuleBase._replacement_registry.items():
if name == "Qwen3MLP":