Skip to content

refactor(loss): move loss-input preparation into loss_input.py - #4232

Merged
yuki-97 merged 3 commits into
mainfrom
yukih/refactor-loss-input
Sep 22, 2026
Merged

yuki-97 merged 3 commits into
mainfrom
yukih/refactor-loss-input

Conversation

@yuki-97

@yuki-97 yuki-97 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

What does this PR do ?

The three prepare_*_loss_input entry points are the main way logits get turned into
loss input, but living in a file called utils.py makes them hard to find.

Pure code move, no behavior change: extracts prepare_opd_full_loss_input,
prepare_loss_input and prepare_packed_loss_input out of
nemo_rl/algorithms/loss/utils.py into a new nemo_rl/algorithms/loss/loss_input.py.
utils.py keeps the five helpers that have consumers outside these three functions.

The nemo_rl.algorithms.loss re-exports are unchanged, so
from nemo_rl.algorithms.loss import prepare_loss_input keeps working.

🤖 Generated with Claude Code

@copy-pr-bot

copy-pr-bot Bot commented Sep 21, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@yuki-97 yuki-97 added the CI:L1 Run doctests, unit tests, and functional tests label Sep 21, 2026
@yuki-97

yuki-97 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 16dd107

@yuki-97 yuki-97 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No action needed — LGTM. The pure-code-move claim was verified mechanically; no inline comments.

TL;DR — an AST comparison of base utils.py against head utils.py + loss_input.py shows all 8 top-level functions identical; the only differing node in ~550 lines is the _prepare_opd_full_loss_input rename and its one call site.

Context — no action.

1. The move is AST-clean. base \ (head_utils | head_loss_input) is empty. The single differing node is the call at loss_input.py:285. Module-level statements partition exactly: base's 8 imports plus the TYPE_CHECKING block split with no constant, logger, __all__, decorator or try/except left behind or duplicated. Zero unresolved free names and zero unused imports in either file.

2. No stale string patch targets — the usual silent breakage in a code move. Five monkeypatch.setattr string targets pointed into the moved module and all five were updated (test_loss_functions.py:2742, :2746, :2789, :2793, test_sequence_packing_fusion.py:71). All seven surviving loss.utils importers resolve to names that genuinely stayed. No dynamic reference to the old paths anywhere: no _target_, registry, entry point, getattr, or dotted path in any yaml/toml/json/sh/md.

3. No import cycle. loss_input.py imports the submodules loss.utils and loss.interfaces, never the package. utils.py's transitive nemo_rl closure is {distributed.model_utils, algorithms.logits_sampling_utils} — it never reaches algorithms.loss, in either direction.

4. The cut line is right. _pack_input_ids correctly stayed in utils.py: besides loss_input.py:498 it has an in-module caller at utils.py:99 and a cross-package one at nemo_rl/models/megatron/train.py:397 — moving it would have made utils.py import from loss_input.py. Three of the four remaining public helpers are consumed by wrapper.py.

5. The privatization changes no public surface. prepare_opd_full_loss_input was never in __all__ and had exactly one production caller — the dispatcher itself. tests/unit/algorithms/test_opd_full.py:762 was rewritten to reach the branch through the public prepare_loss_input rather than keeping a private back door; every assertion is unchanged, and the OPD_FULL branch forwards all arguments verbatim.

6. pyrefly.toml:62 adds the new module to project-includes, so the moved code stays type-checked.

Reviewed by a five-agent team (RL expert, bug finder, test reviewer, design reviewer, devil's advocate). One candidate finding — an unexplained deferred import at wrapper.py:313 — was dropped as out-of-diff and pre-existing (introduced in #3463); worth a separate one-line cleanup, not this PR.

Generated by Claude Code

@yuki-97
yuki-97 force-pushed the yukih/refactor-loss-input branch from 16dd107 to cdc1daa Compare September 22, 2026 01:03
@yuki-97

yuki-97 commented Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test cdc1daa

@yuki-97
yuki-97 marked this pull request as ready for review September 22, 2026 01:03
@yuki-97
yuki-97 requested review from a team as code owners September 22, 2026 01:03
Signed-off-by: Yuki Huang <[email protected]>
@yuki-97

yuki-97 commented Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 39f6624

@yuki-97
yuki-97 merged commit 6c5f78b into main Sep 22, 2026
186 of 189 checks passed
@yuki-97
yuki-97 deleted the yukih/refactor-loss-input branch September 22, 2026 08:09
yfw added a commit that referenced this pull request Sep 23, 2026
Brings #4198 (remove the DTensor v1 policy worker), #4207, #4196, #4143
(MiniMax-M3 GRPO support), #4233, #4232 and #3408 (flow_grpo research).

Conflicts and resolution:
- pyproject.toml: main removed the `fsdp` extra together with the v1 worker;
  this branch had given that extra its own torch 2.13 pin. The extra is
  dropped here as well (with its `[tool.uv] conflicts` entries), and the
  per-extra torch comment no longer lists it.
- uv.lock: regenerated from this branch's lock; the only change is the
  addition of #3408's flow-grpo research dependencies (albumentations,
  paddleocr, scikit-image, ...). No existing package moved, and nothing
  resolves below main except the deliberate numpy / llguidance / flash-attn
  pins.

Also removed, not conflicting: `_patch_vllm_minimax_m3_topk_buffer_layout`
(#4143) and its tests. It backported vllm-project/vllm@d1a8ba63 (token-major
vs head-major `topk_indices_buffer`) onto vLLM 0.26.0 and its docstring asks
for removal at vLLM >= 0.27; vLLM 0.29.0's `minimax_m3/common/indexer.py`
already carries the head-major `buf_htk` layout, so on this branch the patch
could only ever log "source shape not found" and its installed-vLLM anchor
tests would fail.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Yi-Fu Wu <[email protected]>
@yfw yfw mentioned this pull request Sep 23, 2026
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:L1 Run doctests, unit tests, and functional tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants