Repository navigation
[ROCm] Added CK-Tile based Group-GEMM backend for ROCm - #200307
TheRealMichaelWang wants to merge 27 commits into
Conversation
PR Status: Approved 🚀Please fix all CI failures and trigger merge by commenting "@pytorchbot merge". 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/200307
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 46841fc with merge base adc4a41 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
|
The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:
Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows. |
|
@pytorchbot label "topic: not user facing" |
jeffdaily
left a comment
There was a problem hiding this comment.
My agent came up with this analysis, but I've read and approved it.
- Odd N (column-major B in 2D/3D or 3D/3D) and jagged N (3D/2D) select
Cfg16_256x128x64_Pad, whose 2-wide C store at a row's or group's last column writes into the neighbor's first column. Commit 40f1701 fixed this and was reverted; please restore that fix.- Please restore the numerics tests reverted in 24aec87 (all 4 ops, both layouts, configs, empty groups, K=0) plus the odd-N test. The remaining test only checks the kernel name.
- With
ROCM_ALLOW_GROUP_GEMM_CK_TILE=1, fp32 inputs, >1024 groups, mixed dtypes and misaligned dims now raise instead of usinggroup_gemm_ckor_grouped_mm_fallback, and since ck_tile is checked first this also breaks fp32 users who set both flags. Gate the dispatch in GroupedBlas.cpp on a supported-inputs check and fall through otherwise. Please also add benchmark numbers against the default path.
|
Question: Why is this wanted? Is this intended to replace another backend? Add new functionality? I'm asking to make sure we actually want to add another optional backend, rather than just shift over to a new one for supported cases and slowly move out the old.. |
Hi slayton58! The addition of the CK-Tile backend for grouped GEMM is ultimately meant to supplant/replace the current CK-Tile backend. The CK-Tile backend currently outperforms the default backend on a shapes that are smaller than a certain threshold (dependent on GPU), and the eventual goal is to use a heuristic to decide when to dispatch the CK-Tile backend or not for grouped GEMM. This PR, for now, adds the CK-Tile backend as an extra backend for grouped gemm. This is a first step, just meant to get the functionality there. There will be more PRs in the future. |
|
Can we use a |
…for ATen and caffe2 to not build ck-tile group gemm for architectures that ck-tile doesn't supportt
…d CMake for ATen and caffe2 to not build ck-tile group gemm for architectures that ck-tile doesn't supportt" This reverts commit 9b6cc17.
…rrors when users use old ck-tile versions without the new tempalte overloads
3cebb7a to
32d0d1c
Compare
|
@pytorchbot merge -f "all UTs passing except Meta internal which we got approval on slack from slayton58 to try the merge again" |
Merge startedYour change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Resolve the conflict with pytorch#200307 in gemm_internal_ck<BFloat16> and gemm_internal_ck<Half>: keep main's supported_archs check and this branch's bool return from the gfx9 dispatch. Authored with Claude.
pytorch#200307 added `ck_tile_group_gemm.hip` to the `ck_gemm` target, a third place where PyTorch compiles ck_tile code. Give it the same `USE_NEW_UNIFIED_FRAMEWORK=0` define as the `ck_sdpa` target and `AsyncMM.hip`. Authored with Claude.
…p-Gemm Backend (#200640) This PR adds config structs for each input and output type and platform combination. Each config struct stores kernel configs (things like M, N, K output tile size, the MFMA M, K, N sizes, etc...) for the CK-Tile group GEMM backend. This PR builds on top of #200307 - Only configs for bf16/fp16 on the mi300 and mi350 will be added in this PR. Support for fp32 on mi300 and mi350 will be added in subsequent PRs - PR adds support for a load-cost heuristic that is used to select configs for CK-Tile - Additionally performance work was done to bring CK-Tile group gemm performance back to where it was on ROCm 7.2.0. - For more details on the 7.2.0 regression see AIPYTORCH-1471 on amd jira. - To be clear the regression was only known to happen for bf16 on the mi350. ### Linked issue or supporting maintainer Supported by @jeffdaily Also tracked internally at AMD (AIPYTORCH-1471): CK-Tile grouped GEMM lost performance on MI350 (gfx950) going from ROCm 7.2.0 to ROCm 10.1. ### Performance `F.grouped_mm` 2D x 3D over 396 MoE shapes, CK-Tile at this PR vs its merge base, and vs the default path (the per-group hipBLASLt loop); geomean speedups, median of 50, every row checked against the default path's output and gradients (measured at 9b613f0; later commits only change tests and comments): | arch | dtype | fwd vs merge base | bwd vs merge base | fwd vs default | bwd vs default | |---|---|---|---|---|---| | MI355X | bf16 | 1.21x | 1.39x | 1.06x | 0.98x | | MI355X | fp16 | 1.18x | 1.34x | 1.07x | 0.99x | | MI300X | bf16 | 1.30x | 1.02x | 1.12x | 0.85x | | MI300X | fp16 | 1.27x | 1.02x | 1.10x | 0.93x | No row is more than 4% slower than the merge base. On MI300X, backward is essentially unchanged and still trails the default path on most shapes. Pull Request resolved: #200640 Approved by: https://github.com/jeffdaily Co-authored-by: Jeff Daily <[email protected]>
…p-Gemm Backend (#200640) This PR adds config structs for each input and output type and platform combination. Each config struct stores kernel configs (things like M, N, K output tile size, the MFMA M, K, N sizes, etc...) for the CK-Tile group GEMM backend. This PR builds on top of #200307 - Only configs for bf16/fp16 on the mi300 and mi350 will be added in this PR. Support for fp32 on mi300 and mi350 will be added in subsequent PRs - PR adds support for a load-cost heuristic that is used to select configs for CK-Tile - Additionally performance work was done to bring CK-Tile group gemm performance back to where it was on ROCm 7.2.0. - For more details on the 7.2.0 regression see AIPYTORCH-1471 on amd jira. - To be clear the regression was only known to happen for bf16 on the mi350. ### Linked issue or supporting maintainer Supported by @jeffdaily Also tracked internally at AMD (AIPYTORCH-1471): CK-Tile grouped GEMM lost performance on MI350 (gfx950) going from ROCm 7.2.0 to ROCm 10.1. ### Performance `F.grouped_mm` 2D x 3D over 396 MoE shapes, CK-Tile at this PR vs its merge base, and vs the default path (the per-group hipBLASLt loop); geomean speedups, median of 50, every row checked against the default path's output and gradients (measured at 9b613f0; later commits only change tests and comments): | arch | dtype | fwd vs merge base | bwd vs merge base | fwd vs default | bwd vs default | |---|---|---|---|---|---| | MI355X | bf16 | 1.21x | 1.39x | 1.06x | 0.98x | | MI355X | fp16 | 1.18x | 1.34x | 1.07x | 0.99x | | MI300X | bf16 | 1.30x | 1.02x | 1.12x | 0.85x | | MI300X | fp16 | 1.27x | 1.02x | 1.10x | 0.93x | No row is more than 4% slower than the merge base. On MI300X, backward is essentially unchanged and still trails the default path on most shapes. @diff-train-skip-merge Pull Request resolved: #200640 Approved by: https://github.com/slayton58 Co-authored-by: Jeff Daily <[email protected]>
…p-Gemm Backend (pytorch#200640) This PR adds config structs for each input and output type and platform combination. Each config struct stores kernel configs (things like M, N, K output tile size, the MFMA M, K, N sizes, etc...) for the CK-Tile group GEMM backend. This PR builds on top of pytorch#200307 - Only configs for bf16/fp16 on the mi300 and mi350 will be added in this PR. Support for fp32 on mi300 and mi350 will be added in subsequent PRs - PR adds support for a load-cost heuristic that is used to select configs for CK-Tile - Additionally performance work was done to bring CK-Tile group gemm performance back to where it was on ROCm 7.2.0. - For more details on the 7.2.0 regression see AIPYTORCH-1471 on amd jira. - To be clear the regression was only known to happen for bf16 on the mi350. ### Linked issue or supporting maintainer Supported by @jeffdaily Also tracked internally at AMD (AIPYTORCH-1471): CK-Tile grouped GEMM lost performance on MI350 (gfx950) going from ROCm 7.2.0 to ROCm 10.1. ### Performance `F.grouped_mm` 2D x 3D over 396 MoE shapes, CK-Tile at this PR vs its merge base, and vs the default path (the per-group hipBLASLt loop); geomean speedups, median of 50, every row checked against the default path's output and gradients (measured at 9b613f0; later commits only change tests and comments): | arch | dtype | fwd vs merge base | bwd vs merge base | fwd vs default | bwd vs default | |---|---|---|---|---|---| | MI355X | bf16 | 1.21x | 1.39x | 1.06x | 0.98x | | MI355X | fp16 | 1.18x | 1.34x | 1.07x | 0.99x | | MI300X | bf16 | 1.30x | 1.02x | 1.12x | 0.85x | | MI300X | fp16 | 1.27x | 1.02x | 1.10x | 0.93x | No row is more than 4% slower than the merge base. On MI300X, backward is essentially unchanged and still trails the default path on most shapes. Pull Request resolved: pytorch#200640 Approved by: https://github.com/jeffdaily Co-authored-by: Jeff Daily <[email protected]>
…p-Gemm Backend (pytorch#200640) This PR adds config structs for each input and output type and platform combination. Each config struct stores kernel configs (things like M, N, K output tile size, the MFMA M, K, N sizes, etc...) for the CK-Tile group GEMM backend. This PR builds on top of pytorch#200307 - Only configs for bf16/fp16 on the mi300 and mi350 will be added in this PR. Support for fp32 on mi300 and mi350 will be added in subsequent PRs - PR adds support for a load-cost heuristic that is used to select configs for CK-Tile - Additionally performance work was done to bring CK-Tile group gemm performance back to where it was on ROCm 7.2.0. - For more details on the 7.2.0 regression see AIPYTORCH-1471 on amd jira. - To be clear the regression was only known to happen for bf16 on the mi350. ### Linked issue or supporting maintainer Supported by @jeffdaily Also tracked internally at AMD (AIPYTORCH-1471): CK-Tile grouped GEMM lost performance on MI350 (gfx950) going from ROCm 7.2.0 to ROCm 10.1. ### Performance `F.grouped_mm` 2D x 3D over 396 MoE shapes, CK-Tile at this PR vs its merge base, and vs the default path (the per-group hipBLASLt loop); geomean speedups, median of 50, every row checked against the default path's output and gradients (measured at 9b613f0; later commits only change tests and comments): | arch | dtype | fwd vs merge base | bwd vs merge base | fwd vs default | bwd vs default | |---|---|---|---|---|---| | MI355X | bf16 | 1.21x | 1.39x | 1.06x | 0.98x | | MI355X | fp16 | 1.18x | 1.34x | 1.07x | 0.99x | | MI300X | bf16 | 1.30x | 1.02x | 1.12x | 0.85x | | MI300X | fp16 | 1.27x | 1.02x | 1.10x | 0.93x | No row is more than 4% slower than the merge base. On MI300X, backward is essentially unchanged and still trails the default path on most shapes. @diff-train-skip-merge Pull Request resolved: pytorch#200640 Approved by: https://github.com/slayton58 Co-authored-by: Jeff Daily <[email protected]>
…p-Gemm Backend (pytorch#200640) This PR adds config structs for each input and output type and platform combination. Each config struct stores kernel configs (things like M, N, K output tile size, the MFMA M, K, N sizes, etc...) for the CK-Tile group GEMM backend. This PR builds on top of pytorch#200307 - Only configs for bf16/fp16 on the mi300 and mi350 will be added in this PR. Support for fp32 on mi300 and mi350 will be added in subsequent PRs - PR adds support for a load-cost heuristic that is used to select configs for CK-Tile - Additionally performance work was done to bring CK-Tile group gemm performance back to where it was on ROCm 7.2.0. - For more details on the 7.2.0 regression see AIPYTORCH-1471 on amd jira. - To be clear the regression was only known to happen for bf16 on the mi350. ### Linked issue or supporting maintainer Supported by @jeffdaily Also tracked internally at AMD (AIPYTORCH-1471): CK-Tile grouped GEMM lost performance on MI350 (gfx950) going from ROCm 7.2.0 to ROCm 10.1. ### Performance `F.grouped_mm` 2D x 3D over 396 MoE shapes, CK-Tile at this PR vs its merge base, and vs the default path (the per-group hipBLASLt loop); geomean speedups, median of 50, every row checked against the default path's output and gradients (measured at 9b613f0; later commits only change tests and comments): | arch | dtype | fwd vs merge base | bwd vs merge base | fwd vs default | bwd vs default | |---|---|---|---|---|---| | MI355X | bf16 | 1.21x | 1.39x | 1.06x | 0.98x | | MI355X | fp16 | 1.18x | 1.34x | 1.07x | 0.99x | | MI300X | bf16 | 1.30x | 1.02x | 1.12x | 0.85x | | MI300X | fp16 | 1.27x | 1.02x | 1.10x | 0.93x | No row is more than 4% slower than the merge base. On MI300X, backward is essentially unchanged and still trails the default path on most shapes. Pull Request resolved: pytorch#200640 Approved by: https://github.com/jeffdaily Co-authored-by: Jeff Daily <[email protected]>
…p-Gemm Backend (pytorch#200640) This PR adds config structs for each input and output type and platform combination. Each config struct stores kernel configs (things like M, N, K output tile size, the MFMA M, K, N sizes, etc...) for the CK-Tile group GEMM backend. This PR builds on top of pytorch#200307 - Only configs for bf16/fp16 on the mi300 and mi350 will be added in this PR. Support for fp32 on mi300 and mi350 will be added in subsequent PRs - PR adds support for a load-cost heuristic that is used to select configs for CK-Tile - Additionally performance work was done to bring CK-Tile group gemm performance back to where it was on ROCm 7.2.0. - For more details on the 7.2.0 regression see AIPYTORCH-1471 on amd jira. - To be clear the regression was only known to happen for bf16 on the mi350. ### Linked issue or supporting maintainer Supported by @jeffdaily Also tracked internally at AMD (AIPYTORCH-1471): CK-Tile grouped GEMM lost performance on MI350 (gfx950) going from ROCm 7.2.0 to ROCm 10.1. ### Performance `F.grouped_mm` 2D x 3D over 396 MoE shapes, CK-Tile at this PR vs its merge base, and vs the default path (the per-group hipBLASLt loop); geomean speedups, median of 50, every row checked against the default path's output and gradients (measured at 9b613f0; later commits only change tests and comments): | arch | dtype | fwd vs merge base | bwd vs merge base | fwd vs default | bwd vs default | |---|---|---|---|---|---| | MI355X | bf16 | 1.21x | 1.39x | 1.06x | 0.98x | | MI355X | fp16 | 1.18x | 1.34x | 1.07x | 0.99x | | MI300X | bf16 | 1.30x | 1.02x | 1.12x | 0.85x | | MI300X | fp16 | 1.27x | 1.02x | 1.10x | 0.93x | No row is more than 4% slower than the merge base. On MI300X, backward is essentially unchanged and still trails the default path on most shapes. @diff-train-skip-merge Pull Request resolved: pytorch#200640 Approved by: https://github.com/slayton58 Co-authored-by: Jeff Daily <[email protected]>
…llow-list (#3979) Backport of the CK GEMM single-source allow-list to release/2.13. Replaces the gfx1250 blacklist (list(REMOVE_ITEM ... gfx1250 gfx1250-strict)) with a single CMake cache variable TORCH_CK_GEMM_ARCH_LIST (default gfx90a;gfx942;gfx950) that drives the ck_gemm build arch filter (torch_filter_ck_gemm_arches) and is surfaced to C++ via a generated ATen/ROCmCKGEMMConfig.h header. release/2.13 predates the CK SDPA/GEMM gating split (upstream pytorch#187267) and had no ckGemmSupported() -- only a combined ckSupported(). This adds Context::ckGemmSupported() (parsing TORCH_CK_GEMM_ARCH_LIST via a shared parseArchList helper) and routes the CK blas-backend gate through it, leaving the CK SDPA / flash-attention gates on ckSupported(). The ck_gemm_{half,bfloat16}.hip dispatch calls at::globalContext().ckGemmSupported() instead of an isGPUArch({"gfx9"}) prefix check. TORCH_CK_GEMM_ARCH_LIST is forwarded from the environment via cmake/EnvVarForwarding.cmake. Squashed backport of upstream pytorch/pytorch pytorch#200307 (base allow-list) and pytorch#201113 (TORCH_CK_GEMM_ARCH_LIST consolidation). Once these are merged need to revert ROCm/TheRock#9414 Co-authored-by: Claude Opus 4.8 <[email protected]>
…llow-list (#3980) Backport of the CK GEMM single-source allow-list to release/2.14. Replaces the gfx1250 blacklist (list(REMOVE_ITEM ... gfx1250 gfx1250-strict)) with a single CMake cache variable TORCH_CK_GEMM_ARCH_LIST (default gfx90a;gfx942;gfx950) that drives the ck_gemm build arch filter (torch_filter_ck_gemm_arches) and is surfaced to C++ via a generated ATen/ROCmCKGEMMConfig.h header. Context::ckGemmSupported() parses that list (shared parseArchList helper) and the ck_gemm_{half,bfloat16}.hip dispatch calls at::globalContext().ckGemmSupported() instead of an isGPUArch({"gfx9"}) prefix check. TORCH_CK_GEMM_ARCH_LIST is forwarded from the environment via cmake/EnvVarForwarding.cmake. Squashed backport of upstream pytorch/pytorch pytorch#200307 (base allow-list) and pytorch#201113 (TORCH_CK_GEMM_ARCH_LIST consolidation). Once these are merged need to revert ROCm/TheRock#9414 Co-authored-by: Claude Opus 4.8 <[email protected]>
…03671) ## Linked issue or supporting maintainer Fixes #192650 Supported by @jeffdaily ## BC-breaking? No ## Summary (human written only) Bumps CK to pick up the ppc64le FastGeluAsm build fix, keeping PyTorch buildable against both the old and new CK. ## Agent details (if applicable) Problem: with ROCm 7.14's AMD clang 23 on ppc64le, the host pass of every CK SDPA translation unit fails in CK's `FastGeluAsm` (#192650). Cause: its `CK_TILE_DEVICE` overloads hold device-only AMDGPU inline assembly that the host pass still parses. Fixed upstream in ROCm/rocm-libraries#11474, which is ROCm/composable_kernel c57609f165; the current pin 5a74dec predates it. Change: - `third_party/composable_kernel` 5a74dec -> c57609f165, the fix commit itself (the oldest commit that contains it; the 11 later develop commits are gfx1250/gfx117 and CK-internal work). - Define `USE_NEW_UNIFIED_FRAMEWORK=0` for the `ck_sdpa` target, `AsyncMM.hip`, and the `ck_gemm` target (for `ck_tile_group_gemm.hip`, added by #200307). The new CK defaults it to 1 (ROCm/rocm-libraries#11646), which needs a single compile-time GPU target (`CK_CMAKE_GPU_TARGET_IDS`, first arch only) that multi-arch builds cannot give, so CK falls back to HOST in the host pass and warns in every CK SDPA TU. 0 is the old pin's default (set under `#ifndef`), so builds against older CK, such as Meta's internal one, are unchanged. - One C++ change: on device passes for targets other than gfx942/gfx950, the trap `Kernel` in `ck_tile_group_gemm.hip` is a standalone struct instead of deriving from `GroupedGemmKernel`. The new CK static_asserts against eight-wave CompV3 configs on gfx11/gfx12 targets (gfx1250 included) unless `CK_TILE_USE_WMMA` is set, and deriving instantiated the pipeline, so compiling that file for those targets failed. Otherwise the CK APIs PyTorch uses (`fmha_fwd_args`, `fmha_bwd_launcher`, ck_tile GEMM traits/epilogue) change only additively over this range, and the FMHA codegen emits the same 556 instances at both hashes. - Behaviour note: ck_tile `UniversalGemmKernel::IsSupportedArgument` now rejects operands of 2 GiB or more without `LargeTensors`, so `symm_mem::_async_input_mm` raises its existing "not supported" error for those instead of launching. Test plan, done with Claude: the ppc64le failure cannot be reproduced on x86_64. Built with `USE_ROCM_CK_SDPA=1 USE_ROCM_CK_GEMM=1` on MI300X (gfx942) and MI355X (gfx950) at the new CK hash and at 5a74dec, ran the same tests at each and compared outcomes per test id: identical at both hashes, and the CK target warning count went from 1124 to 0. ``` python -m pytest test/test_transformers.py -k ck TORCH_ROCM_FA_PREFER_CK=1 python -m pytest test/test_transformers.py -k "TestSDPAAcceleratorCUDA and (flash or mem_eff or ck)" python -m pytest test/test_linalg.py -k "ck_blas_library or grouped_mm" python -m pytest test/test_matmul_cuda.py -k grouped python -m pytest test/distributed/test_symmetric_memory.py -k native ``` Also compiled `ck_tile_group_gemm.hip` for gfx90a, gfx942, gfx950, gfx1100, gfx1201 and gfx1250 at both hashes, and built RX 7900 (gfx1100) with `USE_ROCM_CK_SDPA_GFX11=1` at both hashes with identical test outcomes. #200307 (CK-Tile grouped GEMM) applies, builds and passes its tests on top of this. Pull Request resolved: #203671 Approved by: https://github.com/jeffdaily Co-authored-by: Jeff Daily <[email protected]>
…ch#200307)" This reverts commit 31a8827. Reverted pytorch#200307 on behalf of https://github.com/jithunnair-amd due to This change needs to be guarded for the gfx archs supported by CK tile ([comment](pytorch#200307 (comment)))
Linked issue or supporting maintainer
Supported by @jeffdaily
This PR adds a minimal CK-Tile based group GEMM backend, that can be enabled via the env var flag
ROCM_ALLOW_GROUP_GEMM_CK_TILE. Note that this is the first PR in a series of more PRs that will gradually add more capability, support, performance, etc... The goal of this PR is just to get things to a working state.Currently in this implementation:
Follow-up (not in this PR): In the future add support to have _grouped_mm_cuda respect torch.backends.cuda.preferred_blas_library("ck"), using ck_tile where supported and falling back otherwise. Once the old CK grouped gemm kernel replaced/removed, remove the ROCM_ALLOW_GROUP_GEMM_CK / ROCM_ALLOW_GROUP_GEMM_CK_TILE env vars.
cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @naromero77amd @pragupta @jerrymannil @xinyazhang