Repository navigation
[kimi] Make KDA L2Norm numerics deterministic - #5358
Merged
Merged
Conversation
shuhuayu
force-pushed
the
k3-l2norm-determinism
branch
2 times, most recently
from
October 13, 2026 22:27
2d198a2 to
628ce32
Compare
shuhuayu
marked this pull request as ready for review
October 13, 2026 23:46
shuhuayu
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
October 13, 2026 23:46
tianyu-l
approved these changes
Oct 13, 2026
shuhuayu
force-pushed
the
k3-l2norm-determinism
branch
from
October 14, 2026 00:33
628ce32 to
e81226c
Compare
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
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
Validation