You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Refactor: mma.cuh shall be refactored for AMD #17856
Refactor tile in mma.cuh to make a more clear logic for AMD, currently tile <I, J, T> is is used for matrix A, B and C for AMD GPU, DATA_LAYOUT_I_MAJOR means row-major for matrix A on NVIDIA, but it's col-major on AMD.
Possible Refactor Approaches
Let I refactor tile<I,J,T> for AMD WMMA first, use DATA_LAYOUT_I_MAJOR for matrix A and B and DATA_LAYOUT_J_MAJOR for C if C = A * B.
Use DATA_LAYOUT_I_MAJOR for matrix A, B and C if C = B * A for fused gemm cases.
If I understand the data layout correctly with AMD WMMA A is column-major, B is row-major, and C is column-major. On NVIDIA A is row-major, B is column-major, and C is row-major. So the layout is exactly transposed vs. NVIDIA.
Thinking about the more long-term AMD WMMA support for the MMA FlashAttention kernel, we want to organize the data in a way that minimizes data transfer for each Q column between threads. For the KQ calculation on NVIDIA K is row-major and Q is column-major which works out well. If the tensor shape allows it KQ is preferred to be a column-major C matrix to minimize synchronizations of the KQ maximum value. Also that way for the VKQ calculation KQ already has the right column-major layout as a B matrix. V as the A matrix is column-major and needs to be transposed for NVIDIA, for the VKQ accumulators the layout would preferably be the same as for KQ to make the rescaling upon encountering a new KQ maximum easier.
For AMD WMMA I think the way to implement this kernel would be to use a row-major layout for Q, a column-major layout for K, a row-major layout for KQ, a column-major layout for V, and a row-major layout for VKQ. In terms of the interface in mma.cuh my suggestion would therefore be to use a layout like LAYOUT_J_MAJOR for all matrices for the kernel in fattn-mma-f16.cuh. But for the kernels in mmq.cuh and mmf.cuh a column-major layout like you suggested is more convenient. Long-term I think we should add both and the refactor you're proposing makes sense.
This refactor is for Flash Attention, like you said, Q is row-major (m major) and K is column-major (n major), C = A * B will bring a column-major (n major) QK, just use C = B * A will bring a row-major (m major) QK.
V is column-major (n major) as matrix B, still use the same trick C = B * A will bring a row-major (m major) QKV.
Also it's useful for data loading in mmq, like I said before, all AMD matrix core non-transposed data load can be accomplished by cuda_memcpy_1.
I'm not sure where you got AMD matrix core layout, I assume they're from the ISA book.
I looked at the code on master than can be found in mma.cuh for RDNA4. But I just realized that I interpreted it incorrectly. So yes, you are right: I_MAJOR for A and B and then either I_MAJOR or J_MAJOR for C depending on the order you pass the input matrices in.
Oh, I forgot one thing, I will suggest to add a ne_scale value into tile to handle duplicated data in matrix A & B on RDNA3 as I really don't want to see the code like
#elif defined(RDNA3)
static constexpr int ne = (I == 16 && J == 16) ? I * J / 32 : I * J / 16;
#endif // defined(RDNA4)
Define the input or output by the shape on RDNA3, also don't forget that fp16 type matrix C on RDNA3 also has duplicated data contains 0.
This is much more friendly and configurable
template <int I_, int J_, typename T, data_layout ds_=DATA_LAYOUT_I_MAJOR, int NE_SCALE>
static constexpr int ne = I * J / warp_size * NE_SCALE;
I think it would be preferable to control this via the data layout enum. It's fine to add more values if it makes the implementation easier, I will take responsibility to refactor and simplify the code at the end.
Activity
zhanghuiyulo commentedon Dec 8, 2025
cc @JohannesGaessler
JohannesGaessler commentedon Dec 8, 2025
If I understand the data layout correctly with AMD WMMA A is column-major, B is row-major, and C is column-major. On NVIDIA A is row-major, B is column-major, and C is row-major. So the layout is exactly transposed vs. NVIDIA.
Thinking about the more long-term AMD WMMA support for the MMA FlashAttention kernel, we want to organize the data in a way that minimizes data transfer for each Q column between threads. For the KQ calculation on NVIDIA K is row-major and Q is column-major which works out well. If the tensor shape allows it KQ is preferred to be a column-major C matrix to minimize synchronizations of the KQ maximum value. Also that way for the VKQ calculation KQ already has the right column-major layout as a B matrix. V as the A matrix is column-major and needs to be transposed for NVIDIA, for the VKQ accumulators the layout would preferably be the same as for KQ to make the rescaling upon encountering a new KQ maximum easier.
For AMD WMMA I think the way to implement this kernel would be to use a row-major layout for Q, a column-major layout for K, a row-major layout for KQ, a column-major layout for V, and a row-major layout for VKQ. In terms of the interface in
mma.cuhmy suggestion would therefore be to use a layout likeLAYOUT_J_MAJORfor all matrices for the kernel infattn-mma-f16.cuh. But for the kernels inmmq.cuhandmmf.cuha column-major layout like you suggested is more convenient. Long-term I think we should add both and the refactor you're proposing makes sense.cc: @jiachengjason
zhanghuiyulo commentedon Dec 8, 2025
I'm not sure where you got AMD matrix core layout, I assume they're from the ISA book. Actually both RDNA and CDNA have row-major (m major) matrix A, column-major (n major) matrix B and column-major (n major) matrix C, for RDNA 3 and 4, there is a small but useful sample at https://github.com/GPUOpen-LibrariesAndSDKs/Orochi/blob/main/Test/WMMA/wmma_test_kernel.h
This refactor is for Flash Attention, like you said, Q is row-major (m major) and K is column-major (n major), C = A * B will bring a column-major (n major) QK, just use C = B * A will bring a row-major (m major) QK.
V is column-major (n major) as matrix B, still use the same trick C = B * A will bring a row-major (m major) QKV.
Also it's useful for data loading in mmq, like I said before, all AMD matrix core non-transposed data load can be accomplished by cuda_memcpy_1.
JohannesGaessler commentedon Dec 8, 2025
I looked at the code on master than can be found in
mma.cuhfor RDNA4. But I just realized that I interpreted it incorrectly. So yes, you are right:I_MAJORfor A and B and then eitherI_MAJORorJ_MAJORfor C depending on the order you pass the input matrices in.zhanghuiyulo commentedon Dec 9, 2025
Oh, I forgot one thing, I will suggest to add a ne_scale value into tile to handle duplicated data in matrix A & B on RDNA3 as I really don't want to see the code like
Define the input or output by the shape on RDNA3, also don't forget that fp16 type matrix C on RDNA3 also has duplicated data contains 0.
This is much more friendly and configurable
JohannesGaessler commentedon Dec 9, 2025
I think it would be preferable to control this via the data layout enum. It's fine to add more values if it makes the implementation easier, I will take responsibility to refactor and simplify the code at the end.