From a835b266ef6b59ce58a2f833826d250622d55f62 Mon Sep 17 00:00:00 2001 From: DoubleMathew Date: Sun, 1 Mar 2026 21:30:47 -0600 Subject: [PATCH] Fix auto padding free logic to respect user passed False (#4128) * Fix auto padding free logic to respect user passed * Update unsloth/trainer.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- unsloth/models/rl.py | 1 + unsloth/trainer.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py index eb5d4be171..e4f34c908e 100755 --- a/unsloth/models/rl.py +++ b/unsloth/models/rl.py @@ -1037,6 +1037,7 @@ def _patch_trl_rl_trainers(trainer_file = "grpo_trainer"): "include_num_input_tokens_seen": False, "auto_find_batch_size": False, # Auto /2 batch size - too many people complained so removing "dataloader_pin_memory": True, + "padding_free": None, # None = user didn't set it, allows auto-enable detection # Might fail so disable for now # "dataloader_persistent_workers" : True, # Keeps dataloader in RAM # "dataloader_prefetch_factor" : 2, diff --git a/unsloth/trainer.py b/unsloth/trainer.py index cb36b8639d..65abe6801f 100644 --- a/unsloth/trainer.py +++ b/unsloth/trainer.py @@ -73,7 +73,7 @@ def _should_auto_padding_free(config) -> bool: or getattr(config, "packing", False) ): return False - return not getattr(config, "padding_free", False) + return getattr(config, "padding_free", None) is None def _disable_sample_packing(config): @@ -392,6 +392,7 @@ def _patch_sft_trainer_auto_packing(trl_module): packing_active = True logger.info("Unsloth: Sample packing enabled for SFTTrainer instance.") + # Resolve padding_free: None (default) = auto-enable unless env-disabled or packing auto_padding_free_active = False padding_free_requested = getattr(config_arg, "padding_free", None) is True if not blocked: