Skip to content

Context parallel: Gemma-4 vision / audio towers no longer refuse the text route - #17930

Merged
danielhanchen merged 2 commits into
mainfrom
cp-media-tower-attention-main
Nov 16, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
cp-media-tower-attention-main

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Context parallel refused Gemma-4 E2B / E4B. Their vision and audio towers also name their attention modules self_attn, so the text-only route checks saw the non-registry audio attention, refused the language model's sliding window layers, and the generic Ulysses wrapper refused the bidirectional vision attention. CP batches are text only, so the towers no longer decide the route.

Results

Gemma-4-E2B now passes the CP route check on Kaggle T4 (main refuses it). Real-model CP training on T4 still runs out of memory for an unrelated reason (Gemma-4 runs in float32 on fp16-only GPUs and its per-layer embedding table is large), so the GPU evidence here is the route check only.

Tests

tests/test_cp_ulysses_media_towers_gloo.py (CPU gloo, tiny Gemma-4 with towers): main refuses; this PR's cp=2 gradients match one process.

Gemma 4 E2B / E4B towers name their attention modules self_attn too, so the text-only route checks saw
non-registry audio attention, refused the language model's sliding window layers, and the generic Ulysses
wrapper refused the bidirectional vision attention. CP batches are text only: skip the towers.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 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-16T08:49:57.323722Z 731c64e 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. Breezy!

Reviewed commit: 6e0f0ec805

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 731c64e8f6

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 33d639f into main Nov 16, 2026
@danielhanchen
danielhanchen deleted the cp-media-tower-attention-main branch November 16, 2026 09:31
danielhanchen added a commit that referenced this pull request Nov 16, 2026
…rest-2 red on main) (#17955)

## Summary

`Repo tests (CPU, rest-2)` is red on main (seen on the staging run of
#17933):

```
tests/test_ulysses_gemma4.py::test_gemma4_generic_route_gates
E   Failed: DID NOT RAISE NotImplementedError
```

Bisected to #17930 (the test passes on its parent, fails on it). #17930
deliberately stopped Gemma-4's vision and audio towers from deciding the
context parallel route: CP batches are text only, so attention modules
under a media tower are skipped by the route checks and get no Ulysses
hooks. The test still expected `attach_attention_hooks` on a multimodal
Gemma 4 to refuse with "Gemma4VisionAttention is bidirectional", which
is exactly the refusal #17930 removed. The test is what is out of date.

## Change

Test only,
`tests/test_ulysses_gemma4.py::test_gemma4_generic_route_gates`: on the
tiny multimodal Gemma 4, `attach_attention_hooks` now has to succeed,
the model must actually have media tower attention, and the set of
hooked `self_attn` modules must equal the text-stack ones exactly (no
tower module hooked, no text module missed). That keeps the property the
old refusal protected (bidirectional vision attention never runs through
the causal Ulysses path) in the form #17930 implements it.

## Testing

- `tests/test_ulysses_gemma4.py`: 24 passed (CPU, gloo). On main the
test fails as above.
- Sabotage: dropping the media tower check from the hook loop in
`unsloth/context_parallel.py` makes the new assertion fail
(`model.vision_tower.encoder.layers.0.self_attn` hooked).
danielhanchen added a commit that referenced this pull request Nov 16, 2026
…he gloo shard (2100 s cap) (#17969)

## Summary

`Repo tests (CPU, gloo)` is timing out again: the shard's pytest runs
under `timeout --signal=INT 2100` and recent runs were cut off there
with nothing failing:

| run | result |
|---|---|
| #17934 (upstream) | 257 passed, 3 skipped in 2089.55 s, exit 124 |
| staging run of #17904 | 258 passed, 3 skipped in 2095.11 s, exit 124 |
| staging run of #17957 | 251 passed, 1 failed in 2096.44 s, exit 124 |

For comparison the same shard ran 241 tests in 1292 s when #17892 merged
this morning. Since then #17886
(`test_tp_dpo_peft_ref_adapter_gloo.py`), #17930
(`test_cp_ulysses_media_towers_gloo.py`),
`test_cp_ulysses_dynamic_len_gloo.py` and the DeepSeek-V4.1 additions to
`test_parallel_deepseek_v41_gloo.py` landed in it, and a slower runner
now goes over the cap. Every one of those tests passes; the shard just
no longer fits.

## Change

- `.github/workflows/studio-backend-ci.yml`:
`tests/test_tp_[!n]*gloo*.py` (the non-native tensor parallel files:
`tp_env`, `tp_eval`, `tp_full_finetune`, `tp_replica`,
`tp_kv_replicated`, `tp_moe_dropout`, `tp_dpo_peft_ref_adapter`, ...; 12
files) moves out of `gloo` into a new `gloo-tp-env` shard. `gloo` keeps
its other three globs (19 files). Comment added with the numbers above.
- `tests/studio/test_backend_ci_parallel_isolation.py`: the command
scan's expected counts go from 15 parallel runs / 10 repo-root shards to
16 / 11, as its message asks when a shard is added.

## Why that split

Measured locally on main with the shard's exact command (`-n 4 --dist
loadgroup`): 275 passed in 1302.83 s. Of the 40 slowest tests (2815
worker-seconds), 2716 s are in `tests/test_tp_[!n]*gloo*.py`; out of
about 5200 worker-seconds in total, so these 12 files are well over half
of the shard. Each half now has room under the cap on a slower runner.

## Testing

- `tests/test_ci_repo_cpu_shards.py` (every repo test file collected by
exactly one shard, every glob matches a file): 15 passed.
- All 15 test files that read `studio-backend-ci.yml`: 341 passed, 3
skipped.
- The new shard's first run on this PR is the timing check.
danielhanchen added a commit that referenced this pull request Nov 16, 2026
…ameter name (#17972)

## Summary


`tests/test_cp_ulysses_media_towers_gloo.py::test_gemma4_with_towers_matches_one_process`
(from #17930) has failed in four of the last gloo shard runs on
unrelated PRs (staging runs of #17957, #17960, #17831 and #17966),
always as:

```
E   AssertionError: model.language_model.layers.0.self_attn.q_proj.weight
```

and it passes locally every time (5 of 5 standalone, and 53 of 53 with
all five `test_cp_*gloo*.py` files in one process, so it is not state
left by an earlier test in the worker). The assertion passes `msg =
name`, which replaces torch's whole mismatch report with the parameter
name, so the CI logs do not say how many elements are off or by how
much. Without that there is no way to tell a float32 reduction-order
difference on the CI runners' CPUs (a tolerance question) from a real
context parallel gradient bug.

## Change

Test only. The gradient `assert_close` calls in
`test_cp_ulysses_media_towers_gloo.py` and
`test_cp_ulysses_generic_gloo.py` pass `msg` as a callable that prefixes
the parameter name to torch's report instead of replacing it:

```
model.language_model.layers.0.self_attn.q_proj.weight: Tensor-likes are not close!

Mismatched elements: ...
Greatest absolute difference: ... (up to ... allowed)
Greatest relative difference: ... (up to ... allowed)
```

Tolerances are unchanged. The next CI failure will carry the numbers
needed to decide whether the tolerance or the code is wrong.

## Testing

- Both files (2-rank gloo, CPU): 24 passed.
- The message form checked directly against `torch.testing.assert_close`
(output above).
danielhanchen added a commit that referenced this pull request Nov 16, 2026
… flake it (#17980)

## Summary


`tests/test_cp_ulysses_media_towers_gloo.py::test_gemma4_with_towers_matches_one_process`
(added in #17930) fails intermittently in the `Repo tests (CPU, gloo)`
job, always on a layer 0 attention projection. With the mismatch report
from #17972 the latest failure reads:

```
model.language_model.layers.0.self_attn.k_proj.weight: Tensor-likes are not close!
Mismatched elements: 2 / 512 (0.4%)
Greatest absolute difference: 1.88e-05 at index (11, 31) (up to 1.42e-05 allowed)
```

That is float32 rounding, not a context parallel bug. Layer 0 gradients
go through every layer of backprop and are summed in a different order
on either side of the shard seam. Running the same versions as CI (torch
2.10.0+cpu, transformers 5.17.0), the worst parameter on this host is
k_proj at 0.62x the tolerance on every run, and the runners whose CPU
kernels round differently hit 1.33x.

## Change

The test now builds the tiny Gemma 4 model in float64 instead of
float32. It checks attention routing with the towers present, so
precision is not what it measures. The tolerance is unchanged. In
float64 the worst parameter (the same k_proj) is at 0.012x the
tolerance, so there is about 80x headroom where float32 had very little.

## Verification

- `tests/test_cp_ulysses_media_towers_gloo.py`: 2 passed.
- Sabotage: skipping `manager.attach_attention_hooks(model)` in the
worker fails at the same tolerance on `embed_tokens.weight` with 3047 /
3072 elements mismatched and a greatest absolute difference of 2.37.
That means the tight tolerance still catches real routing errors.
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