Skip to content

Add CuTeDSL MXFP8 M-groups scale rearrange kernel - #4585

Closed
alexsamardzic wants to merge 8 commits into
gh/alexsamardzic/1/basefrom
gh/alexsamardzic/1/head
Closed

alexsamardzic wants to merge 8 commits into
gh/alexsamardzic/1/basefrom
gh/alexsamardzic/1/head

Conversation

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (5 Unrelated Failures)

As of commit 571a062 with merge base 3818749 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

BROKEN TRUNK - The following jobs failed but was present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 21, 2026
@alexsamardzic

alexsamardzic commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor Author

This PR adds a CuTeDSL implementation of the existing CUDA mx_block_rearrange_2d_M_groups kernel, as well as validation/benchmark script that compares it against the current CUDA and Triton implementations. The production call sites are unchanged in this PR; the CUDA and Triton variants will be deactivated and deleted in a follow-up.

Benchmarking results
MxK CUDA us Triton us CuTeDSL us Triton speedup CuTeDSL speedup
131072x37 19.26 184.54 15.33 0.10 1.26
131072x45 19.36 259.23 17.31 0.07 1.12
131072x64 16.48 131.10 15.23 0.13 1.08
131072x160 28.54 127.26 25.54 0.22 1.12
131072x224 32.64 170.14 31.65 0.19 1.03
131072x320 41.66 166.91 38.88 0.25 1.07
131072x512 59.26 230.24 58.21 0.26 1.02
131072x768 83.90 262.37 82.82 0.32 1.01
131072x1024 106.46 549.66 107.36 0.19 0.99
131072x1025 204.00 400.42 162.69 0.51 1.25
131072x1536 151.58 477.09 154.62 0.32 0.98
131072x2048 198.53 539.41 203.71 0.37 0.97
131072x2049 396.21 618.46 310.18 0.64 1.28
131072x3072 290.72 771.02 299.23 0.38 0.97
131072x4096 382.90 975.97 398.30 0.39 0.96
131072x4097 770.02 1017.20 609.25 0.76 1.26
Benchmarking command to produce the table above
{
  for c in 37 45 64 160 224 320 512 768 1024 1025 1536 2048 2049 3072 4096 4097; do
    PYTHONPATH=$PWD python scripts/validate_m_groups_cutedsl.py \
      --rows 131072 --cols $c --groups 8 --multiple-of 32
  done
} 2>/dev/null | perl -ne '
BEGIN {
  print "| MxK | CUDA us | Triton us | CuTeDSL us | Triton speedup | CuTeDSL speedup |\n";
  print "|---:|---:|---:|---:|---:|---:|\n";
}
if (/shape=\((\d+),\s*(\d+)\)/) {
  $shape = "$1x$2";
  $triton_ok = undef;
  $cutedsl_ok = undef;
}
elsif (/triton_equal=(True|False)/) {
  $triton_ok = $1;
  die "triton_equal=False for $shape\n" if $triton_ok ne "True";
}
elsif (/cutedsl_equal=(True|False)/) {
  $cutedsl_ok = $1;
  die "cutedsl_equal=False for $shape\n" if $cutedsl_ok ne "True";
}
elsif (/cuda_us=([\d.]+)/) { $cuda = $1; }
elsif (/triton_us=([\d.]+)/) { $triton = $1; }
elsif (/cutedsl_us=([\d.]+)/) {
  $cutedsl = $1;
  die "missing correctness lines for $shape\n"
    unless defined($triton_ok) && defined($cutedsl_ok);
  printf "| %s | %.2f | %.2f | %.2f | %.2f | %.2f |\n",
    $shape, $cuda, $triton, $cutedsl, $cuda / $triton, $cuda / $cutedsl;
}

Edit: Some additional benchmarking results:

Benchmarking results, for M/K being powers of two
MxK CUDA us Triton us CuTeDSL us Triton speedup CuTeDSL speedup
256x32 10.05 9.79 8.96 1.03 1.12
256x64 10.11 10.40 8.96 0.97 1.13
256x128 10.11 11.81 8.96 0.86 1.13
256x256 10.11 11.01 8.99 0.92 1.12
256x512 10.11 12.29 8.99 0.82 1.12
256x1024 10.11 11.26 9.12 0.90 1.11
256x2048 10.75 14.78 10.34 0.73 1.04
256x4096 12.29 14.08 11.14 0.87 1.10
256x8192 18.30 19.42 15.20 0.94 1.20
256x16384 26.85 31.58 21.31 0.85 1.26
512x32 10.11 13.60 9.09 0.74 1.11
512x64 10.14 12.83 9.12 0.79 1.11
512x128 10.24 15.14 9.12 0.68 1.12
512x256 10.30 14.30 9.12 0.72 1.13
512x512 11.04 15.46 9.12 0.71 1.21
512x1024 12.13 14.45 9.86 0.84 1.23
512x2048 12.03 15.04 11.07 0.80 1.09
512x4096 14.13 14.34 12.35 0.99 1.14
512x8192 18.40 20.48 15.52 0.90 1.19
512x16384 29.60 33.66 23.65 0.88 1.25
1024x32 10.14 14.43 9.09 0.70 1.12
1024x64 12.35 14.05 9.09 0.88 1.36
1024x128 12.32 12.80 9.09 0.96 1.36
1024x256 12.19 13.70 9.12 0.89 1.34
1024x512 12.29 13.92 9.22 0.88 1.33
1024x1024 12.29 13.66 10.46 0.90 1.17
1024x2048 12.29 13.12 11.14 0.94 1.10
1024x4096 16.35 18.43 14.53 0.89 1.13
1024x8192 22.37 27.52 19.33 0.81 1.16
1024x16384 34.66 45.28 29.54 0.77 1.17
2048x32 12.16 13.25 9.09 0.92 1.34
2048x64 12.35 12.54 9.09 0.98 1.36
2048x128 14.02 15.04 9.12 0.93 1.54
2048x256 14.18 13.41 9.22 1.06 1.54
2048x512 14.21 12.86 10.30 1.10 1.38
2048x1024 14.34 13.44 11.14 1.07 1.29
2048x2048 16.29 16.45 13.12 0.99 1.24
2048x4096 18.43 25.54 17.28 0.72 1.07
2048x8192 27.15 39.87 25.44 0.68 1.07
2048x16384 44.96 71.65 41.15 0.63 1.09
4096x32 12.13 14.27 9.09 0.85 1.33
4096x64 13.02 14.46 9.12 0.90 1.43
4096x128 14.18 13.60 9.12 1.04 1.55
4096x256 14.21 16.03 10.18 0.89 1.40
4096x512 14.27 14.34 11.10 1.00 1.29
4096x1024 16.16 18.43 12.99 0.88 1.24
4096x2048 18.34 26.78 16.48 0.68 1.11
4096x4096 26.53 41.92 23.39 0.63 1.13
4096x8192 38.91 69.76 37.70 0.56 1.03
4096x16384 69.57 130.02 66.53 0.54 1.05
8192x32 12.16 20.51 9.09 0.59 1.34
8192x64 14.14 17.41 9.15 0.81 1.55
8192x128 14.11 26.66 9.57 0.53 1.47
8192x256 14.21 20.70 10.46 0.69 1.36
8192x512 15.04 23.33 12.10 0.64 1.24
8192x1024 18.21 28.51 15.30 0.64 1.19
8192x2048 24.48 41.25 22.43 0.59 1.09
8192x4096 37.78 69.70 35.01 0.54 1.08
8192x8192 63.39 125.82 62.43 0.50 1.02
8192x16384 114.56 236.58 115.52 0.48 0.99
16384x32 12.16 21.17 8.99 0.57 1.35
16384x64 14.21 25.38 9.25 0.56 1.54
16384x128 14.27 29.60 11.07 0.48 1.29
16384x256 14.98 33.60 12.29 0.45 1.22
16384x512 17.18 52.03 15.23 0.33 1.13
16384x1024 22.43 64.26 21.41 0.35 1.05
16384x2048 34.78 75.07 33.79 0.46 1.03
16384x4096 61.44 132.19 60.35 0.46 1.02
16384x8192 110.66 240.64 111.46 0.46 0.99
16384x16384 208.77 455.65 211.84 0.46 0.99
Benchmarking results, for M/K derived from some realistic (Mixtral, Qwen, Deepseek, Llama) models
MxK CUDA us Triton us CuTeDSL us Triton speedup CuTeDSL speedup
4096x64 13.09 13.57 9.12 0.96 1.44
4096x128 14.11 14.59 9.18 0.97 1.54
4096x160 14.05 14.08 9.31 1.00 1.51
4096x224 14.21 12.38 10.02 1.15 1.42
4096x256 14.21 13.34 9.31 1.07 1.53
4096x384 14.21 14.94 10.27 0.95 1.38
4096x512 14.24 14.37 10.50 0.99 1.36
16384x64 14.21 22.62 9.25 0.63 1.54
16384x128 14.21 27.42 10.37 0.52 1.37
16384x160 14.24 26.75 11.01 0.53 1.29
16384x224 14.40 31.58 12.16 0.46 1.18
16384x256 14.62 32.93 12.29 0.44 1.19
16384x384 16.32 33.73 14.40 0.48 1.13
16384x512 18.24 33.02 15.23 0.55 1.20
65536x64 14.37 89.38 11.36 0.16 1.26
65536x128 16.48 78.69 15.20 0.21 1.08
65536x160 18.40 74.66 17.44 0.25 1.06
65536x224 20.48 104.45 21.31 0.20 0.96
65536x256 22.40 135.30 21.31 0.17 1.05
65536x384 30.50 97.31 27.42 0.31 1.11
65536x512 33.78 196.83 33.44 0.17 1.01
131072x64 16.42 129.49 15.20 0.13 1.08
131072x128 22.37 135.33 21.31 0.17 1.05
131072x160 28.54 281.60 25.50 0.10 1.12
131072x224 32.67 149.47 31.68 0.22 1.03
131072x256 33.54 200.74 32.93 0.17 1.02
131072x384 46.94 189.34 45.12 0.25 1.04
131072x512 59.26 210.98 57.60 0.28 1.03
262144x64 22.27 370.72 20.91 0.06 1.07
262144x128 33.47 459.86 31.90 0.07 1.05
262144x160 42.88 269.18 41.86 0.16 1.02
262144x224 55.23 357.44 55.36 0.15 1.00
262144x256 59.30 426.18 57.47 0.14 1.03
262144x384 83.14 516.38 81.86 0.16 1.02
262144x512 104.32 398.34 104.54 0.26 1.00

For all benchmarking, 8 groups case is used. Summary: CuTeDSL kernel is on par or better than CUDA kernel, while Triton kernel lags behind in performance.

@alexsamardzic alexsamardzic added module: training quantize_ api training flow moe labels Jul 21, 2026
[ghstack-poisoned]
alexsamardzic added a commit that referenced this pull request Jul 21, 2026
Comment on lines +111 to +112
out_cuda = mx_block_rearrange_2d_M_groups_cuda(scales_ref, offsets)
out_triton = triton_mx_block_rearrange_2d_M_groups(scales_ref, offsets)

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.

matching these is good. Do we have a plain PyTorch reference? We should also match against that, and do that in a unit test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added test_cutedsl_mx_block_rearrange_2d_M_groups test to check against PyTorch reference.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@alexsamardzic

Copy link
Copy Markdown
Contributor Author

Replaced by #4701.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: training quantize_ api training flow moe

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants