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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Mask GRPO completions cut by the vLLM server's context limit (#16650)
Daniel Han6f87cf8f3507
diff --git a/tests/test_grpo_stop_string_truncation.py b/tests/test_grpo_stop_string_truncation.pyindex bc6256aa81d..63068ffbe0e 100644--- a/tests/test_grpo_stop_string_truncation.py+++ b/tests/test_grpo_stop_string_truncation.py@@ -110,7 +110,7 @@ def test_completion_cut_by_vllm_context_is_truncated(name, via_vllm_generation):prompts = [[7] * 480, [7] * 480, [7] * 20, [7] * 480, [7] * 20]trainer = _trainer(128, max_model_len = 512, via_vllm_generation = via_vllm_generation)assert _is_truncated(name, completions, trainer, prompts) == [True, False, False, False, True]- # Without a local engine (HF generate, vLLM server mode) only the completion budget is known.+ # HF generate has no context cap: only the completion budget is known.assert _is_truncated(name, completions, _trainer(128), prompts) == [False,False,@@ -118,3 +118,71 @@ def test_completion_cut_by_vllm_context_is_truncated(name, via_vllm_generation):False,True,]+++def _server_trainer(+ max_completion_length,+ vllm_max_model_length = None,+ vlm = False,+):+ """TRL vLLM server mode: use_vllm with no local engine (vllm_generation.llm is None)."""+ text = types.SimpleNamespace(max_position_embeddings = 512)+ config = types.SimpleNamespace(text_config = text) if vlm else text+ return types.SimpleNamespace(+ max_completion_length = max_completion_length,+ use_vllm = True,+ vllm_mode = "server",+ llm = None,+ vllm_generation = types.SimpleNamespace(llm = None, vllm_client = object()),+ args = types.SimpleNamespace(vllm_max_model_length = vllm_max_model_length),+ model = types.SimpleNamespace(config = config),+ )+++ "server",+ [+ {"vllm_max_model_length": 512},+ {"vllm_max_model_length": None},+ {"vllm_max_model_length": -1},+ {"vllm_max_model_length": None, "vlm": True},+ ],+ ids = ["vllm_max_model_length", "max_position_embeddings", "vllm_auto_length", "vlm_text_config"],+)[email protected]("name", NAMES)+def test_completion_cut_by_server_context_is_truncated(name, server):+ completions = [+ [5] * 32, # 480 + 32 = 512+ [5] * 10,+ [5] * 31 + [EOS], # EOS at the limit is a normal stop+ [5] * 128,+ ]+ prompts = [[7] * 480, [7] * 480, [7] * 480, [7] * 20]+ trainer = _server_trainer(128, **server)+ assert _is_truncated(name, completions, trainer, prompts) == [True, False, False, True]++[email protected]("name", NAMES)[email protected]("status", [200, 404])+def test_server_mode_reads_the_served_max_model_len_once(name, status, monkeypatch):+ import requests++ calls = []++ def fake_get(url, timeout = None):+ calls.append(url)+ return types.SimpleNamespace(+ status_code = status, json = lambda: {"data": [{"max_model_len": 500}]}+ )++ monkeypatch.setattr(requests, "get", fake_get)+ trainer = _server_trainer(128) # max_position_embeddings = 512+ trainer.args.vllm_server_base_url = None+ trainer.args.vllm_server_host, trainer.args.vllm_server_port = "10.0.0.1", 8000+ completions = [[5] * 20, [5] * 20, [5] * 19 + [EOS]]+ prompts = [[7] * 480, [7] * 470, [7] * 480]+ # 480 + 20 reaches the served 500; the 404 fallback (512) does not. EOS is never truncated.+ expected = [status == 200, False, False]+ assert _is_truncated(name, completions, trainer, prompts) == expected+ assert _is_truncated(name, completions, trainer, prompts) == expected+ assert calls == ["http://10.0.0.1:8000/v1/models"]diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.pyindex 8f9bca342e5..babf7789b8e 100644--- a/unsloth/models/rl_replacements.py+++ b/unsloth/models/rl_replacements.py@@ -2407,7 +2407,38 @@ def _unsloth_grpo_is_truncated(trainer, completion_ids, prompt_ids, eos_and_pad)if model_config is None:model_config = getattr(getattr(engine, "vllm_config", None), "model_config", None)max_model_len = getattr(model_config, "max_model_len", None)- if not isinstance(max_model_len, int):+ if model_config is None:+ # Server mode: ask the server once per rank (only rank 0 holds a client, so use the args URL).+ # vLLM's /v1/models has it (TRL 1.16+ runs `vllm serve`); older TRL servers lack it, so fall+ # back to vllm_max_model_length, then max_position_embeddings (what TRL's tool loop uses).+ args = getattr(trainer, "args", None)+ max_model_len = getattr(trainer, "_unsloth_server_max_model_len", False)+ if max_model_len is False:+ max_model_len = None+ try:+ import requests++ url = getattr(args, "vllm_server_base_url", None) or (+ f"http://{args.vllm_server_host}:{args.vllm_server_port}"+ )+ response = requests.get(f"{url.rstrip('/')}/v1/models", timeout = 10)+ if response.status_code == 200:+ max_model_len = response.json()["data"][0]["max_model_len"]+ except Exception:+ pass+ trainer._unsloth_server_max_model_len = max_model_len+ if not isinstance(max_model_len, int) or max_model_len <= 0:+ max_model_len = getattr(args, "vllm_max_model_length", None)+ # vLLM reads -1 as "auto"; the resolved server value is unknown here.+ if not isinstance(max_model_len, int) or max_model_len <= 0:+ config = getattr(getattr(trainer, "model", None), "config", None)+ get_text_config = getattr(config, "get_text_config", None)+ if callable(get_text_config):+ config = get_text_config()+ elif getattr(config, "text_config", None) is not None:+ config = config.text_config+ max_model_len = getattr(config, "max_position_embeddings", None)+ if not isinstance(max_model_len, int) or isinstance(max_model_len, bool):max_model_len = Noneif (max_model_len is not None