diff --git a/libxe2/xe2_gemm_w4_esimd.cpp b/libxe2/xe2_gemm_w4_esimd.cpp index 0ac69f49..bac2ae33 100644 --- a/libxe2/xe2_gemm_w4_esimd.cpp +++ b/libxe2/xe2_gemm_w4_esimd.cpp @@ -13,8 +13,8 @@ * shape 466 -> 210 us; at M = 4 row loads were faster). * * w4a8i8_esimd, any M: int8 DPAS with the tiled codes as its u4 B operand, 8 codes a dword, against - * the prepass's activation (bytes of each 8 evens first, the codes' nibble order), staged in local - * memory a work-group (launch_i8). A group is four K-32 steps of DPAS into int32 -- exact -- and then + * the prepass's activation (bytes of each 8 evens first, the codes' nibble order), read by each + * thread with 2D loads that hit L1 (launch_i8). A group is four K-32 steps of DPAS into int32 -- exact -- and then * each output's term ((sum - zero * asum) * a_scale) * scale is added to its f32 total in group * order: libref's result bit for bit. */ #include "xe2_common.h" @@ -144,21 +144,19 @@ int launch16(sycl::queue& q, const void* av, int64_t ars, int mm, const uint32_t }); } -/* The prefill GEMM. A work-group of 8 threads shares 16 rows: each group's activation ([step][row][32 - * bytes], 2 KiB) is staged in local memory once, double-buffered, threads 0-3 a step each by one 2D - * load, and every thread reads its A operands from there. A thread owns 16 rows by 32 columns (two - * 8-row blocks by two tiles): 128 registers, so 8 threads an EU, and the group's epilogue on whole - * [8 rows][16] blocks against the factors broadcast once. RF orders the work-groups rows fastest, so - * 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 +/* The prefill GEMM. A work-group of 8 threads shares 16 rows, and each thread reads its A operands + * ([8 rows][32 bytes] a K-32 step) with its own 2D loads, which hit L1. (They were staged in local + * memory a work-group, double-buffered behind a barrier a group: 15-30% slower.) A thread owns 16 + * rows by 32 columns (two 8-row blocks by two tiles): 128 registers, so 8 threads an EU, and the + * group's epilogue on whole [8 rows][16] blocks against the factors broadcast once. Probe w4p.cpp + * at M = 2048, int8 TOPS, from the first form: gate_up 27 -> 115, inz 27 -> 96, down 24 -> 79, + * N = 5120 K = 6144 29 -> 108; every output identical to the first form's. */ +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) { constexpr int RB = 2, R = RB * 8; /* rows a work-group (32 rows on the 256-register file: 0.6x) */ constexpr int kWg = 8; /* threads a work-group, two tiles each */ - constexpr int kStep = R * 32, kBuf = 4 * kStep; const int64_t G = K / 128, RG = (M + R - 1) / R, CG = N / 32; const int64_t cgw = (CG + kWg - 1) / kWg; namespace intelex = sycl::ext::intel::experimental; @@ -166,10 +164,18 @@ 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; - 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; + /* rows fastest: the work-groups over one 256-column slice of the weight run together and it + * stays in L2. With BAND, in bands of that many row groups, a band's A kept in L2 across its + * column slices: a long K over a narrow output (down's) re-read all of A from memory a slice */ + int64_t rg = wg % RG, cw = wg / RG; + if constexpr (BAND > 0) { + const int64_t band = wg / (BAND * cgw), left = RG - band * BAND; + const int64_t rb = left < BAND ? left : (int64_t)BAND, inner = wg - band * BAND * cgw; + rg = band * BAND + inner % rb; + cw = inner / rb; + } const int64_t cg = cw * kWg + tl; const bool live = cg < CG; const int64_t r0 = rg * R, tile0 = (live ? cg : 0) * 2; @@ -185,29 +191,15 @@ int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const flo * 91 -> 98, inz 74 -> 81, down 64 -> 76, out 86 -> 90. (One 16 x 16 2D load of a tile's * group instead of four block loads: down 71, out 74.) */ namespace xesimd = sycl::ext::intel::experimental::esimd; - xesimd::config_2d_mem_access apl((const uint32_t*)ap, sw, sh, sw, tl * 8, (int)r0); const uint32_t* cb0 = ct + tile0 * (K / 8) * 16; const int kr = (int)(K / 8); 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 */ + /* each thread reads its own A operands with 2D loads, which hit L1 (the work-group's + * threads read the same rows): no staging in SLM, 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 (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); - } - }; - if constexpr (SLM) { - stage(0, 0); - barrier(); - } for (int64_t g = 0; g < G; ++g) { - const int buf = (int)(g & 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 */ @@ -239,14 +231,9 @@ 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; - 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); - } + ap8.set_x((int)(g * 32 + ks * 8)); + ap8.set_y((int)(r0 + i * 8)); + const simd 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, @@ -267,7 +254,6 @@ 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; } } - if constexpr (SLM) barrier(); } if (!live) return; #pragma unroll @@ -308,12 +294,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) { - /* 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); + /* A read per thread from L1 instead of staged in SLM a work-group, rows fastest; a long K over a + * narrow output (down's 5120 x 17408) in bands of 32 row groups. Probe w4p.cpp at M = 2048, int8 + * TOPS, SLM -> now: gate_up 97 -> 115, inz 81 -> 96, qg 73 -> 80..98, out 90 -> 108, kv 72 -> 92, + * down 76 -> 79 (M = 1024: 61 -> 70, M = 4096: 80 -> 86); every output identical. */ + if (N <= 8192 && K >= 16384) return launch_i8<32>(q, ap, asumt, sat, mpad, ct, sz, M, N, K, yb, ydt, yrs); + return launch_i8<0>(q, ap, asumt, sat, mpad, ct, sz, M, N, K, yb, ydt, yrs); } } // namespace xe2