Archived
libxe2: the w2a8 draft head on register int8 DPAS -- 4.2 -> 3.05 ms a round
dpas_s8u8 (xe2_common.h: SPV_INTEL_subgroup_matrix_multiply_accumulate's int8 form, K = 32, A signed, B unsigned; layout checked in a probe) takes a lane's column of codes as B straight from registers, so the group sums no longer go through a local-memory tile and two sub-group barriers a step; the activation is staged once a work-group as A columns, and a step's codes are one 8- or 16-byte load. The group terms are libref's, in order, as before: bit-identical to the libref-checked sub-group form on 50/50 live calls (a temporary cross-check), fixture 228/228. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
1 parent
06bc5475cb
commit
006452c279
2 files changed
+52
-38
No files matched your search
@@ -24,6 +24,8 @@ typedef int xe2_int8 __attribute__((ext_vector_type(8)));
|
||||
typedef float xe2_float8 __attribute__((ext_vector_type(8)));
|
||||
extern SYCL_EXTERNAL xe2_float8 __spirv_SubgroupMatrixMultiplyAccumulateINTEL(int, xe2_short8, xe2_int8,
|
||||
xe2_float8, int);
|
||||
extern SYCL_EXTERNAL xe2_int8 __spirv_SubgroupMatrixMultiplyAccumulateINTEL(int, xe2_short8, xe2_int8,
|
||||
xe2_int8, int);
|
||||
|
||||
namespace xe2 {
|
||||
|
||||
@@ -78,6 +80,11 @@ int xmx_f16_scaled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl
|
||||
inline xe2_float8 dpas_f16(xe2_short8 a, xe2_int8 b, xe2_float8 c) {
|
||||
return __spirv_SubgroupMatrixMultiplyAccumulateINTEL(16, a, b, c, 0xC00); /* A, B packed fp16 */
|
||||
}
|
||||
/* The int8 form, K = 32, exact in int32: lane l holds A[0..7][2l, 2l + 1] (a pair of int8 a short),
|
||||
* lane n B's column n as eight dwords of four consecutive k; A signed, B unsigned (0x31). */
|
||||
inline xe2_int8 dpas_s8u8(xe2_short8 a, xe2_int8 b, xe2_int8 c) {
|
||||
return __spirv_SubgroupMatrixMultiplyAccumulateINTEL(32, a, b, c, 0x31);
|
||||
}
|
||||
|
||||
/* Runs `submit`, turning a SYCL exception into RAD_E_DEVICE with its message. */
|
||||
template <typename F>
|
||||
|
||||
+45
-38
@@ -288,14 +288,28 @@ int zp_gemv(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint
|
||||
});
|
||||
}
|
||||
|
||||
/* The same group terms with the integer sums on XMX: an int8 DPAS (8 x 32 by 32 x 16) forms each
|
||||
* group's sum of a * code exactly in int32 for up to 8 rows and 16 columns at once. The codes
|
||||
* unpack straight into the packed B operand: four codes along k are one dword, and the step
|
||||
* order's swap of bits 2 and 3 of k moves whole dwords, so the activation is used as stored.
|
||||
* A sub-group owns 16 columns and walks the groups in order, each lane adding its column's terms
|
||||
* as the per-output form does -- libref's chain, at one read of the weight. */
|
||||
/* The same group terms with the integer sums on XMX: an int8 DPAS on registers (dpas_s8u8, 8 x 32
|
||||
* by 32 x 16) forms each group's sum of a * code exactly in int32 for up to 8 rows and 16 columns
|
||||
* at once. The codes unpack straight into a lane's B column: four codes along k are one dword, and
|
||||
* the step order's swap of bits 2 and 3 of k moves whole dwords, so the activation is used as
|
||||
* stored (staged once a work-group as A columns). A sub-group owns 16 columns and walks the groups
|
||||
* in order, each lane adding its column's terms as the per-output form does -- libref's chain, at
|
||||
* one read of the weight and without the B tile's trip through local memory. */
|
||||
constexpr int kZpDpasSgs = 16;
|
||||
|
||||
/* The 4 codes of logical dword j of a 32-column step, from the step's codes in registers: 8 bytes
|
||||
* (u2, a byte a dword) or 16 (u4, two). */
|
||||
template <int CBITS>
|
||||
inline uint32_t zp_dword_reg(const uint32_t* w, int j) {
|
||||
if constexpr (CBITS == 2) {
|
||||
const uint32_t b = (w[j / 4] >> (8 * (j % 4))) & 0xFFu;
|
||||
return (b & 3u) | ((b >> 2) & 3u) << 8 | ((b >> 4) & 3u) << 16 | ((b >> 6) & 3u) << 24;
|
||||
} else {
|
||||
const uint32_t h = (w[j / 2] >> (16 * (j % 2))) & 0xFFFFu;
|
||||
return (h & 15u) | ((h >> 4) & 15u) << 8 | ((h >> 8) & 15u) << 16 | ((h >> 12) & 15u) << 24;
|
||||
}
|
||||
}
|
||||
|
||||
template <int CBITS>
|
||||
inline uint32_t zp_dword(const uint8_t* crow, int64_t d) { /* the 4 codes of logical dword d */
|
||||
if constexpr (CBITS == 2) {
|
||||
@@ -312,24 +326,22 @@ int zp_dpas(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint
|
||||
const char* sumb, uint32_t sumdt, int64_t asum_s, int64_t mpad, const uint8_t* bb, int64_t brow,
|
||||
const char* bsb, char* yb, uint32_t ydt, int64_t ys_r, int64_t ys_c, int64_t N, int64_t K,
|
||||
int64_t group, int mm) {
|
||||
namespace jm = sycl::ext::oneapi::experimental::matrix;
|
||||
const int64_t G = K / group;
|
||||
const size_t wgs = (size_t)((N + 16 * kZpDpasSgs - 1) / (16 * kZpDpasSgs));
|
||||
return submit([&] {
|
||||
q.submit([&](sycl::handler& h) {
|
||||
sycl::local_accessor<int8_t, 1> As(sycl::range<1>((size_t)(8 * K)), h);
|
||||
sycl::local_accessor<uint32_t, 1> Bs(sycl::range<1>((size_t)kZpDpasSgs * 8 * 16), h);
|
||||
sycl::local_accessor<int32_t, 1> Cs(sycl::range<1>((size_t)kZpDpasSgs * 8 * 16), h);
|
||||
/* the activation as A columns: at[(k / 2) * 8 + m] is the pair (a[m][k], a[m][k + 1]) */
|
||||
sycl::local_accessor<uint16_t, 1> At(sycl::range<1>((size_t)(4 * K)), h);
|
||||
h.parallel_for(sycl::nd_range<1>{wgs * kZpDpasSgs * kSub, (size_t)kZpDpasSgs * kSub},
|
||||
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
||||
const sycl::sub_group sg = it.get_sub_group();
|
||||
const int s = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id();
|
||||
const int t = (int)it.get_local_id(0);
|
||||
for (int64_t i = t; i < 2 * K; i += kZpDpasSgs * kSub) { /* 8 rows, 4 bytes at a time */
|
||||
const int64_t m = i / (K / 4), c = (i % (K / 4)) * 4;
|
||||
uint32_t v = 0u;
|
||||
if (m < mm) v = *(const uint32_t*)(ab + m * ars + c);
|
||||
*(uint32_t*)&As[m * K + c] = v;
|
||||
for (int64_t i = t; i < 4 * K; i += kZpDpasSgs * kSub) {
|
||||
const int64_t m = i % 8, kp = i / 8;
|
||||
uint16_t v = 0;
|
||||
if (m < mm) v = *(const uint16_t*)(ab + m * ars + 2 * kp);
|
||||
At[i] = v;
|
||||
}
|
||||
sycl::group_barrier(it.get_group());
|
||||
const int64_t n0 = ((int64_t)it.get_group(0) * kZpDpasSgs + s) * 16;
|
||||
@@ -344,44 +356,38 @@ int zp_dpas(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint
|
||||
sa[m] = m < mm ? rad_load_f32(sab, sadt, m * sa_rs) : 0.0f;
|
||||
acc[m] = 0.0f;
|
||||
}
|
||||
uint32_t* bt = &Bs[s * 8 * 16];
|
||||
int32_t* ct = &Cs[s * 8 * 16];
|
||||
auto lb = sycl::address_space_cast<sycl::access::address_space::local_space,
|
||||
sycl::access::decorated::no>((int8_t*)bt);
|
||||
auto lc = sycl::address_space_cast<sycl::access::address_space::local_space,
|
||||
sycl::access::decorated::no>(ct);
|
||||
jm::joint_matrix<sycl::sub_group, int8_t, jm::use::a, 8, 32, jm::layout::row_major> ma;
|
||||
jm::joint_matrix<sycl::sub_group, int8_t, jm::use::b, 32, 16, jm::layout::ext_intel_packed> mb;
|
||||
jm::joint_matrix<sycl::sub_group, int32_t, jm::use::accumulator, 8, 16> mc;
|
||||
for (int64_t g = 0; g < G; ++g) {
|
||||
jm::joint_matrix_fill(sg, mc, 0);
|
||||
xe2_int8 c(0);
|
||||
for (int64_t k0 = g * group; k0 < (g + 1) * group; k0 += 32) {
|
||||
/* stored dword r of this step holds logical dword (r & ~3) | swap(r & 3) */
|
||||
uint32_t w[CBITS]; /* the step's codes: 8 bytes (u2) or 16 (u4) */
|
||||
if constexpr (CBITS == 2) {
|
||||
const sycl::vec<uint32_t, 2> v = *(const sycl::vec<uint32_t, 2>*)(crow + k0 / 4);
|
||||
w[0] = v[0]; w[1] = v[1];
|
||||
} else {
|
||||
const sycl::vec<uint32_t, 4> v = *(const sycl::vec<uint32_t, 4>*)(crow + k0 / 2);
|
||||
for (int u = 0; u < 4; ++u) w[u] = v[u];
|
||||
}
|
||||
xe2_int8 b;
|
||||
for (int r = 0; r < 8; ++r) {
|
||||
const int ld = (r & ~3) | ((r & 1) << 1) | ((r & 2) >> 1);
|
||||
bt[r * 16 + lane] = zp_dword<CBITS>(crow, k0 / 4 + ld);
|
||||
b[r] = (int)zp_dword_reg<CBITS>(w, ld);
|
||||
}
|
||||
sycl::group_barrier(sg);
|
||||
auto la = sycl::address_space_cast<sycl::access::address_space::local_space,
|
||||
sycl::access::decorated::no>(&As[k0]);
|
||||
jm::joint_matrix_load(sg, ma, la, (size_t)K);
|
||||
jm::joint_matrix_load(sg, mb, lb, 64);
|
||||
jm::joint_matrix_mad(sg, mc, ma, mb, mc);
|
||||
sycl::group_barrier(sg);
|
||||
const sycl::vec<uint16_t, 8> av = *(const sycl::vec<uint16_t, 8>*)&At[(k0 / 2 + lane) * 8];
|
||||
xe2_short8 a;
|
||||
for (int m = 0; m < 8; ++m) a[m] = (short)av[m];
|
||||
c = dpas_s8u8(a, b, c);
|
||||
}
|
||||
jm::joint_matrix_store(sg, mc, lc, 16, jm::layout::row_major);
|
||||
sycl::group_barrier(sg);
|
||||
if (live) {
|
||||
const float sb = sc.at(n, g);
|
||||
const float z = (float)(int32_t)zp.at(n, g);
|
||||
for (int m = 0; m < 8; ++m) {
|
||||
if (m >= mm) break;
|
||||
const float sub = (float)ct[m * 16 + lane] - z * rad_load_f32(sumb, sumdt, (g * mpad + m) * asum_s);
|
||||
const float sub = (float)c[m] - z * rad_load_f32(sumb, sumdt, (g * mpad + m) * asum_s);
|
||||
const float term = sub * sa[m] * sb;
|
||||
acc[m] += term;
|
||||
}
|
||||
}
|
||||
sycl::group_barrier(sg);
|
||||
}
|
||||
if (live)
|
||||
for (int m = 0; m < 8; ++m) {
|
||||
@@ -427,7 +433,8 @@ int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream st) {
|
||||
const int64_t sa_rs = row_stride(AS, 1);
|
||||
const int64_t asum_s = ASUM->rank ? ASUM->stride[ASUM->rank - 1] : 1;
|
||||
const int64_t ys_r = row_stride(Y, N), ys_c = Y->rank ? Y->stride[Y->rank - 1] : 1;
|
||||
if (M <= 8 && (cbits == 2 || cbits == 4) && group % 32 == 0 && K * 8 <= 64 * 1024 && ars % 4 == 0 &&
|
||||
if (M <= 8 && (cbits == 2 || cbits == 4) && group % 32 == 0 && K * 8 <= 64 * 1024 && ars % 2 == 0 &&
|
||||
brow % 16 == 0 && rad_ptr_aligned(B->data, 16) &&
|
||||
!std::getenv("XE2_ZP_GEMV")) {
|
||||
auto dp = cbits == 2 ? zp_dpas<2> : zp_dpas<4>;
|
||||
int rc = dp(*q, (const int8_t*)A->data, ars, (const char*)AS->data, AS->dtype, sa_rs,
|
||||
|
||||
Reference in new issue
Block a user