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: 3 patch_* helpers — fast_lora import, sft_trainer Union, openenv OSError (#5319)

Daniel Han948ce43584c2
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index 34fec53504b..d3eee033256 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -2483,6 +2483,7 @@ def patch_tokenizer(model, tokenizer):
def patch_fast_lora():
import peft.tuners.lora.bnb
+ from ..kernels.fast_lora import fast_lora_forward
peft.tuners.lora.bnb.Linear4bit.forward = fast_lora_forward
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py
index 0f10847282b..c2be1bf74a6 100755
--- a/unsloth/models/rl_replacements.py
+++ b/unsloth/models/rl_replacements.py
@@ -1780,7 +1780,20 @@ def openenv_vllm_reload_weights():
patch_target_name = "generate_rollout_completions"
patch_target = getattr(openenv_utils, patch_target_name)
- src = inspect.getsource(patch_target)
+ # TRL 0.29.1+ ships some openenv helpers as compiled bytecode without
+ # accessible source on disk; inspect.getsource raises OSError("could
+ # not get source code") in that case. Skip the source-rewrite patch
+ # rather than crashing -- the core unsloth weight-reload path stays
+ # functional, only the wake_up tag rewrite is skipped.
+ try:
+ src = inspect.getsource(patch_target)
+ except OSError as e:
+ logger.warning(
+ f"Unsloth: Could not retrieve source for trl openenv "
+ f"{patch_target_name} ({e}); skipping rewrite. "
+ f"Weight reload still functional."
+ )
+ return
src = textwrap.dedent(src)
original_src = src
diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.py
index 130894e3855..67edc41d52e 100644
--- a/unsloth/tokenizer_utils.py
+++ b/unsloth/tokenizer_utils.py
@@ -1580,6 +1580,12 @@ def patch_sft_trainer_tokenizer():
except:
return
all_imports = dir(trl.trainer.sft_trainer)
+ # Make typing names available to the exec'd source bodies. TRL >= 1.x
+ # type-hints _prepare_dataset / _prepare_non_packed_dataloader with
+ # `Union[...]` and friends; without these imports in the exec namespace
+ # those become NameErrors at exec time. Mirrors the pattern used in
+ # unsloth/models/_utils.py:patch_linear_scaling.
+ from typing import Union, Optional, List, Any, Callable, Tuple, Dict, Iterator # noqa: F401
for (
function_name,