diff --git a/libxe2/xe2_gemv_m2.cpp b/libxe2/xe2_gemv_m2.cpp index b56ee874..086bf576 100644 --- a/libxe2/xe2_gemv_m2.cpp +++ b/libxe2/xe2_gemv_m2.cpp @@ -351,13 +351,33 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8 const uint8_t* ab = (const uint8_t*)A->data; const char* sab = (const char*)AS->data; const uint32_t sadt = AS->dtype; - /* the activation prepass: a work-group a (row, 2048 columns), each forming the row's factor */ + /* the activation prepass: a work-group a (row, 2048 columns), each forming the row's factor; + * in the same launch, when a kernel below reads it, each column's largest exponent (further + * work-groups, 256 columns each) -- a launch of its own cost more than its work */ const int64_t chunks = raw ? (acols + 2047) / 2048 : 1; const int64_t cspan = raw ? 2048 : acols; + const bool want_emax = es || !(raw && splits == 1); + const int64_t agroups = mpad * chunks, egroups = want_emax ? (N + 255) / 256 : 0; int rc = submit([&] { - q.parallel_for(sycl::nd_range<1>{(size_t)(mpad * chunks) * 256, 256}, [=](sycl::nd_item<1> it) { - const int64_t m = (int64_t)it.get_group(0) / chunks, ch = (int64_t)it.get_group(0) % chunks; + q.parallel_for(sycl::nd_range<1>{(size_t)(agroups + egroups) * 256, 256}, [=](sycl::nd_item<1> it) { + const int64_t gi = (int64_t)it.get_group(0); const int64_t t = (int64_t)it.get_local_id(0); + if (gi >= agroups) { /* uniform over the work-group */ + const int64_t n = (gi - agroups) * 256 + t; + if (n >= N) return; + const uint8_t* er = expe0 + n * brs; + int32_t mx = 0; + int64_t g = 0; + if ((kb & 15) == 0 && ((uintptr_t)er & 15) == 0) + for (; g < kb; g += 16) { + const sycl::vec ev = *(const sycl::vec*)(er + g); + for (int u = 0; u < 16; ++u) mx = sycl::max(mx, (int32_t)ev[u]); + } + for (; g < kb; ++g) mx = sycl::max(mx, (int32_t)er[g]); + emax[n] = mx; + return; + } + const int64_t m = gi / chunks, ch = gi % chunks; float mx = 0.0f; if (m < M) for (int64_t c = t; c < scols; c += 256) mx = sycl::fmax(mx, rad_load_f32(sab, sadt, m * srs + c)); @@ -372,19 +392,6 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8 : (half)0.0f; } }); - if (raw && splits == 1) return; /* the register form reads e_max itself */ - q.parallel_for(sycl::range<1>((size_t)N), [=](sycl::id<1> i) { - const uint8_t* er = expe0 + (int64_t)i[0] * brs; - int32_t mx = 0; - int64_t g = 0; - if ((kb & 15) == 0 && ((uintptr_t)er & 15) == 0) - for (; g < kb; g += 16) { - const sycl::vec ev = *(const sycl::vec*)(er + g); - for (int u = 0; u < 16; ++u) mx = sycl::max(mx, (int32_t)ev[u]); - } - for (; g < kb; ++g) mx = sycl::max(mx, (int32_t)er[g]); - emax[i[0]] = mx; - }); }); if (rc < 0) return rc; char* yb = (char*)Y->data; diff --git a/libxe2/xe2_gemv_m2_esimd.cpp b/libxe2/xe2_gemv_m2_esimd.cpp index b08a0cc1..ca6971dd 100644 --- a/libxe2/xe2_gemv_m2_esimd.cpp +++ b/libxe2/xe2_gemv_m2_esimd.cpp @@ -55,17 +55,8 @@ int launch(sycl::queue& q, const half* ae, const uint8_t* codes0, const uint8_t* const simd coff = (col - (uint32_t)n0) * (uint32_t)(K / 2); const simd eoff = col * (uint32_t)brs; const simd moff = col * (uint32_t)kg; - /* the column's largest exponent: read here when this thread has the whole K, else the - * caller's emax pass */ - simd mx = 0; - if (splits == 1) { - for (int64_t g = 0; g < kg; g += 4) { - const simd e = gather((const uint32_t*)(expe0 + g), eoff); - _Pragma("unroll") for (int b = 0; b < 4; ++b) mx = max(mx, simd((e >> (8 * b)) & 0xFF)); - } - } else { - mx = gather(emax, col * 4u); - } + /* the column's largest exponent, from the caller's prepass */ + const simd mx = gather(emax, col * 4u); simd c[MT]; _Pragma("unroll") for (int i = 0; i < MT; ++i) c[i] = 0.0f; for (int64_t g0 = g0s; g0 < g1s; g0 += kKs) {