Skip to content

Fix FPDT loss layout for batch-first masks - #8541

Open
taking-lying-flat wants to merge 1 commit into
deepspeedai:masterfrom
taking-lying-flat:fix/fpdt-loss-batch-layout
Open

taking-lying-flat wants to merge 1 commit into
deepspeedai:masterfrom
taking-lying-flat:fix/fpdt-loss-batch-layout

Conversation

@taking-lying-flat

Copy link
Copy Markdown
Contributor

FPDT silently applies loss masks to the wrong tokens when the micro-batch contains multiple examples and the mask is nonuniform. FPDT_LogitsLoss returns gathered losses as [sequence, batch], while FPDT_InputConstruct returns its mask as [batch, sequence]. The Megatron-DeepSpeed FPDT caller passes these losses directly to a flattened masked reduction, so the mismatch does not raise a shape error.

Return contiguous [batch, sequence] losses. In backward, select the rank's slice along the sequence dimension and transpose it back before the existing chunked gradient computation. The mask remains global to match the gathered losses.

This PR changes two executable lines and adds one explanatory comment in fpdt_layer.py.

Validation was run locally; test changes are not included in this PR:

  • The regression fails on the original implementation for batch size 2 with a nonuniform mask.
  • A small embedding and output-head training step matches dense cross entropy for the masked loss, both parameter gradients, and the SGD update. It covers batch sizes 1/2 and uniform/nonuniform masks.
  • CPU/gloo on an Intel Core i7-12800HX: 4 parameterized tests passed, each at SP=1 and SP=2 with real collectives.
  • NVIDIA RTX A1000 Laptop GPU (4 GiB), PyTorch 2.13.0+cu130: the same 4 tests passed at SP=1.
  • Changed-file pre-commit checks passed.

Multi-GPU NCCL was not tested on this single-GPU machine.

FPDT returns gathered losses as [sequence, batch], while its loss mask
uses [batch, sequence]. Flattening both silently weights the wrong
tokens when a batch contains multiple examples and a nonuniform mask.

Return contiguous batch-first losses. In backward, select the local
sequence slice and transpose it back for the existing chunked backward.

Validated locally against dense cross entropy on CPU/gloo at SP=1/2
and on an RTX A1000 Laptop GPU at SP=1, comparing loss, parameter
gradients, and one SGD update. Regression tests remain outside this
production-only change. Changed-file pre-commit checks passed.

Signed-off-by: taking-lying-flat <1615405@qq.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 16, 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-09-16T13:37:05.908482Z 74febd0 PR opened
ℹ️ 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 74febd0228

ℹ️ 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".


return loss_all
# Match the batch-first loss mask produced by FPDT_InputConstruct.
return loss_all.t().contiguous()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add the required Signed-off-by trailer

This is a non-merge commit, but its metadata contains no Signed-off-by trailer; the repository requires every non-merge commit to be created with --signoff, so this commit does not satisfy the contribution requirements and should be recreated with the configured Codex <codex@openai.com> identity.

AGENTS.md reference: AGENTS.md:L8-L8

Useful? React with 👍 / 👎.

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