Skip to content

TP Trainer parity tests: run AdamW with eps 1e-4 so reduction-order noise is not amplified - #15381

Merged
danielhanchen merged 1 commit into
mainfrom
ci-tp-env-adam-eps
Oct 26, 2026
Merged

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

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-26T14:20:11.150688Z 03b841e 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 it up!

Reviewed commit: 03b841e234

ℹ️ 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 7b14417 into main Oct 26, 2026
5 of 21 checks passed
@danielhanchen
danielhanchen deleted the ci-tp-env-adam-eps branch October 26, 2026 14:25
danielhanchen added a commit that referenced this pull request Oct 26, 2026
…ative Trainer runs back on the default (#15397)

## Problem

#15381 set `adam_epsilon = 1e-4` in the shared `_trainer_args` of
`tests/test_tp_native_gloo.py`. That fixed the three UNSLOTH_TP_SIZE
Trainer tests in `test_tp_env_gloo.py`, but on the CI runners it broke
the two native Trainer tests that use the same helper. Comparing the
`gloo` shard before and after (both on current-main bases):

| CI run | native
`test_trainer_trains_a_tensor_parallel_model_like_one_process[1-2]` /
`[2-2]` | `test_env_tp2_*` (3 tests) |
|---|---|---|
| #15284, base 975e864 (before #15381) | pass | fail |
| #15386, base 0be4c5d (after #15381) | fail on `lora_A` state (`rtol =
1e-4, atol = 1e-6`) | pass |

On this host both groups pass at both settings. The runners' CPUs round
differently from this machine, and I could not emulate theirs here:
`ATEN_CPU_CAPABILITY=avx2` aborts the spawned ranks with `buffer
overflow detected`. So the CI table above is the evidence for which eps
each group needs.

The two groups differ in their adapters. The env tests use
`init_lora_weights = False`, where every factor gets a real gradient
from step 1; that is where the default eps amplifies reduction-order
noise, as measured in #15381. The native tests use the default `B = 0`
start, where `lora_A`'s gradients are tiny for the first steps. There,
eps 1e-4 sits inside their range and moves `lora_A` differently on the
two arms.

## Fix

- `_trainer_args` goes back to AdamW's default eps, so the native
Trainer tests run as they did when they passed on CI.
- `test_tp_env_gloo.py` passes `adam_epsilon = 1e-4` only at its own
training call, with the measurement and the reason in a comment.

Tolerances are unchanged.

## Verification

- `tests/test_tp_env_gloo.py` and `tests/test_tp_native_gloo.py`: 52
passed on this host.
- This PR's own `Repo tests (CPU, gloo)` shard is the check on runner
hardware. Both groups should pass there.

## Not covered here

`test_tp2_x_fsdp2_dp2_matches_one_process[phi3-True-None]` also fails in
both CI runs above, by 6.9e-5 to 7.7e-5 relative on grad norms
(tolerance 1e-5), and passes on this host. It only became visible after
#15355: before that, the `phi3` cases errored on the removed tiny model.
It uses its own hand-written loop (eps 1e-6), not `_trainer_args`, so
this change does not touch it.
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