Skip to content

Vision training: Mllama LoRA under block CUDA graphs, chunked DPO head for VLM batches - #17850

Merged
danielhanchen merged 3 commits into
mainfrom
w12-vision-dpo-graphs
Nov 15, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
w12-vision-dpo-graphs

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Two vision training fixes.

1. Llama 3.2 Vision LoRA fails on step 2 with block CUDA graphs

Mllama cross-attention layers all read the same cross_attention_states. When the vision tower trains (LoRA on the projector), that tensor takes a gradient. A replayed block backward hands the gradient out in graph pool memory, and the next cross-attention layer's replay of the same graph overwrites it before autograd sums it. Training fails on the second step with "accessing gradient tensor output of CUDAGraphs that has been overwritten" once auto mode turns graphs on. Only hidden_states was guarded.

  • Fix: auto mode now keeps graphs off for such a block and logs the reason. A forced-graph call falls back to the compiled block without graphs.
  • Test: tests/test_block_cudagraphs_side_grad.py.
    • On CPU, auto mode keeps graphs off.
    • On GPU, forced graphs over three cross-attention blocks that share a trained side input train like eager, side gradient included, with both plain and reentrant checkpointing.
    • Main reproduces the exact Mllama error. On this PR: 7 passed, including the existing GPU graph tests.

2. VLM DPO builds full [2B, S, V] logits on TRL < 0.29

_unsloth_dpo_chunked_head refused any batch carrying pixel_values, pixel_attention_mask, image_sizes, token_type_ids or the Gemma 4 image keys. The head only reads the final hidden states, and concatenated_forward already trims a forward that prepends image positions (LLaVA) to the labels' length. The hidden states are trimmed the same way.

  • Engagement on GPU, TRL 0.22: with this PR the chunked head scores 4 of 4 times on LLaVA and Idefics3. On main it falls back with "vision inputs" 4 of 4 times.
  • TRL 1.18 is unaffected: its own fused lm head already runs for VLMs.
  • Test: tests/test_dpo_chunked_vision.py. A tiny LLaVA batch takes the chunked head and matches TRL's logits path on loss, every metric and every gradient (vision tower and projector included). Each vision key set no longer refuses the head. Main: 7 failed. This PR: 7 passed.

….29)

_unsloth_dpo_chunked_head refused any batch carrying pixel_values,
pixel_attention_mask, image_sizes, token_type_ids or the Gemma 4 image
keys, so VLM DPO on TRL < 0.29 materialised the full [2B, S, V] logits.
The towers run inside the forward and the head only reads the final hidden
states; concatenated_forward already trims a forward that prepends image
positions (LLaVA) to the labels' length before the log-prob line the
rewrite replaces, so the hidden states are trimmed the same way.

tests/test_dpo_chunked_vision.py: a tiny LLaVA batch takes the chunked
head and matches TRL's logits path on loss, every metric and every
gradient (vision tower and projector included); each vision key set no
longer refuses the head.
…ut graphs

Mllama cross-attention layers all read the same cross_attention_states,
and with a trained vision tower (LoRA on the projector) that tensor takes
a gradient. A replayed block backward hands it out in graph pool memory
and the next cross-attention layer's replay of the same graph overwrites
it before the engine sums it, so Llama 3.2 Vision LoRA training failed
on its second step ("accessing gradient tensor output of CUDAGraphs that
has been overwritten") once auto mode turned graphs on. Only
hidden_states was guarded (_CloneGrad).

Auto mode now turns graphs off with the reason, and a forced graph call
falls back to the compiled block without graphs.

tests/test_block_cudagraphs_side_grad.py: auto keeps graphs off (CPU); on
a GPU, forced graphs over three cross-attention blocks sharing a trained
side input train like eager, side gradient included (plain and reentrant
checkpointing).
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T15:39:16.571739Z 1e5d11c Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 1e5d11c226

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

[skip ci]
@danielhanchen
danielhanchen merged commit 3c416df into main Nov 15, 2026
@danielhanchen
danielhanchen deleted the w12-vision-dpo-graphs branch November 15, 2026 20:49
danielhanchen added a commit that referenced this pull request Nov 16, 2026
…able torch.cuda.is_available (#17890)

## Summary

`Repo tests (CPU, studio)` has failed on `main` since #17850 (Mllama
LoRA under block CUDA graphs):

```
tests/python/test_accelerator_skip_guards.py::test_no_skip_guard_reads_a_spoofable_accelerator_probe
E   AssertionError: these skip guards read a probe tests/_zoo_aggressive_cuda_spoof.py patches to True process-wide ...
E       test_block_cudagraphs_side_grad.py:79: torch.cuda.is_available()
```

`tests/test_block_cudagraphs_side_grad.py` gates its GPU case on
`torch.cuda.is_available()`. The CPU legs load
`tests/_zoo_aggressive_cuda_spoof.py`, which patches that probe to
`True` for the whole process. So on a CPU-only runner the GPU case
un-skips whenever it shares an xdist worker with the spoof, and the
guard test is right to reject it.

## Change (test only)

The guard now uses `has_real_cuda()` from `tests/real_accelerator.py`,
the recorded real-accelerator answer the rest of the suite uses (for
example `tests/test_bf16_adamw.py`). On a real GPU the case still runs;
on a CPU runner it skips regardless of the spoof.

## Verification

`tests/python/test_accelerator_skip_guards.py
tests/test_block_cudagraphs_side_grad.py`: 26 passed, 2 skipped. On
`main`, `test_no_skip_guard_reads_a_spoofable_accelerator_probe` fails
as above.
Penguinjanator pushed a commit to Penguinjanator/unsloth that referenced this pull request Nov 16, 2026
…on without Triton, kernel and test gates (unslothai#17882)

Backend CI on main has been red since today's merges. Every failure
traces to one commit; this fixes the unsloth side (the gloo-cp shard
also needs unslothai/unsloth-zoo#2542, since CI installs zoo from main).

| CI shard / test | Cause | Fix |
|---|---|---|
| gloo: `test_qwen4exp_parallel_matches_one_process[pp*]`,
`test_pp_splits_streams_and_runs_the_mixer_last`, all of
`test_frozen_table_pp_gloo.py` | unslothai#17785: transformers 5.17 (the CI pin)
names the Qwen4Exp layer type `qwen_sparse_attention`, 5.22 renamed it
`indexed_attention`; pipeline parallel only accepted the new name, so
every rank raised "hybrid layer stacks" | accept both names in
`_PP_HYBRID_FAMILIES` |
| diffusion (Python 3.13): 13 tests in
`test_diffusion_int8_attention_triton_device.py` | unslothai#17855: `_launch`
read `triton.runtime` while `triton` is `None` on that shard | check
`triton is not None` first |
| rest: `test_every_constexpr_line_has_a_comment` | unslothai#17860: 23
uncommented `tl.constexpr` parameters in `large_head_attention.py` |
comments added; `_tma_tile`'s signature split one parameter per line |
| rest: `test_every_launch_requests_fp_fusion` | unslothai#17860: the `_launch`
helper forwards `enable_fp_fusion` from every caller, but the gate did
not know it is a helper | registered like `layernorm.py`'s |
| rest: `test_no_file_is_dropped_or_run_twice` | unslothai#17868:
`tests/test_ddp_push_rank0_gloo.py` matched both the gloo shard and
rest-2's `test_[dimsu]*.py` | renamed
`tests/test_hub_push_rank0_ddp_gloo.py` |
| rest-2: `test_checked_in_file_reads_name_an_encoding` | unslothai#17867:
`read_text()` without an encoding in
`tests/test_bind_local_cuda_device.py` | `encoding = "utf-8"` |
| rest-2:
`test_sentence_transformer_trainer_checks_the_placement_before_the_deferred_compile[*]`
| unslothai#17859: `patch_train_dataloader` assumed every trainer class has
`get_train_dataloader` | skip the patch when the method is missing |
| rest-2:
`test_router_aux_loss_gradient_survives_unsloth_checkpointing[*]` |
unslothai#17873: the gpu-marked test still runs under CI's `-m 'not server and
not e2e'` | `has_real_accelerator()` skip next to the gpu mark |
| studio: `test_no_skip_guard_reads_a_spoofable_accelerator_probe` |
unslothai#17850: `test_block_cudagraphs_side_grad.py` skipped on
`torch.cuda.is_available()`, which the CUDA spoof fakes | skip on
`has_real_cuda()` |

## Tests

In a venv matching CI (Python 3.12, torch 2.10.0+cpu, transformers
5.17.0, TRL 1.15, PEFT 0.21.3, `UNSLOTH_COMPILE_DISABLE=1`, `-m 'not
server and not e2e'`):

- gloo: `test_qwen4exp_parallel_gloo.py` failed on main, 9 / 9 pass
here; `test_frozen_table_pp_gloo.py` 13 / 13.
- rest / rest-2 / studio: the nine affected files had exactly CI's 9
failures on main; 153 passed / 6 skipped here.
- diffusion: with Triton blocked (as on the Python 3.13 shard), the 13
failures pass here; with Triton present everything passes.

The only non-test code changes are the PP layer-type name, the Triton
`None` check and the Sentence Transformers method check;
`large_head_attention.py` changes are comments only.
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.

1 participant