From 89736a5054bb44e7219f0fc856ccb441a9b1cf1e Mon Sep 17 00:00:00 2001 From: Ryan Spring Date: Wed, 18 Dec 2024 17:17:51 -0800 Subject: [PATCH 01/10] change transformLikeMmaOutput --- csrc/scheduler/hopper_multi_matmul.cpp | 36 ++++++++++++++------------ csrc/scheduler/hopper_multi_matmul.h | 2 +- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/csrc/scheduler/hopper_multi_matmul.cpp b/csrc/scheduler/hopper_multi_matmul.cpp index cedc7d262d5..d9107bf9cdf 100644 --- a/csrc/scheduler/hopper_multi_matmul.cpp +++ b/csrc/scheduler/hopper_multi_matmul.cpp @@ -29,24 +29,18 @@ namespace nvfuser { -void HopperMultipleMatmulScheduler::transformLikeMmaOutput( - TensorView* tv, - bool is_mma_result) { +void HopperMultipleMatmulScheduler::transformLikeMmaOutput(TensorView* tv) { // TODO Add constraints - auto apply_k_dim_offset = [is_mma_result](int64_t idx) constexpr { - return (is_mma_result) ? idx - 1 : idx; - }; - // Original: [..., Mo, No, Mi, Ni] - tv->split(apply_k_dim_offset(-2), getM(params_->mma_macro)); - tv->split(apply_k_dim_offset(-1), getN(params_->mma_macro)); + tv->split(-2, getM(params_->mma_macro)); + tv->split(-1, getN(params_->mma_macro)); // After Split: [..., Mo, No, Mio, Mii, Nio, Nii] - tv->reorder({{apply_k_dim_offset(-3), apply_k_dim_offset(-2)}}); + tv->reorder({{-3, -2}}); // After Reorder: [..., Mo, No, Mio, Nio, Mii, Nii] - tv->merge(apply_k_dim_offset(-4)); + tv->merge(-4); // After Merge: [..., Mo, No, Mio * Nio, Mii, Nii] - tv->axis(apply_k_dim_offset(-3))->parallelize(ParallelType::TIDy); + tv->axis(-3)->parallelize(ParallelType::TIDy); // After Parallelize: [..., Mo, No, Mio * Nio (TIDy), Mii, Nii] } @@ -424,7 +418,17 @@ void HopperMultipleMatmulScheduler::scheduleMmaResults() { splitk_sums_.push_back(splitk_sum); } - transformLikeMmaOutput(mma_result, /*is_mma_result=*/true); + // Original: [..., Mo, No, Mi, Ni] + mma_result->split(-3, getM(params_->mma_macro)); + mma_result->split(-2, getN(params_->mma_macro)); + // After Split: [..., Mo, No, Mio, Mii, Nio, Nii] + mma_result->reorder({{-4, -3}}); + // After Reorder: [..., Mo, No, Mio, Nio, Mii, Nii] + mma_result->merge(-5); + // After Merge: [..., Mo, No, Mio * Nio, Mii, Nii] + mma_result->axis(-4)->parallelize(ParallelType::TIDy); + // After Parallelize: [..., Mo, No, Mio * Nio (TIDy), Mii, Nii] + auto s = mma_utils::MmaSwizzler::scheduleMmaOutputAllocation( mma_result->getLoopDomain()); mma_result->setAllocationDomain(s.as(), true); @@ -459,7 +463,7 @@ void HopperMultipleMatmulScheduler::scheduleEpilogue() { // op. blockTileTensors({d}); parallelizeBlocks({d}); - transformLikeMmaOutput(d, /*is_mma_result=*/false); + transformLikeMmaOutput(d); auto s = mma_utils::MmaSwizzler::scheduleMmaOutputAllocation( d->getLoopDomain()); @@ -536,7 +540,7 @@ void HopperMultipleMatmulScheduler::scheduleEpilogue() { blockTileTensors(tvs_to_schedule); parallelizeBlocks(tvs_to_schedule); for (auto tv : tvs_to_schedule) { - transformLikeMmaOutput(tv, /*is_mma_result=*/false); + transformLikeMmaOutput(tv); } auto s = mma_utils::MmaSwizzler::scheduleMmaOutputAllocation( @@ -570,7 +574,7 @@ void HopperMultipleMatmulScheduler::scheduleSplitKSum() { for (TensorView* splitk_sum : splitk_sums_) { // Always use serial grid reduction for split-K sum splitk_sum->definition()->as()->requestSerialGridReduction(); - transformLikeMmaOutput(splitk_sum, /*is_mma_result=*/false); + transformLikeMmaOutput(splitk_sum); auto s = mma_utils::MmaSwizzler::scheduleMmaOutputAllocation( splitk_sum->getLoopDomain()); splitk_sum->setLoopDomain(s.as()); diff --git a/csrc/scheduler/hopper_multi_matmul.h b/csrc/scheduler/hopper_multi_matmul.h index 295b55ee96e..864bbb0f3fc 100644 --- a/csrc/scheduler/hopper_multi_matmul.h +++ b/csrc/scheduler/hopper_multi_matmul.h @@ -187,7 +187,7 @@ class HopperMultipleMatmulScheduler : public MultipleMatmulScheduler { // Schedule a block-tiled TensorView like mma output. // Why? WGMMA has a unique output format. TensorViews after the mma-result in // registers must respect this format for correctness. - void transformLikeMmaOutput(TensorView* tv, bool is_mma_result); + void transformLikeMmaOutput(TensorView* tv); private: std::vector canonical_dim_ordering_; From b0682cd81b0ff15dc933504757d5c13a9f4c2f85 Mon Sep 17 00:00:00 2001 From: Ryan Spring Date: Wed, 18 Dec 2024 17:33:46 -0800 Subject: [PATCH 02/10] fix mma_result --- csrc/scheduler/hopper_multi_matmul.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/csrc/scheduler/hopper_multi_matmul.cpp b/csrc/scheduler/hopper_multi_matmul.cpp index d9107bf9cdf..0521d41a7ac 100644 --- a/csrc/scheduler/hopper_multi_matmul.cpp +++ b/csrc/scheduler/hopper_multi_matmul.cpp @@ -418,15 +418,18 @@ void HopperMultipleMatmulScheduler::scheduleMmaResults() { splitk_sums_.push_back(splitk_sum); } - // Original: [..., Mo, No, Mi, Ni] + // Original: [..., Mo, No, Mi, Ni, Ki] mma_result->split(-3, getM(params_->mma_macro)); mma_result->split(-2, getN(params_->mma_macro)); - // After Split: [..., Mo, No, Mio, Mii, Nio, Nii] - mma_result->reorder({{-4, -3}}); - // After Reorder: [..., Mo, No, Mio, Nio, Mii, Nii] - mma_result->merge(-5); + mma_result->split(-1, getK(params_->mma_macro)); + // After Split: [..., Mo, No, Mio, Mii, Nio, Nii, Kio, Kii] + mma_result->reorder({{-5, -4}}); + // After Reorder: [..., Mo, No, Mio, Nio, Mii, Nii, Kio, Kii] + mma_result->reorder({{-2, -4}}); + // After Reorder: [..., Mo, No, Mio, Nio, Kio, Mii, Nii, Kii] + mma_result->merge(-6); // After Merge: [..., Mo, No, Mio * Nio, Mii, Nii] - mma_result->axis(-4)->parallelize(ParallelType::TIDy); + mma_result->axis(-5)->parallelize(ParallelType::TIDy); // After Parallelize: [..., Mo, No, Mio * Nio (TIDy), Mii, Nii] auto s = mma_utils::MmaSwizzler::scheduleMmaOutputAllocation( From 57030711ff08fa410304e48e5684271528a61452 Mon Sep 17 00:00:00 2001 From: Ryan Spring Date: Wed, 18 Dec 2024 18:01:03 -0800 Subject: [PATCH 03/10] update HopperMatmulSchedulerTest --- tests/cpp/test_matmul_scheduler.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/cpp/test_matmul_scheduler.cpp b/tests/cpp/test_matmul_scheduler.cpp index c860b908a92..83b4434b49a 100644 --- a/tests/cpp/test_matmul_scheduler.cpp +++ b/tests/cpp/test_matmul_scheduler.cpp @@ -3273,13 +3273,13 @@ class HopperMatmulSchedulerTest // TODO cta tile is a multiple of mma macro for hopper. // Default cta_tile configuration is 2-CTA. gemm_tile.cta_tile = - GemmTile(2 * getM(mma_macro), getN(mma_macro), getK(mma_macro)); + GemmTile(2 * getM(mma_macro), getN(mma_macro), 2 * getK(mma_macro)); // TODO warp tile is (macroM, macroN, macroK) for hopper. gemm_tile.warp_tile = - GemmTile(getM(mma_macro), getN(mma_macro), getK(mma_macro)); + GemmTile(getM(mma_macro), getN(mma_macro), 2 * getK(mma_macro)); - mparams.supported_vec_size = {8, 8, 4}; + mparams.supported_vec_size = {8, 8, 8}; mparams.mma_macro = mma_macro; @@ -3467,7 +3467,7 @@ INSTANTIATE_TEST_SUITE_P( testing::Bool(), // b_k_inner testing::Values(512), // M testing::Values(256), // N - testing::Values(64), // K + testing::Values(128), // K testing::Values(MmaMacro::Hopper_64_128_16), // mma_macros testing::Values(1, 2) // SplitK Factor ), From 16594968cc28c7fbce12a1de7e249450b75af897 Mon Sep 17 00:00:00 2001 From: Ryan Spring Date: Thu, 19 Dec 2024 15:08:20 -0800 Subject: [PATCH 04/10] update diagnosis --- tests/cpp/test_matmul.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/cpp/test_matmul.cpp b/tests/cpp/test_matmul.cpp index 9e9395c5e18..f40d2e6026f 100644 --- a/tests/cpp/test_matmul.cpp +++ b/tests/cpp/test_matmul.cpp @@ -4089,6 +4089,9 @@ TEST_F(HopperMatmulTest, HSH_TN_UseScheduler) { MatmulParams mparams; mparams.supported_vec_size = {8, 8, 8}; + // TODO When B operand is transposed, cta.k == 64 and + // getM(mma_macro) != getN(mma_macro), there is incorrect results with + // stmatrix. mparams.mma_macro = MmaMacro::Hopper_64_256_16; mparams.tile_sizes = gemm_tile; mparams.cta_order = MatmulParams::TileRasterizationOrder::ColumnMajor; @@ -4152,6 +4155,9 @@ TEST_F(HopperMatmulTest, HSH_NN_UseScheduler) { MatmulParams mparams; mparams.supported_vec_size = {8, 8, 8}; + // TODO When B operand is transposed, cta.k == 64 and + // getM(mma_macro) != getN(mma_macro), there is incorrect results with + // stmatrix. mparams.mma_macro = MmaMacro::Hopper_64_256_16; mparams.tile_sizes = gemm_tile; mparams.cta_order = MatmulParams::TileRasterizationOrder::ColumnMajor; From 88ceaf136eb542c2b7f2f1756d994619830e1463 Mon Sep 17 00:00:00 2001 From: Ryan Spring Date: Thu, 19 Dec 2024 22:31:27 -0800 Subject: [PATCH 05/10] Use block sync to sync main loop in pipeline tma --- csrc/device_lower/utils.cpp | 4 ++++ tests/cpp/test_matmul.cpp | 22 ++++++++-------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/csrc/device_lower/utils.cpp b/csrc/device_lower/utils.cpp index 35b825d5348..077e757349e 100644 --- a/csrc/device_lower/utils.cpp +++ b/csrc/device_lower/utils.cpp @@ -1995,6 +1995,10 @@ std::vector getSyncExprs(AsyncOpType async_type, int64_t keep_stages) { sync_exprs.push_back(commit); auto wait = IrBuilder::create(async_type, keep_stages); sync_exprs.push_back(wait); + if (async_type == AsyncOpType::WgMma) { + auto sync = IrBuilder::create(true); + sync_exprs.push_back(sync); + } return sync_exprs; } diff --git a/tests/cpp/test_matmul.cpp b/tests/cpp/test_matmul.cpp index f40d2e6026f..a0dbeb87777 100644 --- a/tests/cpp/test_matmul.cpp +++ b/tests/cpp/test_matmul.cpp @@ -4027,8 +4027,8 @@ TEST_F(HopperMatmulTest, HSH_NT_UseScheduler) { auto out_ref = at::matmul(a_ref.squeeze().t(), b_ref.squeeze()).to(at::kHalf); MatMulTileOptions gemm_tile; - gemm_tile.cta_tile = GemmTile(128, 256, 16); - gemm_tile.warp_tile = GemmTile(64, 256, 16); + gemm_tile.cta_tile = GemmTile(128, 256, 64); + gemm_tile.warp_tile = GemmTile(64, 256, 64); MatmulParams mparams; mparams.supported_vec_size = {8, 8, 8}; @@ -4084,14 +4084,11 @@ TEST_F(HopperMatmulTest, HSH_TN_UseScheduler) { auto out_ref = at::matmul(a_ref.squeeze(), b_ref.squeeze().t()).to(at::kHalf); MatMulTileOptions gemm_tile; - gemm_tile.cta_tile = GemmTile(128, 256, 16); - gemm_tile.warp_tile = GemmTile(64, 256, 16); + gemm_tile.cta_tile = GemmTile(128, 256, 64); + gemm_tile.warp_tile = GemmTile(64, 256, 64); MatmulParams mparams; mparams.supported_vec_size = {8, 8, 8}; - // TODO When B operand is transposed, cta.k == 64 and - // getM(mma_macro) != getN(mma_macro), there is incorrect results with - // stmatrix. mparams.mma_macro = MmaMacro::Hopper_64_256_16; mparams.tile_sizes = gemm_tile; mparams.cta_order = MatmulParams::TileRasterizationOrder::ColumnMajor; @@ -4150,14 +4147,11 @@ TEST_F(HopperMatmulTest, HSH_NN_UseScheduler) { at::matmul(a_ref.squeeze().t(), b_ref.squeeze().t()).to(at::kHalf); MatMulTileOptions gemm_tile; - gemm_tile.cta_tile = GemmTile(128, 256, 16); - gemm_tile.warp_tile = GemmTile(64, 256, 16); + gemm_tile.cta_tile = GemmTile(128, 256, 64); + gemm_tile.warp_tile = GemmTile(64, 256, 64); MatmulParams mparams; mparams.supported_vec_size = {8, 8, 8}; - // TODO When B operand is transposed, cta.k == 64 and - // getM(mma_macro) != getN(mma_macro), there is incorrect results with - // stmatrix. mparams.mma_macro = MmaMacro::Hopper_64_256_16; mparams.tile_sizes = gemm_tile; mparams.cta_order = MatmulParams::TileRasterizationOrder::ColumnMajor; @@ -4215,8 +4209,8 @@ TEST_F(HopperMatmulTest, HSH_TT_UseScheduler) { auto out_ref = at::matmul(a_ref.squeeze(), b_ref.squeeze()).to(at::kHalf); MatMulTileOptions gemm_tile; - gemm_tile.cta_tile = GemmTile(128, 256, 16); - gemm_tile.warp_tile = GemmTile(64, 256, 16); + gemm_tile.cta_tile = GemmTile(128, 256, 64); + gemm_tile.warp_tile = GemmTile(64, 256, 64); MatmulParams mparams; mparams.supported_vec_size = {8, 8, 8}; From fca0086fb590f967d4f4398dc66f0913e98ee9f7 Mon Sep 17 00:00:00 2001 From: Ryan Spring Date: Thu, 19 Dec 2024 22:57:51 -0800 Subject: [PATCH 06/10] add default warp specialization --- csrc/device_lower/pass/allocation.cpp | 2 +- csrc/device_lower/pass/insert_syncs.cpp | 2 +- csrc/scheduler/hopper_multi_matmul.cpp | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/csrc/device_lower/pass/allocation.cpp b/csrc/device_lower/pass/allocation.cpp index bee61c46873..1242ddc794b 100644 --- a/csrc/device_lower/pass/allocation.cpp +++ b/csrc/device_lower/pass/allocation.cpp @@ -601,7 +601,7 @@ class AllocationInserter : public kir::ExprMutator { // generic-async proxy fence and wgmma fence before each mma // instruction. For this case, we need to insert these fences // after the initialization of the accumulator, so that the - // inilization is visible to the async proxy. + // initialization is visible to the async proxy. // When all inputs are guarded by mbarrier, we will insert these // fences before each mma instruction, so there is no need to // insert them after the initialization of the accumulator here. diff --git a/csrc/device_lower/pass/insert_syncs.cpp b/csrc/device_lower/pass/insert_syncs.cpp index 4e2f55323be..a0ccd8f5f56 100644 --- a/csrc/device_lower/pass/insert_syncs.cpp +++ b/csrc/device_lower/pass/insert_syncs.cpp @@ -782,7 +782,7 @@ class ReadAfterWriteSyncs : public kir::ExprMutator { } }; -// Insert wait expressions for WAR harzard for async operations such as wgmma +// Insert wait expressions for WAR hazard for async operations such as wgmma // and tma store. To do so, we find the structure like the following example: // for 1 // for 2 diff --git a/csrc/scheduler/hopper_multi_matmul.cpp b/csrc/scheduler/hopper_multi_matmul.cpp index 0521d41a7ac..ac9a476a508 100644 --- a/csrc/scheduler/hopper_multi_matmul.cpp +++ b/csrc/scheduler/hopper_multi_matmul.cpp @@ -633,7 +633,8 @@ void HopperMultipleMatmulScheduler::setUpCircularBuffering() { /*prefetch_distance=*/ params_->circular_buffer_options.smem_circular_buffer_stage - params_->circular_buffer_options - .smem_circular_buffer_prefetch_gap); + .smem_circular_buffer_prefetch_gap, + WarpSpecialized(ParallelType::TIDy)); } for (TensorView* bcw_smem : bcw_smems_) { bcw_smem->circularBuffer( @@ -641,7 +642,8 @@ void HopperMultipleMatmulScheduler::setUpCircularBuffering() { /*prefetch_distance=*/ params_->circular_buffer_options.smem_circular_buffer_stage - params_->circular_buffer_options - .smem_circular_buffer_prefetch_gap); + .smem_circular_buffer_prefetch_gap, + WarpSpecialized(ParallelType::TIDy)); } } From 99f492ac2b323c8ab5fbd9585c604c30003ea94d Mon Sep 17 00:00:00 2001 From: Ryan Spring Date: Fri, 20 Dec 2024 12:21:36 -0800 Subject: [PATCH 07/10] create MaxNReg and Return kir nodes --- csrc/codegen.cpp | 4 +++ csrc/device_lower/pass/index.cpp | 10 ++++++ csrc/device_lower/pass/index.h | 2 ++ csrc/device_lower/pass/inline_ptx.cpp | 13 ++++++++ csrc/dispatch.h | 2 ++ csrc/kernel_ir.cpp | 41 ++++++++++++++++++++++++ csrc/kernel_ir.h | 46 +++++++++++++++++++++++++++ 7 files changed, 118 insertions(+) diff --git a/csrc/codegen.cpp b/csrc/codegen.cpp index 0060e626fe6..34abf9f8058 100644 --- a/csrc/codegen.cpp +++ b/csrc/codegen.cpp @@ -3505,6 +3505,10 @@ class CudaKernelGenerator : private kir::ConstIrVisitor { indent() << "NVFUSER_UPDATE_MAGIC_ZERO;\n"; } + void handle(const kir::Return* ret) final { + indent() << "return;\n"; + } + private: std::stringstream code_; const kir::Kernel* kernel_; diff --git a/csrc/device_lower/pass/index.cpp b/csrc/device_lower/pass/index.cpp index f4121021f3b..32b8ba3938b 100644 --- a/csrc/device_lower/pass/index.cpp +++ b/csrc/device_lower/pass/index.cpp @@ -2583,6 +2583,16 @@ void IndexLowering::handle(const kir::WgMmaFence* fence) { pushBack(const_cast(fence)); // NOLINT } +void IndexLowering::handle(const kir::MaxNReg* maxnreg) { + // TODO(kir): remove the need for const_cast + pushBack(const_cast(maxnreg)); // NOLINT +} + +void IndexLowering::handle(const kir::Return* ret) { + // TODO(kir): remove the need for const_cast + pushBack(const_cast(ret)); // NOLINT +} + void IndexLowering::handle(const kir::AsyncCommit* commit) { // TODO(kir): remove the need for const_cast pushBack(const_cast(commit)); // NOLINT diff --git a/csrc/device_lower/pass/index.h b/csrc/device_lower/pass/index.h index 8d206159128..03e281dc868 100644 --- a/csrc/device_lower/pass/index.h +++ b/csrc/device_lower/pass/index.h @@ -75,6 +75,8 @@ class IndexLowering : private OptOutConstDispatch { void handle(const kir::GridSync*) final; void handle(const kir::FenceAsyncProxy*) final; void handle(const kir::WgMmaFence*) final; + void handle(const kir::MaxNReg*) final; + void handle(const kir::Return*) final; void handle(const kir::MBarrierInit*) final; void handle(const kir::MBarrierInvalidate*) final; void handle(const kir::MBarrierArrive*) final; diff --git a/csrc/device_lower/pass/inline_ptx.cpp b/csrc/device_lower/pass/inline_ptx.cpp index 31afc58a775..3a99ecda938 100644 --- a/csrc/device_lower/pass/inline_ptx.cpp +++ b/csrc/device_lower/pass/inline_ptx.cpp @@ -272,6 +272,19 @@ class LowerToInlinePtx : public kir::ExprMutator { std::vector{}, kir::Asm::Options{/*volatile=*/true})); } + + void handle(kir::MaxNReg* maxnreg) final { + std::string ptx = (maxnreg->increaseRegisters()) + ? "setmaxnreg.inc.sync.aligned.u32" + : "setmaxnreg.dec.sync.aligned.u32"; + registerReplace( + maxnreg, + IrBuilder::create( + ptx, + std::vector{}, + std::vector{maxnreg->numberOfRegisters()}, + kir::Asm::Options{/*volatile=*/true})); + } }; std::vector lowerToInlinePtx(const std::vector& exprs) { diff --git a/csrc/dispatch.h b/csrc/dispatch.h index 4fe0f86cc5f..7f7fce03e4e 100644 --- a/csrc/dispatch.h +++ b/csrc/dispatch.h @@ -120,6 +120,8 @@ class Val; f(GridSync); \ f(FenceAsyncProxy); \ f(WgMmaFence); \ + f(MaxNReg); \ + f(Return); \ f(MBarrierInit); \ f(MBarrierInvalidate); \ f(MBarrierArrive); \ diff --git a/csrc/kernel_ir.cpp b/csrc/kernel_ir.cpp index fc464eac315..b04297583ca 100644 --- a/csrc/kernel_ir.cpp +++ b/csrc/kernel_ir.cpp @@ -485,6 +485,47 @@ std::string WgMmaFence::toInlineString(int indent_size) const { NVFUSER_DEFINE_CLONE_AND_CREATE(WgMmaFence) +MaxNReg::MaxNReg( + IrBuilderPasskey passkey, + Val* number_of_registers, + bool increase_registers) + : Expr(passkey) { + NVF_ERROR(passkey.ir_container_ != nullptr); + NVF_ERROR( + passkey.ir_container_->isA(), + "IR type only valid for Kernel container."); + addInput(number_of_registers); + addDataAttribute(increase_registers); +} + +std::string MaxNReg::toString(int indent_size) const { + return (increaseRegisters()) ? "setmaxnreg.inc.sync.aligned.u32" + : "setmaxnreg.dec.sync.aligned.u32"; +} + +std::string MaxNReg::toInlineString(int indent_size) const { + NVF_CHECK(false, "MaxNReg can not be printed inline"); +} + +NVFUSER_DEFINE_CLONE_AND_CREATE(MaxNReg) + +Return::Return(IrBuilderPasskey passkey) : Expr(passkey) { + NVF_ERROR(passkey.ir_container_ != nullptr); + NVF_ERROR( + passkey.ir_container_->isA(), + "IR type only valid for Kernel container."); +} + +std::string Return::toString(int indent_size) const { + return "return"; +} + +std::string Return::toInlineString(int indent_size) const { + NVF_CHECK(false, "Return can not be printed inline"); +} + +NVFUSER_DEFINE_CLONE_AND_CREATE(Return) + MBarrierInit::MBarrierInit( IrBuilderPasskey passkey, Val* mbarrier, diff --git a/csrc/kernel_ir.h b/csrc/kernel_ir.h index 60421db1995..cf3e50ed8e6 100644 --- a/csrc/kernel_ir.h +++ b/csrc/kernel_ir.h @@ -41,6 +41,8 @@ class BlockSync; class GridSync; class FenceAsyncProxy; class WgMmaFence; +class MaxNReg; +class Return; class MBarrierInit; class MBarrierInvalidate; class MBarrierArrive; @@ -469,6 +471,50 @@ class WgMmaFence final : public Expr { std::string toInlineString(int indent_size = 0) const override; }; +// PTX: setmaxnreg.inc.sync.aligned.u32 and setmaxnreg.dec.sync.aligned.u32 +class MaxNReg final : public Expr { + public: + using Expr::Expr; + + explicit MaxNReg( + IrBuilderPasskey passkey, + Val* number_of_registers, + bool increase_registers); + + NVFUSER_DECLARE_CLONE_AND_CREATE + + const char* getOpString() const override { + return (increaseRegisters()) ? "IncMaxNReg" : "DecMaxNReg"; + } + + std::string toString(int indent_size = 0) const override; + std::string toInlineString(int indent_size = 0) const override; + + bool increaseRegisters() const { + return attribute(0); + } + + Val* numberOfRegisters() const { + return input(0); + } +}; + +class Return final : public Expr { + public: + using Expr::Expr; + + explicit Return(IrBuilderPasskey passkey); + + NVFUSER_DECLARE_CLONE_AND_CREATE + + const char* getOpString() const override { + return "Return"; + } + + std::string toString(int indent_size = 0) const override; + std::string toInlineString(int indent_size = 0) const override; +}; + class MBarrierInit final : public Expr { public: using Expr::Expr; From 0c784e02363164115a13d81bb03d01b879c2f653 Mon Sep 17 00:00:00 2001 From: Ryan Spring Date: Fri, 20 Dec 2024 15:25:01 -0800 Subject: [PATCH 08/10] make blocksync compatible with warp specialization --- csrc/codegen.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/csrc/codegen.cpp b/csrc/codegen.cpp index 34abf9f8058..c7972951f4f 100644 --- a/csrc/codegen.cpp +++ b/csrc/codegen.cpp @@ -3325,10 +3325,16 @@ class CudaKernelGenerator : private kir::ConstIrVisitor { // Use a custom synchronization method if enabled if (getNvFuserEnv("USE_BLOCK_SYNC_ATOMIC")) { indent() << "block_sync::sync();\n"; - } else if (isAligned()) { - indent() << "__syncthreads();\n"; } else { - indent() << "__barrier_sync(0);\n"; + ArgumentBuilder sync_call_template_parms; + sync_call_template_parms.arg(isAligned()); + + ArgumentBuilder sync_call_args; + sync_call_args.arg(genComputeBlockDim()); + + auto sync_call = + genCall("block_sync::sync", sync_call_template_parms, sync_call_args); + indent() << sync_call << ";\n"; } } From 9288baca1c1c935a3809569b5ef1eb30eca2a590 Mon Sep 17 00:00:00 2001 From: Ryan Spring Date: Fri, 20 Dec 2024 15:32:07 -0800 Subject: [PATCH 09/10] Initial warp specialization --- csrc/codegen.cpp | 2 ++ csrc/device_lower/pass/circular_buffer.cpp | 13 +++++++++++++ csrc/device_lower/pass/insert_syncs.cpp | 19 +++++++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/csrc/codegen.cpp b/csrc/codegen.cpp index c7972951f4f..e6b927f351c 100644 --- a/csrc/codegen.cpp +++ b/csrc/codegen.cpp @@ -274,6 +274,8 @@ class CudaKernelGenerator : private kir::ConstIrVisitor { // Generates the kernel function declaration void genDeclaration(const std::string& kernel_name) { code_ << "__global__ void "; + // TODO Fix hardcoded values + code_ << "__launch_bounds__(384, 1) "; if (kernel_->hasManaged("cluster_dims")) { auto cluster_dims = kernel_->getManaged>( diff --git a/csrc/device_lower/pass/circular_buffer.cpp b/csrc/device_lower/pass/circular_buffer.cpp index 15ed808b936..3b0f5dd7e34 100644 --- a/csrc/device_lower/pass/circular_buffer.cpp +++ b/csrc/device_lower/pass/circular_buffer.cpp @@ -1394,11 +1394,24 @@ class CircularBufferInserter : private kir::ExprMutator { warp_specialize_on), circular_buffer_loop->fusion()->oneVal())))); + kir::MaxNReg* dec_reg_load_warp = IrBuilder::create( + IrBuilder::create(24, DataType::Index), + /*increase_registers=*/false); + warp_dispatch_ite->thenBody().push_back(dec_reg_load_warp); + // Load loop: ForLoop* load_loop = CloneTmaCircularBufferLoopAndInsertSync::clone( circular_buffer_loop, loads, CircularBufferLoopStage::LoadWarp); warp_dispatch_ite->thenBody().push_back(load_loop); + kir::Return* ret = IrBuilder::create(); + warp_dispatch_ite->thenBody().push_back(ret); + + kir::MaxNReg* inc_reg_load_warp = IrBuilder::create( + IrBuilder::create(240, DataType::Index), + /*increase_registers*/ true); + warp_dispatch_ite->elseBody().push_back(inc_reg_load_warp); + // Prefetch: auto prefetch_loop = createArrivesForWar(circular_buffer_loop); warp_dispatch_ite->elseBody().push_back(prefetch_loop); diff --git a/csrc/device_lower/pass/insert_syncs.cpp b/csrc/device_lower/pass/insert_syncs.cpp index a0ccd8f5f56..5ced19589e5 100644 --- a/csrc/device_lower/pass/insert_syncs.cpp +++ b/csrc/device_lower/pass/insert_syncs.cpp @@ -393,11 +393,11 @@ class ReadAfterWriteSyncs : public kir::ExprMutator { if (auto mma = dynamic_cast(expr)) { if (mma->isHopper()) { auto scope = scope_.empty() ? nullptr : scope_.back(); + auto wgmma_fence = IrBuilder::create(); + registerInsertBefore(expr, wgmma_fence, scope); if (!lower_utils::allMmaInputsGuardedByMBarrier(mma)) { // Makes sure that writes to operands in the generic proxy are visible // to the async proxy - auto wgmma_fence = IrBuilder::create(); - registerInsertBefore(expr, wgmma_fence, scope); auto fence_async = IrBuilder::create(); registerInsertBefore(expr, fence_async, scope); } @@ -1007,6 +1007,21 @@ class WarAsyncWaitInserter : private kir::ExprMutator { // Process the expressions in the for loop kir::ExprMutator::handle(for_loop); + // NOTE Warp Specialization require WAR wgmma sync before launching next tma + // load + if (for_loop->circularBufferLoopStage() == + CircularBufferLoopStage::ComputeWarp) { + for (Expr* expr : for_loop->body().exprs()) { + if (expr->isA()) { + auto sync_exprs = lower_utils::getSyncExprs(AsyncOpType::WgMma, 0); + while (!sync_exprs.empty()) { + registerInsertBefore(expr, sync_exprs.back(), &for_loop->body()); + sync_exprs.pop_back(); + } + } + } + } + // Insert async wait at the end of this for loop if (within_iter_loop_) { std::unordered_map types_and_pending_ops_to_protect; From 202e1bf7e755c8e2d92a50f447f2a80489940eeb Mon Sep 17 00:00:00 2001 From: Ryan Spring Date: Sun, 22 Dec 2024 12:25:31 -0800 Subject: [PATCH 10/10] add todo --- csrc/device_lower/utils.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/csrc/device_lower/utils.cpp b/csrc/device_lower/utils.cpp index 077e757349e..4a56b814d72 100644 --- a/csrc/device_lower/utils.cpp +++ b/csrc/device_lower/utils.cpp @@ -1995,6 +1995,7 @@ std::vector getSyncExprs(AsyncOpType async_type, int64_t keep_stages) { sync_exprs.push_back(commit); auto wait = IrBuilder::create(async_type, keep_stages); sync_exprs.push_back(wait); + // TODO Do not apply for warp specialization if (async_type == AsyncOpType::WgMma) { auto sync = IrBuilder::create(true); sync_exprs.push_back(sync);