Repository navigation
Closed
Labels
feature requestFeature request pending on roadmap
Description
Grad norm at step 1 will be nan. Other base models (i.e. Qwen3-14B-Base) and even instruct models like Qwen3-30B-A3B-VL have normal loss and grad norms.
See above notebook for reproduction. I have very little insight into why this is happening, but I do notice that changing from 16384 (which starts off at high grads and loss but slowly goes to normal) to 17000 at a batch size of 4 is enough to go nan.
In this notebook, I patched the model's config rather than unsloth for reproduction, but this also occurs if you do modify unsloth via:
diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py
index e4f34c90..ee9f04bd 100755
--- a/unsloth/models/rl.py
+++ b/unsloth/models/rl.py
@@ -1442,6 +1442,17 @@ def _patch_trl_rl_trainers(trainer_file = "grpo_trainer"):
_prep_pattern, _prep_replacement, RLTrainer_source, count = 1
)
+ # Remove TRL's VLM packing and padding_free ValueError checks so that
+ # packing is allowed for vision-language models.
+ RLTrainer_source = RLTrainer_source.replace(
+ "if self._is_vlm and args.packing:",
+ "if False: # Unsloth: allow packing for VLMs",
+ )
+ RLTrainer_source = RLTrainer_source.replace(
+ "if self._is_vlm and args.padding_free:",
+ "if False: # Unsloth: allow padding_free for VLMs",
+ )
+
# Silence TRL's noisy batch_size=1 + padding-free warning (handles both
# the original "anihilate" typo and the corrected "annihilate" spelling)
for _typo in ("anihilate", "annihilate"):
diff --git a/unsloth/trainer.py b/unsloth/trainer.py
index 65abe680..1a4f6c6f 100644
--- a/unsloth/trainer.py
+++ b/unsloth/trainer.py
@@ -336,7 +336,6 @@ def _patch_sft_trainer_auto_packing(trl_module):
# Check if model type is unsupported for padding_free
model = kwargs.get("model")
is_unsupported_model = False
- is_vlm = False
if model is not None:
model_config = getattr(model, "config", None)
if model_config is not None:
@@ -346,15 +345,6 @@ def _patch_sft_trainer_auto_packing(trl_module):
x in PADDING_FREE_BLOCKLIST for x in model_types
)
- # Check if VLM
- architectures = getattr(model_config, "architectures", None)
- if architectures is None:
- architectures = []
- is_vlm = any(
- x.endswith("ForConditionalGeneration") for x in architectures
- )
- is_vlm = is_vlm or hasattr(model_config, "vision_config")
-
processing_class = kwargs.get("processing_class") or kwargs.get("tokenizer")
data_collator = kwargs.get("data_collator")
@@ -362,7 +352,6 @@ def _patch_sft_trainer_auto_packing(trl_module):
blocked = (
(data_collator is not None)
or isinstance(processing_class, ProcessorMixin)
- or is_vlm
or is_unsupported_model
or (
os.environ.get("UNSLOTH_RETURN_LOGITS", "0") == "1"
@@ -379,8 +368,6 @@ def _patch_sft_trainer_auto_packing(trl_module):
reason = "custom data collator"
if data_collator is None and isinstance(processing_class, ProcessorMixin):
reason = "processor-based model"
- elif is_vlm:
- reason = "vision-language model"
elif is_unsupported_model:
reason = f"unsupported model type(s): {', '.join(model_types)}"
message = "Unsloth: Sample packing skipped " f"({reason} detected)."Note that I’ve also updated trl to 0.27.1 in the notebook, but I don’t think that would affect things?
Metadata
Metadata
Assignees
Labels
feature requestFeature request pending on roadmap
Activity
kirawi commentedon Mar 5, 2026
\cc @mmathew23
adityaghai07 commentedon Mar 5, 2026
@kirawi will try to look into this, and get back to you!
kirawi commentedon Mar 6, 2026
huggingface/transformers#44157 might be related?
Edit: NVM, that's a separate thing. But I also found huggingface/transformers#44322 (comment)
kirawi commentedon Mar 7, 2026
Ref: axolotl-ai-cloud/axolotl#3442 which patches transformers to support packing
Datta0 commentedon Mar 9, 2026
Hey @kirawi so what happens is for VLM and especially in case of Linear attention model like Qwen3.5, if you're using flash (linear) attention, the sequence packing will cause issues if your position_ids is not translated to cu_seqlens properly. This is perhaps what you're seeing as well.
If you check the gradients (max) at 16k+1=16385 too (I noticed), the values are high (almost as high as 16 bit allows) and then 17k is probably where the things add up so much that they go to Inf.
So if you want sample packing on VLM, esp hybrid model like qwen3.5, I'd suggest fixing the cu_seqlens thingy for FLA :)
Datta0 commentedon Mar 9, 2026
Hey @kirawi do you have flash attention installed btw?
kirawi commentedon Mar 9, 2026
Gotcha. I did have flash-attention, FLA, and causal-conv1d all installed when I tried… I think. I’ll check again if it’s supposed to work OOTB, or else try to use a patched version of Transformers per the axolotl PR I linked earlier.
kirawi commentedon Mar 9, 2026
I wasn't able to get it working, so I'll just wait for it to be implemented upstream in
transformers.mmathew23 commentedon Apr 13, 2026
@kirawi at the moment packing is not supported for GatedDeltaNet. Also not supported on the HF side too it seems.
The nan's i believe are due to sensitivity on in_proj_a, and b loras. If you remove those, you stop getting nan's but the losses would be incorrect since gated delta net isn't prevented from seeing the whole sequence.
danielhanchen commentedon Sep 6, 2026
Fixed! Hybrid linear attention models (Qwen3.5, GatedDeltaNet) force packing off with a warning now, since packing isn't supported for them.
Please update Unsloth:
uv pip install --upgrade unsloth unsloth_zoo --force-reinstall --no-cache-dir(if it's not in the current release yet it'll be in the next one). Reopen if it still happens!