Skip to content

fix(trainer): disable padding-free for multi-GPU Gemma 4 loads - #12095

Closed
Souravrajvi0 wants to merge 7 commits into
unslothai:mainfrom
Souravrajvi0:feat/gemma4-padding-free-multigpu-4c78
Closed

Souravrajvi0 wants to merge 7 commits into
unslothai:mainfrom
Souravrajvi0:feat/gemma4-padding-free-multigpu-4c78

Conversation

@Souravrajvi0

Copy link
Copy Markdown
Contributor

Problem

Fixes #11952.

Gemma 4 text-only SFT in Unsloth Studio can crash on the first training step with:

RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cuda:0)

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

  1. Trainer gate — When the loaded model is Gemma 4 (gemma4 / gemma4_text) and hf_device_map spans more than one device, disable sample packing and padding-free (same safety pattern as other blocked setups).
  2. Batch device alignment — Before compute_loss, move tensor batch fields (e.g. packed_seq_lengths, position_ids) onto input_ids.device so collator metadata left on CPU cannot desync from GPU activations.

Testing

  • Added tests/test_gemma4_padding_free_multidevice.py (device-map detection, gate, align helper).
  • Full CUDA + 31B smoke not run on CI agent (no GPU / weights); reporter or maintainer GPU confirm appreciated.

User workaround (until release)

  • Set padding_free=False, or
  • UNSLOTH_DISABLE_AUTO_PADDING_FREE=1, or
  • Load Gemma 4 31B on a single GPU when VRAM allows.

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.
@danielhanchen

Copy link
Copy Markdown
Member

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.
@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

@danielhanchen I've Fixed the Mismatch , studio recipe changes are out of this branch; trainer + _utils + test only. Recipe follow-up is #12091

@danielhanchen

Copy link
Copy Markdown
Member

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:

  • Disabling padding-free does not change where that token type tensor lives inside the model, so the mismatch remains (Let Gemma 4 31B train when it is split across GPUs #12233 reports the same error still happens with this branch).
  • _unsloth_align_batch_tensor_devices moves batch tensors onto input_ids.device, which is already the first GPU, and mm_token_type_ids is built with zeros_like(input_ids) on that device, so it is a no-op for this crash.
  • Merging it now would also turn off padding-free for every multi-GPU Gemma 4 run, a throughput cost for a crash that is already fixed.

Thanks again for looking into this one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

2 participants