diff --git a/libxe2/xe2_gemm_w4_esimd.cpp b/libxe2/xe2_gemm_w4_esimd.cpp index 044a710e..0ac69f49 100644 --- a/libxe2/xe2_gemm_w4_esimd.cpp +++ b/libxe2/xe2_gemm_w4_esimd.cpp @@ -152,7 +152,7 @@ int launch16(sycl::queue& q, const void* av, int64_t ars, int mm, const uint32_t * those over one 256-column slice of the weight run together and it stays in L2 (the wide outputs). * Probe w4p.cpp at M = 2048, int8 TOPS: gate_up 27 -> 78, inz 27 -> 75, down 24 -> 62 (66 without * RF), N = 5120 K = 6144 29 -> 78 (without RF); every output identical to the first form's. */ -template +template int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const float* sat, int64_t mpad, const uint32_t* ct, const uint32_t* sz, int64_t M, int64_t N, int64_t K, char* yb, uint32_t ydt, int64_t yrs) { @@ -166,7 +166,7 @@ int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const flo return submit([&] { q.parallel_for(sycl::nd_range<1>{(size_t)(RG * cgw * kWg), (size_t)kWg}, props, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL { using namespace esimd; - slm_init<2 * kBuf>(); + if constexpr (SLM) slm_init<2 * kBuf>(); const int tl = (int)it.get_local_id(0); const int64_t wg = (int64_t)it.get_group(0); const int64_t rg = RF ? wg % RG : wg / cgw, cw = RF ? wg / RG : wg % cgw; @@ -191,18 +191,23 @@ int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const flo const uint32_t* szp = sz + tile0 * G * 16; const int32_t* asp = asumt + r0; const float* sap = sat + r0; + /* without SLM each thread reads its own A operands with 2D loads, which hit L1 (the + * work-group's threads read the same rows): no staging store, no barrier a group */ + xesimd::config_2d_mem_access ap8((const uint32_t*)ap, sw, sh, sw, 0, (int)r0); auto stage = [&](int64_t g, int buf) SYCL_ESIMD_FUNCTION { - if (tl < 4) { /* rows past M load as zeros (the 2D load's bounds) */ + if (SLM && tl < 4) { /* rows past M load as zeros (the 2D load's bounds) */ apl.set_x((int)(g * 32 + tl * 8)); simd a = xesimd::lsc_load_2d(apl); slm_block_store((uint32_t)(buf * kBuf + tl * kStep), a); } }; - stage(0, 0); - barrier(); + if constexpr (SLM) { + stage(0, 0); + barrier(); + } for (int64_t g = 0; g < G; ++g) { const int buf = (int)(g & 1); - if (g + 1 < G) stage(g + 1, buf ^ 1); + if (SLM && g + 1 < G) stage(g + 1, buf ^ 1); /* the accumulators start at -zero * asum (int16 operands, exact: |zero * asum| <= * 15 * 127 * 128), so the DPAS leaves sum - zero * asum, the integer libref's chain * forms first, and the epilogue only converts and scales */ @@ -234,7 +239,14 @@ int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const flo for (int j = 0; j < 2; ++j) B[j] = block_load(cbp + j * kr * 16 + ks * 64); #pragma unroll for (int i = 0; i < RB; ++i) { - simd Ai = slm_block_load((uint32_t)(buf * kBuf + ks * kStep + i * 256)); + simd Ai; + if constexpr (SLM) { + Ai = slm_block_load((uint32_t)(buf * kBuf + ks * kStep + i * 256)); + } else { + ap8.set_x((int)(g * 32 + ks * 8)); + ap8.set_y((int)(r0 + i * 8)); + Ai = xesimd::lsc_load_2d(ap8); + } #pragma unroll for (int j = 0; j < 2; ++j) c[i][j] = xmx::dpas<8, 8, int32_t, int32_t, uint32_t, uint32_t, xmx::dpas_argument_type::u4, @@ -255,7 +267,7 @@ int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const flo acc[i][j].template select<16, 1>(m * 16) += tt; } } - barrier(); + if constexpr (SLM) barrier(); } if (!live) return; #pragma unroll @@ -296,8 +308,12 @@ int w4a16_esimd(sycl::queue& q, int rc, const void* a, bool a_bf16, int64_t ars, int w4a8i8_esimd(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const float* sat, int64_t mpad, const uint32_t* ct, const uint32_t* sz, int64_t M, int64_t N, int64_t K, char* yb, uint32_t ydt, int64_t yrs) { - if (N > 8192) return launch_i8(q, ap, asumt, sat, mpad, ct, sz, M, N, K, yb, ydt, yrs); - return launch_i8(q, ap, asumt, sat, mpad, ct, sz, M, N, K, yb, ydt, yrs); + /* A staged in SLM a work-group pays only on a long K over a narrow output (down's 5120 x + * 17408); everywhere else each thread's own L1-hit 2D loads win, rows fastest. Probe w4p.cpp at + * M = 2048, int8 TOPS, SLM -> per-thread: gate_up 97 -> 115, inz 81 -> 96, qg 73 -> 80, out + * 90 -> 108, kv 72 -> 92; down 77 -> 74 (kept); every output identical. */ + if (N <= 8192 && K >= 16384) return launch_i8(q, ap, asumt, sat, mpad, ct, sz, M, N, K, yb, ydt, yrs); + return launch_i8(q, ap, asumt, sat, mpad, ct, sz, M, N, K, yb, ydt, yrs); } } // namespace xe2