Skip to content

Add a backwards linear function to be used with the fused mla q up-proj - #3330

Open
chaseblock wants to merge 7 commits into
NVIDIA:mainfrom
chaseblock:lin_bwd
Open

Add a backwards linear function to be used with the fused mla q up-proj#3330
chaseblock wants to merge 7 commits into
NVIDIA:mainfrom
chaseblock:lin_bwd

Conversation

@chaseblock

Copy link
Copy Markdown
Contributor

Description

Please include a brief summary of the changes, relevant motivation and context.

Fixes # (issue)

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Please list the changes introduced in this PR:

  • Change A
  • Change B

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Signed-off-by: Chase Block <cblock@nvidia.com>
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Aug 7, 2026
chaseblock added a commit to chaseblock/Megatron-LM that referenced this pull request Aug 7, 2026
Requires TE PR: NVIDIA/TransformerEngine#3330

Signed-off-by: Chase Block <cblock@nvidia.com>
@greptile-apps

greptile-apps Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR adds autograd support for the fused MLA query up-projection by applying the inverse RoPE transformation and delegating projection gradients to TE’s linear backward implementation.

  • Adds a Triton kernel for the RoPE backward transformation.
  • Adds fused projection input and weight gradient computation.
  • Adds numerical autograd coverage for input and weight gradients.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
transformer_engine/pytorch/attention/fused_mla_q_uproj.py Adds the fused MLA autograd function, inverse-RoPE Triton kernel, and delegation to the established linear backward implementation; no blocking issue remains.
tests/pytorch/attention/test_fused_mla_q_uproj.py Adds numerical coverage for input and weight gradients through the new autograd path.

Sequence Diagram

sequenceDiagram
  participant A as PyTorch autograd
  participant R as RoPE backward kernel
  participant L as TE linear backward
  A->>R: Query output gradient
  R-->>A: Pre-RoPE projection gradient
  A->>L: Gradient, saved input, weight
  L-->>A: Input and weight gradients
Loading

Reviews (5): Last reviewed commit: "Add fused MLA Q up-projection backward" | Re-trigger Greptile

Comment thread transformer_engine/pytorch/module/linear.py Outdated
Signed-off-by: Chase Block <cblock@nvidia.com>
Signed-off-by: Chase Block <cblock@nvidia.com>
inputmat=x_saved,
weight_fp8=w_q,
saved_weight=w_q,
bias=None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 BF16 bias gradient remains missing

When a BF16 fused MLA backward call sets use_bias=True, this wrapper still passes bias=None; the non-FP8 path delegates bgrad to the weight-gradient GEMM, which does not allocate db without a bias tensor, causing backward_linear to return grad_bias=None and preventing the projection bias from being updated.

Knowledge Base Used: PyTorch Fused Modules (transformer_engine/pytorch/module)

@cyanguwa cyanguwa added the 2.19 label Aug 10, 2026
@cyanguwa
cyanguwa requested review from sudhakarsingh27 and removed request for cyanguwa and ksivaman August 10, 2026 21:58
return (saved_inputmat, wt_save, saved_weight, saved_bias)


def backward_linear(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we really need this function as we could directly call _linear_backward? If we absolutely need it, could we move it to fused_mla_q_uproj.py instead then?

Port the RoPE and projection backward needed by the fused Q up-projection path. Keep the one-use Triton kernel colocated and optional, and request both gradients because this temporary wrapper returns both. Cover the real MXFP8 autograd path so dgrad and wgrad are verified end to end.

Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.19 community-contribution PRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants