Repository navigation
fix(trainer): disable padding-free for multi-GPU Gemma 4 loads - #12095
Souravrajvi0 wants to merge 7 commits into
Conversation
Persist engaged speed, attention, step cache, and CPU offload in PNG recipes with schema_version 1, and surface workflow-specific settings, LoRAs, and ControlNet in the Images Recipe popover.
Exercise generate -> gallery list -> PNG download and assert engaged runtime build fields round-trip in the embedded unsloth chunk.
Gemma 4 text SFT with auto padding-free can hit CPU/CUDA index errors when the model uses a multi-entry hf_device_map (reported for 31B in Studio). Turn off packing and padding-free for that case and align batch tensor devices before compute_loss.
|
The Gemma 4 multi-GPU padding-free guard in unsloth/trainer.py looks like the right direction, but this branch also changes Studio image recipe metadata in diffusion.py, image_gallery.py and the images frontend. Could you split those out so this PR covers only the trainer and _utils.py change? |
Keep this PR scoped to unsloth/trainer.py, unsloth/models/_utils.py, and tests only, per review on unslothai#12095.
|
@danielhanchen I've Fixed the Mismatch , studio recipe changes are out of this branch; trainer + _utils + test only. Recipe follow-up is #12091 |
|
Thank you for the PR and for splitting out the Studio recipe changes! Closing this since #11952 was fixed by #12233, which is now merged. The root cause turned out to be inside the Gemma 4 31B forward: the token type tensor used to build the vision attention mask lands on the first GPU, while the text embeddings it is compared against sit on the last GPU. #12233 moves that tensor next to the embeddings only when Gemma 4 is split across devices, and was verified on 2x L4 (main fails at step 0, the fix trains with losses tracking a single GPU run). This PR would not resolve that error:
Thanks again for looking into this one! |
Problem
Fixes #11952.
Gemma 4 text-only SFT in Unsloth Studio can crash on the first training step with:
Reported setup:
Gemma4ForConditionalGeneration, padding-free auto-enabled, model sharded across 2 GPUs (hf_device_map). Smaller Gemma 4 loads on a single device may still train with padding-free.Fix
gemma4/gemma4_text) andhf_device_mapspans more than one device, disable sample packing and padding-free (same safety pattern as other blocked setups).compute_loss, move tensor batch fields (e.g.packed_seq_lengths,position_ids) ontoinput_ids.deviceso collator metadata left on CPU cannot desync from GPU activations.Testing
tests/test_gemma4_padding_free_multidevice.py(device-map detection, gate, align helper).User workaround (until release)
padding_free=False, orUNSLOTH_DISABLE_AUTO_PADDING_FREE=1, or