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.

Fix gpt temporary patch for grpo to happen after compile (#4180)

DoubleMathewc3b7614bd587
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index 03cdcd92fa0..89ced4f7c43 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -254,8 +254,21 @@ def prefer_flex_attn_if_supported(model_class, config):
return None
-for temporary_patch in TEMPORARY_PATCHES:
- temporary_patch()
+def _run_temporary_patches(phase):
+ import inspect
+
+ for temporary_patch in TEMPORARY_PATCHES:
+ try:
+ sig = inspect.signature(temporary_patch)
+ if "phase" in sig.parameters:
+ temporary_patch(phase = phase)
+ else:
+ temporary_patch()
+ except (ValueError, TypeError):
+ temporary_patch()
+
+
+_run_temporary_patches("init")
# =============================================
# Disable some warnings which can get annoying
@@ -2095,8 +2108,7 @@ def unsloth_compile_transformers(
# Run patches BEFORE compiler so class replacements (e.g. GptOssTopKRouter,
# GptOssExperts) are in place before the compiler caches references to them.
- for temporary_patch in TEMPORARY_PATCHES:
- temporary_patch()
+ _run_temporary_patches("pre_compile")
for model_type in model_types:
_unsloth_compile_transformers(
@@ -2128,8 +2140,7 @@ def unsloth_compile_transformers(
supports_sdpa = supports_sdpa,
)
# Redo patches which override compiler
- for temporary_patch in TEMPORARY_PATCHES:
- temporary_patch()
+ _run_temporary_patches("post_compile")
return model_types, supports_sdpa[0]