[ROCm][Bugfix][Perf] Tune multi-stream shared experts use; wvSplitKrc fixes (#56098)
Signed-off-by: Matthew Wong <Matthew.Wong2@amd.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+215
-31
@@ -8,12 +8,20 @@
|
||||
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <utility>
|
||||
|
||||
#include "../cuda_compat.h"
|
||||
#include "dispatch_utils.h"
|
||||
#include "quantization/w8a8/fp8/common.cuh"
|
||||
#include "core/batch_invariant.hpp"
|
||||
|
||||
// Number of streams the pre-allocated split-K pool covers. ~7.5 MiB per slot.
|
||||
static constexpr int64_t kWvSlots = 8;
|
||||
// Slots held back for capturing streams, which cannot allocate one themselves.
|
||||
static constexpr int64_t kWvCaptureSlots = 8;
|
||||
|
||||
// TODO(rasmith): The kernels in this file are susceptible to integer overflow
|
||||
// issues, do not take strides, and are unable to handle PyTorch tensors that
|
||||
// return is_contiguous() as False (the tensors may actually be contiguous
|
||||
@@ -1364,9 +1372,18 @@ __global__ void __launch_bounds__(WvPrGrp* THRDS)
|
||||
};
|
||||
using big4 = __attribute__((__vector_size__(4 * sizeof(bigType)))) __bf16;
|
||||
|
||||
__shared__ scalar_t stg[WvPrGrp * WVLDS / GrpsShrB];
|
||||
// The main-loop staging areas are separated from the split-K readback by a
|
||||
// __syncthreads(), so the readback may reuse all of LDS.
|
||||
__shared__ union {
|
||||
struct {
|
||||
scalar_t stg[WvPrGrp * WVLDS / GrpsShrB];
|
||||
scalar_t s[max_lds_len - WvPrGrp * WVLDS / GrpsShrB];
|
||||
} mainloop;
|
||||
scalar_t all[max_lds_len];
|
||||
} lds;
|
||||
auto& stg = lds.mainloop.stg;
|
||||
auto& s = lds.mainloop.s;
|
||||
unsigned int* myStg = (unsigned int*)(&stg[WVLDS * (threadIdx.y / GrpsShrB)]);
|
||||
__shared__ scalar_t s[max_lds_len - WvPrGrp * WVLDS / GrpsShrB];
|
||||
|
||||
#ifndef WVSPLITKRC_1KPASS
|
||||
constexpr int TUC_ = (THRDS * UNRL * A_CHUNK);
|
||||
@@ -1682,33 +1699,69 @@ __global__ void __launch_bounds__(WvPrGrp* THRDS)
|
||||
if (my_cntr + 1 == k_rnd) {
|
||||
cntr[adr_] = 0; // clear for next round
|
||||
if constexpr (DTRMNSTC) {
|
||||
constexpr uint32_t NT = N / NTILE / GrpsShrB;
|
||||
// The readback stages k-shard partials through LDS, which bounds how
|
||||
// many shards may be in flight at once.
|
||||
constexpr uint32_t LDS_F4 =
|
||||
max_lds_len * sizeof(scalar_t) / sizeof(float4);
|
||||
constexpr uint32_t KSBMAX = LDS_F4 / (THRDS * 4 * NT);
|
||||
static_assert(KSBMAX >= 1, "LDS cannot stage even one k-shard");
|
||||
const uint32_t KSB = (k_rnd < KSBMAX) ? k_rnd : KSBMAX;
|
||||
|
||||
// First batch is peeled; it is the only batch for most shapes.
|
||||
#pragma unroll
|
||||
for (int ks = 0; ks < k_rnd; ks++) {
|
||||
for (uint32_t nt = 0; nt < N / NTILE / GrpsShrB; nt++) {
|
||||
for (uint32_t kb = 0; kb < KSB; kb++) {
|
||||
for (uint32_t nt = 0; nt < NT; nt++) {
|
||||
int g_nindx =
|
||||
(nt * NTILE + (N / GrpsShrB) * (threadIdx.y % GrpsShrB)) / 4;
|
||||
int g_adr = g_mindx * 4 + 0 + M * g_nindx * 4;
|
||||
__builtin_amdgcn_global_load_lds(
|
||||
(float4*)(&glbl[g_adr + M * N * ks]),
|
||||
&(((float4*)s)[(threadIdx.y * THRDS) + ks * THRDS * 4 +
|
||||
nt * THRDS * 4 * k_rnd]),
|
||||
(float4*)(&glbl[g_adr + M * N * kb]),
|
||||
&(((float4*)lds.all)[(threadIdx.y * THRDS) + kb * THRDS * 4 +
|
||||
nt * THRDS * 4 * KSB]),
|
||||
16, 0, 0);
|
||||
}
|
||||
}
|
||||
if (BIAS)
|
||||
for (uint32_t nt = 0; nt < N / NTILE / GrpsShrB; nt++) {
|
||||
if (BIAS) // overlaps the DMAs above
|
||||
for (uint32_t nt = 0; nt < NT; nt++) {
|
||||
for (uint32_t j = 0; j < 4; j++) {
|
||||
int nindx = (j + (threadIdx.x / 16) * 4) + nt * NTILE +
|
||||
(N / GrpsShrB) * (threadIdx.y % GrpsShrB);
|
||||
biases[nt][j] = BIAS[(mindx % Bx) + (nindx % By) * Bx];
|
||||
}
|
||||
}
|
||||
asm volatile("s_waitcnt 0");
|
||||
for (int ks = 0; ks < k_rnd; ks++) {
|
||||
for (uint32_t nt = 0; nt < N / NTILE / GrpsShrB; nt++) {
|
||||
float4 eval = ((float4*)s)[(threadIdx.x + threadIdx.y * THRDS) +
|
||||
ks * THRDS * 4 + nt * THRDS * 4 * k_rnd];
|
||||
vals[nt].f4 += eval;
|
||||
asm volatile("s_waitcnt vmcnt(0)" ::: "memory");
|
||||
for (uint32_t kb = 0; kb < KSB; kb++) {
|
||||
for (uint32_t nt = 0; nt < NT; nt++) {
|
||||
vals[nt].f4 +=
|
||||
((float4*)lds.all)[(threadIdx.x + threadIdx.y * THRDS) +
|
||||
kb * THRDS * 4 + nt * THRDS * 4 * KSB];
|
||||
}
|
||||
}
|
||||
|
||||
for (uint32_t ks0 = KSB; ks0 < k_rnd; ks0 += KSB) {
|
||||
const uint32_t ksn = (k_rnd - ks0 < KSB) ? (k_rnd - ks0) : KSB;
|
||||
// prior reads must retire before the staging is overwritten
|
||||
asm volatile("s_waitcnt lgkmcnt(0)" ::: "memory");
|
||||
for (uint32_t kb = 0; kb < ksn; kb++) {
|
||||
for (uint32_t nt = 0; nt < NT; nt++) {
|
||||
int g_nindx =
|
||||
(nt * NTILE + (N / GrpsShrB) * (threadIdx.y % GrpsShrB)) / 4;
|
||||
int g_adr = g_mindx * 4 + 0 + M * g_nindx * 4;
|
||||
__builtin_amdgcn_global_load_lds(
|
||||
(float4*)(&glbl[g_adr + M * N * (ks0 + kb)]),
|
||||
&(((float4*)lds.all)[(threadIdx.y * THRDS) + kb * THRDS * 4 +
|
||||
nt * THRDS * 4 * KSB]),
|
||||
16, 0, 0);
|
||||
}
|
||||
}
|
||||
asm volatile("s_waitcnt vmcnt(0)" ::: "memory");
|
||||
for (uint32_t kb = 0; kb < ksn; kb++) {
|
||||
for (uint32_t nt = 0; nt < NT; nt++) {
|
||||
vals[nt].f4 +=
|
||||
((float4*)lds.all)[(threadIdx.x + threadIdx.y * THRDS) +
|
||||
kb * THRDS * 4 + nt * THRDS * 4 * KSB];
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -1771,7 +1824,7 @@ __global__ void wvSplitKrc_(const int actlN, const int K, const int Kap,
|
||||
torch::Tensor wvSplitKrc(const at::Tensor& in_a, const at::Tensor& in_b,
|
||||
const std::optional<at::Tensor>& in_bias,
|
||||
const int64_t CuCount) {
|
||||
int _DTRMNSTC = 1; // vllm::vllm_is_batch_invariant();
|
||||
static constexpr bool _DTRMNSTC = true;
|
||||
|
||||
auto M_in = in_b.size(0);
|
||||
auto N_in = in_a.size(0);
|
||||
@@ -1819,26 +1872,157 @@ torch::Tensor wvSplitKrc(const at::Tensor& in_a, const at::Tensor& in_b,
|
||||
|
||||
if (CuNeeded > CuCount) throw std::runtime_error("Invalid wvSplitKrc size");
|
||||
|
||||
// Can we increase SplitK by shrinking the K-shared to 256?
|
||||
int chunkk = (CuNeeded * 2 <= CuCount) ? 2 : 1;
|
||||
// Can we increase SplitK by shrinking the K-shard to 256? That doubles both
|
||||
// the CUs needed and the k-shards to read back, and the readback is serial in
|
||||
// the shard count, so it pays only up to a crossover. The crossover is not a
|
||||
// constant -- it falls as M grows -- so one cap cannot sit on it for every
|
||||
// shape; this value minimizes total time over the admitted shapes. Retune it
|
||||
// if the readback cost changes, and note the directions are not symmetric:
|
||||
// capping too high costs about twice what capping too low does.
|
||||
// Mirrored by rocm_unquantized_gemm_impl() in
|
||||
// vllm/model_executor/layers/utils.py; both must pick the same chunkk or the
|
||||
// host-side fit check bounds the wrong k_rnd.
|
||||
constexpr int64_t CHUNKK2_MAX_SHARDS = 11;
|
||||
const int64_t shards_chunkk2 = (K_in + 256 - 1) / 256; // 256 == 512 / 2
|
||||
const int chunkk = (N_p2 != 16 && CuNeeded * 2 <= CuCount &&
|
||||
shards_chunkk2 <= CHUNKK2_MAX_SHARDS)
|
||||
? 2
|
||||
: 1;
|
||||
|
||||
static torch::Tensor axl_glbl =
|
||||
torch::zeros(
|
||||
128 * 1024 * (_DTRMNSTC ? 12 : 1),
|
||||
torch::TensorOptions().dtype(torch::kFloat32).device(in_a.device()))
|
||||
.detach();
|
||||
static torch::Tensor axl_cntr =
|
||||
torch::zeros(
|
||||
128 * 1024 * (_DTRMNSTC ? 12 : 1) / 4,
|
||||
torch::TensorOptions().dtype(torch::kInt).device(in_a.device()))
|
||||
.detach();
|
||||
auto glbl = axl_glbl.data_ptr<float>();
|
||||
auto cntr = axl_cntr.data_ptr<int>();
|
||||
// One fp32 partial per (M, N, k-shard) must fit the split-K workspace.
|
||||
const int64_t k_rnd = (K_in + 512 / chunkk - 1) / (512 / chunkk);
|
||||
constexpr int64_t glbl_cap = 128 * 1024 * (_DTRMNSTC ? 12 : 1);
|
||||
const int64_t glbl_needed =
|
||||
_DTRMNSTC ? (int64_t)N_p2 * M_in * k_rnd : (int64_t)N_p2 * M_in;
|
||||
TORCH_CHECK(glbl_needed <= glbl_cap,
|
||||
"wvSplitKrc: split-K workspace too small", " (need ", glbl_needed,
|
||||
" > ", glbl_cap, " floats) for N=", N_in, " K=", K_in,
|
||||
" M=", M_in);
|
||||
|
||||
// Split-K workspaces: one pre-partitioned pool per device.
|
||||
// These must be zeroed before first use, and the kernel re-zeros them after
|
||||
// each use.
|
||||
// To avoid unnecessary overhead, we do not allocate them zeroed
|
||||
// per-invocation. However, a simple static allocation does not work with
|
||||
// multi-streams, because concurrent streams can alias into the same
|
||||
// workspace. Solution: pre-allocate kWvSlots = 8 slots per device, plus
|
||||
// kWvCaptureSlots reserved for capturing streams, and zero the whole pool
|
||||
// once at creation. On first use, a stream takes one of the slots if any are
|
||||
// still available. On subsequent uses, it re-uses the slot. If no slots are
|
||||
// available, fall back to allocating a zeroed one per call, which is safe but
|
||||
// adds overhead.
|
||||
// A capturing stream must neither allocate nor enqueue anything -- either
|
||||
// would be captured into the graph -- so it is only handed a reserved slot.
|
||||
struct WvSplitKrcPool {
|
||||
torch::Tensor glbl, cntr;
|
||||
int64_t glbl_stride = 0, cntr_stride = 0;
|
||||
int next_slot = 0;
|
||||
int next_capture_slot = 0;
|
||||
std::map<cudaStream_t, int> slot_of;
|
||||
std::map<cudaStream_t, int> capture_slot_of;
|
||||
};
|
||||
// mutex not strictly necessary if caller is always single-threaded (Python)
|
||||
// but this preserves Torch thread_local semantics for the C++ API
|
||||
static std::mutex wv_pool_mu;
|
||||
static std::map<int, WvSplitKrcPool> wv_pools;
|
||||
|
||||
float* glbl;
|
||||
int* cntr;
|
||||
// Fallback space
|
||||
torch::Tensor spill_glbl, spill_cntr;
|
||||
{
|
||||
std::lock_guard<std::mutex> wv_lk(wv_pool_mu);
|
||||
const int wv_dev = static_cast<int>(in_a.device().index());
|
||||
WvSplitKrcPool& P = wv_pools[wv_dev];
|
||||
constexpr int64_t wv_gf = 128 * 1024 * (_DTRMNSTC ? 12 : 1);
|
||||
constexpr int64_t wv_ci = wv_gf / 4;
|
||||
constexpr int64_t wv_all_slots = kWvSlots + kWvCaptureSlots;
|
||||
|
||||
// A stream whose capture was already invalidated reports an error; treat
|
||||
// that as not capturing, since the capture is lost either way.
|
||||
cudaStreamCaptureStatus capture_status = cudaStreamCaptureStatusNone;
|
||||
unsigned long long capture_id = 0;
|
||||
if (cudaStreamGetCaptureInfo(stream, &capture_status, &capture_id) !=
|
||||
cudaSuccess) {
|
||||
capture_status = cudaStreamCaptureStatusNone;
|
||||
}
|
||||
const bool wv_capturing = capture_status != cudaStreamCaptureStatusNone;
|
||||
|
||||
if (!P.glbl.defined()) {
|
||||
TORCH_CHECK(!wv_capturing,
|
||||
"wvSplitKrc: the split-K workspace pool cannot be allocated "
|
||||
"during graph capture. Call "
|
||||
"warmup_rocm_skinny_gemm_workspaces() "
|
||||
"(vllm/model_executor/layers/utils.py) eagerly first.");
|
||||
P.glbl_stride = wv_gf;
|
||||
P.cntr_stride = wv_ci;
|
||||
P.glbl = torch::zeros(wv_gf * wv_all_slots, torch::TensorOptions()
|
||||
.dtype(torch::kFloat32)
|
||||
.device(in_a.device()))
|
||||
.detach();
|
||||
P.cntr =
|
||||
torch::zeros(
|
||||
wv_ci * wv_all_slots,
|
||||
torch::TensorOptions().dtype(torch::kInt).device(in_a.device()))
|
||||
.detach();
|
||||
// The mutex orders host access to the map, not device execution, so it
|
||||
// alone would let another stream be handed a slot the fill above is
|
||||
// still clearing. Synchronizing here, inside the lock, is what makes
|
||||
// every slot safe to use without any further per-slot zeroing.
|
||||
const cudaError_t wv_err = cudaStreamSynchronize(stream);
|
||||
TORCH_CHECK(wv_err == cudaSuccess,
|
||||
"wvSplitKrc: failed to zero the split-K workspace pool: ",
|
||||
cudaGetErrorString(wv_err));
|
||||
}
|
||||
|
||||
int wv_slot = -1;
|
||||
if (wv_capturing) {
|
||||
// One workspace per capturing stream, so graphs captured on the same
|
||||
// stream share it -- safe because at most one of them replays at a time
|
||||
// (vLLM captures one graph per padded batch size, and runs one per step).
|
||||
auto cap_it = P.capture_slot_of.find(stream);
|
||||
if (cap_it == P.capture_slot_of.end()) {
|
||||
TORCH_CHECK(P.next_capture_slot < kWvCaptureSlots,
|
||||
"wvSplitKrc: no free capture workspace slot; more than ",
|
||||
kWvCaptureSlots,
|
||||
" distinct streams have captured a graph containing "
|
||||
"wvSplitKrc. Raise kWvCaptureSlots in "
|
||||
"csrc/rocm/skinny_gemms.cu.");
|
||||
cap_it = P.capture_slot_of
|
||||
.emplace(stream, static_cast<int>(kWvSlots) +
|
||||
P.next_capture_slot++)
|
||||
.first;
|
||||
}
|
||||
wv_slot = cap_it->second;
|
||||
} else {
|
||||
auto slot_it = P.slot_of.find(stream);
|
||||
if (slot_it != P.slot_of.end()) {
|
||||
wv_slot = slot_it->second;
|
||||
} else if (P.next_slot < kWvSlots) {
|
||||
wv_slot = P.next_slot++;
|
||||
P.slot_of.emplace(stream, wv_slot);
|
||||
}
|
||||
}
|
||||
|
||||
if (wv_slot >= 0) {
|
||||
glbl = P.glbl.data_ptr<float>() + wv_slot * P.glbl_stride;
|
||||
cntr = P.cntr.data_ptr<int>() + wv_slot * P.cntr_stride;
|
||||
} else {
|
||||
spill_glbl = torch::zeros(
|
||||
wv_gf,
|
||||
torch::TensorOptions().dtype(torch::kFloat32).device(in_a.device()));
|
||||
spill_cntr = torch::zeros(
|
||||
wv_ci,
|
||||
torch::TensorOptions().dtype(torch::kInt).device(in_a.device()));
|
||||
glbl = spill_glbl.data_ptr<float>();
|
||||
cntr = spill_cntr.data_ptr<int>();
|
||||
}
|
||||
}
|
||||
|
||||
#define WVSPLITKrc(_N, _GrpsShrB, _CHUNKK) \
|
||||
{ \
|
||||
dim3 block(64, 4); \
|
||||
if (_DTRMNSTC) \
|
||||
if constexpr (_DTRMNSTC) \
|
||||
wvSplitKrc_<fptype, 64, 16, 4, 8, 1, _N, _GrpsShrB, _CHUNKK, 1> \
|
||||
<<<grid, block, 0, stream>>>(N_in, K_in, Kap_in, M_in, Bx_in, By_in, \
|
||||
af4, bf4, biasf4, glbl, cntr, c, \
|
||||
|
||||
Reference in New Issue
Block a user