From a281de6638dc69d3fdfb9c83f3671107961fc395 Mon Sep 17 00:00:00 2001 From: zhangxiao35 Date: Tue, 1 Dec 2026 23:22:03 +0800 Subject: [PATCH] feat(moe): support more hidden and latent sizes in the fused MoE tail kernel --- csrc/kernel/moe/mega_ll_moe.cu | 4 +- csrc/kernel/moe/moe_configs.cuh | 17 +- csrc/kernel/moe/moe_mainloop.cuh | 6 +- csrc/kernel/moe/moe_mainloop_tail.cuh | 57 ++- csrc/kernel/moe/moe_tail.cuh | 146 ++++-- csrc/kernel/moe/moe_tail_fused.cuh | 712 +++++++++++++++----------- csrc/kernel/moe/moe_tail_prims.cuh | 6 + 7 files changed, 555 insertions(+), 393 deletions(-) diff --git a/csrc/kernel/moe/mega_ll_moe.cu b/csrc/kernel/moe/mega_ll_moe.cu index 9593282..f73d732 100644 --- a/csrc/kernel/moe/mega_ll_moe.cu +++ b/csrc/kernel/moe/mega_ll_moe.cu @@ -105,7 +105,7 @@ void CheckTailFc2Shape(const torch::Tensor& tail_fc2_weight, constexpr std::int64_t kScaleRows = moe::TailGeometryT::kMxfp8 ? MoEDims::kHiddenSize : MoEDims::kHiddenSize / kBlock; - constexpr std::int64_t kScaleCols = MoEDims::kLatentSize / kBlock; + constexpr std::int64_t kScaleCols = moe::TailGeometryT::kScaleCols; TORCH_CHECK( tail_fc2_weight.sizes() == at::IntArrayRef({MoEDims::kHiddenSize, MoEDims::kLatentSize}) && @@ -295,7 +295,7 @@ void MoEImpl(const torch::Tensor& input, const torch::Tensor& router_weight, const cudaStream_t stream = at::cuda::getCurrentCUDAStream(device).stream(); const std::uint32_t grid_cap = static_cast(tail_grid_cap); const int64_t* uc_bases = tail_uc_bases.const_data_ptr(); - for (int r = 0; r < MoEDims::KernelConfig::kTailWorld; ++r) { + for (int r = 0; r < MoEDims::KernelConfig::kTpSize; ++r) { args.tail_uc_base[r] = reinterpret_cast(uc_bases[r]); } args.tail_mc_base = reinterpret_cast( diff --git a/csrc/kernel/moe/moe_configs.cuh b/csrc/kernel/moe/moe_configs.cuh index 4dfdce2..8116c55 100644 --- a/csrc/kernel/moe/moe_configs.cuh +++ b/csrc/kernel/moe/moe_configs.cuh @@ -141,7 +141,7 @@ struct QuantConfig { static constexpr std::uint32_t kBlockSize = 128; }; -template +template struct Dims_BS8_E512_N512_K2048_SM100 { static constexpr uint32_t kHiddenSize = 4096; static constexpr uint32_t kIntermediateSize = 512; @@ -176,13 +176,13 @@ struct Dims_BS8_E512_N512_K2048_SM100 { static constexpr std::uint32_t kNumDownWeightSlots = 4; static constexpr std::uint32_t kNumAccSlots = 4; // Tail configs - static constexpr int kTailWorld = TailWorld; + static constexpr int kTpSize = TpSize; static constexpr int kTailTpg = 2; static constexpr int kTailGsize = 32; static constexpr int kTailThreads = 256; static constexpr int kTailStage = 1; - static constexpr int kTailBlockRows = 32; - static constexpr int kTailBlkB = 4; + static constexpr int kTailHiddenTileRows = 32; + static constexpr int kTailSuperBlocksPerKChunk = 4; static constexpr int kTailWStagePad = 64; static constexpr int kTailXPad = 8; static constexpr int kTailMaxStageSmemBytes = 160 * 1024; @@ -190,13 +190,12 @@ struct Dims_BS8_E512_N512_K2048_SM100 { }; }; -template +template struct Dims_BS16_E512_N512_K2048_SM100 - : Dims_BS8_E512_N512_K2048_SM100 { + : Dims_BS8_E512_N512_K2048_SM100 { static constexpr uint32_t kNumTokens = 16; - struct KernelConfig - : Dims_BS8_E512_N512_K2048_SM100::KernelConfig { + struct KernelConfig : Dims_BS8_E512_N512_K2048_SM100::KernelConfig { static constexpr std::uint32_t kNumUpInputSlots = 4; // Router configs. static constexpr std::uint32_t kNumCtasForRouter = 32; diff --git a/csrc/kernel/moe/moe_mainloop.cuh b/csrc/kernel/moe/moe_mainloop.cuh index c61bed7..9d8c6ee 100644 --- a/csrc/kernel/moe/moe_mainloop.cuh +++ b/csrc/kernel/moe/moe_mainloop.cuh @@ -84,7 +84,7 @@ struct CollectiveMainloopMoE { const typename TailGeometry::ElementWeight* tail_fc2_w8; const typename TailGeometry::ElementScale* tail_fc2_scale; const DataType::ElementA* tail_norm_weight; - std::uint8_t* tail_uc_base[TailGeometryT::kWorld]; + std::uint8_t* tail_uc_base[TailGeometryT::kTpSize]; std::uint8_t* tail_mc_base; std::uint32_t* tail_epochs; DataType::ElementOut* tail_out; @@ -133,7 +133,7 @@ struct CollectiveMainloopMoE { const typename TailGeometry::ElementWeight* tail_fc2_w8; const typename TailGeometry::ElementScale* tail_fc2_scale; const DataType::ElementA* tail_norm_weight; - std::uint8_t* tail_uc_base[TailGeometryT::kWorld]; + std::uint8_t* tail_uc_base[TailGeometryT::kTpSize]; std::uint8_t* tail_mc_base; std::uint32_t* tail_epochs; DataType::ElementOut* __restrict__ shared_out; @@ -193,7 +193,7 @@ struct CollectiveMainloopMoE { offsetof(Workspace, shared_down_out)); params.tail_out = args.tail_out; params.scratchpad = args.workspace; - for (int r = 0; r < TailGeometryT::kWorld; ++r) { + for (int r = 0; r < TailGeometryT::kTpSize; ++r) { params.tail_uc_base[r] = args.tail_uc_base[r]; } params.tail_mc_base = args.tail_mc_base; diff --git a/csrc/kernel/moe/moe_mainloop_tail.cuh b/csrc/kernel/moe/moe_mainloop_tail.cuh index b27ab60..1e1c0af 100644 --- a/csrc/kernel/moe/moe_mainloop_tail.cuh +++ b/csrc/kernel/moe/moe_mainloop_tail.cuh @@ -34,34 +34,41 @@ struct TailPhase { template __device__ static void prefetch_weights(P const& params) { - constexpr std::uint32_t kGsize = Geometry::kGsize; - constexpr std::uint32_t kShard = Geometry::kShard; - constexpr std::uint32_t kRows = Geometry::kRows; - constexpr std::uint32_t kBytes = kRows * Dims::kLatentSize; + constexpr std::uint32_t group_size = Geometry::kGroupSize; + constexpr std::uint32_t kLd = Geometry::kTailFc2Ld; + constexpr std::uint32_t kCols = Geometry::kScaleCols; constexpr std::uint32_t kChunk = Geometry::kPrefetchChunk; - constexpr std::uint32_t kScale = Geometry::kScaleBlock; - if (blockIdx.x >= kGsize) return; + [[maybe_unused]] constexpr std::uint32_t kScale = Geometry::kScaleBlock; + if (blockIdx.x >= group_size) return; if (threadIdx.x != WarpRole::kPublishWarpId * WarpRole::kThreadsPerWarp) return; - const std::uint64_t row0 = - static_cast(params.tail_rank) * kShard + - static_cast(blockIdx.x) * kRows; + const int chunk = + static_cast(params.tail_rank * group_size + blockIdx.x); + const std::uint64_t row0 = std::uint64_t(tail::kElementsPerVector) * + Geometry::Chunks::chunk_start(chunk); + constexpr std::uint32_t rows = Geometry::Chunks::kChunkRows; + const std::uint32_t bytes = rows * kLd; const std::uint8_t* w = reinterpret_cast(params.tail_fc2_w8) + - row0 * Dims::kLatentSize; -#pragma unroll - for (std::uint32_t off = 0; off < kBytes; off += kChunk) { + row0 * kLd; + for (std::uint32_t off = 0; off < bytes; off += kChunk) { prefetch_l2_bulk(w + off, - (kBytes - off) < kChunk ? (kBytes - off) : kChunk); + (bytes - off) < kChunk ? (bytes - off) : kChunk); } + const std::uint8_t* sf_begin; + std::uint64_t sf_bytes; if constexpr (Geometry::kMxfp8) { - prefetch_l2_bulk( - params.tail_fc2_scale + row0 * (Dims::kLatentSize / 32), - kRows * (Dims::kLatentSize / 32)); + sf_begin = params.tail_fc2_scale + row0 * kCols; + sf_bytes = std::uint64_t(rows) * kCols; } else { - prefetch_l2_bulk(params.tail_fc2_scale + - (row0 / kScale) * (Dims::kLatentSize / kScale), - (Dims::kLatentSize / kScale) * sizeof(float)); + const std::uint64_t first = row0 / kScale; + const std::uint64_t last = (row0 + rows - 1) / kScale; + sf_begin = reinterpret_cast( + params.tail_fc2_scale + first * kCols); + sf_bytes = (last - first + 1) * kCols * sizeof(float); + } + for (std::uint64_t off = 0; off < sf_bytes; off += 128) { + prefetch_l2_line(sf_begin + off); } } @@ -70,7 +77,8 @@ struct TailPhase { P const& params, std::uint32_t tok, std::uint32_t col, DataType::ElementOut b, std::uint32_t tail_parity) { const std::uint32_t lane = threadIdx.x & 31u; - const std::uint32_t grp = lane & ~7u; + constexpr std::uint32_t kVectorLaneMask = tail::kElementsPerVector - 1; + const std::uint32_t grp = lane & ~kVectorLaneMask; const std::uint32_t raw = *reinterpret_cast(&b); std::uint32_t w[4]; #pragma unroll @@ -80,14 +88,15 @@ struct TailPhase { __shfl_sync(0xffffffffu, raw, grp + 2 * j + 1); w[j] = lo | (hi << 16); } - if ((lane & 7u) == 0u) { + if ((lane & kVectorLaneMask) == 0u) { const uint4 u = make_uint4(w[0], w[1], w[2], w[3]); - ::halops::moe::tail::push_only( params, *reinterpret_cast(&u), /*round=*/0, static_cast(tail_parity & 1u), - static_cast(tok), Dims::kLatentSize, col >> 3); + static_cast(tok), Dims::kLatentSize, + col / tail::kElementsPerVector); } } @@ -113,7 +122,7 @@ struct TailPhase { } ::halops::moe::tail::eb5_tail_group_body< - Geometry::kWorld, Dims::kLatentSize, Dims::kHiddenSize, + Geometry::kTpSize, Dims::kLatentSize, Dims::kHiddenSize, Geometry::kMaxM, Geometry>( params, blockIdx.x, threadIdx.x, reinterpret_cast(&smem->routed_experts), diff --git a/csrc/kernel/moe/moe_tail.cuh b/csrc/kernel/moe/moe_tail.cuh index c6c732d..7d8044e 100644 --- a/csrc/kernel/moe/moe_tail.cuh +++ b/csrc/kernel/moe/moe_tail.cuh @@ -20,6 +20,28 @@ namespace halops::moe { +// Partition output H into whole 16B vectors. +template +struct OutputChunkLayout { + static_assert(H > 0 && H % tail::kElementsPerVector == 0, + "H must contain whole 16B vectors"); + static_assert(TpSize > 0 && GroupSize > 0, "chunk counts must be positive"); + static constexpr int kChunks = TpSize * GroupSize; + static constexpr int kChunkRows = H / kChunks; + static constexpr int kVectorsPerChunk = + kChunkRows / tail::kElementsPerVector; + static constexpr int kVectorsPerShard = GroupSize * kVectorsPerChunk; + static_assert(H % kChunks == 0 && kChunkRows % 16 == 0, + "each Hidden chunk must be a multiple of 16 rows"); + + __host__ __device__ static constexpr int chunk_start(int c) { + return c * kVectorsPerChunk; + } + __host__ __device__ static constexpr int shard_start(int rank) { + return rank * kVectorsPerShard; + } +}; + template struct TailGeometryT { using Config = typename Dims::KernelConfig; @@ -29,81 +51,113 @@ struct TailGeometryT { Dims::kQuantConfig.kTailQuant == QuantMethod::kMXFP8; using ElementWeight = typename WeightQuant::ElementType; using ElementScale = typename WeightQuant::ScaleType; - static constexpr int kWorld = Config::kTailWorld; + static constexpr int kTpSize = Config::kTpSize; static constexpr int kMaxM = static_cast(Dims::kNumTokens); static constexpr int kTpg = Config::kTailTpg; - static constexpr int kGsize = Config::kTailGsize; + static constexpr int kGroupSize = Config::kTailGsize; static constexpr int kThreads = Config::kTailThreads; static constexpr std::uint32_t kWarps = kThreads / 32; + static constexpr int L = static_cast(Dims::kLatentSize); + static constexpr int H = static_cast(Dims::kHiddenSize); + + static constexpr std::uint32_t kNumCtas = (kMaxM / kTpg) * kGroupSize; + static constexpr std::int64_t kSlotRegionBytes = + std::int64_t(2) * 2 * kTpSize * kMaxM * H * 2; + static constexpr std::int64_t kXbufRegionBytes = + std::int64_t(2) * kMaxM * L * 2; + + // Output chunk layout and Round-A geometry. + using Chunks = OutputChunkLayout; + static constexpr int kOwnMaxHV = Chunks::kVectorsPerChunk; + static constexpr int kNonOwnMaxHV = + (kTpSize - 1) * Chunks::kVectorsPerChunk; + static constexpr int kHiddenRows = Chunks::kChunkRows; + + // Round-B Hidden/M decomposition. static constexpr int kStage = Config::kTailStage; - static constexpr int kBlkB = Config::kTailBlkB; + static constexpr int kTailFc2Ld = L; static constexpr int kWStagePad = Config::kTailWStagePad; - static constexpr int kXPad = Config::kTailXPad; - static constexpr int kXStride = static_cast(Dims::kLatentSize) + kXPad; - static constexpr int kShard = static_cast(Dims::kHiddenSize) / kWorld; - static constexpr int kRows = kShard / kGsize; - // A staged CTA runs its rows in passes of kBlockRows through kStageBufs - // SMEM buffers. - static constexpr int kBlockRows = - kStage != 0 ? Config::kTailBlockRows : kRows; - static constexpr int kNumBlocks = kRows / kBlockRows; - static constexpr int kStageBufs = kNumBlocks < 2 ? kNumBlocks : 2; - static constexpr int kTiles = kBlockRows / 16; - static constexpr int kKSplit = kTiles >= static_cast(kWarps) - ? 1 - : static_cast(kWarps) / kTiles; - static constexpr int kItemsPerWarp = - kTiles * kKSplit / static_cast(kWarps); - static_assert(kRows % kBlockRows == 0 && kBlockRows % 16 == 0, - "passes must tile the CTA's rows in mma M tiles"); - static_assert(kTiles * kKSplit % static_cast(kWarps) == 0, - "work items must tile over the warps"); - static constexpr int kMaxStageSmemBytes = Config::kTailMaxStageSmemBytes; - static constexpr std::uint32_t kPrefetchChunk = Config::kTailPrefetchChunk; + static constexpr int kHiddenTileRows = + kStage != 0 ? (kHiddenRows % Config::kTailHiddenTileRows == 0 + ? Config::kTailHiddenTileRows + : 16) + : kHiddenRows; + static constexpr int kNumHiddenTiles = kHiddenRows / kHiddenTileRows; + static constexpr int kStageBufs = kNumHiddenTiles < 2 ? kNumHiddenTiles : 2; + static constexpr int kHiddenMmaTiles = kHiddenTileRows / 16; + static constexpr int kKSplitCount = + kHiddenMmaTiles >= static_cast(kWarps) + ? 1 + : static_cast(kWarps) / kHiddenMmaTiles; + static constexpr int kTotalMmaWorkItems = kHiddenMmaTiles * kKSplitCount; + static constexpr int kMmaWorkItemsPerWarp = + (kTotalMmaWorkItems + static_cast(kWarps) - 1) / + static_cast(kWarps); + + // Round-B K/scale decomposition. + static constexpr int kSuperBlocksPerKChunk = + Config::kTailSuperBlocksPerKChunk; + static constexpr int kKSuperBlockSize = 64; static constexpr int kScaleBlock = static_cast(WeightQuant::kBlockSize); - static constexpr std::uint32_t kNumCtas = (kMaxM / kTpg) * kGsize; - static_assert(kMaxM % kTpg == 0, "tail groups tile the token capacity"); - static_assert(kThreads == static_cast(WarpRole::kCalcWarpCount * - WarpRole::kThreadsPerWarp), - "the tail runs on exactly the calc warps"); - static_assert( - kThreads * 8 == static_cast(Dims::kLatentSize), - "one CTA covers one latent row, one 16B vector (8 bf16) per thread"); + static constexpr int kXPad = Config::kTailXPad; + static constexpr int kKGran = + kKSplitCount * kSuperBlocksPerKChunk * kKSuperBlockSize; + static constexpr int Lg = (L + kKGran - 1) / kKGran * kKGran; + static constexpr int kXStride = Lg + kXPad; + static constexpr int kScaleCols = L / kScaleBlock; + static constexpr int kSfAtomGroups = (kScaleCols + 3) / 4 * 4; + static constexpr int kStageLd = Lg; + static constexpr bool kSfBulk = Lg == L; - // Shared-memory layout of one tail CTA: byte offsets from the scratch base. + // Shared-memory layout. + static constexpr int kMaxStageSmemBytes = Config::kTailMaxStageSmemBytes; + static constexpr std::uint32_t kPrefetchChunk = Config::kTailPrefetchChunk; static constexpr std::uint32_t align_up(std::uint32_t v, std::uint32_t a) { return (v + a - 1) / a * a; } static constexpr std::uint32_t kSmemWStage = 0; static constexpr std::uint32_t kStageBufBytes = static_cast( - tail::stage_smem_bytes()); + tail::stage_smem_bytes()); static constexpr std::uint32_t kSmemWStageBytes = kStage == 0 ? 0u : kStageBufs * kStageBufBytes; - // One barrier per staging buffer. static constexpr std::uint32_t kSmemWBar = align_up(kSmemWStage + kSmemWStageBytes, 16); - // MXFP8 stages this CTA's rows of fc2 scales after the barriers. static constexpr std::uint32_t kSmemSf = align_up(kSmemWBar + 8u * kStageBufs, 16); static constexpr std::uint32_t kSmemSfBytes = - kMxfp8 ? static_cast(kRows) * (Dims::kLatentSize / 32) - : 0u; + kMxfp8 ? static_cast(kHiddenRows) * (Lg / 32) : 0u; static constexpr std::uint32_t kSmemX = align_up(kSmemSf + kSmemSfBytes, 16); static constexpr std::uint32_t kSmemWarp = align_up(kSmemX + std::uint32_t(kTpg * kXStride) * 2, 16); static constexpr std::uint32_t kSmemPart = align_up(kSmemWarp + kWarps * 4, 16); - static constexpr std::uint32_t kSmemCol = - align_up(kSmemPart + std::uint32_t(kKSplit * kTpg * kRows) * 4, 16); + static constexpr std::uint32_t kSmemCol = align_up( + kSmemPart + std::uint32_t(kKSplitCount * kTpg * kHiddenRows) * 4, 16); static constexpr std::uint32_t kSmemBytes = - align_up(kSmemCol + std::uint32_t(kTpg * kRows) * 4, 128); + align_up(kSmemCol + std::uint32_t(kTpg * kHiddenRows) * 4, 128); - static constexpr std::int64_t kSlotRegionBytes = - std::int64_t(2) * 2 * kWorld * kMaxM * Dims::kHiddenSize * 2; - static constexpr std::int64_t kXbufRegionBytes = - std::int64_t(2) * kMaxM * Dims::kLatentSize * 2; + static_assert( + H % tail::kElementsPerVector == 0 && (kMxfp8 || H % kScaleBlock == 0), + "H must be 16B-vector aligned and, for block-wise FP8, a multiple " + "of the scale block"); + static_assert(L <= H && L % kScaleBlock == 0, + "L must fit in H-wide mailbox slots and be a multiple of the " + "fc2 scale block (block-wise FP8: 128, MXFP8: 32)"); + static_assert( + kMxfp8 + ? (kScaleBlock == 32 && kKSuperBlockSize % kScaleBlock == 0) + : (kScaleBlock == 128 && + kSuperBlocksPerKChunk % (kScaleBlock / kKSuperBlockSize) == 0), + "K splits must satisfy the quantization scale alignment"); + static_assert(kHiddenRows % kHiddenTileRows == 0 && + kHiddenTileRows % 16 == 0, + "passes must tile the CTA's rows in mma M tiles"); + static_assert(kMaxM % kTpg == 0, "tail groups tile the token capacity"); + static_assert(kThreads == static_cast(WarpRole::kCalcWarpCount * + WarpRole::kThreadsPerWarp), + "the tail runs on exactly the calc warps"); }; template diff --git a/csrc/kernel/moe/moe_tail_fused.cuh b/csrc/kernel/moe/moe_tail_fused.cuh index 7c5507d..cd3c9f1 100644 --- a/csrc/kernel/moe/moe_tail_fused.cuh +++ b/csrc/kernel/moe/moe_tail_fused.cuh @@ -32,29 +32,31 @@ struct Fp8Block128 { using Frag = uint4; static constexpr int kTigStride = 16; static constexpr int SCALE_BLOCK = TC::kScaleBlock; - static constexpr int KR = L / TC::kKSplit; + static constexpr int KR = L / TC::kKSplitCount; static constexpr int kNG = KR / SCALE_BLOCK; static_assert(SCALE_BLOCK == 128, "scale group = two 64-wide super-blocks"); - static_assert(TC::kRows <= SCALE_BLOCK, - "CTA rows must fit in one scale block"); - static_assert(KR % SCALE_BLOCK == 0 && (KR / 64) % TC::kBlkB == 0 && - TC::kBlkB % 2 == 0, + static_assert(KR % SCALE_BLOCK == 0 && + (KR / TC::kKSuperBlockSize) % TC::kSuperBlocksPerKChunk == + 0, "K blocks must tile into batches"); struct State { - const float* sc; - float4 g0, g1; + const float* scale_row; + int first_group; + float4 g0; + float4 g1; }; - __device__ static State init(const float* scale, int /*row*/, int ks, - uint32_t /*tig*/) { + __device__ static State init(const float* scale, int row, int ks, + uint32_t /*tig*/, int grow) { State s; - s.sc = scale + ks * kNG; - s.g0 = s.g1 = make_float4(0.f, 0.f, 0.f, 0.f); + s.scale_row = scale + ((grow + row) / SCALE_BLOCK) * TC::kScaleCols; + s.first_group = ks * kNG; + s.g0 = make_float4(0.f, 0.f, 0.f, 0.f); + s.g1 = make_float4(0.f, 0.f, 0.f, 0.f); return s; } - // mma column grp is token grp. __device__ static int x_row(uint32_t grp, uint32_t /*tig*/) { return grp < uint32_t(TC::kTpg) ? int(grp) : -1; } @@ -65,43 +67,59 @@ struct Fp8Block128 { __device__ static Frag load_w(const uint8_t* p, int sb) { Frag f; if constexpr (STAGED) { - f = *reinterpret_cast(p + sb * 64); + f = *reinterpret_cast(p + sb * TC::kKSuperBlockSize); } else { - ld_global_16B(f, p + sb * 64, 0); + ld_global_16B(f, p + sb * TC::kKSuperBlockSize, 0); } return f; } __device__ static void load_x(const uint16_t* xs, int sb, bool has_x, uint32_t (&xb)[8]) { - uint4 xlo = make_uint4(0u, 0u, 0u, 0u), xhi = xlo; + uint4 xlo = make_uint4(0u, 0u, 0u, 0u); + uint4 xhi = make_uint4(0u, 0u, 0u, 0u); if (has_x) { - xlo = *reinterpret_cast(xs + sb * 64); - xhi = *reinterpret_cast(xs + sb * 64 + 8); + xlo = + *reinterpret_cast(xs + sb * TC::kKSuperBlockSize); + xhi = *reinterpret_cast( + xs + sb * TC::kKSuperBlockSize + 8); } - xb[0] = xlo.x, xb[1] = xlo.y, xb[2] = xlo.z, xb[3] = xlo.w; - xb[4] = xhi.x, xb[5] = xhi.y, xb[6] = xhi.z, xb[7] = xhi.w; + xb[0] = xlo.x; + xb[1] = xlo.y; + xb[2] = xlo.z; + xb[3] = xlo.w; + xb[4] = xhi.x; + xb[5] = xhi.y; + xb[6] = xhi.z; + xb[7] = xhi.w; } __device__ static void step(State& s, float4& total, int sb, const Frag& A, const Frag& B, const uint32_t (&xb)[8]) { - const uint32_t aw[4] = {A.x, A.y, A.z, A.w}, - bw[4] = {B.x, B.y, B.z, B.w}; + const uint32_t aw[4] = {A.x, A.y, A.z, A.w}; + const uint32_t bw[4] = {B.x, B.y, B.z, B.w}; #pragma unroll for (int mm = 0; mm < 4; ++mm) { - uint32_t a0, a1, a2, a3; + uint32_t a0; + uint32_t a1; + uint32_t a2; + uint32_t a3; e4m3x4_to_f16x2x2(aw[mm], a0, a2); e4m3x4_to_f16x2x2(bw[mm], a1, a3); mma_f16_m16n8k16((mm & 1) ? s.g1 : s.g0, a0, a1, a2, a3, xb[2 * mm], xb[2 * mm + 1]); } if (sb & 1) { - const float sc = __ldg(s.sc + (sb >> 1)); + const int group = s.first_group + (sb >> 1); + const float sc = (L == TC::L || group < TC::kScaleCols) + ? __ldg(s.scale_row + group) + : 0.f; total.x += sc * (s.g0.x + s.g1.x); total.y += sc * (s.g0.y + s.g1.y); total.z += sc * (s.g0.z + s.g1.z); total.w += sc * (s.g0.w + s.g1.w); - s.g0 = s.g1 = make_float4(0.f, 0.f, 0.f, 0.f); + s.g0 = make_float4(0.f, 0.f, 0.f, 0.f); + s.g1 = make_float4(0.f, 0.f, 0.f, 0.f); } } }; @@ -111,31 +129,30 @@ struct Mxfp8 { using Scale = uint8_t; using Frag = uint4; static constexpr int kTigStride = 16; - static constexpr int KR = L / TC::kKSplit; + static constexpr int KR = L / TC::kKSplitCount; static constexpr int kNG = KR / 32; static_assert(TC::kTpg == 2 || TC::kTpg == 4, "both group parities must fit the 8 mma columns"); - static_assert(KR % 64 == 0 && (KR / 64) % TC::kBlkB == 0, + static_assert(KR % TC::kKSuperBlockSize == 0 && + (KR / TC::kKSuperBlockSize) % TC::kSuperBlocksPerKChunk == + 0, "K blocks must tile into batches"); - // The lane's scale rows in SMEM, at its first group of its parity. struct State { const uint8_t* sfa; const uint8_t* sfb; }; - // The group parity of the lane's accumulator columns 2 * tig, 2 * tig + 1. __device__ static uint32_t parity(uint32_t tig) { return (2u * tig / uint32_t(TC::kTpg)) & 1u; } __device__ static State init(const uint8_t* sf, int row, int ks, - uint32_t tig) { + uint32_t tig, int /*grow*/) { const uint8_t* const a = sf + row * (L / 32) + ks * kNG + parity(tig); return State{a, a + 8 * (L / 32)}; } - // B column grp is zero unless its parity matches the lane's A groups. __device__ static int x_row(uint32_t grp, uint32_t tig) { constexpr uint32_t TPG = TC::kTpg; return grp < 2u * TPG && grp / TPG == tig / 2u ? int(grp % TPG) : -1; @@ -145,49 +162,60 @@ struct Mxfp8 { __device__ static Frag load_w(const uint8_t* p, int sb) { Frag f; if constexpr (STAGED) { - f = *reinterpret_cast(p + sb * 64); + f = *reinterpret_cast(p + sb * TC::kKSuperBlockSize); } else { - ld_global_16B(f, p + sb * 64, 0); + ld_global_16B(f, p + sb * TC::kKSuperBlockSize, 0); } return f; } __device__ static void load_x(const uint16_t* xs, int sb, bool has_x, uint32_t (&xb)[8]) { - uint4 xlo = make_uint4(0u, 0u, 0u, 0u), xhi = xlo; + uint4 xlo = make_uint4(0u, 0u, 0u, 0u); + uint4 xhi = make_uint4(0u, 0u, 0u, 0u); if (has_x) { - xlo = *reinterpret_cast(xs + sb * 64); - xhi = *reinterpret_cast(xs + sb * 64 + 8); + xlo = + *reinterpret_cast(xs + sb * TC::kKSuperBlockSize); + xhi = *reinterpret_cast( + xs + sb * TC::kKSuperBlockSize + 8); } - xb[0] = xlo.x, xb[1] = xlo.y, xb[2] = xlo.z, xb[3] = xlo.w; - xb[4] = xhi.x, xb[5] = xhi.y, xb[6] = xhi.z, xb[7] = xhi.w; + xb[0] = xlo.x; + xb[1] = xlo.y; + xb[2] = xlo.z; + xb[3] = xlo.w; + xb[4] = xhi.x; + xb[5] = xhi.y; + xb[6] = xhi.z; + xb[7] = xhi.w; } - // Super-block sb's scale group 2 * sb + parity, as a float. __device__ static float scale_of(const uint8_t* sf, int sb) { return __uint_as_float(uint32_t(sf[2 * sb]) << 23); } __device__ static void step(State& s, float4& total, int sb, const Frag& A, const Frag& B, const uint32_t (&xb)[8]) { - const uint32_t aw[4] = {A.x, A.y, A.z, A.w}, - bw[4] = {B.x, B.y, B.z, B.w}; + const uint32_t aw[4] = {A.x, A.y, A.z, A.w}; + const uint32_t bw[4] = {B.x, B.y, B.z, B.w}; float4 g = make_float4(0.f, 0.f, 0.f, 0.f); #pragma unroll for (int mm = 0; mm < 4; ++mm) { - uint32_t a0, a1, a2, a3; + uint32_t a0; + uint32_t a1; + uint32_t a2; + uint32_t a3; e4m3x4_to_f16x2x2(aw[mm], a0, a2); e4m3x4_to_f16x2x2(bw[mm], a1, a3); mma_f16_m16n8k16(g, a0, a1, a2, a3, xb[2 * mm], xb[2 * mm + 1]); } - const float sA = scale_of(s.sfa, sb), sB = scale_of(s.sfb, sb); + const float sA = scale_of(s.sfa, sb); + const float sB = scale_of(s.sfb, sb); total.x += sA * g.x; total.y += sA * g.y; total.z += sB * g.z; total.w += sB * g.w; } - // Lanes tig and tig ^ (TPG / 2) hold the same tokens' two parities. __device__ static void finish(float4& total) { constexpr int kOther = TC::kTpg / 2; total.x += __shfl_xor_sync(0xffffffffu, total.x, kOther); @@ -197,124 +225,135 @@ struct Mxfp8 { } }; -template