Skip to content

Fixup multi GPU workload. - #3049

Merged
danielhanchen merged 8 commits into
unslothai:mainfrom
Datta0:sync_rope
Jul 28, 2025
Merged

danielhanchen merged 8 commits into
unslothai:mainfrom
Datta0:sync_rope

Conversation

@Datta0

@Datta0 Datta0 commented Jul 27, 2025 •

Copy link
Copy Markdown
Contributor

Multi GPU was failing due to few reasons

Previously we initialised per gpu position embeddings. That fixed the operands being on right devices. Now there's a small issue that after few layers, the activations/hidden_states end up becoming NaNs.

A workaround I stumbled upon was adding print (for debugging purposes lol) after fast_rope_embedding.
Inspired by that as discussed with @danielhanchen , I added sync. Initial thought was to sync all devices but syncing only current device felt like the right choice as we use PP. (Might need to revisit for other cases later)

Verified inference of mistralai/Mistral-7B-Instruct-v0.3, unsloth/Llama-3.1-8B-Instruct, google/gemma-2-9b-it and Qwen/Qwen3-8B. There are no NaNs in generate.

On Mi(s/x)tral, when using xformers, no causal mask was being applied. It was doing full attention. To avoid that we need to create and pass mask irrespective of whether attention_mask is mentioned. (Note that when you do

inputs = tokenizer(text, return_tensors='pt')
model(**inputs)

the inputs would have attention_mask key/field causing causal_mask to not be created previously. So this also addresses that.

Magistral as example starts at a loss of 1.x as it should :)
image

@danielhanchen
danielhanchen merged commit 00af713 into unslothai:main Jul 28, 2025
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
* sync all instead

* sync after move and rope init instead

* sync after rope inside

* Return new tensors and no sync

* Sync only current stream

* Fixup mask for xformers

* sync for prefill only

* clean up
danielhanchen referenced this pull request in danielhanchen/unsloth-staging-2 Oct 14, 2026
…vices (unslothai#13609)

* Skip the per-layer RoPE stream sync unless the model spans several devices

The stream drain after RoPE (#3049, #3061) fired on every host with more than
one visible GPU, so single-GPU training or one DDP rank on a multi-GPU machine
paid one host-blocking synchronize per attention layer per forward and per
gradient checkpoint recompute. inplace_rope_embedding synced even with one GPU.

Keep the drain only once RoPE has run on a second device in the process
(device_map / pipeline split), which is the case it was added for.

* Register split models at load so RoPE drains from the first forward, and only while one is alive

* Register splits made by a hand call to accelerate.dispatch_model too

* Drain every RoPE call once a split nobody registered shows up

* Resolve index-less device_map entries to the current device, and never let split registration raise

* Trim comments in RoPE split-model sync

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Guard the real-kernel RoPE test with has_real_cuda so the CUDA spoof cannot un-skip it

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Lyxot pushed a commit to NilayYadav/unsloth that referenced this pull request Oct 22, 2026
…lothai#14611)

unslothai#14599 (fused decode RoPE) calls `_sync_if_split(Q)` after its Triton
launch in `decode_rope_inplace` and `decode_qk_norm_rope`. On a model
split across GPUs (device_map), that blocks the host once per layer per
decoded token.

The drain is not needed there. Both helpers launch inside
`torch_gpu_device(Q.device)`, so the kernel runs on Q's device and its
current stream. The next layer's `.to(other_device)` is already ordered
after it: PyTorch's device-to-device copy synchronizes with the current
streams of both devices (`aten/src/ATen/native/cuda/Copy.cu`). The older
eager RoPE entry points keep their drains. Those were added in unslothai#3049 /
unslothai#3061 for launches made while another GPU was current, and this PR does
not touch them.

Measured on a random-weight 8-layer Llama (hidden 2048, fp16) split 4 +
4 over two B200s: batch 1, 32 new tokens, 4 interleaved repeats, median
generate time.

| decode RoPE path | median s | tokens/s |
|---|---|---|
| before unslothai#14599 | 0.442 | 72.4 |
| main (fused + drain) | 0.723 | 44.3 |
| fused, drain removed (this PR) | 0.444 | 72.0 |

All three arms produced the same tokens. With random weights that is
only a weak correctness check (the output is mostly one repeated token),
so this PR also adds a direct ordering test.

## Change
- Remove `_sync_if_split(Q)` from `decode_rope_inplace` and
`decode_qk_norm_rope`.
- Single-GPU behaviour is unchanged: the drain was a no-op there.

## Validation
- `test_decode_split_sync_and_mixed_devices` now asserts that the decode
helpers make no drain call.
- New `test_decode_rope_on_another_device_orders_with_the_copy`, which
needs two GPUs and skips on one:
  - rotates Q / K on GPU 1 while GPU 0 is current;
  - copies straight to GPU 0 with no host sync;
  - compares the result with the unfused rotation.
- `test_decode_fused_rope`, `test_qk_norm_rope`,
`test_rope_kernel_layouts`, `test_rope_sync_single_device`,
`test_qwen3_decode_offset_rope`, GPU-marked tests included: 414 passed,
1 skipped (the two-GPU test) on torch 2.14.1 and on torch 2.10.0.

Not covered: the two-GPU test was not run here (single-GPU leases). The
timing table comes from a two-GPU run of the same change outside this
PR's CI. ROCm split models were not tested.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants