Repository navigation
[router] Fp32 for backward and use bf16x9 kernels - #4484
Conversation
1d3dc70 to
3b9653c
Compare
| parallel_dims=self.parallel_dims, | ||
| spmd_typechecking=False, | ||
| ) | ||
|
|
| if not isinstance(config.gate, RouterGateLinear.Config): | ||
| raise ValueError( | ||
| "TokenChoiceTopKRouter requires an unquantized " | ||
| "RouterGateLinear.Config gate." | ||
| ) |
There was a problem hiding this comment.
you already have the typing restriction above, so I feel this is less necessary
| ) | ||
|
|
||
|
|
||
| @spmd.register_local_autograd_function |
There was a problem hiding this comment.
@pianpwk do we still need this for the trivial rule?
| @dataclass(kw_only=True, slots=True) | ||
| class Config(Linear.Config): | ||
| def build(self, **kwargs): | ||
| if get_spmd_backend() == "partial_dtensor": | ||
| raise ValueError( | ||
| "RouterGateLinear requires parallelism.spmd_backend=" | ||
| "'spmd_types'; got 'partial_dtensor'. aten.mm.dtype has no " | ||
| "DTensor sharding strategy." | ||
| ) | ||
| return Linear.Config.build(self, **kwargs) |
There was a problem hiding this comment.
#4419 is removing partial_dtensor backend, I think it's fine to assume it'd be landed soon
| with torch.autocast(device_type=input.device.type, enabled=False): | ||
| output_TE = _RouterGateLinearFunction.apply(input, self.weight) | ||
| if self.bias is not None: | ||
| output_TE = output_TE + self.bias.float() |
There was a problem hiding this comment.
there's no addmm variant that can take this?
There was a problem hiding this comment.
We have an operator aten.addmm.dtype which supports addmm with output_dtype but it causes compile time failure. So I kept it outside.
If you would insist, I can go figure out whats going on with inductor here because it is giving a little weird error
TypeError: tuned_addmm() takes 3 positional arguments but 4 were given
But I think its a second order optimization (bias is 1D tensor).
There was a problem hiding this comment.
sounds good as it seems router typically doesn't have bias anyway.
https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/common/config_utils.py#L338
| def forward(self, input: torch.Tensor) -> torch.Tensor: | ||
| # The router owns its input casts; an enclosing autocast context must not | ||
| # promote the BF16 forward operands back to FP32. | ||
| with torch.autocast(device_type=input.device.type, enabled=False): |
There was a problem hiding this comment.
is this necessary -- what if we don't have this autocast?
There was a problem hiding this comment.
It was not necessary ... I tried to force the forward to be bf16 compute and missed that if the input dtypes are fp32, we should keep the compute in fp32.
| ) | ||
| else: | ||
| # aten.mm.dtype is CUDA-only. Multiplying the BF16-rounded values in | ||
| # FP32 is an exact CPU reference for BF16 products with FP32 |
There was a problem hiding this comment.
what if it's rocm or other GPU?
| input_bf16_TD = input_TD.to(torch.bfloat16) | ||
| weight_bf16_ED = weight_ED.to(torch.bfloat16) |
There was a problem hiding this comment.
if input and weight are of fp32, do we still
- convert them to bf16 in fwd and do matmul
- save bf16 ctx and cast up again in bwd
I somehow feel we could just run high precision matmul anyway, in those uncommon cases.
| if ( | ||
| device_type != "cuda" | ||
| or not torch.cuda.is_available() | ||
| or torch.cuda.get_device_capability() not in {(10, 0), (10, 3)} |
There was a problem hiding this comment.
Change the cond so that vera rubin and future gpus are also supported.
a427493 to
7311ca3
Compare
7311ca3 to
df58dd5
Compare
## Summary - automatically apply `ciflow/8gpu` when a PR changes files under `tests/assets/losses/` - refresh the stale Real-PG A10G goldens for DeepSeek V3, GPT-OSS, Muse Glimmer, and Qwen3 - normalize the generated parallelism header in the DeepSeek V3 CP+PP golden ## Why A Fake-PG golden update signals an expected numerics change, but the corresponding Real-PG numerics currently run only after merge unless the author manually adds `ciflow/8gpu`. Labeling any golden update automatically makes the author validate the full 8-GPU Real-PG suite before landing. The current Real-PG values became stale after #4484 changed MoE router numerics and #4489 changed Muse Glimmer initialization. ## Test plan - `pre-commit run --files .github/labeler.yml tests/assets/losses/real_pg/*.txt` - [8-GPU Real-PG golden export on current main](https://github.com/pytorch/torchtitan/actions/runs/34075482250): feature and model suites passed - compared all exported goldens byte-for-byte with the checked-in files - confirmed all 10 loss and grad-norm steps for the four changed numerical goldens against an independent scheduled Real-PG run ([run 34070052002](https://github.com/pytorch/torchtitan/actions/runs/34070052002))
The PR introduces RouterLinear, a subclass of Linear
fp32For the fp32 compute, we use "bf16x9" trick to emulate fp32 precision, this allows Blackwell to use tensor cores, and even though bf16x9 has way more compute ops than fp32, the tensor core acceleration overall benefits. This feature was recently introduced in pytorch/pytorch#195301