Skip to content

TP x FSDP2 parity test: keep AdamW out of its eps cliff on the lm_head copy - #15264

Merged
danielhanchen merged 1 commit into
mainfrom
ci-tp-fsdp2-adam-eps
Oct 25, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
ci-tp-fsdp2-adam-eps

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

tests/test_tp_native_gloo.py::test_tp2_x_fsdp2_dp2_matches_one_process[llama-True-lora_kwargs4] fails now and then in Repo tests (CPU, rest) on unrelated PRs (#15159, #15174, #15217, #15218, #15222, #15225) with only AssertionError: base_model.model.lm_head.weight. Gradients, losses and norms all match in those runs. Only the saved lm_head copy differs.

Cause

The test, not the product. That case trains a full lm_head copy (modules_to_save, 512000 entries) with AdamW at lr 1e-2 and the default eps of 1e-8. At g = 0, Adam's g / (sqrt(v) + eps) has slope lr / eps = 1e6. Measured on main on this host:

value
median step-0 lm_head grad 5.7e-6
entries with step-0 grad below 1e-8 313
step-0 grad of the 10 worst saved-weight entries 4e-11 to 7e-9
worst saved-weight diff / tolerance 0.46

So reduction-order noise of about 1e-12 on a near-zero gradient becomes about 5e-6 of weight. That is already half the atol = 1e-5 here. A runner whose CPU kernels sum in a different order goes over it.

Fix

  • Both arms use AdamW(eps = 1e-6). The optimizer is still identical on both sides, and 1e-6 is small next to the typical gradient, so the head still trains. The worst entry drops to 0.0075 of the tolerance (60x headroom).
  • The saved-weight assert now keeps assert_close's mismatch detail. It used to print only the key, which is why the CI logs never showed how far off the weight was.

Verification

  • tests/test_tp_native_gloo.py: 38 passed.
  • Sabotage on the last step, after the norms are taken, so only the saved weight can catch it:
    • TP arm skips the lm_head update: fails, 511627 / 512000 mismatched, max abs diff 1.0e-2.
    • Each dp half's lm_head gradient scaled by +/-15% (a stand-in for a missing dp reduction): fails, 493693 / 512000 mismatched, max abs diff 8.1e-4.
    • No sabotage: passes.

The modules_to_save lm_head holds hundreds of entries whose step-0 gradient
is about 1e-9, below AdamW's default eps of 1e-8, where g / (sqrt(v) + eps)
has slope lr / eps = 1e6. Reduction-order noise of about 1e-12 then becomes
about 5e-6 of saved weight, half the tolerance on this host and over it on
some CI runners. eps = 1e-6 keeps both arms on the same optimizer and cuts the
worst element to under 1% of the tolerance, while a skipped or unreduced
lm_head update on the last step still fails the check. The assert now keeps
the mismatch detail instead of printing only the key.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T13:32:15.004503Z e9dde45 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. More of your lovely PRs please.

Reviewed commit: e9dde458ce

ℹ️ 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 ab7624b into main Oct 25, 2026
0 of 14 checks passed
@danielhanchen
danielhanchen deleted the ci-tp-fsdp2-adam-eps branch October 25, 2026 13:44
danielhanchen added a commit that referenced this pull request Oct 26, 2026
…oise is not amplified (#15381)

## Problem

`Repo tests (CPU, gloo)` is red on main since #15326. Three tests fail
locally and in CI (for example #15284):

- `test_env_tp2_trainer_matches_one_process[sharded_16bit]`
- `test_env_tp2_trainer_matches_one_process[full_load]`
- `test_env_tp2_with_fsdp2_flag_and_no_dp_dimension`

They fail like this:

```
comparison failed. Mismatched elements: 1 / 3:
Max relative difference: 1.24e-05
Index | Obtained           | Expected
2     | 0.3388126790523529 | 0.33881688117980957 +- 3.4e-06
```

## Product or test

The test. I measured both arms (`tp = 2` against one process) step by
step on main:

| | step 1 | step 2 | step 3 |
|---|---|---|---|
| loss, relative gap | 0 | 0 | 0 |
| grad norm, relative gap | 3.2e-7 | 1.6e-6 | 1.2e-5 |

The worst LoRA tensor after the run (`layers.1.mlp.gate_proj.lora_B`) is
1.35e-4 off, while the rest sit near 2e-6.

The losses match exactly and the first-step gap is fp32 reduction-order
noise. The gap then grows about 10x per step, which is what `m /
(sqrt(v) + eps)` does to near-zero LoRA gradients at the Trainer's
default `adam_epsilon = 1e-8`. Changing only eps on both arms:

| `adam_epsilon` | step-3 grad-norm gap | worst tensor |
|---|---|---|
| 1e-8 (default) | 1.24e-5 | 1.35e-4 |
| 1e-5 | 5.3e-6 | 5.9e-6 |
| 1e-4 | 9.4e-7 | 4.4e-7 |
| 1e-3 | 1.2e-6 | 1.4e-7 |

A tensor-parallel bug would not shrink with eps. This is the same effect
#15264 fixed for the hand-written TP x FSDP2 loop. The new
UNSLOTH_TP_SIZE Trainer tests share `_trainer_args`, which still used
the default eps.

## Fix

`_trainer_args` in `tests/test_tp_native_gloo.py`, used by both the
native and the env-driven Trainer tests, now passes `adam_epsilon =
_TRAINER_ADAM_EPS` (1e-4). A comment records the measurement. The
tolerances (`rel = 1e-5` on grad norms, `rtol = 1e-4, atol = 1e-6` on
the adapter state) are unchanged.

## Verification

- `tests/test_tp_env_gloo.py` and `tests/test_tp_native_gloo.py` in
full: 52 passed. On main, the three tests above fail.
- Sabotage: with the backward all-reduce of the column-parallel input
removed in `tp_native.py` (`return grad, None`), a real TP bug that
leaves earlier layers with partial gradients, all three tests fail at
`Max relative difference: 3.5e-4`, 35x over the tolerance. The looser
eps does not hide a real TP error.
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