okami.mom/strata

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

Niko1221Merge branch 'f-141-intel' into f-14028f70a52cf

main
2.9 KiB60 linesraw
1// gemm_launch: what does one expert-sized FP16 GEMM (oneMKL, half in, float out) cost back to back, by row count?
2// The prompt path issues one gate/up and one down GEMM per routed expert (~25k experts per 4K prompt on the 48 layers); if the
3// per-call time barely moves with the rows, the path is bound by launches, not by the XMX units.
4//   gemm_launch [calls=2000]
5#include <sycl/sycl.hpp>
6#include <oneapi/mkl.hpp>
7#include <dpct/dpct.hpp>
8#include <dpct/blas_utils.hpp>
9#include <chrono>
10#include <cstdio>
11#include <cstdlib>
12#include <vector>
13
14int main(int argc, char** argv) {
15    const int calls = argc > 1 ? std::atoi(argv[1]) : 2000;
16    sycl::queue q{sycl::gpu_selector_v, sycl::property::queue::in_order{}};
17    const int64_t N = 2560;
18    struct Shape { const char* name; int64_t n, k; };
19    const Shape shapes[] = {{"gate/up 1280x2560", 1280, N}, {"down 2560x640", N, 640}};
20    const int64_t Ts[] = {8, 32, 80, 160, 320, 640, 1280};
21    const int64_t maxT = 1280;
22    using half = sycl::half;
23    half* X = sycl::malloc_device<half>(maxT * N, q);
24    half* W = sycl::malloc_device<half>(N * 1280 * 4, q);
25    float* Y = sycl::malloc_device<float>(maxT * N, q);
26    q.memset(X, 0, maxT * N * sizeof(half));
27    q.memset(W, 0, N * 1280 * 4 * sizeof(half));
28    q.wait();
29    for (const Shape& s : shapes) {
30        for (int64_t T : Ts) {
31            auto run = [&](int n) {
32                for (int i = 0; i < n; ++i)
33                    oneapi::mkl::blas::column_major::gemm(q, oneapi::mkl::transpose::trans, oneapi::mkl::transpose::nontrans, s.n, T, s.k, 1.0f,
34                                                          W, s.k, X, s.k, 0.0f, Y, s.n);
35                q.wait();
36            };
37            run(50);
38            const auto t0 = std::chrono::steady_clock::now();
39            run(calls);
40            const double us = std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count() / calls;
41            auto* h = new dpct::blas::descriptor();
42            h->set_queue(&q);
43            const float alpha = 1.0f, beta = 0.0f;
44            auto run2 = [&](int cnt) {
45                for (int i = 0; i < cnt; ++i)
46                    dpct::blas::gemm(h, oneapi::mkl::transpose::trans, oneapi::mkl::transpose::nontrans, (int) s.n, (int) T, (int) s.k, &alpha, W,
47                                     dpct::library_data_t::real_half, (int) s.k, X, dpct::library_data_t::real_half, (int) s.k, &beta, Y,
48                                     dpct::library_data_t::real_float, (int) s.n, dpct::compute_type::f32);
49                q.wait();
50            };
51            run2(50);
52            const auto t1 = std::chrono::steady_clock::now();
53            run2(calls);
54            const double us2 = std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t1).count() / calls;
55            delete h;
56            std::printf("%-20s T=%5lld: mkl %7.1f us/call   dpct::blas::gemm %7.1f us/call\n", s.name, (long long) T, us, us2);
57        }
58    }
59    return 0;
60}