@@ -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