Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
163 lines
8.1 KiB
Plaintext
163 lines
8.1 KiB
Plaintext
// GPU memory bandwidth and tensor-core GEMM throughput for the model's real shapes (Qwen3.8-Flash-Next: hidden 2560,
|
|
// 4 hyper-connection streams of 2560 (10240), expert intermediate 640).
|
|
// mb_gpu [--quick]
|
|
// RESULT gpubw kind=... gbps=...
|
|
// RESULT gemm name=... prec=bf16|fp16|int8 T=... K=... N=... us=... tflops=... frac_of_peak=...
|
|
#include <cublas_v2.h>
|
|
#include <cuda_fp16.h>
|
|
#include <cuda_runtime.h>
|
|
#include "mb_common.h"
|
|
|
|
#define CB(x) do { cublasStatus_t s_ = (x); if (s_ != CUBLAS_STATUS_SUCCESS) { std::fprintf(stderr, "cuBLAS error %d at %s:%d\n", (int)s_, __FILE__, __LINE__); } } while (0)
|
|
|
|
__global__ void k_read(const float4* __restrict__ p, size_t n, float* out) {
|
|
float acc = 0;
|
|
for (size_t i = blockIdx.x * (size_t)blockDim.x + threadIdx.x; i < n; i += (size_t)gridDim.x * blockDim.x) {
|
|
float4 v = p[i];
|
|
acc += v.x + v.y + v.z + v.w;
|
|
}
|
|
if (acc == 12345.678f) *out = acc;
|
|
}
|
|
__global__ void k_write(float4* __restrict__ p, size_t n) {
|
|
for (size_t i = blockIdx.x * (size_t)blockDim.x + threadIdx.x; i < n; i += (size_t)gridDim.x * blockDim.x) p[i] = make_float4(1, 2, 3, 4);
|
|
}
|
|
__global__ void k_copy(const float4* __restrict__ a, float4* __restrict__ b, size_t n) {
|
|
for (size_t i = blockIdx.x * (size_t)blockDim.x + threadIdx.x; i < n; i += (size_t)gridDim.x * blockDim.x) b[i] = a[i];
|
|
}
|
|
|
|
template <typename F>
|
|
static double time_ms(F f, int iters) {
|
|
cudaEvent_t a, b;
|
|
CK(cudaEventCreate(&a));
|
|
CK(cudaEventCreate(&b));
|
|
f();
|
|
CK(cudaDeviceSynchronize());
|
|
CK(cudaEventRecord(a));
|
|
for (int i = 0; i < iters; ++i) f();
|
|
CK(cudaEventRecord(b));
|
|
CK(cudaEventSynchronize(b));
|
|
float ms;
|
|
CK(cudaEventElapsedTime(&ms, a, b));
|
|
CK(cudaEventDestroy(a));
|
|
CK(cudaEventDestroy(b));
|
|
return ms / iters;
|
|
}
|
|
|
|
static double g_peak_bf16 = 0, g_peak_int8 = 0;
|
|
|
|
// Y[T x N] = X[T x K] * W[N x K]^T. `nw` distinct weight matrices are cycled so weights stream from memory like the model's.
|
|
static void gemm_case(cublasHandle_t h, const char* name, const char* prec, int T, int K, int N, int nw, void* X, void* W, void* Y, bool record_peak) {
|
|
const float one = 1.f, zero = 0.f;
|
|
const int32_t ione = 1, izero = 0;
|
|
const bool i8 = !std::strcmp(prec, "int8"), f16 = !std::strcmp(prec, "fp16");
|
|
const size_t welem = (size_t)N * K * (i8 ? 1 : 2);
|
|
int w = 0;
|
|
auto call = [&]() {
|
|
char* Wp = (char*)W + (size_t)(w++ % nw) * welem;
|
|
if (i8)
|
|
CB(cublasGemmEx(h, CUBLAS_OP_T, CUBLAS_OP_N, N, T, K, &ione, Wp, CUDA_R_8I, K, X, CUDA_R_8I, K, &izero, Y, CUDA_R_32I, N,
|
|
CUBLAS_COMPUTE_32I, CUBLAS_GEMM_DEFAULT_TENSOR_OP));
|
|
else
|
|
CB(cublasGemmEx(h, CUBLAS_OP_T, CUBLAS_OP_N, N, T, K, &one, Wp, f16 ? CUDA_R_16F : CUDA_R_16BF, K, X, f16 ? CUDA_R_16F : CUDA_R_16BF, K,
|
|
&zero, Y, CUDA_R_32F, N, CUBLAS_COMPUTE_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP));
|
|
};
|
|
const double flops = 2.0 * T * K * N;
|
|
int iters = (int)std::max(5.0, std::min(2000.0, 0.15 / std::max(flops / 200e12, 5e-6)));
|
|
double ms = time_ms(call, iters);
|
|
const double tf = flops / (ms * 1e-3) / 1e12;
|
|
if (record_peak) {
|
|
if (i8) g_peak_int8 = std::max(g_peak_int8, tf);
|
|
else g_peak_bf16 = std::max(g_peak_bf16, tf);
|
|
}
|
|
const double wbytes = (double)welem;
|
|
std::printf("RESULT gemm name=%s prec=%s T=%d K=%d N=%d us=%.1f tflops=%.1f weight_gbps=%.0f\n", name, prec, T, K, N, ms * 1e3, tf, wbytes / (ms * 1e-3) / 1e9);
|
|
std::fflush(stdout);
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
bool quick = argc > 1 && !std::strcmp(argv[1], "--quick");
|
|
cudaDeviceProp p;
|
|
CK(cudaGetDeviceProperties(&p, 0));
|
|
int clk = 0, mclk = 0, bus = 0;
|
|
cudaDeviceGetAttribute(&clk, cudaDevAttrClockRate, 0);
|
|
cudaDeviceGetAttribute(&mclk, cudaDevAttrMemoryClockRate, 0);
|
|
cudaDeviceGetAttribute(&bus, cudaDevAttrGlobalMemoryBusWidth, 0);
|
|
std::printf("# device %s sm_%d%d SMs=%d L2=%d MiB sm_clock=%d MHz mem_clock=%d MHz bus=%d bit spec_bw~%.0f GB/s (DDR factor 2; GDDR7 may be 4x)\n", p.name,
|
|
p.major, p.minor, p.multiProcessorCount, p.l2CacheSize >> 20, clk / 1000, mclk / 1000, bus, 2.0 * mclk * 1e3 * bus / 8 / 1e9);
|
|
|
|
// ---- memory bandwidth
|
|
const size_t gb = 1ull << 30;
|
|
float4 *a, *b;
|
|
float* o;
|
|
CK(cudaMalloc(&a, gb));
|
|
CK(cudaMalloc(&b, gb));
|
|
CK(cudaMalloc(&o, 4));
|
|
CK(cudaMemset(a, 1, gb));
|
|
CK(cudaMemset(b, 2, gb));
|
|
const size_t n = gb / sizeof(float4);
|
|
const int sms = p.multiProcessorCount;
|
|
for (int blocks_per_sm : {4, 8, 16}) {
|
|
const int blocks = sms * blocks_per_sm;
|
|
double ms = time_ms([&] { k_read<<<blocks, 256>>>(a, n, o); }, 20);
|
|
std::printf("RESULT gpubw kind=read_kernel blocks_per_sm=%d gbps=%.0f\n", blocks_per_sm, gb / (ms * 1e-3) / 1e9);
|
|
}
|
|
{
|
|
double ms = time_ms([&] { k_write<<<sms * 8, 256>>>(b, n); }, 20);
|
|
std::printf("RESULT gpubw kind=write_kernel gbps=%.0f\n", gb / (ms * 1e-3) / 1e9);
|
|
ms = time_ms([&] { k_copy<<<sms * 8, 256>>>(a, b, n); }, 20);
|
|
std::printf("RESULT gpubw kind=copy_kernel_rw gbps=%.0f\n", 2.0 * gb / (ms * 1e-3) / 1e9);
|
|
ms = time_ms([&] { CK(cudaMemcpyAsync(b, a, gb, cudaMemcpyDeviceToDevice)); }, 20);
|
|
std::printf("RESULT gpubw kind=memcpy_d2d_rw gbps=%.0f\n", 2.0 * gb / (ms * 1e-3) / 1e9);
|
|
// small reads that fit L2 (re-read): the KV / activation working set
|
|
const size_t small = 16ull << 20;
|
|
ms = time_ms([&] { k_read<<<sms * 8, 256>>>(a, small / sizeof(float4), o); }, 200);
|
|
std::printf("RESULT gpubw kind=read_16MiB_L2 gbps=%.0f\n", small / (ms * 1e-3) / 1e9);
|
|
}
|
|
CK(cudaFree(a));
|
|
CK(cudaFree(b));
|
|
|
|
// ---- GEMMs
|
|
cublasHandle_t h;
|
|
CB(cublasCreate(&h));
|
|
const size_t wmax = 1536ull << 20; // 1.5 GiB of weights cycled so they come from memory, not L2
|
|
const size_t xmax = 16384ull * 12288ull * 2;
|
|
void *W, *X, *Y;
|
|
CK(cudaMalloc(&W, wmax));
|
|
CK(cudaMalloc(&X, xmax));
|
|
CK(cudaMalloc(&Y, 16384ull * 12288ull * 4));
|
|
CK(cudaMemset(W, 0x11, wmax));
|
|
CK(cudaMemset(X, 0x11, xmax));
|
|
struct Sh { const char* name; int K, N, count; };
|
|
const Sh dense[] = {{"gdn_qkv", 2560, 10240, 36}, {"gdn_gate_z", 2560, 6144, 36}, {"gdn_out", 6144, 2560, 36}, {"qsa_q", 2560, 12288, 12},
|
|
{"qsa_out", 6144, 2560, 12}, {"qsa_kv", 2560, 512, 24}, {"shexp_gateup", 2560, 1280, 48}, {"shexp_down", 640, 2560, 48},
|
|
{"hc_down", 10240, 320, 96}, {"hc_up", 320, 10240, 96}, {"router", 2560, 512, 48}, {"head", 2560, 248320, 1}};
|
|
for (const char* prec : {"bf16", "fp16", "int8"}) {
|
|
for (int T : {8192, 2048}) {
|
|
if (quick && T == 2048) continue;
|
|
if (!std::strcmp(prec, "fp16") && T == 2048) continue;
|
|
for (const Sh& s : dense) {
|
|
if (!std::strcmp(s.name, "head")) continue;
|
|
const size_t welem = (size_t)s.N * s.K * (!std::strcmp(prec, "int8") ? 1 : 2);
|
|
const int nw = (int)std::max<size_t>(1, std::min<size_t>(64, wmax / welem));
|
|
gemm_case(h, s.name, prec, T, s.K, s.N, nw, X, W, Y, s.N >= 2560 && s.K >= 2560 && T == 8192);
|
|
}
|
|
}
|
|
// big square-ish case for the headline peak
|
|
gemm_case(h, "square_8192", prec, 8192, 8192, 8192, 1, X, W, Y, true);
|
|
}
|
|
// per-expert shapes at the group sizes of a prefill chunk (avg tokens per expert = T*10/512)
|
|
for (const char* prec : {"bf16", "int8"})
|
|
for (int te : {640, 160, 40, 10}) {
|
|
gemm_case(h, "expert_gateup", prec, te, 2560, 1280, 64, X, W, Y, false);
|
|
gemm_case(h, "expert_down", prec, te, 640, 2560, 64, X, W, Y, false);
|
|
}
|
|
// decode-sized batches against bf16 weights (memory bound): what a window of 1-6 rows costs per matrix
|
|
for (int T : {1, 3, 6, 12})
|
|
for (const Sh& s : {Sh{"gdn_qkv", 2560, 10240, 36}, Sh{"head", 2560, 248320, 1}}) {
|
|
const size_t welem = (size_t)s.N * s.K * 2;
|
|
gemm_case(h, s.name, "bf16", T, s.K, s.N, (int)std::max<size_t>(1, std::min<size_t>(8, wmax / welem)), X, W, Y, false);
|
|
}
|
|
std::printf("RESULT peak bf16_tflops=%.1f int8_tops=%.1f\n", g_peak_bf16, g_peak_int8);
|
|
return 0;
|
|
}
|