Skip to content

[kimi] Make KDA L2Norm numerics deterministic - #5358

Merged
shuhuayu merged 1 commit into
pytorch:mainfrom
shuhuayu:k3-l2norm-determinism
Oct 14, 2026
Merged

shuhuayu merged 1 commit into
pytorch:mainfrom
shuhuayu:k3-l2norm-determinism

Conversation

@shuhuayu

@shuhuayu shuhuayu commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Bump Attention Gym from 0.0.16 to 0.0.17.
  • Disable KDA L2Norm backward autotuning when PyTorch deterministic algorithms are enabled.
  • Keep the existing Kimi K3 B200 golden: two fresh exports with the pinned schedule reproduce it byte-for-byte.

Why

TorchTitan already pins the chunk KDA schedule in deterministic mode, but L2Norm backward had an independent Triton autotuner. Attention Gym 0.0.17 adds an autotune flag to L2Norm, allowing the complete KDA training path to avoid benchmark- and cache-dependent schedule selection during numerical validation.

Upstream fix: meta-pytorch/attention-gym#632

Numerical validation

B200

Local GB300 repeatability

  • Ran the same FSDP2/EP2 10-step export three independent times on physical GPUs 0 and 1.
  • All three full-precision loss/grad-norm traces are byte-for-byte identical.
  • The GB300 trace is repeatability evidence only; it does not replace the B200 golden.

Validation

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 13, 2026
@shuhuayu shuhuayu added the ciflow/b200 Trigger B200 CI label Oct 13, 2026
@shuhuayu
shuhuayu force-pushed the k3-l2norm-determinism branch 2 times, most recently from 2d198a2 to 628ce32 Compare October 13, 2026 22:27
@shuhuayu
shuhuayu marked this pull request as ready for review October 13, 2026 23:46
@pytorch-bot pytorch-bot Bot added ciflow/docker ciflow/fake-pg Run 1-GPU Fake PG integration tests labels Oct 13, 2026
@shuhuayu
shuhuayu force-pushed the k3-l2norm-determinism branch from 628ce32 to e81226c Compare October 14, 2026 00:33
@shuhuayu
shuhuayu merged commit 508125f into pytorch:main Oct 14, 2026
tianyu-l added a commit that referenced this pull request Oct 14, 2026
… transfer consistently (#5316)

## Why

`torchtitan/rl/` was grouped by technical layer (`components/`,
`distributed/`, `model/`, `observability/`, `losses/`) next to flat
`controller.py`, `trainer.py` and `generator.py`. Code for one service
ended up spread over several of these folders: the generator alone
spanned `generator.py`, `distributed/parallelism.py`, `model/vllm_*.py`
and `observability/vllm.py`. That left no obvious place for a second
inference engine (e.g. SGLang next to vLLM) or a second actor framework
(e.g. Ray next to Monarch).

This PR regroups the package by role, so each service's code sits
together and engine- or framework-specific code sits in its own
subpackage. It also replaces the informal "weight sync" with the push /
pull / load `model_state_dict` terms the RL code already uses elsewhere.
No behavior changes.

This prepares for splitting `generator.py` (engine calls vs. request
dispatch vs. engine-loop thread) in the next PR of the stack.

## What changed

**Layout** (commit 1: moves plus import and path updates only)

| Package | Contents | Moved from |
| --- | --- | --- |
| `controller/` | `controller.py`, `work_buffer.py`, `metrics.py`,
`WINDOWED_FIFO.md` | `controller.py`, `components/work_buffer.py`,
`observability/controller.py` |
| `trainer/` | `trainer.py`, `batcher.py`, `training_sample_builder.py`,
`controller_state.py`, `losses/` | `trainer.py`, `components/*`,
`components/checkpointer.py`, `losses/` |
| `generator/` | `generator.py`, `parallelism.py` | `generator.py`,
`distributed/parallelism.py` |
| `generator/vllm/` | `model_wrapper.py`, `registry.py`, `worker.py`,
`stat_logger.py`, attention / GDN / KDA adapters, `batch_invariance.py`
| `model/*`, `observability/vllm.py` |
| `rollout/` | adds `data.py`, `recorder.py` | `components/data.py`,
`observability/rollout_recorder.py` |
| `routing/` | inter-/intra-generator routing | `distributed/routing/` |
| `actors/monarch/` | Monarch actor wrappers, `torch_elastic.py` |
`distributed/monarch_actors/`, `distributed/torch_elastic.py` |
| `_env.py` | process environment defaults | `_runtime.py` |

`components/checkpointer.py` becomes `trainer/controller_state.py`
because it only holds the controller state that the trainer checkpoints
(`MirroredState`, the state keys). The new package `__init__.py` files
re-export nothing, so importing a light module (e.g.
`generator.parallelism`) never pulls in vLLM or Monarch.

**Names** (commit 2)

| Before | After |
| --- | --- |
| generator `pull_model_state_dict(version)` |
`load_model_state_dict(version)` |
| `ModelStateDictPullMessage`, `LoopAction.PULL_MODEL_STATE_DICT`,
`LoopDecision.pull_version` | `ModelStateDictLoadMessage`,
`LoopAction.LOAD_MODEL_STATE_DICT`, `LoopDecision.load_version` |
| `VLLMGenerator.Config.reset_kv_cache_on_weight_sync` |
`reset_kv_cache_on_model_state_dict_load` |
| "weight sync" in docs and comments | "model state dict sync" (the
whole handoff) or push / pull / load |

The trainer pushes, the router pulls (prefetch on every generator, then
load), and the generator loads. The trainer's `push_model_state_dict`,
the router's `pull_model_state_dict` and the generator's
`prefetch_model_state_dict` keep their names. Metric keys
(`timing/weight_sync/*`) are unchanged so existing dashboards keep
working.

**Push and pull scheduled in the trainer loop** (commit 4)

`WeightSyncManager` only held the trainer loop's two in-flight tasks
(the trainer's push and the generators' pull of the previous step's
policy) plus the group-buffer release that keeps new rollouts born
fresh. It was a piece of `Controller._trainer_loop`'s scheduling, so it
is inlined:

- `_trainer_loop` keeps `push_task` / `pull_task` and awaits each next
to the operation it protects: push N-1 before optimizer step N, pull N-1
before push N.
- `Controller._push_model_state_dict` and
`_pull_model_state_dict_and_release_groups` wrap the actor calls.
- `WEIGHT_SYNC.md` folds into the `_trainer_loop` docstring and
`Trainer.push_model_state_dict` (why staging can overlap
forward/backward); `test_weight_sync.py` becomes `test_trainer_loop.py`,
which drives the real loop with fake actors.

The actor calls and awaits run in the same order as before; trace spans
and metric keys are unchanged.

## User-facing changes

- RL import paths change, e.g. `torchtitan.rl.controller.Controller` ->
`torchtitan.rl.controller.controller.Controller` and
`torchtitan.rl.distributed.monarch_actors` ->
`torchtitan.rl.actors.monarch`. Every in-repo caller is updated.
- `reset_kv_cache_on_weight_sync` is renamed to
`reset_kv_cache_on_model_state_dict_load` (recipes updated).
- `torchtitan.rl.distributed.weight_sync.WeightSyncManager` is removed
(its scheduling is now in `Controller._trainer_loop`).

## Testing

- `pre-commit run --all-files`: clean for the changed files. Locally,
pyrefly also reports 2 errors in
`torchtitan/models/common/attention/kda.py`, which this PR does not
touch; they come from an older local `attn_gym` without the `autotune`
argument that #5358 uses.
- RL CPU unit tests pass on every commit of the stack (441 passed on
this PR's head, none failed).
- Every absolute `torchtitan` import and dotted string reference (e.g.
`mock.patch` targets) resolves to an existing module and symbol, checked
statically. Every `torchtitan.rl` module imports.
- Every RL recipe in `torchtitan_recipes/rl` and
`torchtitan_recipes/tests/rl` builds, except a hardware-gated MXFP8
recipe.
- GPU A/B against main (2x GB300, Qwen3 debug model with random init and
a deterministic varying reward, so every step changes the weights the
generator loads), run on this stack's head (#5317):
- on-policy, batch-invariant: all 65 non-timing metrics (loss, grad
norm, rewards, entropy, trainer-vs-generator logprob diffs, policy age,
rollout lengths) are bitwise identical to main, and the stack is bitwise
identical run to run;
- async (`target_offpolicy_steps=3`): differs from main by the same
amount main differs from itself (which rollouts finish first depends on
timing).

Also included: a separate one-line fix for a malformed call in
`tests/unit_tests/cpu/models/test_build_model_config.py`. pre-commit's
top-level `exclude: 'build'` is an unanchored regex, so it skips every
path containing "build" and never formatted that file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/b200 Trigger B200 CI ciflow/docker ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants