gridwise_gemm_xdlops_bwd_weight.hpp Source File

gridwise_gemm_xdlops_bwd_weight.hpp Source File#

Composable Kernel: gridwise_gemm_xdlops_bwd_weight.hpp Source File
gridwise_gemm_xdlops_bwd_weight.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2018-2025, Advanced Micro Devices, Inc. All rights reserved.
3
4#pragma once
5
17
18namespace ck {
19
20// Implementation of "Merge" transformation primitive that uses division and mod. It is supposed to
21// be used for low_lengths that are known at compile time and are power of 2, otherwise performance
22// will be very bad
23template <typename LowLengths>
25{
26 static constexpr index_t NDimLow = LowLengths::Size();
27
30
33
34 using UpLengths =
35 decltype(make_tuple(container_reduce(LowLengths{}, math::multiplies{}, Number<1>{})));
36
37 LowLengths low_lengths_;
40
41 __host__ __device__ constexpr Merge_v4_no_carry() = default;
42
43 __host__ __device__ constexpr Merge_v4_no_carry(const LowLengths& low_lengths)
44 : low_lengths_{low_lengths},
46 container_reverse_exclusive_scan(low_lengths, math::multiplies{}, Number<1>{})},
47 up_lengths_{make_tuple(container_reduce(low_lengths, math::multiplies{}, Number<1>{}))}
48 {
49 static_assert(LowerIndex::Size() == NDimLow, "wrong!");
50 }
51
52 __host__ __device__ static constexpr index_t GetNumOfLowerDimension() { return NDimLow; }
53
54 __host__ __device__ static constexpr index_t GetNumOfUpperDimension() { return 1; }
55
56 __host__ __device__ constexpr const auto& GetUpperLengths() const { return up_lengths_; }
57
58 template <typename LowIdx, typename UpIdx>
59 __host__ __device__ constexpr void CalculateLowerIndex(LowIdx& idx_low,
60 const UpIdx& idx_up) const
61 {
62 static_assert(LowIdx::Size() == NDimLow && UpIdx::Size() == 1,
63 "wrong! inconsistent # of dimension");
64
65 index_t tmp = idx_up[Number<0>{}];
66
67 // division and mod
68 static_for<0, NDimLow - 1, 1>{}([&](auto i) {
69 idx_low(i) = tmp / this->low_lengths_scan_[i];
70 tmp %= this->low_lengths_scan_[i];
71 });
72
73 idx_low(Number<NDimLow - 1>{}) = tmp;
74 }
75
76 template <typename LowIdxDiff,
77 typename UpIdxDiff,
78 typename LowIdx,
79 typename UpIdx,
80 index_t Hack>
81 __host__ __device__ void UpdateLowerIndex(LowIdxDiff& idx_diff_low,
82 const UpIdxDiff& idx_up_diff,
83 LowIdx& idx_low,
84 const UpIdx& idx_up_new,
85 Number<Hack>) const
86 {
87 static_assert(LowIdxDiff::Size() == NDimLow && UpIdxDiff::Size() == 1 &&
88 LowIdx::Size() == NDimLow && UpIdx::Size() == 1,
89 "wrong! inconsistent # of dimension");
90
91 constexpr auto I0 = Number<0>{};
92 constexpr auto INm1 = Number<NDimLow - 1>{};
93
94 index_t tmp = idx_up_new[I0];
95
96 idx_low(INm1) = tmp;
97 idx_diff_low(INm1) = idx_up_diff[I0];
98 }
99
100 __host__ __device__ static constexpr bool IsLinearTransform() { return false; }
101
102 __host__ __device__ static constexpr bool IsValidUpperIndexAlwaysMappedToValidLowerIndex()
103 {
104 return true;
105 }
106
113
114 template <typename UpIdx>
115 __host__ __device__ static constexpr bool
117 {
118 return true;
119 }
120
121 __host__ __device__ void Print() const
122 {
123 printf("{");
124 printf("Merge_v3_direct_division_mod_wrw, ");
125 printf("low_lengths_ ");
127 printf("low_lengths_scan_ ");
129 printf("up_lengths_ ");
131 printf("}");
132 }
133};
134
135template <typename LowLengths>
136__host__ __device__ constexpr auto make_merge_transform_v4_no_carry(const LowLengths& low_lengths)
137{
138 return Merge_v4_no_carry<LowLengths>{low_lengths};
139}
140
141template <typename GridwiseGemm,
142 typename FloatA,
143 typename FloatB,
144 typename FloatC,
145 typename AGridDesc_B_K0_M_K1,
146 typename BGridDesc_B_K0_N_K1,
147 typename CGridDesc_MBlock_MPerBlock_NBlock_NPerBlock,
148 typename AElementwiseOperation,
149 typename BElementwiseOperation,
150 typename CElementwiseOperation,
151 typename CBlockClusterAdaptor,
152 bool HasMainKBlockLoop>
153__global__ void
154#if CK_USE_LAUNCH_BOUNDS
156#endif
157 kernel_gemm_xdlops_bwd_weight(const FloatA* __restrict__ p_a_grid,
158 const FloatB* __restrict__ p_b_grid,
159 FloatC* __restrict__ p_c_grid,
160 const AGridDesc_B_K0_M_K1 a_b_k0_m_k1_grid_desc,
161 const BGridDesc_B_K0_N_K1 b_b_k0_n_k1_grid_desc,
162 const CGridDesc_MBlock_MPerBlock_NBlock_NPerBlock
163 c_grid_desc_mblock_mperblock_nblock_nperblock,
164 const AElementwiseOperation a_element_op,
165 const BElementwiseOperation b_element_op,
166 const CElementwiseOperation c_element_op,
167 const CBlockClusterAdaptor c_block_cluster_adaptor)
168{
169#if defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx94__) || defined(__gfx11__) || \
170 defined(__gfx12__)
171 if constexpr(GridwiseGemm::template IsValidCompilationParameter<>())
172 {
173 __shared__ char p_shared[GridwiseGemm::GetSharedMemoryNumberOfByte()];
174
175 GridwiseGemm::template Run<HasMainKBlockLoop>(p_a_grid,
176 p_b_grid,
177 p_c_grid,
178 p_shared,
179 a_b_k0_m_k1_grid_desc,
180 b_b_k0_n_k1_grid_desc,
181 c_grid_desc_mblock_mperblock_nblock_nperblock,
182 a_element_op,
183 b_element_op,
184 c_element_op,
185 c_block_cluster_adaptor);
186 }
187#else
188 ignore = p_a_grid;
189 ignore = p_b_grid;
190 ignore = p_c_grid;
191 ignore = a_b_k0_m_k1_grid_desc;
192 ignore = b_b_k0_n_k1_grid_desc;
193 ignore = c_grid_desc_mblock_mperblock_nblock_nperblock;
194 ignore = a_element_op;
195 ignore = b_element_op;
196 ignore = c_element_op;
197 ignore = c_block_cluster_adaptor;
198#endif // end of if (defined(__gfx908__) || defined(__gfx90a__))
199}
200
201template <index_t BlockSize,
202 typename FloatA,
203 typename FloatB,
204 typename FloatAcc,
205 typename FloatC,
206 InMemoryDataOperationEnum CGlobalMemoryDataOperation,
207 typename AGridDesc_B_K0_M_K1,
208 typename BGridDesc_B_K0_N_K1,
209 typename CMNGridDesc,
210 typename AElementwiseOperation,
211 typename BElementwiseOperation,
212 typename CElementwiseOperation,
213 index_t MPerBlock,
214 index_t NPerBlock,
215 index_t K0PerBlock,
216 index_t MPerXdl,
217 index_t NPerXdl,
218 index_t K1Value,
219 index_t MRepeat,
220 index_t NRepeat,
221 typename ABlockTransferThreadClusterLengths_K0_M_K1,
222 typename ABlockTransferThreadClusterArrangeOrder,
223 typename ABlockTransferSrcAccessOrder,
224 index_t ABlockTransferSrcVectorDim,
225 index_t ABlockTransferSrcScalarPerVector,
226 index_t ABlockTransferDstScalarPerVector_K1,
227 bool AThreadTransferSrcResetCoordinateAfterRun,
228 bool ABlockLdsExtraM,
229 index_t ABlockLdsM1PerBlock,
230 index_t ABlockLdsM0PerBlock,
231 index_t ABlockLdsM1Padding,
232 typename BBlockTransferThreadClusterLengths_K0_N_K1,
233 typename BBlockTransferThreadClusterArrangeOrder,
234 typename BBlockTransferSrcAccessOrder,
235 index_t BBlockTransferSrcVectorDim,
236 index_t BBlockTransferSrcScalarPerVector,
237 index_t BBlockTransferDstScalarPerVector_K1,
238 bool BThreadTransferSrcResetCoordinateAfterRun,
239 bool BBlockLdsExtraN,
240 index_t BBlockLdsN1PerBlock,
241 index_t BBlockLdsN0PerBlock,
242 index_t BBlockLdsN1Padding,
243 index_t CShuffleMRepeatPerShuffle,
244 index_t CShuffleNRepeatPerShuffle,
245 index_t CBlockTransferScalarPerVector_NWaveNPerXDL,
246 typename CBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock,
247 bool ABlockLdsExtraM1Wrw = false,
248 bool BBlockLdsExtraN1Wrw = false,
249 index_t NumGemmKPrefetchStage = 1,
250 PipelineVersion PipelineVer = PipelineVersion::v1,
251 typename ComputeTypeA = FloatA,
252 typename ComputeTypeB = ComputeTypeA>
254{
255 static constexpr auto I0 = Number<0>{};
256 static constexpr auto I1 = Number<1>{};
257 static constexpr auto I2 = Number<2>{};
258 static constexpr auto I3 = Number<3>{};
259 static constexpr auto I4 = Number<4>{};
260 static constexpr auto I5 = Number<5>{};
261 static constexpr auto I6 = Number<6>{};
262 static constexpr auto I7 = Number<7>{};
263
264 // K1 should be Number<...>
265 static constexpr auto K1 = Number<K1Value>{};
266
268
271
272 // denorm test fix, required to work around fp16 mfma issue
273 // we convert fp16->fp32->bf16 and execute bf16 mfma instruction
274 // when mfma if fixed, remove this section and update
275 // FloatAAdjusted -> ComputeTypeA, FloatBAdjusted -> ComputeTypeB,
276 // throughout this file
277#if CK_GFX90A_DENORM_WORKAROUND
278 using FloatAAdjusted =
280 using FloatBAdjusted =
282#else
285#endif
286
287 // M0/M1/M1Padding
288 static constexpr auto M1PerBlock = Number<ABlockLdsM1PerBlock>{};
289 static constexpr auto M0PerBlock = Number<ABlockLdsM0PerBlock>{};
290 static constexpr auto M1Padding = Number<ABlockLdsM1Padding>{};
291
292 // N0/N1/N1Padding
293 static constexpr auto N1PerBlock = Number<BBlockLdsN1PerBlock>{};
294 static constexpr auto N0PerBlock = Number<BBlockLdsN0PerBlock>{};
295 static constexpr auto N1Padding = Number<BBlockLdsN1Padding>{};
296
297 __host__ __device__ static constexpr auto GetABlockDescriptor_K0PerBlock_MPerBlock_K1()
298 {
299 constexpr auto max_lds_align = K1;
300
301 // A matrix in LDS memory, dst of blockwise copy
302 constexpr auto a_block_desc_k0_m_k1 = [&]() {
303 if constexpr(ABlockLdsExtraM)
304 {
305 if constexpr(ABlockLdsExtraM1Wrw)
306 {
307 constexpr auto a_block_desc_k0_m0_m1_k1 = make_naive_tensor_descriptor(
312 K1,
313 I1));
314
315 constexpr auto a_block_desc_k0_m_k1_tmp = transform_tensor_descriptor(
316 a_block_desc_k0_m0_m1_k1,
323
324 return a_block_desc_k0_m_k1_tmp;
325 }
326 else
327 {
331 }
332 }
333 else
334 {
336 make_tuple(Number<K0PerBlock>{}, Number<MPerBlock>{}, K1), max_lds_align);
337 }
338 }();
339
340 return a_block_desc_k0_m_k1;
341 }
342
343 __host__ __device__ static constexpr auto GetABlockDescriptor_Batch_K0PerBlock_MPerBlock_K1()
344 {
345 constexpr auto max_lds_align = K1;
346
347 // A matrix in LDS memory, dst of blockwise copy
348 constexpr auto a_block_desc_b_k0_m_k1 = [&]() {
349 if constexpr(ABlockLdsExtraM)
350 {
351 if constexpr(ABlockLdsExtraM1Wrw)
352 {
353 constexpr auto a_block_desc_b_k0_m0_m1_k1 = make_naive_tensor_descriptor(
358 K1),
363 K1,
364 I1));
365
366 constexpr auto a_block_desc_b_k0_m_k1_tmp = transform_tensor_descriptor(
367 a_block_desc_b_k0_m0_m1_k1,
375
376 return a_block_desc_b_k0_m_k1_tmp;
377 }
378 else
379 {
384 K1,
385 I1));
386 }
387 }
388 else
389 {
392 max_lds_align);
393 }
394 }();
395
396 return a_block_desc_b_k0_m_k1;
397 }
398
399 __host__ __device__ static constexpr auto GetBBlockDescriptor_K0PerBlock_NPerBlock_K1()
400 {
401 constexpr auto max_lds_align = K1;
402
403 // B matrix in LDS memory, dst of blockwise copy
404 constexpr auto b_block_desc_k0_n_k1 = [&]() {
405 if constexpr(BBlockLdsExtraN)
406 {
407 if constexpr(BBlockLdsExtraN1Wrw)
408 {
409 constexpr auto b_block_desc_k0_n0_n1_k1 = make_naive_tensor_descriptor(
414 K1,
415 I1));
416
417 constexpr auto b_block_desc_k0_n_k1_tmp = transform_tensor_descriptor(
418 b_block_desc_k0_n0_n1_k1,
425
426 return b_block_desc_k0_n_k1_tmp;
427 }
428 else
429 {
433 }
434 }
435 else
436 {
438 make_tuple(Number<K0PerBlock>{}, Number<NPerBlock>{}, K1), max_lds_align);
439 }
440 }();
441
442 return b_block_desc_k0_n_k1;
443 }
444
445 __host__ __device__ static constexpr auto GetBBlockDescriptor_Batch_K0PerBlock_NPerBlock_K1()
446 {
447 constexpr auto max_lds_align = K1;
448
449 // B matrix in LDS memory, dst of blockwise copy
450 constexpr auto b_block_desc_b_k0_n_k1 = [&]() {
451 if constexpr(BBlockLdsExtraN)
452 {
453 if constexpr(BBlockLdsExtraN1Wrw)
454 {
455 constexpr auto b_block_desc_b_k0_n0_n1_k1 = make_naive_tensor_descriptor(
460 K1),
465 K1,
466 I1));
467
468 constexpr auto b_block_desc_b_k0_n_k1_tmp = transform_tensor_descriptor(
469 b_block_desc_b_k0_n0_n1_k1,
477
478 return b_block_desc_b_k0_n_k1_tmp;
479 }
480 else
481 {
486 K1,
487 I1));
488 }
489 }
490 else
491 {
494 max_lds_align);
495 }
496 }();
497
498 return b_block_desc_b_k0_n_k1;
499 }
500
501 __host__ __device__ static constexpr index_t GetSharedMemoryNumberOfByte()
502 {
503 constexpr auto max_lds_align = K1;
504
505 // A matrix in LDS memory, dst of blockwise copy
506 constexpr auto a_b_k0_m_k1_block_desc = GetABlockDescriptor_Batch_K0PerBlock_MPerBlock_K1();
507
508 // B matrix in LDS memory, dst of blockwise copy
509 constexpr auto b_b_k0_n_k1_block_desc = GetBBlockDescriptor_Batch_K0PerBlock_NPerBlock_K1();
510
511 // LDS allocation for A and B: be careful of alignment
512 constexpr auto a_block_space_size = math::integer_least_multiple(
513 a_b_k0_m_k1_block_desc.GetElementSpaceSize(), max_lds_align);
514
515 constexpr auto b_block_space_size = math::integer_least_multiple(
516 b_b_k0_n_k1_block_desc.GetElementSpaceSize(), max_lds_align);
517
518 constexpr auto c_block_size =
520
521 return math::max((a_block_space_size * sizeof(FloatAAdjusted) +
522 b_block_space_size * sizeof(FloatBAdjusted)),
523 c_block_size * sizeof(FloatC));
524 }
525
526 template <
527 InMemoryDataOperationEnum CGlobalMemoryDataOperation_ = InMemoryDataOperationEnum::Set>
528 __device__ static bool constexpr IsValidCompilationParameter()
529 {
530 return ck::tensor_operation::device::IsValidGemmCompilationParameter<
531 BlockSize,
532 MPerBlock,
533 NPerBlock,
534 MPerXdl,
535 NPerXdl,
536 MRepeat,
537 NRepeat,
538 FloatC,
539 CGlobalMemoryDataOperation>();
540 }
541 // block_id to matrix tile idx (m0, n0) mapping are controlled by {M01, N01}
542 template <typename Block2CTileMap>
543 __host__ __device__ static constexpr bool
544 CheckValidity(const AGridDesc_B_K0_M_K1& a_b_k0_m_k1_grid_desc,
545 const BGridDesc_B_K0_N_K1& b_b_k0_n_k1_grid_desc,
546 const CMNGridDesc& c_m_n_grid_desc,
547 const Block2CTileMap& block_2_ctile_map)
548 {
549 static_assert(is_known_at_compile_time<remove_cv_t<decltype(K1)>>::value,
550 "wrong! K1 need to be known at compile-time");
551
552 static_assert((MPerBlock % (MPerXdl * MRepeat) == 0) &&
553 (NPerBlock % (NRepeat * NPerXdl)) == 0,
554 "Invalid tuning param!");
555
556 const auto M = a_b_k0_m_k1_grid_desc.GetLength(I2);
557 const auto N = b_b_k0_n_k1_grid_desc.GetLength(I2);
558 const auto K0 = a_b_k0_m_k1_grid_desc.GetLength(I1);
559 const auto KBatch = a_b_k0_m_k1_grid_desc.GetLength(I0);
560
561 // check gridwise gemm pipeline
562 const auto num_k_loop = K0 / K0PerBlock;
563
564 if(!GridwiseGemmPipe::IsSupported(num_k_loop))
565 {
566 return false;
567 }
568
569 if(!(M == c_m_n_grid_desc.GetLength(I0) && N == c_m_n_grid_desc.GetLength(I1) &&
570 K0 == b_b_k0_n_k1_grid_desc.GetLength(I1) &&
571 K1 == a_b_k0_m_k1_grid_desc.GetLength(I3) &&
572 K1 == b_b_k0_n_k1_grid_desc.GetLength(I3) &&
573 KBatch == b_b_k0_n_k1_grid_desc.GetLength(I0)))
574 return false;
575
576 if(!(M % MPerBlock == 0 && N % NPerBlock == 0 && K0 % K0PerBlock == 0))
577 return false;
578
579 if(!block_2_ctile_map.CheckValidity(c_m_n_grid_desc))
580 {
581 return false;
582 }
583
584 constexpr long_index_t TwoGB = (long_index_t{1} << 31);
585
586 if(!(a_b_k0_m_k1_grid_desc.GetElementSpaceSize() * sizeof(FloatA) <= TwoGB &&
587 b_b_k0_n_k1_grid_desc.GetElementSpaceSize() * sizeof(FloatB) <= TwoGB &&
588 c_m_n_grid_desc.GetElementSpaceSize() * sizeof(FloatC) <= TwoGB))
589 {
590 return false;
591 }
592
593 // TODO: also check validity of all components (blockwise-copy, threadwise-copy, etc)
594 return true;
595 }
596
597 __host__ __device__ static constexpr bool CalculateHasMainK0BlockLoop(index_t K0)
598 {
599 // const bool has_main_k0_block_loop = K0 > K0PerBlock;
600 const index_t num_loop = K0 / K0PerBlock;
601
602 return GridwiseGemmPipe::CalculateHasMainLoop(num_loop);
603
604 // return has_main_k0_block_loop;
605 }
606
607 __host__ __device__ static constexpr auto
608 MakeCGridDesc_MBlock_MPerBlock_NBlock_NPerBlock(const CMNGridDesc& c_m_n_grid_desc)
609 {
610 const auto M = c_m_n_grid_desc.GetLength(I0);
611 const auto N = c_m_n_grid_desc.GetLength(I1);
612
613 const auto MBlock = M / MPerBlock;
614 const auto NBlock = N / NPerBlock;
615
617 c_m_n_grid_desc,
622 }
623
624 // return block_id to C matrix tile idx (m0, n0) mapping
625 __host__ __device__ static constexpr auto MakeCBlockClusterAdaptor(
626 const CMNGridDesc& c_m_n_grid_desc, index_t M01, index_t N01, index_t KBatch)
627 {
629 c_m_n_grid_desc, M01, N01, KBatch);
630 }
631
632 __host__ __device__ static constexpr auto
634 {
635 constexpr index_t MWave = MPerBlock / (MRepeat * MPerXdl);
636 constexpr index_t NWave = NPerBlock / (NRepeat * NPerXdl);
637
641 I1,
643 }
644
647 using CBlockClusterAdaptor = decltype(MakeCBlockClusterAdaptor(CMNGridDesc{}, 1, 1, 1));
648
649 template <bool HasMainKBlockLoop>
650 __device__ static void Run(const FloatA* __restrict__ p_a_grid,
651 const FloatB* __restrict__ p_b_grid,
652 FloatC* __restrict__ p_c_grid,
653 void* __restrict__ p_shared,
654 const AGridDesc_B_K0_M_K1& a_b_k0_m_k1_grid_desc,
655 const BGridDesc_B_K0_N_K1& b_b_k0_n_k1_grid_desc,
657 c_grid_desc_mblock_mperblock_nblock_nperblock,
658 const AElementwiseOperation& a_element_op,
659 const BElementwiseOperation& b_element_op,
660 const CElementwiseOperation& c_element_op,
661 const CBlockClusterAdaptor& c_block_cluster_adaptor)
662 {
663 const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
664 p_a_grid, a_b_k0_m_k1_grid_desc.GetElementSpaceSize());
665 const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
666 p_b_grid, b_b_k0_n_k1_grid_desc.GetElementSpaceSize());
668 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
669
670 const auto K0 = a_b_k0_m_k1_grid_desc.GetLength(I1);
671
672 // divide block work by [M, N]
673 const auto block_work_idx =
674 c_block_cluster_adaptor.CalculateBottomIndex(make_multi_index(get_block_1d_id()));
675
676 const index_t k_batch_id = block_work_idx[I0];
677
678 if(!c_block_cluster_adaptor.ValidCTileIndex(
679 make_tuple(block_work_idx[I1], block_work_idx[I2]),
680 make_tuple(c_grid_desc_mblock_mperblock_nblock_nperblock.GetLength(I0),
681 c_grid_desc_mblock_mperblock_nblock_nperblock.GetLength(I2))))
682 {
683 return;
684 }
685
686 // HACK: this force m/n_block_data_idx_on_grid into SGPR
687 const index_t m_block_data_idx_on_grid =
688 __builtin_amdgcn_readfirstlane(block_work_idx[I1] * MPerBlock);
689
690 const index_t n_block_data_idx_on_grid =
691 __builtin_amdgcn_readfirstlane(block_work_idx[I2] * NPerBlock);
692
693 // lds max alignment
694 constexpr auto max_lds_align = K1;
695
696 // A matrix in LDS memory, dst of blockwise copy
697 constexpr auto a_k0_m_k1_block_desc = GetABlockDescriptor_K0PerBlock_MPerBlock_K1();
698
699 constexpr auto a_b_k0_m_k1_block_desc = GetABlockDescriptor_Batch_K0PerBlock_MPerBlock_K1();
700 // B matrix in LDS memory, dst of blockwise copy
701 constexpr auto b_k0_n_k1_block_desc = GetBBlockDescriptor_K0PerBlock_NPerBlock_K1();
702
703 constexpr auto b_b_k0_n_k1_block_desc = GetBBlockDescriptor_Batch_K0PerBlock_NPerBlock_K1();
704 // A matrix blockwise copy
705 auto a_blockwise_copy =
707 AElementwiseOperation,
711 ABlockTransferThreadClusterLengths_K0_M_K1,
712 ABlockTransferThreadClusterArrangeOrder,
713 FloatA,
715 decltype(a_b_k0_m_k1_grid_desc),
716 decltype(a_b_k0_m_k1_block_desc),
717 ABlockTransferSrcAccessOrder,
719 ABlockTransferSrcVectorDim,
720 3,
721 ABlockTransferSrcScalarPerVector,
722 ABlockTransferDstScalarPerVector_K1,
723 1,
724 1,
725 AThreadTransferSrcResetCoordinateAfterRun,
726 true>(
727 a_b_k0_m_k1_grid_desc,
728 make_multi_index(k_batch_id, 0, m_block_data_idx_on_grid, 0),
729 a_element_op,
730 a_b_k0_m_k1_block_desc,
731 make_multi_index(0, 0, 0, 0),
733
734 // B matrix blockwise copy
735 auto b_blockwise_copy =
737 BElementwiseOperation,
741 BBlockTransferThreadClusterLengths_K0_N_K1,
742 BBlockTransferThreadClusterArrangeOrder,
743 FloatB,
745 decltype(b_b_k0_n_k1_grid_desc),
746 decltype(b_b_k0_n_k1_block_desc),
747 BBlockTransferSrcAccessOrder,
749 BBlockTransferSrcVectorDim,
750 3,
751 BBlockTransferSrcScalarPerVector,
752 BBlockTransferDstScalarPerVector_K1,
753 1,
754 1,
755 BThreadTransferSrcResetCoordinateAfterRun,
756 true>(
757 b_b_k0_n_k1_grid_desc,
758 make_multi_index(k_batch_id, 0, n_block_data_idx_on_grid, 0),
759 b_element_op,
760 b_b_k0_n_k1_block_desc,
761 make_multi_index(0, 0, 0, 0),
763
764 // GEMM definition
765 // c_mtx += transpose(a_mtx) * b_mtx
766 // a_mtx[K0PerBlock, MPerBlock] is in LDS
767 // b_mtx[K0PerBlock, NPerBlock] is in LDS
768 // c_mtx[MPerBlock, NPerBlock] is distributed among threads, and saved in
769 // register
770 // sanity check
771 constexpr bool is_single_rate_mfma =
773 K1 <= 4) ||
776 K1 < 32))
777 ? true
778 : false;
779 constexpr auto is_scale_mfma = false;
780 constexpr index_t KPack = math::max(K1,
781 MfmaSelector<ComputeTypeA,
782 MPerXdl,
783 NPerXdl,
784 ComputeTypeB,
785 is_single_rate_mfma,
786 is_scale_mfma>::selected_mfma.k_per_blk);
787
788 auto blockwise_gemm =
792 FloatAcc,
793 decltype(a_k0_m_k1_block_desc),
794 decltype(b_k0_n_k1_block_desc),
795 MPerXdl,
796 NPerXdl,
797 MRepeat,
798 NRepeat,
799 KPack,
800 ComputeTypeA,
801 ComputeTypeB>{};
802
803 auto c_thread_buf = blockwise_gemm.GetCThreadBuffer();
804
805 // LDS allocation for A and B: be careful of alignment
806 constexpr auto a_block_space_size =
807 math::integer_least_multiple(a_k0_m_k1_block_desc.GetElementSpaceSize(), max_lds_align);
808
809 constexpr auto a_block_slice_copy_step = make_multi_index(0, K0PerBlock, 0, 0);
810 constexpr auto b_block_slice_copy_step = make_multi_index(0, K0PerBlock, 0, 0);
811
813 static_cast<FloatAAdjusted*>(p_shared), a_k0_m_k1_block_desc.GetElementSpaceSize());
814
816 static_cast<FloatBAdjusted*>(p_shared) + a_block_space_size,
817 b_k0_n_k1_block_desc.GetElementSpaceSize());
818
819 // gridwise GEMM pipeline
820 const index_t K0BlockMainLoop = __builtin_amdgcn_readfirstlane(K0 / K0PerBlock);
821
822 GridwiseGemmPipe::template Run<HasMainKBlockLoop>(a_b_k0_m_k1_grid_desc,
823 a_b_k0_m_k1_block_desc,
824 a_blockwise_copy,
825 a_grid_buf,
826 a_block_buf,
827 a_block_slice_copy_step,
828 b_b_k0_n_k1_grid_desc,
829 b_b_k0_n_k1_block_desc,
830 b_blockwise_copy,
831 b_grid_buf,
832 b_block_buf,
833 b_block_slice_copy_step,
834 blockwise_gemm,
835 c_thread_buf,
836 K0BlockMainLoop);
837
838 // output: register to global memory
839 {
840 constexpr index_t MWave = MPerBlock / (MRepeat * MPerXdl);
841 constexpr index_t NWave = NPerBlock / (NRepeat * NPerXdl);
842
843 constexpr auto c_m0_n0_m1_n1_m2_m3_m4_n2_block_desc =
844 blockwise_gemm.GetCBlockDescriptor_M0_N0_M1_N1_M2_M3_M4_N2();
845
846 constexpr auto c_m0_n0_m1_n1_m2_m3_m4_n2_thread_desc =
847 blockwise_gemm.GetCThreadDescriptor_M0_N0_M1_N1_M2_M3_M4_N2();
848
849 constexpr auto M0 = c_m0_n0_m1_n1_m2_m3_m4_n2_block_desc.GetLength(I0);
850 constexpr auto N0 = c_m0_n0_m1_n1_m2_m3_m4_n2_block_desc.GetLength(I1);
851 constexpr auto M1 = c_m0_n0_m1_n1_m2_m3_m4_n2_block_desc.GetLength(I2);
852 constexpr auto N1 = c_m0_n0_m1_n1_m2_m3_m4_n2_block_desc.GetLength(I3);
853 constexpr auto M2 = c_m0_n0_m1_n1_m2_m3_m4_n2_block_desc.GetLength(I4);
854 constexpr auto M3 = c_m0_n0_m1_n1_m2_m3_m4_n2_block_desc.GetLength(I5);
855 constexpr auto M4 = c_m0_n0_m1_n1_m2_m3_m4_n2_block_desc.GetLength(I6);
856 constexpr auto N2 = c_m0_n0_m1_n1_m2_m3_m4_n2_block_desc.GetLength(I7);
857
858 constexpr auto c_block_desc_mblock_mperblock_nblock_nperblock =
860
862 static_cast<FloatC*>(p_shared),
863 c_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
864
865 static_assert(M1 == MWave, "");
866 static_assert(N1 == NWave, "");
867 static_assert(M2 * M3 * M4 == MPerXdl, "");
868 static_assert(N2 == NPerXdl, "");
869
870 constexpr auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2 = transform_tensor_descriptor(
871 c_block_desc_mblock_mperblock_nblock_nperblock,
873 make_freeze_transform(I0), // freeze mblock
874 make_unmerge_transform(make_tuple(CShuffleMRepeatPerShuffle,
875 M1,
876 M2,
877 M3,
878 M4)), // M1 = MWave, M2 * M3 * M4 = MPerXdl
879 make_freeze_transform(I0), // freeze nblock
880 make_unmerge_transform(make_tuple(CShuffleNRepeatPerShuffle,
881 N1,
882 N2))), // M1 = MWave, M2 * M3 * M4 = MPerXdl
886
887 // calculate origin of thread output tensor on global memory
888 // blockwise GEMM c matrix starting index
889 const auto c_thread_mtx_on_block =
890 blockwise_gemm.CalculateCThreadOriginDataIndex(I0, I0, I0, I0);
891
892 const index_t m_thread_data_on_block = c_thread_mtx_on_block[I0];
893 const index_t n_thread_data_on_block = c_thread_mtx_on_block[I1];
894
895 const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
897 make_tuple(make_merge_transform(make_tuple(M0, M1, M2, M3, M4))),
900
901 const auto m_thread_data_on_block_idx =
902 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
903 make_multi_index(m_thread_data_on_block));
904
905 const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
910
911 const auto n_thread_data_on_block_idx =
912 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
913 make_multi_index(n_thread_data_on_block));
914
915 // VGPR to LDS
916 auto c_thread_copy_vgpr_to_lds =
918 FloatC,
919 decltype(c_m0_n0_m1_n1_m2_m3_m4_n2_thread_desc),
920 decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
922 Sequence<CShuffleMRepeatPerShuffle,
923 CShuffleNRepeatPerShuffle,
924 I1,
925 I1,
926 M2,
927 I1,
928 M4,
929 I1>,
931 7,
932 1,
934 1,
935 true>{
936 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
938 0,
939 m_thread_data_on_block_idx[I1],
940 n_thread_data_on_block_idx[I1],
941 m_thread_data_on_block_idx[I2],
942 m_thread_data_on_block_idx[I3],
943 m_thread_data_on_block_idx[I4],
944 n_thread_data_on_block_idx[I2]),
946
947 // LDS to global
948 auto c_block_copy_lds_to_global = ThreadGroupTensorSliceTransfer_v6r1<
949 ThisThreadBlock, // index_t BlockSize,
950 CElementwiseOperation, // ElementwiseOperation,
951 CGlobalMemoryDataOperation, // DstInMemOp,
952 Sequence<1,
953 CShuffleMRepeatPerShuffle * MWave * MPerXdl,
954 1,
955 CShuffleNRepeatPerShuffle * NWave * NPerXdl>, // BlockSliceLengths,
956 CBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock,
957 Sequence<0, 1, 2, 3>, // typename ThreadClusterArrangeOrder,
958 FloatC, // typename SrcData,
959 FloatC, // typename DstData,
960 decltype(c_block_desc_mblock_mperblock_nblock_nperblock),
961 decltype(c_grid_desc_mblock_mperblock_nblock_nperblock),
962 Sequence<0, 1, 2, 3>, // typename DimAccessOrder,
963 3, // index_t VectorDim,
964 CBlockTransferScalarPerVector_NWaveNPerXDL, // index_t ScalarPerVector,
965 true, // bool ThreadTransferSrcResetCoordinateAfterRun,
966 false> // bool ThreadTransferDstResetCoordinateAfterRun
967 {c_block_desc_mblock_mperblock_nblock_nperblock,
968 make_multi_index(0, 0, 0, 0),
969 c_grid_desc_mblock_mperblock_nblock_nperblock,
970 make_multi_index(block_work_idx[I1], 0, block_work_idx[I2], 0),
971 c_element_op};
972
973 constexpr auto mxdlperwave_forward_step =
974 make_multi_index(0, CShuffleMRepeatPerShuffle * MWave * MPerXdl, 0, 0);
975 constexpr auto nxdlperwave_forward_step =
976 make_multi_index(0, 0, 0, CShuffleNRepeatPerShuffle * NWave * NPerXdl);
977 constexpr auto nxdlperwave_backward_step =
978 make_multi_index(0, 0, 0, -CShuffleNRepeatPerShuffle * NWave * NPerXdl);
979
980 static_for<0, MRepeat, CShuffleMRepeatPerShuffle>{}([&](auto mxdlperwave_iter) {
981 constexpr auto mxdlperwave = mxdlperwave_iter;
982
983 static_for<0, NRepeat, CShuffleNRepeatPerShuffle>{}([&](auto nxdlperwave_iter) {
984 constexpr bool nxdlperwave_forward_sweep =
985 (mxdlperwave % (2 * CShuffleMRepeatPerShuffle) == 0);
986
987 constexpr index_t nxdlperwave_value =
988 nxdlperwave_forward_sweep
989 ? nxdlperwave_iter
990 : (NRepeat - nxdlperwave_iter - CShuffleNRepeatPerShuffle);
991
992 constexpr auto nxdlperwave = Number<nxdlperwave_value>{};
993
994 // make sure it's safe to do ds_write
996
997 // VGPR to LDS
998 c_thread_copy_vgpr_to_lds.Run(
999 c_m0_n0_m1_n1_m2_m3_m4_n2_thread_desc,
1000 make_tuple(mxdlperwave, nxdlperwave, I0, I0, I0, I0, I0, I0),
1001 c_thread_buf,
1002 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
1003 c_block_buf);
1004
1005 // make sure it's safe to do ds_read
1007
1008 // LDS to global
1009 c_block_copy_lds_to_global.Run(c_block_desc_mblock_mperblock_nblock_nperblock,
1010 c_block_buf,
1011 c_grid_desc_mblock_mperblock_nblock_nperblock,
1012 c_grid_buf);
1013
1014 // move on nxdlperwave dimension
1015 if constexpr(nxdlperwave_forward_sweep &&
1016 (nxdlperwave < NRepeat - CShuffleNRepeatPerShuffle))
1017 {
1018 c_block_copy_lds_to_global.MoveDstSliceWindow(
1019 c_grid_desc_mblock_mperblock_nblock_nperblock,
1020 nxdlperwave_forward_step);
1021 }
1022 else if constexpr((!nxdlperwave_forward_sweep) && (nxdlperwave > 0))
1023 {
1024 c_block_copy_lds_to_global.MoveDstSliceWindow(
1025 c_grid_desc_mblock_mperblock_nblock_nperblock,
1026 nxdlperwave_backward_step);
1027 }
1028 });
1029
1030 // move on mxdlperwave dimension
1031 if constexpr(mxdlperwave < MRepeat - CShuffleMRepeatPerShuffle)
1032 {
1033 c_block_copy_lds_to_global.MoveDstSliceWindow(
1034 c_grid_desc_mblock_mperblock_nblock_nperblock, mxdlperwave_forward_step);
1035 }
1036 });
1037 }
1038 }
1039}; // namespace ck
1040
1041} // namespace ck
#define CK_MIN_BLOCK_PER_CU
Definition ck.hpp:31
#define CK_MAX_THREAD_PER_BLOCK
Definition ck.hpp:30
Definition utility/math.hpp:13
__host__ __device__ constexpr auto integer_least_multiple(X x, Y y)
Definition utility/math.hpp:78
__host__ __device__ constexpr T max(T x)
Definition utility/math.hpp:84
Definition ck.hpp:268
ushort bhalf_t
Definition data_type.hpp:30
__host__ __device__ constexpr auto make_multi_index(Xs &&... xs)
Definition array_multi_index.hpp:15
__host__ __device__ constexpr auto make_pass_through_transform(const LowLength &low_length)
Definition multi_index_transform_helper.hpp:12
constexpr auto GridwiseGemmPipeline_Selector()
Definition gridwise_gemm_pipeline_selector.hpp:31
int32_t index_t
Definition ck.hpp:299
typename conditional< predicate, X, Y >::type conditional_t
Definition utility/functional.hpp:115
__host__ __device__ constexpr auto make_naive_tensor_descriptor(const Tuple< Lengths... > &lengths, const Tuple< Strides... > &strides)
Definition tensor_descriptor_helper.hpp:49
__global__ void kernel_gemm_xdlops_bwd_weight(const FloatA *__restrict__ p_a_grid, const FloatB *__restrict__ p_b_grid, FloatC *__restrict__ p_c_grid, const AGridDesc_B_K0_M_K1 a_b_k0_m_k1_grid_desc, const BGridDesc_B_K0_N_K1 b_b_k0_n_k1_grid_desc, const CGridDesc_MBlock_MPerBlock_NBlock_NPerBlock c_grid_desc_mblock_mperblock_nblock_nperblock, const AElementwiseOperation a_element_op, const BElementwiseOperation b_element_op, const CElementwiseOperation c_element_op, const CBlockClusterAdaptor c_block_cluster_adaptor)
Definition gridwise_gemm_xdlops_bwd_weight.hpp:157
InMemoryDataOperationEnum
Definition ck.hpp:277
@ Set
Definition ck.hpp:278
__host__ __device__ constexpr auto make_single_stage_tensor_adaptor(const Transforms &transforms, LowerDimensionOldTopIdss, UpperDimensionNewTopIdss)
Definition tensor_description/tensor_adaptor.hpp:425
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Definition type.hpp:297
__host__ __device__ constexpr auto make_freeze_transform(const LowerIndex &low_idx)
Definition multi_index_transform_helper.hpp:151
integral_constant< index_t, N > Number
Definition number.hpp:12
__host__ __device__ constexpr auto make_merge_transform_v4_no_carry(const LowLengths &low_lengths)
Definition gridwise_gemm_xdlops_bwd_weight.hpp:136
__host__ __device__ constexpr auto make_merge_transform(const LowLengths &low_lengths)
Definition multi_index_transform_helper.hpp:55
constexpr detail::ignore_t ignore
Definition utility/ignore.hpp:20
__device__ index_t get_block_1d_id()
Definition get_id.hpp:47
__host__ __device__ constexpr auto container_reduce(const Container &x, Reduce reduce, Init init, Number< IBegin >=Number< 0 >{}, Number< IEnd >=Number< Container::Size()>{}, Number< IStep >=Number< 1 >{})
Definition utility/container_helper.hpp:111
__host__ __device__ constexpr auto make_merge_transform_v3_division_mod(const LowLengths &low_lengths)
Definition multi_index_transform_helper.hpp:84
__host__ __device__ constexpr auto make_naive_tensor_descriptor_aligned(const Tuple< Lengths... > &lengths, Align align)
Definition tensor_descriptor_helper.hpp:132
__host__ __device__ constexpr auto make_naive_tensor_descriptor_packed(const Tuple< Lengths... > &lengths)
Definition tensor_descriptor_helper.hpp:101
__host__ __device__ constexpr auto make_tuple(Xs &&... xs)
Definition utility/tuple.hpp:211
int64_t long_index_t
Definition ck.hpp:300
__host__ __device__ constexpr auto transform_tensor_descriptor(const OldTensorDescriptor &old_tensor_desc, const NewTransforms &new_transforms, NewLowerDimensionOldVisibleIdss, NewUpperDimensionNewVisibleIdss)
Definition tensor_description/tensor_descriptor.hpp:319
__host__ __device__ constexpr auto container_reverse_exclusive_scan(const Array< TData, NSize > &x, Reduce f, TData init)
Definition utility/container_helper.hpp:213
__device__ void block_sync_lds()
Definition synchronization.hpp:16
__host__ __device__ constexpr auto make_unmerge_transform(const UpLengths &up_lengths, integral_constant< bool, Use24BitIntegerCalculation >=integral_constant< bool, false >{})
Definition multi_index_transform_helper.hpp:90
__host__ __device__ void print_multi_index(const Tuple< Xs... > &x)
Definition statically_indexed_array_multi_index.hpp:147
typename remove_cv< T >::type remove_cv_t
Definition type.hpp:295
__host__ __device__ constexpr auto make_dynamic_buffer(T *p, ElementSpaceSize element_space_size)
Definition dynamic_buffer.hpp:472
Array< index_t, N > MultiIndex
Definition array_multi_index.hpp:12
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1697
Definition block_to_ctile_map.hpp:720
Definition blockwise_gemm_smfmac_xdlops.hpp:44
Definition gridwise_gemm_xdlops_bwd_weight.hpp:254
static __device__ void Run(const ADataType *__restrict__ p_a_grid, const AccDataType *__restrict__ p_b_grid, InMemoryDataOperationEnum::Set *__restrict__ p_c_grid, void *__restrict__ p_shared, const BGridDesc_K0_N_K1 &a_b_k0_m_k1_grid_desc, const CGridDesc_M_N &b_b_k0_n_k1_grid_desc, const CGridDesc_MBlock_MPerBlock_NBlock_NPerBlock &c_grid_desc_mblock_mperblock_nblock_nperblock, const BElementwiseOperation &a_element_op, const CElementwiseOperation &b_element_op, const MPerBlock &c_element_op, const CBlockClusterAdaptor &c_block_cluster_adaptor)
Definition gridwise_gemm_xdlops_bwd_weight.hpp:650
Definition gridwise_gemm_xdlops_bwd_weight.hpp:25
decltype(make_tuple(container_reduce(LowLengths{}, math::multiplies{}, Number< 1 >{}))) UpLengths
Definition gridwise_gemm_xdlops_bwd_weight.hpp:34
LowLengthsScan low_lengths_scan_
Definition gridwise_gemm_xdlops_bwd_weight.hpp:38
__host__ __device__ void UpdateLowerIndex(LowIdxDiff &idx_diff_low, const UpIdxDiff &idx_up_diff, LowIdx &idx_low, const UpIdx &idx_up_new, Number< Hack >) const
Definition gridwise_gemm_xdlops_bwd_weight.hpp:81
static constexpr index_t NDimLow
Definition gridwise_gemm_xdlops_bwd_weight.hpp:26
__host__ static __device__ constexpr bool IsKnownAtCompileTime()
Definition gridwise_gemm_xdlops_bwd_weight.hpp:107
MultiIndex< NDimLow > LowerIndex
Definition gridwise_gemm_xdlops_bwd_weight.hpp:28
__host__ __device__ constexpr Merge_v4_no_carry()=default
__host__ static __device__ constexpr bool IsValidUpperIndexAlwaysMappedToValidLowerIndex()
Definition gridwise_gemm_xdlops_bwd_weight.hpp:102
__host__ static __device__ constexpr bool IsLinearTransform()
Definition gridwise_gemm_xdlops_bwd_weight.hpp:100
__host__ static __device__ constexpr index_t GetNumOfUpperDimension()
Definition gridwise_gemm_xdlops_bwd_weight.hpp:54
__host__ __device__ constexpr Merge_v4_no_carry(const LowLengths &low_lengths)
Definition gridwise_gemm_xdlops_bwd_weight.hpp:43
MultiIndex< 1 > UpperIndex
Definition gridwise_gemm_xdlops_bwd_weight.hpp:29
__host__ static __device__ constexpr index_t GetNumOfLowerDimension()
Definition gridwise_gemm_xdlops_bwd_weight.hpp:52
UpLengths up_lengths_
Definition gridwise_gemm_xdlops_bwd_weight.hpp:39
decltype(container_reverse_exclusive_scan(LowLengths{}, math::multiplies{}, Number< 1 >{})) LowLengthsScan
Definition gridwise_gemm_xdlops_bwd_weight.hpp:31
__host__ static __device__ constexpr bool IsValidUpperIndexMappedToValidLowerIndex(const UpIdx &)
Definition gridwise_gemm_xdlops_bwd_weight.hpp:116
__host__ __device__ constexpr const auto & GetUpperLengths() const
Definition gridwise_gemm_xdlops_bwd_weight.hpp:56
LowLengths low_lengths_
Definition gridwise_gemm_xdlops_bwd_weight.hpp:37
__host__ __device__ void Print() const
Definition gridwise_gemm_xdlops_bwd_weight.hpp:121
__host__ __device__ constexpr void CalculateLowerIndex(LowIdx &idx_low, const UpIdx &idx_up) const
Definition gridwise_gemm_xdlops_bwd_weight.hpp:59
Selects the appropriate MFMA instruction type and configuration for given data types and tile sizes o...
Definition xdlops_gemm.hpp:1208
Definition utility/sequence.hpp:43
Blockwise data transfer.
Definition thread_group_tensor_slice_transfer_v4r1.hpp:46
Definition thread_group_tensor_slice_transfer_v6r1.hpp:34
Definition threadwise_tensor_slice_transfer.hpp:39
static constexpr value_type value
Definition utility/integral_constant.hpp:13
Definition is_known_at_compile_time.hpp:14
Definition utility/math.hpp:34
Definition functional2.hpp:33
Definition tensor_operation/gpu/element/unary_element_wise_operation.hpp:340