diff --git a/sycl/CMakeLists.txt b/sycl/CMakeLists.txt index 4d79be35..49c36ebb 100644 --- a/sycl/CMakeLists.txt +++ b/sycl/CMakeLists.txt @@ -281,6 +281,8 @@ if(STRATA_SYCL_PARITY) add_executable(kv_hybrid_parity ${_src}) target_include_directories(kv_hybrid_parity PRIVATE ${STRATA_ROOT}/src/kernels) target_link_libraries(kv_hybrid_parity PRIVATE strata_kernels) + add_executable(dequant_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/dequant_bench.cpp) # prompt-path expert dequant timing + output hash + target_link_libraries(dequant_bench PRIVATE strata_kernels) add_executable(mmvq_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/mmvq_bench.cpp) # dense mmvq timing target_link_libraries(mmvq_bench PRIVATE strata_kernels) add_executable(mmvq_sg_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/mmvq_sg_bench.cpp) # SIMD16 vs 32 decode mmvq diff --git a/sycl/src/kernels/cuda/iq_kernels.dp.cpp b/sycl/src/kernels/cuda/iq_kernels.dp.cpp index 324bc9ed..3110c887 100644 --- a/sycl/src/kernels/cuda/iq_kernels.dp.cpp +++ b/sycl/src/kernels/cuda/iq_kernels.dp.cpp @@ -2386,7 +2386,7 @@ __dpct_inline__ void store_run(dst_t* y, const float* v) { template inline void dq_iq2_xxs(const void *vx, int64_t ibs, dst_t *yy, int tid) { const block_iq2_xxs* x = (const block_iq2_xxs*) vx; - const int64_t il = tid / 8, ib = tid % 8; + const int64_t il = tid % 4, ib = tid / 4; // consecutive lanes write consecutive 16-byte runs (coalesced stores) dst_t* y = yy + 32 * ib + 8 * il; const uint16_t* q2 = x[ibs].qs + 4 * ib; const uint8_t* aux8 = (const uint8_t*) q2; @@ -2402,7 +2402,7 @@ inline void dq_iq2_xxs(const void *vx, int64_t ibs, dst_t *yy, int tid) { template inline void dq_iq2_xs(const void *vx, int64_t ibs, dst_t *yy, int tid) { const block_iq2_xs* x = (const block_iq2_xs*) vx; - const int64_t il = tid / 8, ib = tid % 8; + const int64_t il = tid % 4, ib = tid / 4; // consecutive lanes write consecutive 16-byte runs (coalesced stores) dst_t* y = yy + 32 * ib + 8 * il; const uint16_t* q2 = x[ibs].qs + 4 * ib; const uint8_t* grid = (const uint8_t*) (iq2xs_grid + (q2[il] & 511)); @@ -2416,7 +2416,7 @@ inline void dq_iq2_xs(const void *vx, int64_t ibs, dst_t *yy, int tid) { template inline void dq_iq2_s(const void *vx, int64_t ibs, dst_t *yy, int tid) { const block_iq2_s* x = (const block_iq2_s*) vx; - const int64_t il = tid / 8, ib = tid % 8; + const int64_t il = tid % 4, ib = tid / 4; // consecutive lanes write consecutive 16-byte runs (coalesced stores) dst_t* y = yy + 32 * ib + 8 * il; const uint8_t* grid = (const uint8_t*) (iq2s_grid + (x[ibs].qs[4 * ib + il] | ((x[ibs].qh[ib] << (8 - 2 * il)) & 0x300))); const float d = (float) x[ibs].d * (0.5f + ((x[ibs].scales[ib] >> 4 * (il / 2)) & 0xf)) * 0.25f; @@ -2429,7 +2429,7 @@ inline void dq_iq2_s(const void *vx, int64_t ibs, dst_t *yy, int tid) { template inline void dq_iq3_xxs(const void *vx, int64_t ibs, dst_t *yy, int tid) { const block_iq3_xxs* x = (const block_iq3_xxs*) vx; - const int64_t il = tid / 8, ib = tid % 8; + const int64_t il = tid % 4, ib = tid / 4; // consecutive lanes write consecutive 16-byte runs (coalesced stores) dst_t* y = yy + 32 * ib + 8 * il; const uint8_t* q3 = x[ibs].qs + 8 * ib; const uint16_t* gas = (const uint16_t*) (x[ibs].qs + QK_K / 4) + 2 * ib; @@ -2449,7 +2449,7 @@ inline void dq_iq3_xxs(const void *vx, int64_t ibs, dst_t *yy, int tid) { template inline void dq_iq3_s(const void *vx, int64_t ibs, dst_t *yy, int tid) { const block_iq3_s* x = (const block_iq3_s*) vx; - const int64_t il = tid / 8, ib = tid % 8; + const int64_t il = tid % 4, ib = tid / 4; // consecutive lanes write consecutive 16-byte runs (coalesced stores) dst_t* y = yy + 32 * ib + 8 * il; const uint8_t* qs = x[ibs].qs + 8 * ib; const uint8_t* grid1 = (const uint8_t*) (iq3s_grid + (qs[2 * il + 0] | ((x[ibs].qh[ib] << (8 - 2 * il)) & 256))); @@ -2467,7 +2467,7 @@ inline void dq_iq3_s(const void *vx, int64_t ibs, dst_t *yy, int tid) { template inline void dq_iq1_m(const void *vx, int64_t ibs, dst_t *yy, int tid) { const block_iq1_m* x = (const block_iq1_m*) vx; - const int64_t il = tid / 8, ib = tid % 8; + const int64_t il = tid % 4, ib = tid / 4; // consecutive lanes write consecutive 16-byte runs (coalesced stores) dst_t* y = yy + 32 * ib + 8 * il; const uint16_t* sc = (const uint16_t*) x[ibs].scales; iq1m_scale_t scale; @@ -2488,7 +2488,7 @@ inline void dq_iq4_nl(const void *vx, int64_t ibs, dst_t *yy, int tid) { const block_iq4_nl* x = (const block_iq4_nl*) vx + ibs * (QK_K / QK4_NL); // SYCL port: thread (ib, il) writes the block's values [8 il, 8 il + 8) as one run: il < 2 the low nibbles of // qs[8 il ..], il >= 2 the high nibbles of qs[8 (il - 2) ..] (the same values as llama.cpp's 4 + 4 split) - const int64_t il = tid / 8, ib = tid % 8; + const int64_t il = tid % 4, ib = tid / 4; // consecutive lanes write consecutive 16-byte runs (coalesced stores) dst_t* y = yy + 32 * ib + 8 * il; const uint8_t* q4 = x[ib].qs + 8 * (il & 1); const float d = (float) x[ib].d; diff --git a/sycl/src/kernels/dequant_bench.cpp b/sycl/src/kernels/dequant_bench.cpp new file mode 100644 index 00000000..1e0127f9 --- /dev/null +++ b/sycl/src/kernels/dequant_bench.cpp @@ -0,0 +1,73 @@ +// sycl/src/kernels/dequant_bench.cpp - time the prompt path's expert dequantizers (iq_dequant_gu_f16 / iq_dequant_f16) +// on random blocks and print a hash of the FP16 output, so two builds can be compared bit for bit. +// dequant_bench [type|all] [experts] (gate/up 1280 x 2560 and down 2560 x 1280 per expert, the model's shape) +#include "strata/kernels/iq_kernels.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { +struct Ty { int id; const char* name; int bytes_per_256; }; +const Ty kTypes[] = {{16, "IQ2_XXS", 66}, {17, "IQ2_XS", 74}, {22, "IQ2_S", 82}, {18, "IQ3_XXS", 98}, + {21, "IQ3_S", 110}, {20, "IQ4_NL", 144}, {42, "Q2_0", 72}}; +uint64_t fnv(const uint16_t* p, size_t n) { + uint64_t h = 1469598103934665603ull; + for (size_t i = 0; i < n; ++i) { h ^= p[i]; h *= 1099511628211ull; } + return h; +} +} // namespace + +int main(int argc, char** argv) { + const int want = argc > 1 && std::strcmp(argv[1], "all") != 0 ? std::atoi(argv[1]) : -1; + const int experts = argc > 2 ? std::atoi(argv[2]) : 32; + const int64_t n_ff = 1280, n_embd = 2560; + sycl::queue* s = &dpct::get_in_order_queue(); + for (const Ty& t : kTypes) { + if (want >= 0 && t.id != want) continue; + const size_t per_mat = (size_t) n_ff * n_embd / 256 * t.bytes_per_256; // one of gate / up / down + std::vector hb(per_mat); + uint32_t x = 12345u + (uint32_t) t.id; + for (auto& b : hb) { x = x * 1664525u + 1013904223u; b = (uint8_t) (x >> 24); } + uint8_t* src = (uint8_t*) sycl::malloc_device(per_mat * 3 * experts, *s); // gate, up, down per expert + for (int e = 0; e < experts * 3; ++e) s->memcpy(src + (size_t) e * per_mat, hb.data(), per_mat); + const size_t gu_out = (size_t) 2 * n_ff * n_embd, d_out = (size_t) n_embd * n_ff; + uint16_t* dst = (uint16_t*) sycl::malloc_device((gu_out + d_out) * 2 * experts, *s); + s->wait(); + auto run = [&]() { + for (int e = 0; e < experts; ++e) { + const uint8_t* b = src + (size_t) e * 3 * per_mat; + uint16_t* o = dst + (size_t) e * (gu_out + d_out); + strata::kernels::iq_dequant_gu_f16(t.id, b, b + per_mat, n_ff, n_embd, o, s); + strata::kernels::iq_dequant_f16(t.id, b + 2 * per_mat, n_embd * n_ff, o + gu_out, s); + } + s->wait(); + }; + for (int i = 0; i < 3; ++i) run(); + const int reps = 10; + std::vector ms; + for (int i = 0; i < reps; ++i) { + const auto t0 = std::chrono::steady_clock::now(); + run(); + ms.push_back(std::chrono::duration(std::chrono::steady_clock::now() - t0).count()); + } + std::sort(ms.begin(), ms.end()); + const double med = ms[reps / 2]; + const double wr = (double) (gu_out + d_out) * 2 * experts, rd = (double) per_mat * 3 * experts; + std::vector ho((gu_out + d_out) * 2); // hash expert 0 and the last expert + s->memcpy(ho.data(), dst, (gu_out + d_out) * 2).wait(); + const uint64_t h0 = fnv(ho.data(), gu_out + d_out); + s->memcpy(ho.data(), dst + (size_t) (experts - 1) * (gu_out + d_out), (gu_out + d_out) * 2).wait(); + const uint64_t h1 = fnv(ho.data(), gu_out + d_out); + std::printf("%-8s %2d experts: median %7.2f ms write %6.1f GB/s (read %5.1f GB/s) hash %016llx %016llx\n", t.name, + experts, med, wr / med / 1e6, rd / med / 1e6, (unsigned long long) h0, (unsigned long long) h1); + sycl::free(src, *s); + sycl::free(dst, *s); + } + return 0; +}