Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions csrc/kernel/moe/mega_ll_moe.cu
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void CheckTailFc2Shape(const torch::Tensor& tail_fc2_weight,
constexpr std::int64_t kScaleRows = moe::TailGeometryT<MoEDims>::kMxfp8
? MoEDims::kHiddenSize
: MoEDims::kHiddenSize / kBlock;
constexpr std::int64_t kScaleCols = MoEDims::kLatentSize / kBlock;
constexpr std::int64_t kScaleCols = moe::TailGeometryT<MoEDims>::kScaleCols;
TORCH_CHECK(
tail_fc2_weight.sizes() ==
at::IntArrayRef({MoEDims::kHiddenSize, MoEDims::kLatentSize}) &&
Expand Down Expand Up @@ -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<std::uint32_t>(tail_grid_cap);
const int64_t* uc_bases = tail_uc_bases.const_data_ptr<int64_t>();
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<std::uint8_t*>(uc_bases[r]);
}
args.tail_mc_base = reinterpret_cast<std::uint8_t*>(
Expand Down
17 changes: 8 additions & 9 deletions csrc/kernel/moe/moe_configs.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ struct QuantConfig {
static constexpr std::uint32_t kBlockSize = 128;
};

template <QuantConfig Config, bool UseLatent = true, int TailWorld = 4>
template <QuantConfig Config, bool UseLatent = true, int TpSize = 4>
struct Dims_BS8_E512_N512_K2048_SM100 {
static constexpr uint32_t kHiddenSize = 4096;
static constexpr uint32_t kIntermediateSize = 512;
Expand Down Expand Up @@ -176,27 +176,26 @@ 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;
static constexpr std::uint32_t kTailPrefetchChunk = 16384;
};
};

template <QuantConfig Config, bool UseLatent = true, int TailWorld = 4>
template <QuantConfig Config, bool UseLatent = true, int TpSize = 4>
struct Dims_BS16_E512_N512_K2048_SM100
: Dims_BS8_E512_N512_K2048_SM100<Config, UseLatent, TailWorld> {
: Dims_BS8_E512_N512_K2048_SM100<Config, UseLatent, TpSize> {
static constexpr uint32_t kNumTokens = 16;
struct KernelConfig
: Dims_BS8_E512_N512_K2048_SM100<Config, UseLatent,
TailWorld>::KernelConfig {
struct KernelConfig : Dims_BS8_E512_N512_K2048_SM100<Config, UseLatent,
TpSize>::KernelConfig {
static constexpr std::uint32_t kNumUpInputSlots = 4;
// Router configs.
static constexpr std::uint32_t kNumCtasForRouter = 32;
Expand Down
6 changes: 3 additions & 3 deletions csrc/kernel/moe/moe_mainloop.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dims>::kWorld];
std::uint8_t* tail_uc_base[TailGeometryT<Dims>::kTpSize];
std::uint8_t* tail_mc_base;
std::uint32_t* tail_epochs;
DataType::ElementOut* tail_out;
Expand Down Expand Up @@ -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<Dims>::kWorld];
std::uint8_t* tail_uc_base[TailGeometryT<Dims>::kTpSize];
std::uint8_t* tail_mc_base;
std::uint32_t* tail_epochs;
DataType::ElementOut* __restrict__ shared_out;
Expand Down Expand Up @@ -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<Dims>::kWorld; ++r) {
for (int r = 0; r < TailGeometryT<Dims>::kTpSize; ++r) {
params.tail_uc_base[r] = args.tail_uc_base[r];
}
params.tail_mc_base = args.tail_mc_base;
Expand Down
57 changes: 33 additions & 24 deletions csrc/kernel/moe/moe_mainloop_tail.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,41 @@ struct TailPhase {

template <class P>
__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<std::uint64_t>(params.tail_rank) * kShard +
static_cast<std::uint64_t>(blockIdx.x) * kRows;
const int chunk =
static_cast<int>(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<const std::uint8_t*>(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<const std::uint8_t*>(
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);
}
}

Expand All @@ -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<const std::uint16_t*>(&b);
std::uint32_t w[4];
#pragma unroll
Expand All @@ -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<Geometry::kWorld, Dims::kLatentSize,
::halops::moe::tail::push_only<Geometry::kTpSize, Dims::kLatentSize,
Dims::kHiddenSize, Geometry::kMaxM>(
params,
*reinterpret_cast<const ::halops::moe::tail::Vec16*>(&u),
/*round=*/0, static_cast<int>(tail_parity & 1u),
static_cast<int>(tok), Dims::kLatentSize, col >> 3);
static_cast<int>(tok), Dims::kLatentSize,
col / tail::kElementsPerVector);
}
}

Expand All @@ -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<std::uint8_t*>(&smem->routed_experts),
Expand Down
146 changes: 100 additions & 46 deletions csrc/kernel/moe/moe_tail.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@

namespace halops::moe {

// Partition output H into whole 16B vectors.
template <int H, int TpSize, int GroupSize>
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 <typename Dims>
struct TailGeometryT {
using Config = typename Dims::KernelConfig;
Expand All @@ -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<int>(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<int>(Dims::kLatentSize);
static constexpr int H = static_cast<int>(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<H, kTpSize, kGroupSize>;
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<int>(Dims::kLatentSize) + kXPad;
static constexpr int kShard = static_cast<int>(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<int>(kWarps)
? 1
: static_cast<int>(kWarps) / kTiles;
static constexpr int kItemsPerWarp =
kTiles * kKSplit / static_cast<int>(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<int>(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<int>(kWarps)
? 1
: static_cast<int>(kWarps) / kHiddenMmaTiles;
static constexpr int kTotalMmaWorkItems = kHiddenMmaTiles * kKSplitCount;
static constexpr int kMmaWorkItemsPerWarp =
(kTotalMmaWorkItems + static_cast<int>(kWarps) - 1) /
static_cast<int>(kWarps);

// Round-B K/scale decomposition.
static constexpr int kSuperBlocksPerKChunk =
Config::kTailSuperBlocksPerKChunk;
static constexpr int kKSuperBlockSize = 64;
static constexpr int kScaleBlock =
static_cast<int>(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<int>(WarpRole::kCalcWarpCount *
WarpRole::kThreadsPerWarp),
"the tail runs on exactly the calc warps");
static_assert(
kThreads * 8 == static_cast<int>(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<std::uint32_t>(
tail::stage_smem_bytes<kBlockRows, Dims::kLatentSize, kWStagePad>());
tail::stage_smem_bytes<kHiddenTileRows, kStageLd, kWStagePad>());
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<std::uint32_t>(kRows) * (Dims::kLatentSize / 32)
: 0u;
kMxfp8 ? static_cast<std::uint32_t>(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<int>(WarpRole::kCalcWarpCount *
WarpRole::kThreadsPerWarp),
"the tail runs on exactly the calc warps");
};

template <typename Dims>
Expand Down
Loading
Loading