From 7ebde8ee678b7c7b6bcc7a2830cec2d1c4ef8163 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Thu, 17 Jul 2025 07:02:38 -0700 Subject: [PATCH] Bug fixes (#2982) * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Synthetic data * Update mapper.py * Xet and Synthetic * Update synthetic.py * Update loader.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update pyproject.toml * Delete .gitignore * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update _utils.py * Update pyproject.toml * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update synthetic.py * Update chat_templates.py * Seasame force float16 / float32 * Fix Seasame * Update loader.py * Update vision.py * Update vision.py * Update vision.py * Update loader.py * is_multimodal * Update loader.py * Update loader.py * Update loader.py * Update loader.py * Update vision.py * Update vision.py * Update vision.py * UNSLOTH_DISABLE_STATIC_GENERATION * Update vision.py * Auto vision detection * Sesame * Whisper * Update loader.py * Update loader.py * Update loader.py * Update mapper.py * Update vision.py * Update vision.py * Update vision.py * Update vision.py * Update vision.py * Update vision.py * Update loader.py * Update loader.py * Update loader.py * Update loader.py * Update _utils.py * Update rl.py * versioning * Update rl.py * Update rl.py * Update rl.py * Update rl.py * Update rl.py * logging * Update pyproject.toml * Update rl.py * versioning * Update rl.py * Update rl.py * Update rl_replacements.py * Update rl_replacements.py * Update rl.py * Update rl_replacements.py * Update rl_replacements.py * logits / temperature * Update rl_replacements.py * Update pyproject.toml * Update rl_replacements.py * Update rl_replacements.py * Debugging only * Update llama.py * Update llama.py * Update rl_replacements.py * Update rl_replacements.py * Update rl_replacements.py * Update rl_replacements.py * Update rl_replacements.py * Generic efficient GRPO * Update rl_replacements.py * Update rl_replacements.py * Remove debugging * Update rl_replacements.py * Update rl_replacements.py * Update vision.py * Update llama.py * Update rl_replacements.py * versioning * Update _utils.py * Update vision.py * Update mapper.py * Update loader.py * Update mapper.py * Update vision.py * Update loader.py * Update vision.py * Update loader.py * Update _utils.py * Update vision.py * gradient checkpointing * Gemma 3N fixes * Update loader.py * Versioning * Gemma 3N fixes * Update vision.py * Update vision.py * Update loader.py * Update vision.py * Fix setup.py * setup.py * Prints * Update setup.py * Update setup.py * Update setup.py * Update pyproject.toml * Update pyproject.toml * Update pyproject.toml * Update pyproject.toml * Update pyproject.toml * Update pyproject.toml * Update vision.py * Update vision.py * Update pyproject.toml * Update vision.py * Update _utils.py * Update __init__.py * Update __init__.py * Small fixes * Update vision.py * Update vision.py * versioning * Update __init__.py * Update llama.py * Update rl.py * Update rl.py * Update _utils.py * Update vision.py * Update vision.py * compiler stance * Update _utils.py * Update pyproject.toml * Update pyproject.toml * Update rl_replacements.py * Update rl_replacements.py * Update rl_replacements.py * Update rl_replacements.py * Update rl.py * Update rl_replacements.py * Update rl_replacements.py * Update rl_replacements.py * Update rl_replacements.py * Update rl_replacements.py * Update rl_replacements.py * Update rl_replacements.py --- pyproject.toml | 8 ++--- unsloth/models/_utils.py | 10 +++++- unsloth/models/llama.py | 4 +-- unsloth/models/rl.py | 9 +++--- unsloth/models/rl_replacements.py | 53 +++++++++++++++++++++++++++++++ unsloth/models/vision.py | 4 +++ 6 files changed, 77 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ae46d7aaf5..d17859cfa8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,7 +37,7 @@ triton = [ ] huggingface = [ - "unsloth_zoo>=2025.7.4", + "unsloth_zoo>=2025.7.5", "packaging", "tyro", "transformers>=4.51.3,!=4.47.0,!=4.52.0,!=4.52.1,!=4.52.2,!=4.52.3,!=4.53.0", @@ -48,7 +48,7 @@ huggingface = [ "wheel>=0.42.0", "numpy", "accelerate>=0.34.1", - "trl>=0.7.9,!=0.9.0,!=0.9.1,!=0.9.2,!=0.9.3,!=0.15.0", + "trl>=0.7.9,!=0.9.0,!=0.9.1,!=0.9.2,!=0.9.3,!=0.15.0,!=0.19.0", "peft>=0.7.1,!=0.11.0", "protobuf", "huggingface_hub", @@ -381,7 +381,7 @@ colab-ampere-torch220 = [ "flash-attn>=2.6.3", ] colab-new = [ - "unsloth_zoo>=2025.7.4", + "unsloth_zoo>=2025.7.5", "packaging", "tyro", "transformers>=4.51.3,!=4.47.0,!=4.52.0,!=4.52.1,!=4.52.2,!=4.52.3,!=4.53.0", @@ -399,7 +399,7 @@ colab-new = [ ] colab-no-deps = [ "accelerate>=0.34.1", - "trl>=0.7.9,!=0.9.0,!=0.9.1,!=0.9.2,!=0.9.3,!=0.15.0", + "trl>=0.7.9,!=0.9.0,!=0.9.1,!=0.9.2,!=0.9.3,!=0.15.0,!=0.19.0", "peft>=0.7.1", "xformers", "bitsandbytes>=0.45.5", diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py index 5b3879ce68..bfd7c8c5a5 100644 --- a/unsloth/models/_utils.py +++ b/unsloth/models/_utils.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "2025.7.3" +__version__ = "2025.7.4" __all__ = [ "SUPPORTS_BFLOAT16", @@ -190,6 +190,14 @@ try: except: pass +# The following generation flags are not valid and may be ignored: +try: + from transformers.generation.configuration_utils import logger as configuration_logger + configuration_logger.addFilter(HideLoggingMessage("following generation flags")) + del configuration_logger +except: + pass + # Gemma3 It is strongly recommended to train Gemma3 models with the `eager` try: from transformers.models.gemma3.modeling_gemma3 import logger as gemma3_logger diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py index 3551c6ca5e..8d985aa9d2 100644 --- a/unsloth/models/llama.py +++ b/unsloth/models/llama.py @@ -2224,7 +2224,7 @@ class FastLlamaModel: bias = "none", layers_to_transform = None, layers_pattern = None, - use_gradient_checkpointing = True, + use_gradient_checkpointing = "unsloth", random_state = 3407, max_seq_length = 2048, # not used anymore use_rslora = False, @@ -2678,7 +2678,7 @@ class FastLlamaModel: @staticmethod def patch_peft_model( model, - use_gradient_checkpointing = True, + use_gradient_checkpointing = "unsloth", ): if os.environ.get("UNSLOTH_USE_NEW_MODEL", "0") == "1": return FastBaseModel.patch_peft_model( diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py index 45b8ca6334..664fe10c4f 100644 --- a/unsloth/models/rl.py +++ b/unsloth/models/rl.py @@ -481,10 +481,11 @@ def _patch_trl_rl_trainers(trainer_file = "grpo_trainer"): "include_tokens_per_second" : False, "include_num_input_tokens_seen" : False, "auto_find_batch_size" : True, # Auto /2 batch size - "dataloader_persistent_workers" : True, # Keeps dataloader in RAM - "dataloader_prefetch_factor" : 2, "dataloader_pin_memory" : True, - "dataloader_num_workers" : 1, + # Might fail so disable for now + # "dataloader_persistent_workers" : True, # Keeps dataloader in RAM + # "dataloader_prefetch_factor" : 2, + # "dataloader_num_workers" : 2, # Default is 0 means 1 } for k, v in replacements.items(): x = f"{k}( = [^,\n]{{1,}})?,\n" @@ -671,7 +672,7 @@ def _patch_trl_rl_trainers(trainer_file = "grpo_trainer"): RLTrainer_source, f"trl.trainer.{trainer_file}", imports, - overwrite = False, + overwrite = True, ) # Patch Trainer diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py index 4d5b4d7a4d..a88385bf03 100644 --- a/unsloth/models/rl_replacements.py +++ b/unsloth/models/rl_replacements.py @@ -235,6 +235,59 @@ pass RL_FUNCTIONS["grpo_trainer"].append(grpo_trainer__prepare_inputs) +# Fix incorrect special tokens handling and truncation in older TRL versions +def grpo_trainer__generate_and_score_completions(function_name, function): + if function_name != "_generate_and_score_completions": return function + + # TRL 0.19.0 did skip_special_tokens = True which should be False + function = function.replace( + "prompt_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False", + "prompt_ids, skip_special_tokens=False, clean_up_tokenization_spaces=False", + ) + + # Always between max_prompt_length and use_vllm + found = re.findall( + r"\n(([ ]{8,})if self\.max_prompt_length is not None:.*?"\ + r"\2if self\.use_vllm:)", + function, + flags = re.DOTALL | re.MULTILINE, + ) + if len(found) != 0: + replace_part, spacing = found[0] + removed_comments = re.sub(r"\#[^\n]{1,}", "", replace_part) + splits = removed_comments.split("\n") + if sum(re.match(rf"{spacing}[^\s]", x) is not None for x in splits) == 2 and len(spacing) >= 8: + + new_replacement = \ + f"""\n{spacing}if self.max_prompt_length is not None: + # If max_prompt_length is set, we trim the prompt to keep only the last `max_prompt_length` tokens. + # Then we decode those tokens back into text. We manually remove leading pad tokens from the decoded text, + # because we can't use `skip_special_tokens=True` (some special tokens are still needed for generation). + prompt_ids = prompt_ids[:, -self.max_prompt_length :] + prompt_mask = prompt_mask[:, -self.max_prompt_length :] + prompts_text = self.processing_class.batch_decode( + prompt_ids, skip_special_tokens=False, clean_up_tokenization_spaces=False + ) + pad_token = self.processing_class.pad_token + def strip_leading_tokens(text): + while text.startswith(pad_token): + text = text.removeprefix(pad_token) + return text + + if pad_token is not None: + prompts_text = [ + strip_leading_tokens(text) for text in prompts_text + ] + + # Generate completions using either vLLM or regular generation + if self.use_vllm:""" + function = function.replace(replace_part, new_replacement) + pass + return function +pass +RL_FUNCTIONS["grpo_trainer"].append(grpo_trainer__generate_and_score_completions) + + # Remove _move_model_to_vllm def grpo_trainer__move_model_to_vllm(function_name, function): if function_name != "_move_model_to_vllm": return function diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py index 7442f07e73..5bbf4c76a6 100644 --- a/unsloth/models/vision.py +++ b/unsloth/models/vision.py @@ -755,6 +755,8 @@ class FastBaseModel: os.environ["UNSLOTH_RETURN_HIDDEN_STATES"] = "0" # Must enable returning logits os.environ["UNSLOTH_RETURN_LOGITS"] = "1" + # Turn off skip guards and set stance to default + torch.compiler.set_stance(stance = "default", skip_guard_eval_unsafe = False) return model pass @@ -801,6 +803,8 @@ class FastBaseModel: pass # Can re-enable not returning logits os.environ["UNSLOTH_RETURN_LOGITS"] = "0" + # Turn off skip guards and set stance to default + torch.compiler.set_stance(stance = "default", skip_guard_eval_unsafe = False) return model pass pass