Skip to content

Commit ec226bc

Browse files
authored
Merge pull request #95 from vladnosiv/native-block32-sm90
[SM90] Add native block32 FP8 GEMM support (dsv41)
2 parents c518ae0 + cbac37f commit ec226bc

6 files changed

Lines changed: 376 additions & 98 deletions

File tree

‎csrc/apis/gemm.hpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,17 @@ static void fp8_fp4_gemm_nt(const std::pair<torch::Tensor, torch::Tensor>& a,
199199

200200
if (arch_major == 9 and sfa.scalar_type() == torch::kFloat) {
201201
DG_HOST_ASSERT(not alpha.has_value() and "FP8 GEMM alpha requires SM100");
202+
// SM90 kernels read one activation scale per row.
203+
const int gran_m = recipe.has_value() ? std::get<0>(recipe.value()) : std::get<0>(recipe_a.value());
202204
const int gran_n = recipe.has_value() ? std::get<1>(recipe.value()) : std::get<0>(recipe_b.value());
205+
DG_HOST_ASSERT(gran_m == 1);
206+
DG_HOST_ASSERT(gran_k_a == gran_k_b);
207+
DG_HOST_ASSERT((gran_n == 32 and gran_k_a == 32) or (gran_k_a == 128 and (gran_n == 1 or gran_n == 128)));
203208
if (gran_n == 1) {
204209
sm90_fp8_gemm_1d1d(a.first, sfa, b.first, sfb, c, d, m, n, k, major_a, major_b, compiled_dims);
205210
} else {
206211
const auto major_sfb = get_major_type_ab(sfb);
207-
sm90_fp8_gemm_1d2d(a.first, sfa, b.first, sfb, c, d, m, n, k, major_a, major_b, major_sfb, compiled_dims);
212+
sm90_fp8_gemm_1d2d(a.first, sfa, b.first, sfb, c, d, m, n, k, major_a, major_b, major_sfb, compiled_dims, std::nullopt, gran_k_a);
208213
}
209214
} else if (arch_major == 10 and sfa.scalar_type() == torch::kInt) {
210215
sm100_fp8_fp4_gemm_1d1d(a.first, sfa, b.first, sfb, c, d, m, n, k, gran_k_a, gran_k_b,
@@ -319,6 +324,7 @@ static void m_grouped_fp8_fp4_gemm_nt_contiguous(const std::pair<torch::Tensor,
319324

320325
// Dispatch implementation
321326
if (arch_major == 9 and sfa.scalar_type() == torch::kFloat) {
327+
DG_HOST_ASSERT(gran_k_a == 128 and gran_k_b == 128);
322328
const auto major_sfb = get_major_type_ab(sfb);
323329
sm90_m_grouped_fp8_gemm_contiguous_1d2d(a.first, sfa, b.first, sfb, d, grouped_layout,
324330
num_groups, m, n, k, major_a, major_b, major_sfb,
@@ -393,6 +399,7 @@ static void m_grouped_fp8_fp4_gemm_nt_masked(const std::pair<torch::Tensor, torc
393399

394400
// Dispatch implementation
395401
if (arch_major == 9 and sfa.scalar_type() == torch::kFloat) {
402+
DG_HOST_ASSERT(gran_k_a == 128 and gran_k_b == 128);
396403
const auto major_sfb = get_major_type_ab(sfb);
397404
sm90_m_grouped_fp8_gemm_masked_1d2d(a.first, sfa, b.first, sfb, d, masked_m,
398405
num_groups, m, n, k, expected_m, major_a, major_b, major_sfb, compiled_dims);

‎csrc/apis/layout.hpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ static torch::Tensor transform_sf_into_required_layout(const torch::Tensor& sf,
3434
// Pre-transform checks
3535
check_sf_layout(sf, mn, k, gran_mn, gran_k, num_groups);
3636

37-
// (FP32, 1, 128) on SM90: transform to TMA-aligned and MN-major
38-
if (sf.scalar_type() == torch::kFloat and gran_mn == 1 and gran_k == 128 and (arch_major == 9 or disable_ue8m0_cast))
37+
// FP32 per-token scales on SM90: transform to TMA-aligned and MN-major
38+
if (sf.scalar_type() == torch::kFloat and gran_mn == 1 and (gran_k == 128 or gran_k == 32) and (arch_major == 9 or disable_ue8m0_cast))
3939
return get_mn_major_tma_aligned_tensor(sf);
4040

41-
// (FP32, 128, 128) on SM90: no need to transform, check SFB requirements
42-
if (sf.scalar_type() == torch::kFloat and gran_mn == 128 and gran_k == 128 and (arch_major == 9 or disable_ue8m0_cast))
41+
// FP32 block scales on SM90: no need to transform, check SFB requirements
42+
if (sf.scalar_type() == torch::kFloat and ((gran_mn == 128 and gran_k == 128) or (gran_mn == 32 and gran_k == 32)) and (arch_major == 9 or disable_ue8m0_cast))
4343
return check_sf_layout(sf, mn, k, gran_mn, gran_k, num_groups, false, true, torch::kFloat);
4444

4545
// (FP32, x, gran_k) on SM100/SM120: transform to (INT, 1, gran_k), TMA-aligned and MN-major

‎csrc/jit_kernels/heuristics/sm90.hpp‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ namespace deep_gemm {
1414

1515
struct SM90ArchSpec {
1616
static constexpr int smem_capacity = 232448;
17-
1817
static std::vector<Layout> get_layout_candidates(const GemmDesc& desc) {
18+
constexpr int short_k_stages = 8;
19+
// Block K is fixed by the MMA operand width.
20+
const int block_k = 128 * 8 / get_num_element_bits(desc.get_mma_kind());
21+
const bool block32 = desc.kernel_type == KernelType::Kernel1D2D and desc.max_gran_k == 32;
22+
const int n_tile_multiple = block32 ? desc.max_gran_k : 16;
23+
const int max_1d2d_block_n = block32 ? block_k : 192;
24+
const int short_k = short_k_stages * block_k;
25+
const int max_block_m = desc.k <= short_k ? 2 * block_k : block_k;
26+
const bool allow_small_m = not block32 or desc.k <= 2 * short_k;
27+
1928
// Block M candidates
2029
std::vector<int> block_m_candidates;
2130
if (desc.gemm_type == GemmType::Normal or
@@ -24,8 +33,8 @@ struct SM90ArchSpec {
2433
// TODO: check 256's performance
2534
block_m_candidates = {64, 128};
2635
// NOTES: smaller block M can avoid TMA L2 OOB bound
27-
if (desc.m <= 16) block_m_candidates.push_back(16);
28-
if (desc.m <= 32) block_m_candidates.push_back(32);
36+
if (desc.m <= 16 and allow_small_m) block_m_candidates.push_back(16);
37+
if (desc.m <= 32 and allow_small_m) block_m_candidates.push_back(32);
2938

3039
// BF16 output GEMM supports 256
3140
if (desc.cd_dtype != torch::kFloat)
@@ -39,7 +48,7 @@ struct SM90ArchSpec {
3948

4049
// Block N candidates
4150
std::vector<int> block_n_candidates;
42-
int step = std::lcm(16, heuristics_runtime->get_block_n_multiple_of());
51+
int step = std::lcm(n_tile_multiple, heuristics_runtime->get_block_n_multiple_of());
4352
int start = step;
4453
// Avoid bank conflicts for 1D1D kernel FP32 output
4554
if (desc.kernel_type == KernelType::Kernel1D1D and desc.cd_dtype == torch::kFloat) {
@@ -51,22 +60,19 @@ struct SM90ArchSpec {
5160
// Register spills
5261
int end = 256;
5362
if (desc.kernel_type == KernelType::Kernel1D2D)
54-
end = 192;
63+
end = max_1d2d_block_n;
5564
if (desc.kernel_type == KernelType::Kernel1D1D)
5665
end = 160;
5766
// Enumerate
5867
for (int i = start; i <= end; i += step)
5968
block_n_candidates.push_back(i);
6069

61-
// Block K is always in a fixed manner
62-
const int block_k = 128 * 8 / get_num_element_bits(desc.get_mma_kind());
63-
6470
// Disable multicast for performance
6571
const bool disable_multicast =
6672
// The number of k-groups is large (a heuristic)
6773
(desc.gemm_type == GemmType::KGroupedContiguous and desc.num_groups > 4) or
6874
// Not supported
69-
(desc.gemm_type == GemmType::Batched);
75+
(desc.gemm_type == GemmType::Batched) or block32;
7076

7177
// Enumerate all candidates
7278
std::vector<Layout> candidates;
@@ -82,6 +88,10 @@ struct SM90ArchSpec {
8288

8389
for (int block_m: block_m_candidates) {
8490
for (int block_n: block_n_candidates) {
91+
// Long K favors overlapping wide single-wave tiles; short K can
92+
// amortize B loads across two M waves without spilling.
93+
if (block32 and block_m > max_block_m)
94+
continue;
8595
// 1D2D kernel unroll requirement
8696
if (desc.kernel_type == KernelType::Kernel1D2D and block_n > block_k and (block_n % (block_n - block_k) != 0 and block_k % (block_n - block_k) != 0))
8797
continue;
@@ -149,6 +159,11 @@ struct SM90ArchSpec {
149159

150160
static PipelineConfig get_pipeline_config(const GemmDesc& desc, const Layout& layout, const StorageConfig& storage_config) {
151161
constexpr int kNumMaxStages = 16;
162+
const bool block32 = desc.kernel_type == KernelType::Kernel1D2D and desc.max_gran_k == 32;
163+
const int scale_gran_k = block32 ? desc.max_gran_k : layout.block_k;
164+
const int scales_per_stage = layout.block_k / scale_gran_k;
165+
const int scale_b_rows = block32 ? layout.block_n / scale_gran_k :
166+
(layout.block_k % layout.block_n == 0 ? 1 : 2);
152167

153168
// TODO: consider swap AB
154169
// C/D for TMA stores
@@ -163,14 +178,13 @@ struct SM90ArchSpec {
163178

164179
// Calculate SF A/B per stages
165180
const int smem_sfa_per_stage = desc.kernel_type == KernelType::KernelNoSF ?
166-
0 : align(layout.block_m * static_cast<int>(sizeof(float)), 128);
181+
0 : align(layout.block_m * scales_per_stage * static_cast<int>(sizeof(float)), 128);
167182
const int smem_sfb_per_stage = desc.kernel_type != KernelType::Kernel1D1D ?
168183
0 : align(layout.block_n * static_cast<int>(sizeof(float)), 128);
169184

170185
// Extra SFB sizes for 1D2D kernels
171-
const int use_uniform_sfb = layout.block_k % layout.block_n == 0 ? 1 : 2;
172186
const int smem_extra_sfb = desc.kernel_type != KernelType::Kernel1D2D ?
173-
0 : align<int>(ceil_div(desc.k, layout.block_k) * static_cast<int>(sizeof(float)) * use_uniform_sfb, 8);
187+
0 : align<int>(ceil_div(desc.k, scale_gran_k) * scale_b_rows * static_cast<int>(sizeof(float)), 8);
174188

175189
// Extra tensormap for 1D1D kernels
176190
const int smem_tensormap =

‎csrc/jit_kernels/impls/sm90_fp8_gemm_1d2d.hpp‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ class SM90FP8Gemm1D2DRuntime final {
2727
CUtensorMap tensor_map_b;
2828
CUtensorMap tensor_map_d;
2929
CUtensorMap tensor_map_sfa;
30+
int sf_granularity = 128;
3031
};
3132

3233
static void compile_and_launch(const std::string& tag, const Args& args) {
3334
// A known N keeps wide single-CTA SM90 tiles free of register spills.
3435
const bool specialize_n = args.gemm_desc.gemm_type == GemmType::Normal &&
3536
args.gemm_desc.compiled_dims.empty() && args.gemm_config.layout.block_m == 128 &&
3637
args.gemm_config.layout.block_n > 128 && args.gemm_config.layout.get_cluster_size() == 1;
38+
// Specialize fixed N/K to simplify scale indexing.
39+
// Keep M dynamic to avoid specialization for each token count.
40+
auto compiled_dims = args.gemm_desc.compiled_dims;
41+
if (args.sf_granularity == 32 and compiled_dims.empty())
42+
compiled_dims = "nk";
3743
const auto kernel = jit->compile(tag, std::format(R"(
3844
#include <deep_gemm/impls/sm90_fp8_gemm_1d2d.cuh>
3945
@@ -51,14 +57,14 @@ static void __instantiate_kernel() {{
5157
{}, {},
5258
{}, {},
5359
{},
54-
{}
60+
{}, {}
5561
>);
5662
}};
5763
)",
5864
to_string(args.major_sfb),
59-
get_compiled_dim(args.gemm_desc.m, 'm', args.gemm_desc.compiled_dims),
60-
get_compiled_dim(args.gemm_desc.n, 'n', specialize_n ? "n" : args.gemm_desc.compiled_dims),
61-
get_compiled_dim(args.gemm_desc.k, 'k', args.gemm_desc.compiled_dims),
65+
get_compiled_dim(args.gemm_desc.m, 'm', compiled_dims),
66+
get_compiled_dim(args.gemm_desc.n, 'n', specialize_n ? "n" : compiled_dims),
67+
get_compiled_dim(args.gemm_desc.k, 'k', compiled_dims),
6268
args.gemm_desc.num_groups,
6369
args.gemm_config.layout.block_m, args.gemm_config.layout.block_n, args.gemm_config.layout.block_k,
6470
args.gemm_config.storage_config.swizzle_a_mode, args.gemm_config.storage_config.swizzle_b_mode, args.gemm_config.storage_config.swizzle_cd_mode,
@@ -67,7 +73,7 @@ static void __instantiate_kernel() {{
6773
args.gemm_config.layout.get_cluster_size(), args.gemm_config.layout.cluster_n > 1,
6874
args.gemm_config.launch_config.num_sms, to_string(args.gemm_desc.gemm_type),
6975
to_string(args.gemm_desc.cd_dtype),
70-
get_default_epilogue_type(args.epilogue_type)));
76+
get_default_epilogue_type(args.epilogue_type), args.sf_granularity));
7177

7278
// Launch
7379
jit->launch(
@@ -87,8 +93,11 @@ static void sm90_fp8_gemm_1d2d(const torch::Tensor& a, const torch::Tensor& sfa,
8793
const int& m, const int& n, const int& k,
8894
const cute::UMMA::Major& major_a, const cute::UMMA::Major& major_b, const cute::UMMA::Major& major_sfb,
8995
const std::string& compiled_dims,
90-
const std::optional<std::string>& epilogue_type = std::nullopt) {
96+
const std::optional<std::string>& epilogue_type = std::nullopt,
97+
const int sf_granularity = 128) {
9198
DG_HOST_ASSERT(not c.has_value() and d.scalar_type() == torch::kBFloat16);
99+
DG_HOST_ASSERT(sf_granularity == 128 or sf_granularity == 32);
100+
DG_HOST_ASSERT(sf_granularity != 32 or (n % 8 == 0 and k % 32 == 0));
92101
DG_HOST_ASSERT(major_a == cute::UMMA::Major::K and major_b == cute::UMMA::Major::K);
93102

94103
const auto desc = GemmDesc {
@@ -101,7 +110,8 @@ static void sm90_fp8_gemm_1d2d(const torch::Tensor& a, const torch::Tensor& sfa,
101110
.with_accumulation = c.has_value(),
102111
.num_sms = runtime->get_num_sms(),
103112
.tc_util = runtime->get_tc_util(),
104-
.compiled_dims = compiled_dims
113+
.compiled_dims = compiled_dims,
114+
.max_gran_k = sf_granularity
105115
};
106116
const auto config = get_best_config<SM90ArchSpec>(desc);
107117

@@ -124,7 +134,7 @@ static void sm90_fp8_gemm_1d2d(const torch::Tensor& a, const torch::Tensor& sfa,
124134
static_cast<int>(d.stride(-2)), 1,
125135
config.storage_config.swizzle_cd_mode);
126136
const auto tensor_map_sfa = make_tma_sf_desc(cute::UMMA::Major::MN, sfa, m, k,
127-
config.layout.block_m, config.layout.block_k, 1, 0);
137+
config.layout.block_m, sf_granularity, 1, 0, 0, false, config.layout.block_k / sf_granularity);
128138

129139
// Compile and launch
130140
SM90FP8Gemm1D2DRuntime::compile_and_launch("sm90_fp8_gemm_1d2d", {
@@ -144,6 +154,7 @@ static void sm90_fp8_gemm_1d2d(const torch::Tensor& a, const torch::Tensor& sfa,
144154
.tensor_map_b = tensor_map_b,
145155
.tensor_map_d = tensor_map_d,
146156
.tensor_map_sfa = tensor_map_sfa,
157+
.sf_granularity = sf_granularity,
147158
});
148159
}
149160

0 commit comments

Comments
 (0)