interplane.Get Omega
Read in your language

omega-blas

aien-dev/omega-blas

Brought by drakestapleton

branch main Branches Tags History

omega-blas / src / omegablas.c

579 lines · 25618 bytes Raw Blame History
1/* omegablas.c: INTERIM C orchestration for libomegablas (aien-dev/omega-blas).
2 *
3 * Every floating-point operation, format conversion, argument check and packed-panel move is done
4 * by the Omega unit om/blas.om (bound by omega-bridge tools/native into omega_native.c). This file
5 * only maps the CBLAS / Fortran calling conventions onto that unit: argument translation, xerbla
6 * routing, workspace allocation, cache blocking and batch loops. docs/EXTENSIONS.md lists the Omega
7 * features (more than 6 parameter slots, f32 scalar parameters, a C-callable variadic ABI, global
8 * state) that let this layer move into Omega.
9 *
10 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11 */
12#include "omegablas.h"
13
14#include <pthread.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include "omega_api.h"
20#ifdef OMB_GB10
21#include "omega_gpu_gemm_ex.h"
22#include "omega_gpu_session.h"
23#endif
24
25/* The CBLAS reference test drivers read and write these two globals (c_xerbla.c). */
26int RowMajorStrg = 0;
27int CBLAS_CallFromC = 0;
28
29/* The bound Omega runtime is one per library (one stack, one reentry flag): calls are serialised. */
30static pthread_mutex_t omb_lock = PTHREAD_MUTEX_INITIALIZER;
31
32/* Cache blocks (rows of op(A), columns of op(B), depth). Each keeps every workspace offset far
33 * below 2^32 (the Omega kernel carries panel offsets as uint). */
34enum { MC = 256, NC = 256, KC = 256 };
35
36static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
37
38static int is16(omb_type t) { return t == OMB_F16 || t == OMB_BF16; }
39static int is8(omb_type t) { return t == OMB_E4M3 || t == OMB_E5M2; }
40
41static void *xmalloc(size_t n)
42{
43 void *p = malloc(n ? n : 1);
44 if (!p) { fputs("omegablas: out of memory\n", stderr); abort(); }
45 return p;
46}
47
48/* Element count a view must cover for an r x c column-major block with leading dimension ld. */
49static size_t span(int64_t r, int64_t c, int64_t ld)
50{
51 if (r <= 0 || c <= 0) return 0;
52 return (size_t)(ld * (c - 1) + r);
53}
54
55/* Pack rows x cols of X (element (i, q) at x[off + i*si + q*sq]) into 2-row panels at d[dst]. */
56static void pack(const void *x, omb_type t, size_t xlen, uint64_t rows, uint64_t cols,
57 uint64_t off, uint64_t si, uint64_t sq, uint32_t *d, size_t dlen, uint64_t dst)
58{
59 pk p = { .rows = rows, .cols = cols, .si = si, .off = off, .sq = sq, .w = 2 * cols - 2,
60 .dst = dst, .dt = (t == OMB_BF16 || t == OMB_E5M2) ? 1 : 0 };
61 if (t == OMB_F32) pack32(&p, x, xlen, d, dlen);
62 else if (is16(t)) pack16(&p, x, xlen, d, dlen);
63 else pack8(&p, x, xlen, d, dlen);
64}
65
66/* The column-major core. ta, tb: 0 = no transpose, 1 = transpose. Arguments already checked. */
67static void core_gemm(int ta, int tb, int64_t m, int64_t n, int64_t k, float alpha, const void *A,
68 omb_type at, int64_t lda, const void *B, omb_type bt, int64_t ldb, float beta,
69 void *C, omb_type ct, int64_t ldc, const float *bias, omb_bias bmode,
70 omb_act act)
71{
72 if (m == 0 || n == 0) return;
73 int epi = act != OMB_ACT_NONE || (bias && bmode != OMB_BIAS_NONE);
74 if ((alpha == 0.0f || k == 0) && beta == 1.0f && !epi) return;
75 int nok = (alpha == 0.0f || k == 0); /* the reference reads no A or B */
76 size_t la = ta ? span(k, m, lda) : span(m, k, lda);
77 size_t lb = tb ? span(n, k, ldb) : span(k, n, ldb);
78 size_t lc = span(m, n, ldc);
79 int64_t blen = epi && bias && bmode != OMB_BIAS_NONE ? (bmode == OMB_BIAS_ROW ? m : n) : 0;
80 size_t awords = 2 * (size_t)((MC + 1) / 2) * KC, bwords = 2 * (size_t)((NC + 1) / 2) * KC;
81 size_t wlen = awords + bwords + (size_t)blen;
82 uint32_t *w = xmalloc(wlen * 4);
83 if (blen) memcpy(w + awords + bwords, bias, (size_t)blen * 4);
84 uint32_t *stg = NULL;
85 size_t slen = 0;
86 if (ct != OMB_F32) { slen = (size_t)m * NC; stg = xmalloc(slen * 4); }
87 uint64_t dt = ct == OMB_BF16 ? 1 : 0;
88 uint64_t actw = (uint64_t)act | (blen ? (bmode == OMB_BIAS_ROW ? 16u : 48u) : 0u);
89
90 for (int64_t jc = 0; jc < n; jc += NC) {
91 int64_t nb = n - jc < NC ? n - jc : NC;
92 if (ct != OMB_F32) {
93 pk sp = { .rows = (uint64_t)m, .cols = (uint64_t)nb, .si = 1, .sq = (uint64_t)ldc,
94 .off = (uint64_t)(jc * ldc), .dst = 0, .dt = dt };
95 if (beta != 0.0f) stage16(&sp, C, lc, stg, slen);
96 else memset(stg, 0, slen * 4);
97 }
98 int64_t kstep = nok ? 1 : KC;
99 int64_t kend = nok ? 1 : k;
100 for (int64_t pc = 0; pc < kend; pc += kstep) {
101 int64_t kb = nok ? 0 : (k - pc < KC ? k - pc : KC);
102 int last = pc + kstep >= kend;
103 if (kb) {
104 if (tb) pack(B, bt, lb, nb, kb, jc + pc * ldb, 1, ldb, w, wlen, awords);
105 else pack(B, bt, lb, nb, kb, pc + jc * ldb, ldb, 1, w, wlen, awords);
106 }
107 for (int64_t ic = 0; ic < m; ic += MC) {
108 int64_t mb = m - ic < MC ? m - ic : MC;
109 if (kb) {
110 if (ta) pack(A, at, la, mb, kb, pc + ic * lda, lda, 1, w, wlen, 0);
111 else pack(A, at, la, mb, kb, ic + pc * lda, 1, lda, w, wlen, 0);
112 }
113 gp g = { .m = (uint64_t)mb, .n = (uint64_t)nb, .k = (uint64_t)kb,
114 .alpha = f2u(nok ? 0.0f : alpha), .beta = f2u(pc == 0 ? beta : 1.0f),
115 .act = last ? actw : 0, .wsa = 0, .wsb = awords,
116 .wsbias = awords + bwords + (uint64_t)(bmode == OMB_BIAS_ROW ? ic : jc),
117 .mp = (uint64_t)(mb + 1) / 2, .np = (uint64_t)(nb + 1) / 2 };
118 if (ct == OMB_F32) {
119 g.ldc = (uint64_t)ldc;
120 g.offc = (uint64_t)(ic + jc * ldc);
121 gemm_k(&g, w, wlen, C, lc);
122 } else {
123 g.ldc = (uint64_t)m;
124 g.offc = (uint64_t)ic;
125 gemm_k(&g, w, wlen, stg, slen);
126 }
127 }
128 }
129 if (ct != OMB_F32) {
130 pk up = { .rows = (uint64_t)m, .cols = (uint64_t)nb, .si = 1, .sq = (uint64_t)ldc,
131 .off = (uint64_t)(jc * ldc), .dst = 0, .dt = dt };
132 unpack16(&up, stg, slen, C, lc);
133 }
134 }
135 free(stg);
136 free(w);
137}
138
139static int64_t chk_gemm(char ta, char tb, int64_t m, int64_t n, int64_t k, int64_t lda,
140 int64_t ldb, int64_t ldc)
141{
142 ck c = { .ta = (unsigned char)ta, .tb = (unsigned char)tb, .m = m, .n = n, .k = k,
143 .lda = lda, .ldb = ldb, .ldc = ldc };
144 return (int64_t)gemm_info(&c);
145}
146
147/* ---- device routing (README.md, "GB10 device path") ----
148 * MIGRATION-PENDING C, build switch OMB_GB10 (make GB10=1; the default build has no device code).
149 * The dispatch to CUDApp omega_gpu_gemm_ex stays C because an Omega unit cannot call a C entry
150 * point; its named successor is a unit-to-unit Omega call into CUDApp's src/omega_gpu_gemm_ex.om
151 * (docs/EXTENSIONS.md). Argument checks and the gemv gather/scatter stay in the Omega unit. */
152
153static int omb_dev = -1; /* -1: OMEGABLAS_DEVICE not read yet */
154static omb_route_counts omb_counts;
155
156static void omb_die(const char *what, const char *detail)
157{
158 fprintf(stderr, "omega-blas: %s%s\n", what, detail ? detail : "");
159 abort();
160}
161
162/* lock held */
163static int dev_gb10(void)
164{
165 if (omb_dev < 0) {
166 const char *e = getenv("OMEGABLAS_DEVICE");
167 if (!e || !*e || !strcmp(e, "host")) omb_dev = OMB_DEVICE_HOST;
168#ifdef OMB_GB10
169 else if (!strcmp(e, "gb10")) omb_dev = OMB_DEVICE_GB10;
170#else
171 else if (!strcmp(e, "gb10")) omb_die("OMEGABLAS_DEVICE=gb10, but this library was built without the GB10 path (make GB10=1)", NULL);
172#endif
173 else omb_die("OMEGABLAS_DEVICE must be host or gb10, not ", e);
174 }
175 return omb_dev == OMB_DEVICE_GB10;
176}
177
178#ifdef OMB_GB10
179/* count column-major products C_b (m x n, ldc) = alpha op(A_b) op(B_b) + beta C_b at element strides
180 * sa, sb, sc (all >= 0, C batches disjoint), as gemm_ex's row-major C^T = op(B)^T op(A)^T: column-major
181 * storage of B, A and C is row-major storage of their transposes, so A' = B (op tb, lda' = ldb),
182 * B' = A (op ta, ldb' = lda), M' = n, N' = m. k = 0 runs as alpha 0, k 1 (C = beta C, A and B unread).
183 * Arguments already checked, m, n, count >= 1. Any error aborts. lock held */
184static void gb10_gemm(int ta, int tb, int64_t m, int64_t n, int64_t k, float alpha, const float *A,
185 int64_t lda, const float *B, int64_t ldb, float beta, float *C, int64_t ldc,
186 int64_t count, int64_t sa, int64_t sb, int64_t sc)
187{
188 int nok = alpha == 0.0f || k == 0;
189 OmegaGemmExInfo inf;
190 OmegaGemmExArgs x;
191 memset(&x, 0, sizeof x);
192 memset(&inf, 0, sizeof inf);
193 x.op_a = tb ? OMEGA_GEMM_OP_T : OMEGA_GEMM_OP_N;
194 x.op_b = ta ? OMEGA_GEMM_OP_T : OMEGA_GEMM_OP_N;
195 x.m = (uint64_t)n;
196 x.n = (uint64_t)m;
197 x.k = nok ? 1u : (uint64_t)k;
198 x.batch = (uint64_t)count;
199 x.a_type = x.b_type = x.c_type = OMEGA_GEMM_DT_F32;
200 x.a = nok ? NULL : B;
201 x.lda = (uint64_t)ldb;
202 x.stride_a = (uint64_t)sb;
203 x.b = nok ? NULL : A;
204 x.ldb = (uint64_t)lda;
205 x.stride_b = (uint64_t)sa;
206 x.c = C;
207 x.ldc = (uint64_t)ldc;
208 x.stride_c = (uint64_t)sc;
209 x.alpha = nok ? 0.0f : alpha;
210 x.beta = beta;
211 x.compute = OMEGA_GEMM_EX_DEFAULT;
212 x.scale = OMEGA_GEMM_SCALE_NONE;
213 x.info = &inf;
214 int rc = omega_gpu_gemm_ex(&x);
215 if (rc != OMEGA_GPU_GEMM_OK) {
216 const char *why = omega_gpu_session_last_error();
217 fprintf(stderr, "omega-blas: omega_gpu_gemm_ex %s (%d) on m %lld n %lld k %lld batch %lld: %s\n",
218 omega_gpu_gemm_rc_name(rc), rc, (long long)m, (long long)n, (long long)k,
219 (long long)count, why && *why ? why : "-");
220 abort();
221 }
222 omb_counts.gb10++;
223 omb_counts.gb10_core_calls += inf.core_calls;
224 omb_counts.gb10_host_fixed += inf.host_fixed;
225}
226#endif
227
228static size_t esize(omb_type t) { return t == OMB_F32 ? 4 : is16(t) ? 2 : 1; }
229static const void *at_elem(const void *p, int64_t i, omb_type t) { return (const char *)p + (ptrdiff_t)i * (ptrdiff_t)esize(t); }
230
231/* count checked column-major products at element strides sa, sb, sc: on the GB10 for f32 without an
232 * epilogue when that device is selected, else in the host core. lock held */
233static void run_gemm(int ta, int tb, int64_t m, int64_t n, int64_t k, float alpha, const void *A,
234 omb_type at, int64_t lda, const void *B, omb_type bt, int64_t ldb, float beta,
235 void *C, omb_type ct, int64_t ldc, const float *bias, omb_bias bmode,
236 omb_act act, int64_t count, int64_t sa, int64_t sb, int64_t sc)
237{
238 int epi = act != OMB_ACT_NONE || (bias && bmode != OMB_BIAS_NONE);
239 if (m == 0 || n == 0 || count == 0 || ((alpha == 0.0f || k == 0) && beta == 1.0f && !epi)) return;
240 int gb10 = dev_gb10();
241#ifdef OMB_GB10
242 if (gb10 && at == OMB_F32 && bt == OMB_F32 && ct == OMB_F32 && !epi) {
243 if (sa >= 0 && sb >= 0 && (count == 1 || sc >= (int64_t)span(m, n, ldc)))
244 gb10_gemm(ta, tb, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc, count, sa, sb, sc);
245 else
246 for (int64_t b = 0; b < count; b++)
247 gb10_gemm(ta, tb, m, n, k, alpha, at_elem(A, b * sa, at), lda, at_elem(B, b * sb, bt),
248 ldb, beta, (void *)at_elem(C, b * sc, ct), ldc, 1, 0, 0, 0);
249 return;
250 }
251#else
252 (void)gb10;
253#endif
254 for (int64_t b = 0; b < count; b++) {
255 core_gemm(ta, tb, m, n, k, alpha, at_elem(A, b * sa, at), at, lda, at_elem(B, b * sb, bt), bt,
256 ldb, beta, (void *)at_elem(C, b * sc, ct), ct, ldc, bias, bmode, act);
257 omb_counts.host++;
258 }
259}
260
261int omb_set_device(omb_device d)
262{
263#ifdef OMB_GB10
264 if (d != OMB_DEVICE_HOST && d != OMB_DEVICE_GB10) return -1;
265#else
266 if (d != OMB_DEVICE_HOST) return -1;
267#endif
268 pthread_mutex_lock(&omb_lock);
269 omb_dev = (int)d;
270 pthread_mutex_unlock(&omb_lock);
271 return 0;
272}
273
274omb_device omb_get_device(void)
275{
276 pthread_mutex_lock(&omb_lock);
277 omb_device d = dev_gb10() ? OMB_DEVICE_GB10 : OMB_DEVICE_HOST;
278 pthread_mutex_unlock(&omb_lock);
279 return d;
280}
281
282void omb_get_route_counts(omb_route_counts *out)
283{
284 pthread_mutex_lock(&omb_lock);
285 *out = omb_counts;
286 pthread_mutex_unlock(&omb_lock);
287}
288
289#ifndef OMB_CUDAPP_REV
290#define OMB_CUDAPP_REV "none"
291#endif
292/* OMEGABLAS_ROUTE_REPORT=1: one line on stderr at unload, the receipt that calls reached the device. */
293__attribute__((destructor)) static void omb_route_report(void)
294{
295 const char *e = getenv("OMEGABLAS_ROUTE_REPORT");
296 if (!e || strcmp(e, "1")) return;
297 fprintf(stderr, "omega-blas routes: device %s host %llu gb10 %llu gb10_core_calls %llu gb10_host_fixed %llu cudapp %s\n",
298 omb_dev == OMB_DEVICE_GB10 ? "gb10" : omb_dev == OMB_DEVICE_HOST ? "host" : "unread",
299 (unsigned long long)omb_counts.host, (unsigned long long)omb_counts.gb10,
300 (unsigned long long)omb_counts.gb10_core_calls,
301 (unsigned long long)omb_counts.gb10_host_fixed, OMB_CUDAPP_REV);
302}
303
304static int trn(char c) { return !(c == 'N' || c == 'n'); }
305
306/* ---- Fortran 77 ---- */
307
308__attribute__((weak)) void xerbla_(const char *srname, const int *info, size_t len)
309{
310 char name[8] = { 0 };
311 size_t i;
312 for (i = 0; i < len && i < 7 && srname[i] != ' '; i++) name[i] = srname[i];
313 if (CBLAS_CallFromC) {
314 char rout[16] = "cblas_";
315 for (i = 0; name[i]; i++) rout[6 + i] = (char)(name[i] | 0x20);
316 cblas_xerbla(*info + 1, rout, "");
317 return;
318 }
319 fprintf(stderr, " ** On entry to %s parameter number %d had an illegal value\n", name, *info);
320 exit(1);
321}
322
323__attribute__((weak)) void cblas_xerbla(int p, const char *rout, const char *form, ...)
324{
325 (void)form;
326 if (RowMajorStrg && strstr(rout, "gemm")) {
327 if (p == 4) p = 5; else if (p == 5) p = 4; else if (p == 9) p = 11; else if (p == 11) p = 9;
328 } else if (RowMajorStrg && strstr(rout, "gemv")) {
329 if (p == 3) p = 4; else if (p == 4) p = 3;
330 }
331 fprintf(stderr, "Parameter %d to routine %s was incorrect\n", p, rout);
332}
333
334void sgemm_(const char *transa, const char *transb, const int *m, const int *n, const int *k,
335 const float *alpha, const float *a, const int *lda, const float *b, const int *ldb,
336 const float *beta, float *c, const int *ldc, size_t lta, size_t ltb)
337{
338 (void)lta; (void)ltb;
339 pthread_mutex_lock(&omb_lock);
340 int64_t info = chk_gemm(*transa, *transb, *m, *n, *k, *lda, *ldb, *ldc);
341 if (info) {
342 pthread_mutex_unlock(&omb_lock);
343 int i = (int)info;
344 xerbla_("SGEMM ", &i, 6);
345 return;
346 }
347 run_gemm(trn(*transa), trn(*transb), *m, *n, *k, *alpha, a, OMB_F32, *lda, b, OMB_F32, *ldb,
348 *beta, c, OMB_F32, *ldc, NULL, OMB_BIAS_NONE, OMB_ACT_NONE, 1, 0, 0, 0);
349 pthread_mutex_unlock(&omb_lock);
350}
351
352/* x of length nx with increment inc as a view: its covering length and the index of element 0. */
353static size_t vlen(int64_t nx, int64_t inc) { return nx ? (size_t)(1 + (nx - 1) * (inc < 0 ? -inc : inc)) : 0; }
354static uint64_t vstart(int64_t nx, int64_t inc) { return inc < 0 ? (uint64_t)((nx - 1) * -inc) : 0; }
355
356void sgemv_(const char *trans, const int *m, const int *n, const float *alpha, const float *a,
357 const int *lda, const float *x, const int *incx, const float *beta, float *y,
358 const int *incy, size_t ltrans)
359{
360 (void)ltrans;
361 pthread_mutex_lock(&omb_lock);
362 ck c = { .ta = (unsigned char)*trans, .m = *m, .n = *n, .lda = *lda, .incx = *incx,
363 .incy = *incy };
364 int info = (int)gemv_info(&c);
365 if (info) {
366 pthread_mutex_unlock(&omb_lock);
367 xerbla_("SGEMV ", &info, 6);
368 return;
369 }
370 int64_t M = *m, N = *n, ix = *incx, iy = *incy;
371 if (M == 0 || N == 0 || (*alpha == 0.0f && *beta == 1.0f)) {
372 pthread_mutex_unlock(&omb_lock);
373 return;
374 }
375 int t = trn(*trans);
376 int64_t lx = t ? M : N, ly = t ? N : M;
377 size_t wl = (size_t)(lx + ly);
378 uint32_t *w = xmalloc(wl * 4);
379 size_t xl = vlen(lx, ix), yl = vlen(ly, iy);
380 cp gx = { .n = (uint64_t)lx, .os = vstart(lx, ix), .is = ix, .od = 0, .id = 1 };
381 copy32(&gx, (const uint32_t *)x, xl, w, wl);
382 cp gy = { .n = (uint64_t)ly, .os = vstart(ly, iy), .is = iy, .od = (uint64_t)lx, .id = 1 };
383 copy32(&gy, (const uint32_t *)y, yl, w, wl);
384 if (dev_gb10()) {
385#ifdef OMB_GB10
386 /* y (ly) = alpha op(A) x + beta y as the column-major product with x a k = lx by 1 B and
387 * y a ly by 1 C, both in the workspace */
388 gb10_gemm(t, 0, ly, 1, lx, *alpha, a, *lda, (const float *)w, lx, *beta, (float *)(w + lx),
389 ly, 1, 0, 0, 0);
390#endif
391 } else {
392 gv g = { .m = (uint64_t)M, .n = (uint64_t)N, .lda = (uint64_t)*lda, .offa = 0, .ta = (uint64_t)t,
393 .alpha = f2u(*alpha), .beta = f2u(*beta), .wx = 0, .wy = (uint64_t)lx };
394 gemv_k(&g, (const uint32_t *)a, span(M, N, *lda), w, wl);
395 omb_counts.host++;
396 }
397 cp sy = { .n = (uint64_t)ly, .os = (uint64_t)lx, .is = 1, .od = vstart(ly, iy), .id = iy };
398 copy32(&sy, w, wl, (uint32_t *)y, yl);
399 free(w);
400 pthread_mutex_unlock(&omb_lock);
401}
402
403/* ---- CBLAS ---- */
404
405static char tchar(enum CBLAS_TRANSPOSE t)
406{
407 return t == CblasNoTrans ? 'N' : t == CblasTrans ? 'T' : t == CblasConjTrans ? 'C' : 0;
408}
409
410void cblas_sgemm(CBLAS_LAYOUT layout, enum CBLAS_TRANSPOSE TransA, enum CBLAS_TRANSPOSE TransB,
411 int M, int N, int K, float alpha, const float *A, int lda, const float *B, int ldb,
412 float beta, float *C, int ldc)
413{
414 char ta = tchar(TransA), tb = tchar(TransB);
415 RowMajorStrg = 0;
416 CBLAS_CallFromC = 1;
417 if (layout != CblasColMajor && layout != CblasRowMajor) {
418 cblas_xerbla(1, "cblas_sgemm", "Illegal layout setting, %d\n", layout);
419 } else {
420 if (layout == CblasRowMajor) RowMajorStrg = 1;
421 if (!ta) cblas_xerbla(2, "cblas_sgemm", "Illegal TransA setting, %d\n", TransA);
422 else if (!tb) cblas_xerbla(3, "cblas_sgemm", "Illegal TransB setting, %d\n", TransB);
423 else if (layout == CblasColMajor)
424 sgemm_(&ta, &tb, &M, &N, &K, &alpha, A, &lda, B, &ldb, &beta, C, &ldc, 1, 1);
425 else
426 sgemm_(&tb, &ta, &N, &M, &K, &alpha, B, &ldb, A, &lda, &beta, C, &ldc, 1, 1);
427 }
428 CBLAS_CallFromC = 0;
429 RowMajorStrg = 0;
430}
431
432void cblas_sgemv(CBLAS_LAYOUT layout, enum CBLAS_TRANSPOSE TransA, int M, int N, float alpha,
433 const float *A, int lda, const float *X, int incX, float beta, float *Y, int incY)
434{
435 char ta = tchar(TransA);
436 RowMajorStrg = 0;
437 CBLAS_CallFromC = 1;
438 if (layout == CblasColMajor) {
439 if (!ta) cblas_xerbla(2, "cblas_sgemv", "Illegal TransA setting, %d\n", TransA);
440 else sgemv_(&ta, &M, &N, &alpha, A, &lda, X, &incX, &beta, Y, &incY, 1);
441 } else if (layout == CblasRowMajor) {
442 RowMajorStrg = 1;
443 char tr = ta == 'N' ? 'T' : 'N';
444 if (!ta) cblas_xerbla(2, "cblas_sgemv", "Illegal TransA setting, %d\n", TransA);
445 else sgemv_(&tr, &N, &M, &alpha, A, &lda, X, &incX, &beta, Y, &incY, 1);
446 } else {
447 cblas_xerbla(1, "cblas_sgemv", "Illegal layout setting, %d\n", layout);
448 }
449 CBLAS_CallFromC = 0;
450 RowMajorStrg = 0;
451}
452
453/* ---- extensions ---- */
454
455/* omb_gemm_ex over count problems at element strides sA, sB, sC (count >= 1; batches share every
456 * argument but the pointers, so one check covers all). */
457static int gemm_exb(CBLAS_LAYOUT layout, enum CBLAS_TRANSPOSE TransA, enum CBLAS_TRANSPOSE TransB,
458 int M, int N, int K, float alpha, const void *A, omb_type atype, int lda,
459 const void *B, omb_type btype, int ldb, float beta, void *C, omb_type ctype,
460 int ldc, const float *bias, omb_bias bias_mode, omb_act act, int64_t count,
461 int64_t sA, int64_t sB, int64_t sC)
462{
463 char ta = tchar(TransA), tb = tchar(TransB);
464 if (layout != CblasColMajor && layout != CblasRowMajor) return 1;
465 if (!ta) return 2;
466 if (!tb) return 3;
467 if ((unsigned)atype > OMB_E5M2) return 9;
468 if ((unsigned)btype > OMB_E5M2) return 12;
469 if (ctype != OMB_F32 && !is16(ctype)) return 16;
470 if ((unsigned)bias_mode > OMB_BIAS_COL || (bias_mode != OMB_BIAS_NONE && !bias)) return 19;
471 if ((unsigned)act > OMB_ACT_GELU_TANH) return 20;
472 pthread_mutex_lock(&omb_lock);
473 int64_t info;
474 if (layout == CblasColMajor) {
475 info = chk_gemm(ta, tb, M, N, K, lda, ldb, ldc);
476 if (!info)
477 run_gemm(trn(ta), trn(tb), M, N, K, alpha, A, atype, lda, B, btype, ldb, beta, C,
478 ctype, ldc, bias, bias_mode, act, count, sA, sB, sC);
479 else info += 1;
480 } else {
481 /* row major C (M x N) is column major C^T = op(B)^T op(A)^T (N x M): a row bias of C is
482 * a column bias of C^T. */
483 omb_bias bm = bias_mode == OMB_BIAS_ROW ? OMB_BIAS_COL
484 : bias_mode == OMB_BIAS_COL ? OMB_BIAS_ROW : OMB_BIAS_NONE;
485 info = chk_gemm(tb, ta, N, M, K, ldb, lda, ldc);
486 if (!info)
487 run_gemm(trn(tb), trn(ta), N, M, K, alpha, B, btype, ldb, A, atype, lda, beta, C,
488 ctype, ldc, bias, bm, act, count, sB, sA, sC);
489 else {
490 info += 1;
491 if (info == 4) info = 5; else if (info == 5) info = 4;
492 else if (info == 9) info = 11; else if (info == 11) info = 9;
493 }
494 }
495 pthread_mutex_unlock(&omb_lock);
496 return (int)info;
497}
498
499int omb_gemm_ex(CBLAS_LAYOUT layout, enum CBLAS_TRANSPOSE TransA, enum CBLAS_TRANSPOSE TransB,
500 int M, int N, int K, float alpha, const void *A, omb_type atype, int lda,
501 const void *B, omb_type btype, int ldb, float beta, void *C, omb_type ctype,
502 int ldc, const float *bias, omb_bias bias_mode, omb_act act)
503{
504 return gemm_exb(layout, TransA, TransB, M, N, K, alpha, A, atype, lda, B, btype, ldb, beta, C,
505 ctype, ldc, bias, bias_mode, act, 1, 0, 0, 0);
506}
507
508int omb_hgemm(CBLAS_LAYOUT layout, enum CBLAS_TRANSPOSE TransA, enum CBLAS_TRANSPOSE TransB,
509 int M, int N, int K, float alpha, const uint16_t *A, int lda, const uint16_t *B,
510 int ldb, float beta, uint16_t *C, int ldc)
511{
512 return omb_gemm_ex(layout, TransA, TransB, M, N, K, alpha, A, OMB_F16, lda, B, OMB_F16, ldb,
513 beta, C, OMB_F16, ldc, NULL, OMB_BIAS_NONE, OMB_ACT_NONE);
514}
515
516int omb_bfgemm(CBLAS_LAYOUT layout, enum CBLAS_TRANSPOSE TransA, enum CBLAS_TRANSPOSE TransB,
517 int M, int N, int K, float alpha, const uint16_t *A, int lda, const uint16_t *B,
518 int ldb, float beta, uint16_t *C, int ldc)
519{
520 return omb_gemm_ex(layout, TransA, TransB, M, N, K, alpha, A, OMB_BF16, lda, B, OMB_BF16, ldb,
521 beta, C, OMB_BF16, ldc, NULL, OMB_BIAS_NONE, OMB_ACT_NONE);
522}
523
524int omb_gemm_batched(CBLAS_LAYOUT layout, enum CBLAS_TRANSPOSE TransA,
525 enum CBLAS_TRANSPOSE TransB, int M, int N, int K, float alpha,
526 const void *const *A, omb_type atype, int lda, const void *const *B,
527 omb_type btype, int ldb, float beta, void *const *C, omb_type ctype, int ldc,
528 int count)
529{
530 if (count < 0) return 21;
531 for (int b = 0; b < count; b++) {
532 int r = omb_gemm_ex(layout, TransA, TransB, M, N, K, alpha, A[b], atype, lda, B[b], btype,
533 ldb, beta, C[b], ctype, ldc, NULL, OMB_BIAS_NONE, OMB_ACT_NONE);
534 if (r) return r;
535 }
536 return 0;
537}
538
539int omb_gemm_strided_batched(CBLAS_LAYOUT layout, enum CBLAS_TRANSPOSE TransA,
540 enum CBLAS_TRANSPOSE TransB, int M, int N, int K, float alpha,
541 const void *A, omb_type atype, int lda, int64_t strideA,
542 const void *B, omb_type btype, int ldb, int64_t strideB, float beta,
543 void *C, omb_type ctype, int ldc, int64_t strideC, int count)
544{
545 if (count < 0) return 21;
546 if (count == 0) return 0;
547 return gemm_exb(layout, TransA, TransB, M, N, K, alpha, A, atype, lda, B, btype, ldb, beta, C,
548 ctype, ldc, NULL, OMB_BIAS_NONE, OMB_ACT_NONE, count, strideA, strideB, strideC);
549}
550
551/* The seeded Omega loop bound is 1,000,000 iterations; convert in chunks below it. */
552#define OMB_CHUNK ((size_t)1 << 19)
553
554void omb_widen(omb_type src, const void *s, float *d, size_t n)
555{
556 pthread_mutex_lock(&omb_lock);
557 if (src == OMB_F32) memmove(d, s, n * 4);
558 else
559 for (size_t o = 0; o < n; o += OMB_CHUNK) {
560 size_t c = n - o < OMB_CHUNK ? n - o : OMB_CHUNK;
561 if (is16(src)) widen16(src == OMB_BF16, (const uint16_t *)s + o, c, (uint32_t *)d + o, c);
562 else if (is8(src)) widen8(src == OMB_E5M2, (const uint8_t *)s + o, c, (uint32_t *)d + o, c);
563 }
564 pthread_mutex_unlock(&omb_lock);
565}
566
567void omb_narrow(omb_type dst, const float *s, void *d, size_t n)
568{
569 pthread_mutex_lock(&omb_lock);
570 if (dst == OMB_F32) memmove(d, s, n * 4);
571 else if (is16(dst))
572 for (size_t o = 0; o < n; o += OMB_CHUNK) {
573 size_t c = n - o < OMB_CHUNK ? n - o : OMB_CHUNK;
574 narrow16(dst == OMB_BF16, (const uint32_t *)s + o, c, (uint16_t *)d + o, c);
575 }
576 pthread_mutex_unlock(&omb_lock);
577}
578
579const char *omb_ident(void) { return omega2_ident_blas(); }