aien-train
AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.
Brought by drakestapleton
aien-train / tools / omegallm / wpatch / wporacle.py
| 1 | #!/usr/bin/env python3 |
| 2 | """ORACLE for wpcheck. A digest calculator only; it never decides PASS or FAIL. |
| 3 | |
| 4 | wpcheck (Omega) writes a request file and runs `/usr/bin/python3 wporacle.py REQUEST`. |
| 5 | Each request line produces exactly one output line, in order. The path is always the |
| 6 | last field and may contain spaces. |
| 7 | |
| 8 | sha OFF LEN PATH hex SHA-256 (hashlib) of LEN bytes at OFF; LEN `-` means to end. |
| 9 | f32 PATH hex SHA-256 of the little-endian IEEE f32 casts of the CK02 |
| 10 | parameter words (count P = u64 at 96, words from byte 120); |
| 11 | a finite value too large for f32 casts to a same-signed infinity. |
| 12 | f32ne A B STRIDE COUNT PATH decimal count of k < COUNT where the f32 casts of the u64 at |
| 13 | A + k*STRIDE and at B + k*STRIDE differ. |
| 14 | |
| 15 | It is independent of the product: it shares no code with wpatch or wpcheck, and nothing in |
| 16 | the build or in wpatch calls it. Any malformed request raises and exits nonzero. |
| 17 | """ |
| 18 | import hashlib |
| 19 | import math |
| 20 | import mmap |
| 21 | import struct |
| 22 | import sys |
| 23 | |
| 24 | U = struct.Struct('<Q') |
| 25 | F = struct.Struct('<d') |
| 26 | |
| 27 | |
| 28 | def f32bits(bits): |
| 29 | x = F.unpack(U.pack(bits))[0] |
| 30 | try: |
| 31 | return struct.pack('<f', x) |
| 32 | except OverflowError: |
| 33 | return struct.pack('<f', math.copysign(math.inf, x)) |
| 34 | |
| 35 | |
| 36 | def mapped(path): |
| 37 | with open(path, 'rb') as f: |
| 38 | return mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) |
| 39 | |
| 40 | |
| 41 | def sha(off, n, path): |
| 42 | with mapped(path) as m: |
| 43 | end = len(m) if n == '-' else int(off) + int(n) |
| 44 | if int(off) > end or end > len(m): |
| 45 | raise ValueError('range outside file: ' + path) |
| 46 | h = hashlib.sha256() |
| 47 | for o in range(int(off), end, 1 << 20): |
| 48 | h.update(m[o:min(o + (1 << 20), end)]) |
| 49 | return h.hexdigest() |
| 50 | |
| 51 | |
| 52 | def f32(path): |
| 53 | with mapped(path) as m: |
| 54 | p = U.unpack_from(m, 96)[0] |
| 55 | h = hashlib.sha256() |
| 56 | for o in range(120, 120 + 8 * p, 8 * 8192): |
| 57 | end = min(o + 8 * 8192, 120 + 8 * p) |
| 58 | h.update(b''.join(f32bits(U.unpack_from(m, j)[0]) for j in range(o, end, 8))) |
| 59 | return h.hexdigest() |
| 60 | |
| 61 | |
| 62 | def f32ne(a, b, stride, count, path): |
| 63 | with mapped(path) as m: |
| 64 | a, b, stride, count = int(a), int(b), int(stride), int(count) |
| 65 | return str(sum(f32bits(U.unpack_from(m, a + k * stride)[0]) |
| 66 | != f32bits(U.unpack_from(m, b + k * stride)[0]) |
| 67 | for k in range(count))) |
| 68 | |
| 69 | |
| 70 | def main(): |
| 71 | with open(sys.argv[1], encoding='utf-8') as req: |
| 72 | for line in req.read().splitlines(): |
| 73 | op = line.split(' ', 1)[0] |
| 74 | if op == 'sha': |
| 75 | _, off, n, path = line.split(' ', 3) |
| 76 | print(sha(off, n, path)) |
| 77 | elif op == 'f32': |
| 78 | print(f32(line.split(' ', 1)[1])) |
| 79 | elif op == 'f32ne': |
| 80 | _, a, b, stride, count, path = line.split(' ', 5) |
| 81 | print(f32ne(a, b, stride, count, path)) |
| 82 | else: |
| 83 | raise ValueError('unknown request: ' + line) |
| 84 | |
| 85 | |
| 86 | if __name__ == '__main__': |
| 87 | main() |