Files
leleka/tools/microbench/mb_h2d2.cu
T

158 lines
7.6 KiB
Plaintext

// How to copy routed experts from a pinned bank: piece alignment, run length, pitched copies, batch API.
// mb_h2d2 [--gib 2]
// The bank holds, per layer and per role (gate, up, down), 512 experts back to back (index.txt / native_experts.txt), so the
// expert pieces of one layer are NOT contiguous per expert but consecutive experts of one role ARE contiguous. Piece sizes below are
// layer 2's (gate = up = 531,730 B at an arbitrary byte alignment, down = 907,740 B; blob 1,971,200 B).
// RESULT h2d2 variant=... experts_per_call=... gbps=... host_us_per_expert=...
#include <cuda_runtime.h>
#include "mb_common.h"
static const size_t GATE = 531730, UP = 531730, DOWN = 907740;
static size_t up64(size_t x) { return (x + 63) & ~(size_t)63; }
int main(int argc, char** argv) {
double gib = 2;
for (int i = 1; i < argc; ++i) if (!std::strcmp(argv[i], "--gib") && i + 1 < argc) gib = std::atof(argv[++i]);
const size_t bytes = (size_t)(gib * (1ull << 30));
if (!host_alloc_allowed(bytes)) return 3;
uint8_t* pin;
CK(cudaHostAlloc((void**)&pin, bytes, cudaHostAllocPortable));
std::memset(pin, 3, bytes);
// bank layout: three role arrays of 512 experts each; unaligned strides exactly like the GGUF tensors
const size_t role_bytes[3] = {512 * GATE, 512 * UP, 512 * DOWN};
const size_t role_off[3] = {1 << 20 | 18, (1 << 20 | 18) + role_bytes[0] + 4096, (1 << 20 | 18) + role_bytes[0] + role_bytes[1] + 8192};
const size_t piece[3] = {GATE, UP, DOWN};
uint8_t* dev;
CK(cudaMalloc(&dev, 1ull << 30));
cudaStream_t st;
CK(cudaStreamCreateWithFlags(&st, cudaStreamNonBlocking));
cudaDeviceProp p;
CK(cudaGetDeviceProperties(&p, 0));
std::printf("# device %s, bank %.1f GiB pinned\n", p.name, gib);
const int total_experts = 4096; // ~8 GB moved per variant run
auto report = [&](const char* name, int per_call, double secs, double host_s, size_t moved) {
std::printf("RESULT h2d2 variant=%s experts_per_call=%d gbps=%.1f host_us_per_expert=%.2f\n", name, per_call, moved / secs / 1e9, host_s / total_experts * 1e6);
std::fflush(stdout);
};
// dst layout: expert e of a group at slot e*stride; three pieces back to back inside the slot (the MMQ group-slot shape)
for (int aligned = 0; aligned < 3; ++aligned) { // 0 raw offsets+sizes, 1 sizes rounded to 64 B (src+dst), 2 4 KiB-aligned pieces
const char* an[] = {"raw", "round64", "round4k"};
for (int k : {1, 4, 16}) { // experts per "group" of calls
std::vector<double> t, th;
size_t moved = 0;
for (int rep = 0; rep < 3; ++rep) {
CK(cudaDeviceSynchronize());
const double t0 = now_s();
uint64_t rng = 1234567 + rep;
moved = 0;
for (int e0 = 0; e0 < total_experts; e0 += k) {
rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17;
const int base = (int)(rng % (512 - k)); // k consecutive experts (stream-all order within a layer)
for (int r = 0; r < 3; ++r) {
size_t sz = piece[r] * k, off = role_off[r] + (size_t)base * piece[r];
if (aligned == 1) { sz = up64(sz); off &= ~(size_t)63; }
if (aligned == 2) { sz = (sz + 4095) & ~(size_t)4095; off &= ~(size_t)4095; }
CK(cudaMemcpyAsync(dev + ((size_t)(e0 % 8) * 3 + r) * (16u << 20), pin + off, sz, cudaMemcpyHostToDevice, st));
moved += sz;
}
}
const double te = now_s();
CK(cudaStreamSynchronize(st));
const double t1 = now_s();
t.push_back(t1 - t0);
th.push_back(te - t0);
}
char name[64];
std::snprintf(name, sizeof name, "pieces3_%s_run%d", an[aligned], k);
report(name, k, median(t), median(th), moved);
}
}
// pitched copy: k experts' gate pieces into slots of pitch (GATE+UP+DOWN)
for (int k : {4, 16}) {
std::vector<double> t;
size_t moved = 0;
for (int rep = 0; rep < 3; ++rep) {
CK(cudaDeviceSynchronize());
const double t0 = now_s();
moved = 0;
for (int e0 = 0; e0 < total_experts; e0 += k) {
const int base = (e0 * 7) % (512 - k);
for (int r = 0; r < 3; ++r) {
CK(cudaMemcpy2DAsync(dev + (size_t)r * (40u << 20), GATE + UP + DOWN, pin + role_off[r] + (size_t)base * piece[r], piece[r], piece[r], k,
cudaMemcpyHostToDevice, st));
moved += piece[r] * k;
}
}
CK(cudaStreamSynchronize(st));
t.push_back(now_s() - t0);
}
char name[64];
std::snprintf(name, sizeof name, "pitched2d_run%d", k);
report(name, k, median(t), 0, moved);
}
// batch API: all pieces of k separate experts in one call (48 descriptors for k = 16)
for (int k : {4, 16}) {
std::vector<double> t, th;
size_t moved = 0;
bool ok = true;
for (int rep = 0; rep < 3 && ok; ++rep) {
CK(cudaDeviceSynchronize());
const double t0 = now_s();
uint64_t rng = 99 + rep;
moved = 0;
for (int e0 = 0; e0 < total_experts && ok; e0 += k) {
std::vector<void*> d;
std::vector<const void*> s;
std::vector<size_t> n;
for (int j = 0; j < k; ++j) {
rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17;
const int e = (int)(rng % 512);
for (int r = 0; r < 3; ++r) {
d.push_back(dev + ((size_t)j * 3 + r) * (4u << 20));
s.push_back(pin + role_off[r] + (size_t)e * piece[r]);
n.push_back(piece[r]);
moved += piece[r];
}
}
cudaMemcpyAttributes at = {};
at.srcAccessOrder = cudaMemcpySrcAccessOrderStream;
size_t idx = 0;
if (!CKS(cudaMemcpyBatchAsync(d.data(), s.data(), n.data(), d.size(), &at, &idx, 1, st))) ok = false;
}
const double te = now_s();
CK(cudaStreamSynchronize(st));
t.push_back(now_s() - t0);
th.push_back(te - t0);
}
char name[64];
std::snprintf(name, sizeof name, "batch_api_pieces3_scattered");
if (ok) report(name, k, median(t), median(th), moved);
}
// scattered experts (random ids, as decode misses), one memcpy per piece, for the reference
{
std::vector<double> t, th;
size_t moved = 0;
for (int rep = 0; rep < 3; ++rep) {
CK(cudaDeviceSynchronize());
const double t0 = now_s();
uint64_t rng = 7 + rep;
moved = 0;
for (int i = 0; i < total_experts; ++i) {
rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17;
const int e = (int)(rng % 512);
for (int r = 0; r < 3; ++r) {
CK(cudaMemcpyAsync(dev + ((size_t)(i % 16) * 3 + r) * (4u << 20), pin + role_off[r] + (size_t)e * piece[r], piece[r], cudaMemcpyHostToDevice, st));
moved += piece[r];
}
}
const double te = now_s();
CK(cudaStreamSynchronize(st));
t.push_back(now_s() - t0);
th.push_back(te - t0);
}
report("pieces3_scattered_raw", 1, median(t), median(th), moved);
}
return 0;
}