diff --git a/sycl/CMakeLists.txt b/sycl/CMakeLists.txt index 316838b2..0937f632 100644 --- a/sycl/CMakeLists.txt +++ b/sycl/CMakeLists.txt @@ -359,6 +359,8 @@ if(STRATA_SYCL_PARITY) 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(dense_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/dense_bench.cpp) # the decode window's dense GEMVs, graph, hash + target_link_libraries(dense_bench PRIVATE strata_kernels) add_executable(mmvq_sg_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/mmvq_sg_bench.cpp) # SIMD16 vs 32 decode mmvq target_link_libraries(mmvq_sg_bench PRIVATE strata_kernels) add_executable(q6k_align_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/q6k_align_bench.cpp) # Q6_K block alignment diff --git a/sycl/src/kernels/cuda/fused_gr.dp.cpp b/sycl/src/kernels/cuda/fused_gr.dp.cpp index 2c8d1246..cf2ced86 100644 --- a/sycl/src/kernels/cuda/fused_gr.dp.cpp +++ b/sycl/src/kernels/cuda/fused_gr.dp.cpp @@ -609,17 +609,18 @@ gr_up_multi_kernel exceeds 128 bytes and may cause high register pressure. Consult with your hardware vendor to find the total register size available and adjust the code, or use smaller sub-group size to avoid high register pressure. */ -__dpct_inline__ void gr_up_multi_kernel(GrMulti m) { +template // columns per block: 16 (the original), 8 or 4 - each row's dot, butterfly and the per-column +__dpct_inline__ void gr_up_multi_kernel(GrMulti m) { // stream sum unchanged, so the same bits; more blocks, fewer serial rows per warp auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][LR]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &g = *sycl::ext::oneapi::group_local_memory_for_overwrite< - float[kFusedGrMaxT][HC][UPM_COLS]>( + float[kFusedGrMaxT][HC][COLS]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const int T = m.T; - const int d0 = item_ct1.get_group(2) * UPM_COLS; + const int d0 = item_ct1.get_group(2) * COLS; #pragma unroll for (int i = t; i < T * LR; i += THREADS) { // the down kernel's partial sums, in split order, then silu const int k = i / LR, r = i % LR; @@ -637,8 +638,8 @@ auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite< performance if there is no access to global memory. */ item_ct1.barrier(); - for (int r = warp; r < HC * UPM_COLS; r += WARPS) { - const int c = r / UPM_COLS, dd = r - c * UPM_COLS, i = c * N + d0 + dd; + for (int r = warp; r < HC * COLS; r += WARPS) { + const int c = r / COLS, dd = r - c * COLS, i = c * N + d0 + dd; const sycl::uint4 *w4 = reinterpret_cast(m.a[0].w_up + (size_t)i * LR); /* @@ -666,6 +667,26 @@ auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite< if (apply) { bo = a.bo_prev[d0 + dd]; ip = a.inj_prev[c]; } } float mine = 0.0f; + if constexpr (ILP) { // STRATA_GR_UP_ILP: the T dots first, then their butterflies interleaved step by step - + float accs[kFusedGrMaxT]; // each value sees warp_sum's shuffles in warp_sum's order: the same bits +#pragma unroll + for (int k = 0; k < kFusedGrMaxT; ++k) { + float acc = 0.0f; + if (k < T) { + acc = dot8(wa, lo[k] + lane * 8); + if (lane < LR / 8 - 32) acc += dot8(wb, lo[k] + (32 + lane) * 8); + } + accs[k] = acc; + } +#pragma unroll + for (int o = 16; o > 0; o >>= 1) +#pragma unroll + for (int k = 0; k < kFusedGrMaxT; ++k) + if (k < T) accs[k] += dpct::experimental::permute_sub_group_by_xor( + 0xffffffffu, sycl::ext::oneapi::this_work_item::get_sub_group(), accs[k], o); +#pragma unroll + for (int k = 0; k < kFusedGrMaxT; ++k) if (k < T && lane == k) mine = accs[k]; + } else { #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { if (k >= T) break; @@ -674,6 +695,7 @@ auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite< acc = warp_sum(acc); if (lane == k) mine = acc; } + } if (lane < T) { if (apply) { rv = sycl::fma(bo, 2.0f * sigmoidf_(ip / (float)HC), rv); @@ -689,8 +711,8 @@ auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite< performance if there is no access to global memory. */ item_ct1.barrier(); - for (int i = t; i < T * UPM_COLS; i += THREADS) { - const int k = i / UPM_COLS, col = i - k * UPM_COLS; + for (int i = t; i < T * COLS; i += THREADS) { + const int k = i / COLS, col = i - k * COLS; float s = 0.0f; #pragma unroll for (int c = 0; c < HC; ++c) s += g[k][c][col]; @@ -699,6 +721,39 @@ auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite< } +// SYCL port: the up step with COLS columns per block. 16 (160 blocks, 8 rows per warp one after another) was +// latency-bound on Xe2 (B70: 23.7 us for 6.55 MB of BF16, 46% of the bandwidth). STRATA_GR_UP_COLS=8 / 4 run 320 / +// 640 blocks with 4 / 2 rows per warp; every row's dot and butterfly and every column's stream sum are the same. +inline int gr_up_cols() { + static const int v = [] { const char* e = std::getenv("STRATA_GR_UP_COLS"); const int c = e ? std::atoi(e) : UPM_COLS; + return c == 4 || c == 8 || c == 32 || c == 64 ? c : UPM_COLS; }(); + return v; +} +inline bool gr_up_ilp() { + static const bool v = [] { const char* e = std::getenv("STRATA_GR_UP_ILP"); return e && e[0] == '1'; }(); + return v; +} +template +void launch_gr_up_cols(dpct::queue_ptr st, const GrMulti& m) { + auto exp_props = sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync}; + const sycl::nd_range<3> r(sycl::range(1, 1, N / C) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)); + if (gr_up_ilp()) + st->parallel_for>>(r, exp_props, + [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_up_multi_kernel(m); }); + else + st->parallel_for>>(r, exp_props, + [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_up_multi_kernel(m); }); +} +inline void launch_gr_up_multi(dpct::queue_ptr st, const GrMulti& m) { + switch (gr_up_cols()) { + case 4: launch_gr_up_cols<4>(st, m); return; + case 8: launch_gr_up_cols<8>(st, m); return; + case 32: launch_gr_up_cols<32>(st, m); return; + case 64: launch_gr_up_cols<64>(st, m); return; + default: launch_gr_up_cols(st, m); return; + } +} + // ================================ hc read v3 (opt-in: STRATA_GR_V3=1) - two kernels, stream-split ================ // The norm kernel runs on only T blocks (~16 us of pure latency per call) and `down` on 41 blocks (~280 GB/s). // v3: `down` is split over (row group, stream[, column half]) = 164 or 328 blocks; each block stages its slice of @@ -1562,19 +1617,7 @@ void launch_multi(const GrMulti &m, int variant, dpct::queue_ptr st, } } if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, (void*) st); - { - auto exp_props = sycl::ext::oneapi::experimental::properties{ - sycl::ext::oneapi::experimental::use_root_sync}; - - st->parallel_for>( - sycl::nd_range<3>(sycl::range(1, 1, UPM_BLOCKS) * - sycl::range(1, 1, THREADS), - sycl::range(1, 1, THREADS)), - exp_props, - [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { - gr_up_multi_kernel(m); - }); - } + launch_gr_up_multi(st, m); // STRATA_GR_UP_COLS (16 default, 8, 4): the same bits } /// STRATA_HC_SPLIT: unset or 2 = the newest the check accepts (staged), 1 = at most split, 0 = the plain read @@ -1860,19 +1903,7 @@ bool fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voi } } if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, stream); - { - auto exp_props = sycl::ext::oneapi::experimental::properties{ - sycl::ext::oneapi::experimental::use_root_sync}; - - st->parallel_for>( - sycl::nd_range<3>(sycl::range(1, 1, UPM_BLOCKS) * - sycl::range(1, 1, THREADS), - sycl::range(1, 1, THREADS)), - exp_props, - [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { - gr_up_multi_kernel(m); - }); - } + launch_gr_up_multi(st, m); // STRATA_GR_UP_COLS (16 default, 8, 4): the same bits /* DPCT1010: SYCL uses exceptions to report errors and does not use the error codes. The cudaGetLastError function call was replaced with 0. You diff --git a/sycl/src/kernels/cuda/native_mmvq.dp.cpp b/sycl/src/kernels/cuda/native_mmvq.dp.cpp index ea02af52..f8a5446e 100644 --- a/sycl/src/kernels/cuda/native_mmvq.dp.cpp +++ b/sycl/src/kernels/cuda/native_mmvq.dp.cpp @@ -1623,7 +1623,7 @@ void native_mmvq_q6k_wide_kernel(const Q6KBlock* __restrict__ w, const Q81Block* // at a time and the kernel is latency-bound (~146 GB/s). Here a sub-group first issues the ql/qh/scale loads // of U steps (4U blocks), then does their dots; the activation reads stay inline (they are L1 hits, shared by // every row). One row per sub-group, WARPS rows per group. -template +template void native_mmvq_q6k_pf_kernel(const Q6KBlock* __restrict__ w, const Q81Block* __restrict__ x, float* __restrict__ y, int n_in, int n_out) { auto item = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); @@ -1646,8 +1646,9 @@ void native_mmvq_q6k_pf_kernel(const Q6KBlock* __restrict__ w, const Q81Block* _ const int kbx = kb0 + 4 * u + sub; ok[u] = kbx < blocks_per_row; const Q6KBlock* b = wr + (ok[u] ? kbx : 0); - ql4[u] = reinterpret_cast(b->ql)[g]; - qh4[u] = reinterpret_cast(b->qh)[qh4_idx]; + if constexpr (A2) { ql4[u] = load16_a2(b->ql + 16 * g); qh4[u] = load16_a2(b->qh + 16 * qh4_idx); } + else { ql4[u] = reinterpret_cast(b->ql)[g]; + qh4[u] = reinterpret_cast(b->qh)[qh4_idx]; } const float d = b->d; dsc0[u] = d * (float) b->scales[scale_offset]; dsc1[u] = d * (float) b->scales[scale_offset + 4]; } @@ -1666,8 +1667,16 @@ void native_mmvq_q6k_pf_kernel(const Q6KBlock* __restrict__ w, const Q81Block* _ #pragma unroll for (int j = 0; j < NCOLS; ++j) { const Q81Block* xb = x + std::size_t(j) * x_stride + kby + bq8_offset; - const sycl::int4 a0 = reinterpret_cast(xb[0].qs)[u_int4]; - const sycl::int4 a1 = reinterpret_cast(xb[2].qs)[u_int4]; + sycl::int4 a0, a1; + if constexpr (A2) { // Q8_1 qs at offset 4 of a 36-byte block: four int loads (as the wide kernel) + const int* p0 = reinterpret_cast(xb[0].qs) + 4 * u_int4; + const int* p1 = reinterpret_cast(xb[2].qs) + 4 * u_int4; + a0 = sycl::int4(p0[0], p0[1], p0[2], p0[3]); + a1 = sycl::int4(p1[0], p1[1], p1[2], p1[3]); + } else { + a0 = reinterpret_cast(xb[0].qs)[u_int4]; + a1 = reinterpret_cast(xb[2].qs)[u_int4]; + } int s0 = 0, s1 = 0; s0 = strata::dp4a(vi0[0], a0.x(), s0); s0 = strata::dp4a(vi0[1], a0.y(), s0); s0 = strata::dp4a(vi0[2], a0.z(), s0); s0 = strata::dp4a(vi0[3], a0.w(), s0); @@ -1702,7 +1711,12 @@ void launch_q6k_wide(const void* weights, const void* x_q8_1, float* y, int n_in if (const int pf = q6k_pf(); pf > 0) { const unsigned blocks = unsigned((std::size_t(n_out) + WARPS - 1) / WARPS); const sycl::range<3> t(1, WARPS, WARP); -#define STRATA_Q6PF(UU) if (pf == UU) { \ +#define STRATA_Q6PF(UU) if (pf == UU && q6k_a2()) { \ + s->parallel_for, dpct_kernel_scalar>>( \ + sycl::nd_range<3>(sycl::range(1, 1, blocks) * t, t), \ + [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { native_mmvq_q6k_pf_kernel(w, x, y, n_in, n_out); }); \ + return; } \ + if (pf == UU) { \ s->parallel_for, dpct_kernel_scalar>>( \ sycl::nd_range<3>(sycl::range(1, 1, blocks) * t, t), \ [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { native_mmvq_q6k_pf_kernel(w, x, y, n_in, n_out); }); \ @@ -1727,8 +1741,16 @@ void launch_q6k_wide(const void* weights, const void* x_q8_1, float* y, int n_in [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(16)]] { native_mmvq_q6k_wide_kernel(w, x, y, n_in, n_out); }); return; } + // RP rows per sub-group share each column's activation loads (the rows' own lanes, k order and butterfly: every + // output the same bits); with the aligned loads (A2) unless STRATA_MMVQ_A2=0 #define STRATA_Q6W(RP) if (rpw == RP) { \ const unsigned blocks = unsigned((std::size_t(n_out) + WARPS * RP - 1) / (WARPS * RP)); \ + if (q6k_a2()) { \ + s->parallel_for, dpct_kernel_scalar>>( \ + sycl::nd_range<3>(sycl::range(1, 1, blocks) * sycl::range(1, WARPS, WARP), sycl::range(1, WARPS, WARP)), \ + [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { \ + native_mmvq_q6k_wide_kernel(w, x, y, n_in, n_out); }); \ + return; } \ s->parallel_for, dpct_kernel_scalar>>( \ sycl::nd_range<3>(sycl::range(1, 1, blocks) * sycl::range(1, WARPS, WARP), sycl::range(1, WARPS, WARP)), \ [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { native_mmvq_q6k_wide_kernel(w, x, y, n_in, n_out); }); \ @@ -1780,6 +1802,19 @@ struct WideQ4K { return da * (r.dl * (float) dp4a4(r.lo, ua, 0) - r.ml * (float) dp4a4(ones, ua, 0)) + dc * (r.dh * (float) dp4a4(r.hi, uc, 0) - r.mh * (float) dp4a4(ones, uc, 0)); } + // apply() in two parts for several rows of one sub-group: the activations depend on the lane alone (sb, half from g) + struct A { sycl::int4 ua, uc; float da, dc; }; + static A act(const Q81Block* xb, int g) { + const int sb = 2 * (g >> 1), half = g & 1; + const Q81Block* a = xb + sb; const Q81Block* c = xb + sb + 1; + A v; v.ua = ld_q8_16(a, half); v.uc = ld_q8_16(c, half); v.da = (float) a->ds[0]; v.dc = (float) c->ds[0]; + return v; + } + static float dot(const W& r, const A& v) { // apply's expression, the same operations in the same order + const sycl::int4 ones(0x01010101, 0x01010101, 0x01010101, 0x01010101); + return v.da * (r.dl * (float) dp4a4(r.lo, v.ua, 0) - r.ml * (float) dp4a4(ones, v.ua, 0)) + + v.dc * (r.dh * (float) dp4a4(r.hi, v.uc, 0) - r.mh * (float) dp4a4(ones, v.uc, 0)); + } }; struct WideQ5K { using Block = Q5KBlock; @@ -1800,6 +1835,9 @@ struct WideQ5K { return r; } static float apply(const W& r, const Q81Block* xb) { return WideQ4K::apply(r, xb); } + using A = WideQ4K::A; + static A act(const Q81Block* xb, int g) { return WideQ4K::act(xb, g); } + static float dot(const W& r, const A& v) { return WideQ4K::dot(r, v); } }; __dpct_inline__ uint32_t iq4_lut4(uint32_t q4) { // four nibbles -> four codebook bytes, codebook in registers constexpr uint32_t t0 = 0xBFAD9881u, t1 = 0xF6EADDCFu, t2 = 0x26190D01u, t3 = 0x71594535u; @@ -1832,7 +1870,57 @@ struct WideIQ4XS { const Q81Block* a = xb + r.sb; return r.d * (float) a->ds[0] * (float) dp4a4(r.hi, ld_q8_16(a, 1), dp4a4(r.lo, ld_q8_16(a, 0), 0)); } + struct A { sycl::int4 u0, u1; float ds; }; + static A act(const Q81Block* xb, int g) { + const Q81Block* a = xb + g; + A v; v.u0 = ld_q8_16(a, 0); v.u1 = ld_q8_16(a, 1); v.ds = (float) a->ds[0]; + return v; + } + static float dot(const W& r, const A& v) { return r.d * v.ds * (float) dp4a4(r.hi, v.u1, dp4a4(r.lo, v.u0, 0)); } }; +// SYCL port: native_mmvq_wide_kernel with RPW rows per sub-group (STRATA_MMVQ_WIDE_K_RPW): each column's activations +// are loaded once (act) and used by the RPW rows (dot); a row's lanes, its k order and its butterfly are the +// one-row kernel's, so every output is the same bits. At 2-4 columns the one-row kernel reads ~5x its weight bytes +// in activations (each row re-reads the whole vector per column). +template +void native_mmvq_wide_rpw_kernel(const typename F::Block* __restrict__ w, const Q81Block* __restrict__ x, + float* __restrict__ y, int n_in, int n_out) { + auto item = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); + const int lane = int(item.get_local_id(2)), warp = int(item.get_local_id(1)); + const int row0 = (int(item.get_group(2)) * WARPS + warp) * RPW; + if (row0 >= n_out) return; + const int blocks_per_row = n_in / QK, x_stride = n_in / Q8K; + const int g = lane & 7, sub = lane >> 3; + const typename F::Block* wr[RPW]; +#pragma unroll + for (int r = 0; r < RPW; ++r) wr[r] = w + std::size_t(row0 + (row0 + r < n_out ? r : 0)) * blocks_per_row; + float acc[RPW][NCOLS] = {}; + for (int kbx = sub; kbx < blocks_per_row; kbx += 4) { + typename F::W wv[RPW]; +#pragma unroll + for (int r = 0; r < RPW; ++r) wv[r] = F::load(wr[r] + kbx, g); +#pragma unroll + for (int j = 0; j < NCOLS; ++j) { + const typename F::A a = F::act(x + std::size_t(j) * x_stride + kbx * (QK / Q8K), g); +#pragma unroll + for (int r = 0; r < RPW; ++r) acc[r][j] += F::dot(wv[r], a); + } + } + auto sg = item.get_sub_group(); +#pragma unroll + for (int r = 0; r < RPW; ++r) +#pragma unroll + for (int j = 0; j < NCOLS; ++j) { + float v = acc[r][j]; +#pragma unroll + for (int o = WARP / 2; o > 0; o >>= 1) v += sycl::permute_group_by_xor(sg, v, o); + if (lane == 0 && row0 + r < n_out) y[std::size_t(j) * n_out + row0 + r] = v; + } +} +inline int wide_k_rpw() { // STRATA_MMVQ_WIDE_K_RPW: rows per sub-group of the Q4_K / Q5_K / IQ4_XS wide kernel (1, 2, 4) + static const int v = std::getenv("STRATA_MMVQ_WIDE_K_RPW") ? std::atoi(std::getenv("STRATA_MMVQ_WIDE_K_RPW")) : 1; + return v; +} template void native_mmvq_wide_kernel(const typename F::Block* __restrict__ w, const Q81Block* __restrict__ x, float* __restrict__ y, int n_in, int n_out) { @@ -1862,6 +1950,14 @@ template void launch_wide(const void* weights, const void* x_q8_1, float* y, int n_in, int n_out, dpct::queue_ptr s) { const auto* w = static_cast(weights); const auto* x = static_cast(x_q8_1); +#define STRATA_WKR(RP) if (wide_k_rpw() == RP) { \ + const unsigned b = unsigned((std::size_t(n_out) + WARPS * RP - 1) / (WARPS * RP)); \ + s->parallel_for, dpct_kernel_scalar>>( \ + sycl::nd_range<3>(sycl::range(1, 1, b) * sycl::range(1, WARPS, WARP), sycl::range(1, WARPS, WARP)), \ + [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { native_mmvq_wide_rpw_kernel(w, x, y, n_in, n_out); }); \ + return; } + STRATA_WKR(2) STRATA_WKR(4) +#undef STRATA_WKR const unsigned blocks = unsigned((std::size_t(n_out) + WARPS - 1) / WARPS); s->parallel_for>>( sycl::nd_range<3>(sycl::range(1, 1, blocks) * sycl::range(1, WARPS, WARP), sycl::range(1, WARPS, WARP)), diff --git a/sycl/src/kernels/dense_bench.cpp b/sycl/src/kernels/dense_bench.cpp new file mode 100644 index 00000000..ff401b26 --- /dev/null +++ b/sycl/src/kernels/dense_bench.cpp @@ -0,0 +1,105 @@ +// sycl/src/kernels/dense_bench.cpp - the decode window's dense GEMVs (native_mmvq) at the model's shapes and formats: +// NMAT different matrices per shape captured in one graph, so the weights stream from DRAM as in the engine (a window +// reads every layer's matrix once); us per call, GB/s of weights, and an FNV hash of the outputs so two builds or two +// STRATA_MMVQ_* settings can be compared bit for bit. +// +// dense_bench [type ...] (default: 14 13 12 23 = Q6_K Q5_K Q4_K IQ4_XS) +#define DPCT_PROFILING_ENABLED +#include +#include +#include "strata/kernels/native_mmvq.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace k = strata::kernels; + +namespace { +const char* name_of(int t) { + switch (t) { case 14: return "Q6_K"; case 13: return "Q5_K"; case 12: return "Q4_K"; case 23: return "IQ4_XS"; + case 8: return "Q8_0"; default: return "?"; } +} +// random blocks with a finite, small fp16 scale (at the format's offset) so the outputs are finite +void fill(int t, std::vector& w, std::mt19937& rng) { + std::uniform_int_distribution b(0, 255); + for (auto& v : w) v = (uint8_t) b(rng); + const size_t bs = t == 14 ? 210 : t == 13 ? 176 : t == 12 ? 144 : t == 23 ? 136 : 34; + const size_t off = t == 14 ? 208 : 0; + for (size_t o = 0; o + bs <= w.size(); o += bs) { + const uint16_t h = (uint16_t) (0x1400 | (b(rng) & 0x3ff)); // ~2^-10 + std::memcpy(&w[o + off], &h, 2); + if (t == 12 || t == 13) std::memcpy(&w[o + 2], &h, 2); // dmin + } +} +uint64_t fnv(const float* p, size_t n) { + uint64_t h = 1469598103934665603ull; + const uint32_t* u = (const uint32_t*) p; + for (size_t i = 0; i < n; ++i) { h ^= u[i]; h *= 1099511628211ull; } + return h; +} +} // namespace + +int main(int argc, char** argv) { + std::vector types; + for (int i = 1; i < argc; ++i) types.push_back(std::atoi(argv[i])); + if (types.empty()) types = {14, 13, 12, 23}; + dpct::queue_ptr s = dpct::get_current_device().create_queue(true); + std::mt19937 rng(7); + const struct { int n_in, n_out; const char* what; } shapes[] = { + {2560, 10240, "GDN qkv"}, {2560, 6144, "GDN z"}, {6144, 2560, "out-proj"}, {2560, 12288, "QSA q"}, {2560, 512, "k/v"}}; + constexpr int NMAT = 24; + std::printf("dense_bench: %d matrices per shape in one graph; us per call (median of 30), GB/s of weights, hash\n", NMAT); + for (const int t : types) { + for (const auto& sh : shapes) { + const size_t wb = k::native_mmvq_weight_bytes(t, sh.n_in, sh.n_out), slot = (wb + 255) & ~(size_t) 255; + uint8_t* w = (uint8_t*) sycl::malloc_device(slot * NMAT, *s); + std::vector hw(wb); + for (int m = 0; m < NMAT; ++m) { fill(t, hw, rng); s->memcpy(w + slot * m, hw.data(), wb).wait(); } + std::vector hx((size_t) 4 * sh.n_in); + std::normal_distribution nd(0.f, 1.f); + for (auto& v : hx) v = nd(rng); + float* x = sycl::malloc_device(hx.size(), *s); + s->memcpy(x, hx.data(), hx.size() * 4).wait(); + void* xq = sycl::malloc_device(k::native_q8_1_bytes(sh.n_in, 4) + 256, *s); + float* y = sycl::malloc_device((size_t) 4 * sh.n_out * NMAT, *s); + for (int nc = 1; nc <= 4; ++nc) { + k::native_quantize_q8_1(x, xq, sh.n_in, nc, s); + s->wait(); + dpct::experimental::command_graph_ptr g; + dpct::experimental::begin_recording(s); + for (int m = 0; m < NMAT; ++m) + k::native_mmvq(t, w + slot * m, xq, y + (size_t) 4 * sh.n_out * m, sh.n_in, sh.n_out, nc, s); + dpct::experimental::end_recording(s, &g); + auto* ge = new sycl::ext::oneapi::experimental::command_graph< + sycl::ext::oneapi::experimental::graph_state::executable>(g->finalize()); + delete g; + std::vector us; + for (int i = 0; i < 40; ++i) { + sycl::event e0 = s->ext_oneapi_submit_barrier(); + s->ext_oneapi_graph(*ge); + sycl::event e1 = s->ext_oneapi_submit_barrier(); + e1.wait(); + const double ns = (double) (e1.get_profiling_info() - + e0.get_profiling_info()); + if (i >= 10) us.push_back(ns / 1e3 / NMAT); + } + delete ge; + std::sort(us.begin(), us.end()); + std::vector hy((size_t) 4 * sh.n_out * NMAT); + s->memcpy(hy.data(), y, hy.size() * 4).wait(); + uint64_t h = 1469598103934665603ull; + for (int m = 0; m < NMAT; ++m) h ^= fnv(&hy[(size_t) 4 * sh.n_out * m], (size_t) nc * sh.n_out) + m; + std::printf("%-6s %-8s %5d x %5d %d cols: %8.2f us %6.1f GB/s hash %016llx\n", name_of(t), sh.what, + sh.n_in, sh.n_out, nc, us[us.size() / 2], wb / us[us.size() / 2] / 1e3, (unsigned long long) h); + } + sycl::free(w, *s); sycl::free(x, *s); sycl::free(xq, *s); sycl::free(y, *s); + } + } + dpct::get_current_device().destroy_queue(s); + return 0; +}