[LLVMGPU] Re-land FP8 mma.sync transpose shape fix (#25015) Re-lands #24676. #24676 was reverted by #25012 after a rebase exposed an LIT failure. The failure was caused by an incorrect FileCheck pattern in the newly added FP8 test. This PR keeps the compiler fix and corrects the FileCheck expectation. ## Testing - `pipeline_tile_and_fuse_mma_sync.mlir` LIT/FileCheck test passes locally. --------- Signed-off-by: weimin023 <tnwilly@gmail.com>
diff --git a/compiler/src/iree/compiler/Codegen/Dialect/GPU/IR/IREEGPUAttrs.cpp b/compiler/src/iree/compiler/Codegen/Dialect/GPU/IR/IREEGPUAttrs.cpp index 0bc2968..d92c69b 100644 --- a/compiler/src/iree/compiler/Codegen/Dialect/GPU/IR/IREEGPUAttrs.cpp +++ b/compiler/src/iree/compiler/Codegen/Dialect/GPU/IR/IREEGPUAttrs.cpp
@@ -1259,14 +1259,16 @@ .getResult(); } if (isNvMmaSync(intrinsic)) { - // Transpose the two outer dimensions to model the column-major register - // ordering expected by mma.sync. The input shape differs between pipelines: - // VectorDistribute produces 2x2x1x2, TileAndFuse produces 2x1x2x2. - // Remove the unit dimension to simplify the transpose. - // Preserve the operand element type so BF16 fragments are not reshaped as - // f16. + // Transpose outer M/K dims for mma.sync's column-major register order. + // Derive the non-unit shape from the LHS layout because element[K] varies + // by intrinsic. Type elementType = cast<VectorType>(lhs.getType()).getElementType(); - auto nonUnitVecType = VectorType::get({2, 2, 2}, elementType); + auto lhsLayout = getSingleSubgroupLayout(intrinsic, kMMAOperandLhs); + int64_t outerM = lhsLayout.outer[0]; + int64_t outerK = lhsLayout.outer[1]; + int64_t elemSize = lhsLayout.element[0] * lhsLayout.element[1]; + auto nonUnitVecType = + VectorType::get({outerM, outerK, elemSize}, elementType); auto reshaped = vector::ShapeCastOp::create(builder, loc, nonUnitVecType, lhs); auto permAttr = builder.getDenseI64ArrayAttr({1, 0, 2});
diff --git a/compiler/src/iree/compiler/Codegen/LLVMGPU/test/NVVM/pipeline_tile_and_fuse_mma_sync.mlir b/compiler/src/iree/compiler/Codegen/LLVMGPU/test/NVVM/pipeline_tile_and_fuse_mma_sync.mlir index 99fa509..8d79799 100644 --- a/compiler/src/iree/compiler/Codegen/LLVMGPU/test/NVVM/pipeline_tile_and_fuse_mma_sync.mlir +++ b/compiler/src/iree/compiler/Codegen/LLVMGPU/test/NVVM/pipeline_tile_and_fuse_mma_sync.mlir
@@ -139,3 +139,50 @@ // Verify nvgpu.mma.sync is generated with correct shape for BF16 // CHECK-COUNT-8: nvgpu.mma.sync({{.*}}) mmaShape = [16, 8, 16] : ({{.*}}) -> vector<2x2xf32> // CHECK: scf.yield + +// ----- + +// Test FP8 E4M3FN m16n8k32 matmul lowering to nvgpu.mma.sync. +// FP8 uses reduction = 4 and element[K] = 4. + +#executable_target_cuda_fp8 = #hal.executable.target<"cuda", "cuda-nvptx-fb"> +#pipeline_layout_fp8 = #hal.pipeline.layout<bindings = [ + #hal.pipeline.binding<storage_buffer>, + #hal.pipeline.binding<storage_buffer>, + #hal.pipeline.binding<storage_buffer> +]> +#config_fp8e4m3fn = #iree_gpu.lowering_config<{ + workgroup = [64, 64, 0], + reduction = [0, 0, 4], + subgroup = [2, 4], + mma_kind = #iree_gpu.mma_layout<NV_MMA_SYNC_F32_16x8x32_F8E4M3FN>, + promote_operands = [0, 1] +}> +func.func @matmul_tile_and_fuse_mma_sync_fp8e4m3fn() + attributes { + hal.executable.target = #executable_target_cuda_fp8, + translation_info = #iree_codegen.translation_info<pipeline = #iree_gpu.pipeline<TileAndFuse> workgroup_size = [128, 1, 1] subgroup_size = 32>} { + %cst = arith.constant 0.000000e+00 : f32 + %c0 = arith.constant 0 : index + %0 = hal.interface.binding.subspan layout(#pipeline_layout_fp8) binding(0) alignment(64) offset(%c0) flags(ReadOnly) : !iree_tensor_ext.dispatch.tensor<readonly:tensor<2048x1280xf8E4M3FN>> + %1 = hal.interface.binding.subspan layout(#pipeline_layout_fp8) binding(1) alignment(64) offset(%c0) flags(ReadOnly) : !iree_tensor_ext.dispatch.tensor<readonly:tensor<1280x10240xf8E4M3FN>> + %2 = hal.interface.binding.subspan layout(#pipeline_layout_fp8) binding(2) alignment(64) offset(%c0) : !iree_tensor_ext.dispatch.tensor<writeonly:tensor<2048x10240xf32>> + %3 = iree_tensor_ext.dispatch.tensor.load %0, offsets = [0, 0], sizes = [2048, 1280], strides = [1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<2048x1280xf8E4M3FN>> -> tensor<2048x1280xf8E4M3FN> + %4 = iree_tensor_ext.dispatch.tensor.load %1, offsets = [0, 0], sizes = [1280, 10240], strides = [1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<1280x10240xf8E4M3FN>> -> tensor<1280x10240xf8E4M3FN> + %5 = tensor.empty() : tensor<2048x10240xf32> + %6 = linalg.fill ins(%cst : f32) outs(%5 : tensor<2048x10240xf32>) -> tensor<2048x10240xf32> + %7 = linalg.matmul {lowering_config = #config_fp8e4m3fn} ins(%3, %4 : tensor<2048x1280xf8E4M3FN>, tensor<1280x10240xf8E4M3FN>) outs(%6 : tensor<2048x10240xf32>) -> tensor<2048x10240xf32> + iree_tensor_ext.dispatch.tensor.store %7, %2, offsets = [0, 0], sizes = [2048, 10240], strides = [1, 1] : tensor<2048x10240xf32> -> !iree_tensor_ext.dispatch.tensor<writeonly:tensor<2048x10240xf32>> + return +} + +// CHECK-LABEL: func @matmul_tile_and_fuse_mma_sync_fp8e4m3fn +// CHECK-DAG: memref.alloc() : memref<{{.*}}xf8E4M3FN, #gpu.address_space<workgroup>> +// CHECK-DAG: memref.alloc() : memref<{{.*}}xf8E4M3FN, #gpu.address_space<workgroup>> +// CHECK: scf.for +// Verify the FP8 LHS mma.sync transpose and m16n8k32 lowering. +// CHECK: vector.shape_cast {{.*}} : vector<2x1x2x4xf8E4M3FN> to vector<2x2x4xf8E4M3FN> +// CHECK: vector.transpose {{.*}}, [1, 0, 2] : vector<2x2x4xf8E4M3FN> to vector<2x2x4xf8E4M3FN> +// CHECK: vector.shape_cast {{.*}} : vector<2x2x4xf8E4M3FN> to vector<4x4xf8E4M3FN> +// CHECK-COUNT-8: nvgpu.mma.sync({{.*}}) mmaShape = [16, 8, 32] : ({{.*}}) -> vector<2x2xf32> +// CHECK: scf.yield