Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Auto-enable grouped MoE on loaded / PEFT'd models via loader hook (#6727)

Daniel Hanc7b8666ce43d
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index 417a88f480f..564be095780 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -3824,4 +3824,17 @@ class FastLlamaModel:
from .rl import PatchFastRL
+# Auto-enable grouped-GEMM MoE (tf<5 ModuleList experts) on built / PEFT'd models. Wrap the
+# loader leaves before PatchFastRL so downstream patchers see the wrapped versions. Guarded.
+try:
+ from unsloth_zoo.temporary_patches.moe_grouped_modulelist import wrap_loader_for_grouped_moe
+ FastLlamaModel.from_pretrained = staticmethod(
+ wrap_loader_for_grouped_moe(FastLlamaModel.from_pretrained)
+ )
+ FastLlamaModel.get_peft_model = staticmethod(
+ wrap_loader_for_grouped_moe(FastLlamaModel.get_peft_model)
+ )
+except Exception:
+ pass
+
PatchFastRL(FastLanguageModel = FastLlamaModel)
diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py
index 22cb65dc4a5..2818b6ee80d 100644
--- a/unsloth/models/loader.py
+++ b/unsloth/models/loader.py
@@ -896,6 +896,15 @@ class FastLanguageModel(FastLlamaModel):
)
# Patch it as well!
model = dispatch_model.patch_peft_model(model, use_gradient_checkpointing)
+ # Re-evaluate grouped MoE now the adapter is attached: an expert-LoRA block falls back
+ # to the original loop, an attention-only adapter keeps the grouped path. Guarded.
+ try:
+ from unsloth_zoo.temporary_patches.moe_grouped_modulelist import (
+ auto_enable_grouped_moe,
+ )
+ auto_enable_grouped_moe(model)
+ except Exception:
+ pass # optional speedup; never block model loading
# Patch Tiled MLP
# to turn on set UNSLOTH_TILED_MLP to "arctic", "target", or "target:{GB}""
@@ -1852,6 +1861,15 @@ class FastModel(FastBaseModel):
model = FastBaseModel.post_patch_model(
model, use_gradient_checkpointing, trust_remote_code = trust_remote_code
)
+ # Re-evaluate grouped MoE now the adapter is attached: an expert-LoRA block falls back
+ # to the original loop, an attention-only adapter keeps the grouped path. Guarded.
+ try:
+ from unsloth_zoo.temporary_patches.moe_grouped_modulelist import (
+ auto_enable_grouped_moe,
+ )
+ auto_enable_grouped_moe(model)
+ except Exception:
+ pass # optional speedup; never block model loading
# Apply QAT if specified
if qat_scheme is not None:
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index e80ed8917bc..0a68a49fee6 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -2304,3 +2304,16 @@ def check_dataset_for_missing_videos(
warnings.warn(error_msg, stacklevel = 2)
return missing
+
+
+# Auto-enable grouped-GEMM MoE (transformers<5 ModuleList experts); see llama.py.
+try:
+ from unsloth_zoo.temporary_patches.moe_grouped_modulelist import wrap_loader_for_grouped_moe
+ FastBaseModel.from_pretrained = staticmethod(
+ wrap_loader_for_grouped_moe(FastBaseModel.from_pretrained)
+ )
+ FastBaseModel.get_peft_model = staticmethod(
+ wrap_loader_for_grouped_moe(FastBaseModel.get_peft_model)
+ )
+except Exception:
+ pass