/* xe2_zp.h -- what the zero-point GEMMs share (xe2_gemm_zp.cpp, xe2_gemm_w4.cpp): `b_scale`'s * joined planes found on the device, and an operand's row stride. */ #pragma once #include "xe2_common.h" #include "rad_planes.h" namespace xe2 { /* One plane of the joined operand, found on the device. */ struct DPlane { const char* p = nullptr; uint32_t dt = 0; int64_t row_bytes = 0; float at(int64_t i, int64_t j) const { return rad_load_f32(p + i * row_bytes, dt, j); } }; inline bool role_is(const char* role, const char* want) { for (int i = 0; i < RAD_ENC_STR; ++i) { if (role[i] != want[i]) return false; if (!want[i]) return true; } return true; } /* The plane of `role` in the joined operand at `base` (abi/rad_planes.h): a header, an entry per * plane, then the planes. The header lives in device memory, so the kernel finds its planes itself: * a launch must not read device memory from the host. */ inline DPlane find_plane(const char* base, const char* role) { DPlane 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; return v; } return v; } /* The element distance between rows of `t` read as rows of `cols`. */ inline 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]; } } // namespace xe2