Skip to content

[Linear] Pass parameters to _linear as stored and read them once - #5249

Merged
anijain2305 merged 12 commits into
mainfrom
gh/anijain2305/68/head
Oct 10, 2026
Merged

anijain2305 merged 12 commits into
mainfrom
gh/anijain2305/68/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Why:

What:

  • Linear.forward reads weight and bias once and passes them to _linear
    unflattened ([N, K] or [num_linears, N, K]); implementations flatten them
    with the static Linear._flatten_weight_and_bias(). Updates the TP, vision,
    async TP, LoRA and NVFP4 call sites.
  • MXFP8Linear uses its weight argument (quantize count back to 1).
  • HiMidLoLinear's Function takes the parameter and flattens inside, so
    stacked weights accumulate in place and keep an fp32 grad.
  • No numerics change for unstacked linears.

[ghstack-poisoned]
[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added the ciflow/rl label Oct 9, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 9, 2026
@anijain2305
anijain2305 marked this pull request as ready for review October 9, 2026 22:25
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 9, 2026
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]

@tianyu-l tianyu-l left a comment

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.

please address comments

# reaches AccumulateGrad without the view's backward casting it. Always
# a plain tensor: spmd_types carries TP and EP as annotations instead
# of wrapping the weight as a model-parallel DTensor.
physical_weight = self.weight

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.

MXFP8Linear also quantizes, so each MXFP8 forward did both twice

I'm a bit surprised by this, could you say more?

For traditional bf16 weights, my mental model was that FSDP2 has a state machine, which works roughly as follows:

  • when getting to the module forward boundary, it calls all-gather and points to the unsharded params when calling things like self.weight
  • after forward, it goes back to sharded, if reshard_after_forward is on
  • this mxfp8 impl has a post-all-gather hook which does quantization

Accoding to this mental model, on this line, I'm not calling all-gather again, so I thought we won't quantize again.

@anijain2305 anijain2305 Oct 10, 2026 •

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.

This is when using parameterization (shows up in GT), not for fsdp2, where reading a weight twice leads to parametrization called twice.

Comment thread torchtitan/models/common/linear.py Outdated
return F.linear(input, *flatten_weight_and_bias(weight, bias))


def flatten_weight_and_bias(

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.

in terms code organization, makes more sense to me if we make this a staticmethod of Linear, wdyt?

Comment on lines +111 to +112
self.lora_a._linear(input, self.lora_a.weight, None),
self.lora_b.weight,

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.

do they not have the issue, if lora adapters are wrapped by FSDP?

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Oct 10, 2026
…n place (#5137)

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.18.0)
(oldest at bottom):
* #5250
* #5249
* __->__ #5137

----

- LM-head (wide) backward adds WGRAD into the running fp32 weight.grad
with
addmm(out=), as MXFP8Linear already does; skips AccumulateGrad's add and
a
  [vocab, D] fp32 temporary on ChunkedLossWrapper chunks / microbatches.
- Router (narrow) and fp32-fallback paths unchanged; comments explain
why.
- Explicit `inplace_wgrad_accum: bool = True` config on HiMidLoLinear
and
MXFP8Linear (like DistMoeTransform). Raises instead of silently falling
  back when traced (torch.compile, make_fx) or for a non-leaf weight;
HiMidLoLinear rejects num_linears > 1 with it on. GraphTrainer
conversion
and flavor builders turn it off, and GraphTrainer.Config rejects it on.
- Shared helpers in models/common/inplace_wgrad_accum.py
(uses_inplace_wgrad_accum, read-only running_grad); each backward clears
  weight.grad itself next to its in-place add.
- Pipeline schedules that split backward into input and weight passes
(ZBVZeroBubble, DualPipeV, InterleavedZeroBubble) turn the option off:
both
  passes run under autograd.grad(), where AccumulateGrad never runs, and
adding into .grad there dropped the earlier microbatches' gradients (an
  existing MXFP8 bug). 1F1B, Interleaved1F1B and GPipe keep it.
- The in-place add uses the running grad's dtype (fp32 under an FSDP
fp32
reduce dtype, bf16 without FSDP), so it needs no dtype check; keep
MXFP8's
  grad_dtype fallback, since FSDP clears grad_dtype during backward.
- Qwen3-8B LM head, chunk-2 backward on H100: 11.98 -> 9.19 ms, peak
  3.54 -> 1.22 GiB; weight.grad bitwise identical.
@anijain2305
anijain2305 changed the base branch from gh/anijain2305/68/base to main October 10, 2026 15:36
@anijain2305
anijain2305 changed the base branch from main to gh/anijain2305/68/base October 10, 2026 15:36
[ghstack-poisoned]
[ghstack-poisoned]
@anijain2305
anijain2305 changed the base branch from gh/anijain2305/68/base to main October 10, 2026 15:37
@anijain2305
anijain2305 merged commit 1b06808 into main Oct 10, 2026
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Oct 10, 2026
Why:
- In-place WGRAD accumulation (#5137, and the bf16 Linear next in this
  stack) needs the parameter itself in backward, to read and clear its
  .grad and return a gradient in its shape. _linear only got a flattened
  view, so implementations re-read self.weight or gave up on stacked weights.
- Re-reading self.weight is not free: SimpleFSDP's parametrization
  all-gathers on every read, and MXFP8Linear also quantizes, so each MXFP8
  forward did both twice (the quantize-count test asserted 2).
- A stacked weight's flattened view routes its gradient through the view's
  backward, which rounds HiMidLoLinear's fp32 grad_weight to bf16
  (pytorch/pytorch#189633), so HiMidLoLinear rejected stacked weights with
  inplace_wgrad_accum on.

What:
- Linear.forward reads weight and bias once and passes them to _linear
  unflattened ([N, K] or [num_linears, N, K]); implementations flatten them
  with the static Linear._flatten_weight_and_bias(). Updates the TP, vision,
  async TP, LoRA and NVFP4 call sites.
- MXFP8Linear uses its weight argument (quantize count back to 1).
- HiMidLoLinear's Function takes the parameter and flattens inside, so
  stacked weights accumulate in place and keep an fp32 grad.
- No numerics change for unstacked linears.

ghstack-source-id: ccf7c3d
Pull-Request: pytorch/torchtitan#5249
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants