Skip to content

draft(engine): preserve explicit loss reduction prototype - #4

Closed
EazyReal wants to merge 4 commits into
archive/loss-reduction-basefrom
archive/loss-reduction-prototype
Closed

draft(engine): preserve explicit loss reduction prototype#4
EazyReal wants to merge 4 commits into
archive/loss-reduction-basefrom
archive/loss-reduction-prototype

Conversation

@EazyReal

Copy link
Copy Markdown
Owner

Purpose

This draft PR is an archival design surface for the broader engine-level
LossReduction/LossTerm prototype that was removed from the public
loss-aggregation contribution.

The public, review-ready feature implementation is
areal-project#1546. It deliberately uses AReaL's existing
train_batch(loss_fn, loss_weight_fn) contract and does not change FSDP,
Megatron, Archon, or the public engine API.

This PR preserves the exact former aggressive head (2496822a) over its exact
base (aea57eb6) so the design and tests remain inspectable if AReaL later
needs independently normalized multi-term losses.

Prototype captured here

  • LossReduction and named LossTerm contracts;
  • mean, sum, and multi-term global normalizers;
  • train_batch_with_reduction / eval_batch_with_reduction adapters;
  • FSDP, Megatron, and Archon prototype implementations; and
  • distributed and backend-oriented prototype tests.

Status

This is reference code, not merge-ready code. Before reuse, the abstraction
needs a production consumer and a fresh design pass. Known issues include:

  • a zero-normalizer term can still produce NaN gradients when another term is
    active, even if torch.where masks the forward scalar;
  • Megatron evaluation captures a detached scalar that shares storage with the
    value Megatron-Core may rescale in place;
  • local normalizers are not fully constrained to finite, scalar,
    non-negative values; and
  • the backend test additions do not yet prove numerical gradient equivalence
    for sum or multi-term reductions.

Keeping this as a separate draft prevents speculative engine surface from
obscuring the focused public feature while retaining the implementation and its
design history for future work.

EazyReal added 4 commits July 7, 2026 11:16
Represent actor policy-gradient loss aggregation as an explicit distributed loss-reduction contract so token, sequence, prompt, and constant modes share the same engine boundary.

Key changes:

- Add LossReduction/LossTerm and migrate train/eval engine call sites

- Add token_mean, seq_mean, prompt_mean, and constant actor reductions

- Preserve variable prompt groups without rollout filtering changes

- Reject unsupported teacher distillation with non-token aggregation

- Fail fast on non-positive global loss normalizers
Centralize policy-gradient aggregation and route engine loss contracts through one validated reduction. Adapt the original callback-based engine API at the boundary while removing the PR-only helper signatures. Reuse rejection masks for distillation and fail fast for unsupported packed reductions.

Key changes:\n- Move generic loss contracts into a dedicated API module\n- Replace duplicated actor aggregation helpers with PolicyGradientReduction\n- Preserve original callback calls across FSDP, Megatron, and Archon\n- Restore the original v2 RPC callback boundary
Keep the pre-PR TrainEngine callback contract as the stable extension boundary while exposing advanced reductions through explicit methods.

Key changes:
- Restore original trainer and abstract engine callback signatures
- Harden distributed normalizers and policy reduction invariants
- Keep KD rejection normalization partition invariant
- Reject impossible M2PO configurations before training
- Add compatibility and regression coverage across all backends

Refs: areal-project#1443
Keep function documentation focused on behavior and contract rather than refactor history or compatibility rationale.
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 77310042-7247-4a1f-975e-abe284460ac2

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch archive/loss-reduction-prototype

Comment @coderabbitai help to get the list of available commands.

@EazyReal EazyReal closed this Jul 21, 2026
@EazyReal
EazyReal deleted the archive/loss-reduction-prototype branch July 21, 2026 05:37
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