Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement persistent matmul scheduling #3812

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
851669a
Split Hopper MMA by warp-tile before instruction tile
jacobhinkle Dec 24, 2024
8b42cd6
Use 4 warpgroups, disable smem epilogue
jacobhinkle Dec 31, 2024
7c6d417
Merge branch 'main' into hopper_warptile_split
jacobhinkle Dec 31, 2024
521d5cc
Use warp_tile for tma_m and tma_n
jacobhinkle Dec 31, 2024
dce16ad
Two warp tiles per CTA in each dim, increase instr to 64_64_16
jacobhinkle Jan 2, 2025
f5e084c
Also split by K
jacobhinkle Jan 2, 2025
be705bf
Add ScheduleWithTranslation test (failing)
jacobhinkle Jan 7, 2025
9de3202
Merge remote-tracking branch 'origin/main' into hopper_warptile_split
jacobhinkle Jan 8, 2025
41e2b94
Merge remote-tracking branch 'origin/main' into hopper_warptile_split
jacobhinkle Jan 17, 2025
e010ead
Merge remote-tracking branch 'origin/main' into hopper_warptile_split
jacobhinkle Jan 28, 2025
5246fb3
Update to fix compilation
jacobhinkle Jan 28, 2025
1dccf22
Don't do K split. Fix TMA offset
jacobhinkle Jan 28, 2025
496d8d7
Merge remote-tracking branch 'origin/main' into hopper_warptile_split
jacobhinkle Jan 29, 2025
dfa6ff8
Add options for warp specialization and persistence strategy
jacobhinkle Jan 29, 2025
21c508d
Temporarily revert change to scheduleStMatrixForMmaOutput
jacobhinkle Jan 29, 2025
174deda
Parametrize MLP Benchmark tests to run three configurations
jacobhinkle Jan 29, 2025
7868900
Unguard most matmul node translation tests on Hopper
jacobhinkle Jan 29, 2025
9b5e73c
lintrunner
jacobhinkle Jan 29, 2025
21a2710
Apply suggestions from code review
jacobhinkle Jan 29, 2025
f1fff43
Reparametrize and place a big comment explaining
jacobhinkle Jan 30, 2025
a3b8fd4
Update python bindings
jacobhinkle Jan 31, 2025
bfd65f3
Add more checks for valid configs
jacobhinkle Jan 31, 2025
694e0fe
Set warp specialization as default on hopper
jacobhinkle Jan 31, 2025
794285b
Merge remote-tracking branch 'origin/main' into jh/persistent_kernel_…
jacobhinkle Jan 31, 2025
95cf199
Guard MLPBenchmarkTest to Hopper only
jacobhinkle Jan 31, 2025
ffa276e
Merge remote-tracking branch 'origin/hopper_warptile_split' into jh/p…
jacobhinkle Jan 31, 2025
86d75de
Merge in from #3642. Add persistent change
jacobhinkle Jan 31, 2025
6d98405
Add BroadcastInputs tests
jacobhinkle Feb 3, 2025
438e1a0
Remove debug prints
jacobhinkle Feb 3, 2025
68c07a0
Merge remote-tracking branch 'origin/main' into jh/persistent_kernel_…
jacobhinkle Feb 3, 2025
4d0226c
Fix block parallelization
jacobhinkle Feb 3, 2025
07c93c6
Override params for horizontal fusion tests
jacobhinkle Feb 6, 2025
37a7282
Merge commit '9dc94c0' into jh/persistent_kernel_impl
jacobhinkle Feb 6, 2025
74751b3
Merge commit '3ac19f0' into jh/persistent_kernel_impl
jacobhinkle Feb 6, 2025
2527dc0
Merge commit 'a1baafa' into jh/persistent_kernel_impl
jacobhinkle Feb 6, 2025
e4486c8
Merge remote-tracking branch 'origin/main' into jh/persistent_kernel_…
jacobhinkle Feb 6, 2025
b0359a2
Merge remote-tracking branch 'origin/main' into jh/persistent_kernel_…
jacobhinkle Feb 6, 2025
7f161bc
Uncomment correctness checks in tests
jacobhinkle Feb 6, 2025
0cbb3e6
Guard failing MLPBenchmarkTest cases on Ampere
jacobhinkle Feb 6, 2025
dec223e
Merge remote-tracking branch 'origin/main' into jh/persistent_kernel_…
jacobhinkle Feb 11, 2025
913ba63
Don't do register sharing for OneTilePerCTA
jacobhinkle Feb 11, 2025
4ef0339
Merge remote-tracking branch 'origin/main' into jh/persistent_kernel_…
jacobhinkle Feb 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 64 additions & 33 deletions csrc/scheduler/hopper_multi_matmul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <scheduler/debug_utils.h>
#include <scheduler/hopper_multi_matmul.h>
#include <scheduler/matmul.h>
#include <scheduler/matmul_heuristic.h>
#include <scheduler/matmul_utils.h>
#include <scheduler/mma_utils.h>
#include <scheduler/tools/abstract_tensor.h>
Expand Down Expand Up @@ -114,11 +115,6 @@ void HopperMultipleMatmulScheduler::validate() const {
"Hopper matmul scheduler does not support scheduling persistent split-K kernels");
}

NVF_CHECK(
params_->tiling_strategy !=
MatmulParams::TilingStrategy::DistributeTilesAcrossSMs,
"Hopper matmul scheduler TEMPORARILY does not support persistent scheduling of tiles yet");

NVF_CHECK(
params_->tiling_strategy !=
MatmulParams::TilingStrategy::DistributeStagesAcrossSMs,
Expand Down Expand Up @@ -383,6 +379,21 @@ std::vector<std::vector<MatmulDimRole>> HopperMultipleMatmulScheduler::
}
}
}

if (params_->tiling_strategy ==
MatmulParams::TilingStrategy::DistributeTilesAcrossSMs) {
// Persistent kernel scheduling
if (params_->cta_order ==
MatmulParams::TileRasterizationOrder::ColumnMajor) {
tv->reorder(
{{num_device_and_batch_dims_, num_device_and_batch_dims_ + 1}});
}
tv->merge(num_device_and_batch_dims_, num_device_and_batch_dims_ + 1);

const int64_t num_sms =
at::cuda::getCurrentDeviceProperties()->multiProcessorCount;
tv->split(num_device_and_batch_dims_, num_sms);
}
}
return all_merged_roles;
}
Expand Down Expand Up @@ -427,21 +438,35 @@ void HopperMultipleMatmulScheduler::scheduleOperands() {
void HopperMultipleMatmulScheduler::parallelizeBlocks(
const std::vector<TensorView*>& tvs) const {
for (TensorView* tv : tvs) {
switch (params_->cta_order) {
// TODO: Should we instead check the roles of these dimensions to take the
// outermost two M or N axes?
case MatmulParams::TileRasterizationOrder::RowMajor:
tv->axis(num_device_and_batch_dims_)->parallelize(ParallelType::BIDx);
tv->axis(num_device_and_batch_dims_ + 1)
->parallelize(ParallelType::BIDy);
switch (params_->tiling_strategy) {
case MatmulParams::TilingStrategy::OneTilePerCTA:
// Data-parallel kernels are parallelized BIDx BIDy
switch (params_->cta_order) {
// TODO: Should we instead check the roles of these dimensions to take
// the outermost two M or N axes?
case MatmulParams::TileRasterizationOrder::RowMajor:
tv->axis(num_device_and_batch_dims_)
->parallelize(ParallelType::BIDx);
tv->axis(num_device_and_batch_dims_ + 1)
->parallelize(ParallelType::BIDy);
break;
case MatmulParams::TileRasterizationOrder::ColumnMajor:
tv->axis(num_device_and_batch_dims_)
->parallelize(ParallelType::BIDy);
tv->axis(num_device_and_batch_dims_ + 1)
->parallelize(ParallelType::BIDx);
break;
default:
NVF_THROW(
"Invalid TileRasterizationOrder passed to Matmul scheduler");
}
break;
case MatmulParams::TileRasterizationOrder::ColumnMajor:
tv->axis(num_device_and_batch_dims_)->parallelize(ParallelType::BIDy);
case MatmulParams::TilingStrategy::DistributeTilesAcrossSMs:
case MatmulParams::TilingStrategy::DistributeStagesAcrossSMs:
// For persistent kernels, we just parallelize the SM dimension
tv->axis(num_device_and_batch_dims_ + 1)
->parallelize(ParallelType::BIDx);
break;
default:
NVF_THROW("Invalid TileRasterizationOrder passed to Matmul scheduler");
}
}
}
Expand Down Expand Up @@ -722,23 +747,29 @@ void HopperMultipleMatmulScheduler::setUpCircularBuffering() {
break;
}
case MatmulParams::CircularBufferingStrategy::WarpSpecialized: {
NVF_ERROR(
std::all_of(
mma_results_.begin(),
mma_results_.end(),
[](TensorView* tv) {
IterDomain* ws_axis = tv->axis(-7);
return ws_axis->getParallelType() == ParallelType::TIDy &&
ws_axis->extent()->evaluate().as<int64_t>() <= 2;
}),
"There can be at most two compute warp groups for register ",
"sharing with warp specialization");
constexpr int64_t num_registers_load_warp = 40;
constexpr int64_t num_registers_compute_warp = 232;
cb_type = (CircularBufferType)WarpSpecialized(
ParallelType::TIDy,
std::make_pair(
num_registers_load_warp, num_registers_compute_warp));
if (params_->tiling_strategy ==
MatmulParams::TilingStrategy::OneTilePerCTA) {
Comment on lines +750 to +751
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@rdspring1 I had to add this condition because of this:

NVF_ERROR(
tv->getLoopDomain().at(axis)->isThread() ||
tv->getLoopDomain().at(axis)->isDeviceDim() ||
tv->getLoopDomain().at(axis)->isBroadcast() ||
tv->getLoopDomain().at(axis)->isOneInt(),
"When using register sharing with warp-specialized circular "
"buffering, the circular buffer loop must be the outer-most "
"for-loop.");

Commenting out that check leads nvrtc to hang :-D. This means we cannot use register sharing for persistent kernels yet. Do you think this might work if we lift the warp specialization predicate to the top level (i.e. outside of the persistent loop) during lowering?

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you merge cta-M, cta-N, cta-K together into an iterdomain, couldn't you circular buffer this loop?

e.g., Implement a general persistent stream-k first, then add boundary conditions so you don't have mma tiles split over multiple SMs. Then, you wouldn't need special lowering for persistent matmul and stream-k matmul.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't there a return in the LoadWarp? NVRTC is likely lost with this kernel.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, you're right. There is a return in that if, so it's not a valid persistent kernel, but ptxas compiles it without warnings about register sharing. And actually I had that wrong, the kernel compiles but it deadlocks when the dma warps return too early. If I comment out the insertion of the kir::Return the kernel succeeds. So maybe we should just not insert the return if we detect an outer non-trivial loop?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The kernel will run without the return but performance tanks. The compiler will not optimize anything with register sharing enabled.

NVF_ERROR(
std::all_of(
mma_results_.begin(),
mma_results_.end(),
[](TensorView* tv) {
IterDomain* ws_axis = tv->axis(-7);
return ws_axis->getParallelType() == ParallelType::TIDy &&
ws_axis->extent()->evaluate().as<int64_t>() <= 2;
}),
"There can be at most two compute warp groups for register ",
"sharing with warp specialization");
constexpr int64_t num_registers_load_warp = 40;
constexpr int64_t num_registers_compute_warp = 232;
cb_type = (CircularBufferType)WarpSpecialized(
ParallelType::TIDy,
std::make_pair(
num_registers_load_warp, num_registers_compute_warp));
} else {
// Persistent kernels cannot yet use register sharing
cb_type = (CircularBufferType)WarpSpecialized(ParallelType::TIDy);
}
break;
}
}
Expand Down
4 changes: 4 additions & 0 deletions csrc/scheduler/matmul_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ bool fillDefaultHopperHeuristic(

mparams->tile_sizes = {cta_tile, warp_tile};

// Use warp specialization on hopper by default
Copy link
Collaborator

Choose a reason for hiding this comment

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

I thought using warp specialization by default was causing some test failures.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not anymore. I think that was before integrating the warp tile split.

mparams->circular_buffering_strategy =
MatmulParams::CircularBufferingStrategy::WarpSpecialized;

// stages and async mem copy
mparams->circular_buffer_options.smem_circular_buffer_stage = 8;

Expand Down
Loading