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/ggml-omega/blob/main/ggml-omega-gb10.c # ggml-omega aien-dev/ggml-omega 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 `ggml-omega / ggml-omega-gb10.c` 436 lines ยท 22381 bytes Raw Blame History 1 // ggml-omega-gb10.c: the "OmegaGB10" device of ggml-omega. MUL_MAT, RMS_NORM and GLU (swiglu) run on the 2 // NVIDIA GB10 through CUDA++ (aien-dev/CUDApp) runtime calls whose kernels come from Omega's own code 3 // generator (no CUDA toolkit, no NVIDIA user-space library). Every other op stays on ggml's CPU backend. 4 // 5 // INTERIM HOST GLUE, migration-pending C (build switch OMEGA_GGML_GB10=1 in build.sh, off by default). 6 // This file only fills tensor descriptors, stages rows into dense buffers and back (bit moves) and calls 7 // the CUDApp entries. The MUL_MAT shape rules, leading dimensions, batch strides and per-call byte 8 // offsets are computed by the seeded-Omega unit kernels/gbmm.om; q8_0 weights are dequantised by the 9 // Omega unit kernels/dq8r.om. Named .om successor: the same unit set exported as an Omega loadable 10 // object (aien-dev/omega-mv #636/#637) calling the CUDApp entries, once Omega has host callbacks. 11 // 12 // Arithmetic on the chip (CUDApp src/omega_gpu_gemm_ex.h, src/omega_gpu_elementwise_api.h): 13 // MUL_MAT omega_gpu_gemm_ex, A = src1 (f32), B = src0 op T (f32, f16 or bf16; q8_0 dequantised to f32), 14 // the f32 route is bf16x3 on HMMA.BF16 with fp32 tensor-core accumulation: not exact fp32. 15 // RMS_NORM omega_gpu_rmsnorm_f32 with weight 1 (dim a multiple of 128, at most 16384). 16 // GLU omega_gpu_swiglu_f32 (MUFU.EX2 / MUFU.RCP: approximate units). 17 // Same device model as the host device: ACCEL with the CPU buffer type, so weights stay in CPU buffers. 18 19 #include "ggml-backend-impl.h" 20 #include "ggml.h" 21 #include "ggml-omega-gb10.h" 22 23 #include "omega_gpu_bf16_gemv_api.h" 24 #include "omega_gpu_elementwise_api.h" 25 #include "omega_gpu_gemm_ex.h" 26 27 #include 28 #include 29 #include 30 #include 31 #include 32 #include 33 34 static int32_t gb_op_i32(const struct ggml_tensor *t, uint32_t i) { int32_t v; memcpy(&v, (const char *) t->op_params + 4 * i, 4); return v; } 35 36 // Omega units (generated by omega-bridge src/native.om, see build.sh) 37 uint32_t omega_gbmm(uint64_t *r, const void *d, uint64_t d_n, uint64_t g, void *p, uint64_t p_n); 38 uint32_t omega_dq8r(uint64_t *r, const void *w, uint64_t w_n, void *y, uint64_t y_n); 39 uint32_t omega_h2fv_32(uint64_t *r, const void *s, uint64_t s_n, void *d, uint64_t d_n); 40 uint32_t omega_bfs_256(uint64_t *r, const void *s, uint64_t s_n, void *h, uint64_t h_n, void *l, uint64_t l_n); 41 uint32_t omega_bin_32(uint64_t *r, uint64_t op, const void *a, uint64_t a_n, const void *b, uint64_t b_n, void *d, uint64_t d_n); 42 uint32_t omega_bin_1(uint64_t *r, uint64_t op, const void *a, uint64_t a_n, const void *b, uint64_t b_n, void *d, uint64_t d_n); 43 44 #define OK(call) \ 45 do { \ 46 uint32_t st_ = (call); \ 47 if (st_ != 0) GGML_ABORT("ggml-omega-gb10: %s returned Omega status %u", #call, st_); \ 48 } while (0) 49 50 static bool f32t(const struct ggml_tensor *t) { return t && t->type == GGML_TYPE_F32; } 51 static bool dense_rows(const struct ggml_tensor *t) { return t->nb[0] == ggml_type_size(t->type); } 52 53 // --- MUL_MAT --------------------------------------------------------------------------------------- 54 55 // src0 as the gemm sees it: its own bytes (f32, f16, bf16) or a dense f32 copy of a q8_0 tensor. 56 struct w_view { const char *data; size_t nb[4]; uint64_t es; OmegaGemmDtype dt; }; 57 58 static void w_desc(const struct ggml_tensor *w, struct w_view *v) { 59 if (w->type == GGML_TYPE_Q8_0) { 60 v->es = 4; v->dt = OMEGA_GEMM_DT_F32; 61 v->nb[0] = 4; v->nb[1] = 4 * w->ne[0]; v->nb[2] = v->nb[1] * w->ne[1]; v->nb[3] = v->nb[2] * w->ne[2]; 62 } else { 63 v->es = ggml_type_size(w->type); 64 v->dt = w->type == GGML_TYPE_F32 ? OMEGA_GEMM_DT_F32 : w->type == GGML_TYPE_F16 ? OMEGA_GEMM_DT_F16 : OMEGA_GEMM_DT_BF16; 65 for (int i = 0; i < 4; i++) v->nb[i] = w->nb[i]; 66 } 67 v->data = w->data; 68 } 69 70 static void mm_desc(const struct ggml_tensor *dst, const struct w_view *v, uint64_t d[32]) { 71 const struct ggml_tensor *w = dst->src[0], *x = dst->src[1]; 72 memset(d, 0, 32 * sizeof *d); 73 for (int i = 0; i < 4; i++) { 74 d[i] = w->ne[i]; d[4 + i] = v->nb[i]; 75 d[9 + i] = x->ne[i]; d[13 + i] = x->nb[i]; 76 d[18 + i] = dst->ne[i]; d[22 + i] = dst->nb[i]; 77 } 78 d[8] = v->es; d[17] = ggml_type_size(x->type); d[26] = ggml_type_size(dst->type); 79 } 80 81 static bool gv_supported(const struct ggml_tensor *op); 82 83 static bool mm_supported(const struct ggml_tensor *op) { 84 const struct ggml_tensor *w = op->src[0], *x = op->src[1]; 85 if (!f32t(op) || !f32t(x)) return false; 86 if (w->type != GGML_TYPE_F32 && w->type != GGML_TYPE_F16 && w->type != GGML_TYPE_BF16 && w->type != GGML_TYPE_Q8_0) return false; 87 if (w->type == GGML_TYPE_Q8_0 && (!dense_rows(w) || w->ne[0] % 32 != 0)) return false; 88 if (gb_op_i32(op, 1) == GGML_HINT_SRC0_IS_HADAMARD) return false; 89 if (gv_supported(op)) return true; 90 struct w_view v; w_desc(w, &v); 91 uint64_t d[32], p[16], r = 1; 92 mm_desc(op, &v, d); 93 OK(omega_gbmm(&r, d, 32, 0, p, 16)); 94 return r == 0; 95 } 96 97 // --- MUL_MAT, GEMV route: 2-D weights as resident bf16 planes, f32 x, fp32 FFMA accumulation on the chip --- 98 // A weight W (k a multiple of 256, 256 <= k <= 16384) is held on the chip as bf16 planes: bf16 weights as 99 // themselves (one plane, exact); f32, f16 and q8_0 (dequantised to f32 by the Omega unit dq8r, exact for q8_0) 100 // split by the Omega unit bfs into hi + lo, W - (hi + lo) < 2^-16 |W| per element. Each token row is one 101 // omega_gpu_bf16_gemv_f32 per plane (bf16 widened to f32 times f32 x, fp32 FFMA, CUDApp 102 // omega_blackwell_codegen_bf16.h); the two plane results are summed by the Omega unit bin (op 0). 103 // Planes are uploaded once for buffers llama.cpp marks as weights; any other buffer per call. 104 // k is split into nch chunks of kc = k / nch (kc a multiple of 512, at most 8192): the chip kernel stages x in 105 // 4 * kc bytes of shared memory (launch descriptor limit 32768) and needs kc % (32 * warps) == 0. The chunk 106 // results are summed with the Omega unit bin (op 0). 107 enum { GW_MAXCH = 8 }; 108 struct gw { const void *key; size_t bytes; int planes; int nch; uint32_t kc; OmegaGpuBf16Tensor *t[2][GW_MAXCH]; struct gw *next; }; 109 110 static int gv_chunks(int64_t k) { 111 if (k < 512 || k % 512) return 0; 112 for (int c = 1; c <= GW_MAXCH; c++) if (k % c == 0 && (k / c) % 512 == 0 && k / c <= 8192) return c; 113 return 0; 114 } 115 static struct gw *gw_head; 116 static pthread_mutex_t gw_mu = PTHREAD_MUTEX_INITIALIZER; 117 118 static bool gv_supported(const struct ggml_tensor *op) { 119 const struct ggml_tensor *w = op->src[0], *x = op->src[1]; 120 if (!f32t(op) || !f32t(x) || !dense_rows(x) || !dense_rows(op)) return false; 121 if (w->type != GGML_TYPE_F32 && w->type != GGML_TYPE_F16 && w->type != GGML_TYPE_BF16 && w->type != GGML_TYPE_Q8_0) return false; 122 if (w->ne[2] != 1 || w->ne[3] != 1 || !ggml_is_contiguous(w)) return false; 123 if (gv_chunks(w->ne[0]) == 0 || w->ne[1] > UINT32_MAX) return false; 124 if (gb_op_i32(op, 1) == GGML_HINT_SRC0_IS_HADAMARD) return false; 125 return x->ne[0] == w->ne[0] && op->ne[0] == w->ne[1]; 126 } 127 128 struct split_job { const struct ggml_tensor *w; uint16_t *h, *l; int64_t r0, r1; }; 129 130 static void *split_rows(void *arg) { 131 const struct split_job *j = arg; 132 const struct ggml_tensor *w = j->w; 133 const int64_t k = w->ne[0]; 134 float *f = malloc((size_t) k * sizeof *f); 135 if (!f) GGML_ABORT("ggml-omega-gb10: out of host memory"); 136 uint64_t r; 137 for (int64_t row = j->r0; row < j->r1; row++) { 138 const char *s = (const char *) w->data + row * w->nb[1]; 139 if (w->type == GGML_TYPE_Q8_0) for (int64_t b = 0; b < k / 32; b++) OK(omega_dq8r(&r, s + 34 * b, 34, f + 32 * b, 32)); 140 else if (w->type == GGML_TYPE_F16) for (int64_t i = 0; i < k; i += 32) OK(omega_h2fv_32(&r, s + 2 * i, 32, f + i, 32)); 141 else memcpy(f, s, (size_t) k * sizeof *f); 142 for (int64_t i = 0; i < k; i += 256) OK(omega_bfs_256(&r, f + i, 256, j->h + row * k + i, 256, j->l + row * k + i, 256)); 143 } 144 free(f); 145 return NULL; 146 } 147 148 static void gw_free(struct gw *g) { 149 for (int p = 0; p < 2; p++) for (int c = 0; c < GW_MAXCH; c++) if (g->t[p][c]) omega_gpu_bf16_tensor_free(g->t[p][c]); 150 } 151 152 // Upload one plane (n x k bf16, row-major) as nch chunks of kc columns; false: the chip refused one. 153 static bool up_plane(uint32_t n, uint32_t k, int nch, const uint16_t *src, OmegaGpuBf16Tensor **out) { 154 const uint32_t kc = k / nch; 155 if (nch == 1) return omega_gpu_bf16_tensor_upload(n, k, src, &out[0]) == 0; 156 uint16_t *tmp = malloc((size_t) n * kc * 2); 157 if (!tmp) GGML_ABORT("ggml-omega-gb10: out of host memory"); 158 bool ok = true; 159 for (int c = 0; c < nch && ok; c++) { 160 for (uint32_t r = 0; r < n; r++) memcpy(tmp + (size_t) r * kc, src + (size_t) r * k + (size_t) c * kc, (size_t) kc * 2); 161 ok = omega_gpu_bf16_tensor_upload(n, kc, tmp, &out[c]) == 0; 162 } 163 free(tmp); 164 return ok; 165 } 166 167 // Upload W's planes; false when the chip refuses a plane (a NaN/Inf weight): the caller takes the gemm route. 168 static bool gw_upload(const struct ggml_tensor *w, struct gw *g) { 169 const uint32_t k = (uint32_t) w->ne[0], n = (uint32_t) w->ne[1]; 170 memset(g->t, 0, sizeof g->t); 171 g->key = w->data; g->bytes = ggml_nbytes(w); 172 g->nch = gv_chunks(k); g->kc = k / g->nch; 173 if (w->type == GGML_TYPE_BF16) { 174 g->planes = 1; 175 const bool ok = up_plane(n, k, g->nch, (const uint16_t *) w->data, g->t[0]); 176 if (!ok) gw_free(g); 177 return ok; 178 } 179 g->planes = 2; 180 uint16_t *h = malloc((size_t) n * k * 2), *l = malloc((size_t) n * k * 2); 181 if (!h || !l) GGML_ABORT("ggml-omega-gb10: out of host memory"); 182 enum { NT = 10 }; 183 pthread_t th[NT]; struct split_job jb[NT]; 184 for (int i = 0; i < NT; i++) { 185 jb[i] = (struct split_job) { w, h, l, (int64_t) n * i / NT, (int64_t) n * (i + 1) / NT }; 186 if (pthread_create(&th[i], NULL, split_rows, &jb[i]) != 0) GGML_ABORT("ggml-omega-gb10: pthread_create"); 187 } 188 for (int i = 0; i < NT; i++) pthread_join(th[i], NULL); 189 bool ok = up_plane(n, k, g->nch, h, g->t[0]) && up_plane(n, k, g->nch, l, g->t[1]); 190 free(h); free(l); 191 if (!ok) gw_free(g); 192 return ok; 193 } 194 195 // The resident planes of W, or NULL (refused upload). *owned: per-call planes the caller frees. 196 static struct gw *gw_get(const struct ggml_tensor *w, bool *owned) { 197 const bool cache = w->buffer && w->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS; 198 *owned = !cache; 199 if (cache) { 200 pthread_mutex_lock(&gw_mu); 201 for (struct gw *c = gw_head; c; c = c->next) 202 if (c->key == w->data && c->bytes == ggml_nbytes(w)) { pthread_mutex_unlock(&gw_mu); return c->planes ? c : NULL; } 203 } 204 struct gw *g = malloc(sizeof *g); 205 if (!g) GGML_ABORT("ggml-omega-gb10: out of host memory"); 206 if (!gw_upload(w, g)) g->planes = 0; 207 if (cache) { g->next = gw_head; gw_head = g; pthread_mutex_unlock(&gw_mu); } 208 if (g->planes) return g; 209 if (!cache) free(g); 210 return NULL; 211 } 212 213 static void gemv(const OmegaGpuBf16Tensor *t, const float *x, float *y) { 214 const int rc = omega_gpu_bf16_gemv_f32(t, x, y, NULL); 215 if (rc != 0) GGML_ABORT("ggml-omega-gb10: omega_gpu_bf16_gemv_f32 %s (%d): %s", omega_gpu_bf16_rc_name(rc), rc, omega_gpu_bf16_last_error()); 216 } 217 218 // y = sum over plane and chunk of gemv(t[plane][chunk], x chunk), summed left to right by the Omega unit bin. 219 static void gv_row(const struct gw *g, const float *xr, float *yr, int64_t n, float *buf[3]) { 220 const int parts = g->planes * g->nch; 221 if (parts == 1) { gemv(g->t[0][0], xr, yr); return; } 222 float *acc = buf[0], *nxt = buf[1], *part = buf[2]; 223 uint64_t r; 224 for (int j = 0; j < parts; j++) { 225 const int p = j / g->nch, c = j % g->nch; 226 float *out = j == 0 ? acc : part; 227 gemv(g->t[p][c], xr + (size_t) c * g->kc, out); 228 if (j == 0) continue; 229 float *dst = j == parts - 1 ? yr : nxt; 230 int64_t i = 0; 231 for (; i + 32 <= n; i += 32) OK(omega_bin_32(&r, 0, acc + i, 32, part + i, 32, dst + i, 32)); 232 for (; i < n; i++) OK(omega_bin_1(&r, 0, acc + i, 1, part + i, 1, dst + i, 1)); 233 float *t = acc; acc = nxt; nxt = t; 234 } 235 } 236 237 // false: the planes were refused, run the gemm route instead. 238 static bool op_mul_mat_gv(struct ggml_tensor *dst) { 239 const struct ggml_tensor *w = dst->src[0], *x = dst->src[1]; 240 bool owned; 241 struct gw *g = gw_get(w, &owned); 242 if (!g) return false; 243 const int64_t n = w->ne[1]; 244 float *buf[3] = { NULL, NULL, NULL }; 245 if (g->planes * g->nch > 1) 246 for (int i = 0; i < 3; i++) if (!(buf[i] = malloc((size_t) n * sizeof(float)))) GGML_ABORT("ggml-omega-gb10: out of host memory"); 247 for (int64_t i3 = 0; i3 < x->ne[3]; i3++) 248 for (int64_t i2 = 0; i2 < x->ne[2]; i2++) 249 for (int64_t i1 = 0; i1 < x->ne[1]; i1++) { 250 const float *xr = (const float *) ((const char *) x->data + i1 * x->nb[1] + i2 * x->nb[2] + i3 * x->nb[3]); 251 float *yr = (float *) ((char *) dst->data + i1 * dst->nb[1] + i2 * dst->nb[2] + i3 * dst->nb[3]); 252 gv_row(g, xr, yr, n, buf); 253 } 254 for (int i = 0; i < 3; i++) free(buf[i]); 255 if (owned) { gw_free(g); free(g); } 256 return true; 257 } 258 259 // --- MUL_MAT, gemm route (every other accepted shape): src0 per call, q8_0 dequantised per call --- 260 static float *q8_dequant(const struct ggml_tensor *w) { 261 const int64_t rows = w->ne[1] * w->ne[2] * w->ne[3], blocks = w->ne[0] / 32; 262 float *f = malloc((size_t) (rows * w->ne[0]) * sizeof *f); 263 if (!f) GGML_ABORT("ggml-omega-gb10: out of host memory"); 264 uint64_t r; 265 for (int64_t i3 = 0, row = 0; i3 < w->ne[3]; i3++) 266 for (int64_t i2 = 0; i2 < w->ne[2]; i2++) 267 for (int64_t i1 = 0; i1 < w->ne[1]; i1++, row++) { 268 const char *s = (const char *) w->data + i1 * w->nb[1] + i2 * w->nb[2] + i3 * w->nb[3]; 269 for (int64_t b = 0; b < blocks; b++) OK(omega_dq8r(&r, s + 34 * b, 34, f + row * w->ne[0] + 32 * b, 32)); 270 } 271 return f; 272 } 273 274 static void op_mul_mat(struct ggml_tensor *dst) { 275 if (gv_supported(dst) && op_mul_mat_gv(dst)) return; 276 const struct ggml_tensor *w = dst->src[0], *x = dst->src[1]; 277 struct w_view v; w_desc(w, &v); 278 const bool owned = w->type == GGML_TYPE_Q8_0; 279 if (owned) v.data = (const char *) q8_dequant(w); 280 uint64_t d[32], p[16], r; 281 mm_desc(dst, &v, d); 282 OK(omega_gbmm(&r, d, 32, 0, p, 16)); 283 if (r != 0) GGML_ABORT("ggml-omega-gb10: gbmm refused a MUL_MAT it accepted in supports_op (rule %llu)", (unsigned long long) r); 284 const uint64_t groups = p[13]; 285 for (uint64_t g = 0; g < groups; g++) { 286 OK(omega_gbmm(&r, d, 32, g, p, 16)); 287 if (r != 0) GGML_ABORT("ggml-omega-gb10: gbmm rule %llu at call %llu", (unsigned long long) r, (unsigned long long) g); 288 OmegaGemmExArgs a = { 289 .op_a = OMEGA_GEMM_OP_N, .op_b = OMEGA_GEMM_OP_T, 290 .m = p[0], .n = p[1], .k = p[2], .batch = p[6], 291 .a_type = OMEGA_GEMM_DT_F32, .b_type = v.dt, .c_type = OMEGA_GEMM_DT_F32, 292 .a = (const char *) x->data + p[10], .lda = p[3], .stride_a = p[7], 293 .b = v.data + p[11], .ldb = p[4], .stride_b = p[8], 294 .c = (char *) dst->data + p[12], .ldc = p[5], .stride_c = p[9], 295 .alpha = 1.0f, .beta = 0.0f, .compute = OMEGA_GEMM_EX_DEFAULT, .scale = OMEGA_GEMM_SCALE_NONE, 296 }; 297 const int rc = omega_gpu_gemm_ex(&a); 298 if (rc != 0) GGML_ABORT("ggml-omega-gb10: omega_gpu_gemm_ex %s (%d)", omega_gpu_gemm_rc_name(rc), rc); 299 } 300 if (owned) free((void *) v.data); 301 } 302 303 // --- RMS_NORM and GLU: rows gathered into dense staging buffers, one chip call, rows scattered back --- 304 305 static float *stage_alloc(size_t n) { 306 float *s = malloc((n ? n : 1) * sizeof *s); 307 if (!s) GGML_ABORT("ggml-omega-gb10: out of host memory"); 308 return s; 309 } 310 311 static void gather(const struct ggml_tensor *t, size_t off, int64_t nc, float *s) { 312 for (int64_t i3 = 0, row = 0; i3 < t->ne[3]; i3++) 313 for (int64_t i2 = 0; i2 < t->ne[2]; i2++) 314 for (int64_t i1 = 0; i1 < t->ne[1]; i1++, row++) 315 memcpy(s + row * nc, (const char *) t->data + off + i1 * t->nb[1] + i2 * t->nb[2] + i3 * t->nb[3], nc * 4); 316 } 317 318 static void scatter(struct ggml_tensor *t, const float *s) { 319 const int64_t nc = t->ne[0]; 320 for (int64_t i3 = 0, row = 0; i3 < t->ne[3]; i3++) 321 for (int64_t i2 = 0; i2 < t->ne[2]; i2++) 322 for (int64_t i1 = 0; i1 < t->ne[1]; i1++, row++) 323 memcpy((char *) t->data + i1 * t->nb[1] + i2 * t->nb[2] + i3 * t->nb[3], s + row * nc, nc * 4); 324 } 325 326 static bool rms_supported(const struct ggml_tensor *op) { 327 const struct ggml_tensor *s0 = op->src[0]; 328 return f32t(op) && f32t(s0) && dense_rows(op) && dense_rows(s0) && ggml_are_same_shape(op, s0) && 329 s0->ne[0] % 128 == 0 && s0->ne[0] <= 16384 && ggml_nrows(s0) <= UINT32_MAX; 330 } 331 332 static void op_rms_norm(struct ggml_tensor *dst) { 333 const struct ggml_tensor *s0 = dst->src[0]; 334 const int64_t dim = s0->ne[0], rows = ggml_nrows(s0); 335 float eps; memcpy(&eps, dst->op_params, sizeof eps); 336 float *x = stage_alloc((size_t) (rows * dim)), *y = stage_alloc((size_t) (rows * dim)), *one = stage_alloc((size_t) dim); 337 for (int64_t i = 0; i < dim; i++) one[i] = 1.0f; 338 gather(s0, 0, dim, x); 339 const int rc = omega_gpu_rmsnorm_f32((uint32_t) rows, (uint32_t) dim, x, one, eps, y, NULL); 340 if (rc != 0) GGML_ABORT("ggml-omega-gb10: omega_gpu_rmsnorm_f32 %s (%d)", omega_gpu_elementwise_rc_name(rc), rc); 341 scatter(dst, y); 342 free(x); free(y); free(one); 343 } 344 345 static bool glu_supported(const struct ggml_tensor *op) { 346 const struct ggml_tensor *s0 = op->src[0], *s1 = op->src[1]; 347 return ggml_get_glu_op(op) == GGML_GLU_OP_SWIGLU && f32t(op) && f32t(s0) && (!s1 || f32t(s1)) && 348 dense_rows(op) && dense_rows(s0) && (!s1 || dense_rows(s1)) && 349 (uint64_t) ggml_nelements(op) <= UINT32_MAX; 350 } 351 352 static void op_swiglu(struct ggml_tensor *dst) { 353 const struct ggml_tensor *s0 = dst->src[0], *s1 = dst->src[1]; 354 const int64_t nc = dst->ne[0], n = ggml_nelements(dst); 355 const int swapped = gb_op_i32(dst, 1); 356 float *ga = stage_alloc((size_t) n), *up = stage_alloc((size_t) n), *y = stage_alloc((size_t) n); 357 if (s1) { gather(s0, 0, nc, ga); gather(s1, 0, nc, up); } 358 else { gather(s0, swapped ? 4 * nc : 0, nc, ga); gather(s0, swapped ? 0 : 4 * nc, nc, up); } 359 const int rc = omega_gpu_swiglu_f32((uint32_t) n, ga, up, y, NULL); 360 if (rc != 0) GGML_ABORT("ggml-omega-gb10: omega_gpu_swiglu_f32 %s (%d)", omega_gpu_elementwise_rc_name(rc), rc); 361 scatter(dst, y); 362 free(ga); free(up); free(y); 363 } 364 365 // --- device and backend -------------------------------------------------------------------------------- 366 367 bool omega_gb10_supports_op(const struct ggml_tensor *op) { 368 switch (op->op) { 369 case GGML_OP_NONE: case GGML_OP_RESHAPE: case GGML_OP_VIEW: case GGML_OP_PERMUTE: case GGML_OP_TRANSPOSE: 370 return true; 371 case GGML_OP_MUL_MAT: return mm_supported(op); 372 case GGML_OP_RMS_NORM: return rms_supported(op); 373 case GGML_OP_GLU: return glu_supported(op); 374 default: return false; 375 } 376 } 377 378 static const char *gb10_name(ggml_backend_t b) { (void) b; return OMEGA_GB10_NAME; } 379 static void gb10_free(ggml_backend_t b) { free(b); } 380 381 // OMEGA_GGML_PROFILE=1: wall time per op (ggml_op_desc) inside this device's graph_compute, printed to stderr 382 // at exit. Host clock around each synchronous chip call; for op placement and decode shares, not a receipt. 383 enum { PROF_MAX = 32 }; 384 static struct { const char *op; uint64_t calls, ns; } prof[PROF_MAX]; 385 static int prof_on = -1; 386 static uint64_t now_ns(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (uint64_t) t.tv_sec * 1000000000u + (uint64_t) t.tv_nsec; } 387 static void prof_dump(void) { 388 for (int i = 0; i < PROF_MAX && prof[i].op; i++) 389 fprintf(stderr, "omega-gb10-profile op=%s calls=%llu ms=%.3f\n", prof[i].op, (unsigned long long) prof[i].calls, prof[i].ns / 1e6); 390 } 391 static void prof_add(const char *op, uint64_t ns) { 392 for (int i = 0; i < PROF_MAX; i++) { 393 if (!prof[i].op) prof[i].op = op; 394 if (strcmp(prof[i].op, op) == 0) { prof[i].calls++; prof[i].ns += ns; return; } 395 } 396 } 397 398 static enum ggml_status gb10_graph_compute(ggml_backend_t backend, struct ggml_cgraph *g) { 399 (void) backend; 400 if (prof_on < 0) { const char *e = getenv("OMEGA_GGML_PROFILE"); prof_on = e && *e == '1'; if (prof_on) atexit(prof_dump); } 401 for (int i = 0; i < ggml_graph_n_nodes(g); i++) { 402 struct ggml_tensor *n = ggml_graph_node(g, i); 403 if ((n->flags & GGML_TENSOR_FLAG_COMPUTE) == 0) continue; 404 // Empty graph nodes carry no work, as in ggml's CPU executor. In 405 // particular, never pass a zero-row normalization to the chip API. 406 if (ggml_is_empty(n)) continue; 407 const uint64_t t0 = prof_on ? now_ns() : 0; 408 switch (n->op) { 409 case GGML_OP_NONE: case GGML_OP_RESHAPE: case GGML_OP_VIEW: case GGML_OP_PERMUTE: case GGML_OP_TRANSPOSE: continue; 410 case GGML_OP_MUL_MAT: op_mul_mat(n); break; 411 case GGML_OP_RMS_NORM: op_rms_norm(n); break; 412 case GGML_OP_GLU: op_swiglu(n); break; 413 default: GGML_ABORT("ggml-omega-gb10: op %s reached graph_compute without support", ggml_op_name(n->op)); 414 } 415 if (prof_on) prof_add(ggml_op_desc(n), now_ns() - t0); 416 } 417 return GGML_STATUS_SUCCESS; 418 } 419 420 static const struct ggml_backend_i gb10_backend_i = { 421 .get_name = gb10_name, 422 .free = gb10_free, 423 .graph_compute = gb10_graph_compute, 424 }; 425 426 static ggml_guid_t gb10_guid(void) { 427 static ggml_guid guid = { 0x4f, 0x6d, 0x65, 0x67, 0x61, 0x2d, 0x47, 0x42, 0x31, 0x30, 0x2d, 0x63, 0x75, 0x64, 0x61, 0x70 }; 428 return &guid; 429 } 430 431 ggml_backend_t omega_gb10_init(ggml_backend_dev_t d) { 432 ggml_backend_t b = malloc(sizeof *b); 433 if (!b) return NULL; 434 *b = (struct ggml_backend) { .guid = gb10_guid(), .iface = gb10_backend_i, .device = d, .context = NULL }; 435 return b; 436 }