Repository navigation
[Linear] Pass parameters to _linear as stored and read them once - #5249
Conversation
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
tianyu-l
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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_forwardis 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.
There was a problem hiding this comment.
This is when using parameterization (shows up in GT), not for fsdp2, where reading a weight twice leads to parametrization called twice.
| return F.linear(input, *flatten_weight_and_bias(weight, bias)) | ||
|
|
||
|
|
||
| def flatten_weight_and_bias( |
There was a problem hiding this comment.
in terms code organization, makes more sense to me if we make this a staticmethod of Linear, wdyt?
| self.lora_a._linear(input, self.lora_a.weight, None), | ||
| self.lora_b.weight, |
There was a problem hiding this comment.
do they not have the issue, if lora adapters are wrapped by FSDP?
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
…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.
[ghstack-poisoned]
[ghstack-poisoned]
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
Stack from ghstack (oldest at bottom):
Why:
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.
all-gathers on every read, and MXFP8Linear also quantizes, so each MXFP8
forward did both twice (the quantize-count test asserted 2).
backward, which rounds HiMidLoLinear's fp32 grad_weight to bf16
([RFC]
ctxAPIs to declare/query grad dtype for autograd.Function outputs (non-leafgrad_dtype) pytorch#189633), so HiMidLoLinear rejected stacked weights withinplace_wgrad_accum on.
What:
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.
stacked weights accumulate in place and keep an fp32 grad.