Skip to content

[distributed] fix corrupted gradient problem under ring CP. - #14274

Open
sayakpaul wants to merge 7 commits into
mainfrom
fix-ring-grads
Open

[distributed] fix corrupted gradient problem under ring CP.#14274
sayakpaul wants to merge 7 commits into
mainfrom
fix-ring-grads

Conversation

@sayakpaul

Copy link
Copy Markdown
Member

Fixes #14265

@sayakpaul
sayakpaul requested a review from DN6 July 23, 2026 10:02
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@sayakpaul

Copy link
Copy Markdown
Member Author

Cc: @pthombre could you try this PR and tell if it works for your use case as expected?

ctx: torch.autograd.function.FunctionCtx,
grad_out: torch.Tensor,
*args,
query: torch.Tensor | None = None,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think backward ops for varlen flash should also be updated for this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wanted to keep the PR lean and really scoped to what we have tried on the AutoModel side.

Comment thread tests/models/testing_utils/parallelism.py Outdated
value = value.transpose(1, 2).contiguous()
out = out.transpose(1, 2).contiguous()
# Model layout (B, S, H) -> the (B, H, S, 1) shape the cuDNN backward kernel expects.
lse = lse.transpose(1, 2).unsqueeze(-1).contiguous()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does this apply to torch<=2.9? I believe the trailing dim is expected in later versions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah good catch. Should have been addressed now.

lse = softmax_lse.permute(0, 2, 1).contiguous() if return_lse else None
# Save LSE in model layout (B, S, H) so the backward pass — including the ring loop, which
# overrides it with the reduced LSE — always sees the same layout regardless of `return_lse`.
lse = softmax_lse.permute(0, 2, 1).contiguous()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should this apply to FA2 backends as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wanted to keep the PR very lean and cater to what we have verified already.

@sayakpaul
sayakpaul requested a review from DN6 August 11, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ring attention backward uses stale iteration-0 KV from ctx.saved_tensors — silent gradient corruption when training with context parallelism

3 participants