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 TRL 0.27-0.28 GRPOTrainer import on transformers >= 5.3 (No module named 'weave') (#15343)

Daniel Han8ed7abb00667
diff --git a/tests/test_trl_available_helper_coercion.py b/tests/test_trl_available_helper_coercion.py
new file mode 100644
index 00000000000..6a0371ca96b
--- /dev/null
+++ b/tests/test_trl_available_helper_coercion.py
@@ -0,0 +1,73 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+"""Fixtures copy trl/import_utils.py (0.26.2, 0.27.0) and transformers' helper (5.2.0, 5.3.0)."""
+
+import types
+
+import pytest
+
+from unsloth.import_fixes import _coerce_trl_available_checks
+
+INSTALLED = {"torch": "2.9.0"}
+
+
+def _transformers_5_3_helper(pkg_name, return_version = False):
+ exists = pkg_name in INSTALLED
+ return (exists, INSTALLED.get(pkg_name, "N/A")) if return_version else (exists, None)
+
+
+def _transformers_5_2_helper(pkg_name, return_version = False):
+ exists = pkg_name in INSTALLED
+ return (exists, INSTALLED.get(pkg_name, "N/A")) if return_version else exists
+
+
+def _trl_0_27(helper):
+ tiu = types.ModuleType("trl.import_utils")
+ tiu._is_package_available = helper
+ exec(
+ "def is_weave_available():\n return _is_package_available('weave')\n"
+ "def is_torch_available():\n return _is_package_available('torch')\n"
+ "def vllm_version():\n return _is_package_available('torch', return_version = True)\n",
+ tiu.__dict__,
+ )
+ return tiu
+
+
+def test_function_style_checks_return_bools():
+ tiu = _trl_0_27(_transformers_5_3_helper)
+ assert tiu.is_weave_available() == (False, None)
+ is_weave_available = (
+ tiu.is_weave_available
+ ) # bound before the fix, like `from trl.import_utils import ...`
+ _coerce_trl_available_checks(tiu)
+ assert tiu.is_weave_available() is False
+ assert is_weave_available() is False
+ assert tiu.is_torch_available() is True
+ assert tiu.vllm_version() == (True, "2.9.0")
+
+
+def test_idempotent():
+ tiu = _trl_0_27(_transformers_5_3_helper)
+ _coerce_trl_available_checks(tiu)
+ wrapped = tiu._is_package_available
+ _coerce_trl_available_checks(tiu)
+ assert tiu._is_package_available is wrapped
+ assert tiu._is_package_available.__wrapped__ is _transformers_5_3_helper
+
+
[email protected]("helper", [_transformers_5_2_helper, None])
+def test_bool_helpers_are_left_alone(helper):
+ # transformers <= 5.2 and TRL >= 0.29 (its own helper) already return bools
+ tiu = _trl_0_27(helper or (lambda pkg_name, return_version = False: pkg_name in INSTALLED))
+ before = tiu._is_package_available
+ _coerce_trl_available_checks(tiu)
+ assert tiu._is_package_available is before
+ assert tiu.is_weave_available() is False
+
+
+def test_cached_flags_still_coerced():
+ tiu = types.ModuleType("trl.import_utils")
+ tiu._weave_available = (False, None)
+ tiu._torch_available = (True, None)
+ _coerce_trl_available_checks(tiu)
+ assert tiu._weave_available is False and tiu._torch_available is True
diff --git a/unsloth/import_fixes.py b/unsloth/import_fixes.py
index 9a54306ff6d..c8cc6fed172 100644
--- a/unsloth/import_fixes.py
+++ b/unsloth/import_fixes.py
@@ -4326,19 +4326,47 @@ def fix_vllm_guided_decoding_params():
def fix_trl_vllm_ascend():
- # TRL caches transformers' (bool, version) tuple, which is always truthy; coerce to bool.
if importlib.util.find_spec("trl") is None:
return
try:
import trl.import_utils as tiu
except Exception:
return
+ _coerce_trl_available_checks(tiu)
+
+
+def _coerce_trl_available_checks(tiu):
+ # TRL caches transformers' (bool, version) tuple, which is always truthy; coerce to bool.
for attr in list(vars(tiu)):
if not (attr.startswith("_") and attr.endswith("_available")):
continue
cached = getattr(tiu, attr)
if isinstance(cached, tuple):
setattr(tiu, attr, bool(cached and cached[0]))
+ # TRL 0.27-0.28 call this helper per check; transformers >= 5.3 returns (exists, None) there.
+ helper = getattr(tiu, "_is_package_available", None)
+ if helper is None or getattr(helper, "_unsloth_bool", False):
+ return
+ try:
+ if not isinstance(helper("_unsloth_absent_package_probe"), tuple):
+ return
+ except Exception:
+ return
+
+ @functools.wraps(helper)
+ def _is_package_available(
+ pkg_name,
+ return_version = False,
+ *args,
+ **kwargs,
+ ):
+ result = helper(pkg_name, return_version, *args, **kwargs)
+ if not return_version and isinstance(result, tuple):
+ return bool(result and result[0])
+ return result
+
+ _is_package_available._unsloth_bool = True
+ tiu._is_package_available = _is_package_available
def ignore_logger_messages():