@@ -780,29 +780,31 @@ struct FlatmmKernel
780780 const SplitKBatchOffset& splitk_batch_offset,
781781 const index_t block_idx_m)
782782 {
783- constexpr int ScaleGranularityM = decltype (kargs.scale_m_ptr )::GranularityMN;
784- constexpr int ScaleGranularityKA = decltype (kargs.scale_m_ptr )::GranularityK;
783+ constexpr int GM = decltype (kargs.scale_m_ptr )::GranularityMN;
784+ constexpr int GK = decltype (kargs.scale_m_ptr )::GranularityK;
785785
786- auto scale_stride_m = ScaleGranularityM == 0 ? 0 // per-tensor scale
787- : 1 ; // per-token scale
786+ static_assert (GM != -1 ,
787+ " MakeScaleMWindow should only be instantiated when scale is enabled" );
788+
789+ // per-tensor (GM==0) -> Mdim = 1, stride 0
790+ const index_t m_dim = (GM == 0 ) ? 1 : (kargs.M / GM );
791+ const index_t m_stride = (GM == 0 ) ? 0 : 1 ;
792+
793+ const index_t k_dim = (GK == 0 ) ? 1 : (splitk_batch_offset.splitted_k / GK );
794+ const index_t k_stride = 0 ; // your original code keeps K stride 0
788795
789- // Step 1: Create tensor view
790796 const auto scale_m_view = make_naive_tensor_view<address_space_enum::global>(
791797 kargs.scale_m_ptr .ptr ,
792- make_tuple (kargs.M / ScaleGranularityM,
793- ScaleGranularityKA == 0
794- ? 1
795- : (splitk_batch_offset.splitted_k / ScaleGranularityKA)),
796- make_tuple (scale_stride_m, 0 ),
797- number < ScaleGranularityM == 1 ? FlatmmPipeline::GetVectorSizeA () : 1 > {},
798+ make_tuple (m_dim, k_dim),
799+ make_tuple (m_stride, k_stride),
800+ number < (GM == 1 ) ? FlatmmPipeline::GetVectorSizeA () : 1 > {},
798801 number<1 >{});
799802
800- // Step 2: Create tile window
803+ // Window extents: if GM==0, we still just broadcast from [0,*]
801804 return make_tile_window (scale_m_view,
802805 make_tuple (number<TilePartitioner::MPerBlock>{},
803- number < ScaleGranularityKA == 0
804- ? TilePartitioner::NPerBlock
805- : TilePartitioner::KPerBlock > {}),
806+ number < (GK == 0 ) ? TilePartitioner::NPerBlock
807+ : TilePartitioner::KPerBlock > {}),
806808 {block_idx_m, 0 });
807809 }
808810
@@ -811,27 +813,29 @@ struct FlatmmKernel
811813 const SplitKBatchOffset& splitk_batch_offset,
812814 const index_t block_idx_n)
813815 {
814- constexpr int ScaleGranularityN = decltype (kargs.scale_n_ptr )::GranularityMN;
815- constexpr int ScaleGranularityKB = decltype (kargs.scale_n_ptr )::GranularityK;
816+ constexpr int GN = decltype (kargs.scale_n_ptr )::GranularityMN;
817+ constexpr int GK = decltype (kargs.scale_n_ptr )::GranularityK;
816818
817- auto scale_stride_n = ScaleGranularityN == 0 ? 0 // per-tensor scale
818- : 1 ; // per-channel scale
819+ static_assert (GN != -1 ,
820+ " MakeScaleNWindow should only be instantiated when scale is enabled" );
821+
822+ // per-tensor (GN==0) -> Ndim = 1, stride 0
823+ const index_t n_dim = (GN == 0 ) ? 1 : (kargs.N / GN );
824+ const index_t n_stride = (GN == 0 ) ? 0 : 1 ;
825+
826+ const index_t k_dim = (GK == 0 ) ? 1 : (splitk_batch_offset.splitted_k / GK );
827+ const index_t k_stride = 0 ;
819828
820- // Step 1: Create tensor view
821829 const auto scale_n_view = make_naive_tensor_view<address_space_enum::global>(
822830 kargs.scale_n_ptr .ptr ,
823- make_tuple (
824- ScaleGranularityKB == 0 ? 1 : (splitk_batch_offset.splitted_k / ScaleGranularityKB),
825- kargs.N / ScaleGranularityN),
826- make_tuple (0 , scale_stride_n),
827- number < ScaleGranularityN == 1 ? FlatmmPipeline::GetVectorSizeB () : 1 > {},
831+ make_tuple (k_dim, n_dim),
832+ make_tuple (k_stride, n_stride),
833+ number < (GN == 1 ) ? FlatmmPipeline::GetVectorSizeB () : 1 > {},
828834 number<1 >{});
829835
830- // Step 2: Create tile window
831836 return make_tile_window (scale_n_view,
832- make_tuple (number < ScaleGranularityKB == 0
833- ? TilePartitioner::MPerBlock
834- : TilePartitioner::KPerBlock > {},
837+ make_tuple (number < (GK == 0 ) ? TilePartitioner::MPerBlock
838+ : TilePartitioner::KPerBlock > {},
835839 number<TilePartitioner::NPerBlock>{}),
836840 {0 , block_idx_n});
837841 }
@@ -854,8 +858,6 @@ struct FlatmmKernel
854858 MakeABlockWindow (a_ptr, kargs, splitk_batch_offset.splitted_k , block_idx_m);
855859 const auto & b_flat_block_window = MakeBFlatBlockWindow (b_flat_ptr, kargs, block_idx_n);
856860 const auto & ds_block_window = MakeDBlockWindows (ds_ptr, kargs, block_idx_m, block_idx_n);
857- const auto & scale_m_window = MakeScaleMWindow (kargs, splitk_batch_offset, block_idx_m);
858- const auto & scale_n_window = MakeScaleNWindow (kargs, splitk_batch_offset, block_idx_n);
859861
860862 const index_t num_loop = TilePartitioner::GetLoopNum (splitk_batch_offset.splitted_k );
861863
@@ -866,6 +868,8 @@ struct FlatmmKernel
866868 // Run Epilogue Pipeline with k_batch dispatching
867869 if constexpr (ScaleM::GranularityMN != -1 || ScaleN::GranularityMN != -1 )
868870 {
871+ const auto & scale_m_window = MakeScaleMWindow (kargs, splitk_batch_offset, block_idx_m);
872+ const auto & scale_n_window = MakeScaleNWindow (kargs, splitk_batch_offset, block_idx_n);
869873 if (kargs.k_batch == 1 )
870874 {
871875 auto e_block_window = MakeEBlockWindow<memory_operation_enum::set>(
0 commit comments