Repository navigation
[MoE] change torch.bmm back to scatter add - #2775
Conversation
6eac1c1 to
6002fcd
Compare
| out = out.scatter_add( | ||
| dim=0, | ||
| index=token_indices_experts_sorted.reshape(-1, 1).expand(-1, dim), | ||
| src=routed_output, | ||
| ) |
There was a problem hiding this comment.
Can we localize the torch.use_deterministic_algorithms(True) usage to just around this scatter_add, instead of relying on --debug.deterministic for the entire run?
e.g. https://www.internalfb.com/metamate/M4AK5M not sure if context manager works with compile
| token_indices_experts_sorted, | ||
| num_tokens_per_expert, | ||
| ) = self.reorderer(top_scores, selected_experts_indices) | ||
| token_indices_experts_sorted = token_indices_experts_sorted // self.router.top_k |
There was a problem hiding this comment.
In general the code looks a bit different from what's before #1974
Please make sure numerics are not changed. In particular, we had to modify ReordererSequenceParallel to make tp2ep correct.
There was a problem hiding this comment.
also, in this series of refactor, it would be good if you can help rename variables using https://medium.com/@NoamShazeer/shape-suffixes-good-coding-style-f836e72e24fd
There was a problem hiding this comment.
I think previously we also changed ReordererSequenceParallel because we did token_indices_experts_sorted = token_indices_experts_sorted // self.router.top_k inside the reorderer. Now I move this line right after the reorderer. @garrett361 maybe could you also help confirm we don't need other changes here?
For numerics, I ran the loss compare script with dp2tp2ep4etp1(should have tp2ep), loss is matching exactly.
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='llama4' --baseline-config='llama4_debugmodel' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --baseline-ngpus=4 --test-module='llama4' --test-config='llama4_debugmodel' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --test-ngpus=4 --assert-equal --no-seed-checkpoint
|
Do you have performance numbers comparing before/after @acisseJZhong? |
|
I've been doing some testing and found another way to disappear the slow bmm dispatch with just extracting the bmm into a separate function with torch.compile annotation. Boosts qwen3 30b-a30b from 22.9% mfu to 24.7%. Would ya'll be interested in a PR? its a pretty simple one without any triton kernels. |
@chelsea0x3b how's that compared with just enable compile in general? For compile, our goal is to enable it for entire transformer block, instead of on individual functions. |
|
the MoE.forward is not compiled currently, only its submodules are: if isinstance(submod, moe_module.MoE):
# avoid graph breaking on the GroupedExperts' FSDP hooks
# by wrapping each submod's forward instead of their __call__
moe = submod
for attr_name, submod in moe.named_children():
if attr_name == "experts":
# NOTE: We don't compile token dispatch and token combine due to an issue on B200:
# https://github.com/pytorch/torchtitan/issues/1940
continue
submod.compile(backend=compile_config.backend, fullgraph=True)I think you could probably add moe.compile(..., fullgraph=False)to the end and it would be equivalent? You'd need the I will test it out as well |
|
|
Oh, missed the image somehow, sorry. And weird, I found in #1974 (comment) that bmm was faster, fwd + bwd. Maybe inequivalent tests. |
2d2df06 to
2c904d0
Compare
maybe worth re-running your script @garrett361
see #2775 (comment) |
tianyu-l
left a comment
There was a problem hiding this comment.
Is the perf you tested with this local determinism?
Please address comments before landing.
| return _round_up(num_tokens, MXFP8_GROUP_ALIGNMENT_SIZE) | ||
|
|
||
|
|
||
| def scatter_add_with_local_determinism( |
There was a problem hiding this comment.
maybe just
| def scatter_add_with_local_determinism( | |
| def _deterministic_scatter_add( |
inside moe.py
Please don't put in this file because I'm deleting it in #2774
2c904d0 to
d5d54e8
Compare
|
hmm, how about if we compile scatter_add? I think if we compile bmm it will be quite faster. |
|
@tianyu-l compile scatter add and compile bmm seems to be on par |
49ba16e to
718f2da
Compare
There was a problem hiding this comment.
model/common so far mainly holds shared modules used by various models. For ops, which can be potentially used by other components than model, maybe it's worth its own folder, such as torchtitan/ops/. Wdyt?
| @@ -0,0 +1,48 @@ | |||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | |||
There was a problem hiding this comment.
custom op scatter add seems to have slightly lower mfu initially, but later on par. @xmfan please help review if this file looks good to you.
There was a problem hiding this comment.
custom op introduces more cpu overhead
e8adfe0 to
1144c60
Compare
There was a problem hiding this comment.
let's not call it ops/custom_ops.py which sounds redundant. I'd suggest
- either put it in init.py for now since there's only one op
- put it in
scatter_add.py
1144c60 to
8a309f9
Compare
8a309f9 to
f578339
Compare
| out = ( | ||
| self.shared_experts(x) | ||
| if self.shared_experts is not None | ||
| else torch.zeros_like(x) |
There was a problem hiding this comment.
I don't see how that PR means we need to have a torch.zeros_like(x) that ends up being unused
There was a problem hiding this comment.
sorry wdym? out is used here https://github.com/pytorch/torchtitan/pull/2775/changes#diff-9c3e17fd22b0e249ef06806990a6a33b74d45f9ce009100d3a040e27ef074a3eR483, and we don't always have shared expert right
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 Pull Request resolved: #179082 Approved by: https://github.com/slayton58, https://github.com/malfet, https://github.com/albanD
|
locally ran the failed test, it's passing |
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 Pull Request resolved: #179082 Approved by: https://github.com/slayton58, https://github.com/malfet
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 Pull Request resolved: pytorch#179082 Approved by: https://github.com/slayton58, https://github.com/malfet
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 Pull Request resolved: pytorch#179082 Approved by: https://github.com/slayton58, https://github.com/malfet, https://github.com/albanD
…outer product" # Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo slayton58 [ghstack-poisoned]
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo slayton58 [ghstack-poisoned]
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 Pull Request resolved: #179082 Approved by: https://github.com/slayton58, https://github.com/malfet
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 Pull Request resolved: #179082 Approved by: https://github.com/slayton58, https://github.com/malfet, https://github.com/albanD
### Change scatter_add was replaced by torch.bmm in pytorch#1974 due to its non determinism. However, bmm backward kernel was reported to be slow in pytorch#2225. Although @drisspg has pytorch/pytorch#176552 which greatly optimized the bmm backward time under compile, eager mode backward is still slow. Therefore we try to switch back to scatter_add with `torch.use_deterministic_algorithms(True)`, see more in https://docs.pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms. We tested out the determinism part using [this test script. ](https://gist.github.com/acisseJZhong/c56dbc331d3248e488edb24894dc4565), updated [test](https://gist.github.com/acisseJZhong/562b635e6ef96d4f2ca68769dbdbad82) ### Test with topk=2 llama4 debug model(topk=2), qwen3 debug model(topk=8) ``` NCCL_NVLS_ENABLE=0 NGPU=8 ./run_train.sh --module llama4 --config llama4_debugmodel --parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 4 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 2 --parallelism.expert_tensor_parallel_degree 2 --metrics.enable_wandb --training.steps 100 ``` <img width="1075" height="288" alt="image" src="https://github.com/user-attachments/assets/81e7523b-e833-4351-ad57-f5e1276807f3" /> ### Loss compare ``` NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='llama4' --baseline-config='llama4_debugmodel' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --baseline-ngpus=4 --test-module='llama4' --test-config='llama4_debugmodel' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --test-ngpus=4 --assert-equal --no-seed-checkpoint ``` Testing with llama4 debug model, scatter_add with torch deterministic mode on seems still slightly faster than torch.bmm. cc @garrett361 @chelsea0x3b @rakkit @drisspg
### Change scatter_add was replaced by torch.bmm in pytorch#1974 due to its non determinism. However, bmm backward kernel was reported to be slow in pytorch#2225. Although @drisspg has pytorch/pytorch#176552 which greatly optimized the bmm backward time under compile, eager mode backward is still slow. Therefore we try to switch back to scatter_add with `torch.use_deterministic_algorithms(True)`, see more in https://docs.pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms. We tested out the determinism part using [this test script. ](https://gist.github.com/acisseJZhong/c56dbc331d3248e488edb24894dc4565), updated [test](https://gist.github.com/acisseJZhong/562b635e6ef96d4f2ca68769dbdbad82) ### Test with topk=2 llama4 debug model(topk=2), qwen3 debug model(topk=8) ``` NCCL_NVLS_ENABLE=0 NGPU=8 ./run_train.sh --module llama4 --config llama4_debugmodel --parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 4 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 2 --parallelism.expert_tensor_parallel_degree 2 --metrics.enable_wandb --training.steps 100 ``` <img width="1075" height="288" alt="image" src="https://github.com/user-attachments/assets/81e7523b-e833-4351-ad57-f5e1276807f3" /> ### Loss compare ``` NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='llama4' --baseline-config='llama4_debugmodel' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --baseline-ngpus=4 --test-module='llama4' --test-config='llama4_debugmodel' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --test-ngpus=4 --assert-equal --no-seed-checkpoint ``` Testing with llama4 debug model, scatter_add with torch deterministic mode on seems still slightly faster than torch.bmm. cc @garrett361 @chelsea0x3b @rakkit @drisspg
### Change scatter_add was replaced by torch.bmm in pytorch#1974 due to its non determinism. However, bmm backward kernel was reported to be slow in pytorch#2225. Although @drisspg has pytorch/pytorch#176552 which greatly optimized the bmm backward time under compile, eager mode backward is still slow. Therefore we try to switch back to scatter_add with `torch.use_deterministic_algorithms(True)`, see more in https://docs.pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms. We tested out the determinism part using [this test script. ](https://gist.github.com/acisseJZhong/c56dbc331d3248e488edb24894dc4565), updated [test](https://gist.github.com/acisseJZhong/562b635e6ef96d4f2ca68769dbdbad82) ### Test with topk=2 llama4 debug model(topk=2), qwen3 debug model(topk=8) ``` NCCL_NVLS_ENABLE=0 NGPU=8 ./run_train.sh --module llama4 --config llama4_debugmodel --parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 4 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 2 --parallelism.expert_tensor_parallel_degree 2 --metrics.enable_wandb --training.steps 100 ``` <img width="1075" height="288" alt="image" src="https://github.com/user-attachments/assets/81e7523b-e833-4351-ad57-f5e1276807f3" /> ### Loss compare ``` NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='llama4' --baseline-config='llama4_debugmodel' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --baseline-ngpus=4 --test-module='llama4' --test-config='llama4_debugmodel' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --test-ngpus=4 --assert-equal --no-seed-checkpoint ``` Testing with llama4 debug model, scatter_add with torch deterministic mode on seems still slightly faster than torch.bmm. cc @garrett361 @chelsea0x3b @rakkit @drisspg
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 Pull Request resolved: pytorch#179082 Approved by: https://github.com/slayton58, https://github.com/malfet
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 Pull Request resolved: pytorch#179082 Approved by: https://github.com/slayton58, https://github.com/malfet, https://github.com/albanD
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 Pull Request resolved: pytorch#179082 Approved by: https://github.com/slayton58, https://github.com/malfet, https://github.com/albanD
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 Pull Request resolved: pytorch#179082 Approved by: https://github.com/slayton58, https://github.com/malfet
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 Pull Request resolved: pytorch#179082 Approved by: https://github.com/slayton58, https://github.com/malfet, https://github.com/albanD
# Summary PT does not do a great job with this special case which showed up in how titan implemented the post expert router weighting. W/ scores after expert. Deemed a decent enough use case to try out our new APIS; Some learnings -> copy on write will fail for pretty much every triton overload. Why? because calling `data_ptr` always redirects to mutable_data_ptr. As a follow up we should expose const_data_ptr to python. But even then its pretty annoying to jam this into triton or other dsls. Autouning is really nice, we should figure out what are reasonable bounds for first run overhead. Im sure after this lands more learnings will come 🙈 ### Perf ```csv dtype,layout,shape,cublas_ms,cublas_TB/s,triton_ms,triton_TB/s float16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,contiguous,64x256x1 @ 64x1x256,0.007,1.22,0.003,2.45 float16,contiguous,512x256x1 @ 512x1x4096,0.558,1.93,0.152,7.11 float16,contiguous,65536x8x1 @ 65536x1x2048,4.031,0.60,0.713,3.39 float16,contiguous,12345x7x1 @ 12345x1x1999,0.758,0.52,0.170,2.33 float16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 float16,lhs_permuted,64x256x1 @ 64x1x256,0.007,1.22,0.004,2.36 float16,lhs_permuted,512x256x1 @ 512x1x4096,0.560,1.93,0.152,7.09 float16,lhs_permuted,65536x8x1 @ 65536x1x2048,4.039,0.60,0.716,3.38 float16,lhs_permuted,12345x7x1 @ 12345x1x1999,0.759,0.52,0.184,2.14 float16,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.04 float16,rhs_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.37 float16,rhs_permuted,512x256x1 @ 512x1x4096,0.542,1.99,0.148,7.30 float16,rhs_permuted,65536x8x1 @ 65536x1x2048,15.139,0.16,0.884,2.74 float16,rhs_permuted,12345x7x1 @ 12345x1x1999,2.798,0.14,0.176,2.24 float16,both_permuted,4x128x1 @ 4x1x64,0.004,0.02,0.002,0.03 float16,both_permuted,64x256x1 @ 64x1x256,0.007,1.24,0.004,2.30 float16,both_permuted,512x256x1 @ 512x1x4096,0.540,2.00,0.148,7.29 float16,both_permuted,65536x8x1 @ 65536x1x2048,15.112,0.16,0.892,2.71 float16,both_permuted,12345x7x1 @ 12345x1x1999,2.876,0.14,0.188,2.10 bfloat16,contiguous,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,contiguous,64x256x1 @ 64x1x256,0.008,1.01,0.003,2.44 bfloat16,contiguous,512x256x1 @ 512x1x4096,0.756,1.43,0.152,7.11 bfloat16,contiguous,65536x8x1 @ 65536x1x2048,10.272,0.24,0.713,3.39 bfloat16,contiguous,12345x7x1 @ 12345x1x1999,1.800,0.22,0.170,2.33 bfloat16,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,lhs_permuted,64x256x1 @ 64x1x256,0.008,1.00,0.004,2.37 bfloat16,lhs_permuted,512x256x1 @ 512x1x4096,0.761,1.42,0.152,7.09 bfloat16,lhs_permuted,65536x8x1 @ 65536x1x2048,10.272,0.24,0.716,3.38 bfloat16,lhs_permuted,12345x7x1 @ 12345x1x1999,1.800,0.22,0.184,2.14 bfloat16,rhs_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.04 bfloat16,rhs_permuted,64x256x1 @ 64x1x256,0.008,1.02,0.004,2.37 bfloat16,rhs_permuted,512x256x1 @ 512x1x4096,0.543,1.99,0.148,7.30 bfloat16,rhs_permuted,65536x8x1 @ 65536x1x2048,10.416,0.23,0.884,2.73 bfloat16,rhs_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.176,2.24 bfloat16,both_permuted,4x128x1 @ 4x1x64,0.003,0.02,0.002,0.03 bfloat16,both_permuted,64x256x1 @ 64x1x256,0.008,1.05,0.004,2.29 bfloat16,both_permuted,512x256x1 @ 512x1x4096,0.541,1.99,0.148,7.29 bfloat16,both_permuted,65536x8x1 @ 65536x1x2048,10.415,0.23,0.892,2.71 bfloat16,both_permuted,12345x7x1 @ 12345x1x1999,1.769,0.22,0.188,2.10 float32,contiguous,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,contiguous,64x256x1 @ 64x1x256,0.008,2.03,0.005,3.64 float32,contiguous,512x256x1 @ 512x1x4096,0.551,3.91,0.317,6.80 float32,contiguous,65536x8x1 @ 65536x1x2048,3.718,1.30,0.824,5.86 float32,contiguous,12345x7x1 @ 12345x1x1999,0.696,1.14,0.170,4.65 float32,lhs_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,lhs_permuted,64x256x1 @ 64x1x256,0.008,2.01,0.005,3.49 float32,lhs_permuted,512x256x1 @ 512x1x4096,0.560,3.85,0.317,6.81 float32,lhs_permuted,65536x8x1 @ 65536x1x2048,3.719,1.30,0.873,5.54 float32,lhs_permuted,12345x7x1 @ 12345x1x1999,0.696,1.13,0.185,4.27 float32,rhs_permuted,4x128x1 @ 4x1x64,0.004,0.04,0.002,0.07 float32,rhs_permuted,64x256x1 @ 64x1x256,0.011,1.50,0.005,3.53 float32,rhs_permuted,512x256x1 @ 512x1x4096,0.738,2.92,0.305,7.08 float32,rhs_permuted,65536x8x1 @ 65536x1x2048,7.400,0.65,1.178,4.10 float32,rhs_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.207,3.83 float32,both_permuted,4x128x1 @ 4x1x64,0.003,0.04,0.002,0.07 float32,both_permuted,64x256x1 @ 64x1x256,0.011,1.48,0.005,3.43 float32,both_permuted,512x256x1 @ 512x1x4096,0.751,2.87,0.304,7.09 float32,both_permuted,65536x8x1 @ 65536x1x2048,7.397,0.65,1.188,4.07 float32,both_permuted,12345x7x1 @ 12345x1x1999,1.420,0.56,0.208,3.79 ``` <img width="1866" height="186" alt="image" src="https://github.com/user-attachments/assets/7646d6b2-def7-4a0a-bd97-ad4e249ad39f" /> ### Correctness testing: ```Py """Test bmm outer product against cublas for all layout/dtype/shape permutations.""" import torch from torch._native.registry import deregister_op_overrides, reenable_op_overrides SHAPES = [ (1, 1, 1), (1, 64, 128), (4, 8, 16), (8, 1, 1), (32, 8, 256), (64, 256, 512), (256, 8, 2048), (12345, 7, 1999), ] DTYPES = [torch.float32, torch.float16, torch.bfloat16] LAYOUTS = { "contiguous": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "lhs_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(B, 1, N, device="cuda", dtype=dt), ), "rhs_permuted": lambda B, M, N, dt: ( torch.randn(B, M, 1, device="cuda", dtype=dt), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), "both_permuted": lambda B, M, N, dt: ( torch.randn(M, B, 1, device="cuda", dtype=dt).permute(1, 0, 2), torch.randn(N, B, 1, device="cuda", dtype=dt).permute(1, 2, 0), ), } def cublas_bmm(a, b): deregister_op_overrides(disable_op_symbols="bmm") try: return torch.bmm(a, b) finally: reenable_op_overrides(enable_op_symbols="bmm") passed = 0 failed = 0 for dtype in DTYPES: for layout_name, make_inputs in LAYOUTS.items(): for B, M, N in SHAPES: a, b = make_inputs(B, M, N, dtype) assert a.shape == (B, M, 1) and b.shape == (B, 1, N) triton_result = torch.bmm(a, b) cublas_result = cublas_bmm(a, b) assert triton_result.stride() == cublas_result.stride() if torch.equal(triton_result, cublas_result): passed += 1 else: max_diff = (triton_result - cublas_result).abs().max().item() print(f"MISMATCH {dtype} {layout_name} ({B},{M},{N}) max_diff={max_diff:.2e}") failed += 1 print(f"\n{passed} passed, {failed} failed out of {passed + failed}") ``` See: pytorch/torchtitan#2775 Pull Request resolved: pytorch#179082 Approved by: https://github.com/slayton58, https://github.com/malfet, https://github.com/albanD



Change
scatter_add was replaced by torch.bmm in #1974 due to its non determinism. However, bmm backward kernel was reported to be slow in #2225. Although @drisspg has pytorch/pytorch#176552 which greatly optimized the bmm backward time under compile, eager mode backward is still slow.
Therefore we try to switch back to scatter_add with
torch.use_deterministic_algorithms(True), see more in https://docs.pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms. We tested out the determinism part using this test script. , updated testTest with topk=2
llama4 debug model(topk=2), qwen3 debug model(topk=8)
Loss compare
Testing with llama4 debug model, scatter_add with torch deterministic mode on seems still slightly faster than torch.bmm.
cc @garrett361 @chelsea0x3b @rakkit @drisspg