okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Techanneldocs + bench: the Tesla P40 (sm_61) profile, the four changes and their measurements5025324c2

main
5.2 KiB86 linesraw
1// gemm_sweep.cu - P40: cublasGemmEx (FP16 operands, FP32 sums) against cublasSgemm on FP32 copies, a sweep over T for
2// the dense prompt projections' shapes: time of each and whether the two outputs are bitwise equal.
3//   nvcc -O3 -arch=sm_61 gemm_sweep.cu -o gemm_sweep -lcublas && ./gemm_sweep
4#include <cublas_v2.h>
5#include <cuda_fp16.h>
6#include <cuda_runtime.h>
7#include <cmath>
8#include <cstdio>
9#include <cstdlib>
10#include <cstring>
11#include <vector>
12
13__global__ void fill_f16(__half* p, long n, unsigned seed, float scale) {
14    long i = (long) blockIdx.x * blockDim.x + threadIdx.x;
15    if (i >= n) return;
16    unsigned x = (unsigned) i * 2654435761u + seed; x ^= x >> 16; x *= 2246822519u; x ^= x >> 13; x *= 3266489917u; x ^= x >> 16;
17    p[i] = __float2half(((float) (x & 0xffff) / 32768.0f - 1.0f) * scale);
18}
19__global__ void f16_to_f32(const __half* in, float* out, long n) {
20    long i = (long) blockIdx.x * blockDim.x + threadIdx.x;
21    if (i < n) out[i] = __half2float(in[i]);
22}
23__global__ void count_diff(const unsigned* a, const unsigned* b, long n, unsigned* out) {
24    long i = (long) blockIdx.x * blockDim.x + threadIdx.x;
25    if (i < n && a[i] != b[i]) atomicAdd(out, 1u);
26}
27static double ms(cudaEvent_t a, cudaEvent_t b) { float t = 0; cudaEventElapsedTime(&t, a, b); return t; }
28#define CK(x) do { auto s_ = (x); if (s_ != 0) { std::fprintf(stderr, "FAIL %s = %d line %d\n", #x, (int) s_, __LINE__); std::exit(1); } } while (0)
29
30int main() {
31    setvbuf(stdout, nullptr, _IONBF, 0);
32    struct Shape { const char* name; long N, K; };
33    const Shape shapes[] = {{"gdn qkv", 10240, 2560}, {"gdn gate z", 6144, 2560}, {"qsa q", 12288, 2560}, {"gdn out", 2560, 6144}, {"qsa out", 2560, 4096},
34                            {"qsa k/v", 512, 2560}, {"shexp gate/up", 640, 2560}};
35    std::vector<long> Ts;
36    for (long t = 1; t <= 16; ++t) Ts.push_back(t);
37    for (long t = 24; t <= 512; t += 24) Ts.push_back(t);
38    for (long t = 560; t <= 8192; t += 253) Ts.push_back(t);
39    for (long t = 7030; t <= 7056; ++t) Ts.push_back(t);
40    for (long t : {1024L, 2048L, 4096L, 8192L, 1000L, 3000L, 5000L}) Ts.push_back(t);
41    const long TMAX = 8192;
42    cublasHandle_t h; CK(cublasCreate(&h));
43    cudaStream_t st; CK(cudaStreamCreate(&st)); CK(cublasSetStream(h, st));
44    void* ws; CK(cudaMalloc(&ws, 32u << 20)); CK(cublasSetWorkspace(h, ws, 32u << 20));
45    cudaEvent_t e0, e1; cudaEventCreate(&e0); cudaEventCreate(&e1);
46    unsigned* cnt; CK(cudaMalloc(&cnt, 4));
47    for (const Shape& s : shapes) {
48        const long N = s.N, K = s.K;
49        __half *W16, *X16; float *Wf, *Xf, *Ya, *Yb;
50        CK(cudaMalloc(&W16, N * K * 2)); CK(cudaMalloc(&X16, TMAX * K * 2)); CK(cudaMalloc(&Wf, N * K * 4)); CK(cudaMalloc(&Xf, TMAX * K * 4));
51        CK(cudaMalloc(&Ya, TMAX * N * 4)); CK(cudaMalloc(&Yb, TMAX * N * 4));
52        fill_f16<<<(unsigned) ((N * K + 255) / 256), 256, 0, st>>>(W16, N * K, 1u, 0.05f);
53        fill_f16<<<(unsigned) ((TMAX * K + 255) / 256), 256, 0, st>>>(X16, TMAX * K, 7u, 2.0f);
54        f16_to_f32<<<(unsigned) ((N * K + 255) / 256), 256, 0, st>>>(W16, Wf, N * K);
55        f16_to_f32<<<(unsigned) ((TMAX * K + 255) / 256), 256, 0, st>>>(X16, Xf, TMAX * K);
56        const float one = 1.0f, zero = 0.0f;
57        int n_equal = 0, n_all = 0, n_slow = 0;
58        double sum_a = 0, sum_b = 0;
59        for (long T : Ts) {
60            double ta = 1e30, tb = 1e30;
61            for (int r = 0; r < 3; ++r) {
62                cudaEventRecord(e0, st);
63                CK(cublasGemmEx(h, CUBLAS_OP_T, CUBLAS_OP_N, (int) N, (int) T, (int) K, &one, W16, CUDA_R_16F, (int) K, X16, CUDA_R_16F, (int) K,
64                                &zero, Ya, CUDA_R_32F, (int) N, CUBLAS_COMPUTE_32F, CUBLAS_GEMM_DEFAULT));
65                cudaEventRecord(e1, st); CK(cudaEventSynchronize(e1));
66                if (r) ta = fmin(ta, ms(e0, e1));
67                cudaEventRecord(e0, st);
68                CK(cublasSgemm(h, CUBLAS_OP_T, CUBLAS_OP_N, (int) N, (int) T, (int) K, &one, Wf, (int) K, Xf, (int) K, &zero, Yb, (int) N));
69                cudaEventRecord(e1, st); CK(cudaEventSynchronize(e1));
70                if (r) tb = fmin(tb, ms(e0, e1));
71            }
72            CK(cudaMemsetAsync(cnt, 0, 4, st));
73            count_diff<<<(unsigned) ((T * N + 255) / 256), 256, 0, st>>>((const unsigned*) Ya, (const unsigned*) Yb, T * N, cnt);
74            unsigned nd = 0; CK(cudaMemcpyAsync(&nd, cnt, 4, cudaMemcpyDeviceToHost, st)); CK(cudaStreamSynchronize(st));
75            const double fl = 2.0 * N * K * T / 1e12, fa = fl / (ta / 1e3), fb = fl / (tb / 1e3);
76            ++n_all; n_equal += nd == 0; n_slow += fa < 6.0 && T >= 256; sum_a += ta; sum_b += tb;
77            std::printf("SWEEP %-14s N %5ld K %5ld T %5ld | f16 %8.3f ms %5.2f TFLOPS | f32 %8.3f ms %5.2f TFLOPS | %5.2fx | %s\n", s.name, N, K, T, ta, fa, tb,
78                        fb, ta / tb, nd == 0 ? "bitwise equal" : "DIFFERENT");
79            if (nd) std::printf("       %u of %ld outputs differ\n", nd, T * N);
80        }
81        std::printf("SUMMARY %-14s: %d of %d T values bitwise equal; f16 below 6 TFLOPS at %d of the T >= 256; total f16 %.0f ms, f32 %.0f ms\n", s.name, n_equal,
82                    n_all, n_slow, sum_a, sum_b);
83        cudaFree(W16); cudaFree(X16); cudaFree(Wf); cudaFree(Xf); cudaFree(Ya); cudaFree(Yb);
84    }
85    return 0;
86}