Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/src/omega_gpu_gemm_ex.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_gpu_gemm_ex.c` 1153 lines · 63952 bytes Raw Blame History 1 /* omega_gpu_gemm_ex.c -- the general matrix multiply over the C3 kernels (contract: omega_gpu_gemm_ex.h). 2 * The plan, the routes, the k parts, the operand conversion, the k-part sums, the host recompute, the epilogue and the fp8 3 * dequantization are seeded Omega (the om/gemmex units, generated src/cudapp_om_gemm_ex_.c, CUDApp#144); this file is the 4 * memory shell: gathers into the units' windows, copies, allocation and the C3 calls. 5 * Migration-pending C, kept as the differential oracle (mk/gemm-ex.mk): CUDAPP_MIGRATION_PENDING_GEMM_EX=1 builds the 6 * old C path instead; CUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF=1 runs both on every call and aborts on any difference. */ 7 #include "omega_gpu_gemm_ex.h" 8 #include "omega_gpu_session.h" 9 10 #include 11 #include 12 #ifndef CUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY 13 #define CUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY 0 14 #endif 15 16 #ifndef CUDAPP_MIGRATION_PENDING_GEMM_EX 17 #define CUDAPP_MIGRATION_PENDING_GEMM_EX 0 18 #endif 19 #ifndef CUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF 20 #define CUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF 0 21 #endif 22 #define GX_C (CUDAPP_MIGRATION_PENDING_GEMM_EX || CUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF) 23 #define GX_OM (!CUDAPP_MIGRATION_PENDING_GEMM_EX || CUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF) 24 25 static int chip_core(void *ctx, OmegaGpuGemmType t, unsigned layout, uint32_t M, uint32_t N, uint32_t K, const void *A, uint64_t lda, 26 const void *B, uint64_t ldb, float *C) { 27 (void)ctx; 28 return omega_gpu_gemm_ld(t, layout, M, N, K, A, lda, B, ldb, C, NULL); 29 } 30 static OmegaGemmExCoreFn g_core = chip_core; 31 static void *g_core_ctx; 32 static int g_mutant; 33 34 void omega_gpu_gemm_ex_set_backend(OmegaGemmExCoreFn fn, void *ctx) { g_core = fn ? fn : chip_core; g_core_ctx = fn ? ctx : NULL; } 35 void omega_gpu_gemm_ex_test_set_mutant(int mutant) { g_mutant = mutant; } 36 37 #if GX_C /* ------------------------------------------------------------------ the migration-pending C (the oracle) */ 38 39 #define TILE 4096u /* rows / columns of C per host tile */ 40 #define KMAX 16384u /* k per C3 call (OMEGA_GPU_GEMM_MAX_DIM) */ 41 #define KMAX3 1808u /* k per bf16x3 call: 9 planes of roundup(k, 16) <= 16384 */ 42 43 /* ------------------------------------------------------------------ number formats */ 44 45 static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } 46 static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 47 static int fin(float f) { return (f2u(f) & 0x7f800000u) != 0x7f800000u; } 48 static double dabs(double v) { return v < 0 ? -v : v; } 49 /* The NaN a double operation gives, as the seeded Omega units define it (o_fpn in src/cudapp_om_gemm_ex_*.c, AArch64's 50 * rule): a NaN result is operand a's signaling NaN quieted, else b's, else a's quiet NaN, else b's, else the default NaN 51 * 0x7ff8000000000000. AArch64 arithmetic gives that already; x86-64's invalid operation gives the negative default NaN, 52 * and which operand of a + b or a * b x86 passes on is the compiler's choice. The oracle applies the rule itself, at the 53 * operations the units do in the same operand order, so the differential holds on both hosts. */ 54 static double fpn(double a, double b, double r) { 55 uint64_t ua, ub; 56 const uint64_t q = UINT64_C(0x8000000000000), dn = UINT64_C(0x7ff8000000000000); 57 if (r == r) return r; 58 memcpy(&ua, &a, 8); memcpy(&ub, &b, 8); 59 if (a != a && !(ua & q)) ua |= q; 60 else if (b != b && !(ub & q)) ua = ub | q; 61 else if (a != a) {} 62 else if (b != b) ua = ub; 63 else ua = dn; 64 memcpy(&r, &ua, 8); 65 return r; 66 } 67 static double dmul(double a, double b) { return fpn(a, b, a * b); } 68 static double dadd(double a, double b) { return fpn(a, b, a + b); } 69 /* s + a * b with the rule: the plain expression (left contractable, so test-gemm-ex-unfused's red check still finds the 70 * fused multiply-add a build without -ffp-contract=off makes), recomputed through the rule only when it gives a NaN; a 71 * NaN anywhere in the chain reaches the result, so a non-NaN result needs no rule. The recompute stays out of line: a 72 * product shared with it would have a second use, and the compiler fuses only a product whose one use is the add. */ 73 __attribute__((noinline, cold)) static double dmadd_nan(double s, double a, double b) { return dadd(s, dmul(a, b)); } 74 static double dmadd(double s, double a, double b) { 75 const double t = s + a * b; 76 return t == t ? t : dmadd_nan(s, a, b); 77 } 78 79 static float h2f(uint16_t h) { 80 uint32_t s = (uint32_t)(h >> 15) << 31, e = (h >> 10) & 31u, m = h & 0x3ffu; 81 if (e == 31u) return u2f(s | 0x7f800000u | m << 13); 82 if (e == 0u) { float v = (float)m * (1.0f / `16777216`.0f); return s ? -v : v; } /* m 2^-24, exact */ 83 return u2f(s | (e + 112u) << 23 | m << 13); 84 } 85 static float e4m3f(uint8_t b) { 86 uint32_t e = (b >> 3) & 15u, m = b & 7u; 87 if (e == 15u && m == 7u) return u2f(0x7fc00000u); 88 float v = e == 0u ? (float)m * (1.0f / 512.0f) : u2f((e + 120u) << 23 | m << 20); 89 return b & 0x80u ? -v : v; 90 } 91 static uint32_t esize(unsigned t) { return t == OMEGA_GEMM_DT_F32 ? 4u : t <= OMEGA_GEMM_DT_F16 ? 2u : 1u; } 92 static float ldf(unsigned t, const void *p, uint64_t i) { 93 switch (t) { 94 case OMEGA_GEMM_DT_F32: { float f; memcpy(&f, (const uint8_t *)p + i * 4u, 4); return f; } 95 case OMEGA_GEMM_DT_BF16: return u2f((uint32_t)((const uint16_t *)p)[i] << 16); 96 case OMEGA_GEMM_DT_F16: return h2f(((const uint16_t *)p)[i]); 97 case OMEGA_GEMM_DT_E4M3: return e4m3f(((const uint8_t *)p)[i]); 98 default: return h2f((uint16_t)(((const uint8_t *)p)[i] << 8)); /* an e5m2 byte is the top byte of an f16 */ 99 } 100 } 101 static uint16_t f2bf_rne(float f) { 102 uint32_t u = f2u(f); 103 if (!fin(f)) return (uint16_t)(u >> 16 | ((u & 0x7fffffu) ? 0x40u : 0u)); 104 return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); 105 } 106 static uint16_t f2h_rne(float f) { 107 uint32_t u = f2u(f), s = (u >> 16) & 0x8000u, a = u & 0x7fffffffu; 108 if (a >= 0x7f800000u) return (uint16_t)(s | 0x7c00u | (a > 0x7f800000u ? 0x200u : 0u)); 109 if (a >= 0x477ff000u) return (uint16_t)(s | 0x7c00u); /* >= 65520 rounds past the f16 maximum */ 110 if (a < 0x38800000u) { /* below 2^-14: an f16 subnormal (unit 2^-24) or zero */ 111 if (a < 0x33000000u) return (uint16_t)s; /* below 2^-25 */ 112 uint32_t mant = (a & 0x7fffffu) | 0x800000u, sh = 126u - (a >> 23); 113 uint32_t q = mant >> sh, rem = mant & ((1u << sh) - 1u), half = 1u << (sh - 1u); 114 if (rem > half || (rem == half && (q & 1u))) q++; 115 return (uint16_t)(s | q); 116 } 117 uint32_t r = a + 0xfffu + ((a >> 13) & 1u); 118 return (uint16_t)(s | (((r >> 23) - 112u) << 10 | ((r >> 13) & 0x3ffu))); 119 } 120 /* double to float rounding to odd: one more rounding to bf16 or f16 is then the correct rounding of v */ 121 static float d2f_odd(double v) { 122 float f = (float)v; 123 if (v != v || !fin(f) || (double)f == v) return f; 124 uint32_t u = f2u(f); 125 if (u & 1u) return f; 126 return u2f(dabs(v) > dabs((double)f) ? u + 1u : u - 1u); 127 } 128 /* exact split of a finite f32 into three bf16 planes by truncation: f = hi + mid + lo, except the parts below 2^-133 */ 129 static void split3(float f, uint16_t p[3]) { 130 if (!fin(f)) { p[0] = p[1] = p[2] = 0; return; } 131 uint32_t u = f2u(f); 132 p[0] = (uint16_t)(u >> 16); 133 float r1 = f - u2f(u & 0xffff0000u); 134 uint32_t u1 = f2u(r1); 135 p[1] = (uint16_t)(u1 >> 16); 136 p[2] = (uint16_t)(f2u(r1 - u2f(u1 & 0xffff0000u)) >> 16); 137 } 138 /* plane products in k order, smallest first: (lo,lo) (lo,mid) (mid,lo) (lo,hi) (mid,mid) (hi,lo) (mid,hi) (hi,mid) (hi,hi) */ 139 static const uint8_t TA[9] = { 2, 2, 1, 2, 1, 0, 1, 0, 0 }, TB[9] = { 2, 1, 2, 0, 1, 2, 0, 1, 0 }; 140 141 /* ------------------------------------------------------------------ the call */ 142 143 typedef struct { 144 OmegaGemmExArgs x; 145 uint64_t sa_r, sa_c, sb_c; /* block-scale grid: rows, k blocks, column blocks */ 146 const float *sa, *sb; /* this batch's scales */ 147 const uint8_t *A, *B; /* this batch's operands */ 148 unsigned route; 149 OmegaGpuGemmType core; 150 uint64_t kmax; 151 } Call; 152 153 static uint64_t a_at(const Call *c, uint64_t i, uint64_t kk) { return c->x.op_a ? kk * c->x.lda + i : i * c->x.lda + kk; } 154 static uint64_t b_at(const Call *c, uint64_t kk, uint64_t j) { return c->x.op_b ? j * c->x.ldb + kk : kk * c->x.ldb + j; } 155 static float aval(const Call *c, uint64_t i, uint64_t kk) { return ldf(c->x.a_type, c->A, a_at(c, i, kk)); } 156 static float bval(const Call *c, uint64_t kk, uint64_t j) { return ldf(c->x.b_type, c->B, b_at(c, kk, j)); } 157 /* a stored element is non-finite (inf, NaN; e4m3: NaN only) when its exponent field has every bit set */ 158 static int elem_bad(unsigned t, const uint8_t *p, uint64_t i) { 159 switch (t) { 160 case OMEGA_GEMM_DT_F32: { uint32_t u; memcpy(&u, p + i * 4u, 4); return (u & 0x7f800000u) == 0x7f800000u; } 161 case OMEGA_GEMM_DT_BF16: return (((const uint16_t *)p)[i] & 0x7f80u) == 0x7f80u; 162 case OMEGA_GEMM_DT_F16: return (((const uint16_t *)p)[i] & 0x7c00u) == 0x7c00u; 163 case OMEGA_GEMM_DT_E4M3: return (p[i] & 0x7fu) == 0x7fu; 164 default: return (p[i] & 0x7cu) == 0x7cu; /* e5m2 */ 165 } 166 } 167 /* 1 when any of n contiguous elements is non-finite. Per 64-bit word: every lane of y = (w & mask) ^ mask has its top bit 168 * clear and is 0 exactly for a non-finite element, so y - 1 per lane sets a lane's top bit only when that lane or a lower 169 * lane is 0 (a borrow); the OR of all words has a top bit set exactly when some element is non-finite. 64 bytes per step in 170 * four 128-bit accumulators (GCC vector extension: NEON on arm64, SSE2 on x86-64). */ 171 typedef uint64_t ScanV __attribute__((vector_size(16))); 172 static int run_bad(unsigned t, const uint8_t *p, uint64_t n) { 173 const uint64_t es = esize(t), bytes = n * es; 174 const uint64_t mask = t == OMEGA_GEMM_DT_F32 ? 0x7f8000007f800000ull : t == OMEGA_GEMM_DT_BF16 ? 0x7f807f807f807f80ull 175 : t == OMEGA_GEMM_DT_F16 ? 0x7c007c007c007c00ull : t == OMEGA_GEMM_DT_E4M3 ? 0x7f7f7f7f7f7f7f7full 176 : 0x7c7c7c7c7c7c7c7cull; 177 const uint64_t lo = es == 4u ? 0x0000000100000001ull : es == 2u ? 0x0001000100010001ull : 0x0101010101010101ull; 178 const uint64_t hi = lo << (8u * es - 1u); 179 const ScanV vm = { mask, mask }, vl = { lo, lo }; 180 ScanV a0 = { 0, 0 }, a1 = a0, a2 = a0, a3 = a0; 181 uint64_t i = 0, acc = 0; 182 for (; i + 64u <= bytes; i += 64u) { 183 ScanV w0, w1, w2, w3; 184 memcpy(&w0, p + i, 16); memcpy(&w1, p + i + 16u, 16); memcpy(&w2, p + i + 32u, 16); memcpy(&w3, p + i + 48u, 16); 185 a0 |= ((w0 & vm) ^ vm) - vl; a1 |= ((w1 & vm) ^ vm) - vl; a2 |= ((w2 & vm) ^ vm) - vl; a3 |= ((w3 & vm) ^ vm) - vl; 186 } 187 a0 |= a1 | a2 | a3; 188 acc = a0[0] | a0[1]; 189 for (; i + 8u <= bytes; i += 8u) { uint64_t w; memcpy(&w, p + i, 8); acc |= ((w & mask) ^ mask) - lo; } 190 if (acc & hi) return 1; 191 for (i /= es; i < n; i++) if (elem_bad(t, p, i)) return 1; 192 return 0; 193 } 194 /* one stored operand (rows stored rows of cols elements, leading dimension ld): flag[r] for every stored row r holding a 195 * non-finite element (per_row), else flag[j] for every stored column j holding one; 1 when there is any */ 196 static int scan_bad(unsigned t, const uint8_t *p, uint64_t rows, uint64_t cols, uint64_t ld, int per_row, uint8_t *flag) { 197 const uint64_t es = esize(t); 198 int any = 0; 199 for (uint64_t r = 0; r < rows; r++) { 200 const uint8_t *q = p + r * ld * es; 201 if (!run_bad(t, q, cols)) continue; 202 any = 1; 203 if (per_row) flag[r] = 1; 204 else for (uint64_t j = 0; j < cols; j++) if (elem_bad(t, q, j)) flag[j] = 1; 205 } 206 return any; 207 } 208 static double block_factor(const Call *c, uint64_t i, uint64_t kb, uint64_t j) { 209 if (c->x.scale != OMEGA_GEMM_SCALE_BLOCK) return 1.0; 210 return dmul((double)c->sa[(i / c->x.block_m) * c->sa_c + kb], (double)c->sb[kb * c->sb_c + j / c->x.block_n]); 211 } 212 213 /* stored rows x cols with leading dimension ld; 0 when ld is short or the span passes 2^56 */ 214 static int span_of(uint64_t rows, uint64_t cols, uint64_t ld, uint64_t *span) { 215 if (ld < cols || (rows - 1u) > (1ull << 56) / ld) return 0; 216 *span = (rows - 1u) * ld + cols; 217 return 1; 218 } 219 static int extent(uint64_t span, uint64_t stride, uint64_t batch, uint64_t *ext) { 220 uint64_t t; 221 if (__builtin_mul_overflow(batch - 1u, stride, &t) || __builtin_add_overflow(t, span, ext) || *ext > (1ull << 58)) return 0; 222 return 1; 223 } 224 static int overlap(const void *p, uint64_t pbytes, const void *q, uint64_t qbytes) { 225 uintptr_t a = (uintptr_t)p, b = (uintptr_t)q; 226 return a < b + qbytes && b < a + pbytes; 227 } 228 static int scales_finite(const float *s, uint64_t count, uint64_t stride, uint64_t batch) { 229 for (uint64_t b = 0; b < (stride ? batch : 1u); b++) 230 for (uint64_t i = 0; i < count; i++) if (!fin(s[b * stride + i])) return 0; 231 return 1; 232 } 233 234 static int validate(Call *c, const OmegaGemmExArgs *in) { 235 c->x = *in; 236 OmegaGemmExArgs *x = &c->x; 237 if (!x->batch) x->batch = 1; 238 if (x->op_a > 1u || x->op_b > 1u || x->a_type > OMEGA_GEMM_DT_E5M2 || x->b_type > OMEGA_GEMM_DT_E5M2 || 239 x->c_type > OMEGA_GEMM_DT_E5M2 || x->compute > OMEGA_GEMM_EX_F32_FFMA_REF || x->scale > OMEGA_GEMM_SCALE_BLOCK || !x->c) 240 return OMEGA_GPU_GEMM_BAD_ARGS; 241 if (!x->m || !x->n || !x->k) return OMEGA_GPU_GEMM_BAD_ARGS; 242 if (x->m > OMEGA_GPU_GEMM_EX_MAX_DIM || x->n > OMEGA_GPU_GEMM_EX_MAX_DIM || x->k > OMEGA_GPU_GEMM_EX_MAX_DIM || 243 x->batch > OMEGA_GPU_GEMM_EX_MAX_BATCH) return OMEGA_GPU_GEMM_TOO_LARGE; 244 if (x->c_type > OMEGA_GEMM_DT_F16) return OMEGA_GPU_GEMM_UNSUPPORTED; 245 const uint64_t ar = x->op_a ? x->k : x->m, ac = x->op_a ? x->m : x->k; 246 const uint64_t br = x->op_b ? x->n : x->k, bc = x->op_b ? x->k : x->n; 247 if (!x->lda) x->lda = ac; 248 if (!x->ldb) x->ldb = bc; 249 if (!x->ldc) x->ldc = x->n; 250 uint64_t sa, sb, sc, ea, eb, ec; 251 if (!span_of(ar, ac, x->lda, &sa) || !span_of(br, bc, x->ldb, &sb) || !span_of(x->m, x->n, x->ldc, &sc)) return OMEGA_GPU_GEMM_BAD_ARGS; 252 if (x->batch > 1u && x->stride_c < sc) return OMEGA_GPU_GEMM_BAD_ARGS; /* C batches would overlap */ 253 if (!extent(sa, x->stride_a, x->batch, &ea) || !extent(sb, x->stride_b, x->batch, &eb) || !extent(sc, x->stride_c, x->batch, &ec)) 254 return OMEGA_GPU_GEMM_BAD_ARGS; 255 const uint64_t cbytes = ec * esize(x->c_type); 256 if (x->alpha != 0.0f) { 257 if (!x->a || !x->b) return OMEGA_GPU_GEMM_BAD_ARGS; 258 if (overlap(x->c, cbytes, x->a, ea * esize(x->a_type)) || overlap(x->c, cbytes, x->b, eb * esize(x->b_type))) return OMEGA_GPU_GEMM_BAD_ARGS; 259 } 260 if (x->scale == OMEGA_GEMM_SCALE_TENSOR) { 261 if (!x->scale_a || !x->scale_b || !scales_finite(x->scale_a, 1, x->scale_stride_a, x->batch) || 262 !scales_finite(x->scale_b, 1, x->scale_stride_b, x->batch)) return OMEGA_GPU_GEMM_BAD_ARGS; 263 } else if (x->scale == OMEGA_GEMM_SCALE_BLOCK) { 264 if (!x->scale_a || !x->scale_b || !x->block_m || !x->block_n || !x->block_k) return OMEGA_GPU_GEMM_BAD_ARGS; 265 c->sa_r = (x->m + x->block_m - 1u) / x->block_m; c->sa_c = (x->k + x->block_k - 1u) / x->block_k; 266 c->sb_c = (x->n + x->block_n - 1u) / x->block_n; 267 if (!scales_finite(x->scale_a, c->sa_r * c->sa_c, x->scale_stride_a, x->batch) || 268 !scales_finite(x->scale_b, c->sa_c * c->sb_c, x->scale_stride_b, x->batch)) return OMEGA_GPU_GEMM_BAD_ARGS; 269 } 270 /* the route */ 271 const unsigned ta = x->a_type, tb = x->b_type; 272 const int bf_ok = ta != OMEGA_GEMM_DT_F32 && ta != OMEGA_GEMM_DT_F16 && tb != OMEGA_GEMM_DT_F32 && tb != OMEGA_GEMM_DT_F16; 273 const int h_ok = ta != OMEGA_GEMM_DT_F32 && ta != OMEGA_GEMM_DT_BF16 && tb != OMEGA_GEMM_DT_F32 && tb != OMEGA_GEMM_DT_BF16; 274 c->kmax = KMAX; 275 if (x->compute == OMEGA_GEMM_EX_F32_FFMA_REF) { c->route = OMEGA_GEMM_ROUTE_FFMA; c->core = OMEGA_GPU_GEMM_F32_FFMA; } 276 else if (ta == tb && ta == OMEGA_GEMM_DT_BF16) { c->route = OMEGA_GEMM_ROUTE_NATIVE; c->core = OMEGA_GPU_GEMM_BF16; } 277 else if (ta == tb && ta == OMEGA_GEMM_DT_F16) { c->route = OMEGA_GEMM_ROUTE_NATIVE; c->core = OMEGA_GPU_GEMM_F16; } 278 else if (ta == tb && ta == OMEGA_GEMM_DT_E4M3) { c->route = OMEGA_GEMM_ROUTE_NATIVE; c->core = OMEGA_GPU_GEMM_E4M3; } 279 else if (bf_ok) { c->route = OMEGA_GEMM_ROUTE_WIDEN; c->core = OMEGA_GPU_GEMM_BF16; } 280 else if (h_ok) { c->route = OMEGA_GEMM_ROUTE_WIDEN; c->core = OMEGA_GPU_GEMM_F16; } 281 else if (x->compute == OMEGA_GEMM_EX_F32_FAST_BF16) { c->route = OMEGA_GEMM_ROUTE_FAST_BF16; c->core = OMEGA_GPU_GEMM_BF16; } 282 else { c->route = OMEGA_GEMM_ROUTE_BF16X3; c->core = OMEGA_GPU_GEMM_BF16; c->kmax = KMAX3; } 283 if (g_mutant == 2) x->op_b = OMEGA_GEMM_OP_N; 284 return OMEGA_GPU_GEMM_OK; 285 } 286 287 /* the operand buffers of one k part: A' rows of op(A), B' columns of op(B) (B' row-major k x n for FFMA), allocated on the 288 * first part that packs (na, nb elements of the core type); P the dense core output of a tile, acc the k-part sum */ 289 typedef struct { uint8_t *a, *b; uint64_t na, nb; float *p; double *acc; uint8_t *rowbad, *colbad; } Bufs; 290 291 static uint32_t core_esize(OmegaGpuGemmType t) { return t == OMEGA_GPU_GEMM_E4M3 ? 1u : t == OMEGA_GPU_GEMM_F32_FFMA ? 4u : 2u; } 292 static uint32_t round16(uint32_t v) { return (v + 15u) & ~15u; } 293 294 /* encode an exactly representable finite value in the core type */ 295 static void put_core(OmegaGpuGemmType t, uint8_t *dst, uint64_t i, float f) { 296 if (!fin(f)) f = 0.0f; 297 if (t == OMEGA_GPU_GEMM_F32_FFMA) memcpy(dst + i * 4u, &f, 4); 298 else if (t == OMEGA_GPU_GEMM_F16) ((uint16_t *)dst)[i] = f2h_rne(f); 299 else ((uint16_t *)dst)[i] = (uint16_t)(f2u(f) >> 16); 300 } 301 302 /* one k part [k0, k0 + kc) of the tile [m0, m0 + mc) x [n0, n0 + nc) into P (dense mc x nc) */ 303 static int run_part(Call *c, Bufs *bf, int any_bad, uint64_t m0, uint32_t mc, uint64_t n0, uint32_t nc, uint64_t k0, uint32_t kc, 304 float *P, uint64_t *calls) { 305 const OmegaGemmExArgs *x = &c->x; 306 const OmegaGpuGemmType t = c->core; 307 const unsigned layout = (x->op_a ? OMEGA_GPU_GEMM_A_COL : 0u) | (x->op_b ? OMEGA_GPU_GEMM_B_COL : 0u); 308 (*calls)++; 309 if (c->route == OMEGA_GEMM_ROUTE_NATIVE && !any_bad && omega_gpu_gemm_span(layout, mc, nc, kc, x->lda, x->ldb, NULL, NULL) == 0) { 310 const uint64_t es = esize(x->a_type); 311 const uint8_t *pa = c->A + (x->op_a ? k0 * x->lda + m0 : m0 * x->lda + k0) * es; 312 const uint8_t *pb = c->B + (x->op_b ? n0 * x->ldb + k0 : k0 * x->ldb + n0) * es; 313 return g_core(g_core_ctx, t, layout, mc, nc, kc, pa, x->lda, pb, x->ldb, P); 314 } 315 if (!bf->a) { 316 bf->a = omega_gpu_host_calloc(bf->na, core_esize(t)); 317 bf->b = omega_gpu_host_calloc(bf->nb, core_esize(t)); 318 if (!bf->a || !bf->b) return OMEGA_GPU_GEMM_CHIP_FAIL; 319 } 320 if (c->route == OMEGA_GEMM_ROUTE_BF16X3) { 321 const uint32_t kp = round16(kc), K = 9u * kp; 322 uint16_t *A2 = (uint16_t *)bf->a, *B2 = (uint16_t *)bf->b; 323 for (uint32_t i = 0; i < mc; i++) { 324 uint16_t *row = A2 + (uint64_t)i * K; 325 for (uint32_t kk = 0; kk < kp; kk++) { 326 uint16_t p[3] = { 0, 0, 0 }; 327 if (kk < kc) split3(aval(c, m0 + i, k0 + kk), p); 328 for (unsigned q = 0; q < 9u; q++) row[q * kp + kk] = p[TA[q]]; 329 } 330 } 331 for (uint32_t j = 0; j < nc; j++) { 332 uint16_t *col = B2 + (uint64_t)j * K; 333 for (uint32_t kk = 0; kk < kp; kk++) { 334 uint16_t p[3] = { 0, 0, 0 }; 335 if (kk < kc) split3(bval(c, k0 + kk, n0 + j), p); 336 for (unsigned q = 0; q < 9u; q++) col[q * kp + kk] = p[TB[q]]; 337 } 338 } 339 return g_core(g_core_ctx, OMEGA_GPU_GEMM_BF16, OMEGA_GPU_GEMM_B_COL, mc, nc, K, A2, 0, B2, 0, P); 340 } 341 if (c->route == OMEGA_GEMM_ROUTE_FAST_BF16) { 342 uint16_t *A2 = (uint16_t *)bf->a, *B2 = (uint16_t *)bf->b; 343 for (uint32_t i = 0; i < mc; i++) 344 for (uint32_t kk = 0; kk < kc; kk++) { float f = aval(c, m0 + i, k0 + kk); A2[(uint64_t)i * kc + kk] = fin(f) ? f2bf_rne(f) : 0; } 345 for (uint32_t j = 0; j < nc; j++) 346 for (uint32_t kk = 0; kk < kc; kk++) { float f = bval(c, k0 + kk, n0 + j); B2[(uint64_t)j * kc + kk] = fin(f) ? f2bf_rne(f) : 0; } 347 return g_core(g_core_ctx, OMEGA_GPU_GEMM_BF16, OMEGA_GPU_GEMM_B_COL, mc, nc, kc, A2, 0, B2, 0, P); 348 } 349 if (c->route == OMEGA_GEMM_ROUTE_FFMA) { 350 for (uint32_t i = 0; i < mc; i++) 351 for (uint32_t kk = 0; kk < kc; kk++) put_core(t, bf->a, (uint64_t)i * kc + kk, aval(c, m0 + i, k0 + kk)); 352 for (uint32_t kk = 0; kk < kc; kk++) 353 for (uint32_t j = 0; j < nc; j++) put_core(t, bf->b, (uint64_t)kk * nc + j, bval(c, k0 + kk, n0 + j)); 354 return g_core(g_core_ctx, t, 0u, mc, nc, kc, bf->a, 0, bf->b, 0, P); 355 } 356 /* WIDEN, or NATIVE with non-finite inputs or a span past 2^32: dense copies in the core type, non-finite as 0 */ 357 if (t == OMEGA_GPU_GEMM_E4M3) { 358 for (uint32_t i = 0; i < mc; i++) 359 for (uint32_t kk = 0; kk < kc; kk++) { uint8_t v = c->A[a_at(c, m0 + i, k0 + kk)]; bf->a[(uint64_t)i * kc + kk] = (v & 0x7fu) == 0x7fu ? 0 : v; } 360 for (uint32_t j = 0; j < nc; j++) 361 for (uint32_t kk = 0; kk < kc; kk++) { uint8_t v = c->B[b_at(c, k0 + kk, n0 + j)]; bf->b[(uint64_t)j * kc + kk] = (v & 0x7fu) == 0x7fu ? 0 : v; } 362 } else { 363 for (uint32_t i = 0; i < mc; i++) 364 for (uint32_t kk = 0; kk < kc; kk++) put_core(t, bf->a, (uint64_t)i * kc + kk, aval(c, m0 + i, k0 + kk)); 365 for (uint32_t j = 0; j < nc; j++) 366 for (uint32_t kk = 0; kk < kc; kk++) put_core(t, bf->b, (uint64_t)j * kc + kk, bval(c, k0 + kk, n0 + j)); 367 } 368 return g_core(g_core_ctx, t, OMEGA_GPU_GEMM_B_COL, mc, nc, kc, bf->a, 0, bf->b, 0, P); 369 } 370 371 static double host_dot(const Call *c, uint64_t i, uint64_t j) { 372 double s = 0.0; 373 for (uint64_t kk = 0; kk < c->x.k; kk++) 374 s = dmadd(s, dmul((double)aval(c, i, kk), (double)bval(c, kk, j)), 375 block_factor(c, i, kk / (c->x.block_k ? c->x.block_k : 1u), j)); 376 return s; 377 } 378 379 static double load_c(unsigned t, const void *p, uint64_t i) { return (double)ldf(t, p, i); } 380 static void store_c(unsigned t, void *p, uint64_t i, double v) { 381 if (t == OMEGA_GEMM_DT_F32) { float f = (float)v; memcpy((uint8_t *)p + i * 4u, &f, 4); } 382 else if (t == OMEGA_GEMM_DT_BF16) ((uint16_t *)p)[i] = f2bf_rne(d2f_odd(v)); 383 else ((uint16_t *)p)[i] = f2h_rne(d2f_odd(v)); 384 } 385 386 static int c_gemm_ex(const OmegaGemmExArgs *args) { 387 if (!args) return OMEGA_GPU_GEMM_BAD_ARGS; 388 Call c; 389 memset(&c, 0, sizeof c); 390 int rc = validate(&c, args); 391 if (rc != OMEGA_GPU_GEMM_OK) return rc; 392 const OmegaGemmExArgs *x = &c.x; 393 OmegaGemmExInfo info; 394 memset(&info, 0, sizeof info); 395 const int compute = x->alpha != 0.0f; 396 const double beta = g_mutant == 4 ? 0.0 : (double)x->beta; 397 const uint64_t tm = x->m < TILE ? x->m : TILE, tn = x->n < TILE ? x->n : TILE; 398 /* one k part and no block scales: the k-part sum is 0 + P, so the epilogue reads P and no sum is kept */ 399 const int one_part = x->k <= c.kmax && x->scale != OMEGA_GEMM_SCALE_BLOCK; 400 /* direct: native, C f32 in dense rows no wider than one C3 call, beta 0, operands C3 addresses in place. A batch with 401 * no non-finite input and alpha s == 1 then has the C3 output as its result (the bit-for-bit case of the header), so 402 * C3 writes C itself, in parts of at most OMEGA_GPU_GEMM_MAX_DIM rows of n, with no host tile, sum or epilogue */ 403 const uint64_t dm = x->m < OMEGA_GPU_GEMM_MAX_DIM ? x->m : OMEGA_GPU_GEMM_MAX_DIM; 404 const unsigned layout = (x->op_a ? OMEGA_GPU_GEMM_A_COL : 0u) | (x->op_b ? OMEGA_GPU_GEMM_B_COL : 0u); 405 const int direct_shape = compute && one_part && c.route == OMEGA_GEMM_ROUTE_NATIVE && beta == 0.0 && 406 x->c_type == OMEGA_GEMM_DT_F32 && x->ldc == x->n && x->n <= OMEGA_GPU_GEMM_MAX_DIM && 407 omega_gpu_gemm_span(layout, (uint32_t)dm, (uint32_t)x->n, (uint32_t)x->k, x->lda, x->ldb, NULL, NULL) == 0; 408 Bufs bf; 409 memset(&bf, 0, sizeof bf); 410 if (compute) { 411 info.route = c.route; info.core_type = c.core; 412 const uint64_t kc = x->k < c.kmax ? x->k : c.kmax; 413 const uint64_t kcore = c.route == OMEGA_GEMM_ROUTE_BF16X3 ? 9u * round16((uint32_t)kc) : kc; 414 bf.na = tm * kcore + 16u; bf.nb = tn * kcore + 16u; 415 bf.rowbad = omega_gpu_host_calloc(x->m, 1); 416 bf.colbad = omega_gpu_host_calloc(x->n, 1); 417 if (!bf.rowbad || !bf.colbad) { rc = OMEGA_GPU_GEMM_CHIP_FAIL; goto out; } 418 } 419 for (uint64_t b = 0; b < x->batch; b++) { 420 uint8_t *C = (uint8_t *)x->c + b * x->stride_c * esize(x->c_type); 421 double alpha = (double)x->alpha; 422 int any_bad = 0; 423 if (compute) { 424 c.A = (const uint8_t *)x->a + b * x->stride_a * esize(x->a_type); 425 c.B = (const uint8_t *)x->b + b * x->stride_b * esize(x->b_type); 426 if (x->scale != OMEGA_GEMM_SCALE_NONE) { c.sa = x->scale_a + b * x->scale_stride_a; c.sb = x->scale_b + b * x->scale_stride_b; } 427 if (x->scale == OMEGA_GEMM_SCALE_TENSOR) alpha = dmul(alpha, dmul((double)c.sa[0], (double)c.sb[0])); 428 memset(bf.rowbad, 0, x->m); memset(bf.colbad, 0, x->n); 429 const int bad_a = scan_bad(x->a_type, c.A, x->op_a ? x->k : x->m, x->op_a ? x->m : x->k, x->lda, !x->op_a, bf.rowbad); 430 const int bad_b = scan_bad(x->b_type, c.B, x->op_b ? x->n : x->k, x->op_b ? x->k : x->n, x->ldb, x->op_b, bf.colbad); 431 any_bad = bad_a | bad_b; 432 const uint64_t ku = g_mutant == 1 ? x->k - 1u : x->k; /* the one part's k (mutant 1 drops the last k) */ 433 if (direct_shape && !any_bad && alpha == 1.0) { 434 for (uint64_t m0 = 0; m0 < x->m; m0 += dm) { 435 const uint32_t mc = (uint32_t)(x->m - m0 < dm ? x->m - m0 : dm); 436 float *out = (float *)C + m0 * x->ldc; 437 if (!ku) { memset(out, 0, (uint64_t)mc * x->n * sizeof(float)); continue; } 438 rc = run_part(&c, &bf, 0, m0, mc, 0, (uint32_t)x->n, 0, (uint32_t)ku, out, &info.core_calls); 439 if (rc != OMEGA_GPU_GEMM_OK) goto out; 440 } 441 continue; 442 } 443 if (!bf.p && !(bf.p = omega_gpu_host_calloc(tm * tn, sizeof(float)))) { rc = OMEGA_GPU_GEMM_CHIP_FAIL; goto out; } 444 if (!one_part && !bf.acc && !(bf.acc = omega_gpu_host_calloc(tm * tn, sizeof(double)))) { rc = OMEGA_GPU_GEMM_CHIP_FAIL; goto out; } 445 } 446 for (uint64_t m0 = 0; m0 < x->m; m0 += TILE) 447 for (uint64_t n0 = 0; n0 < x->n; n0 += TILE) { 448 const uint32_t mc = (uint32_t)(x->m - m0 < TILE ? x->m - m0 : TILE), nc = (uint32_t)(x->n - n0 < TILE ? x->n - n0 : TILE); 449 if (compute && one_part) { 450 const uint64_t ku = g_mutant == 1 ? x->k - 1u : x->k; 451 if (!ku) memset(bf.p, 0, (uint64_t)mc * nc * sizeof(float)); 452 else if ((rc = run_part(&c, &bf, any_bad, m0, mc, n0, nc, 0, (uint32_t)ku, bf.p, &info.core_calls)) != OMEGA_GPU_GEMM_OK) goto out; 453 } else if (compute) { 454 memset(bf.acc, 0, (uint64_t)mc * nc * sizeof(double)); 455 for (uint64_t k0 = 0; k0 < x->k;) { 456 uint64_t kc = x->k - k0 < c.kmax ? x->k - k0 : c.kmax; 457 if (x->scale == OMEGA_GEMM_SCALE_BLOCK && kc > x->block_k - k0 % x->block_k) kc = x->block_k - k0 % x->block_k; 458 uint64_t ku = kc; 459 if (g_mutant == 1 && k0 + kc == x->k) ku = kc - 1u; 460 if (ku) { 461 rc = run_part(&c, &bf, any_bad, m0, mc, n0, nc, k0, (uint32_t)ku, bf.p, &info.core_calls); 462 if (rc != OMEGA_GPU_GEMM_OK) goto out; 463 uint64_t kb = x->scale == OMEGA_GEMM_SCALE_BLOCK ? k0 / x->block_k : 0; 464 if (g_mutant == 3 && x->scale == OMEGA_GEMM_SCALE_BLOCK) kb = (kb + 1u) % c.sa_c; 465 for (uint32_t i = 0; i < mc; i++) 466 for (uint32_t j = 0; j < nc; j++) { 467 double *ac = &bf.acc[(uint64_t)i * nc + j]; 468 *ac = dmadd(*ac, (double)bf.p[(uint64_t)i * nc + j], block_factor(&c, m0 + i, kb, n0 + j)); 469 } 470 } 471 k0 += kc; 472 } 473 } 474 for (uint32_t i = 0; i < mc; i++) 475 for (uint32_t j = 0; j < nc; j++) { 476 const uint64_t ci = (m0 + i) * x->ldc + n0 + j; 477 double v = 0.0; 478 if (compute) { 479 double a = one_part ? 0.0 + (double)bf.p[(uint64_t)i * nc + j] : bf.acc[(uint64_t)i * nc + j]; 480 if (any_bad && (bf.rowbad[m0 + i] || bf.colbad[n0 + j])) { a = host_dot(&c, m0 + i, n0 + j); info.host_fixed++; } 481 v = dmul(alpha, a); 482 } 483 if (beta != 0.0) v = dmadd(v, beta, load_c(x->c_type, C, ci)); 484 store_c(x->c_type, C, ci, v); 485 } 486 } 487 } 488 out: 489 omega_gpu_host_free(bf.a); omega_gpu_host_free(bf.b); omega_gpu_host_free(bf.p); omega_gpu_host_free(bf.acc); 490 omega_gpu_host_free(bf.rowbad); omega_gpu_host_free(bf.colbad); 491 if (x->info) *x->info = info; 492 return rc; 493 } 494 495 /* ------------------------------------------------------------------ the fp8 grouped/MoE fallback */ 496 497 #define FP8_SLAB_BYTES (32ull << 20) /* dequantized weight rows per gemm_ex call */ 498 499 static int c_fp8_block_rows(const uint8_t *w, const float *scale, double scale_mul, uint32_t n, uint32_t k, uint32_t block, 500 const float *x32, const uint16_t *x16, uint32_t m, float *y, uint64_t ldy, OmegaGemmExInfo *info) { 501 if (info) memset(info, 0, sizeof *info); 502 if (!w || !scale || !y || (!x32) == (!x16) || n == 0 || k == 0 || m == 0 || block == 0 || (ldy && ldy < n)) return OMEGA_GPU_GEMM_BAD_ARGS; 503 if (!ldy) ldy = n; 504 const uint64_t kb = (k + (uint64_t)block - 1u) / block; 505 uint64_t rows = FP8_SLAB_BYTES / ((uint64_t)k * 4u); 506 if (rows == 0) rows = 1; 507 if (rows > n) rows = n; 508 float *slab = omega_gpu_host_calloc((size_t)(rows * k), sizeof *slab); 509 if (!slab) return OMEGA_GPU_GEMM_CHIP_FAIL; 510 int rc = OMEGA_GPU_GEMM_OK; 511 for (uint64_t j0 = 0; j0 < n && rc == OMEGA_GPU_GEMM_OK; j0 += rows) { 512 const uint64_t nr = n - j0 < rows ? n - j0 : rows; 513 for (uint64_t j = 0; j < nr; j++) { 514 const uint8_t *wr = w + (j0 + j) * k; 515 const float *sr = scale + ((j0 + j) / block) * kb; 516 float *dr = slab + j * k; 517 for (uint64_t c = 0; c < k; c++) { 518 uint64_t cb = c / block; 519 if (g_mutant == 5) cb = (cb + 1u) % kb; 520 dr[c] = (float)dmul(dmul((double)e4m3f(wr[c]), (double)sr[cb]), scale_mul); /* one rounding */ 521 } 522 } 523 OmegaGemmExInfo pi; 524 OmegaGemmExArgs a = { 525 .op_a = OMEGA_GEMM_OP_N, .op_b = OMEGA_GEMM_OP_T, .m = m, .n = nr, .k = k, .batch = 1, 526 .a_type = x32 ? OMEGA_GEMM_DT_F32 : OMEGA_GEMM_DT_BF16, .b_type = OMEGA_GEMM_DT_F32, .c_type = OMEGA_GEMM_DT_F32, 527 .a = x32 ? (const void *)x32 : (const void *)x16, .lda = k, .b = slab, .ldb = k, .c = y + j0, .ldc = ldy, 528 .alpha = 1.0f, .beta = 0.0f, .info = &pi, 529 }; 530 rc = c_gemm_ex(&a); 531 if (info) { info->route = pi.route; info->core_type = pi.core_type; info->core_calls += pi.core_calls; info->host_fixed += pi.host_fixed; } 532 } 533 omega_gpu_host_free(slab); 534 return rc; 535 } 536 537 #endif /* GX_C */ 538 539 #if GX_OM /* ------------------------------------------------------------------ the Omega path: the memory shell */ 540 #include "cudapp_native.h" 541 542 #define GX_WIN 65536u /* bytes of a unit's window (view uchar 65536; uint, f32 16384 and ushort 32768 in om/gemmex) */ 543 #define GX_ROW 4096u /* words of a unit's tile-row window (acc, fix: one row of a 4096-wide C tile) */ 544 545 /* The units' windows: x the call, p the plan, q one unit call's arguments, res a unit's result; ga gb gathered runs, 546 * gc the copied C row or the acc scales, fl the recompute flags of a row (fix read only where fl is set); tab the row 547 * unit's fp8 decode table, keyed by tab[256] (0: none). gx_new zeroes x..res and the key only: every other window is 548 * written before a unit reads it, and zeroing the whole struct cost more than a small call's arithmetic. owner is the 549 * platform whose host_alloc made the block. */ 550 typedef struct { 551 const OmegaGpuPlatform *owner; 552 uint64_t x[48], p[64], q[8], res, fix[GX_ROW], tab[257]; 553 uint8_t ga[GX_WIN], gb[GX_WIN], gc[GX_WIN], fl[GX_WIN]; 554 } Gx; 555 556 /* a zero P row and a zero sum row: never written, so it stays in .bss */ 557 static uint64_t gx_zero[GX_WIN / 8u]; 558 559 /* One spare Gx kept between calls. A fresh block of this size is a fresh mapping each call (glibc maps blocks this 560 * large, and the first touch of each page faults): 9x9x9 cost 11.8 us that way against 1.8 us on a kept block 561 * (2026-11-06, core 13). The slot is swapped atomically, so concurrent calls never share a block: a call that finds it 562 * empty allocates, and a call that finds it full on return frees the block it displaces. A block made under another 563 * platform than the current one is freed through its owner, never reused. The kept block is still reachable, never 564 * leaked; it lives until the process ends. */ 565 static Gx *gx_spare; 566 567 static void gx_release(Gx *g) { 568 if (g) g->owner->host_free(g->owner->ctx, g); 569 } 570 571 static Gx *gx_new(void) { 572 const OmegaGpuPlatform *p = omega_gpu_session_platform(); 573 Gx *g = __atomic_exchange_n(&gx_spare, (Gx *)0, __ATOMIC_ACQ_REL); 574 if (g && g->owner != p) { 575 gx_release(g); 576 g = 0; 577 } 578 if (!g) { 579 g = p ? p->host_alloc(p->ctx, sizeof *g) : 0; 580 if (!g) return 0; 581 g->owner = p; 582 } 583 memset(g->x, 0, offsetof(Gx, fix) - offsetof(Gx, x)); 584 g->tab[256] = 0; 585 return g; 586 } 587 588 static void gx_drop(Gx *g) { 589 gx_release(__atomic_exchange_n(&gx_spare, g, __ATOMIC_ACQ_REL)); 590 } 591 592 /* a buffer of n zeroed bytes and a GX_WIN tail that only lets a unit's window start near its end (never read as data) */ 593 static void *gx_buf(uint64_t n) { 594 uint8_t *b = omega_gpu_host_alloc(n + GX_WIN); 595 if (b) memset(b, 0, n); 596 return b; 597 } 598 599 /* the heap buffers of one call: the converted operands, the bad-row and bad-column flags, P and the k-part sums. They 600 * are carved from one block with one GX_WIN tail (bf.slab, gx_run): a window that starts near the end of one buffer 601 * runs into the next one or the tail, and no unit reads or writes past its own elements. Six blocks with six tails 602 * cost 6 x 64 KiB that the allocator handed back to the system at every free (glibc trims a free top above 128 KiB), 603 * so every small call faulted its pages in again. ab and pa: a b, and P acc, zeroed on their first use (as gx_buf did 604 * when it made them then), so a call that never converts or never sums never touches them. */ 605 typedef struct { 606 uint8_t *slab, *a, *b, *rowbad, *colbad; 607 float *P; 608 uint64_t *acc; 609 uint64_t na, nb, np, nacc; 610 int ab, pa; 611 } GxBufs; 612 613 /* A unit that refuses its views (32), re-enters (33) or traps (1..14) is a defect of this shell, not of the call. */ 614 #define GX_FAULT OMEGA_GPU_GEMM_CODEGEN_FAIL 615 616 static uint64_t gx_fbits(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 617 static uint64_t gx_dbits(double d) { uint64_t u; memcpy(&u, &d, 8); return u; } 618 #if !CUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY 619 /* Pure scalar calls have no refusal path for validated shell inputs. A trap is a shell defect. */ 620 static uint64_t gx_policy(uint64_t op, uint64_t a, uint64_t b, uint64_t c, uint64_t d) { 621 uint64_t result; 622 if (omega_gemm_ex_policy(&result, op, a, b, c, d) != OMEGA_OK) abort(); 623 return result; 624 } 625 #endif 626 static uint64_t gx_min(uint64_t a, uint64_t b) { 627 #if CUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY 628 return a < b ? a : b; 629 #else 630 return gx_policy(0, a, b, 0, 0); 631 #endif 632 } 633 634 static void gx_load(Gx *g, const OmegaGemmExArgs *a) { 635 uint64_t *x = g->x; 636 x[0] = (uint64_t)a->op_a; x[1] = (uint64_t)a->op_b; x[2] = a->m; x[3] = a->n; x[4] = a->k; x[5] = a->batch; 637 x[6] = (uint64_t)a->a_type; x[7] = (uint64_t)a->b_type; x[8] = (uint64_t)a->c_type; 638 x[9] = a->lda; x[10] = a->stride_a; x[11] = a->ldb; x[12] = a->stride_b; x[13] = a->ldc; x[14] = a->stride_c; 639 x[15] = gx_fbits(a->alpha); x[16] = gx_fbits(a->beta); x[17] = (uint64_t)a->compute; x[18] = (uint64_t)a->scale; 640 x[19] = (uint64_t)(uintptr_t)a->scale_a; x[20] = (uint64_t)(uintptr_t)a->scale_b; 641 x[21] = a->scale_stride_a; x[22] = a->scale_stride_b; x[23] = a->block_m; x[24] = a->block_n; x[25] = a->block_k; 642 x[26] = (uint64_t)(uintptr_t)a->a; x[27] = (uint64_t)(uintptr_t)a->b; x[28] = (uint64_t)(uintptr_t)a->c; 643 x[29] = (uint64_t)(int64_t)g_mutant; 644 } 645 646 static int gx_plan(Gx *g, uint64_t op) { 647 static const int code[4] = {OMEGA_GPU_GEMM_OK, OMEGA_GPU_GEMM_BAD_ARGS, OMEGA_GPU_GEMM_TOO_LARGE, OMEGA_GPU_GEMM_UNSUPPORTED}; 648 if (omega_gemm_ex_plan(&g->res, op, g->x, 48, g->p, 64) != OMEGA_OK || g->res > 3u) return GX_FAULT; 649 return code[g->res]; 650 } 651 652 /* the row unit: ops 0 to 7 also read src as words, so src is a word-aligned window there (ga, gb, or in place; see 653 * gx_bad7); ops 8 and 9 read only bytes and get ga as the unused word view. Ops 5 and 6 (the bf16x3 planes) are the x3 654 * unit, which stores whole words: straight into dst when it is word aligned, else through gc */ 655 static int gx_row(Gx *g, uint64_t op, uint64_t t, uint64_t n, const uint8_t *src, uint8_t *dst) { 656 const uint8_t *w = op < 8u ? src : g->ga; 657 uint32_t s; 658 if (op == 5u || op == 6u) { 659 uint8_t *d = ((uintptr_t)dst & 7u) ? g->gc : dst; 660 s = omega_gemm_ex_x3(&g->res, op, t, n, w, GX_WIN / 8u, d, GX_WIN / 8u); 661 if (s == OMEGA_OK && d != dst) memcpy(dst, d, g->res * 2u); 662 } else { 663 s = omega_gemm_ex_row(&g->res, op | t << 4, n, src, GX_WIN, w, GX_WIN / 8u, g->tab, 257, dst, GX_WIN); 664 } 665 return s == OMEGA_OK ? OMEGA_GPU_GEMM_OK : GX_FAULT; 666 } 667 668 /* n elements of es bytes from element i0 of src at element stride st, packed into dst */ 669 static void gx_gather(uint8_t *dst, const uint8_t *src, uint64_t i0, uint64_t st, uint64_t n, uint64_t es) { 670 if (st == 1u) { memcpy(dst, src + i0 * es, n * es); return; } 671 for (uint64_t i = 0; i < n; i++) memcpy(dst + i * es, src + (i0 + i * st) * es, es); 672 } 673 674 /* the start element and stride of: row i of op(A) from k0; column j of op(B) from k0; row kk of op(B) from column n0 */ 675 static uint64_t gx_arow(const Gx *g, uint64_t i, uint64_t k0, uint64_t *st) { 676 #if CUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY 677 *st = g->x[0] ? g->p[2] : 1u; 678 return g->x[0] ? k0 * g->p[2] + i : i * g->p[2] + k0; 679 #else 680 *st = gx_policy(2, g->x[0], g->p[2], 0, 0); 681 return gx_policy(1, g->x[0], g->p[2], i, k0); 682 #endif 683 } 684 static uint64_t gx_bcol(const Gx *g, uint64_t j, uint64_t k0, uint64_t *st) { 685 #if CUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY 686 *st = g->p[5] ? 1u : g->p[3]; 687 return g->p[5] ? j * g->p[3] + k0 : k0 * g->p[3] + j; 688 #else 689 *st = gx_policy(7, g->p[5], g->p[3], 0, 0); 690 return gx_policy(1, g->p[5], g->p[3], k0, j); 691 #endif 692 } 693 static uint64_t gx_brow(const Gx *g, uint64_t kk, uint64_t n0, uint64_t *st) { 694 #if CUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY 695 *st = g->p[5] ? g->p[3] : 1u; 696 return g->p[5] ? n0 * g->p[3] + kk : kk * g->p[3] + n0; 697 #else 698 *st = gx_policy(2, g->p[5], g->p[3], 0, 0); 699 return gx_policy(1, g->p[5], g->p[3], kk, n0); 700 #endif 701 } 702 703 /* the scale arrays: count floats in each of batches copies stride apart; a non-finite scale refuses the call */ 704 static int gx_scales(Gx *g, const float *s, uint64_t count, uint64_t batches, uint64_t stride) { 705 for (uint64_t b = 0; b < batches; b++) 706 for (uint64_t i0 = 0; i0 < count; i0 += GX_WIN / 4u) { 707 const uint64_t cn = gx_min(GX_WIN / 4u, count - i0); 708 memcpy(g->ga, s + b * stride + i0, cn * 4u); 709 if (gx_row(g, 7, OMEGA_GEMM_DT_F32, cn, g->ga, g->gb)) return GX_FAULT; 710 if (g->res) return OMEGA_GPU_GEMM_BAD_ARGS; 711 } 712 return OMEGA_GPU_GEMM_OK; 713 } 714 715 /* rows of n elements per packed unit run: as many as fit the byte windows of src (es bytes each) and dst (ce bytes each); 716 * 1 when the op is not element-wise (5 6, the bf16x3 planes) */ 717 static uint64_t gx_pack(uint64_t op, uint64_t n, uint64_t es, uint64_t ce) { 718 #if CUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY 719 const uint64_t w = n * (es > ce ? es : ce); 720 return op != 5u && op != 6u && w <= GX_WIN ? GX_WIN / w : 1u; 721 #else 722 return gx_policy(3, op, n, es, ce); 723 #endif 724 } 725 726 /* convert rn packed rows of n elements in src into dst rows ld bytes apart (ce bytes per converted element): straight 727 * into dst when the rows are adjacent there, else through gc */ 728 static int gx_conv(Gx *g, uint64_t op, uint64_t t, uint64_t n, uint64_t rn, const uint8_t *src, uint8_t *dst, uint64_t ld, 729 uint64_t ce) { 730 #if CUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY 731 if (rn == 1u || ld == n * ce) return gx_row(g, op, t, rn * n, src, dst); 732 if (gx_row(g, op, t, rn * n, src, g->gc)) return GX_FAULT; 733 for (uint64_t r = 0; r < rn; r++) memcpy(dst + r * ld, g->gc + r * n * ce, n * ce); 734 return OMEGA_GPU_GEMM_OK; 735 #else 736 const uint64_t count = gx_policy(5, rn, n, 0, 0), bytes = gx_policy(5, n, ce, 0, 0); 737 if (gx_policy(4, rn, ld, n, ce)) return gx_row(g, op, t, count, src, dst); 738 if (gx_row(g, op, t, count, src, g->gc)) return GX_FAULT; 739 for (uint64_t r = 0; r < rn; r++) 740 memcpy(dst + gx_policy(5, r, ld, 0, 0), g->gc + gx_policy(6, r, n, ce, 0), bytes); 741 return OMEGA_GPU_GEMM_OK; 742 #endif 743 } 744 745 /* op 7 over the n elements (es bytes) at *s: read in place when *s is word aligned and a whole window from it lies before 746 * end, else copied to ga (and *s set to ga), so op 8 can read the same run */ 747 static int gx_bad7(Gx *g, uint64_t t, uint64_t n, uint64_t es, const uint8_t **s, const uint8_t *end) { 748 if (((uintptr_t)*s & 7u) || (uint64_t)(end - *s) < GX_WIN) { 749 memcpy(g->ga, *s, n * es); 750 *s = g->ga; 751 } 752 const uint32_t r = omega_gemm_ex_row(&g->res, 7u | t << 4, n, *s, GX_WIN, *s, GX_WIN / 8u, g->tab, 257, g->gb, GX_WIN); 753 return r == OMEGA_OK ? OMEGA_GPU_GEMM_OK : GX_FAULT; 754 } 755 756 /* one stored operand of rows x cols elements (type t, es bytes, leading dimension ld): per_row sets flag[r] for a row 757 * holding a non-finite element, else flag[c] for each non-finite column c; *any is set when one exists. Short rows go 758 * through op 7 packed, as many as fill a window (in place when the rows are adjacent, else gathered into ga); only a 759 * packed run holding a non-finite element is scanned again row by row. */ 760 static int gx_scan(Gx *g, uint64_t t, uint64_t es, const uint8_t *src, uint64_t rows, uint64_t cols, uint64_t ld, int per_row, 761 uint8_t *flag, uint64_t *any) { 762 const uint64_t cw = GX_WIN / es, pr = gx_pack(7, cols, es, es); 763 const uint8_t *end = rows ? src + ((rows - 1u) * ld + cols) * es : src, *s; 764 for (uint64_t r0 = 0; r0 < rows; r0 += pr) { 765 const uint64_t rn = gx_min(pr, rows - r0); 766 if (rn > 1u) { 767 s = src + r0 * ld * es; 768 if (ld != cols) { 769 for (uint64_t r = 0; r < rn; r++) memcpy(g->ga + r * cols * es, s + r * ld * es, cols * es); 770 s = g->ga; 771 } 772 if (gx_bad7(g, t, rn * cols, es, &s, s == g->ga ? g->ga + GX_WIN : end)) return GX_FAULT; 773 if (!g->res) continue; 774 } 775 for (uint64_t r = r0; r < r0 + rn; r++) 776 for (uint64_t c0 = 0; c0 < cols; c0 += cw) { 777 const uint64_t cn = gx_min(cw, cols - c0); 778 s = src + (r * ld + c0) * es; 779 if (gx_bad7(g, t, cn, es, &s, end)) return GX_FAULT; 780 if (!g->res) continue; 781 *any = 1; 782 if (per_row) { flag[r] = 1; break; } 783 if (gx_row(g, 8, t, cn, s, flag + c0)) return GX_FAULT; 784 } 785 } 786 return OMEGA_GPU_GEMM_OK; 787 } 788 789 /* one C3 call for the tile part the plan's op 1 described: P (mc x nc, f32) = A'(mc x K) . B'; A and B are this batch's */ 790 static int gx_part(Gx *g, GxBufs *bf, const uint8_t *A, const uint8_t *B, uint64_t m0, uint64_t mc, uint64_t n0, uint64_t nc, 791 uint64_t k0, float *P, uint64_t *calls) { 792 const uint64_t *p = g->p, ku = p[37], K = p[47], ce = p[25], ea = p[26], eb = p[27]; 793 const OmegaGpuGemmType t = (OmegaGpuGemmType)p[7]; 794 uint64_t st; 795 (*calls)++; 796 if (p[39]) 797 return g_core(g_core_ctx, t, (unsigned)p[24], (uint32_t)mc, (uint32_t)nc, (uint32_t)ku, A + p[40], p[2], B + p[41], p[3], P); 798 if (!bf->ab) { 799 memset(bf->a, 0, bf->na); 800 memset(bf->b, 0, bf->nb); 801 bf->ab = 1; 802 } 803 const uint64_t ra = gx_pack(p[30], ku, ea, ce); 804 for (uint64_t i = 0, rn; i < mc; i += rn) { 805 rn = gx_min(ra, mc - i); 806 for (uint64_t r = 0; r < rn; r++) { 807 const uint64_t i0 = gx_arow(g, m0 + i + r, k0, &st); 808 gx_gather(g->ga + r * ku * ea, A, i0, st, ku, ea); 809 } 810 if (gx_conv(g, p[30], g->x[6], ku, rn, g->ga, bf->a + i * K * ce, K * ce, ce)) return GX_FAULT; 811 } 812 if (p[32]) { 813 const uint64_t rb = gx_pack(p[31], nc, eb, ce); 814 for (uint64_t kk = 0, rn; kk < ku; kk += rn) { 815 rn = gx_min(rb, ku - kk); 816 for (uint64_t r = 0; r < rn; r++) { 817 const uint64_t i0 = gx_brow(g, k0 + kk + r, n0, &st); 818 gx_gather(g->gb + r * nc * eb, B, i0, st, nc, eb); 819 } 820 if (gx_conv(g, p[31], g->x[7], nc, rn, g->gb, bf->b + kk * nc * ce, nc * ce, ce)) return GX_FAULT; 821 } 822 } else { 823 const uint64_t rb = gx_pack(p[31], ku, eb, ce); 824 for (uint64_t j = 0, rn; j < nc; j += rn) { 825 rn = gx_min(rb, nc - j); 826 for (uint64_t r = 0; r < rn; r++) { 827 const uint64_t i0 = gx_bcol(g, n0 + j + r, k0, &st); 828 gx_gather(g->gb + r * ku * eb, B, i0, st, ku, eb); 829 } 830 if (gx_conv(g, p[31], g->x[7], ku, rn, g->gb, bf->b + j * K * ce, K * ce, ce)) return GX_FAULT; 831 } 832 } 833 return g_core(g_core_ctx, t, (unsigned)p[33], (uint32_t)mc, (uint32_t)nc, (uint32_t)K, bf->a, 0, bf->b, 0, P); 834 } 835 836 /* the host recompute of row i (global) of the tile at column n0: fix[j] for every flagged j, in block_k runs */ 837 static int gx_fixrow(Gx *g, const uint8_t *A, const uint8_t *B, const float *sa, const float *sb, uint64_t i, uint64_t n0, uint64_t nc) { 838 const uint64_t *x = g->x, *p = g->p, k = x[4], blk = x[18] == OMEGA_GEMM_SCALE_BLOCK; 839 uint64_t st; 840 for (uint64_t j = 0; j < nc; j++) { 841 if (!g->fl[j]) continue; 842 uint64_t s = 0; 843 for (uint64_t k0 = 0, cn; k0 < k; k0 += cn) { 844 cn = gx_min(GX_WIN / 4u, k - k0); 845 if (blk) cn = gx_min(cn, x[25] - k0 % x[25]); 846 uint64_t i0 = gx_arow(g, i, k0, &st); 847 gx_gather(g->ga, A, i0, st, cn, p[26]); 848 i0 = gx_bcol(g, n0 + j, k0, &st); 849 gx_gather(g->gb, B, i0, st, cn, p[27]); 850 const uint64_t q[8] = {x[6], x[7], cn, s, blk, blk ? gx_fbits(sa[(i / x[23]) * p[13] + k0 / x[25]]) : 0u, 851 blk ? gx_fbits(sb[(k0 / x[25]) * p[14] + (n0 + j) / x[24]]) : 0u, 0u}; 852 memcpy(g->q, q, sizeof q); 853 if (omega_gemm_ex_dot(&g->res, g->q, 8, g->ga, GX_WIN / 8u, g->gb, GX_WIN / 8u) != OMEGA_OK) return GX_FAULT; 854 s = g->res; 855 } 856 g->fix[j] = s; 857 } 858 return OMEGA_GPU_GEMM_OK; 859 } 860 861 /* the k-part sum of the tile rows: acc row i += P row i (times the block scales of k block p[38]). Without block scales 862 * the factor is 1.0 for every element, so the rows go packed, as many as fill one acc window. */ 863 static int gx_accrows(Gx *g, const float *sa, const float *sb, uint64_t m0, uint64_t mc, uint64_t n0, uint64_t nc, const float *P, 864 uint64_t *acc) { 865 const uint64_t *x = g->x, *p = g->p, blk = x[18] == OMEGA_GEMM_SCALE_BLOCK, kb = p[38], pr = blk ? 1u : GX_ROW / nc; 866 uint64_t q[8] = {nc, blk, n0, blk ? x[24] : 1u, 0u, 0u, 0u, 0u}; 867 if (blk) memcpy(g->gc + 4, sb + kb * p[14] + n0 / x[24], ((n0 + nc - 1u) / x[24] - n0 / x[24] + 1u) * 4u); 868 for (uint64_t i = 0, rn; i < mc; i += rn) { 869 rn = gx_min(pr, mc - i); 870 if (blk) memcpy(g->gc, sa + ((m0 + i) / x[23]) * p[13] + kb, 4); 871 q[0] = rn * nc; 872 memcpy(g->q, q, sizeof q); 873 if (omega_gemm_ex_acc(&g->res, g->q, 8, P + i * nc, GX_WIN / 4u, g->gc, GX_WIN / 4u, acc + i * nc, GX_ROW) != OMEGA_OK) 874 return GX_FAULT; 875 } 876 return OMEGA_GPU_GEMM_OK; 877 } 878 879 /* the epilogue of the tile rows into C (this batch's), with the recompute of the flagged elements. With no non-finite 880 * input (or nothing computed) no element is recomputed, so the rows go packed, as many as fill one window: their C 881 * elements gathered into gc and back. */ 882 static int gx_epirows(Gx *g, GxBufs *bf, const uint8_t *A, const uint8_t *B, const float *sa, const float *sb, uint8_t *C, 883 uint64_t any, uint64_t m0, uint64_t mc, uint64_t n0, uint64_t nc, uint64_t *fixed) { 884 const uint64_t *x = g->x, *p = g->p, ec = p[28], compute = p[10], one = p[9], pr = compute && any ? 1u : GX_ROW / nc; 885 uint64_t q[8] = {nc, x[8], compute, one, p[42], p[11], 0u, 0u}; 886 for (uint64_t i = 0, rn; i < mc; i += rn) { 887 rn = gx_min(pr, mc - i); 888 if (compute) { 889 if (gx_row(g, 9, any | (uint64_t)bf->rowbad[m0 + i] << 1, rn * nc, bf->colbad + n0, g->fl)) return GX_FAULT; 890 if (any && gx_fixrow(g, A, B, sa, sb, m0 + i, n0, nc)) return GX_FAULT; 891 } 892 const uint8_t *pv = compute ? (const uint8_t *)(bf->P + i * nc) : (const uint8_t *)gx_zero; 893 const uint64_t *av = compute && !one ? bf->acc + i * nc : gx_zero; 894 for (uint64_t r = 0; r < rn; r++) memcpy(g->gc + r * nc * ec, C + ((m0 + i + r) * p[4] + n0) * ec, nc * ec); 895 q[0] = rn * nc; 896 memcpy(g->q, q, sizeof q); 897 if (omega_gemm_ex_epi(&g->res, g->q, 8, pv, GX_WIN / 4u, av, GX_ROW, g->fix, GX_ROW, g->fl, GX_ROW, g->gc, GX_WIN / 2u) != OMEGA_OK) 898 return GX_FAULT; 899 for (uint64_t r = 0; r < rn; r++) memcpy(C + ((m0 + i + r) * p[4] + n0) * ec, g->gc + r * nc * ec, nc * ec); 900 *fixed += g->res; 901 } 902 return OMEGA_GPU_GEMM_OK; 903 } 904 905 /* one batch: the bad-element scan, the per-batch plan (op 2), then the direct rows or the tiles */ 906 static int gx_batch(Gx *g, GxBufs *bf, const OmegaGemmExArgs *args, uint64_t b, uint64_t *calls, uint64_t *fixed) { 907 uint64_t *x = g->x; 908 const uint64_t *p = g->p, m = x[2], n = x[3], k = x[4]; 909 const uint8_t *A = (const uint8_t *)args->a + b * x[10] * p[26], *B = (const uint8_t *)args->b + b * x[12] * p[27]; 910 uint8_t *C = (uint8_t *)args->c + b * x[14] * p[28]; 911 const float *sa = args->scale_a ? args->scale_a + b * args->scale_stride_a : NULL; 912 const float *sb = args->scale_b ? args->scale_b + b * args->scale_stride_b : NULL; 913 uint64_t any = 0; 914 int rc; 915 if (p[10]) { 916 memset(bf->rowbad, 0, m); 917 memset(bf->colbad, 0, n); 918 if ((rc = gx_scan(g, x[6], p[26], A, x[0] ? k : m, x[0] ? m : k, p[2], !x[0], bf->rowbad, &any))) return rc; 919 if ((rc = gx_scan(g, x[7], p[27], B, p[5] ? n : k, p[5] ? k : n, p[3], (int)p[5], bf->colbad, &any))) return rc; 920 x[30] = x[18] != OMEGA_GEMM_SCALE_NONE ? gx_fbits(sa[0]) : 0u; 921 x[31] = x[18] != OMEGA_GEMM_SCALE_NONE ? gx_fbits(sb[0]) : 0u; 922 x[32] = any; 923 if ((rc = gx_plan(g, 2))) return rc; 924 if (p[43]) { 925 for (uint64_t m0 = 0; m0 < m; m0 += p[20]) { 926 const uint64_t mc = gx_min(p[20], m - m0); 927 x[30] = 0; x[31] = mc; x[32] = n; x[33] = 0; x[34] = m0; x[35] = 0; 928 if ((rc = gx_plan(g, 1))) return rc; 929 float *out = (float *)C + m0 * p[4]; 930 if (!p[37]) memset(out, 0, mc * n * 4u); 931 else if ((rc = gx_part(g, bf, A, B, m0, mc, 0, n, 0, out, calls))) return rc; 932 } 933 return OMEGA_GPU_GEMM_OK; 934 } 935 if (!bf->pa) { 936 memset(bf->P, 0, bf->np); 937 memset(bf->acc, 0, bf->nacc); 938 bf->pa = 1; 939 } 940 } 941 for (uint64_t m0 = 0; m0 < m; m0 += p[21]) 942 for (uint64_t n0 = 0; n0 < n; n0 += p[22]) { 943 const uint64_t mc = gx_min(p[21], m - m0), nc = gx_min(p[22], n - n0); 944 if (p[10]) { 945 if (p[9]) { 946 x[30] = 0; x[31] = mc; x[32] = nc; x[33] = any; x[34] = m0; x[35] = n0; 947 if ((rc = gx_plan(g, 1))) return rc; 948 if (!p[37]) memset(bf->P, 0, mc * nc * 4u); 949 else if ((rc = gx_part(g, bf, A, B, m0, mc, n0, nc, 0, bf->P, calls))) return rc; 950 } else { 951 memset(bf->acc, 0, mc * nc * 8u); 952 for (uint64_t k0 = 0; k0 < k; k0 += p[36]) { 953 x[30] = k0; x[31] = mc; x[32] = nc; x[33] = any; x[34] = m0; x[35] = n0; 954 if ((rc = gx_plan(g, 1))) return rc; 955 if (!p[37]) continue; 956 if ((rc = gx_part(g, bf, A, B, m0, mc, n0, nc, k0, bf->P, calls))) return rc; 957 if ((rc = gx_accrows(g, sa, sb, m0, mc, n0, nc, bf->P, bf->acc))) return rc; 958 } 959 } 960 } 961 if ((rc = gx_epirows(g, bf, A, B, sa, sb, C, any, m0, mc, n0, nc, fixed))) return rc; 962 } 963 return OMEGA_GPU_GEMM_OK; 964 } 965 966 static int gx_run(Gx *g, const OmegaGemmExArgs *args) { 967 const uint64_t *p = g->p; 968 GxBufs bf = {0}; 969 uint64_t calls = 0, fixed = 0; 970 int rc = OMEGA_GPU_GEMM_OK; 971 if (p[10]) { 972 /* acc rowbad colbad P a b, each from a 64-byte boundary. The acc unit edits a GX_ROW-word window of acc while it 973 * reads a GX_WIN window of P, and a unit refuses an edited view that overlaps another one (32), so acc comes 974 * first and keeps GX_ROW words of its own after its end. acc only when the k parts are summed (not p[9]). */ 975 const uint64_t tile = p[21] * p[22]; 976 uint64_t o[7] = {0}, n[6] = {p[9] ? 0u : tile * 8u, g->x[2], g->x[3], tile * 4u, p[34] * p[25], p[35] * p[25]}; 977 int over = 0; 978 for (int i = 0; i < 6; i++) 979 over |= (n[i] > ~(uint64_t)63u) | __builtin_add_overflow(o[i], (n[i] + 63u) & ~(uint64_t)63u, &o[i + 1]); 980 if (n[0]) { 981 for (int i = 1; i < 7; i++) over |= __builtin_add_overflow(o[i], GX_ROW * 8u, &o[i]); 982 } 983 bf.slab = over || o[6] > UINT64_MAX - GX_WIN ? NULL : omega_gpu_host_alloc(o[6] + GX_WIN); 984 if (!bf.slab) rc = OMEGA_GPU_GEMM_CHIP_FAIL; 985 else { 986 bf.acc = (uint64_t *)bf.slab; bf.rowbad = bf.slab + o[1]; bf.colbad = bf.slab + o[2]; 987 bf.P = (float *)(bf.slab + o[3]); bf.a = bf.slab + o[4]; bf.b = bf.slab + o[5]; 988 bf.nacc = n[0]; bf.np = n[3]; bf.na = n[4]; bf.nb = n[5]; 989 memset(bf.rowbad, 0, o[3] - o[1]); 990 } 991 } 992 for (uint64_t b = 0; b < p[1] && rc == OMEGA_GPU_GEMM_OK; b++) rc = gx_batch(g, &bf, args, b, &calls, &fixed); 993 if (args->info) { 994 args->info->route = p[10] ? (uint32_t)p[6] : OMEGA_GEMM_ROUTE_NONE; 995 args->info->core_type = p[10] ? (uint32_t)p[7] : 0u; 996 args->info->core_calls = calls; 997 args->info->host_fixed = fixed; 998 } 999 omega_gpu_host_free(bf.slab); 1000 return rc; 1001 } 1002 1003 static int om_gemm_ex(const OmegaGemmExArgs *args) { 1004 if (!args) return OMEGA_GPU_GEMM_BAD_ARGS; 1005 Gx *g = gx_new(); 1006 if (!g) return OMEGA_GPU_GEMM_CHIP_FAIL; 1007 gx_load(g, args); 1008 int rc = gx_plan(g, 0); 1009 if (rc == OMEGA_GPU_GEMM_OK) rc = gx_scales(g, args->scale_a, g->p[15], g->p[17], args->scale_stride_a); 1010 if (rc == OMEGA_GPU_GEMM_OK) rc = gx_scales(g, args->scale_b, g->p[16], g->p[18], args->scale_stride_b); 1011 if (rc == OMEGA_GPU_GEMM_OK) rc = gx_run(g, args); 1012 gx_drop(g); 1013 return rc; 1014 } 1015 1016 static void gx_fp8_load(Gx *g, const uint8_t *w, const float *scale, uint32_t n, uint32_t k, uint32_t block, const float *x32, 1017 const uint16_t *x16, uint32_t m, const float *y, uint64_t ldy) { 1018 uint64_t *x = g->x; 1019 x[29] = (uint64_t)(int64_t)g_mutant; 1020 x[30] = (uint64_t)(uintptr_t)w; x[31] = (uint64_t)(uintptr_t)scale; x[32] = (uint64_t)(uintptr_t)y; 1021 x[33] = (uint64_t)(uintptr_t)x32; x[34] = (uint64_t)(uintptr_t)x16; 1022 x[35] = n; x[36] = k; x[37] = m; x[38] = block; x[39] = ldy; 1023 } 1024 1025 static int om_fp8_block_rows(const uint8_t *w, const float *scale, double scale_mul, uint32_t n, uint32_t k, uint32_t block, 1026 const float *x32, const uint16_t *x16, uint32_t m, float *y, uint64_t ldy, OmegaGemmExInfo *info) { 1027 if (info) memset(info, 0, sizeof *info); 1028 Gx *g = gx_new(); 1029 if (!g) return OMEGA_GPU_GEMM_CHIP_FAIL; 1030 gx_fp8_load(g, w, scale, n, k, block, x32, x16, m, y, ldy); 1031 int rc = gx_plan(g, 3); 1032 const uint64_t ly = g->p[1], kb = g->p[2], rows = g->p[3], at = g->p[4]; 1033 uint8_t *slab = rc == OMEGA_GPU_GEMM_OK ? gx_buf(rows * k * 4u) : NULL; 1034 if (rc == OMEGA_GPU_GEMM_OK && !slab) rc = OMEGA_GPU_GEMM_CHIP_FAIL; 1035 const uint64_t cw = GX_WIN / 4u - 1u; /* 16383 weights per run: a run's scales, shifted one block, stay in one window */ 1036 for (uint64_t j0 = 0; j0 < n && rc == OMEGA_GPU_GEMM_OK; j0 += rows) { 1037 const uint64_t nr = gx_min(rows, n - j0); 1038 for (uint64_t j = 0; j < nr && rc == OMEGA_GPU_GEMM_OK; j++) { 1039 const uint8_t *wr = w + (j0 + j) * k; 1040 const float *sr = scale + (j0 + j) / block * kb; 1041 for (uint64_t c0 = 0; c0 < k; c0 += cw) { 1042 const uint64_t cn = gx_min(cw, k - c0), sbase = c0 / block; 1043 memcpy(g->ga, wr + c0, cn); 1044 memcpy(g->gb, sr + sbase, gx_min(GX_WIN / 4u, kb - sbase) * 4u); 1045 const uint64_t q[8] = {cn, c0, block, kb, gx_dbits(scale_mul), (uint64_t)(int64_t)g_mutant, sbase, gx_fbits(sr[0])}; 1046 memcpy(g->q, q, sizeof q); 1047 if (omega_gemm_ex_fp8(&g->res, g->q, 8, g->ga, GX_WIN, g->gb, GX_WIN / 4u, slab + (j * k + c0) * 4u, GX_WIN / 4u) != OMEGA_OK || 1048 g->res) { 1049 rc = GX_FAULT; 1050 break; 1051 } 1052 } 1053 } 1054 if (rc != OMEGA_GPU_GEMM_OK) break; 1055 OmegaGemmExInfo pi = {0}; 1056 OmegaGemmExArgs a = { 1057 .op_a = OMEGA_GEMM_OP_N, .op_b = OMEGA_GEMM_OP_T, .m = m, .n = nr, .k = k, .batch = 1, 1058 .a_type = (OmegaGemmDtype)at, .b_type = OMEGA_GEMM_DT_F32, .c_type = OMEGA_GEMM_DT_F32, 1059 .a = x32 ? (const void *)x32 : (const void *)x16, .lda = k, .b = slab, .ldb = k, .c = y + j0, .ldc = ly, 1060 .alpha = 1.0f, .beta = 0.0f, .info = &pi, 1061 }; 1062 rc = om_gemm_ex(&a); 1063 if (info) { info->route = pi.route; info->core_type = pi.core_type; info->core_calls += pi.core_calls; info->host_fixed += pi.host_fixed; } 1064 } 1065 omega_gpu_host_free(slab); 1066 gx_drop(g); 1067 return rc; 1068 } 1069 #endif /* GX_OM */ 1070 1071 /* ------------------------------------------------------------------ the entries */ 1072 #if CUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF 1073 #ifndef OMEGA_GPU_NO_POSIX /* the differential is a hosted test build only */ 1074 #include 1075 #include 1076 #else 1077 #error "CUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF is a hosted test build" 1078 #endif 1079 1080 /* Both paths on every call: the C runs on a copy of C (or y), the Omega path on the caller's; return code, info and 1081 * output bytes must agree bit for bit. CUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF_SELFTEST flips one bit of the C copy 1082 * (the red check). */ 1083 static void gx_diff(int crc, int orc, const OmegaGemmExInfo *ci, const OmegaGemmExInfo *oi, uint8_t *ref, const void *out, uint64_t bytes) { 1084 const char *what = NULL; 1085 #ifdef CUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF_SELFTEST 1086 if (bytes) ref[bytes / 2u] ^= 1u; 1087 #endif 1088 if (crc != orc) what = "return codes differ"; 1089 else if (memcmp(ci, oi, sizeof *ci)) what = "info differs"; 1090 else if (bytes && memcmp(ref, out, bytes)) what = "output bytes differ"; 1091 if (!what) return; 1092 fprintf(stderr, "FAIL gemm_ex C-vs-Omega differential: %s (C rc %d, Omega rc %d)\n", what, crc, orc); 1093 abort(); 1094 } 1095 1096 int omega_gpu_gemm_ex(const OmegaGemmExArgs *args) { 1097 OmegaGemmExInfo ci, oi; 1098 memset(&ci, 0xff, sizeof ci); 1099 memset(&oi, 0xff, sizeof oi); 1100 if (!args) { 1101 const int crc = c_gemm_ex(NULL), orc = om_gemm_ex(NULL); 1102 gx_diff(crc, orc, &ci, &oi, NULL, NULL, 0); 1103 return orc; 1104 } 1105 Gx *g = gx_new(); 1106 if (!g) return OMEGA_GPU_GEMM_CHIP_FAIL; 1107 gx_load(g, args); 1108 const uint64_t bytes = gx_plan(g, 0) == OMEGA_GPU_GEMM_OK ? g->p[46] * g->p[28] : 0u; 1109 gx_drop(g); 1110 uint8_t *ref = bytes ? omega_gpu_host_calloc(bytes, 1) : NULL; 1111 if (bytes && !ref) return OMEGA_GPU_GEMM_CHIP_FAIL; 1112 OmegaGemmExArgs ca = *args, oa = *args; 1113 if (ref) { memcpy(ref, args->c, bytes); ca.c = ref; } 1114 ca.info = &ci; 1115 oa.info = &oi; 1116 const int crc = c_gemm_ex(&ca), orc = om_gemm_ex(&oa); 1117 gx_diff(crc, orc, &ci, &oi, ref, args->c, bytes); 1118 omega_gpu_host_free(ref); 1119 if (args->info && oi.route != 0xffffffffu) *args->info = oi; 1120 return orc; 1121 } 1122 1123 int omega_gpu_gemm_ex_fp8_block_rows(const uint8_t *w, const float *scale, double scale_mul, uint32_t n, uint32_t k, uint32_t block, 1124 const float *x32, const uint16_t *x16, uint32_t m, float *y, uint64_t ldy, OmegaGemmExInfo *info) { 1125 OmegaGemmExInfo ci, oi; 1126 Gx *g = gx_new(); 1127 if (!g) return OMEGA_GPU_GEMM_CHIP_FAIL; 1128 gx_fp8_load(g, w, scale, n, k, block, x32, x16, m, y, ldy); 1129 const uint64_t bytes = gx_plan(g, 3) == OMEGA_GPU_GEMM_OK ? ((uint64_t)(m - 1u) * g->p[1] + n) * 4u : 0u; 1130 gx_drop(g); 1131 uint8_t *ref = bytes ? omega_gpu_host_calloc(bytes, 1) : NULL; 1132 if (bytes && !ref) return OMEGA_GPU_GEMM_CHIP_FAIL; 1133 if (ref) memcpy(ref, y, bytes); 1134 const int crc = c_fp8_block_rows(w, scale, scale_mul, n, k, block, x32, x16, m, ref ? (float *)(void *)ref : y, ldy, &ci); 1135 const int orc = om_fp8_block_rows(w, scale, scale_mul, n, k, block, x32, x16, m, y, ldy, &oi); 1136 gx_diff(crc, orc, &ci, &oi, ref, y, bytes); 1137 omega_gpu_host_free(ref); 1138 if (info) *info = oi; 1139 return orc; 1140 } 1141 #elif CUDAPP_MIGRATION_PENDING_GEMM_EX 1142 int omega_gpu_gemm_ex(const OmegaGemmExArgs *args) { return c_gemm_ex(args); } 1143 int omega_gpu_gemm_ex_fp8_block_rows(const uint8_t *w, const float *scale, double scale_mul, uint32_t n, uint32_t k, uint32_t block, 1144 const float *x32, const uint16_t *x16, uint32_t m, float *y, uint64_t ldy, OmegaGemmExInfo *info) { 1145 return c_fp8_block_rows(w, scale, scale_mul, n, k, block, x32, x16, m, y, ldy, info); 1146 } 1147 #else 1148 int omega_gpu_gemm_ex(const OmegaGemmExArgs *args) { return om_gemm_ex(args); } 1149 int omega_gpu_gemm_ex_fp8_block_rows(const uint8_t *w, const float *scale, double scale_mul, uint32_t n, uint32_t k, uint32_t block, 1150 const float *x32, const uint16_t *x16, uint32_t m, float *y, uint64_t ldy, OmegaGemmExInfo *info) { 1151 return om_fp8_block_rows(w, scale, scale_mul, n, k, block, x32, x16, m, y, ldy, info); 1152 } 1153 #endif