Skip to content

Adding stamp out using native api for bmm outer product - #179082

Closed
drisspg wants to merge 9 commits into
gh/drisspg/304/basefrom
gh/drisspg/304/head
Closed

drisspg wants to merge 9 commits into
gh/drisspg/304/basefrom
gh/drisspg/304/head

Conversation

@drisspg

@drisspg drisspg commented Apr 1, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

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

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
image

Correctness testing:

"""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 @ezyang @gchanan @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo @slayton58

[ghstack-poisoned]
@drisspg
drisspg requested a review from slayton58 as a code owner April 1, 2026 22:38
@pytorch-bot

pytorch-bot Bot commented Apr 1, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/179082

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 1 New Failure, 8 Unrelated Failures

As of commit 4e878a1 with merge base 511fdd1 (image):

NEW FAILURE - The following job has failed:

UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot

pytorch-bot Bot commented Apr 1, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

drisspg added a commit that referenced this pull request Apr 1, 2026
[ghstack-poisoned]
drisspg added a commit that referenced this pull request Apr 1, 2026
[ghstack-poisoned]
drisspg added a commit that referenced this pull request Apr 2, 2026

@slayton58 slayton58 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes look good, failures unfortunately look real :(

Tests are not what I think we want permanently, but they are tests, and the final guidance on tests hasn't been written yet :)

Stamping.

[ghstack-poisoned]
@drisspg drisspg mentioned this pull request Apr 2, 2026
from .triton_impl import register_to_dispatch


with warnings.catch_warnings():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should put this higher in the stack, around the initial op import - torch/_native/__init__.py#L6 or around the per-op import in torch/_native/ops/__init__.py -- that way if something gets imported before this op later, we won't start getting the warning again

@drisspg drisspg added release notes: nn release notes category module: dsl-native-ops All DSL-based ops (and infra) in torch/_native labels Apr 3, 2026
[ghstack-poisoned]
@drisspg

drisspg commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

I believe this titan tests are flaky, tried a bunch to repo:

Main branch torchtitan tests also fail ~4% of the time with the same crash type
The crash is an inductor compile subprocess dying, nothing to do with bmm
Our bmm override only runs in eager mode, inductor generates its own code and never hits it
Couldn't reproduce any crash locally

@drisspg

drisspg commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 3, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 2 checks: torchtitan-test / torchtitan-x-pytorch-test / test (torchtitan_models_integration, 1, 1, linux.g5.48xlarge.nvidia.gpu), torchtitan-test / torchtitan-x-pytorch-test / test (torchtitan_features_integration, 1, 1, linux.g5.48xlarge.nvidia.gpu)

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / macos-py3-arm64 / test (mps, 1, 1, macos-m2-15)

Details for Dev Infra team Raised by workflow job

@drisspg

drisspg commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

Comment on lines +50 to +56
tu.register_op_override(
"aten",
"bmm",
"CUDA",
functools.partial(_bmm_outer_product_impl, fallback_kernel=fallback_kernel),
allow_multiple_override=True,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure if we care, but this is slightly bc-breaking in that it prevents a user from doing their own override (see vllm-project/vllm#40562 (comment)). Should at least document on how to workaround it in in the release notes

@zou3519 zou3519 added the module: bc-breaking Related to a BC-breaking change label Apr 22, 2026
@pytorch-bot pytorch-bot Bot added the topic: bc breaking topic category label Apr 22, 2026
atalman added a commit that referenced this pull request Apr 27, 2026
) (#181658)

Revert "Adding stamp out using native api for bmm outer product (#179082)"

This reverts commit a021b85.
@github-actions
github-actions Bot deleted the gh/drisspg/304/head branch May 23, 2026 02:27
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
# 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
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…rch#179082)"

This reverts commit fcf5f21.

Reverted pytorch#179082 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#179082 (comment)))
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
# 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
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…rch#179082)"

This reverts commit d33a316.

Reverted pytorch#179082 on behalf of https://github.com/izaitsevfb due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#179082 (comment)))
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
# 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
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
# 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
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…rch#179082)"

This reverts commit fcf5f21.

Reverted pytorch#179082 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#179082 (comment)))
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
# 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
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…rch#179082)"

This reverts commit d33a316.

Reverted pytorch#179082 on behalf of https://github.com/izaitsevfb due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#179082 (comment)))
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
# 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
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
warrendeng added a commit that referenced this pull request Sep 19, 2026
# Summary

PyTorch 2.13 began routing outer-product `bmm` calls to a conditional Triton
override. Registration checks that Triton is installed at a supported version,
but it does not initialize Triton's driver. The first matching call does that
work and may fail when `Python.h`, a host compiler, or another driver
prerequisite is unavailable. Because the Triton implementation has already
been selected, the error escapes instead of using the existing ATen fallback.

PyTorch 2.12.1 passes and 2.13.0 fails with the same Triton 3.7.1 package, so
this is a regression from the eager override introduced by
#179082, not a Triton pin regression.

# Change

Add a private, eager-only availability callback to conditional native override
nodes. After an override's normal predicate matches, the eager router checks
availability before calling its implementation. A false result continues to a
later override or the captured ATen kernel.

Conditional Triton overrides use a cached callback that reads
`triton.runtime.driver.active`. This performs the same driver initialization
the kernel would perform, but while fallback remains possible. The check stays
out of registration so `import torch` does not import Triton or initialize an
accelerator.

Compile/export routing skips the eager callback. Unconditional overrides do
not receive one. Only driver-initialization failures become unavailability;
predicate, implementation, kernel-compilation, and launch errors still
propagate. The public registration API is unchanged.

Test Plan:

```
python test/python_native/test_registry.py -k eager_unavailable -v
python test/python_native/test_registry.py -k fake_tensor_shape_inference -v
python test/python_native/test_native_dsl_ops.py -k triton_driver_availability_is_cached -v
python test/python_native/test_native_dsl_ops.py -k version_skip_env_var_overrides -v
python test/python_native/test_decomp.py -k export_ -v
```

- Fresh-cache missing-header, invalid-compiler, invalid-backend, and custom-
  builder failures used ATen. Healthy cold and warm-cache runs used Triton.
- Import, non-matching `bmm`, FakeTensor, export, and default Inductor execution
  did not initialize the driver through this eager check.
- Later-override routing, ordinary error propagation, CUDA device 1 first use,
  and CPU-only/CUDA/XPU selection were checked separately.
- The cached check costs about 50 ns. Repeated fallback measured 32 us versus
  16 us for direct ATen.
- `git diff --check` and Python byte-compilation passed.

Like existing `functools.cache` users, simultaneous first calls may repeat the
check and cached results are inherited across `fork`. Explicitly exported
graphs containing `_native` ops do not gain runtime ATen fallback.

AI assistance: This change was investigated and prepared with Codex.

Reviewers:

Subscribers:

Tasks:

Tags:

Differential Revision: https://phabricator.intern.facebook.com/D120069454
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged module: bc-breaking Related to a BC-breaking change module: dsl-native-ops All DSL-based ops (and infra) in torch/_native module: inductor release notes: nn release notes category Reverted topic: bc breaking topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants