/* xe2_gemm.cpp -- the unquantised GEMMs: gemm_nt (Y = A B^T) and logits_gemm, its lm_head twin. * * Correctness first (docs/SYCL_PORTING.md ยง4.8, Phase 3): one work-item an output, accumulating * a * b in f32 over ascending k from zero, no fused multiply-add, then one store: libref's * gemm_plain exactly. It reads each weight row once per output row, so it is slow; the XMX kernels * of Phase 4 are checked against it and against libref. * * A is read through its row and column strides (an activation may be a slice of a wider arena * buffer); B is the weight, [N, K]. A joined head (codes with a scale plane) and a non-float * weight are refused: those are gemm_nt_q's. */ #include "xe2_common.h" #include "xe2_ops.h" #include "rad_planes.h" #include #include #include namespace xe2 { namespace { /* libref's row_stride: fold trailing axes until they span `cols`. */ int64_t row_stride(const RadTensor* t, int64_t cols) { if (!t || t->rank < 2) return cols; int64_t span = 1; for (int i = (int)t->rank - 1; i >= 0; --i) { span *= t->shape[i]; if (span == cols) return (i > 0) ? t->stride[i - 1] : cols; if (span > cols) break; } return t->stride[t->rank - 2]; } bool is_float(uint32_t dt) { return dt == RAD_F32 || dt == RAD_F16 || dt == RAD_BF16 || dt == RAD_F8E4M3 || dt == RAD_F8E5M2; } /* A plane of a joined head, found on the card (the header lives in device memory). */ struct HPlane { const char* p = nullptr; uint32_t dt = 0; int64_t row_bytes = 0, br = 0, bc = 0; float at(int64_t i, int64_t j) const { return rad_load_f32(p + i * row_bytes, dt, j); } float cover(int64_t r, int64_t c) const { return at(br > 0 ? r / br : 0, bc > 0 ? c / bc : 0); } }; inline bool role_is(const char* r, const char* want) { for (int i = 0; i < RAD_ENC_STR; ++i) { if (r[i] != want[i]) return false; if (!want[i]) return true; } return true; } inline HPlane hplane(const char* base, const char* role) { HPlane v; const RadPlanesHeader* h = (const RadPlanesHeader*)base; if (h->magic != RAD_PLANES_MAGIC) return v; const RadPlanesEntry* e = (const RadPlanesEntry*)(base + sizeof(RadPlanesHeader)); for (int i = 0; i < h->n_planes && i < RAD_ENC_MAX_PLANES; ++i) if (role_is(e[i].role, role)) { v.p = base + e[i].offset; v.dt = e[i].dtype; v.row_bytes = e[i].row_bytes; v.br = e[i].block[0]; v.bc = e[i].block[1]; return v; } return v; } inline float gelu(float x) { return 0.5f * x * (1.0f + (float)sycl::erf((double)(x * 0.70710678118654752f))); } inline float gelu_tanh(float x) { const float k0 = 0.7978845608028654f, k1 = 0.044715f; return 0.5f * x * (1.0f + (float)sycl::tanh((double)(k0 * (x + k1 * x * x * x)))); } /* libref's gemm_nt_impl: acc += a * b over ascending k, then the bias, stored; then the activation * on the stored value and the residual added to it, each stored (gemm_epilogue). `joined`: B is a * head of E4M3 / int8 codes and a scale plane, each element decoded to code * scale in f32 first * (gemm_scaled_rows). */ int gemm_plain(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, const char* op, bool bias_form = false, bool joined = false) { const int nt = bias_form ? 5 : 3; if (!a || a->n_t < nt || !a->t[0].data || !a->t[1].data || !a->t[nt - 1].data) return RAD_E_INVAL; const RadTensor *A = &a->t[0], *B = &a->t[1], *Y = &a->t[nt - 1]; const RadTensor* bias = bias_form ? rad_arg_in(a, 2) : nullptr; const RadTensor* res = bias_form ? rad_arg_in(a, 3) : nullptr; int act = 0; if (bias_form) { const char* as = rad_args_gets_or(a, "act", "none"); if (!std::strcmp(as, "none")) act = 0; else if (!std::strcmp(as, "gelu")) act = 1; else if (!std::strcmp(as, "gelu_tanh")) act = 2; else return RAD_E_INVAL; } const int64_t K = rad_args_geti_or(a, kkey, B->rank >= 2 ? B->shape[B->rank - 1] : 0); const int64_t N = rad_args_geti_or(a, nkey, B->rank >= 2 ? B->shape[B->rank - 2] : 0); if (K <= 0 || N <= 0) return RAD_E_SHAPE; const int64_t M = rad_tensor_numel(Y) / N; if (M <= 0) return RAD_E_SHAPE; if (!is_float(A->dtype) || (!joined && !is_float(B->dtype))) return refuse("%s: dtype pair %u x %u is not served (a quantised weight is gemm_nt_q's)", op, A->dtype, B->dtype); if (rad_tensor_numel(A) < M * K || (!joined && rad_tensor_numel(B) < N * K)) return RAD_E_SHAPE; if (bias && rad_tensor_numel(bias) < N) return RAD_E_SHAPE; if (res && rad_tensor_numel(res) < M * N) return RAD_E_SHAPE; const int64_t ars = row_stride(A, K), acs = A->rank >= 2 ? A->stride[A->rank - 1] : 1; const int64_t brs = joined ? 0 : row_stride(B, K), bcs = !joined && B->rank >= 2 ? B->stride[B->rank - 1] : 1; const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1; const int64_t rrs = res ? row_stride(res, N) : 0, rcs = res && res->rank >= 2 ? res->stride[res->rank - 1] : 1; const int64_t bis = bias && bias->rank ? bias->stride[bias->rank - 1] : 1; if (res && res->data != Y->data) { /* libref snapshots an overlapping residual; here an element reads its residual before its * store, which is the same only when the residual is Y itself or apart from it. */ const char *rl = (const char*)res->data, *yl = (const char*)Y->data; const int64_t span = (M - 1) * std::max(yrs, rrs) + N * std::max(ycs, rcs); const int64_t eb = 4; if (rl < yl + span * eb && yl < rl + span * eb && (rrs != yrs || rcs != ycs || res->dtype != Y->dtype)) return refuse("%s: a residual overlapping y in another layout", op); } const char *ab = (const char*)A->data, *bb = (const char*)B->data; const char* bsb = bias ? (const char*)bias->data : nullptr; const char* rb = res ? (const char*)res->data : nullptr; char* yb = (char*)Y->data; const uint32_t adt = A->dtype, bdt = B->dtype, ydt = Y->dtype; const uint32_t bidt = bias ? bias->dtype : 0, rdt = res ? res->dtype : 0; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; const size_t wn = 64; const size_t nn = (size_t)((N + (int64_t)wn - 1) / (int64_t)wn) * wn; return submit([&] { syclex::nd_launch(*q, sycl::nd_range<2>{{(size_t)M, nn}, {1, wn}}, [=](sycl::nd_item<2> it) { const int64_t m = (int64_t)it.get_global_id(0), n = (int64_t)it.get_global_id(1); if (n >= N) return; float acc = 0.0f; if (joined) { const HPlane cp = hplane(bb, "codes"), sp = hplane(bb, "scale"); if (!cp.p || !sp.p) return; /* the layout hook guarantees a joined head */ for (int64_t k = 0; k < K; ++k) acc += rad_load_f32(ab, adt, m * ars + k * acs) * (cp.at(n, k) * sp.cover(n, k)); } else { for (int64_t k = 0; k < K; ++k) acc += rad_load_f32(ab, adt, m * ars + k * acs) * rad_load_f32(bb, bdt, n * brs + k * bcs); } const int64_t o = m * yrs + n * ycs; const float rv = rb ? rad_load_f32(rb, rdt, m * rrs + n * rcs) : 0.0f; float v = acc; if (bsb) v += rad_load_f32(bsb, bidt, n * bis); rad_store_f32(yb, ydt, o, v); if (act || rb) { if (act == 1) rad_store_f32(yb, ydt, o, gelu(rad_load_f32(yb, ydt, o))); else if (act == 2) rad_store_f32(yb, ydt, o, gelu_tanh(rad_load_f32(yb, ydt, o))); if (rb) rad_store_f32(yb, ydt, o, rv + rad_load_f32(yb, ydt, o)); } }); }); } } // namespace int gemm_nt(const RadArgs* a, RadStream s) { return gemm_plain(a, s, "N", "K", "gemm_nt"); } int gemm_nt_bias(const RadArgs* a, RadStream s) { return gemm_plain(a, s, "N", "K", "gemm_nt_bias", true); } int logits_gemm(const RadArgs* a, RadStream s) { return gemm_plain(a, s, "n_vocab", "n_embd", "logits_gemm"); } int logits_gemm_head(const RadArgs* a, RadStream s) { return gemm_plain(a, s, "n_vocab", "n_embd", "logits_gemm", false, true); } /* The joined fp8 head at M <= 16 (decode, verify): a sub-group a vocab column, each lane 16 E4M3 * codes a step (one 16-byte load) inside one scale block, summed against the f32 activation and * scaled once: libref's products code * scale * a regrouped, held to the gemm tolerance and the * KL bound like the other fast GEMVs. The activation is widened to f32 once into the workspace. */ inline float e4m3f(uint8_t b) { const uint32_t sgn = (uint32_t)(b & 0x80) << 24; const uint32_t e = (b >> 3) & 0xF, m = b & 7; if ((b & 0x7F) == 0x7F) return sycl::bit_cast(sgn | 0x7FC00000u); if (e == 0) { const float v = (float)m * (1.0f / 512.0f); return sgn ? -v : v; } return sycl::bit_cast(sgn | ((e + 120) << 23) | (m << 20)); } /* E4M3 codes as fp16 by bits: ((b & 0x7F) << 7) | ((b & 0x80) << 8) is exactly E4M3(b) * 2^-8 * (its subnormals land on fp16 subnormals), so bytes (0, 2) and (1, 3) of a word are each a half * pair after two masks and shifts. The activation is laid out in that order, as * fp16(x / R * 2^8), R = max|x| / 2 of the row (|a16| <= 512); a lane's sixteen products sum in * half2, scaled once by the block's scale in f32. An i8 head, or a scale block narrower than 16 * columns, takes the f32 form. */ template int head_gemv(sycl::queue& q, const sycl::half* a16, const float* rr, const char* bb, char* yb, uint32_t ydt, int64_t N, int64_t K, int64_t yrs, int64_t ycs, int mm) { constexpr size_t wg = 256; const size_t groups = (size_t)((N + 15) / 16); return submit([&] { syclex::nd_launch(q, sycl::nd_range<1>{groups * wg, wg}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { const sycl::sub_group sg = it.get_sub_group(); const int64_t n = (int64_t)it.get_group(0) * 16 + (int64_t)sg.get_group_linear_id(); if (n >= N) return; const HPlane cp = hplane(bb, "codes"), sp = hplane(bb, "scale"); if (!cp.p || !sp.p) return; /* the layout hook guarantees a joined head */ const int lane = (int)sg.get_local_linear_id(); if (cp.dt != RAD_F8E4M3 || !(sp.bc == 0 || sp.bc % 16 == 0)) { /* an i8 head or a narrow scale block: element by element in f32 (a16 carries * 2^8 / R, its permutation undone per element) */ float accg[MM]; for (int r = 0; r < MM; ++r) accg[r] = 0.0f; const char* crow = cp.p + n * cp.row_bytes; for (int64_t k = lane; k < K; k += kSub) { const float w = rad_load_f32(crow, cp.dt, k) * sp.cover(n, k); const int64_t pos = (k & ~(int64_t)3) + ((k & 3) == 1 ? 2 : (k & 3) == 2 ? 1 : (k & 3)); for (int r = 0; r < MM; ++r) { if (r >= mm) break; accg[r] += (float)a16[r * K + pos] * w; } } for (int r = 0; r < MM; ++r) { if (r >= mm) break; const float v = sycl::reduce_over_group(sg, accg[r], sycl::plus()); if (lane == 0) rad_store_f32(yb, ydt, r * yrs + n * ycs, v * rr[r] * (1.0f / 256.0f)); } return; } const uint8_t* crow = (const uint8_t*)cp.p + n * cp.row_bytes; using H2 = sycl::vec; float acc[MM]; for (int r = 0; r < MM; ++r) acc[r] = 0.0f; for (int64_t k = (int64_t)lane * 16; k < K; k += 16 * kSub) { const sycl::vec c = *(const sycl::vec*)(crow + k); H2 w2[8]; for (int j = 0; j < 4; ++j) { const uint32_t x = c[j] & 0x00FF00FFu, y = (c[j] >> 8) & 0x00FF00FFu; w2[2 * j] = sycl::bit_cast

(((x & 0x007F007Fu) << 7) | ((x & 0x00800080u) << 8)); w2[2 * j + 1] = sycl::bit_cast

(((y & 0x007F007Fu) << 7) | ((y & 0x00800080u) << 8)); } const float sc = sp.cover(n, k); for (int r = 0; r < MM; ++r) { if (r >= mm) break; const sycl::vec aw = *(const sycl::vec*)(a16 + r * K + k); H2 p2(0.0f, 0.0f); for (int u = 0; u < 8; ++u) p2 = sycl::fma(sycl::bit_cast

(aw[u]), w2[u], p2); acc[r] += ((float)p2[0] + (float)p2[1]) * sc; } } for (int r = 0; r < MM; ++r) { if (r >= mm) break; const float v = sycl::reduce_over_group(sg, acc[r], sycl::plus()); if (lane == 0) rad_store_f32(yb, ydt, r * yrs + n * ycs, v * rr[r]); } }); }); } /* M <= 8 on XMX (decode too: 3.4 ms against the SIMT form's 3.9 at M = 1): a sub-group 16 vocab columns, a lane one column's 64 codes a step decoded * by the bits above into its slice of a packed fp16 B tile in local memory (the pairs (0, 2) and * (1, 3) of each word are the activation's pair order already), four fp16 DPAS K16 against the * activation; a scale block's sum is taken out of the accumulator and scaled in f32 per column, * so the products are summed in f32 rather than half2. Needs the scale block a multiple of 64 * columns (or one block); else the SIMT form. */ int head_dpas(sycl::queue& q, const sycl::half* a16, const float* rr, const char* bb, char* yb, uint32_t ydt, int64_t N, int64_t K, int64_t yrs, int64_t ycs, int mm) { namespace jm = sycl::ext::oneapi::experimental::matrix; using hf = sycl::half; constexpr int kSgs = 16; const int64_t tiles = (N + 15) / 16; const size_t groups = (size_t)((tiles + kSgs - 1) / kSgs); return submit([&] { q.submit([&](sycl::handler& h) { sycl::local_accessor Bs(sycl::range<1>((size_t)kSgs * 32 * 16), h); sycl::local_accessor Cs(sycl::range<1>((size_t)kSgs * 8 * 16), h); h.parallel_for(sycl::nd_range<1>{groups * kSgs * kSub, (size_t)kSgs * 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 int64_t n0 = ((int64_t)it.get_group(0) * kSgs + s) * 16; if (n0 >= N) return; /* uniform */ const HPlane cp = hplane(bb, "codes"), sp = hplane(bb, "scale"); if (!cp.p || !sp.p) return; if (head_i8_fast(cp.dt, cp.row_bytes, sp.br, sp.bc, sp.dt, K)) return; /* xe2_head_esimd.cpp's */ const int64_t n = sycl::min(n0 + lane, N - 1); const uint8_t* crow = (const uint8_t*)cp.p + n * cp.row_bytes; if (cp.dt != RAD_F8E4M3 || !(sp.bc == 0 || sp.bc % 64 == 0) || cp.row_bytes % 16 || ((uintptr_t)cp.p & 15)) { /* not this form's head (the layout is only known here): a lane its column in * f32, a16's permutation undone per element */ float accg[8]; for (int m = 0; m < 8; ++m) accg[m] = 0.0f; for (int64_t k = 0; k < K; ++k) { const float w = rad_load_f32(crow, cp.dt, k) * sp.cover(n, k); const int64_t pos = (k & ~(int64_t)3) + ((k & 3) == 1 ? 2 : (k & 3) == 2 ? 1 : (k & 3)); for (int m = 0; m < 8; ++m) accg[m] += (float)a16[m * K + pos] * w; } if (n0 + lane < N) for (int m = 0; m < mm; ++m) rad_store_f32(yb, ydt, m * yrs + n * ycs, accg[m] * rr[m] * (1.0f / 256.0f)); return; } const int64_t blk = sp.bc > 0 ? sp.bc : K; uint32_t* bt = &Bs[(size_t)s * 32 * 16]; float* ct = &Cs[(size_t)s * 8 * 16]; auto lc = sycl::address_space_cast(ct); jm::joint_matrix ma; jm::joint_matrix mb; jm::joint_matrix mc; float acc[8]; for (int m = 0; m < 8; ++m) acc[m] = 0.0f; for (int64_t b0 = 0; b0 < K; b0 += blk) { jm::joint_matrix_fill(sg, mc, 0.0f); for (int64_t k0 = b0; k0 < b0 + blk; k0 += 64) { uint32_t c[16]; for (int v = 0; v < 4; ++v) { const sycl::vec c4 = *(const sycl::vec*)(crow + k0 + 16 * v); for (int u = 0; u < 4; ++u) c[4 * v + u] = c4[u]; } for (int j = 0; j < 16; ++j) { const uint32_t x = c[j] & 0x00FF00FFu, y = (c[j] >> 8) & 0x00FF00FFu; bt[(2 * j) * 16 + lane] = ((x & 0x007F007Fu) << 7) | ((x & 0x00800080u) << 8); bt[(2 * j + 1) * 16 + lane] = ((y & 0x007F007Fu) << 7) | ((y & 0x00800080u) << 8); } sycl::group_barrier(sg); for (int kk = 0; kk < 64; kk += 16) { auto la = sycl::address_space_cast(a16 + k0 + kk); jm::joint_matrix_load(sg, ma, la, (size_t)K); auto lb = sycl::address_space_cast((hf*)&bt[(kk / 2) * 16]); jm::joint_matrix_load(sg, mb, lb, 32); jm::joint_matrix_mad(sg, mc, ma, mb, mc); } sycl::group_barrier(sg); } jm::joint_matrix_store(sg, mc, lc, 16, jm::layout::row_major); sycl::group_barrier(sg); const float sc = sp.cover(n, b0); for (int m = 0; m < 8; ++m) acc[m] += ct[m * 16 + lane] * sc; sycl::group_barrier(sg); } if (n0 + lane < N) for (int m = 0; m < mm; ++m) rad_store_f32(yb, ydt, m * yrs + n * ycs, acc[m] * rr[m]); }); }); }); } /* xe2_head_esimd.cpp */ int head_i8_esimd(sycl::queue& q, int rc, const sycl::half* a16, const float* rr, const char* bb, char* yb, uint32_t ydt, int64_t N, int64_t K, int64_t yrs, int64_t ycs, int mm); int logits_gemm_head_gemv(const RadArgs* a, RadStream st) { if (exact_only()) return logits_gemm_head(a, st); if (!a || a->n_t < 3 || !a->t[0].data || !a->t[1].data || !a->t[2].data) return RAD_E_INVAL; const RadTensor *A = &a->t[0], *B = &a->t[1], *Y = &a->t[2]; const int64_t K = rad_args_geti_or(a, "n_embd", 0), N = rad_args_geti_or(a, "n_vocab", 0); if (K <= 0 || N <= 0) return RAD_E_SHAPE; const int64_t M = rad_tensor_numel(Y) / N; if (M < 1 || M > 16 || K % 128 || !is_float(A->dtype) || !rad_ptr_aligned(B->data, 16)) return logits_gemm_head(a, st); const int64_t ars = row_stride(A, K), acs = A->rank >= 2 ? A->stride[A->rank - 1] : 1; const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; /* the XMX form reads 8 activation rows, the ones past M zero; 9-16 rows (a verify step past * depth 7) run it once a block of 8 -- the SIMT kernel took ~100 ms a call there */ static const bool simt = std::getenv("XE2_HEAD_SIMT") != nullptr; const bool dpas = M <= 16 && K % 64 == 0 && !simt; const int64_t mrows = dpas ? (M <= 8 ? 8 : 16) : M; const int64_t abytes = (mrows * K * 2 + 255) / 256 * 256; float* ws = workspace(*q, (size_t)(abytes / 4 + mrows + 64)); if (!ws) return RAD_E_DEVICE; sycl::half* a16 = (sycl::half*)ws; float* rr = (float*)((char*)ws + abytes); const char* ab = (const char*)A->data; const uint32_t adt = A->dtype; int rc = submit([&] { q->parallel_for(sycl::nd_range<1>{(size_t)mrows * 256, 256}, [=](sycl::nd_item<1> it) { const int64_t m = (int64_t)it.get_group(0); const int64_t t = (int64_t)it.get_local_id(0); if (m >= M) { /* uniform: the XMX form's padding rows */ for (int64_t k = t; k < K; k += 256) a16[m * K + k] = (sycl::half)0.0f; if (t == 0) rr[m] = 1.0f; return; } float mx = 0.0f; for (int64_t k = t; k < K; k += 256) mx = sycl::fmax(mx, sycl::fabs(rad_load_f32(ab, adt, m * ars + k * acs))); mx = sycl::reduce_over_group(it.get_group(), mx, sycl::maximum()); const float R = mx > 0.0f ? mx * 0.5f : 1.0f; if (t == 0) rr[m] = R; const float f = 256.0f / R; /* position p of each 4 holds element (p % 2) * 2 + (p / 2): bytes (0, 2), (1, 3) */ for (int64_t k = t; k < K; k += 256) { const int64_t p = k & 3, src = (k & ~(int64_t)3) + (p & 1) * 2 + (p >> 1); a16[m * K + k] = (sycl::half)(rad_load_f32(ab, adt, m * ars + src * acs) * f); } }); }); if (rc < 0) return rc; const char* bb = (const char*)B->data; char* yb = (char*)Y->data; const int mm = (int)M; if (dpas) { /* head_dpas serves the E4M3 head and every form but the int8 one, which it leaves to the * ESIMD kernel (each reads the planes on the card; a form not its own returns at once) */ for (int b0 = 0; b0 < mm; b0 += 8) { const int mb = mm - b0 < 8 ? mm - b0 : 8; const sycl::half* ab0 = a16 + (int64_t)b0 * K; char* yb0 = yb + rad_dtype_bytes(Y->dtype, (int64_t)b0 * yrs); if (int r = head_dpas(*q, ab0, rr + b0, bb, yb0, Y->dtype, N, K, yrs, ycs, mb); r != RAD_OK) return r; if (int r = head_i8_esimd(*q, mb == 1 ? 1 : (mb <= 2 ? 2 : (mb <= 4 ? 4 : 8)), ab0, rr + b0, bb, yb0, Y->dtype, N, K, yrs, ycs, mb); r != RAD_OK) return r; } return RAD_OK; } if (mm == 1) return head_gemv<1>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm); if (mm <= 8) return head_gemv<8>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm); return head_gemv<16>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm); } /* libref's ref_layout_head: operand 1 (the head) of codes and one scale, joined. */ int head_layout(const RadParam*, int, int operand, const RadEncoding* enc, const int* sel, const RadTensor* planes, int n, RadLayout* out) { if (n < 2 || operand != 1) return RAD_E_UNSUPPORTED; if (!rad_enc_is(enc, "affine") || enc->transform[0] || enc->n_planes != 2) return RAD_E_DTYPE; if (!sel || n != 2 || sel[0] < 0 || sel[0] >= enc->n_planes || sel[1] < 0 || sel[1] >= enc->n_planes) return RAD_E_DTYPE; const RadEncPlane& c = enc->plane[sel[0]]; const RadEncPlane& sc = enc->plane[sel[1]]; if (std::strncmp(c.role, "codes", RAD_ENC_STR) != 0 || std::strncmp(sc.role, "scale", RAD_ENC_STR) != 0) return RAD_E_DTYPE; const bool fs = sc.dtype == RAD_F32 || sc.dtype == RAD_F16 || sc.dtype == RAD_BF16; if (!(c.kind == RAD_PLANE_TILED && c.block[0] == 1 && c.block[1] == 1 && (c.dtype == RAD_F8E4M3 || c.dtype == RAD_I8) && sc.kind == RAD_PLANE_TILED && fs)) return RAD_E_DTYPE; return rad_planes_layout(enc, sel, planes, n, out); } int head_relayout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel, const RadTensor* planes, int n, void* dst, int64_t dst_bytes) { RadLayout lay{}; const int st = head_layout(p, n_p, operand, enc, sel, planes, n, &lay); if (st != RAD_OK) return st; return rad_planes_relayout(enc, sel, planes, n, dst, dst_bytes); } int head_unrelayout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel, const void* src, int64_t src_bytes, const RadTensor* planes, int n) { RadLayout lay{}; const int st = head_layout(p, n_p, operand, enc, sel, planes, n, &lay); if (st != RAD_OK) return st; return rad_planes_unrelayout(enc, sel, src, src_bytes, planes, n); } } // namespace xe2