* Merge with private repo * Update README * Update README * Update README * Add PyTorch requirements * Fix sync scopes for MQA logits (#256) * Update README
389 lines
22 KiB
Plaintext
389 lines
22 KiB
Plaintext
#pragma once
|
|
|
|
#pragma clang diagnostic push
|
|
#pragma clang diagnostic ignored "-Wunknown-attributes"
|
|
|
|
#include <cutlass/arch/barrier.h>
|
|
#include <cutlass/arch/reg_reconfig.h>
|
|
|
|
#include <cute/arch/cluster_sm90.hpp>
|
|
#include <cute/arch/copy_sm90_desc.hpp>
|
|
#include <cute/arch/copy_sm90_tma.hpp>
|
|
#include <cute/arch/mma_sm100_desc.hpp>
|
|
|
|
#include <deep_gemm/common/math.cuh>
|
|
#include <deep_gemm/common/utils.cuh>
|
|
#include <deep_gemm/common/tma_copy.cuh>
|
|
#include <deep_gemm/common/types.cuh>
|
|
#include <deep_gemm/mma/sm90.cuh>
|
|
#include <deep_gemm/epilogue/transform.cuh>
|
|
#include <deep_gemm/ptx/ld_st.cuh>
|
|
#include <deep_gemm/ptx/utils.cuh>
|
|
#include <deep_gemm/ptx/wgmma.cuh>
|
|
#include <deep_gemm/scheduler/gemm.cuh>
|
|
|
|
namespace deep_gemm {
|
|
|
|
template <cute::UMMA::Major kMajorA, cute::UMMA::Major kMajorB,
|
|
uint32_t SHAPE_M, uint32_t SHAPE_N, uint32_t SHAPE_K,
|
|
uint32_t kNumGroups,
|
|
uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t BLOCK_K_,
|
|
uint32_t kSwizzleAMode, uint32_t kSwizzleBMode, uint32_t kSwizzleDMode,
|
|
uint32_t kNumStages_,
|
|
uint32_t kNumTMAThreads, uint32_t kNumMathThreads,
|
|
uint32_t kNumTMAMulticast, bool kIsTMAMulticastOnA,
|
|
uint32_t kNumSMs,
|
|
GemmType kGemmType, bool kWithAccumulation,
|
|
typename cd_dtype_t>
|
|
CUTLASS_GLOBAL __launch_bounds__(kNumTMAThreads + kNumMathThreads, 1) void
|
|
sm90_bf16_gemm_impl(int* grouped_layout,
|
|
uint32_t shape_m, uint32_t shape_n, uint32_t shape_k,
|
|
const __grid_constant__ cute::TmaDescriptor tensor_map_a,
|
|
const __grid_constant__ cute::TmaDescriptor tensor_map_b,
|
|
const __grid_constant__ cute::TmaDescriptor tensor_map_cd) {
|
|
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 900)) or defined(__CLION_IDE__)
|
|
// Enlarge `BLOCK_K` for some cases
|
|
// NOTES: this is for reducing the `warpgroup_wait<0>()` overhead
|
|
constexpr uint32_t kDoMergeStages =
|
|
kNumStages_ >= 10 and
|
|
kGemmType == GemmType::Normal and
|
|
kMajorA == cute::UMMA::Major::K and kMajorB == cute::UMMA::Major::K and
|
|
kNumMathThreads == 128;
|
|
// Ensure there are at least `kNumMinStages` stages after merge
|
|
constexpr uint32_t kNumMinStages = 5;
|
|
constexpr uint32_t kNumStagesPerMerge = kDoMergeStages ? kNumStages_ / kNumMinStages : 1;
|
|
constexpr uint32_t BLOCK_K = BLOCK_K_ * kNumStagesPerMerge;
|
|
constexpr uint32_t kNumStages = kNumStages_ / kNumStagesPerMerge;
|
|
|
|
// Types
|
|
using WGMMA = typename mma::sm90::BF16MMASelector<BLOCK_N, kMajorA, kMajorB>::type;
|
|
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
|
DG_STATIC_ASSERT(BLOCK_M % WGMMA::M == 0 or BLOCK_M < WGMMA::M, "Invalid block size");
|
|
|
|
// Overwrite shape constants if the compiler gives
|
|
shape_m = SHAPE_M != 0 ? SHAPE_M : shape_m;
|
|
shape_n = SHAPE_N != 0 ? SHAPE_N : shape_n;
|
|
shape_k = SHAPE_K != 0 ? SHAPE_K : shape_k;
|
|
|
|
// Shared memory
|
|
static constexpr uint32_t SMEM_D_SIZE = math::constexpr_align(BLOCK_M * BLOCK_N * static_cast<uint32_t>(sizeof(cd_dtype_t)), 1024u);
|
|
static constexpr uint32_t SMEM_A_SIZE_PER_STAGE = BLOCK_M * BLOCK_K * sizeof(__nv_bfloat16);
|
|
static constexpr uint32_t SMEM_B_SIZE_PER_STAGE = BLOCK_N * BLOCK_K * sizeof(__nv_bfloat16);
|
|
|
|
// NOTES: Make sure we have enough shared memory for WGMMA padding
|
|
static constexpr uint32_t WGMMA_A_SIZE_PER_STAGE = WGMMA::M * BLOCK_K * sizeof(__nv_fp8_e4m3);
|
|
DG_STATIC_ASSERT(WGMMA_A_SIZE_PER_STAGE <= SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE * kNumStages, "Memory Out of bound for WGMMA");
|
|
|
|
// Configs
|
|
const uint32_t warp_idx = __shfl_sync(0xffffffff, threadIdx.x / 32, 0);
|
|
const uint32_t lane_idx = ptx::get_lane_idx();
|
|
|
|
// Prefetch TMA descriptors at the very beginning
|
|
if (warp_idx == kNumMathThreads / 32 and cute::elect_one_sync()) {
|
|
cute::prefetch_tma_descriptor(&tensor_map_a);
|
|
cute::prefetch_tma_descriptor(&tensor_map_b);
|
|
cute::prefetch_tma_descriptor(&tensor_map_cd);
|
|
}
|
|
__syncwarp();
|
|
|
|
// Align to 1024 bytes for swizzle-128B
|
|
extern __shared__ __align__(1024) uint8_t smem_buffer[];
|
|
DG_STATIC_ASSERT(SMEM_D_SIZE % 1024 == 0 and SMEM_A_SIZE_PER_STAGE % 1024 == 0 and SMEM_B_SIZE_PER_STAGE % 1024 == 0,
|
|
"Shared memory of A/B/D must be aligned to 1024 bytes");
|
|
|
|
// D/A/B shared memory
|
|
auto smem_d = reinterpret_cast<cd_dtype_t*>(smem_buffer);
|
|
auto smem_a = utils::PatternVisitor([&](const uint32_t& i) {
|
|
return reinterpret_cast<cutlass::bfloat16_t*>(smem_buffer + SMEM_D_SIZE + i * SMEM_A_SIZE_PER_STAGE);
|
|
});
|
|
auto smem_b = utils::PatternVisitor([&](const uint32_t& i) {
|
|
return reinterpret_cast<cutlass::bfloat16_t*>(smem_buffer + SMEM_D_SIZE + kNumStages * SMEM_A_SIZE_PER_STAGE + i * SMEM_B_SIZE_PER_STAGE);
|
|
});
|
|
|
|
// Fill barriers
|
|
auto barrier_start_ptr = reinterpret_cast<Barrier*>(smem_buffer + SMEM_D_SIZE + kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE));
|
|
auto full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (i); });
|
|
auto empty_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages + i); });
|
|
|
|
// Initialize barriers
|
|
if (warp_idx == kNumMathThreads / 32 + 1 and cute::elect_one_sync()) {
|
|
#pragma unroll
|
|
for (uint32_t i = 0; i < kNumStages; ++ i) {
|
|
full_barriers[i]->init(1);
|
|
empty_barriers[i]->init(kNumTMAMulticast * kNumMathThreads / 32);
|
|
}
|
|
|
|
// Make initialized barrier visible in async proxy
|
|
cutlass::arch::fence_barrier_init();
|
|
}
|
|
|
|
// Synchronize all threads to make barrier visible in normal memory model
|
|
(kNumTMAMulticast > 1) ? cute::cluster_sync() : __syncthreads();
|
|
|
|
// Register reconfigurations
|
|
constexpr uint32_t kNumTMARegisters = 48;
|
|
constexpr uint32_t kNumMathRegisters = kNumMathThreads == 128 ? 248 : 224;
|
|
|
|
// Wait for primary kernel completion
|
|
cudaGridDependencySynchronize();
|
|
|
|
// Block scheduler
|
|
uint32_t m_block_idx, n_block_idx;
|
|
auto scheduler = sched::Scheduler<kGemmType, BLOCK_M, BLOCK_N, kNumGroups, kNumTMAMulticast, kIsTMAMulticastOnA, kNumSMs>(shape_m, shape_n, shape_k, grouped_layout);
|
|
|
|
// Pipeline and TMA phases
|
|
uint32_t stage_idx = 0, phase = 0;
|
|
auto advance_pipeline = [&](uint32_t& k_block_idx) {
|
|
++ k_block_idx;
|
|
|
|
// Flip phases only if reach the next first stage
|
|
stage_idx = stage_idx == kNumStages - 1 ? 0 : stage_idx + 1;
|
|
phase ^= stage_idx == 0;
|
|
};
|
|
|
|
if (warp_idx >= kNumMathThreads / 32) {
|
|
// TMA warp-group for loading data
|
|
cutlass::arch::warpgroup_reg_dealloc<kNumTMARegisters>();
|
|
|
|
// NOTES: only one thread (or warp) will be used
|
|
// We use the third warp, as warp 0/1 may be doing WGMMA with `BLOCK_M == 32`
|
|
if (warp_idx == kNumMathThreads / 32 + 2 and cute::elect_one_sync()) {
|
|
DG_STATIC_ASSERT(kNumTMAThreads >= 128, "Need at least 128 threads for TMA warp-group");
|
|
|
|
// Persistently schedule over blocks
|
|
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
|
// Assign TMA multicast number into A and B
|
|
// NOTES: there may be additional odd rows/columns or cases where multicast is not possible.
|
|
const bool is_tma_multicast_valid = scheduler.is_tma_multicast_valid(m_block_idx);
|
|
const uint32_t num_tma_multicast_a = (kIsTMAMulticastOnA and is_tma_multicast_valid) ? kNumTMAMulticast : 1;
|
|
const uint32_t num_tma_multicast_b = (not kIsTMAMulticastOnA and is_tma_multicast_valid) ? kNumTMAMulticast : 1;
|
|
DG_STATIC_ASSERT(kNumTMAMulticast <= 2, "Scheduler does not support > 2 TMA multicast");
|
|
|
|
const auto num_total_k_blocks = math::ceil_div(scheduler.current_shape_k, BLOCK_K);
|
|
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
|
// Wait consumer release
|
|
empty_barriers[stage_idx]->wait(phase ^ 1);
|
|
|
|
constexpr bool kWithGroupOffsetA = kGemmType == GemmType::MGroupedMasked;
|
|
auto& full_barrier = *full_barriers[stage_idx];
|
|
|
|
const auto m_idx = scheduler.template get_global_idx<kWithGroupOffsetA, sched::IndexType::MN>(shape_m, BLOCK_M, m_block_idx);
|
|
const auto n_idx = scheduler.template get_global_idx<(kMajorB == cute::UMMA::Major::K), sched::IndexType::MN>(shape_n, BLOCK_N, n_block_idx, m_block_idx);
|
|
|
|
DG_STATIC_ASSERT(kGemmType == GemmType::Normal or kGemmType == GemmType::KGroupedContiguous or kMajorA == cute::UMMA::Major::K, "Invalid major");
|
|
uint32_t k_a_idx = scheduler.template get_global_idx<(kMajorA == cute::UMMA::Major::MN), sched::IndexType::K> (
|
|
shape_k, BLOCK_K, k_block_idx, m_block_idx);
|
|
uint32_t k_b_idx = scheduler.template get_global_idx<(kMajorB == cute::UMMA::Major::MN), sched::IndexType::K> (
|
|
shape_k, BLOCK_K, k_block_idx, m_block_idx);
|
|
|
|
// Issue TMAs
|
|
constexpr bool kIsBatchedMM = (kGemmType == GemmType::Batched);
|
|
const uint32_t batch_idx = (kIsBatchedMM ? scheduler.current_group_idx : 0);
|
|
if constexpr (kMajorA == cute::UMMA::Major::K)
|
|
tma::copy<BLOCK_K, BLOCK_M, kSwizzleAMode, cutlass::bfloat16_t, kIsBatchedMM>(
|
|
&tensor_map_a, &full_barrier, smem_a[stage_idx], k_a_idx, m_idx, num_tma_multicast_a, batch_idx);
|
|
if constexpr (kMajorA == cute::UMMA::Major::MN)
|
|
tma::copy<BLOCK_M, BLOCK_K, kSwizzleAMode, cutlass::bfloat16_t, kIsBatchedMM>(
|
|
&tensor_map_a, &full_barrier, smem_a[stage_idx], m_idx, k_a_idx, num_tma_multicast_a, batch_idx);
|
|
if constexpr (kMajorB == cute::UMMA::Major::K)
|
|
tma::copy<BLOCK_K, BLOCK_N, kSwizzleBMode, cutlass::bfloat16_t, kIsBatchedMM>(
|
|
&tensor_map_b, &full_barrier, smem_b[stage_idx], k_b_idx, n_idx, num_tma_multicast_b, batch_idx);
|
|
if constexpr (kMajorB == cute::UMMA::Major::MN)
|
|
tma::copy<BLOCK_N, BLOCK_K, kSwizzleBMode, cutlass::bfloat16_t, kIsBatchedMM>(
|
|
&tensor_map_b, &full_barrier, smem_b[stage_idx], n_idx, k_b_idx, num_tma_multicast_b, batch_idx);
|
|
full_barrier.arrive_and_expect_tx(SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE);
|
|
}
|
|
}
|
|
|
|
// To safely deconstruct distributed shared barriers, we need another round of empty waits
|
|
if constexpr (kNumTMAMulticast > 1) {
|
|
for (uint32_t i = 0; i < kNumStages; advance_pipeline(i))
|
|
empty_barriers[stage_idx]->wait(phase ^ 1);
|
|
}
|
|
}
|
|
} else {
|
|
// Math warp-groups for WGMMA
|
|
cutlass::arch::warpgroup_reg_alloc<kNumMathRegisters>();
|
|
|
|
// NOTES: use `__shfl_sync` to encourage NVCC to use unified registers
|
|
const auto math_wg_idx = __shfl_sync(0xffffffff, threadIdx.x / 128, 0);
|
|
|
|
// Merged stages only happens in NT normal GEMM cases
|
|
constexpr uint32_t BLOCK_ATOM_K = BLOCK_K / kNumStagesPerMerge;
|
|
auto a_desc = mma::sm90::make_gmma_desc<kMajorA, BLOCK_M, BLOCK_ATOM_K, kSwizzleAMode>(smem_a[0], math_wg_idx * WGMMA::M, 0);
|
|
auto b_desc = mma::sm90::make_gmma_desc<kMajorB, BLOCK_N, BLOCK_ATOM_K, kSwizzleBMode>(smem_b[0], 0, 0);
|
|
const uint32_t a_desc_lo = __shfl_sync(0xffffffff, a_desc.reg32_[0], 0);
|
|
const uint32_t b_desc_lo = __shfl_sync(0xffffffff, b_desc.reg32_[0], 0);
|
|
|
|
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
|
constexpr uint32_t WAVE_BLOCK_M = BLOCK_M <= WGMMA::M ? BLOCK_M : WGMMA::M * 2;
|
|
DG_STATIC_ASSERT(BLOCK_M % WAVE_BLOCK_M == 0, "Invalid block sizes");
|
|
float accum[WGMMA::kNumAccum * (BLOCK_M / WAVE_BLOCK_M)] = {0};
|
|
|
|
// Pick threads whose WGMMA results are to be stored in shared memory
|
|
DG_STATIC_ASSERT(BLOCK_M >= 64 or kNumMathThreads == 128, "Only one math warp group for `BLOCK_M < 64`");
|
|
constexpr uint32_t kNumWGMMAStoreThreads = WAVE_BLOCK_M * (128 / WGMMA::M);
|
|
const bool do_wgmma_store = BLOCK_M >= 64 or warp_idx < kNumWGMMAStoreThreads / 32;
|
|
|
|
// Empty barrier arrival
|
|
auto empty_barrier_arrive = [&](uint32_t s) {
|
|
if constexpr (kNumTMAMulticast == 1) {
|
|
lane_idx == 0 ? empty_barriers[s]->arrive() : void();
|
|
} else {
|
|
auto target_cta = scheduler.is_peer_cta_alive ? lane_idx : cute::block_rank_in_cluster();
|
|
lane_idx < kNumTMAMulticast ? empty_barriers[s]->arrive(target_cta) : void();
|
|
}
|
|
};
|
|
|
|
// TODO: remove some useless computation for unaligned Ms
|
|
const auto num_total_k_blocks = math::ceil_div(scheduler.current_shape_k, BLOCK_K);
|
|
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
|
const auto a_desc_base_lo = a_desc_lo + stage_idx * (SMEM_A_SIZE_PER_STAGE / 16);
|
|
const auto b_desc_base_lo = b_desc_lo + stage_idx * (SMEM_B_SIZE_PER_STAGE / 16);
|
|
|
|
// Wait TMA arrivals
|
|
full_barriers[stage_idx]->wait(phase);
|
|
|
|
// Commit WGMMA instructions
|
|
#pragma unroll
|
|
for (uint32_t i = 0; i < WGMMA::kNumAccum * (BLOCK_M / WAVE_BLOCK_M); ++ i)
|
|
ptx::warpgroup_fence_operand(accum[i]);
|
|
ptx::warpgroup_arrive();
|
|
#pragma unroll
|
|
for (uint32_t local_idx = 0; local_idx < BLOCK_M / WAVE_BLOCK_M; ++ local_idx) {
|
|
auto shifted_accum = accum + WGMMA::kNumAccum * local_idx;
|
|
#pragma unroll
|
|
for (uint32_t k = 0; k < BLOCK_K / WGMMA::K; ++ k) {
|
|
const uint32_t atom_k_idx = k * WGMMA::K / BLOCK_ATOM_K;
|
|
a_desc.reg32_[0] = mma::sm90::advance_gmma_desc_lo<kMajorA, BLOCK_M, BLOCK_ATOM_K, kSwizzleAMode, nv_bfloat16>(
|
|
a_desc_base_lo, local_idx * WAVE_BLOCK_M, (k * WGMMA::K) % BLOCK_ATOM_K, atom_k_idx * BLOCK_M * BLOCK_ATOM_K);
|
|
b_desc.reg32_[0] = mma::sm90::advance_gmma_desc_lo<kMajorB, BLOCK_N, BLOCK_ATOM_K, kSwizzleBMode, nv_bfloat16>(
|
|
b_desc_base_lo, 0, (k * WGMMA::K) % BLOCK_ATOM_K, atom_k_idx * BLOCK_N * BLOCK_ATOM_K);
|
|
WGMMA::wgmma(a_desc, b_desc, shifted_accum, 1);
|
|
}
|
|
}
|
|
ptx::warpgroup_commit_batch();
|
|
#pragma unroll
|
|
for (uint32_t i = 0; i < WGMMA::kNumAccum * (BLOCK_M / WAVE_BLOCK_M); ++ i)
|
|
ptx::warpgroup_fence_operand(accum[i]);
|
|
ptx::warpgroup_wait<0>();
|
|
|
|
// Notify barrier arrival
|
|
empty_barrier_arrive(stage_idx);
|
|
}
|
|
|
|
// TMA checks
|
|
constexpr uint32_t kNumElemBytes = sizeof(nv_bfloat16);
|
|
constexpr uint32_t TMA_D_BLOCK_N = kSwizzleDMode == 0 ? BLOCK_N : (kSwizzleDMode / kNumElemBytes);
|
|
constexpr uint32_t WGMMA_M_PER_WARP = WGMMA::M / 4;
|
|
DG_STATIC_ASSERT(BLOCK_M % 8 == 0, "Invalid swizzling atom");
|
|
DG_STATIC_ASSERT(BLOCK_N % TMA_D_BLOCK_N == 0 and BLOCK_N / TMA_D_BLOCK_N <= 32,
|
|
"Unaligned TMA store or too many TMA store instructions");
|
|
DG_STATIC_ASSERT(TMA_D_BLOCK_N % 8 == 0, "Invalid TMA block N");
|
|
|
|
// Skip WGMMA store for the unfilled parts
|
|
if (not do_wgmma_store)
|
|
continue;
|
|
|
|
// Wait last TMA store to be finished
|
|
if (threadIdx.x < BLOCK_N / TMA_D_BLOCK_N)
|
|
cute::tma_store_wait<0>();
|
|
cutlass::arch::NamedBarrier::sync(kNumWGMMAStoreThreads, 0);
|
|
|
|
if constexpr (cute::is_same_v<cd_dtype_t, cutlass::bfloat16_t>) {
|
|
// Write back to shared memory using STSM and issue TMA stores
|
|
DG_STATIC_ASSERT(kSwizzleDMode > 0, "Invalid swizzling type");
|
|
DG_STATIC_ASSERT(WGMMA::kNumAccum % 4 == 0, "Invalid STSM x2 vectorization");
|
|
#pragma unroll
|
|
for (uint32_t local_idx = 0; local_idx < BLOCK_M / WAVE_BLOCK_M; ++ local_idx) {
|
|
auto m_offset = local_idx * WAVE_BLOCK_M;
|
|
auto shifted_accum = accum + WGMMA::kNumAccum * local_idx;
|
|
#pragma unroll
|
|
for (auto i = 0; i < WGMMA::kNumAccum / 4; ++ i) {
|
|
// Swizzle or padding into the correct address
|
|
uint8_t* smem_ptr = nullptr;
|
|
if constexpr (kSwizzleDMode > 0) {
|
|
// Calculate the swizzling atom offset and in-atom offset
|
|
constexpr uint32_t kNumBankGroupBytes = 16;
|
|
auto atom_offset = i / (TMA_D_BLOCK_N / 8), in_atom_offset = i % (TMA_D_BLOCK_N / 8);
|
|
|
|
// Calculate the index of the bank group to be written in the atom
|
|
auto bank_group_index = in_atom_offset + lane_idx * (kSwizzleDMode / kNumBankGroupBytes);
|
|
|
|
// Reshape the atom in another view and swizzle
|
|
// - original: `(BLOCK_M, kSwizzleDMode / kNumBankGroupBytes)`
|
|
// - new: `(BLOCK_M * kSwizzleDMode / kNumBankGroupBytes / 8, 8)`
|
|
constexpr bool kHasShortcut = (kSwizzleDMode / kNumBankGroupBytes) == 8;
|
|
auto row = kHasShortcut ? (in_atom_offset / 8 + lane_idx) : (bank_group_index / 8);
|
|
auto col = kHasShortcut ? (in_atom_offset) : (bank_group_index % 8);
|
|
col ^= row % (kSwizzleDMode / 16);
|
|
|
|
// Add back into the base pointer
|
|
// NOTES: think twice before modifying this, as changes may affect the number of instructions
|
|
smem_ptr = reinterpret_cast<uint8_t*>(smem_d) + // Base pointer
|
|
warp_idx * (WGMMA_M_PER_WARP * kSwizzleDMode) + // Warp offset
|
|
m_offset * kSwizzleDMode + // Wave offset
|
|
atom_offset * BLOCK_M * kSwizzleDMode + // Swizzle atom offset (constants)
|
|
row * (kNumBankGroupBytes * 8) + col * kNumBankGroupBytes; // In-atom offset
|
|
} else {
|
|
// No swizzling
|
|
smem_ptr = reinterpret_cast<uint8_t*>(smem_d + (m_offset + warp_idx * WGMMA_M_PER_WARP + lane_idx) * BLOCK_N + i * 8);
|
|
}
|
|
|
|
// NOTES: only 16 lanes' addresses are used
|
|
ptx::SM90_U32x2_STSM_N<nv_bfloat162>::copy(
|
|
__float22bfloat162_rn({shifted_accum[i * 4 + 0], shifted_accum[i * 4 + 1]}),
|
|
__float22bfloat162_rn({shifted_accum[i * 4 + 2], shifted_accum[i * 4 + 3]}),
|
|
smem_ptr
|
|
);
|
|
}
|
|
}
|
|
} else {
|
|
// Use `st.shared` if STSM is not available
|
|
#pragma unroll
|
|
for (uint32_t local_idx = 0; local_idx < BLOCK_M / WAVE_BLOCK_M; ++ local_idx) {
|
|
auto m_offset = local_idx * WAVE_BLOCK_M;
|
|
auto shifted_accum = accum + WGMMA::kNumAccum * local_idx;
|
|
auto smem_d_0 = reinterpret_cast<float2*>(smem_d + (m_offset + warp_idx * WGMMA_M_PER_WARP + lane_idx / 4 + 0) * BLOCK_N + (lane_idx % 4) * 2);
|
|
auto smem_d_1 = reinterpret_cast<float2*>(smem_d + (m_offset + warp_idx * WGMMA_M_PER_WARP + lane_idx / 4 + 8) * BLOCK_N + (lane_idx % 4) * 2);
|
|
#pragma unroll
|
|
for (uint32_t i = 0; i < WGMMA::kNumAccum / 4; ++ i) {
|
|
ptx::st_shared(smem_d_0 + i * 4, make_float2(shifted_accum[i * 4 + 0], shifted_accum[i * 4 + 1]));
|
|
ptx::st_shared(smem_d_1 + i * 4, make_float2(shifted_accum[i * 4 + 2], shifted_accum[i * 4 + 3]));
|
|
}
|
|
}
|
|
}
|
|
cute::tma_store_fence();
|
|
cutlass::arch::NamedBarrier::sync(kNumWGMMAStoreThreads, 0);
|
|
|
|
// Use TMA store to write back to global memory
|
|
const auto m_idx = scheduler.template get_global_idx<(not is_m_grouped_contiguous(kGemmType)), sched::IndexType::MN>(shape_m, BLOCK_M, m_block_idx);
|
|
DG_STATIC_ASSERT(kNumWGMMAStoreThreads >= BLOCK_N / TMA_D_BLOCK_N, "Too many TMA blocks");
|
|
if (threadIdx.x < BLOCK_N / TMA_D_BLOCK_N) {
|
|
auto in_block_n_offset = threadIdx.x * TMA_D_BLOCK_N;
|
|
auto smem_ptr = smem_d + in_block_n_offset * BLOCK_M;
|
|
if constexpr (kGemmType == GemmType::Batched) {
|
|
cute::SM90_TMA_STORE_3D::copy(&tensor_map_cd, smem_ptr,
|
|
n_block_idx * BLOCK_N + in_block_n_offset,
|
|
m_idx, scheduler.current_group_idx);
|
|
} else {
|
|
using cute_tma_t = cute::conditional_t<kWithAccumulation,
|
|
cute::SM90_TMA_REDUCE_ADD_2D, cute::SM90_TMA_STORE_2D>;
|
|
cute_tma_t::copy(&tensor_map_cd, smem_ptr,
|
|
n_block_idx * BLOCK_N + in_block_n_offset, m_idx);
|
|
}
|
|
cute::tma_store_arrive();
|
|
}
|
|
__syncwarp();
|
|
}
|
|
}
|
|
#else
|
|
if (blockIdx.x == 0 and threadIdx.x == 0)
|
|
DG_DEVICE_ASSERT(false and "This kernel only support sm_90a");
|
|
#endif
|
|
}
|
|
|
|
}; // namespace deep_gemm
|
|
|
|
#pragma clang diagnostic pop
|