perf(serve): alternative PCIe staging copy kernels with several loads in flight (LELEKA_PCIE_KERNEL / LELEKA_PCIE_GRID, off by default)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
25b3e3284a
commit
095dd67bba
@@ -188,6 +188,50 @@ pub(crate) static PCIE: AssetSpec = AssetSpec {
|
||||
unsealed: "Original verifier asset unsealed: original_verify_window_pcie.ptx (PCIe span staging copy)",
|
||||
};
|
||||
|
||||
/// Alternative PCIe staging copy kernels (`original_verify_window_pcie_fast.cu`, claude/decode-parity): several 16-byte
|
||||
/// loads in flight per thread. Loaded and launched only when `LELEKA_PCIE_KERNEL` names one (no sm_86 companion yet).
|
||||
pub(crate) static PCIE_FAST: AssetSpec = AssetSpec {
|
||||
name: "original_verify_window_pcie_fast.ptx",
|
||||
ptx: include_bytes!("original_verify_window_pcie_fast.ptx"),
|
||||
sha: [116, 7, 133, 159, 141, 136, 129, 102, 63, 58, 100, 110, 190, 40, 140, 49, 19, 140, 206, 50, 39, 193, 197, 217, 226, 229, 146, 100, 142, 215, 122, 234],
|
||||
entries: &[
|
||||
"verify_fetch_spans_k1u4_kernel",
|
||||
"verify_fetch_spans_k1u8_kernel",
|
||||
"verify_fetch_spans_k1u16_kernel",
|
||||
"verify_fetch_spans_k2u4_kernel",
|
||||
"verify_fetch_spans_k2u8_kernel",
|
||||
"verify_fetch_spans_k2u16_kernel",
|
||||
],
|
||||
unsealed: "Original verifier asset unsealed: original_verify_window_pcie_fast.ptx (alternative PCIe staging copy)",
|
||||
};
|
||||
|
||||
/// `LELEKA_PCIE_KERNEL` = `k1u4|k1u8|k1u16|k2u4|k2u8|k2u16` selects an alternative staging copy kernel of the copy-kernel
|
||||
/// PCIe mode (default: the sealed `verify_fetch_spans_kernel`); `LELEKA_PCIE_GRID=BLOCKSxTHREADS` its launch shape
|
||||
/// (default 384x256, the Original's `48 * 8` blocks of 256).
|
||||
pub(crate) fn pcie_fast_kernel() -> Option<(&'static str, [u32; 3], [u32; 3])> {
|
||||
static CHOICE: std::sync::OnceLock<Option<(&'static str, [u32; 3], [u32; 3])>> = std::sync::OnceLock::new();
|
||||
*CHOICE.get_or_init(|| {
|
||||
let name = match std::env::var("LELEKA_PCIE_KERNEL").ok()?.as_str() {
|
||||
"k1u4" => "verify_fetch_spans_k1u4_kernel",
|
||||
"k1u8" => "verify_fetch_spans_k1u8_kernel",
|
||||
"k1u16" => "verify_fetch_spans_k1u16_kernel",
|
||||
"k2u4" => "verify_fetch_spans_k2u4_kernel",
|
||||
"k2u8" => "verify_fetch_spans_k2u8_kernel",
|
||||
"k2u16" => "verify_fetch_spans_k2u16_kernel",
|
||||
_ => return None,
|
||||
};
|
||||
let (blocks, threads) = std::env::var("LELEKA_PCIE_GRID")
|
||||
.ok()
|
||||
.and_then(|v| {
|
||||
let (b, t) = v.split_once('x')?;
|
||||
Some((b.parse::<u32>().ok()?, t.parse::<u32>().ok()?))
|
||||
})
|
||||
.filter(|&(b, t)| (1..=4096).contains(&b) && (32..=1024).contains(&t) && t % 32 == 0)
|
||||
.unwrap_or((48 * 8, 256));
|
||||
Some((name, [blocks, 1, 1], [threads, 1, 1]))
|
||||
})
|
||||
}
|
||||
|
||||
/// Every module the MoE producers may launch from (the lead loads them all before capture).
|
||||
pub(crate) fn assets() -> Vec<&'static AssetSpec> {
|
||||
vec![
|
||||
@@ -204,6 +248,9 @@ pub(crate) fn assets() -> Vec<&'static AssetSpec> {
|
||||
&EXPERTS,
|
||||
&PCIE,
|
||||
]
|
||||
.into_iter()
|
||||
.chain(pcie_fast_kernel().map(|_| &PCIE_FAST))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Kernels of the MoE family that need `MAX_DYNAMIC_SHARED_SIZE` raised before capture: none (the generic
|
||||
@@ -850,6 +897,12 @@ mod tests {
|
||||
|
||||
const BASE: u64 = 0x2000_0000;
|
||||
|
||||
#[test]
|
||||
fn the_alternative_pcie_staging_copy_kernels_are_sealed() {
|
||||
PCIE_FAST.assert_sealed_and_fail_closed();
|
||||
assert!(PCIE_FAST.sealed().is_ok());
|
||||
}
|
||||
|
||||
fn map() -> LayoutMap {
|
||||
LayoutMap::new(BASE, test_geometry())
|
||||
}
|
||||
|
||||
@@ -507,11 +507,15 @@ impl WindowOps for OriginalVerifyWindowOps<'_, '_, '_, '_> {
|
||||
let source = self.plan_word(rows.group, layout.ptr2)?;
|
||||
let count = self.plan_word(rows.group, layout.counts + 2)?;
|
||||
let words = |bytes: usize| A::I64((bytes / 16) as i64);
|
||||
let (spec, entry, grid, block) = match moe::pcie_fast_kernel() {
|
||||
Some((entry, grid, block)) => (&moe::PCIE_FAST, entry, grid, block),
|
||||
None => (&moe::PCIE, "verify_fetch_spans_kernel", [48 * 8, 1, 1], [256, 1, 1]),
|
||||
};
|
||||
self.launch(vec![Launch::new(
|
||||
&moe::PCIE,
|
||||
"verify_fetch_spans_kernel",
|
||||
[48 * 8, 1, 1],
|
||||
[256, 1, 1],
|
||||
spec,
|
||||
entry,
|
||||
grid,
|
||||
block,
|
||||
0,
|
||||
vec![A::P(source), A::P(count), A::P(stage), words(gate), words(up), words(down)],
|
||||
)?])?;
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
// Alternative PCIe staging copy kernels of the verify window (claude/decode-parity; `LELEKA_PCIE_KERNEL`), same
|
||||
// arguments and result as `verify_fetch_spans_kernel` (original_verify_window_pcie.cu): `src[3 q + r]` is span r of PCIe
|
||||
// group q (device alias of the registered bank), `w0`/`w1`/`w2` their lengths in 16-byte words, staging slot q receives
|
||||
// gate|up|down contiguously at `dst + q * (w0 + w1 + w2)`.
|
||||
//
|
||||
// The sealed kernel reads one 16-byte word per thread per iteration behind 64-bit divides; a read of registered host
|
||||
// memory costs a PCIe round trip, so a thread should have several loads in flight before it stores. These kernels issue
|
||||
// U independent loads per thread first (`__ldcs`: streaming, nothing is reused from the L1/L2).
|
||||
// k1: spans in order, a grid-stride loop over U*blockDim-word chunks of one span at a time
|
||||
// k2: the chunks of every span are flattened into work items distributed over the grid
|
||||
#include <cstdint>
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
template <int U>
|
||||
__device__ __forceinline__ void fetch_k1(const unsigned long long* __restrict__ src, const int32_t* __restrict__ n,
|
||||
uint4* __restrict__ dst, long long w0, long long w1, long long w2) {
|
||||
const long long per = w0 + w1 + w2;
|
||||
const long long wsz[3] = {w0, w1, w2};
|
||||
const int spans = *n * 3;
|
||||
const long long chunk = (long long) U * blockDim.x;
|
||||
for (int s = 0; s < spans; ++s) {
|
||||
const int q = s / 3, r = s - 3 * q;
|
||||
const long long w = wsz[r];
|
||||
const long long base_off = (r == 0) ? 0 : (r == 1 ? w0 : w0 + w1);
|
||||
const uint4* sp = (const uint4*) src[s];
|
||||
uint4* dp = dst + (long long) q * per + base_off;
|
||||
for (long long c0 = (long long) blockIdx.x * chunk; c0 < w; c0 += (long long) gridDim.x * chunk) {
|
||||
uint4 v[U];
|
||||
#pragma unroll
|
||||
for (int u = 0; u < U; ++u) {
|
||||
const long long i = c0 + (long long) u * blockDim.x + threadIdx.x;
|
||||
if (i < w) v[u] = __ldcs(sp + i);
|
||||
}
|
||||
#pragma unroll
|
||||
for (int u = 0; u < U; ++u) {
|
||||
const long long i = c0 + (long long) u * blockDim.x + threadIdx.x;
|
||||
if (i < w) dp[i] = v[u];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <int U>
|
||||
__device__ __forceinline__ void fetch_k2(const unsigned long long* __restrict__ src, const int32_t* __restrict__ n,
|
||||
uint4* __restrict__ dst, long long w0, long long w1, long long w2) {
|
||||
const long long per = w0 + w1 + w2;
|
||||
const int chunk = U * blockDim.x;
|
||||
const int c0 = (int) ((w0 + chunk - 1) / chunk), c1 = (int) ((w1 + chunk - 1) / chunk), c2 = (int) ((w2 + chunk - 1) / chunk);
|
||||
const int per_items = c0 + c1 + c2;
|
||||
const int items = *n * per_items;
|
||||
for (int item = blockIdx.x; item < items; item += gridDim.x) {
|
||||
const int q = item / per_items;
|
||||
int rem = item - q * per_items;
|
||||
int r = 0;
|
||||
long long base = 0, w = w0;
|
||||
if (rem >= c0) {
|
||||
rem -= c0;
|
||||
r = 1;
|
||||
base = w0;
|
||||
w = w1;
|
||||
if (rem >= c1) {
|
||||
rem -= c1;
|
||||
r = 2;
|
||||
base = w0 + w1;
|
||||
w = w2;
|
||||
}
|
||||
}
|
||||
const uint4* sp = (const uint4*) src[3 * q + r];
|
||||
uint4* dp = dst + (long long) q * per + base;
|
||||
const long long start = (long long) rem * chunk;
|
||||
uint4 v[U];
|
||||
#pragma unroll
|
||||
for (int u = 0; u < U; ++u) {
|
||||
const long long i = start + (long long) u * blockDim.x + threadIdx.x;
|
||||
if (i < w) v[u] = __ldcs(sp + i);
|
||||
}
|
||||
#pragma unroll
|
||||
for (int u = 0; u < U; ++u) {
|
||||
const long long i = start + (long long) u * blockDim.x + threadIdx.x;
|
||||
if (i < w) dp[i] = v[u];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define FETCH_ENTRY(NAME, FN, U) \
|
||||
extern "C" __global__ void NAME(const unsigned long long* __restrict__ src, const int32_t* __restrict__ n, \
|
||||
uint4* __restrict__ dst, long long w0, long long w1, long long w2) { \
|
||||
FN<U>(src, n, dst, w0, w1, w2); \
|
||||
}
|
||||
|
||||
FETCH_ENTRY(verify_fetch_spans_k1u4_kernel, fetch_k1, 4)
|
||||
FETCH_ENTRY(verify_fetch_spans_k1u8_kernel, fetch_k1, 8)
|
||||
FETCH_ENTRY(verify_fetch_spans_k1u16_kernel, fetch_k1, 16)
|
||||
FETCH_ENTRY(verify_fetch_spans_k2u4_kernel, fetch_k2, 4)
|
||||
FETCH_ENTRY(verify_fetch_spans_k2u8_kernel, fetch_k2, 8)
|
||||
FETCH_ENTRY(verify_fetch_spans_k2u16_kernel, fetch_k2, 16)
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user