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.

Name the encoding on the tensor parallel index reads; give the PPO unwrap test _decode_graphs (#15209)

Daniel Han65e2e9f7fc2b
diff --git a/tests/test_padding_choice.py b/tests/test_padding_choice.py
index 4d43b21eb70..0703609a02c 100644
--- a/tests/test_padding_choice.py
+++ b/tests/test_padding_choice.py
@@ -264,7 +264,7 @@ def test_moe_detection():
def test_trainer_wires_the_choice():
- src = (ROOT / "unsloth" / "trainer.py").read_text()
+ src = (ROOT / "unsloth" / "trainer.py").read_text(encoding = "utf-8")
assert "_auto_padding_choice(" in src
assert "from unsloth.utils.padding_choice import decide_auto_padding_free" in src
diff --git a/tests/test_ppo_trainer_patches.py b/tests/test_ppo_trainer_patches.py
index 809ee719fe4..f29dd1b92f0 100644
--- a/tests/test_ppo_trainer_patches.py
+++ b/tests/test_ppo_trainer_patches.py
@@ -7,6 +7,7 @@ from __future__ import annotations
import ast
import copy
import functools
+import importlib.util
import inspect
import sys
from contextlib import contextmanager
@@ -119,6 +120,28 @@ def test_wrapper_found_through_the_trainer_module_and_upstream_toggles_kept():
assert upstream.PolicyAndValueWrapper.gradient_checkpointing_disable is own
+def _decode_graphs(calls):
+ """rl.py's _decode_graphs with its real rl_generation scope, loaded by file path (importing
+ unsloth needs an accelerator); entering the scope and releasing for training are recorded."""
+ spec = importlib.util.spec_from_file_location(
+ "_unsloth_decode_graphs_ppo", RL_PY.with_name("_decode_graphs.py")
+ )
+ real = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(real)
+
+ class rl_generation(real.rl_generation):
+ __slots__ = ()
+
+ def __enter__(self):
+ calls.append("rl_generation")
+ return super().__enter__()
+
+ return SimpleNamespace(
+ rl_generation = rl_generation,
+ release_for_training = lambda model: calls.append("release_for_training"),
+ )
+
+
def _unsloth_unwrap(calls):
fast = SimpleNamespace(
for_inference = lambda model: calls.append("for_inference"),
@@ -133,6 +156,7 @@ def _unsloth_unwrap(calls):
"FastLanguageModel": fast,
"copy": copy,
"inspect": inspect,
+ "_decode_graphs": _decode_graphs(calls),
}
_lift("_generate_accepts_use_model_defaults", namespace)
_lift("_caller_sampling_only", namespace)
@@ -154,7 +178,13 @@ def test_unwrap_generates_through_the_ppo_policy():
out = unwrapped.policy.generate()
assert not out.is_inference()
assert "generate" not in vars(wrapper)
- assert calls == ["for_inference", ("for_training", "unsloth")]
+ # Generation runs inside the RL scope; the decode buffers go before training resumes.
+ assert calls == [
+ "for_inference",
+ "rl_generation",
+ "release_for_training",
+ ("for_training", "unsloth"),
+ ]
def test_unwrap_still_wraps_a_model_with_generate():
@@ -163,6 +193,7 @@ def test_unwrap_still_wraps_a_model_with_generate():
with _unsloth_unwrap(calls)(policy) as unwrapped:
out = unwrapped.generate()
assert not out.is_inference()
+ assert calls.index("rl_generation") < calls.index("release_for_training")
def _has_real_gpu():
diff --git a/tests/test_tp_native_oracle.py b/tests/test_tp_native_oracle.py
index 865a0c453f0..823f81b6135 100644
--- a/tests/test_tp_native_oracle.py
+++ b/tests/test_tp_native_oracle.py
@@ -694,7 +694,7 @@ def test_generated_trl_trainer_init_skips_for_inference_on_a_tp_model():
import re
import textwrap
- source = open(os.path.join(REPO, "unsloth", "models", "rl.py")).read()
+ source = open(os.path.join(REPO, "unsloth", "models", "rl.py"), encoding = "utf-8").read()
match = re.search(
r"super\(\)\.__init__\(\{RLTrainer_call_args\}\{RLTrainer_kwargs\}\)\n(.*?)\n\{RLTrainer_post\}",
source,
diff --git a/unsloth/models/tp_native.py b/unsloth/models/tp_native.py
index fa015ecab60..62fdd8fc79c 100644
--- a/unsloth/models/tp_native.py
+++ b/unsloth/models/tp_native.py
@@ -1633,7 +1633,7 @@ def _checkpoint_index(name, kwargs):
root = os.path.join(name, sub)
index = os.path.join(root, "model.safetensors.index.json")
if os.path.isfile(index):
- with open(index) as f:
+ with open(index, encoding = "utf-8") as f:
weight_map = json.load(f)["weight_map"]
return {k: os.path.join(root, v) for k, v in weight_map.items()}
single = os.path.join(root, "model.safetensors")
@@ -1657,7 +1657,7 @@ def _checkpoint_index(name, kwargs):
index = None
if index is None:
return keys_of(hf_hub_download(name, "model.safetensors", **hub))
- with open(index) as f:
+ with open(index, encoding = "utf-8") as f:
weight_map = json.load(f)["weight_map"]
paths = {v: hf_hub_download(name, v, **hub) for v in sorted(set(weight_map.values()))}
return {k: paths[v] for k, v in weight_map.items()}