interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / om / gpumath / parity / corpus.py

570 lines · 21061 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3"""Deterministic parity corpus for om/gpumath/gpumath.om (aien-dev/omega-mv#726).
4
5usage: corpus.py OUTDIR [--c-cases FILE]
6
7Writes one record file per stream into OUTDIR (record formats: the harness section of gpumath.om).
8Every random value comes from splitmix64 with a fixed seed per stream, so the files are the same
9on every run and every Python 3. --c-cases also writes the typed omega_gpu_fmt calls a C oracle
10needs for the fmt stream (one `case` per M record, in order).
11"""
12import math
13import os
14import struct
15import sys
16
17M64 = (1 << 64) - 1
18
19
20class Rng:
21 def __init__(self, seed):
22 self.s = seed & M64
23
24 def u64(self):
25 self.s = (self.s + 0x9E3779B97F4A7C15) & M64
26 z = self.s
27 z = ((z ^ (z >> 30)) * 0xBF58476D1CE4E5B9) & M64
28 z = ((z ^ (z >> 27)) * 0x94D049BB133111EB) & M64
29 return z ^ (z >> 31)
30
31 def below(self, n):
32 return self.u64() % n
33
34 def unit(self):
35 return (self.u64() >> 11) / float(1 << 53)
36
37 def pick(self, seq):
38 return seq[self.below(len(seq))]
39
40
41def dbits(x):
42 return struct.unpack("<Q", struct.pack("<d", x))[0]
43
44
45def bitsd(u):
46 return struct.unpack("<d", struct.pack("<Q", u))[0]
47
48
49def fbits(x):
50 return struct.unpack("<I", struct.pack("<f", x))[0]
51
52
53def h(v):
54 return format(v, "x")
55
56
57def hb(bs):
58 return bs.hex() if bs else "-"
59
60
61D_SPECIAL = [
62 0, 1 << 63, 1, (1 << 63) | 1, 0x000FFFFFFFFFFFFF, 0x800FFFFFFFFFFFFF, 0x0010000000000000,
63 0x8010000000000000, 0x7FEFFFFFFFFFFFFF, 0xFFEFFFFFFFFFFFFF, 0x7FF0000000000000, 0xFFF0000000000000,
64 0x7FF8000000000000, 0xFFF8000000000000, 0x7FF8000000000001, 0x7FF0000000000001, 0xFFF4000000000123,
65 0x7FFFFFFFFFFFFFFF, 0x3FF0000000000000, 0xBFF0000000000000, 0x4000000000000000, 0x3FE0000000000000,
66 0x3FF6A09E667F3BCD, 0x3FF6A09E667F3BCC, 0x3FF6A09E667F3BCE, 0x0000000000000002, 0x0008000000000000,
67]
68F_SPECIAL = [
69 0, 0x80000000, 1, 0x80000001, 0x007FFFFF, 0x807FFFFF, 0x00800000, 0x80800000, 0x7F7FFFFF, 0xFF7FFFFF,
70 0x7F800000, 0xFF800000, 0x7FC00000, 0xFFC00000, 0x7FC00001, 0x7F800001, 0xFFA00123, 0x7FFFFFFF,
71 0x3F800000, 0xBF800000, 0x1F800000, 0x1F7FFFFF, 0x5F800000, 0x5F7FFFFF, 0x20000000,
72]
73
74
75def rand_double(r):
76 """A double from a mix of classes: raw bits, specials, subnormals, normals of every exponent."""
77 k = r.below(8)
78 if k == 0:
79 return r.u64()
80 if k == 1:
81 return r.pick(D_SPECIAL)
82 if k == 2:
83 return (r.below(2) << 63) | (r.u64() & 0x000FFFFFFFFFFFFF)
84 return (r.below(2) << 63) | ((1 + r.below(2046)) << 52) | (r.u64() & 0x000FFFFFFFFFFFFF)
85
86
87def rand_f32(r):
88 k = r.below(8)
89 if k == 0:
90 return r.pick(F_SPECIAL)
91 if k == 1:
92 return (r.below(2) << 31) | (r.u64() & 0x7FFFFF)
93 if k == 2:
94 return (r.below(2) << 31) | (0x7F800000) | (r.u64() & 0x7FFFFF)
95 return r.u64() & 0xFFFFFFFF
96
97
98def norm_with_exp(r, e):
99 """A random double with unbiased exponent e (clamped to the normal range), random sign."""
100 e = max(-1022, min(1023, e))
101 return (r.below(2) << 63) | ((e + 1023) << 52) | (r.u64() & 0x000FFFFFFFFFFFFF)
102
103
104def short_mant(r, e, bits):
105 """A double 1.f * 2^e whose significand has at most `bits` significant bits."""
106 m = (1 << (bits - 1)) | r.below(1 << (bits - 1))
107 return (r.below(2) << 63) | ((e + 1023) << 52) | ((m << (53 - bits)) & 0x000FFFFFFFFFFFFF)
108
109
110def gen_fma(out):
111 r = Rng(0x726F4D41)
112 n = 0
113 lines = []
114 def rec(a, b, c):
115 lines.append("Z %s %s %s\n" % (h(a), h(b), h(c)))
116 # every pair of specials with every special addend
117 for a in D_SPECIAL:
118 for b in D_SPECIAL:
119 for c in D_SPECIAL[::3]:
120 rec(a, b, c)
121 for _ in range(100000): # class mix
122 rec(rand_double(r), rand_double(r), rand_double(r))
123 for _ in range(60000): # near cancellation: c close to -(a * b)
124 ea = r.below(400) - 200
125 eb = r.below(400) - 200
126 a = norm_with_exp(r, ea)
127 b = norm_with_exp(r, eb)
128 p = bitsd(a) * bitsd(b)
129 c = dbits(-p) ^ r.below(16)
130 if r.below(4) == 0:
131 c = (c & ~0xFFFF) | r.below(1 << 16)
132 rec(a, b, c & M64)
133 for _ in range(50000): # exact short products plus addends at and around half an ulp (ties)
134 ea = r.below(200) - 100
135 eb = r.below(200) - 100
136 a = short_mant(r, ea, 1 + r.below(27))
137 b = short_mant(r, eb, 1 + r.below(27))
138 p = abs(bitsd(a) * bitsd(b))
139 e = math.frexp(p)[1] - 53
140 f = r.pick([0.5, 0.25, 0.75, 1.5, 1.0, 2.5, 0.5 + 2.0 ** -40, 0.5 - 2.0 ** -40, 3.0])
141 c = math.ldexp(f, e) * (1 if r.below(2) else -1)
142 rec(a, b, dbits(c))
143 for _ in range(50000): # products near and below the subnormal range, addends tiny
144 ea = r.below(1100) - 1100
145 eb = -1022 - 60 - ea + r.below(120)
146 a = norm_with_exp(r, ea) if r.below(5) else (r.u64() & 0x800FFFFFFFFFFFFF)
147 b = norm_with_exp(r, eb)
148 c = r.pick([0, 1 << 63, r.u64() & 0x800FFFFFFFFFFFFF, norm_with_exp(r, -1022 + r.below(8))])
149 rec(a, b, c)
150 for _ in range(20000): # products near overflow
151 ea = 500 + r.below(524)
152 eb = 1023 - ea + r.below(3) - 1
153 rec(norm_with_exp(r, ea), norm_with_exp(r, eb), r.pick([0, rand_double(r), 0xFFEFFFFFFFFFFFFF, 0x7FEFFFFFFFFFFFFF]))
154 for _ in range(20000): # the hostmath shapes: -n * piece + a, s * t2 + 1/k
155 nd = float(r.below(1 << 21)) * (1 if r.below(2) else -1)
156 piece = r.pick([0x3FF921FB54400000, 0x3DD0B4611A600000, 0x3BA3198A2E000000, 0x397B839A252049C1,
157 0x3FE62E42FEE00000, 0x3DEA39EF35793C76])
158 a = dbits(nd * 1.5707963267948966 + (r.unit() - 0.5))
159 rec(dbits(-nd), piece, a)
160 for _ in range(20000): # exact 54-bit products (a tie) plus an addend far below: only the sticky bit decides
161 while True:
162 ma = (1 << 26) | r.below(1 << 26) | 1
163 mb = (1 << 26) | r.below(1 << 26) | 1
164 if ma * mb >= 1 << 53:
165 break
166 ea = r.below(600) - 300
167 eb = r.below(600) - 300
168 a = math.ldexp(ma, ea) * (1 if r.below(2) else -1)
169 b = math.ldexp(mb, eb) * (1 if r.below(2) else -1)
170 low = ea + eb # exponent of the product's lowest (tie) bit
171 c = math.ldexp(1.0 + r.unit(), max(-1074, low - 2 - r.below(900))) * (1 if r.below(2) else -1)
172 rec(dbits(a), dbits(b), r.pick([dbits(c), dbits(c), dbits(c), 0, 1 << 63]))
173 with open(os.path.join(out, "fma.txt"), "w") as f:
174 f.writelines(lines)
175 return len(lines)
176
177
178def gen_log(out):
179 r = Rng(0x6C6F67)
180 L = []
181 for v in D_SPECIAL:
182 L.append(v)
183 for e in range(-1074, 1024):
184 L.append(dbits(math.ldexp(1.0, e)))
185 for _ in range(100000):
186 L.append(r.u64())
187 for _ in range(100000):
188 if r.below(20) == 0:
189 L.append(1 + (r.u64() & 0x000FFFFFFFFFFFFE))
190 else:
191 L.append(((1 + r.below(2046)) << 52) | (r.u64() & 0x000FFFFFFFFFFFFF))
192 for _ in range(20000):
193 L.append(dbits(1.0 + (r.unit() - 0.5) * 2.0 ** -(r.below(50))))
194 for d in range(-64, 65):
195 L.append(0x3FF6A09E667F3BCD + d)
196 for e in range(-1070, 1024, 3): # x = 2^e (1 + tiny): t is small, the fused s * 2t + e * ln2lo matters
197 for _ in range(6):
198 L.append(dbits(math.ldexp(1.0 + r.unit() * 2.0 ** -(1 + r.below(45)), e)))
199 with open(os.path.join(out, "log.txt"), "w") as f:
200 f.writelines("A %s\n" % h(v) for v in L)
201 return len(L)
202
203
204def gen_exp(out):
205 r = Rng(0x657870)
206 L = list(D_SPECIAL)
207 for _ in range(100000):
208 L.append(r.u64())
209 for _ in range(100000):
210 L.append(dbits(-750.0 + 1462.0 * r.unit()))
211 for edge in (709.78, -745.2):
212 for d in range(-200, 201):
213 L.append(dbits(edge) + d)
214 ln2 = math.log(2.0)
215 for k in list(range(-1080, -1015)) + list(range(1018, 1025)) + list(range(-5, 6)):
216 for _ in range(60):
217 L.append(dbits(k * ln2 + (r.unit() - 0.5) * 1.4))
218 for _ in range(5000):
219 L.append(dbits((r.unit() - 0.5) * 2.0 ** -(r.below(60))))
220 for k in range(-1075, 1025): # y within a few ulps of k * ln2: r is small, the fused reduction matters
221 y = dbits(k * ln2)
222 for d in (-3, -1, 0, 1, 2):
223 L.append((y + d) & M64)
224 with open(os.path.join(out, "exp.txt"), "w") as f:
225 f.writelines("B %s\n" % h(v) for v in L)
226 return len(L)
227
228
229def gen_pow(out):
230 r = Rng(0x706F77)
231 P = []
232 thetas = [10000.0, 500000.0, 1000000.0, 5000000.0, 10000000.0, 1000.0, 50000.0]
233 for th in thetas:
234 for d in (64, 80, 96, 128, 256):
235 for i in range(d // 2):
236 P.append((dbits(th), dbits(-(2.0 * i) / d)))
237 P.append((dbits(th), dbits((2.0 * i) / d)))
238 for _ in range(40000):
239 P.append((dbits(math.exp((r.unit() - 0.5) * 40.0)), dbits((r.unit() - 0.5) * 20.0)))
240 for _ in range(30000):
241 P.append((r.u64(), r.u64()))
242 for _ in range(25000):
243 P.append((rand_double(r), rand_double(r)))
244 for a in D_SPECIAL:
245 for b in D_SPECIAL:
246 P.append((a, b))
247 with open(os.path.join(out, "pow.txt"), "w") as f:
248 f.writelines("C %s %s\n" % (h(a), h(b)) for a, b in P)
249 return len(P)
250
251
252def gen_sincos(out):
253 r = Rng(0x73696E)
254 L = list(D_SPECIAL)
255 for _ in range(100000):
256 L.append(r.u64())
257 lim = 2.0 ** 20 * math.pi / 2
258 for _ in range(100000):
259 L.append(dbits((2.0 * r.unit() - 1.0) * lim))
260 for _ in range(100000): # RoPE angles: pos * theta^(-2i/d), pos < 65536
261 th = r.pick([10000.0, 500000.0, 1000000.0, 10000000.0])
262 d = r.pick([64, 128, 256])
263 i = r.below(d // 2)
264 inv = th ** (-(2.0 * i) / d)
265 L.append(dbits(float(r.below(65536)) * inv))
266 for _ in range(30000): # near multiples of pi/2: the reduction cancels
267 n = r.below(1 << 21) - (1 << 20)
268 L.append((dbits(n * (math.pi / 2)) + r.below(9) - 4) & M64)
269 for _ in range(10000):
270 L.append(dbits((r.unit() - 0.5) * 2.0 ** -(r.below(70))))
271 with open(os.path.join(out, "sincos.txt"), "w") as f:
272 f.writelines("D %s\n" % h(v) for v in L)
273 return len(L)
274
275
276def gen_pow2f(out):
277 r = Rng(0x703266)
278 L = list(range(-1000, 1001)) + [-(1 << 31), (1 << 31) - 1, -(1 << 31) + 1, 1 << 30, -(1 << 30)]
279 for _ in range(10000):
280 L.append(r.below(1 << 32) - (1 << 31))
281 with open(os.path.join(out, "pow2f.txt"), "w") as f:
282 f.writelines("E %s\n" % h(v & M64) for v in L)
283 return len(L)
284
285
286def gen_e4m3(out):
287 with open(os.path.join(out, "e4m3.txt"), "w") as f:
288 f.writelines("F %s\n" % h(b) for b in range(256))
289 return 256
290
291
292def le16(vals):
293 return b"".join(struct.pack("<H", v) for v in vals)
294
295
296def le32(vals):
297 return b"".join(struct.pack("<I", v) for v in vals)
298
299
300def gen_scales(out):
301 r = Rng(0x73636C)
302 lines = []
303 for nf in (0, 1):
304 for v in range(65536):
305 lines.append("G %d %s\n" % (nf, hb(le16([v]))))
306 bad = [0x7F80, 0xFF80, 0x7FC0, 0x7F81, 0x6000, 0x6400, 0x7F7F] # NaN/inf, and folds past 3e38
307 for i in range(60):
308 n = r.pick([0, 1, 2, 4095, 4096]) if i < 10 else 1 + r.below(4096)
309 vals = [r.below(0x5000) | (r.below(2) << 15) for _ in range(n)]
310 if n and i % 2:
311 vals[r.below(n)] = r.pick(bad)
312 lines.append("G %d %s\n" % (i % 3 == 0, hb(le16(vals))))
313 with open(os.path.join(out, "scales.txt"), "w") as f:
314 f.writelines(lines)
315 return len(lines)
316
317
318def gen_xf32(out):
319 r = Rng(0x786633)
320 vals = list(F_SPECIAL) + [rand_f32(r) for _ in range(100000)]
321 lines = ["H -\n"]
322 for o in range(0, len(vals), 4096):
323 lines.append("H %s\n" % hb(le32(vals[o:o + 4096])))
324 lines.append("H %s\n" % hb(le32(vals[:777])))
325 with open(os.path.join(out, "xf32.txt"), "w") as f:
326 f.writelines(lines)
327 return len(vals) + 777
328
329
330def gen_xbf16(out):
331 lines = ["I -\n"]
332 for o in range(0, 65536, 4096):
333 lines.append("I %s\n" % hb(le16(range(o, o + 4096))))
334 lines.append("I %s\n" % hb(le16(range(0x7F00, 0x7F00 + 333))))
335 with open(os.path.join(out, "xbf16.txt"), "w") as f:
336 f.writelines(lines)
337 return 65536 + 333
338
339
340def gen_scan(out):
341 r = Rng(0x73636E)
342 lines = []
343 sizes = [0, 1, 2, 4095, 4096, 4097, 8191, 8192, 8193, 12288, 20000]
344 for i in range(400):
345 n = sizes[i] if i < len(sizes) else r.below(20001)
346 dens = r.pick([0, 0, 1, 5, 100, 2000, 65536])
347 w = bytearray(r.below(256) for _ in range(n))
348 for j in range(n):
349 if w[j] & 0x7F == 0x7F and not (dens and r.below(65536) < dens):
350 w[j] ^= 1
351 elif dens and r.below(65536) < dens:
352 w[j] = r.pick([0x7F, 0xFF])
353 lines.append("N %s\n" % hb(bytes(w)))
354 with open(os.path.join(out, "scan.txt"), "w") as f:
355 f.writelines(lines)
356 return len(lines)
357
358
359def gen_gemv(out):
360 r = Rng(0x67656D)
361 lines = []
362 rows = 0
363 shapes = [(128, 128), (128, 256), (256, 128), (256, 384), (128, 512), (384, 256), (128, 2048),
364 (256, 1024), (128, 128), (256, 256), (128, 384), (128, 1024)]
365 for i, (n, k) in enumerate(shapes):
366 nan_ok = i % 3 == 0
367 w = bytearray(r.below(256) for _ in range(n * k))
368 if not nan_ok:
369 for j in range(len(w)):
370 if w[j] & 0x7F == 0x7F:
371 w[j] ^= 1
372 sc = [r.below(0x8000) | (r.below(2) << 15) if i % 4 == 1 else (0x3B00 + r.below(0x700)) for _ in range((n // 128) * (k // 128))]
373 if i % 4 == 2:
374 x = [rand_f32(r) for _ in range(k)]
375 else:
376 x = [fbits((r.unit() - 0.5) * 8.0) for _ in range(k)]
377 lines.append("Q %s %s %s %s %s\n" % (h(n), h(k), hb(bytes(w)), hb(le16(sc)), hb(le32(x))))
378 rows += n
379 # first NaN of every row is an e4m3 NaN code times a NaN x with a payload: the operand order decides
380 n, k = 128, 256
381 w = bytearray(r.below(256) for _ in range(n * k))
382 for j in range(len(w)):
383 if w[j] & 0x7F == 0x7F:
384 w[j] ^= 1
385 c0 = 3 + r.below(100)
386 for row in range(n):
387 w[row * k + c0] = r.pick([0x7F, 0xFF])
388 x = [fbits((r.unit() - 0.5) * 8.0) for _ in range(k)]
389 x[c0] = 0x7FC00000 | (1 + r.below(0x3FFFFF)) | (r.below(2) << 31)
390 x[c0 + 7] = 0x7F800001 + r.below(0x3FFFFF)
391 sc = [0x3B00 + r.below(0x700) for _ in range((n // 128) * (k // 128))]
392 lines.append("Q %s %s %s %s %s\n" % (h(n), h(k), hb(bytes(w)), hb(le16(sc)), hb(le32(x))))
393 rows += n
394 with open(os.path.join(out, "gemv.txt"), "w") as f:
395 f.writelines(lines)
396 return rows
397
398
399# ---- omega_gpu_fmt -------------------------------------------------------------------------------
400
401def fmt_kinds(fb):
402 """The va_arg kinds omega_gpu_fmt consumes for format bytes fb (a model of the C parser)."""
403 out = []
404 p = 0
405 n = len(fb)
406 at = lambda i: fb[i] if i < n else 0
407 while at(p):
408 if at(p) != 37:
409 p += 1
410 continue
411 q = p + 1
412 z = 0
413 l = 0
414 if at(q) == 122:
415 z = 1
416 q += 1
417 else:
418 while at(q) == 108 and l < 2:
419 l += 1
420 q += 1
421 c = at(q)
422 k = 0
423 if c == 115:
424 k = 9
425 elif c == 99:
426 k = 1
427 elif c == 100:
428 k = 4 if z else (3 if l == 2 else (2 if l == 1 else 1))
429 elif c in (117, 120):
430 k = 8 if z else (7 if l == 2 else (6 if l == 1 else 5))
431 if k:
432 out.append((k, c))
433 p = q if c == 0 else q + 1
434 return out
435
436
437C_TYPES = {1: "int", 2: "long", 3: "long long", 4: "ptrdiff_t", 5: "unsigned", 6: "unsigned long",
438 7: "unsigned long long", 8: "size_t"}
439
440STRINGS = [b"", b"hello", b"x", b"stage: device open", b"%d%s%%", bytes(range(1, 256)), b"a" * 300,
441 b"\xff\xfe\x80 bytes", b"(null)", b"tab\tnl\n"]
442
443
444def cstr(bs):
445 return '"' + "".join("\\%03o" % c for c in bs) + '"'
446
447
448def fmt_value(r, k, c):
449 """(word for the .om, C argument expression) for kind k."""
450 if k == 9:
451 if r.below(6) == 0:
452 return None, "(const char *)0"
453 s = r.pick(STRINGS)
454 return s, cstr(s)
455 if k == 1:
456 v = r.pick([0, 1, -1, 2147483647, -2147483648, 42, -42, 65, 255, 256, 300, -129, 10, 99999])
457 if r.below(3) == 0:
458 v = r.below(1 << 32) - (1 << 31)
459 word = v & 0xFFFFFFFF
460 if r.below(2):
461 word |= r.u64() & 0xFFFFFFFF00000000 # upper half garbage: the C reads an int
462 return word, "(int)%dLL" % v if v != -2147483648 else "(int)(-2147483647 - 1)"
463 if k in (2, 3, 4):
464 v = r.pick([0, 1, -1, (1 << 63) - 1, -(1 << 63), 1 << 40, -(1 << 40), 123456789012])
465 if r.below(3) == 0:
466 v = r.u64() - (1 << 63)
467 e = "%dLL" % v if v != -(1 << 63) else "(-9223372036854775807LL - 1)"
468 return v & M64, "(%s)%s" % (C_TYPES[k], e)
469 if k == 5:
470 v = r.pick([0, 1, 0xFFFFFFFF, 0x80000000, 255, 4096, 0xDEADBEEF])
471 if r.below(3) == 0:
472 v = r.below(1 << 32)
473 word = v
474 if r.below(2):
475 word |= r.u64() & 0xFFFFFFFF00000000
476 return word, "(unsigned)%dU" % v
477 v = r.pick([0, 1, M64, 1 << 63, 0xFFFFFFFF, 1 << 32, 0xDEADBEEFCAFEF00D])
478 if r.below(3) == 0:
479 v = r.u64()
480 return v, "(%s)%dULL" % (C_TYPES[k], v)
481
482
483def fmt_record(r, fb, cap, lines, cases):
484 kinds = fmt_kinds(fb)
485 words = []
486 cargs = []
487 pool = bytearray()
488 for k, c in kinds:
489 w, ce = fmt_value(r, k, c)
490 if k == 9:
491 if w is None:
492 words.append(M64)
493 else:
494 if len(pool) + len(w) + 1 > 4000:
495 w, ce = b"x", cstr(b"x")
496 words.append(len(pool))
497 pool += w + b"\0"
498 else:
499 words.append(w)
500 cargs.append(ce)
501 if not pool:
502 pool = bytearray(b"\0")
503 ks = "".join(str(k) for k, _ in kinds) or "-"
504 parts = ["M", h(cap), hb(fb), h(len(words))] + [h(w) for w in words] + [hb(bytes(pool))]
505 lines.append(" ".join(parts) + "\n")
506 cases.append("case %d: kinds = \"%s\"; n = omega_gpu_fmt(out, %d, %s%s); break;\n"
507 % (len(cases), ks, cap, cstr(fb), "".join(", " + a for a in cargs)))
508
509
510def gen_fmt(out, cfile):
511 r = Rng(0x666D74)
512 lines = []
513 cases = []
514 alpha = b"%lzdusxcA"
515 import itertools
516 for L in (1, 2, 3, 4):
517 for t in itertools.product(alpha, repeat=L):
518 fb = bytes(t)
519 if len(fmt_kinds(fb)) > 16:
520 continue
521 fmt_record(r, fb, 1024, lines, cases)
522 fmt_record(r, fb, r.below(len(fb) + 30), lines, cases)
523 mods = [b"", b"l", b"ll", b"lll", b"z", b"zl", b"lz", b"zz"]
524 convs = b"duxsc%qXi.5-"
525 for m in mods:
526 for c in convs:
527 for _ in range(20):
528 fb = b"<" + b"%" + m + bytes([c]) + b">"
529 fmt_record(r, fb, r.pick([1024, 1024, 0, 1, 2, 3, 5, 8]), lines, cases)
530 fmt_record(r, b"tail %" + m, 1024, lines, cases)
531 pieces = [b"%d", b"%ld", b"%lld", b"%zd", b"%u", b"%lu", b"%llu", b"%zu", b"%x", b"%lx", b"%llx", b"%zx",
532 b"%s", b"%c", b"%%", b"%q", b"%5d", b"%", b"%l", b"%z", b"stage ", b": ", b"=", b" rc ", b"\xff"]
533 for _ in range(12000):
534 fb = bytearray()
535 for _ in range(r.below(14)):
536 fb += r.pick(pieces)
537 fb = bytes(fb)
538 if len(fmt_kinds(fb)) > 16 or len(fb) > 1000:
539 continue
540 cap = r.pick([1024, 1024, 1024, r.below(80), r.below(8), 1, 0])
541 fmt_record(r, fb, cap, lines, cases)
542 with open(os.path.join(out, "fmt.txt"), "w") as f:
543 f.writelines(lines)
544 if cfile:
545 with open(cfile, "w") as f:
546 f.write("/* generated by om/gpumath/parity/corpus.py: one typed omega_gpu_fmt call per M record */\n")
547 f.write("static int fmt_case(long idx, char *out, const char **kindsp) {\n int n = -1; const char *kinds = 0;\n switch (idx) {\n")
548 f.writelines(" " + c for c in cases)
549 f.write(" default: break;\n }\n *kindsp = kinds;\n return n;\n}\n")
550 return len(lines)
551
552
553def main():
554 if len(sys.argv) not in (2, 4) or (len(sys.argv) == 4 and sys.argv[2] != "--c-cases"):
555 sys.exit(__doc__)
556 out = sys.argv[1]
557 cfile = sys.argv[3] if len(sys.argv) == 4 else None
558 os.makedirs(out, exist_ok=True)
559 counts = [
560 ("fma", gen_fma(out)), ("log", gen_log(out)), ("exp", gen_exp(out)), ("pow", gen_pow(out)),
561 ("sincos", gen_sincos(out)), ("pow2f", gen_pow2f(out)), ("e4m3", gen_e4m3(out)),
562 ("scales", gen_scales(out)), ("xf32", gen_xf32(out)), ("xbf16", gen_xbf16(out)),
563 ("scan", gen_scan(out)), ("gemv", gen_gemv(out)), ("fmt", gen_fmt(out, cfile)),
564 ]
565 for name, n in counts:
566 print("%s %d" % (name, n))
567
568
569if __name__ == "__main__":
570 main()