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.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.c` 571 lines ยท 29933 bytes Raw Blame History 1 // ggml-omega: a ggml backend (llama.cpp backend API v3) whose op arithmetic runs in seeded-Omega units. 2 // 3 // INTERIM HOST GLUE. This C file only registers the backend, decides which ops it takes, walks tensor 4 // strides, slices rows into fixed-length tiles, stages tiles through a scratch buffer and moves bits. 5 // Every arithmetic step, every type conversion (f16, q8_0, int32 positions) and every libm-style 6 // function (exp, log, pow, sin, cos, sqrt) is computed by the Omega units in kernels/*.om, called 7 // through the omega-bridge native ABI (docs/NATIVE-ABI.md). Its .om successor is the Omega native 8 // loadable-object ABI (aien-dev/omega-mv #636 / #637): once Omega can export the ggml backend 9 // tables itself, this file is replaced by a .om unit. 10 // 11 // Device type ACCEL with the CPU buffer type, as ggml-blas: weights stay in CPU buffers (no copies), 12 // llama.cpp places ACCEL backends ahead of CPU, and the scheduler leaves every op this backend does 13 // not claim in supports_op on the CPU backend. 14 15 #include "ggml-backend-impl.h" 16 #include "ggml.h" 17 18 #include 19 #include 20 #include 21 #include 22 #include 23 24 // --- Omega units (generated by omega-bridge src/native.om from kernels/*.om; see build.sh) --------- 25 #define ARR(p) const void *p, uint64_t p##_n 26 #define OUT(p) void *p, uint64_t p##_n 27 uint32_t omega_bin_32(uint64_t *r, uint64_t op, ARR(a), ARR(b), OUT(d)); 28 uint32_t omega_bin_1(uint64_t *r, uint64_t op, ARR(a), ARR(b), OUT(d)); 29 uint32_t omega_sq_32(uint64_t *r, uint64_t acc, ARR(x)); 30 uint32_t omega_sq_1(uint64_t *r, uint64_t acc, ARR(x)); 31 uint32_t omega_rmsc(uint64_t *r, uint64_t acc, uint64_t n, uint64_t eps); 32 uint32_t omega_scl_32(uint64_t *r, ARR(x), OUT(d), uint64_t s, uint64_t b); 33 uint32_t omega_scl_1(uint64_t *r, ARR(x), OUT(d), uint64_t s, uint64_t b); 34 uint32_t omega_smw_32(uint64_t *r, ARR(x), ARR(m), OUT(d), uint64_t sc); 35 uint32_t omega_smw_1(uint64_t *r, ARR(x), ARR(m), OUT(d), uint64_t sc); 36 uint32_t omega_smwh_32(uint64_t *r, ARR(x), ARR(m), OUT(d), uint64_t sc); 37 uint32_t omega_smwh_1(uint64_t *r, ARR(x), ARR(m), OUT(d), uint64_t sc); 38 uint32_t omega_vmax_32(uint64_t *r, uint64_t acc, ARR(x)); 39 uint32_t omega_vmax_1(uint64_t *r, uint64_t acc, ARR(x)); 40 uint32_t omega_sexp_32(uint64_t *r, uint64_t acc, OUT(w), uint64_t mx); 41 uint32_t omega_sexp_1(uint64_t *r, uint64_t acc, OUT(w), uint64_t mx); 42 uint32_t omega_rcp(uint64_t *r, uint64_t acc); 43 uint32_t omega_rtsc(uint64_t *r, uint64_t base, uint64_t nd); 44 uint32_t omega_rope(uint64_t *r, uint64_t xy, uint64_t pos, uint64_t i, uint64_t ts, uint64_t fa); 45 uint32_t omega_df32_256(uint64_t *r, uint64_t acc, ARR(w), ARR(x), uint64_t fin); 46 uint32_t omega_df32_32(uint64_t *r, uint64_t acc, ARR(w), ARR(x), uint64_t fin); 47 uint32_t omega_df32_1(uint64_t *r, uint64_t acc, ARR(w), ARR(x), uint64_t fin); 48 uint32_t omega_dh16_256(uint64_t *r, uint64_t acc, ARR(w), ARR(x), uint64_t fin); 49 uint32_t omega_dh16_32(uint64_t *r, uint64_t acc, ARR(w), ARR(x), uint64_t fin); 50 uint32_t omega_dh16_1(uint64_t *r, uint64_t acc, ARR(w), ARR(x), uint64_t fin); 51 uint32_t omega_dq8_8(uint64_t *r, uint64_t acc, ARR(w), ARR(x), uint64_t fin); 52 uint32_t omega_dq8_1(uint64_t *r, uint64_t acc, ARR(w), ARR(x), uint64_t fin); 53 uint32_t omega_dq8r(uint64_t *r, ARR(w), OUT(y)); 54 uint32_t omega_h2fv_32(uint64_t *r, ARR(s), OUT(d)); 55 uint32_t omega_h2fv_1(uint64_t *r, ARR(s), OUT(d)); 56 uint32_t omega_f2hv_32(uint64_t *r, ARR(s), OUT(d)); 57 uint32_t omega_f2hv_1(uint64_t *r, ARR(s), OUT(d)); 58 uint32_t omega_cpy_32(uint64_t *r, ARR(s), OUT(d)); 59 uint32_t omega_cpy_1(uint64_t *r, ARR(s), OUT(d)); 60 uint32_t omega_cpyh_32(uint64_t *r, ARR(s), OUT(d)); 61 uint32_t omega_cpyh_1(uint64_t *r, ARR(s), OUT(d)); 62 uint32_t omega_swi_32(uint64_t *r, ARR(a), ARR(b), OUT(d)); 63 uint32_t omega_swi_1(uint64_t *r, ARR(a), ARR(b), OUT(d)); 64 uint32_t omega_silu_32(uint64_t *r, ARR(a), OUT(d)); 65 uint32_t omega_silu_1(uint64_t *r, ARR(a), OUT(d)); 66 67 // A non-zero status (trap, refused argument, runtime failure) is never ignored. 68 #define OK(call) \ 69 do { \ 70 uint32_t st_ = (call); \ 71 if (st_ != 0) GGML_ABORT("ggml-omega: %s returned Omega status %u", #call, st_); \ 72 } while (0) 73 74 #define TILE 32 75 76 static int32_t ggml_get_op_params_i32(const struct ggml_tensor *t, uint32_t i) { int32_t v; memcpy(&v, (const char *) t->op_params + 4 * i, 4); return v; } 77 static float ggml_get_op_params_f32(const struct ggml_tensor *t, uint32_t i) { float v; memcpy(&v, (const char *) t->op_params + 4 * i, 4); return v; } 78 79 static uint64_t bits_f32(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 80 static float f32_bits(uint64_t v) { uint32_t u = (uint32_t) v; float f; memcpy(&f, &u, 4); return f; } 81 82 static char *at(const struct ggml_tensor *t, int64_t i1, int64_t i2, int64_t i3) { 83 return (char *) t->data + i1 * t->nb[1] + i2 * t->nb[2] + i3 * t->nb[3]; 84 } 85 86 // --- row kernels: tiles of 32 lanes, then single lanes; outputs staged through t[] ------------- 87 88 static void row_bin(uint64_t op, const float *a, const float *b, float *d, int64_t n) { 89 float t[TILE]; uint64_t r; int64_t i = 0; 90 for (; i + TILE <= n; i += TILE) { OK(omega_bin_32(&r, op, a + i, TILE, b + i, TILE, t, TILE)); memcpy(d + i, t, sizeof t); } 91 for (; i < n; i++) { OK(omega_bin_1(&r, op, a + i, 1, b + i, 1, t, 1)); d[i] = t[0]; } 92 } 93 94 static void row_scl(const float *x, float *d, int64_t n, uint64_t s, uint64_t b) { 95 float t[TILE]; uint64_t r; int64_t i = 0; 96 for (; i + TILE <= n; i += TILE) { OK(omega_scl_32(&r, x + i, TILE, t, TILE, s, b)); memcpy(d + i, t, sizeof t); } 97 for (; i < n; i++) { OK(omega_scl_1(&r, x + i, 1, t, 1, s, b)); d[i] = t[0]; } 98 } 99 100 // one row of src (f32, f16 or q8_0) to f32 or f16 at d 101 static void row_cvt(enum ggml_type st, const void *s, enum ggml_type dt, void *d, int64_t n) { 102 uint64_t r; int64_t i = 0; 103 if (st == GGML_TYPE_Q8_0) { 104 float t[32]; 105 for (; i < n / 32; i++) { OK(omega_dq8r(&r, (const char *) s + 34 * i, 34, t, 32)); memcpy((float *) d + 32 * i, t, sizeof t); } 106 return; 107 } 108 if (dt == GGML_TYPE_F32) { 109 float t[TILE]; float *df = d; 110 if (st == GGML_TYPE_F32) { 111 const float *sf = s; 112 for (; i + TILE <= n; i += TILE) { OK(omega_cpy_32(&r, sf + i, TILE, t, TILE)); memcpy(df + i, t, sizeof t); } 113 for (; i < n; i++) { OK(omega_cpy_1(&r, sf + i, 1, t, 1)); df[i] = t[0]; } 114 } else { 115 const uint16_t *sh = s; 116 for (; i + TILE <= n; i += TILE) { OK(omega_h2fv_32(&r, sh + i, TILE, t, TILE)); memcpy(df + i, t, sizeof t); } 117 for (; i < n; i++) { OK(omega_h2fv_1(&r, sh + i, 1, t, 1)); df[i] = t[0]; } 118 } 119 } else { 120 uint16_t t[TILE]; uint16_t *dh = d; 121 if (st == GGML_TYPE_F32) { 122 const float *sf = s; 123 for (; i + TILE <= n; i += TILE) { OK(omega_f2hv_32(&r, sf + i, TILE, t, TILE)); memcpy(dh + i, t, sizeof t); } 124 for (; i < n; i++) { OK(omega_f2hv_1(&r, sf + i, 1, t, 1)); dh[i] = t[0]; } 125 } else { 126 const uint16_t *sh = s; 127 for (; i + TILE <= n; i += TILE) { OK(omega_cpyh_32(&r, sh + i, TILE, t, TILE)); memcpy(dh + i, t, sizeof t); } 128 for (; i < n; i++) { OK(omega_cpyh_1(&r, sh + i, 1, t, 1)); dh[i] = t[0]; } 129 } 130 } 131 } 132 133 // dot of one weight row (f32, f16 or q8_0) with one f32 row of k elements 134 static float row_dot(enum ggml_type wt, const void *w, const float *x, int64_t k) { 135 uint64_t acc = 0; int64_t i = 0; 136 if (wt == GGML_TYPE_Q8_0) { 137 const char *wb = w; const int64_t nb = k / 32; 138 while (i < nb) { 139 if (nb - i >= 8) { OK(omega_dq8_8(&acc, acc, wb + 34 * i, 272, x + 32 * i, 256, nb - i == 8)); i += 8; } 140 else { OK(omega_dq8_1(&acc, acc, wb + 34 * i, 34, x + 32 * i, 32, nb - i == 1)); i += 1; } 141 } 142 } else if (wt == GGML_TYPE_F16) { 143 const uint16_t *wh = w; 144 while (i < k) { 145 if (k - i >= 256) { OK(omega_dh16_256(&acc, acc, wh + i, 256, x + i, 256, k - i == 256)); i += 256; } 146 else if (k - i >= 32) { OK(omega_dh16_32(&acc, acc, wh + i, 32, x + i, 32, k - i == 32)); i += 32; } 147 else { OK(omega_dh16_1(&acc, acc, wh + i, 1, x + i, 1, k - i == 1)); i += 1; } 148 } 149 } else { 150 const float *wf = w; 151 while (i < k) { 152 if (k - i >= 256) { OK(omega_df32_256(&acc, acc, wf + i, 256, x + i, 256, k - i == 256)); i += 256; } 153 else if (k - i >= 32) { OK(omega_df32_32(&acc, acc, wf + i, 32, x + i, 32, k - i == 32)); i += 32; } 154 else { OK(omega_df32_1(&acc, acc, wf + i, 1, x + i, 1, k - i == 1)); i += 1; } 155 } 156 } 157 return f32_bits(acc); 158 } 159 160 // --- ops ------------------------------------------------------------------------------------------ 161 162 static void op_bin(struct ggml_tensor *dst, uint64_t code) { 163 const struct ggml_tensor *a = dst->src[0], *b = dst->src[1]; 164 for (int64_t i3 = 0; i3 < dst->ne[3]; i3++) 165 for (int64_t i2 = 0; i2 < dst->ne[2]; i2++) 166 for (int64_t i1 = 0; i1 < dst->ne[1]; i1++) 167 row_bin(code, (const float *) at(a, i1, i2, i3), 168 (const float *) at(b, i1 % b->ne[1], i2 % b->ne[2], i3 % b->ne[3]), 169 (float *) at(dst, i1, i2, i3), dst->ne[0]); 170 } 171 172 static void op_rms_norm(struct ggml_tensor *dst) { 173 const struct ggml_tensor *x = dst->src[0]; 174 const uint64_t eps = bits_f32(ggml_get_op_params_f32(dst, 0)); 175 const int64_t n = x->ne[0]; 176 for (int64_t i3 = 0; i3 < x->ne[3]; i3++) 177 for (int64_t i2 = 0; i2 < x->ne[2]; i2++) 178 for (int64_t i1 = 0; i1 < x->ne[1]; i1++) { 179 const float *xr = (const float *) at(x, i1, i2, i3); 180 uint64_t acc = 0, sc; int64_t i = 0; 181 for (; i + TILE <= n; i += TILE) OK(omega_sq_32(&acc, acc, xr + i, TILE)); 182 for (; i < n; i++) OK(omega_sq_1(&acc, acc, xr + i, 1)); 183 OK(omega_rmsc(&sc, acc, (uint64_t) n, eps)); 184 row_scl(xr, (float *) at(dst, i1, i2, i3), n, sc, 0); 185 } 186 } 187 188 static void op_scale(struct ggml_tensor *dst) { 189 const struct ggml_tensor *x = dst->src[0]; 190 const uint64_t s = bits_f32(ggml_get_op_params_f32(dst, 0)), b = bits_f32(ggml_get_op_params_f32(dst, 1)); 191 for (int64_t i3 = 0; i3 < x->ne[3]; i3++) 192 for (int64_t i2 = 0; i2 < x->ne[2]; i2++) 193 for (int64_t i1 = 0; i1 < x->ne[1]; i1++) 194 row_scl((const float *) at(x, i1, i2, i3), (float *) at(dst, i1, i2, i3), x->ne[0], s, b); 195 } 196 197 static void op_soft_max(struct ggml_tensor *dst) { 198 const struct ggml_tensor *x = dst->src[0], *m = dst->src[1]; 199 const uint64_t sc = bits_f32(ggml_get_op_params_f32(dst, 0)); 200 const int64_t n = x->ne[0]; 201 float t[TILE]; uint64_t r; 202 for (int64_t i3 = 0; i3 < x->ne[3]; i3++) 203 for (int64_t i2 = 0; i2 < x->ne[2]; i2++) 204 for (int64_t i1 = 0; i1 < x->ne[1]; i1++) { 205 const float *xr = (const float *) at(x, i1, i2, i3); 206 float *dr = (float *) at(dst, i1, i2, i3); 207 int64_t i = 0; 208 if (!m) { 209 row_scl(xr, dr, n, sc, 0); 210 } else { 211 const char *mr = at(m, i1, i2 % m->ne[2], i3 % m->ne[3]); 212 if (m->type == GGML_TYPE_F32) { 213 const float *mf = (const float *) mr; 214 for (; i + TILE <= n; i += TILE) { OK(omega_smw_32(&r, xr + i, TILE, mf + i, TILE, t, TILE, sc)); memcpy(dr + i, t, sizeof t); } 215 for (; i < n; i++) { OK(omega_smw_1(&r, xr + i, 1, mf + i, 1, t, 1, sc)); dr[i] = t[0]; } 216 } else { 217 const uint16_t *mh = (const uint16_t *) mr; 218 for (; i + TILE <= n; i += TILE) { OK(omega_smwh_32(&r, xr + i, TILE, mh + i, TILE, t, TILE, sc)); memcpy(dr + i, t, sizeof t); } 219 for (; i < n; i++) { OK(omega_smwh_1(&r, xr + i, 1, mh + i, 1, t, 1, sc)); dr[i] = t[0]; } 220 } 221 } 222 uint64_t mx = bits_f32(-INFINITY), sum = 0, rs; 223 for (i = 0; i + TILE <= n; i += TILE) OK(omega_vmax_32(&mx, mx, dr + i, TILE)); 224 for (; i < n; i++) OK(omega_vmax_1(&mx, mx, dr + i, 1)); 225 for (i = 0; i + TILE <= n; i += TILE) OK(omega_sexp_32(&sum, sum, dr + i, TILE, mx)); 226 for (; i < n; i++) OK(omega_sexp_1(&sum, sum, dr + i, 1, mx)); 227 OK(omega_rcp(&rs, sum)); 228 row_scl(dr, dr, n, rs, 0); 229 } 230 } 231 232 static void op_rope(struct ggml_tensor *dst) { 233 const struct ggml_tensor *x = dst->src[0], *p = dst->src[1]; 234 const int n_dims = ggml_get_op_params_i32(dst, 1), mode = ggml_get_op_params_i32(dst, 2); 235 const int n_offs = ggml_get_op_params_i32(dst, 15); 236 const uint64_t fs = bits_f32(ggml_get_op_params_f32(dst, 6)), af = bits_f32(ggml_get_op_params_f32(dst, 8)); 237 const uint64_t fa = fs | (af << 32); 238 uint64_t ts; 239 OK(omega_rtsc(&ts, bits_f32(ggml_get_op_params_f32(dst, 5)), (uint64_t) n_dims)); 240 const int64_t half = n_dims / 2, step = mode == GGML_ROPE_TYPE_NEOX ? half : 1; 241 const int32_t *pos = (const int32_t *) p->data; 242 for (int64_t i3 = 0; i3 < x->ne[3]; i3++) 243 for (int64_t i2 = 0; i2 < x->ne[2]; i2++) 244 for (int64_t i1 = 0; i1 < x->ne[1]; i1++) { 245 const float *s = (const float *) at(x, i1, i2, i3); 246 float *d = (float *) at(dst, i1, i2, i3); 247 float tail[TILE]; 248 for (int64_t k = 0; k < half; k++) { 249 const int64_t c = n_offs + (mode == GGML_ROPE_TYPE_NEOX ? k : 2 * k); 250 const uint64_t xy = bits_f32(s[c]) | (bits_f32(s[c + step]) << 32); 251 uint64_t o; 252 OK(omega_rope(&o, xy, (uint32_t) pos[i2], (uint64_t) k, ts, fa)); 253 const float o0 = f32_bits(o), o1 = f32_bits(o >> 32); 254 d[c] = o0; d[c + step] = o1; 255 } 256 // channels outside [n_offs, n_offs + n_dims) are copied (Omega cpy tiles) 257 for (int64_t i0 = 0; i0 < x->ne[0];) { 258 if (i0 == n_offs) { i0 += n_dims; continue; } 259 const int64_t end = i0 < n_offs ? n_offs : x->ne[0]; 260 for (; i0 < end; i0++) { uint64_t r; OK(omega_cpy_1(&r, s + i0, 1, tail, 1)); d[i0] = tail[0]; } 261 } 262 } 263 } 264 265 static void op_get_rows(struct ggml_tensor *dst) { 266 const struct ggml_tensor *w = dst->src[0], *ix = dst->src[1]; 267 for (int64_t i12 = 0; i12 < ix->ne[2]; i12++) 268 for (int64_t i11 = 0; i11 < ix->ne[1]; i11++) 269 for (int64_t i10 = 0; i10 < ix->ne[0]; i10++) { 270 const int64_t r = *(const int32_t *) ((const char *) ix->data + i10 * ix->nb[0] + i11 * ix->nb[1] + i12 * ix->nb[2]); 271 GGML_ASSERT(r >= 0 && r < w->ne[1]); 272 row_cvt(w->type, at(w, r, i11, i12), dst->type, at(dst, i10, i11, i12), w->ne[0]); 273 } 274 } 275 276 static void op_set_rows(struct ggml_tensor *dst) { 277 const struct ggml_tensor *s = dst->src[0], *ix = dst->src[1]; 278 for (int64_t i03 = 0; i03 < s->ne[3]; i03++) 279 for (int64_t i02 = 0; i02 < s->ne[2]; i02++) 280 for (int64_t i = 0; i < s->ne[1]; i++) { 281 const char *ip = (const char *) ix->data + i * ix->nb[0] + (i02 % ix->ne[1]) * ix->nb[1] + (i03 % ix->ne[2]) * ix->nb[2]; 282 const int64_t r = ix->type == GGML_TYPE_I64 ? *(const int64_t *) ip : *(const int32_t *) ip; 283 GGML_ASSERT(r >= 0 && r < dst->ne[1]); 284 row_cvt(s->type, at(s, i, i02, i03), dst->type, at(dst, r, i02, i03), s->ne[0]); 285 } 286 } 287 288 static void op_cpy(struct ggml_tensor *dst) { 289 const struct ggml_tensor *s = dst->src[0]; 290 if (ggml_are_same_shape(s, dst)) { 291 for (int64_t i3 = 0; i3 < s->ne[3]; i3++) 292 for (int64_t i2 = 0; i2 < s->ne[2]; i2++) 293 for (int64_t i1 = 0; i1 < s->ne[1]; i1++) 294 row_cvt(s->type, at(s, i1, i2, i3), dst->type, at(dst, i1, i2, i3), s->ne[0]); 295 } else { // both contiguous (checked in supports_op): one flat row 296 row_cvt(s->type, s->data, dst->type, dst->data, ggml_nelements(s)); 297 } 298 } 299 300 static void op_swiglu(struct ggml_tensor *dst) { 301 const struct ggml_tensor *a = dst->src[0], *b = dst->src[1]; 302 const int64_t nc = dst->ne[0]; 303 const int swapped = ggml_get_op_params_i32(dst, 1); 304 float t[TILE]; uint64_t r; 305 for (int64_t i3 = 0; i3 < dst->ne[3]; i3++) 306 for (int64_t i2 = 0; i2 < dst->ne[2]; i2++) 307 for (int64_t i1 = 0; i1 < dst->ne[1]; i1++) { 308 const float *ap = (const float *) at(a, i1, i2, i3); 309 const float *bp = b ? (const float *) at(b, i1, i2, i3) : ap; 310 if (!b) { ap += swapped ? nc : 0; bp += swapped ? 0 : nc; } 311 float *d = (float *) at(dst, i1, i2, i3); 312 int64_t i = 0; 313 for (; i + TILE <= nc; i += TILE) { OK(omega_swi_32(&r, ap + i, TILE, bp + i, TILE, t, TILE)); memcpy(d + i, t, sizeof t); } 314 for (; i < nc; i++) { OK(omega_swi_1(&r, ap + i, 1, bp + i, 1, t, 1)); d[i] = t[0]; } 315 } 316 } 317 318 static void op_silu(struct ggml_tensor *dst) { 319 const struct ggml_tensor *a = dst->src[0]; 320 float t[TILE]; uint64_t r; 321 for (int64_t i3 = 0; i3 < dst->ne[3]; i3++) 322 for (int64_t i2 = 0; i2 < dst->ne[2]; i2++) 323 for (int64_t i1 = 0; i1 < dst->ne[1]; i1++) { 324 const float *ap = (const float *) at(a, i1, i2, i3); 325 float *d = (float *) at(dst, i1, i2, i3); 326 int64_t i = 0; 327 for (; i + TILE <= dst->ne[0]; i += TILE) { OK(omega_silu_32(&r, ap + i, TILE, t, TILE)); memcpy(d + i, t, sizeof t); } 328 for (; i < dst->ne[0]; i++) { OK(omega_silu_1(&r, ap + i, 1, t, 1)); d[i] = t[0]; } 329 } 330 } 331 332 // MUL_MAT: dst[i01, col] = dot(src0 row i01, src1 col). Weight rows are split across threads. 333 struct mm_job { struct ggml_tensor *dst; int64_t r0, r1; }; 334 335 static void *mm_run(void *arg) { 336 const struct mm_job *j = arg; 337 struct ggml_tensor *dst = j->dst; 338 const struct ggml_tensor *w = dst->src[0], *x = dst->src[1]; 339 const int64_t r2 = x->ne[2] / w->ne[2], r3 = x->ne[3] / w->ne[3]; 340 for (int64_t i13 = 0; i13 < x->ne[3]; i13++) 341 for (int64_t i12 = 0; i12 < x->ne[2]; i12++) 342 for (int64_t i11 = 0; i11 < x->ne[1]; i11++) { 343 const float *xc = (const float *) at(x, i11, i12, i13); 344 char *dc = at(dst, i11, i12, i13); 345 for (int64_t i01 = j->r0; i01 < j->r1; i01++) { 346 const float v = row_dot(w->type, at(w, i01, i12 / r2, i13 / r3), xc, w->ne[0]); 347 memcpy(dc + i01 * dst->nb[0], &v, 4); 348 } 349 } 350 return NULL; 351 } 352 353 static void op_mul_mat(struct ggml_tensor *dst, int n_threads) { 354 const int64_t rows = dst->src[0]->ne[1]; 355 int nt = n_threads < 1 ? 1 : n_threads > 64 ? 64 : n_threads; 356 if (rows < 2 * nt) nt = 1; 357 struct mm_job jobs[64]; pthread_t th[64]; 358 for (int t = 0; t < nt; t++) jobs[t] = (struct mm_job) { dst, rows * t / nt, rows * (t + 1) / nt }; 359 for (int t = 1; t < nt; t++) if (pthread_create(&th[t], NULL, mm_run, &jobs[t]) != 0) GGML_ABORT("ggml-omega: pthread_create"); 360 mm_run(&jobs[0]); 361 for (int t = 1; t < nt; t++) pthread_join(th[t], NULL); 362 } 363 364 // --- backend ---------------------------------------------------------------------------------------- 365 366 struct omega_ctx { int n_threads; }; 367 368 static const char *omega_name(ggml_backend_t b) { (void) b; return "Omega"; } 369 static void omega_free(ggml_backend_t b) { free(b->context); free(b); } 370 371 static enum ggml_status omega_graph_compute(ggml_backend_t backend, struct ggml_cgraph *g) { 372 const struct omega_ctx *ctx = backend->context; 373 for (int i = 0; i < ggml_graph_n_nodes(g); i++) { 374 struct ggml_tensor *n = ggml_graph_node(g, i); 375 if ((n->flags & GGML_TENSOR_FLAG_COMPUTE) == 0) continue; 376 switch (n->op) { 377 case GGML_OP_NONE: case GGML_OP_RESHAPE: case GGML_OP_VIEW: case GGML_OP_PERMUTE: case GGML_OP_TRANSPOSE: break; 378 case GGML_OP_ADD: op_bin(n, 0); break; 379 case GGML_OP_SUB: op_bin(n, 1); break; 380 case GGML_OP_MUL: op_bin(n, 2); break; 381 case GGML_OP_DIV: op_bin(n, 3); break; 382 case GGML_OP_RMS_NORM: op_rms_norm(n); break; 383 case GGML_OP_SCALE: op_scale(n); break; 384 case GGML_OP_SOFT_MAX: op_soft_max(n); break; 385 case GGML_OP_ROPE: op_rope(n); break; 386 case GGML_OP_GET_ROWS: op_get_rows(n); break; 387 case GGML_OP_SET_ROWS: op_set_rows(n); break; 388 case GGML_OP_CPY: case GGML_OP_CONT: case GGML_OP_DUP: op_cpy(n); break; 389 case GGML_OP_GLU: op_swiglu(n); break; 390 case GGML_OP_UNARY: op_silu(n); break; 391 case GGML_OP_MUL_MAT: op_mul_mat(n, ctx->n_threads); break; 392 default: GGML_ABORT("ggml-omega: op %s reached graph_compute without support", ggml_op_name(n->op)); 393 } 394 } 395 return GGML_STATUS_SUCCESS; 396 } 397 398 static const struct ggml_backend_i omega_backend_i = { 399 .get_name = omega_name, 400 .free = omega_free, 401 .graph_compute = omega_graph_compute, 402 }; 403 404 static ggml_guid_t omega_guid(void) { 405 static ggml_guid guid = { 0x4f, 0x6d, 0x65, 0x67, 0x61, 0x2d, 0x67, 0x67, 0x6d, 0x6c, 0x2d, 0x62, 0x72, 0x69, 0x64, 0x67 }; 406 return &guid; 407 } 408 409 static void omega_set_n_threads(ggml_backend_t b, int n) { 410 GGML_ASSERT(b); 411 if (!ggml_guid_matches(b->guid, omega_guid())) return; // the GB10 backend has no host thread pool 412 ((struct omega_ctx *) b->context)->n_threads = n; 413 } 414 415 static ggml_backend_reg_t omega_reg(void); 416 417 // Device context: NULL is the host device "Omega"; &gb10_tag is the GB10 device (OMEGA_GGML_GB10 builds). 418 static int gb10_tag; 419 static bool is_gb10(ggml_backend_dev_t d) { return d->context == &gb10_tag; } 420 421 #ifdef OMEGA_GGML_GB10 422 #include "ggml-omega-gb10.h" 423 #endif 424 425 static const char *dev_name(ggml_backend_dev_t d) { 426 #ifdef OMEGA_GGML_GB10 427 if (is_gb10(d)) return OMEGA_GB10_NAME; 428 #endif 429 (void) d; return "Omega"; 430 } 431 static const char *dev_desc(ggml_backend_dev_t d) { 432 return is_gb10(d) ? "NVIDIA GB10 via CUDA++ (aien-dev/CUDApp), Omega-planned" : "seeded-Omega kernels via omega-bridge (host)"; 433 } 434 static void dev_memory(ggml_backend_dev_t d, size_t *free_, size_t *total) { (void) d; *free_ = 0; *total = 0; } 435 static enum ggml_backend_dev_type dev_type(ggml_backend_dev_t d) { (void) d; return GGML_BACKEND_DEVICE_TYPE_ACCEL; } 436 437 static void dev_props(ggml_backend_dev_t d, struct ggml_backend_dev_props *p) { 438 p->name = dev_name(d); p->description = dev_desc(d); p->type = dev_type(d); p->device_id = NULL; 439 dev_memory(d, &p->memory_free, &p->memory_total); 440 p->caps = (struct ggml_backend_dev_caps) { .async = false, .host_buffer = false, .buffer_from_host_ptr = true, .events = false, .mmap_support = true }; 441 } 442 443 static ggml_backend_t dev_init(ggml_backend_dev_t d, const char *params) { 444 (void) params; 445 #ifdef OMEGA_GGML_GB10 446 if (is_gb10(d)) return omega_gb10_init(d); 447 #endif 448 struct omega_ctx *ctx = malloc(sizeof *ctx); 449 ggml_backend_t b = malloc(sizeof *b); 450 if (!ctx || !b) { free(ctx); free(b); return NULL; } 451 ctx->n_threads = 1; 452 *b = (struct ggml_backend) { .guid = omega_guid(), .iface = omega_backend_i, .device = d, .context = ctx }; 453 return b; 454 } 455 456 static ggml_backend_buffer_type_t dev_buft(ggml_backend_dev_t d) { (void) d; return ggml_backend_cpu_buffer_type(); } 457 static ggml_backend_buffer_t dev_from_ptr(ggml_backend_dev_t d, void *ptr, size_t size, size_t max) { 458 (void) d; (void) max; return ggml_backend_cpu_buffer_from_ptr(ptr, size); 459 } 460 static bool dev_supports_buft(ggml_backend_dev_t d, ggml_backend_buffer_type_t b) { (void) d; return ggml_backend_buft_is_host(b); } 461 462 static bool f32(const struct ggml_tensor *t) { return t && t->type == GGML_TYPE_F32; } 463 static bool rows1(const struct ggml_tensor *t) { return t->nb[0] == ggml_type_size(t->type); } // contiguous within a row 464 static bool fh(const struct ggml_tensor *t) { return t->type == GGML_TYPE_F32 || t->type == GGML_TYPE_F16; } 465 466 static bool dev_supports_op(ggml_backend_dev_t d, const struct ggml_tensor *op) { 467 #ifdef OMEGA_GGML_GB10 468 if (is_gb10(d)) return omega_gb10_supports_op(op); 469 #endif 470 (void) d; 471 const struct ggml_tensor *s0 = op->src[0], *s1 = op->src[1]; 472 switch (op->op) { 473 case GGML_OP_NONE: case GGML_OP_RESHAPE: case GGML_OP_VIEW: case GGML_OP_PERMUTE: case GGML_OP_TRANSPOSE: 474 return true; 475 case GGML_OP_ADD: case GGML_OP_SUB: case GGML_OP_MUL: case GGML_OP_DIV: 476 return f32(op) && f32(s0) && f32(s1) && rows1(op) && rows1(s0) && rows1(s1) && 477 ggml_are_same_shape(op, s0) && s1->ne[0] == s0->ne[0] && ggml_can_repeat(s1, s0); 478 case GGML_OP_RMS_NORM: case GGML_OP_SCALE: 479 return f32(op) && f32(s0) && rows1(op) && rows1(s0); 480 case GGML_OP_SOFT_MAX: 481 return f32(op) && f32(s0) && rows1(op) && rows1(s0) && op->src[2] == NULL && 482 ggml_get_op_params_f32(op, 1) == 0.0f && 483 (!s1 || ((s1->type == GGML_TYPE_F32 || s1->type == GGML_TYPE_F16) && rows1(s1) && s1->ne[0] == s0->ne[0] && 484 s1->ne[1] >= s0->ne[1] && s0->ne[2] % s1->ne[2] == 0 && s0->ne[3] % s1->ne[3] == 0)); 485 case GGML_OP_ROPE: { 486 const int mode = ggml_get_op_params_i32(op, 2); 487 return f32(op) && f32(s0) && rows1(op) && rows1(s0) && s1->type == GGML_TYPE_I32 && op->src[2] == NULL && 488 (mode == GGML_ROPE_TYPE_NORMAL || mode == GGML_ROPE_TYPE_NEOX) && 489 ggml_get_op_params_f32(op, 7) == 0.0f && ggml_get_op_params_i32(op, 1) % 2 == 0; 490 } 491 case GGML_OP_GET_ROWS: 492 return f32(op) && rows1(op) && s1->type == GGML_TYPE_I32 && 493 (s0->type == GGML_TYPE_F32 || s0->type == GGML_TYPE_F16 || s0->type == GGML_TYPE_Q8_0) && rows1(s0); 494 case GGML_OP_SET_ROWS: 495 return f32(s0) && fh(op) && rows1(op) && rows1(s0) && (s1->type == GGML_TYPE_I64 || s1->type == GGML_TYPE_I32); 496 case GGML_OP_CPY: case GGML_OP_CONT: case GGML_OP_DUP: { 497 const struct ggml_tensor *dst = op; 498 return fh(s0) && fh(dst) && rows1(s0) && rows1(dst) && 499 (ggml_are_same_shape(s0, dst) || (ggml_is_contiguous(s0) && ggml_is_contiguous(dst))); 500 } 501 case GGML_OP_GLU: 502 return ggml_get_glu_op(op) == GGML_GLU_OP_SWIGLU && f32(op) && f32(s0) && (!s1 || f32(s1)) && 503 ggml_is_contiguous_1(s0) && ggml_is_contiguous_1(op) && (!s1 || ggml_is_contiguous_1(s1)); 504 case GGML_OP_UNARY: 505 return ggml_get_unary_op(op) == GGML_UNARY_OP_SILU && f32(op) && f32(s0) && rows1(op) && rows1(s0); 506 case GGML_OP_MUL_MAT: 507 return f32(op) && f32(s1) && rows1(op) && rows1(s1) && rows1(s0) && 508 (s0->type == GGML_TYPE_F32 || s0->type == GGML_TYPE_F16 || s0->type == GGML_TYPE_Q8_0) && 509 s1->ne[2] % s0->ne[2] == 0 && s1->ne[3] % s0->ne[3] == 0 && 510 ggml_get_op_params_i32(op, 1) != GGML_HINT_SRC0_IS_HADAMARD; 511 default: 512 return false; 513 } 514 } 515 516 static const struct ggml_backend_device_i omega_dev_i = { 517 .get_name = dev_name, 518 .get_description = dev_desc, 519 .get_memory = dev_memory, 520 .get_type = dev_type, 521 .get_props = dev_props, 522 .init_backend = dev_init, 523 .get_buffer_type = dev_buft, 524 .buffer_from_host_ptr = dev_from_ptr, 525 .supports_op = dev_supports_op, 526 .supports_buft = dev_supports_buft, 527 }; 528 529 // OMEGA_GGML_DEVICES picks the devices: unset or "host" the host device only, "gb10" the GB10 device only, 530 // "host,gb10" both (GB10 needs a build with OMEGA_GGML_GB10=1; otherwise "gb10" registers nothing). 531 static struct ggml_backend_device omega_devs[2]; 532 static size_t omega_ndev; 533 static void devs_init(ggml_backend_reg_t r) { 534 static bool done; 535 if (done) return; 536 done = true; 537 const char *e = getenv("OMEGA_GGML_DEVICES"); 538 const bool host = !e || strstr(e, "host") != NULL; 539 #ifdef OMEGA_GGML_GB10 540 const bool gb10 = e && strstr(e, "gb10") != NULL; 541 #else 542 const bool gb10 = false; 543 #endif 544 if (host) omega_devs[omega_ndev++] = (struct ggml_backend_device) { .iface = omega_dev_i, .reg = r, .context = NULL }; 545 if (gb10) omega_devs[omega_ndev++] = (struct ggml_backend_device) { .iface = omega_dev_i, .reg = r, .context = &gb10_tag }; 546 } 547 static const char *reg_name(ggml_backend_reg_t r) { (void) r; return "Omega"; } 548 static size_t reg_count(ggml_backend_reg_t r) { devs_init(r); return omega_ndev; } 549 static ggml_backend_dev_t reg_dev(ggml_backend_reg_t r, size_t i) { 550 devs_init(r); 551 GGML_ASSERT(i < omega_ndev); 552 return &omega_devs[i]; 553 } 554 static void *reg_proc(ggml_backend_reg_t r, const char *name) { 555 (void) r; 556 if (strcmp(name, "ggml_backend_set_n_threads") == 0) return (void *) omega_set_n_threads; 557 return NULL; 558 } 559 560 static const struct ggml_backend_reg_i omega_reg_i = { 561 .get_name = reg_name, .get_device_count = reg_count, .get_device = reg_dev, .get_proc_address = reg_proc, 562 }; 563 564 static ggml_backend_reg_t omega_reg(void) { 565 static struct ggml_backend_reg reg = { .api_version = GGML_BACKEND_API_VERSION, .iface = omega_reg_i, .context = NULL }; 566 return ® 567 } 568 569 // dynamic-load entry point read by ggml_backend_load (GGML_BACKEND_PATH) 570 __attribute__((visibility("default"))) ggml_backend_reg_t ggml_backend_init(void); 571 ggml_backend_reg_t ggml_backend_init(void) { return omega_reg(); }