interplane.Get Omega
Read in your language

omega-blas

aien-dev/omega-blas

Brought by drakestapleton

branch main Branches Tags History

omega-blas / tests / gb10 / batched.c

180 lines · 8865 bytes Raw Blame History
1/* tests/gb10/batched.c: the f32 entry points netlib does not call (omb_gemm_strided_batched,
2 * omb_gemm_batched, omb_gemm_ex), checked on whichever device OMEGABLAS_DEVICE selects.
3 * Run by tests/gb10/run.sh. A case passes when all three hold:
4 * - every C element: ratio = |C - ref| / (2^-23 G) < limit, with G = |alpha| sum|a||b| + |beta||c0|
5 * + |bias| and ref computed in double (exact to far below 2^-23 G). Every case has K <= 16, one
6 * HMMA step per plane pair, so docs/TOLERANCE.md gives, against the exact product: device route
7 * 5 c + 1/2 + O(2^-23) = 5.69993 (limit 5.70), host route gamma_(K+3)/2^-23 = 9.50001 (limit 9.51);
8 * - every element outside the products (ld padding, gaps between batches) is unchanged;
9 * - the route counters move as predicted. One gemm_ex call per disjoint strided batch, one per
10 * problem otherwise; epilogue and non-f32 calls stay on the host.
11 * Prints one line per failure, the largest ratio and the case count; exits 0 only if every case
12 * passes.
13 * MIGRATION-PENDING C test driver: it drives the C ABI from outside, which an Omega unit cannot do
14 * yet. Successor: an Omega driver once omega-bridge carries Omega-to-C calls (docs/EXTENSIONS.md).
15 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
16#include "omegablas.h"
17
18#include <math.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#define SENT 1234.5f
24#define LIM_DEV 5.70
25#define LIM_HOST 9.51
26
27enum mode { STRIDED, OVERLAP, PTR, EX_BIAS, EX_BF16 };
28static const char *mname[] = { "strided", "overlap", "pointer", "ex+bias", "ex-bf16" };
29
30static uint64_t rs = 0x9e3779b97f4a7c15u;
31static float rnd(void) /* [-1, 1), 24 significant bits */
32{
33 rs = rs * 6364136223846793005u + 1442695040888963407u;
34 return (float)((double)(rs >> 40) * 0x1p-23 - 1.0);
35}
36
37static float bf16val(uint16_t h)
38{
39 uint32_t u = (uint32_t)h << 16;
40 float f;
41 memcpy(&f, &u, 4);
42 return f;
43}
44
45static int fails, cases, gdev;
46static double worst_dev, worst_host;
47
48/* element (i, j) of a stored matrix in the layout */
49static size_t ix(int row, int ld, int i, int j) { return row ? (size_t)i * ld + j : i + (size_t)j * ld; }
50
51static void run_case(int row, int ta, int tb, int M, int N, int K, int count, enum mode md)
52{
53 char name[96];
54 snprintf(name, sizeof name, "%s %s ta=%d tb=%d M=%d N=%d K=%d count=%d", mname[md],
55 row ? "row" : "col", ta, tb, M, N, K, count);
56 cases++;
57 CBLAS_LAYOUT lay = row ? CblasRowMajor : CblasColMajor;
58 enum CBLAS_TRANSPOSE TA = ta ? CblasTrans : CblasNoTrans, TB = tb ? CblasTrans : CblasNoTrans;
59 /* stored shapes: A is (ta ? K x M : M x K), B is (tb ? N x K : K x N), C is M x N */
60 int ar = ta ? K : M, ac = ta ? M : K, br = tb ? N : K, bc = tb ? K : N;
61 int lda = (row ? ac : ar) + 2, ldb = (row ? bc : br) + 2, ldc = (row ? N : M) + 2;
62 if (lda < 1) lda = 1;
63 if (ldb < 1) ldb = 1;
64 size_t sa = (size_t)lda * (row ? ar : ac) + 5, sb = (size_t)ldb * (row ? br : bc) + 5;
65 size_t sc = (size_t)ldc * (row ? M : N) + 5;
66 size_t nc = md == OVERLAP ? sc : sc * count;
67 float *A = malloc(sa * count * 4), *B = malloc(sb * count * 4), *C = malloc(nc * 4), *C0 = malloc(nc * 4);
68 uint16_t *Ah = malloc(sa * count * 2), *Bh = malloc(sb * count * 2);
69 float bias[16];
70 for (size_t i = 0; i < sa * count; i++) {
71 A[i] = rnd();
72 if (md == EX_BF16) { uint32_t u; memcpy(&u, &A[i], 4); Ah[i] = u >> 16; A[i] = bf16val(Ah[i]); }
73 }
74 for (size_t i = 0; i < sb * count; i++) {
75 B[i] = rnd();
76 if (md == EX_BF16) { uint32_t u; memcpy(&u, &B[i], 4); Bh[i] = u >> 16; B[i] = bf16val(Bh[i]); }
77 }
78 for (size_t i = 0; i < nc; i++) C[i] = SENT;
79 int nb = md == OVERLAP ? 1 : count;
80 for (int b = 0; b < nb; b++)
81 for (int i = 0; i < M; i++)
82 for (int j = 0; j < N; j++) C[b * sc + ix(row, ldc, i, j)] = rnd();
83 for (int i = 0; i < M; i++) bias[i] = rnd();
84 memcpy(C0, C, nc * 4);
85 const float al = 1.25f, be = md == OVERLAP ? 0.0f : -0.75f;
86
87 omb_route_counts r0, r1;
88 omb_get_route_counts(&r0);
89 int info = 0;
90 if (md == STRIDED || md == OVERLAP)
91 info = omb_gemm_strided_batched(lay, TA, TB, M, N, K, al, A, OMB_F32, lda, (int64_t)sa, B,
92 OMB_F32, ldb, (int64_t)sb, be, C, OMB_F32, ldc,
93 md == OVERLAP ? 0 : (int64_t)sc, count);
94 else if (md == PTR) {
95 const void *pa[8], *pb[8];
96 void *pc[8];
97 for (int b = 0; b < count; b++) { pa[b] = A + b * sa; pb[b] = B + b * sb; pc[b] = C + b * sc; }
98 info = omb_gemm_batched(lay, TA, TB, M, N, K, al, pa, OMB_F32, lda, pb, OMB_F32, ldb, be, pc,
99 OMB_F32, ldc, count);
100 } else if (md == EX_BIAS)
101 info = omb_gemm_ex(lay, TA, TB, M, N, K, al, A, OMB_F32, lda, B, OMB_F32, ldb, be, C, OMB_F32,
102 ldc, bias, OMB_BIAS_ROW, OMB_ACT_NONE);
103 else
104 info = omb_gemm_ex(lay, TA, TB, M, N, K, al, Ah, OMB_BF16, lda, Bh, OMB_BF16, ldb, be, C,
105 OMB_F32, ldc, NULL, OMB_BIAS_NONE, OMB_ACT_NONE);
106 omb_get_route_counts(&r1);
107
108 int bad = 0;
109 if (info) { printf("FAIL %s: info %d\n", name, info); bad = 1; }
110 /* predicted routes */
111 uint64_t eg = 0, eh = (uint64_t)count;
112 if (gdev && (md == STRIDED || md == OVERLAP || md == PTR))
113 eg = md == STRIDED ? 1 : (uint64_t)count, eh = 0;
114 if (r1.gb10 - r0.gb10 != eg || r1.host - r0.host != eh) {
115 printf("FAIL %s: routes gb10 +%llu host +%llu, predicted gb10 +%llu host +%llu\n", name,
116 (unsigned long long)(r1.gb10 - r0.gb10), (unsigned long long)(r1.host - r0.host),
117 (unsigned long long)eg, (unsigned long long)eh);
118 bad = 1;
119 }
120 /* values: in OVERLAP (stride C 0, beta 0) the last problem wins */
121 int dr = gdev && (md == STRIDED || md == OVERLAP || md == PTR);
122 double lim = dr ? LIM_DEV : LIM_HOST, *wr = dr ? &worst_dev : &worst_host;
123 char *in = calloc(nc, 1);
124 for (int b = 0; b < count; b++) {
125 int cb = md == OVERLAP ? 0 : b;
126 if (md == OVERLAP && b != count - 1) continue;
127 for (int i = 0; i < M; i++)
128 for (int j = 0; j < N; j++) {
129 double s = 0, g = 0;
130 for (int p = 0; p < K; p++) {
131 double a = A[b * sa + (ta ? ix(row, lda, p, i) : ix(row, lda, i, p))];
132 double v = B[b * sb + (tb ? ix(row, ldb, j, p) : ix(row, ldb, p, j))];
133 s += a * v;
134 g += fabs(a * v);
135 }
136 size_t e = cb * sc + ix(row, ldc, i, j);
137 double ref = (double)al * s + (double)be * C0[e];
138 g = fabs((double)al) * g + fabs((double)be) * fabs((double)C0[e]);
139 if (md == EX_BIAS) { ref += bias[i]; g += fabs((double)bias[i]); }
140 double ratio = g > 0 ? fabs((double)C[e] - ref) / (0x1p-23 * g) : (C[e] == ref ? 0 : INFINITY);
141 if (ratio > *wr) *wr = ratio;
142 if (!(ratio < lim)) {
143 if (!bad) printf("FAIL %s: C(%d,%d) batch %d = %.9g, ref %.17g, ratio %.3g\n", name, i, j, b, C[e], ref, ratio);
144 bad = 1;
145 }
146 in[e] = 1;
147 }
148 }
149 for (size_t e = 0; e < nc; e++)
150 if (!in[e] && C[e] != C0[e]) {
151 printf("FAIL %s: element %zu outside the products changed (%.9g -> %.9g)\n", name, e, C0[e], C[e]);
152 bad = 1;
153 break;
154 }
155 fails += bad;
156 free(in); free(A); free(B); free(C); free(C0); free(Ah); free(Bh);
157}
158
159int main(void)
160{
161 setvbuf(stdout, NULL, _IOLBF, 0); /* a device error aborts: keep the lines printed before it */
162 gdev = omb_get_device() == OMB_DEVICE_GB10;
163 static const int sh[][3] = { { 1, 1, 1 }, { 3, 5, 2 }, { 9, 7, 9 }, { 16, 13, 16 }, { 4, 6, 0 } };
164 for (int row = 0; row < 2; row++)
165 for (int ta = 0; ta < 2; ta++)
166 for (int tb = 0; tb < 2; tb++)
167 for (int s = 0; s < 5; s++) run_case(row, ta, tb, sh[s][0], sh[s][1], sh[s][2], 3, STRIDED);
168 run_case(0, 1, 0, 9, 7, 9, 3, OVERLAP);
169 run_case(1, 0, 1, 5, 3, 16, 3, OVERLAP);
170 run_case(0, 1, 0, 9, 7, 9, 3, PTR);
171 run_case(1, 0, 1, 5, 3, 16, 3, PTR);
172 run_case(0, 0, 1, 9, 7, 9, 1, EX_BIAS);
173 run_case(1, 1, 0, 16, 13, 16, 1, EX_BF16);
174 omb_route_counts r;
175 omb_get_route_counts(&r);
176 printf("batched: device %s, %d cases, %d failed, largest ratio device route %.3f (limit %.2f) host route %.3f (limit %.2f), routes host %llu gb10 %llu gb10_core_calls %llu\n",
177 gdev ? "gb10" : "host", cases, fails, worst_dev, LIM_DEV, worst_host, LIM_HOST, (unsigned long long)r.host,
178 (unsigned long long)r.gb10, (unsigned long long)r.gb10_core_calls);
179 return fails != 0;
180}