interplane.Get Omega
Read in your language

omega-tool-model

First-party Omega tool-use model source, serving harnesses, and verification records

Brought by drakestapleton

Comparing b86d8082c7b90e39d67fa6be603afd287fe11553 ... cfeb79af502a83db45143b4b5049ad586ae77a9d

26 files changed, +13701 -0

native-tool/ISOLATION.txt+5-0
native-tool/README.md+19-0
native-tool/RECEIPT.txt+9-0
native-tool/cycle.om+113-0
native-tool/fixture.om+10-0
native-training/FINAL-SEAL.txt+4-0
native-training/README.md+34-0
native-training/RESULTS.txt+62-0
native-training/SEAL.txt+4-0
native-training/SWEEP-SEAL.txt+2-0
native-training/blend.om+69-0
native-training/counter.om+77-0
native-training/final.om+220-0
native-training/paired.om+70-0
native-training/reader.om+246-0
native-training/reset-four.om+48-0
native-training/strict.om+220-0
native-training/sweep.om+270-0
pack/LICENSE.runtime+661-0
pack/README.md+9-0
pack/build+28-0
pack/demo+17-0
pack/inputs.sha256+3-0
pack/jobs/item76666.om+10-0
pack/model+18-0
pack/round40.om+11473-0

native-tool/ISOLATION.txt

new file mode 100644
@@ -0,0 +1,5 @@
1+Runtime isolation on Spark: empty mount root, read-only archive at /app, empty environment, unshared network, /dev/nvidiactl, /dev/nvidia0 and /dev/nvidia-uvm. No host /lib, /usr or shell mounted.
2+{"tool":"read_file","path":"jobs/item76666.om"}
3+{"tool":"omega_check","path":"jobs/item76666.om"}
4+Exit with NVIDIA device nodes omitted:
5+103

native-tool/README.md

new file mode 100644
@@ -0,0 +1,19 @@
1+# Native Omega file-read demonstration on GB10
2+
3+The executable includes the round40 model sampler, the GPU kernel emitter and dispatch code, and the tool executor in one seeded Omega program. It accepts an exact read_file request for the logical fixture jobs/item76666.om, reads fixture.om from the current directory, and feeds its contents into the next model turn. A successful run prints the read request and the model's subsequent omega_check request. It executes the read only. Return 5 rejects a different initial request, 6 rejects missing, oversized, empty or invalid fixture input, and 7 means the follow-up differs from the expected request.
4+
5+Run in the extracted component directory on ARM64 Linux on NVIDIA GB10 with the NVIDIA kernel driver and firmware present. The current working directory must contain model.ck, merges.bin and fixture.om. No shell wrapper, llama.cpp, PyTorch, libcuda or CUDA toolkit is part of this binary. The preview is a bounded demonstration. The full Sovereign Intelligence pack is under construction.
6+
7+## Reproduce the executable
8+
9+Use the seeded ARM64 Linux omc whose SHA-256 is 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f. From the repository root, use host commands to rename the standalone sampler's main entry point and append the native tool entry point. Its relative asset paths are unchanged.
10+
11+```sh
12+sed 's/^fn main to int$/fn oldmain to int/' pack/round40.om > combined.om
13+cat native-tool/cycle.om >> combined.om
14+omc < combined.om > cycle
15+chmod +x cycle
16+sha256sum cycle
17+```
18+
19+The shipped reference checkpoint is 7c7b5b5260f2d9453b6076b64e7f23e88341d96498584f2dfaa229c31ad2e346. The tokenizer is 73a29baf97619a746a21232c403bf08af71a76e48b3b2cde6d64dab766083882. The archive includes the combined Omega source, the runtime's AGPL-3.0-only license and the repository license. Download, archive extraction, Linux, device drivers and firmware are outside the measured user-space executable.

native-tool/RECEIPT.txt

new file mode 100644
@@ -0,0 +1,9 @@
1+{"tool":"read_file","path":"jobs/item76666.om"}
2+{"tool":"omega_check","path":"jobs/item76666.om"}
3+baseline 0
4+{"tool":"read_file","path":"jobs/item76666.om"}
5+nofixture 6
6+4299a6e4563faf7abb2b60a815dd40cfdd7f7799a22575cc47a5d241589395d4 cycle
7+63fe9b2e408ac56252aaf713e6b5157692fa3276e6db04965f72af1ac25f99be cycle-input.om
8+a052c9836c2cf138237ed0b9a1f8d46cbb0d70ed11b0951e70a1bd50752c0c5b baseline/fixture.om
9+4a460332a691895714dbb0612373420beba900521440a153f93c7a9e1a84a2ef native-omega-tool-gb10.tar.gz

native-tool/cycle.om

new file mode 100644
@@ -0,0 +1,113 @@
1+note Native round40 read-file demonstration. The model output is data. Exactly one fixture is authorized.
2+text creq "{\"tool\":\"read_file\",\"path\":\"jobs/item76666.om\"}"
3+text cnext "{\"tool\":\"omega_check\",\"path\":\"jobs/item76666.om\"}"
4+note AArch64 Linux open flags: O_NOFOLLOW (32768) plus O_NONBLOCK (2048).
5+text cfile "fixture.om"
6+text cpre "Task: Fix the error in this file and confirm its goal.\nFile: jobs/item76666.om\nGoal: calc starts with x and adds 4 exactly 3 times in a for loop. main calls calc with 70.\nState: read\nSource:\n"
7+text cend "Tool result: Read succeeded.\nAnswer:\n"
8+global cbuf uchar 1801
9+fn cinfer to void
10+put og 9 0
11+ocpush 0
12+n = obprompt 3
13+if ge n get og 1
14+exit 3
15+end
16+for i ulong n
17+ocpush get obtk i
18+end
19+put oon 0 0
20+for j ulong 160
21+t = gsnext
22+if eq t 0
23+ret
24+end
25+if ge add get oon 0 obtlen t OMOUT
26+exit 4
27+end
28+obtput t
29+ocpush t
30+end
31+exit 4
32+end
33+fn ccycle to int
34+for i ulong len example
35+put iprom i get example i
36+end
37+put icount 0 len example
38+cinfer
39+if ne get oon 0 len creq
40+ret 5
41+end
42+for i ulong len creq
43+if ne get creq i get oout i
44+ret 5
45+end
46+end
47+oputc 10
48+oflush
49+fd = fopen cfile 0 len cfile 34816 0
50+if lt fd 0
51+ret 6
52+end
53+n = ulong 0
54+while lt n len cbuf bound 1802
55+r = read int fd cbuf n sub len cbuf n
56+if lt r 0
57+cl = fclose int fd
58+ret 6
59+end
60+if eq r 0
61+break
62+end
63+n = add n ulong r
64+end
65+cl = fclose int fd
66+if or eq n 0 ge n len cbuf
67+ret 6
68+end
69+for i ulong len cpre
70+put iprom i get cpre i
71+end
72+for i ulong n
73+v = get cbuf i
74+if eq v 0
75+ret 6
76+end
77+put iprom add len cpre i v
78+end
79+pos = add len cpre n
80+if ne get cbuf sub n 1 10
81+put iprom pos 10
82+pos = add pos 1
83+end
84+for i ulong len cend
85+put iprom add pos i get cend i
86+end
87+put icount 0 add pos len cend
88+cinfer
89+if ne get oon 0 len cnext
90+ret 7
91+end
92+for i ulong len cnext
93+if ne get cnext i get oout i
94+ret 7
95+end
96+end
97+oputc 10
98+oflush
99+ret 0
100+end
101+fn main to int
102+if ne argcnt 1
103+ret 2
104+end
105+ockload 1
106+put og 6 1
107+obload 4 get og 0
108+ggopen
109+ggupw
110+rc = ccycle
111+r = nvstop
112+ret rc
113+end

native-tool/fixture.om

new file mode 100644
@@ -0,0 +1,10 @@
1+fn calc x int to int
2+s = x
3+for i ulong 2
4+s = add s int 4
5+end
6+ret s
7+end
8+fn main to int
9+ret calc int 70
10+end

native-training/FINAL-SEAL.txt

new file mode 100644
@@ -0,0 +1,4 @@
1+5336b8011c29ed44e0d059eff8562a9f34d6e30793253394fb7c8c12977d3663 native/counter.om
2+441d31d674367c21183e2e79cb145a26777ba1ab8c7cf265cf7681a6d09de4cc native/final.om
3+7fc81a57656ec055615121454cb5343aaf3db93c762fe310d976e5fe8d05e66d counter/base/notes/item97233.txt
4+9be3da431e0a833d2b07781de97ebbd0b14c274d16c0597820d9982a5f547cb3 counter/base/notes/item97984.txt

native-training/README.md

new file mode 100644
@@ -0,0 +1,34 @@
1+# Native tool-feedback training experiment
2+
3+The trainer and tokenizer come from aien-train at b9bafc3c5a450b44fc5dd544805e0a94479501ca. The starting weights are the round40 reference, 7c7b5b5260f2d9453b6076b64e7f23e88341d96498584f2dfaa229c31ad2e346. Use its existing 4096-token merges.bin. The compiler is the seeded ARM64 executable with SHA-256 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f. The model, tokenizer, generated training bytes, training math, request checks and GPU code for this experiment are Omega. Host commands below orchestrate the checks. The host OS and NVIDIA kernel driver and firmware are prerequisites.
4+
5+The initial 256-update pilot used a batch of one and textual document separators. Its two unseen file requests and both isolated returned-value probes failed. Its missing-file reply also regressed. The second run starts again from the shipped checkpoint. It resets Adam, uses batch four, and teaches each synthetic file under unread, read, permission-denied and missing states. Each training response ends with a zero byte, matching the sampler stop token. The second evaluation has different unpublished-at-training-time file identifiers and values. It actually reads the fixture only after an exact authorized request. It also separately probes two tool errors. This is a small diagnostic, not a general tool-use benchmark.
6+
7+## Reproduction
8+
9+Build aien-train with `TARGET=gb10 OMC=/path/to/pinned/omc OUT=/path/to/build sh tests/omegallm/build.sh`. Use the generated `build/ollm.om` and `build/ollm-gb10` below. Start in an empty working directory, with the pinned compiler available as `omc` and the source files beside you.
10+
11+```sh
12+omc < paired.om > paired && chmod +x paired
13+./paired > data.bin
14+build/ollm-gb10 bpeenc merges.bin 4096 data.bin data.tok
15+sed 's/^fn main to int$/fn oldmain to int/' build/ollm.om > reset-input.om
16+cat reset-four.om >> reset-input.om
17+omc < reset-input.om > reset && chmod +x reset
18+./reset original.ck candidate.ck 640 150
19+for step in 80 160 240 320 400 480 560 640; do build/ollm-gb10 gtrainb candidate.ck data.tok "$step" "$step" || exit; done
20+```
21+
22+For the checker, rename the main in this repository's `pack/round40.om` to `oldmain`, append `strict.om`, and compile with the same omc. Give each model a fresh working directory with model.ck, merges.bin, notes/item98271.txt containing the three bytes 314 and notes/item98349.txt containing the three bytes 268. Run the checker in each directory. Each R row gives exact request and exact final answer as zero or one. S rows give exact denied and missing replies. SEAL.txt records the checker and fixture hashes frozen before scoring. The shell, Git and hash tools are external operator tools.
23+
24+The 640-step candidate improved exact unseen requests from 0/2 to 2/2 and tool-error replies from 1/2 to 2/2. It reported neither of the two returned values correctly. A continuation trains on several different values for the same file, varying the number of digits. Use counter.om for this second corpus, reset from the 640-step candidate for 800 steps at LR_MICRO 100 and run gtrainb in 100-step chunks. The final probe is final.om with two reserved filenames, notes/item97233.txt with 853 and notes/item97984.txt with 426. The retained capability check is the complete native-tool fixture demo using its original file and request protocol. The three-digit inputs are synthetic. Neither this suite nor training loss establishes broad tool proficiency.
25+
26+The continuation completed 24 of 24 reserved synthetic reads. The original completed 0 of 24. The fixture generator and executor are in sweep.om. The generator makes 24 local files with identifiers outside the training range. The executor reads a file only after the model requests the exact path, passes the actual bytes back, and counts the cycle only when the second model reply equals those bytes. The scorer uses the same executable and fixtures for both checkpoints. Run it once with a single plus-sign argument to create fixtures in a fresh notes directory, then without arguments to score the checkpoint. Build it using the same single-entry-point composition as strict.om.
27+
28+The trained checkpoint lost the original repair demo. Interpolating at 40%, 60% and 80% of the trained weights also failed to preserve both behaviors on the small selection probes. The reference stays in its own release. The separate note-reading specialist is an experimental download. Results cover the indicated tool, path grammar and three-digit observations. They do not establish performance with arbitrary tools or general personal files.
29+
30+## Use the specialist
31+
32+Extract the gb10-note-reader archive on a DGX Spark with its NVIDIA Linux driver. From the extracted directory run `./read-note notes/item80000.txt`. The expected first line is a read_file request for that path, then 267. For another local test, supply a notes/itemNNNNN.txt path where N is a decimal digit and the file contains exactly three decimal digits without a newline. Use a directory you control. The model must independently produce the permitted path before the program reads; the final reply is emitted only if it matches the observation. Exit 2 means an invalid argument, 5 means the request was rejected, 6 means unusable file input and 7 means the model returned an incorrect value. Only this read operation is implemented.
33+
34+The executable is built by renaming the main in pack/round40.om to oldmain, appending reader.om, and compiling the combined source with the pinned seeded compiler. The release has the combined source, binary, tokenizer, experimental checkpoint, fixture and licenses. Its host requirements include Linux and the NVIDIA kernel driver and firmware.

native-training/RESULTS.txt

new file mode 100644
@@ -0,0 +1,62 @@
1+First native run, batch 1:
2+R 0 0
3+R 0 0
4+S 0
5+S 1
6+R 0 0
7+R 0 0
8+S 0
9+S 0
10+Paired run, batch 4. Reference, then trained:
11+R 0 0
12+R 0 0
13+S 0
14+S 1
15+R 1 0
16+R 1 0
17+S 1
18+S 1
19+a2ce72131389acfdeeb814c9f1cdc8b67419c29ee6bc93d5fd519ce44cd22743 retry/model.ck
20+e05fb52bf6f4887fe6f0d58500ac0f04cba3bc4d75b9b26dc765673d34976862 retry/data.tok
21+c6f9bb02b165bd9b6e98dc10695bd3134d3bba4b84d75ba166d6646db11f7a9f build/ollm-gb10
22+Continuation corpus and resulting checkpoint:
23+3832e6af82bc6feb4db71808eb53821e0cff38c8a707eeda06b4ae6d5f45feae counter/data.tok
24+1b0daa60596afe18bbebba6171704e47e412755f1b65bed871ce9cda42dfcc10 counter/model.ck
25+dcb7e89c9e5b3f10b88a074f9a74c7042d2171ef74e9cb3750031689dfd3dcbb read-note
26+Reference final cases:
27+R 0 0
28+R 0 0
29+S 0
30+S 1
31+Continuation final cases:
32+R 1 1
33+R 1 1
34+S 1
35+S 1
36+Baseline, trained: retained original demo exit codes
37+0
38+5
39+sweep-ref/result 24 0 0
40+sweep-cand/result 24 24 24
41+Blend fraction, retained-demo exit, followed by two reads and two errors:
42+400 0
43+R 1 0
44+R 1 0
45+S 0
46+S 1
47+600 5
48+R 1 1
49+R 1 0
50+S 0
51+S 1
52+800 5
53+R 1 1
54+R 1 1
55+S 1
56+S 1
57+49186c36bf258095a00743b68ae597c87d2b6abdbc23b190b6a23f0a1b999df9 gb10-note-reader.tar.gz
58+{"tool":"read_file","path":"notes/item80000.txt"}
59+267
60+{"tool":"read_file","path":"notes/item80000.txt"}
61+914
62+Symlink refusal exit 6

native-training/SEAL.txt

new file mode 100644
@@ -0,0 +1,4 @@
1+984e67e4a5359ecbf2ebefc8a0fbfdd955a6ffe1d2c0333a2e4a56a3c2d09842 native/strict.om
2+44be8cd0d77460105e8afb7d4ad627460a07225e36e7e32bf5f566de650843e7 strict
3+748064be03a08df81e31bd6f9e7e7c4cc9f84b4401b9a3c6e85b7ff816d3ba68 ref2/notes/item98271.txt
4+8b496bf96bbcc9e5ac11c068b6cfb00c32f9d163bb8a3d5af107217499de997a ref2/notes/item98349.txt

native-training/SWEEP-SEAL.txt

new file mode 100644
@@ -0,0 +1,2 @@
1+4acdd80e7b927c8c2aab45bf9c499f080327e4cefcf3e04708b2c93d0caed769 native/sweep.om
2+646d0e4b335fdc4f7821c0c696f7825ac5ec0aa7b0375a19b588d4d811d75bf0 sweep

native-training/blend.om

new file mode 100644
@@ -0,0 +1,69 @@
1+note Merge two round40 inference checkpoints. Preserve tensor geometry and reset Adam.
2+global shape ulong 6
3+fn bone i ulong alpha f64 to void
4+x = ogrg i
5+y = owg i
6+y = sub y x
7+y = mul alpha y
8+owp i add x y
9+end
10+fn bmix alpha f64 to void
11+p = get og 8
12+for c ulong div add p 65535 65536
13+st = mul c 65536
14+count = sub p st
15+if gt count 65536
16+count = ulong 65536
17+end
18+for j ulong count
19+i = add st j
20+bone i alpha
21+omvp i 0.0f64
22+omvp add i p 0.0f64
23+end
24+end
25+end
26+fn main to int
27+if ne oargc 5
28+ret 2
29+end
30+mix = oargu 4
31+if or eq mix 0 ge mix 1000
32+ret 3
33+end
34+n = oargp 3
35+fd = fopen oarg 0 n 0 0
36+if ge fd 0
37+r = fclose int fd
38+ret 52
39+end
40+if ne fd -2
41+ret 70
42+end
43+ockload 1
44+for i ulong 6
45+put shape i get og i
46+end
47+p = get og 8
48+for c ulong div add p 65535 65536
49+st = mul c 65536
50+count = sub p st
51+if gt count 65536
52+count = ulong 65536
53+end
54+for j ulong count
55+i = add st j
56+ogrp i owg i
57+end
58+end
59+ockload 2
60+for i ulong 6
61+if ne get shape i get og i
62+ret 53
63+end
64+end
65+bmix div f64 mix 1000.0
66+put og 41 0
67+ocksave 3
68+ret 0
69+end

native-training/counter.om

new file mode 100644
@@ -0,0 +1,77 @@
1+om2
2+text lt1 "notes/item"
3+text lt2 ".txt"
4+text lt3 "Task: Read the note and report its value.\nFile: "
5+text lt4 "\nState: unread\nAnswer:\n{\"tool\":\"read_file\",\"path\":\""
6+text lt5 "\"}"
7+text lt6 "\nState: read\nTool result: value = "
8+text lt7 "\nAnswer:\n"
9+text lt8 "\nState: denied\nTool result: Permission denied.\nAnswer:\nPermission denied."
10+text lt9 "\nState: missing\nTool result: File not found.\nAnswer:\nFile not found."
11+note Emit synthetic tool-call and observation lessons with disjoint numeric identifiers.
12+global term uchar 1
13+global digits uchar 12
14+fn lsout s view uchar * to void
15+r = write 1 s 0 len s
16+if ne r long len s
17+exit 3
18+end
19+end
20+fn lsnum n ulong to void
21+x = n
22+j = ulong 0
23+while gt x 0 bound 12
24+put digits j uchar add 48 rem x 10
25+x = div x 10
26+j = add j 1
27+end
28+for i ulong j
29+r = write 1 digits sub sub j i 1 1
30+if ne r 1
31+exit 3
32+end
33+end
34+end
35+fn lspath n ulong to void
36+lsout lt1
37+lsnum n
38+lsout lt2
39+end
40+fn main to int
41+for i ulong 8192
42+n = add 10000 rem mul div i 8 7919 65521
43+lsout lt3
44+lspath n
45+s = rem i 8
46+v = add 100 rem mul i 7907 900
47+if eq s 2
48+v = add 10 rem mul i 571 90
49+end
50+if eq s 5
51+v = add 1000 rem mul i 7841 9000
52+end
53+if eq s 0
54+lsout lt4
55+lspath n
56+lsout lt5
57+else
58+if lt s 6
59+lsout lt6
60+lsnum v
61+lsout lt7
62+lsnum v
63+else
64+if eq s 2
65+lsout lt8
66+else
67+lsout lt9
68+end
69+end
70+end
71+r = write 1 term 0 1
72+if ne r 1
73+exit 3
74+end
75+end
76+ret 0
77+end

native-training/final.om

new file mode 100644
@@ -0,0 +1,220 @@
1+note Two unseen real fixture reads. Count the first request, the isolated result reply and the complete cycle separately.
2+text qhead "Task: Read the note and report its value.\nFile: "
3+text q1 "notes/item97233.txt"
4+text q2 "notes/item97984.txt"
5+text ask "\nState: unread\nAnswer:\n"
6+text result "\nState: read\nTool result: value = "
7+text answer "\nAnswer:\n"
8+text req1 "{\"tool\":\"read_file\",\"path\":\"notes/item97233.txt\"}"
9+text req2 "{\"tool\":\"read_file\",\"path\":\"notes/item97984.txt\"}"
10+text denied "\nState: denied\nTool result: Permission denied.\nAnswer:\n"
11+text noauth "Permission denied."
12+text missing "\nState: missing\nTool result: File not found.\nAnswer:\n"
13+text nofile "File not found."
14+global qdata uchar 3
15+fn qlen t ulong to ulong
16+if eq t 0
17+ret len qhead
18+end
19+if eq t 1
20+ret len q1
21+end
22+if eq t 2
23+ret len q2
24+end
25+if eq t 3
26+ret len ask
27+end
28+if eq t 4
29+ret len result
30+end
31+if eq t 5
32+ret len answer
33+end
34+if eq t 6
35+ret len req1
36+end
37+if eq t 7
38+ret len req2
39+end
40+if eq t 8
41+ret len denied
42+end
43+if eq t 9
44+ret len noauth
45+end
46+if eq t 10
47+ret len missing
48+end
49+if eq t 11
50+ret len nofile
51+end
52+if eq t 12
53+ret len qdata
54+end
55+exit 77
56+end
57+fn qbyte t ulong i ulong to uchar
58+if eq t 0
59+ret get qhead i
60+end
61+if eq t 1
62+ret get q1 i
63+end
64+if eq t 2
65+ret get q2 i
66+end
67+if eq t 3
68+ret get ask i
69+end
70+if eq t 4
71+ret get result i
72+end
73+if eq t 5
74+ret get answer i
75+end
76+if eq t 6
77+ret get req1 i
78+end
79+if eq t 7
80+ret get req2 i
81+end
82+if eq t 8
83+ret get denied i
84+end
85+if eq t 9
86+ret get noauth i
87+end
88+if eq t 10
89+ret get missing i
90+end
91+if eq t 11
92+ret get nofile i
93+end
94+if eq t 12
95+ret get qdata i
96+end
97+exit 77
98+end
99+fn qadd s ulong to void
100+n = get icount 0
101+for i ulong qlen s
102+put iprom add n i qbyte s i
103+end
104+put icount 0 add n qlen s
105+end
106+fn qmake p ulong to void
107+put icount 0 0
108+qadd 0
109+qadd p
110+end
111+fn qinfer to void
112+put og 9 0
113+ocpush 0
114+n = obprompt 3
115+for i ulong n
116+ocpush get obtk i
117+end
118+put oon 0 0
119+for j ulong 64
120+c = gsnext
121+if eq c 0
122+ret
123+end
124+obtput c
125+ocpush c
126+end
127+end
128+fn qeq s ulong to bool
129+if ne get oon 0 qlen s
130+ret false
131+end
132+for i ulong qlen s
133+if ne get oout i qbyte s i
134+ret false
135+end
136+end
137+ret true
138+end
139+fn qcase p ulong want ulong to void
140+qmake p
141+qadd 3
142+qinfer
143+asked = qeq want
144+if not asked
145+put oon 0 0
146+oputc 82
147+oputc 32
148+oputc 48
149+oputc 32
150+oputc 48
151+oputc 10
152+oflush
153+ret
154+end
155+fd = long 0
156+if eq p 1
157+fd = fopen q1 0 len q1 0 0
158+else
159+fd = fopen q2 0 len q2 0 0
160+end
161+if lt fd 0
162+exit 71
163+end
164+r = read int fd qdata 0 len qdata
165+cl = fclose int fd
166+if ne r 3
167+exit 72
168+end
169+qmake p
170+qadd 4
171+qadd 12
172+qadd 5
173+qinfer
174+got = qeq 12
175+put oon 0 0
176+oputc 82
177+oputc 32
178+if asked
179+oputc 49
180+else
181+oputc 48
182+end
183+oputc 32
184+if got
185+oputc 49
186+else
187+oputc 48
188+end
189+oputc 10
190+oflush
191+end
192+fn qstate st ulong expected ulong to void
193+qmake 1
194+qadd st
195+qinfer
196+ok = qeq expected
197+put oon 0 0
198+oputc 83
199+oputc 32
200+if ok
201+oputc 49
202+else
203+oputc 48
204+end
205+oputc 10
206+oflush
207+end
208+fn main to int
209+ockload 1
210+put og 6 1
211+obload 4 get og 0
212+ggopen
213+ggupw
214+qcase 1 6
215+qcase 2 7
216+qstate 8 9
217+qstate 10 11
218+r = nvstop
219+ret 0
220+end

native-training/paired.om

new file mode 100644
@@ -0,0 +1,70 @@
1+om2
2+text lt1 "notes/item"
3+text lt2 ".txt"
4+text lt3 "Task: Read the note and report its value.\nFile: "
5+text lt4 "\nState: unread\nAnswer:\n{\"tool\":\"read_file\",\"path\":\""
6+text lt5 "\"}"
7+text lt6 "\nState: read\nTool result: value = "
8+text lt7 "\nAnswer:\n"
9+text lt8 "\nState: denied\nTool result: Permission denied.\nAnswer:\nPermission denied."
10+text lt9 "\nState: missing\nTool result: File not found.\nAnswer:\nFile not found."
11+note Emit synthetic tool-call and observation lessons with disjoint numeric identifiers.
12+global term uchar 1
13+global digits uchar 12
14+fn lsout s view uchar * to void
15+r = write 1 s 0 len s
16+if ne r long len s
17+exit 3
18+end
19+end
20+fn lsnum n ulong to void
21+x = n
22+j = ulong 0
23+while gt x 0 bound 12
24+put digits j uchar add 48 rem x 10
25+x = div x 10
26+j = add j 1
27+end
28+for i ulong j
29+r = write 1 digits sub sub j i 1 1
30+if ne r 1
31+exit 3
32+end
33+end
34+end
35+fn lspath n ulong to void
36+lsout lt1
37+lsnum n
38+lsout lt2
39+end
40+fn main to int
41+for i ulong 2048
42+n = add 10000 rem mul div i 4 7919 65521
43+lsout lt3
44+lspath n
45+s = rem i 4
46+if eq s 0
47+lsout lt4
48+lspath n
49+lsout lt5
50+else
51+if eq s 1
52+lsout lt6
53+lsnum add 10 rem n 71
54+lsout lt7
55+lsnum add 10 rem n 71
56+else
57+if eq s 2
58+lsout lt8
59+else
60+lsout lt9
61+end
62+end
63+end
64+r = write 1 term 0 1
65+if ne r 1
66+exit 3
67+end
68+end
69+ret 0
70+end

native-training/reader.om

new file mode 100644
@@ -0,0 +1,246 @@
1+note Two unseen real fixture reads. Count the first request, the isolated result reply and the complete cycle separately.
2+text qhead "Task: Read the note and report its value.\nFile: "
3+text q1 "notes/item97233.txt"
4+text q2 "notes/item97984.txt"
5+text ask "\nState: unread\nAnswer:\n"
6+text result "\nState: read\nTool result: value = "
7+text answer "\nAnswer:\n"
8+text req1 "{\"tool\":\"read_file\",\"path\":\"notes/item97233.txt\"}"
9+text req2 "{\"tool\":\"read_file\",\"path\":\"notes/item97984.txt\"}"
10+text denied "\nState: denied\nTool result: Permission denied.\nAnswer:\n"
11+text noauth "Permission denied."
12+text missing "\nState: missing\nTool result: File not found.\nAnswer:\n"
13+text nofile "File not found."
14+global qdata uchar 3
15+fn qlen t ulong to ulong
16+if eq t 0
17+ret len qhead
18+end
19+if eq t 1
20+ret len q1
21+end
22+if eq t 2
23+ret len q2
24+end
25+if eq t 3
26+ret len ask
27+end
28+if eq t 4
29+ret len result
30+end
31+if eq t 5
32+ret len answer
33+end
34+if eq t 6
35+ret len req1
36+end
37+if eq t 7
38+ret len req2
39+end
40+if eq t 8
41+ret len denied
42+end
43+if eq t 9
44+ret len noauth
45+end
46+if eq t 10
47+ret len missing
48+end
49+if eq t 11
50+ret len nofile
51+end
52+if eq t 12
53+ret len qdata
54+end
55+exit 77
56+end
57+global sid ulong 1
58+global spath uchar 19
59+text mark1 "notes/item"
60+text mark2 "{\"tool\":\"read_file\",\"path\":\"notes/item"
61+fn sdig i ulong to uchar
62+v = get sid 0
63+for j ulong sub 4 i
64+v = div v 10
65+end
66+ret uchar add 48 rem v 10
67+end
68+fn qbyte t ulong i ulong to uchar
69+if eq t 0
70+ret get qhead i
71+end
72+if eq t 1
73+if and ge i len mark1 lt i add len mark1 5
74+ret sdig sub i len mark1
75+end
76+ret get q1 i
77+end
78+if eq t 2
79+ret get q2 i
80+end
81+if eq t 3
82+ret get ask i
83+end
84+if eq t 4
85+ret get result i
86+end
87+if eq t 5
88+ret get answer i
89+end
90+if eq t 6
91+if and ge i len mark2 lt i add len mark2 5
92+ret sdig sub i len mark2
93+end
94+ret get req1 i
95+end
96+if eq t 7
97+ret get req2 i
98+end
99+if eq t 8
100+ret get denied i
101+end
102+if eq t 9
103+ret get noauth i
104+end
105+if eq t 10
106+ret get missing i
107+end
108+if eq t 11
109+ret get nofile i
110+end
111+if eq t 12
112+ret get qdata i
113+end
114+exit 77
115+end
116+fn qadd s ulong to void
117+n = get icount 0
118+for i ulong qlen s
119+put iprom add n i qbyte s i
120+end
121+put icount 0 add n qlen s
122+end
123+fn qmake p ulong to void
124+put icount 0 0
125+qadd 0
126+qadd p
127+end
128+fn qinfer to void
129+put og 9 0
130+ocpush 0
131+n = obprompt 3
132+for i ulong n
133+ocpush get obtk i
134+end
135+put oon 0 0
136+for j ulong 64
137+c = gsnext
138+if eq c 0
139+ret
140+end
141+obtput c
142+ocpush c
143+end
144+end
145+fn qeq s ulong to bool
146+if ne get oon 0 qlen s
147+ret false
148+end
149+for i ulong qlen s
150+if ne get oout i qbyte s i
151+ret false
152+end
153+end
154+ret true
155+end
156+note Read one operator-selected note. Only notes/itemNNNNN.txt is admitted. Write only verified model replies.
157+global sbuf uchar 4
158+fn rdval to int
159+fd = fopen spath 0 len spath 34816 0
160+if lt fd 0
161+ret 6
162+end
163+n = ulong 0
164+while lt n 4 bound 5
165+r = read int fd sbuf n sub 4 n
166+if lt r 0
167+cl = fclose int fd
168+ret 6
169+end
170+if eq r 0
171+break
172+end
173+n = add n ulong r
174+end
175+cl = fclose int fd
176+if ne n 3
177+ret 6
178+end
179+for i ulong 3
180+v = get sbuf i
181+if or lt v 48 gt v 57
182+ret 6
183+end
184+put qdata i v
185+end
186+ret 0
187+end
188+fn deliver to int
189+qmake 1
190+qadd 3
191+qinfer
192+if not qeq 6
193+ret 5
194+end
195+oputc 10
196+oflush
197+rc = rdval
198+if ne rc 0
199+ret rc
200+end
201+qmake 1
202+qadd 4
203+qadd 12
204+qadd 5
205+qinfer
206+if not qeq 12
207+ret 7
208+end
209+oputc 10
210+oflush
211+ret 0
212+end
213+global userarg uchar 21
214+fn main to int
215+if ne argcnt 2
216+ret 2
217+end
218+n = argread 1 userarg 0 len userarg
219+if ne n long len spath
220+ret 2
221+end
222+v = ulong 0
223+for j ulong 5
224+c = get userarg add len mark1 j
225+if or lt c 48 gt c 57
226+ret 2
227+end
228+v = add mul v 10 sub ulong c 48
229+end
230+put sid 0 v
231+for i ulong len spath
232+c = qbyte 1 i
233+if ne get userarg i c
234+ret 2
235+end
236+put spath i c
237+end
238+ockload 1
239+put og 6 1
240+obload 4 get og 0
241+ggopen
242+ggupw
243+rc = deliver
244+r = nvstop
245+ret rc
246+end

native-training/reset-four.om

new file mode 100644
@@ -0,0 +1,48 @@
1+note Experimental new learning stage. INPUT OUTPUT TOTAL LR_MICRO.
2+note Retain all model weights. Reset Adam moments and step for the new corpus.
3+fn ckempty to void
4+n = oargp 2
5+fd = int fopen oarg 0 n 0 0
6+if ge fd 0
7+r = fclose fd
8+exit 52
9+end
10+if ne fd -2
11+exit 70
12+end
13+end
14+fn clrmom to void
15+p = get og 8
16+for c ulong div add p 65535 65536
17+st = mul c 65536
18+nn = sub p st
19+if gt nn 65536
20+nn = 65536
21+end
22+for j ulong nn
23+i = add st j
24+omvp i 0.0f64
25+omvp add p i 0.0f64
26+end
27+end
28+end
29+fn main to int
30+if ne oargc 5
31+ret 2
32+end
33+tt = oargu 3
34+lr = oargu 4
35+if or or lt tt 101 gt tt 10000 or eq lr 0 gt lr 1000
36+ret 51
37+end
38+ckempty
39+ockload 1
40+clrmom
41+put og 41 0
42+put og 42 tt
43+put og 43 bitcast ulong div f64 lr 1000000.0
44+put og 6 4
45+put og 44 1
46+ocksave 2
47+ret 0
48+end

native-training/strict.om

new file mode 100644
@@ -0,0 +1,220 @@
1+note Two unseen real fixture reads. Count the first request, the isolated result reply and the complete cycle separately.
2+text qhead "Task: Read the note and report its value.\nFile: "
3+text q1 "notes/item98271.txt"
4+text q2 "notes/item98349.txt"
5+text ask "\nState: unread\nAnswer:\n"
6+text result "\nState: read\nTool result: value = "
7+text answer "\nAnswer:\n"
8+text req1 "{\"tool\":\"read_file\",\"path\":\"notes/item98271.txt\"}"
9+text req2 "{\"tool\":\"read_file\",\"path\":\"notes/item98349.txt\"}"
10+text denied "\nState: denied\nTool result: Permission denied.\nAnswer:\n"
11+text noauth "Permission denied."
12+text missing "\nState: missing\nTool result: File not found.\nAnswer:\n"
13+text nofile "File not found."
14+global qdata uchar 3
15+fn qlen t ulong to ulong
16+if eq t 0
17+ret len qhead
18+end
19+if eq t 1
20+ret len q1
21+end
22+if eq t 2
23+ret len q2
24+end
25+if eq t 3
26+ret len ask
27+end
28+if eq t 4
29+ret len result
30+end
31+if eq t 5
32+ret len answer
33+end
34+if eq t 6
35+ret len req1
36+end
37+if eq t 7
38+ret len req2
39+end
40+if eq t 8
41+ret len denied
42+end
43+if eq t 9
44+ret len noauth
45+end
46+if eq t 10
47+ret len missing
48+end
49+if eq t 11
50+ret len nofile
51+end
52+if eq t 12
53+ret len qdata
54+end
55+exit 77
56+end
57+fn qbyte t ulong i ulong to uchar
58+if eq t 0
59+ret get qhead i
60+end
61+if eq t 1
62+ret get q1 i
63+end
64+if eq t 2
65+ret get q2 i
66+end
67+if eq t 3
68+ret get ask i
69+end
70+if eq t 4
71+ret get result i
72+end
73+if eq t 5
74+ret get answer i
75+end
76+if eq t 6
77+ret get req1 i
78+end
79+if eq t 7
80+ret get req2 i
81+end
82+if eq t 8
83+ret get denied i
84+end
85+if eq t 9
86+ret get noauth i
87+end
88+if eq t 10
89+ret get missing i
90+end
91+if eq t 11
92+ret get nofile i
93+end
94+if eq t 12
95+ret get qdata i
96+end
97+exit 77
98+end
99+fn qadd s ulong to void
100+n = get icount 0
101+for i ulong qlen s
102+put iprom add n i qbyte s i
103+end
104+put icount 0 add n qlen s
105+end
106+fn qmake p ulong to void
107+put icount 0 0
108+qadd 0
109+qadd p
110+end
111+fn qinfer to void
112+put og 9 0
113+ocpush 0
114+n = obprompt 3
115+for i ulong n
116+ocpush get obtk i
117+end
118+put oon 0 0
119+for j ulong 64
120+c = gsnext
121+if eq c 0
122+ret
123+end
124+obtput c
125+ocpush c
126+end
127+end
128+fn qeq s ulong to bool
129+if ne get oon 0 qlen s
130+ret false
131+end
132+for i ulong qlen s
133+if ne get oout i qbyte s i
134+ret false
135+end
136+end
137+ret true
138+end
139+fn qcase p ulong want ulong to void
140+qmake p
141+qadd 3
142+qinfer
143+asked = qeq want
144+if not asked
145+put oon 0 0
146+oputc 82
147+oputc 32
148+oputc 48
149+oputc 32
150+oputc 48
151+oputc 10
152+oflush
153+ret
154+end
155+fd = long 0
156+if eq p 1
157+fd = fopen q1 0 len q1 0 0
158+else
159+fd = fopen q2 0 len q2 0 0
160+end
161+if lt fd 0
162+exit 71
163+end
164+r = read int fd qdata 0 len qdata
165+cl = fclose int fd
166+if ne r 3
167+exit 72
168+end
169+qmake p
170+qadd 4
171+qadd 12
172+qadd 5
173+qinfer
174+got = qeq 12
175+put oon 0 0
176+oputc 82
177+oputc 32
178+if asked
179+oputc 49
180+else
181+oputc 48
182+end
183+oputc 32
184+if got
185+oputc 49
186+else
187+oputc 48
188+end
189+oputc 10
190+oflush
191+end
192+fn qstate st ulong expected ulong to void
193+qmake 1
194+qadd st
195+qinfer
196+ok = qeq expected
197+put oon 0 0
198+oputc 83
199+oputc 32
200+if ok
201+oputc 49
202+else
203+oputc 48
204+end
205+oputc 10
206+oflush
207+end
208+fn main to int
209+ockload 1
210+put og 6 1
211+obload 4 get og 0
212+ggopen
213+ggupw
214+qcase 1 6
215+qcase 2 7
216+qstate 8 9
217+qstate 10 11
218+r = nvstop
219+ret 0
220+end

native-training/sweep.om

new file mode 100644
@@ -0,0 +1,270 @@
1+note Two unseen real fixture reads. Count the first request, the isolated result reply and the complete cycle separately.
2+text qhead "Task: Read the note and report its value.\nFile: "
3+text q1 "notes/item97233.txt"
4+text q2 "notes/item97984.txt"
5+text ask "\nState: unread\nAnswer:\n"
6+text result "\nState: read\nTool result: value = "
7+text answer "\nAnswer:\n"
8+text req1 "{\"tool\":\"read_file\",\"path\":\"notes/item97233.txt\"}"
9+text req2 "{\"tool\":\"read_file\",\"path\":\"notes/item97984.txt\"}"
10+text denied "\nState: denied\nTool result: Permission denied.\nAnswer:\n"
11+text noauth "Permission denied."
12+text missing "\nState: missing\nTool result: File not found.\nAnswer:\n"
13+text nofile "File not found."
14+global qdata uchar 3
15+fn qlen t ulong to ulong
16+if eq t 0
17+ret len qhead
18+end
19+if eq t 1
20+ret len q1
21+end
22+if eq t 2
23+ret len q2
24+end
25+if eq t 3
26+ret len ask
27+end
28+if eq t 4
29+ret len result
30+end
31+if eq t 5
32+ret len answer
33+end
34+if eq t 6
35+ret len req1
36+end
37+if eq t 7
38+ret len req2
39+end
40+if eq t 8
41+ret len denied
42+end
43+if eq t 9
44+ret len noauth
45+end
46+if eq t 10
47+ret len missing
48+end
49+if eq t 11
50+ret len nofile
51+end
52+if eq t 12
53+ret len qdata
54+end
55+exit 77
56+end
57+global sid ulong 1
58+global spath uchar 19
59+text mark1 "notes/item"
60+text mark2 "{\"tool\":\"read_file\",\"path\":\"notes/item"
61+fn sdig i ulong to uchar
62+v = get sid 0
63+for j ulong sub 4 i
64+v = div v 10
65+end
66+ret uchar add 48 rem v 10
67+end
68+fn qbyte t ulong i ulong to uchar
69+if eq t 0
70+ret get qhead i
71+end
72+if eq t 1
73+if and ge i len mark1 lt i add len mark1 5
74+ret sdig sub i len mark1
75+end
76+ret get q1 i
77+end
78+if eq t 2
79+ret get q2 i
80+end
81+if eq t 3
82+ret get ask i
83+end
84+if eq t 4
85+ret get result i
86+end
87+if eq t 5
88+ret get answer i
89+end
90+if eq t 6
91+if and ge i len mark2 lt i add len mark2 5
92+ret sdig sub i len mark2
93+end
94+ret get req1 i
95+end
96+if eq t 7
97+ret get req2 i
98+end
99+if eq t 8
100+ret get denied i
101+end
102+if eq t 9
103+ret get noauth i
104+end
105+if eq t 10
106+ret get missing i
107+end
108+if eq t 11
109+ret get nofile i
110+end
111+if eq t 12
112+ret get qdata i
113+end
114+exit 77
115+end
116+fn qadd s ulong to void
117+n = get icount 0
118+for i ulong qlen s
119+put iprom add n i qbyte s i
120+end
121+put icount 0 add n qlen s
122+end
123+fn qmake p ulong to void
124+put icount 0 0
125+qadd 0
126+qadd p
127+end
128+fn qinfer to void
129+put og 9 0
130+ocpush 0
131+n = obprompt 3
132+for i ulong n
133+ocpush get obtk i
134+end
135+put oon 0 0
136+for j ulong 64
137+c = gsnext
138+if eq c 0
139+ret
140+end
141+obtput c
142+ocpush c
143+end
144+end
145+fn qeq s ulong to bool
146+if ne get oon 0 qlen s
147+ret false
148+end
149+for i ulong qlen s
150+if ne get oout i qbyte s i
151+ret false
152+end
153+end
154+ret true
155+end
156+fn qcase p ulong want ulong to void
157+qmake p
158+qadd 3
159+qinfer
160+asked = qeq want
161+if not asked
162+put oon 0 0
163+oputc 82
164+oputc 32
165+oputc 48
166+oputc 32
167+oputc 48
168+oputc 10
169+oflush
170+ret
171+end
172+fd = long 0
173+if eq p 1
174+fd = fopen spath 0 len spath 34816 0
175+else
176+fd = fopen q2 0 len q2 0 0
177+end
178+if lt fd 0
179+exit 71
180+end
181+r = read int fd qdata 0 len qdata
182+cl = fclose int fd
183+if ne r 3
184+exit 72
185+end
186+qmake p
187+qadd 4
188+qadd 12
189+qadd 5
190+qinfer
191+got = qeq 12
192+put oon 0 0
193+oputc 82
194+oputc 32
195+if asked
196+oputc 49
197+else
198+oputc 48
199+end
200+oputc 32
201+if got
202+oputc 49
203+else
204+oputc 48
205+end
206+oputc 10
207+oflush
208+end
209+fn qstate st ulong expected ulong to void
210+qmake 1
211+qadd st
212+qinfer
213+ok = qeq expected
214+put oon 0 0
215+oputc 83
216+oputc 32
217+if ok
218+oputc 49
219+else
220+oputc 48
221+end
222+oputc 10
223+oflush
224+end
225+global cbufx uchar 1
226+fn main to int
227+make = eq argcnt 2
228+if make
229+r = argread 1 cbufx 0 1
230+if or ne r 1 ne get cbufx 0 43
231+ret 2
232+end
233+else
234+if ne argcnt 1
235+ret 2
236+end
237+ockload 1
238+put og 6 1
239+obload 4 get og 0
240+ggopen
241+ggupw
242+end
243+for i ulong 24
244+put sid 0 add 80000 rem mul i 7213 19001
245+for j ulong len spath
246+put spath j qbyte 1 j
247+end
248+if make
249+fd = fopen spath 0 len spath 193 384
250+if lt fd 0
251+ret 80
252+end
253+v = add 100 rem add mul i 373 167 900
254+put qdata 0 uchar add 48 div v 100
255+put qdata 1 uchar add 48 rem div v 10 10
256+put qdata 2 uchar add 48 rem v 10
257+n = write int fd qdata 0 3
258+cl = fclose int fd
259+if ne n 3
260+ret 81
261+end
262+else
263+qcase 1 6
264+end
265+end
266+if not make
267+r = nvstop
268+end
269+ret 0
270+end

pack/LICENSE.runtime

new file mode 100644
@@ -0,0 +1,661 @@
1+ GNU AFFERO GENERAL PUBLIC LICENSE
2+ Version 3, 19 November 2007
3+
4+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5+ Everyone is permitted to copy and distribute verbatim copies
6+ of this license document, but changing it is not allowed.
7+
8+ Preamble
9+
10+ The GNU Affero General Public License is a free, copyleft license for
11+software and other kinds of works, specifically designed to ensure
12+cooperation with the community in the case of network server software.
13+
14+ The licenses for most software and other practical works are designed
15+to take away your freedom to share and change the works. By contrast,
16+our General Public Licenses are intended to guarantee your freedom to
17+share and change all versions of a program--to make sure it remains free
18+software for all its users.
19+
20+ When we speak of free software, we are referring to freedom, not
21+price. Our General Public Licenses are designed to make sure that you
22+have the freedom to distribute copies of free software (and charge for
23+them if you wish), that you receive source code or can get it if you
24+want it, that you can change the software or use pieces of it in new
25+free programs, and that you know you can do these things.
26+
27+ Developers that use our General Public Licenses protect your rights
28+with two steps: (1) assert copyright on the software, and (2) offer
29+you this License which gives you legal permission to copy, distribute
30+and/or modify the software.
31+
32+ A secondary benefit of defending all users' freedom is that
33+improvements made in alternate versions of the program, if they
34+receive widespread use, become available for other developers to
35+incorporate. Many developers of free software are heartened and
36+encouraged by the resulting cooperation. However, in the case of
37+software used on network servers, this result may fail to come about.
38+The GNU General Public License permits making a modified version and
39+letting the public access it on a server without ever releasing its
40+source code to the public.
41+
42+ The GNU Affero General Public License is designed specifically to
43+ensure that, in such cases, the modified source code becomes available
44+to the community. It requires the operator of a network server to
45+provide the source code of the modified version running there to the
46+users of that server. Therefore, public use of a modified version, on
47+a publicly accessible server, gives the public access to the source
48+code of the modified version.
49+
50+ An older license, called the Affero General Public License and
51+published by Affero, was designed to accomplish similar goals. This is
52+a different license, not a version of the Affero GPL, but Affero has
53+released a new version of the Affero GPL which permits relicensing under
54+this license.
55+
56+ The precise terms and conditions for copying, distribution and
57+modification follow.
58+
59+ TERMS AND CONDITIONS
60+
61+ 0. Definitions.
62+
63+ "This License" refers to version 3 of the GNU Affero General Public License.
64+
65+ "Copyright" also means copyright-like laws that apply to other kinds of
66+works, such as semiconductor masks.
67+
68+ "The Program" refers to any copyrightable work licensed under this
69+License. Each licensee is addressed as "you". "Licensees" and
70+"recipients" may be individuals or organizations.
71+
72+ To "modify" a work means to copy from or adapt all or part of the work
73+in a fashion requiring copyright permission, other than the making of an
74+exact copy. The resulting work is called a "modified version" of the
75+earlier work or a work "based on" the earlier work.
76+
77+ A "covered work" means either the unmodified Program or a work based
78+on the Program.
79+
80+ To "propagate" a work means to do anything with it that, without
81+permission, would make you directly or secondarily liable for
82+infringement under applicable copyright law, except executing it on a
83+computer or modifying a private copy. Propagation includes copying,
84+distribution (with or without modification), making available to the
85+public, and in some countries other activities as well.
86+
87+ To "convey" a work means any kind of propagation that enables other
88+parties to make or receive copies. Mere interaction with a user through
89+a computer network, with no transfer of a copy, is not conveying.
90+
91+ An interactive user interface displays "Appropriate Legal Notices"
92+to the extent that it includes a convenient and prominently visible
93+feature that (1) displays an appropriate copyright notice, and (2)
94+tells the user that there is no warranty for the work (except to the
95+extent that warranties are provided), that licensees may convey the
96+work under this License, and how to view a copy of this License. If
97+the interface presents a list of user commands or options, such as a
98+menu, a prominent item in the list meets this criterion.
99+
100+ 1. Source Code.
101+
102+ The "source code" for a work means the preferred form of the work
103+for making modifications to it. "Object code" means any non-source
104+form of a work.
105+
106+ A "Standard Interface" means an interface that either is an official
107+standard defined by a recognized standards body, or, in the case of
108+interfaces specified for a particular programming language, one that
109+is widely used among developers working in that language.
110+
111+ The "System Libraries" of an executable work include anything, other
112+than the work as a whole, that (a) is included in the normal form of
113+packaging a Major Component, but which is not part of that Major
114+Component, and (b) serves only to enable use of the work with that
115+Major Component, or to implement a Standard Interface for which an
116+implementation is available to the public in source code form. A
117+"Major Component", in this context, means a major essential component
118+(kernel, window system, and so on) of the specific operating system
119+(if any) on which the executable work runs, or a compiler used to
120+produce the work, or an object code interpreter used to run it.
121+
122+ The "Corresponding Source" for a work in object code form means all
123+the source code needed to generate, install, and (for an executable
124+work) run the object code and to modify the work, including scripts to
125+control those activities. However, it does not include the work's
126+System Libraries, or general-purpose tools or generally available free
127+programs which are used unmodified in performing those activities but
128+which are not part of the work. For example, Corresponding Source
129+includes interface definition files associated with source files for
130+the work, and the source code for shared libraries and dynamically
131+linked subprograms that the work is specifically designed to require,
132+such as by intimate data communication or control flow between those
133+subprograms and other parts of the work.
134+
135+ The Corresponding Source need not include anything that users
136+can regenerate automatically from other parts of the Corresponding
137+Source.
138+
139+ The Corresponding Source for a work in source code form is that
140+same work.
141+
142+ 2. Basic Permissions.
143+
144+ All rights granted under this License are granted for the term of
145+copyright on the Program, and are irrevocable provided the stated
146+conditions are met. This License explicitly affirms your unlimited
147+permission to run the unmodified Program. The output from running a
148+covered work is covered by this License only if the output, given its
149+content, constitutes a covered work. This License acknowledges your
150+rights of fair use or other equivalent, as provided by copyright law.
151+
152+ You may make, run and propagate covered works that you do not
153+convey, without conditions so long as your license otherwise remains
154+in force. You may convey covered works to others for the sole purpose
155+of having them make modifications exclusively for you, or provide you
156+with facilities for running those works, provided that you comply with
157+the terms of this License in conveying all material for which you do
158+not control copyright. Those thus making or running the covered works
159+for you must do so exclusively on your behalf, under your direction
160+and control, on terms that prohibit them from making any copies of
161+your copyrighted material outside their relationship with you.
162+
163+ Conveying under any other circumstances is permitted solely under
164+the conditions stated below. Sublicensing is not allowed; section 10
165+makes it unnecessary.
166+
167+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
168+
169+ No covered work shall be deemed part of an effective technological
170+measure under any applicable law fulfilling obligations under article
171+11 of the WIPO copyright treaty adopted on 20 December 1996, or
172+similar laws prohibiting or restricting circumvention of such
173+measures.
174+
175+ When you convey a covered work, you waive any legal power to forbid
176+circumvention of technological measures to the extent such circumvention
177+is effected by exercising rights under this License with respect to
178+the covered work, and you disclaim any intention to limit operation or
179+modification of the work as a means of enforcing, against the work's
180+users, your or third parties' legal rights to forbid circumvention of
181+technological measures.
182+
183+ 4. Conveying Verbatim Copies.
184+
185+ You may convey verbatim copies of the Program's source code as you
186+receive it, in any medium, provided that you conspicuously and
187+appropriately publish on each copy an appropriate copyright notice;
188+keep intact all notices stating that this License and any
189+non-permissive terms added in accord with section 7 apply to the code;
190+keep intact all notices of the absence of any warranty; and give all
191+recipients a copy of this License along with the Program.
192+
193+ You may charge any price or no price for each copy that you convey,
194+and you may offer support or warranty protection for a fee.
195+
196+ 5. Conveying Modified Source Versions.
197+
198+ You may convey a work based on the Program, or the modifications to
199+produce it from the Program, in the form of source code under the
200+terms of section 4, provided that you also meet all of these conditions:
201+
202+ a) The work must carry prominent notices stating that you modified
203+ it, and giving a relevant date.
204+
205+ b) The work must carry prominent notices stating that it is
206+ released under this License and any conditions added under section
207+ 7. This requirement modifies the requirement in section 4 to
208+ "keep intact all notices".
209+
210+ c) You must license the entire work, as a whole, under this
211+ License to anyone who comes into possession of a copy. This
212+ License will therefore apply, along with any applicable section 7
213+ additional terms, to the whole of the work, and all its parts,
214+ regardless of how they are packaged. This License gives no
215+ permission to license the work in any other way, but it does not
216+ invalidate such permission if you have separately received it.
217+
218+ d) If the work has interactive user interfaces, each must display
219+ Appropriate Legal Notices; however, if the Program has interactive
220+ interfaces that do not display Appropriate Legal Notices, your
221+ work need not make them do so.
222+
223+ A compilation of a covered work with other separate and independent
224+works, which are not by their nature extensions of the covered work,
225+and which are not combined with it such as to form a larger program,
226+in or on a volume of a storage or distribution medium, is called an
227+"aggregate" if the compilation and its resulting copyright are not
228+used to limit the access or legal rights of the compilation's users
229+beyond what the individual works permit. Inclusion of a covered work
230+in an aggregate does not cause this License to apply to the other
231+parts of the aggregate.
232+
233+ 6. Conveying Non-Source Forms.
234+
235+ You may convey a covered work in object code form under the terms
236+of sections 4 and 5, provided that you also convey the
237+machine-readable Corresponding Source under the terms of this License,
238+in one of these ways:
239+
240+ a) Convey the object code in, or embodied in, a physical product
241+ (including a physical distribution medium), accompanied by the
242+ Corresponding Source fixed on a durable physical medium
243+ customarily used for software interchange.
244+
245+ b) Convey the object code in, or embodied in, a physical product
246+ (including a physical distribution medium), accompanied by a
247+ written offer, valid for at least three years and valid for as
248+ long as you offer spare parts or customer support for that product
249+ model, to give anyone who possesses the object code either (1) a
250+ copy of the Corresponding Source for all the software in the
251+ product that is covered by this License, on a durable physical
252+ medium customarily used for software interchange, for a price no
253+ more than your reasonable cost of physically performing this
254+ conveying of source, or (2) access to copy the
255+ Corresponding Source from a network server at no charge.
256+
257+ c) Convey individual copies of the object code with a copy of the
258+ written offer to provide the Corresponding Source. This
259+ alternative is allowed only occasionally and noncommercially, and
260+ only if you received the object code with such an offer, in accord
261+ with subsection 6b.
262+
263+ d) Convey the object code by offering access from a designated
264+ place (gratis or for a charge), and offer equivalent access to the
265+ Corresponding Source in the same way through the same place at no
266+ further charge. You need not require recipients to copy the
267+ Corresponding Source along with the object code. If the place to
268+ copy the object code is a network server, the Corresponding Source
269+ may be on a different server (operated by you or a third party)
270+ that supports equivalent copying facilities, provided you maintain
271+ clear directions next to the object code saying where to find the
272+ Corresponding Source. Regardless of what server hosts the
273+ Corresponding Source, you remain obligated to ensure that it is
274+ available for as long as needed to satisfy these requirements.
275+
276+ e) Convey the object code using peer-to-peer transmission, provided
277+ you inform other peers where the object code and Corresponding
278+ Source of the work are being offered to the general public at no
279+ charge under subsection 6d.
280+
281+ A separable portion of the object code, whose source code is excluded
282+from the Corresponding Source as a System Library, need not be
283+included in conveying the object code work.
284+
285+ A "User Product" is either (1) a "consumer product", which means any
286+tangible personal property which is normally used for personal, family,
287+or household purposes, or (2) anything designed or sold for incorporation
288+into a dwelling. In determining whether a product is a consumer product,
289+doubtful cases shall be resolved in favor of coverage. For a particular
290+product received by a particular user, "normally used" refers to a
291+typical or common use of that class of product, regardless of the status
292+of the particular user or of the way in which the particular user
293+actually uses, or expects or is expected to use, the product. A product
294+is a consumer product regardless of whether the product has substantial
295+commercial, industrial or non-consumer uses, unless such uses represent
296+the only significant mode of use of the product.
297+
298+ "Installation Information" for a User Product means any methods,
299+procedures, authorization keys, or other information required to install
300+and execute modified versions of a covered work in that User Product from
301+a modified version of its Corresponding Source. The information must
302+suffice to ensure that the continued functioning of the modified object
303+code is in no case prevented or interfered with solely because
304+modification has been made.
305+
306+ If you convey an object code work under this section in, or with, or
307+specifically for use in, a User Product, and the conveying occurs as
308+part of a transaction in which the right of possession and use of the
309+User Product is transferred to the recipient in perpetuity or for a
310+fixed term (regardless of how the transaction is characterized), the
311+Corresponding Source conveyed under this section must be accompanied
312+by the Installation Information. But this requirement does not apply
313+if neither you nor any third party retains the ability to install
314+modified object code on the User Product (for example, the work has
315+been installed in ROM).
316+
317+ The requirement to provide Installation Information does not include a
318+requirement to continue to provide support service, warranty, or updates
319+for a work that has been modified or installed by the recipient, or for
320+the User Product in which it has been modified or installed. Access to a
321+network may be denied when the modification itself materially and
322+adversely affects the operation of the network or violates the rules and
323+protocols for communication across the network.
324+
325+ Corresponding Source conveyed, and Installation Information provided,
326+in accord with this section must be in a format that is publicly
327+documented (and with an implementation available to the public in
328+source code form), and must require no special password or key for
329+unpacking, reading or copying.
330+
331+ 7. Additional Terms.
332+
333+ "Additional permissions" are terms that supplement the terms of this
334+License by making exceptions from one or more of its conditions.
335+Additional permissions that are applicable to the entire Program shall
336+be treated as though they were included in this License, to the extent
337+that they are valid under applicable law. If additional permissions
338+apply only to part of the Program, that part may be used separately
339+under those permissions, but the entire Program remains governed by
340+this License without regard to the additional permissions.
341+
342+ When you convey a copy of a covered work, you may at your option
343+remove any additional permissions from that copy, or from any part of
344+it. (Additional permissions may be written to require their own
345+removal in certain cases when you modify the work.) You may place
346+additional permissions on material, added by you to a covered work,
347+for which you have or can give appropriate copyright permission.
348+
349+ Notwithstanding any other provision of this License, for material you
350+add to a covered work, you may (if authorized by the copyright holders of
351+that material) supplement the terms of this License with terms:
352+
353+ a) Disclaiming warranty or limiting liability differently from the
354+ terms of sections 15 and 16 of this License; or
355+
356+ b) Requiring preservation of specified reasonable legal notices or
357+ author attributions in that material or in the Appropriate Legal
358+ Notices displayed by works containing it; or
359+
360+ c) Prohibiting misrepresentation of the origin of that material, or
361+ requiring that modified versions of such material be marked in
362+ reasonable ways as different from the original version; or
363+
364+ d) Limiting the use for publicity purposes of names of licensors or
365+ authors of the material; or
366+
367+ e) Declining to grant rights under trademark law for use of some
368+ trade names, trademarks, or service marks; or
369+
370+ f) Requiring indemnification of licensors and authors of that
371+ material by anyone who conveys the material (or modified versions of
372+ it) with contractual assumptions of liability to the recipient, for
373+ any liability that these contractual assumptions directly impose on
374+ those licensors and authors.
375+
376+ All other non-permissive additional terms are considered "further
377+restrictions" within the meaning of section 10. If the Program as you
378+received it, or any part of it, contains a notice stating that it is
379+governed by this License along with a term that is a further
380+restriction, you may remove that term. If a license document contains
381+a further restriction but permits relicensing or conveying under this
382+License, you may add to a covered work material governed by the terms
383+of that license document, provided that the further restriction does
384+not survive such relicensing or conveying.
385+
386+ If you add terms to a covered work in accord with this section, you
387+must place, in the relevant source files, a statement of the
388+additional terms that apply to those files, or a notice indicating
389+where to find the applicable terms.
390+
391+ Additional terms, permissive or non-permissive, may be stated in the
392+form of a separately written license, or stated as exceptions;
393+the above requirements apply either way.
394+
395+ 8. Termination.
396+
397+ You may not propagate or modify a covered work except as expressly
398+provided under this License. Any attempt otherwise to propagate or
399+modify it is void, and will automatically terminate your rights under
400+this License (including any patent licenses granted under the third
401+paragraph of section 11).
402+
403+ However, if you cease all violation of this License, then your
404+license from a particular copyright holder is reinstated (a)
405+provisionally, unless and until the copyright holder explicitly and
406+finally terminates your license, and (b) permanently, if the copyright
407+holder fails to notify you of the violation by some reasonable means
408+prior to 60 days after the cessation.
409+
410+ Moreover, your license from a particular copyright holder is
411+reinstated permanently if the copyright holder notifies you of the
412+violation by some reasonable means, this is the first time you have
413+received notice of violation of this License (for any work) from that
414+copyright holder, and you cure the violation prior to 30 days after
415+your receipt of the notice.
416+
417+ Termination of your rights under this section does not terminate the
418+licenses of parties who have received copies or rights from you under
419+this License. If your rights have been terminated and not permanently
420+reinstated, you do not qualify to receive new licenses for the same
421+material under section 10.
422+
423+ 9. Acceptance Not Required for Having Copies.
424+
425+ You are not required to accept this License in order to receive or
426+run a copy of the Program. Ancillary propagation of a covered work
427+occurring solely as a consequence of using peer-to-peer transmission
428+to receive a copy likewise does not require acceptance. However,
429+nothing other than this License grants you permission to propagate or
430+modify any covered work. These actions infringe copyright if you do
431+not accept this License. Therefore, by modifying or propagating a
432+covered work, you indicate your acceptance of this License to do so.
433+
434+ 10. Automatic Licensing of Downstream Recipients.
435+
436+ Each time you convey a covered work, the recipient automatically
437+receives a license from the original licensors, to run, modify and
438+propagate that work, subject to this License. You are not responsible
439+for enforcing compliance by third parties with this License.
440+
441+ An "entity transaction" is a transaction transferring control of an
442+organization, or substantially all assets of one, or subdividing an
443+organization, or merging organizations. If propagation of a covered
444+work results from an entity transaction, each party to that
445+transaction who receives a copy of the work also receives whatever
446+licenses to the work the party's predecessor in interest had or could
447+give under the previous paragraph, plus a right to possession of the
448+Corresponding Source of the work from the predecessor in interest, if
449+the predecessor has it or can get it with reasonable efforts.
450+
451+ You may not impose any further restrictions on the exercise of the
452+rights granted or affirmed under this License. For example, you may
453+not impose a license fee, royalty, or other charge for exercise of
454+rights granted under this License, and you may not initiate litigation
455+(including a cross-claim or counterclaim in a lawsuit) alleging that
456+any patent claim is infringed by making, using, selling, offering for
457+sale, or importing the Program or any portion of it.
458+
459+ 11. Patents.
460+
461+ A "contributor" is a copyright holder who authorizes use under this
462+License of the Program or a work on which the Program is based. The
463+work thus licensed is called the contributor's "contributor version".
464+
465+ A contributor's "essential patent claims" are all patent claims
466+owned or controlled by the contributor, whether already acquired or
467+hereafter acquired, that would be infringed by some manner, permitted
468+by this License, of making, using, or selling its contributor version,
469+but do not include claims that would be infringed only as a
470+consequence of further modification of the contributor version. For
471+purposes of this definition, "control" includes the right to grant
472+patent sublicenses in a manner consistent with the requirements of
473+this License.
474+
475+ Each contributor grants you a non-exclusive, worldwide, royalty-free
476+patent license under the contributor's essential patent claims, to
477+make, use, sell, offer for sale, import and otherwise run, modify and
478+propagate the contents of its contributor version.
479+
480+ In the following three paragraphs, a "patent license" is any express
481+agreement or commitment, however denominated, not to enforce a patent
482+(such as an express permission to practice a patent or covenant not to
483+sue for patent infringement). To "grant" such a patent license to a
484+party means to make such an agreement or commitment not to enforce a
485+patent against the party.
486+
487+ If you convey a covered work, knowingly relying on a patent license,
488+and the Corresponding Source of the work is not available for anyone
489+to copy, free of charge and under the terms of this License, through a
490+publicly available network server or other readily accessible means,
491+then you must either (1) cause the Corresponding Source to be so
492+available, or (2) arrange to deprive yourself of the benefit of the
493+patent license for this particular work, or (3) arrange, in a manner
494+consistent with the requirements of this License, to extend the patent
495+license to downstream recipients. "Knowingly relying" means you have
496+actual knowledge that, but for the patent license, your conveying the
497+covered work in a country, or your recipient's use of the covered work
498+in a country, would infringe one or more identifiable patents in that
499+country that you have reason to believe are valid.
500+
501+ If, pursuant to or in connection with a single transaction or
502+arrangement, you convey, or propagate by procuring conveyance of, a
503+covered work, and grant a patent license to some of the parties
504+receiving the covered work authorizing them to use, propagate, modify
505+or convey a specific copy of the covered work, then the patent license
506+you grant is automatically extended to all recipients of the covered
507+work and works based on it.
508+
509+ A patent license is "discriminatory" if it does not include within
510+the scope of its coverage, prohibits the exercise of, or is
511+conditioned on the non-exercise of one or more of the rights that are
512+specifically granted under this License. You may not convey a covered
513+work if you are a party to an arrangement with a third party that is
514+in the business of distributing software, under which you make payment
515+to the third party based on the extent of your activity of conveying
516+the work, and under which the third party grants, to any of the
517+parties who would receive the covered work from you, a discriminatory
518+patent license (a) in connection with copies of the covered work
519+conveyed by you (or copies made from those copies), or (b) primarily
520+for and in connection with specific products or compilations that
521+contain the covered work, unless you entered into that arrangement,
522+or that patent license was granted, prior to 28 March 2007.
523+
524+ Nothing in this License shall be construed as excluding or limiting
525+any implied license or other defenses to infringement that may
526+otherwise be available to you under applicable patent law.
527+
528+ 12. No Surrender of Others' Freedom.
529+
530+ If conditions are imposed on you (whether by court order, agreement or
531+otherwise) that contradict the conditions of this License, they do not
532+excuse you from the conditions of this License. If you cannot convey a
533+covered work so as to satisfy simultaneously your obligations under this
534+License and any other pertinent obligations, then as a consequence you may
535+not convey it at all. For example, if you agree to terms that obligate you
536+to collect a royalty for further conveying from those to whom you convey
537+the Program, the only way you could satisfy both those terms and this
538+License would be to refrain entirely from conveying the Program.
539+
540+ 13. Remote Network Interaction; Use with the GNU General Public License.
541+
542+ Notwithstanding any other provision of this License, if you modify the
543+Program, your modified version must prominently offer all users
544+interacting with it remotely through a computer network (if your version
545+supports such interaction) an opportunity to receive the Corresponding
546+Source of your version by providing access to the Corresponding Source
547+from a network server at no charge, through some standard or customary
548+means of facilitating copying of software. This Corresponding Source
549+shall include the Corresponding Source for any work covered by version 3
550+of the GNU General Public License that is incorporated pursuant to the
551+following paragraph.
552+
553+ Notwithstanding any other provision of this License, you have
554+permission to link or combine any covered work with a work licensed
555+under version 3 of the GNU General Public License into a single
556+combined work, and to convey the resulting work. The terms of this
557+License will continue to apply to the part which is the covered work,
558+but the work with which it is combined will remain governed by version
559+3 of the GNU General Public License.
560+
561+ 14. Revised Versions of this License.
562+
563+ The Free Software Foundation may publish revised and/or new versions of
564+the GNU Affero General Public License from time to time. Such new versions
565+will be similar in spirit to the present version, but may differ in detail to
566+address new problems or concerns.
567+
568+ Each version is given a distinguishing version number. If the
569+Program specifies that a certain numbered version of the GNU Affero General
570+Public License "or any later version" applies to it, you have the
571+option of following the terms and conditions either of that numbered
572+version or of any later version published by the Free Software
573+Foundation. If the Program does not specify a version number of the
574+GNU Affero General Public License, you may choose any version ever published
575+by the Free Software Foundation.
576+
577+ If the Program specifies that a proxy can decide which future
578+versions of the GNU Affero General Public License can be used, that proxy's
579+public statement of acceptance of a version permanently authorizes you
580+to choose that version for the Program.
581+
582+ Later license versions may give you additional or different
583+permissions. However, no additional obligations are imposed on any
584+author or copyright holder as a result of your choosing to follow a
585+later version.
586+
587+ 15. Disclaimer of Warranty.
588+
589+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
590+APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
591+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
592+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
593+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
594+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
595+IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
596+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
597+
598+ 16. Limitation of Liability.
599+
600+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
601+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
602+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
603+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
604+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
605+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
606+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
607+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
608+SUCH DAMAGES.
609+
610+ 17. Interpretation of Sections 15 and 16.
611+
612+ If the disclaimer of warranty and limitation of liability provided
613+above cannot be given local legal effect according to their terms,
614+reviewing courts shall apply local law that most closely approximates
615+an absolute waiver of all civil liability in connection with the
616+Program, unless a warranty or assumption of liability accompanies a
617+copy of the Program in return for a fee.
618+
619+ END OF TERMS AND CONDITIONS
620+
621+ How to Apply These Terms to Your New Programs
622+
623+ If you develop a new program, and you want it to be of the greatest
624+possible use to the public, the best way to achieve this is to make it
625+free software which everyone can redistribute and change under these terms.
626+
627+ To do so, attach the following notices to the program. It is safest
628+to attach them to the start of each source file to most effectively
629+state the exclusion of warranty; and each file should have at least
630+the "copyright" line and a pointer to where the full notice is found.
631+
632+ <one line to give the program's name and a brief idea of what it does.>
633+ Copyright (C) <year> <name of author>
634+
635+ This program is free software: you can redistribute it and/or modify
636+ it under the terms of the GNU Affero General Public License as published by
637+ the Free Software Foundation, either version 3 of the License, or
638+ (at your option) any later version.
639+
640+ This program is distributed in the hope that it will be useful,
641+ but WITHOUT ANY WARRANTY; without even the implied warranty of
642+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643+ GNU Affero General Public License for more details.
644+
645+ You should have received a copy of the GNU Affero General Public License
646+ along with this program. If not, see <https://www.gnu.org/licenses/>.
647+
648+Also add information on how to contact you by electronic and paper mail.
649+
650+ If your software can interact with users remotely through a computer
651+network, you should also make sure that it provides a way for users to
652+get its source. For example, if your program is a web application, its
653+interface could display a "Source" link that leads users to an archive
654+of the code. There are many ways you could offer source, and different
655+solutions will be better for different programs; see section 13 for the
656+specific requirements.
657+
658+ You should also get your employer (if you work as a programmer) or school,
659+if any, to sign a "copyright disclaimer" for the program, if necessary.
660+For more information on this, and how to apply and follow the GNU AGPL, see
661+<https://www.gnu.org/licenses/>.

pack/README.md

new file mode 100644
@@ -0,0 +1,9 @@
1+# Omega round40 for NVIDIA GB10
2+
3+This is the first-party model component of the Sovereign Intelligence Pack. The specialist takes a short structured prompt and predicts the next tool request or answer. Its native backend targets one NVIDIA GB10 on ARM64 Linux. The archive includes the weights, tokenizer, complete Omega runner source, compiled runner, and a read-only tool example.
4+
5+After extracting, enter the directory and run `sha256sum -c SHA256SUMS`. Run `./demo` to request a read of a bundled sample file. The example reads that file and passes its actual contents back to the model. It checks the next predicted tool request. For custom structured prompts use `./model < prompt.txt`. See `deployment/workbench/runner.om` in the source repository for the prompt format. Unpacking or running the demo installs no service.
6+
7+Build on ARM64 Linux using GNU coreutils, tar, gzip, the seeded compiler and the pinned round40 checkpoint and merges file. Run `pack/build /path/to/omc /path/to/model.ck /path/to/merges.bin /path/to/new-archive.tar.gz`. The builder verifies its inputs against `pack/inputs.sha256`. The weights and tokenizer are supplied separately from the Git source. The compiler reads `pack/round40.om`; its arithmetic and kernels come from the deployed round40 sampler. Model and tokenizer paths are relative to the extracted directory. This backend does not load third-party weights.
8+
9+The native runner source carries AGPL-3.0-only notices. Its full text is included in `LICENSE.runtime`. The surrounding model repository carries the Apache license in `LICENSE.source`. The two license files describe source code.

pack/build

new file mode 100755
@@ -0,0 +1,28 @@
1+#!/bin/sh
2+# Build a deterministic archive from already acquired, hash-pinned inputs.
3+set -eu
4+if [ "$#" -ne 4 ]; then
5+ echo 'Usage: pack/build OMC MODEL.ck MERGES.bin NEW-ARCHIVE.tar.gz' >&2
6+ exit 2
7+fi
8+root=$(CDPATH='' cd -P "$(dirname "$0")" && pwd)
9+tmp=$(mktemp -d)
10+trap 'rm -rf "$tmp"' EXIT
11+pkg=$tmp/omega-round40-gb10
12+mkdir -p "$pkg/jobs"
13+cp "$1" "$tmp/omc"
14+cp "$2" "$pkg/model.ck"
15+cp "$3" "$pkg/merges.bin"
16+(cd "$tmp" && sha256sum -c "$root/inputs.sha256")
17+cp "$root/round40.om" "$root/demo" "$root/model" "$root/README.md" "$root/LICENSE.runtime" "$pkg/"
18+cp "$root/jobs/item76666.om" "$pkg/jobs/"
19+cp "$root/../LICENSE" "$pkg/LICENSE.source"
20+"$tmp/omc" < "$pkg/round40.om" > "$pkg/omega-tool-model"
21+chmod 755 "$pkg/omega-tool-model" "$pkg/demo"
22+(cd "$pkg" && sha256sum LICENSE.runtime LICENSE.source README.md demo jobs/item76666.om merges.bin model model.ck omega-tool-model round40.om > SHA256SUMS)
23+# Link the finished file atomically. An existing destination is preserved.
24+out=$(mktemp "$(dirname "$4")/.round40.XXXXXXXXXX")
25+trap 'rm -rf "$tmp"; rm -f "$out"' EXIT
26+tar --format=ustar --sort=name --mtime=@0 --mode='u+rwX,go+rX,go-w' --owner=0 --group=0 --numeric-owner -C "$tmp" -cf "$tmp/bundle.tar" omega-round40-gb10
27+gzip -n -1 -c "$tmp/bundle.tar" > "$out"
28+ln -T "$out" "$4"

pack/demo

new file mode 100755
@@ -0,0 +1,17 @@
1+#!/bin/sh
2+# The only authorized operation in this example is reading the bundled fixture.
3+set -eu
4+cd -P "$(dirname "$0")"
5+request=$(./model --demo)
6+case "$request" in
7+ '{"tool":"read_file","path":"jobs/item76666.om"}') ;;
8+ *) printf 'Unrecognized model request: %s\n' "$request" >&2; exit 1 ;;
9+esac
10+printf 'Model requested %s\n' "$request"
11+source_text=$(cat jobs/item76666.om)
12+reply=$(printf '%s\n' 'Task: Fix the error in this file and confirm its goal.' \
13+ 'File: jobs/item76666.om' \
14+ 'Goal: calc starts with x and adds 4 exactly 3 times in a for loop. main calls calc with 70.' \
15+ 'State: read' 'Source:' "$source_text" 'Tool result: Read succeeded.' 'Answer:' | ./model)
16+printf 'Model replied %s\n' "$reply"
17+test "$reply" = '{"tool":"omega_check","path":"jobs/item76666.om"}'

pack/inputs.sha256

new file mode 100644
@@ -0,0 +1,3 @@
1+2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f omc
2+7c7b5b5260f2d9453b6076b64e7f23e88341d96498584f2dfaa229c31ad2e346 omega-round40-gb10/model.ck
3+73a29baf97619a746a21232c403bf08af71a76e48b3b2cde6d64dab766083882 omega-round40-gb10/merges.bin

pack/jobs/item76666.om

new file mode 100644
@@ -0,0 +1,10 @@
1+fn calc x int to int
2+s = x
3+for i ulong 2
4+s = add s int 4
5+end
6+ret s
7+end
8+fn main to int
9+ret calc int 70
10+end

pack/model

new file mode 100755
@@ -0,0 +1,18 @@
1+#!/bin/sh
2+set -eu
3+cd -P "$(dirname "$0")"
4+if [ "$(uname -s)/$(uname -m)" != Linux/aarch64 ]; then
5+ echo 'This model runner requires ARM64 Linux on NVIDIA GB10.' >&2
6+ exit 2
7+fi
8+# This backend opens /dev/nvidia0 directly. Only admit the single-GPU topology tested.
9+if ! command -v nvidia-smi >/dev/null 2>&1; then
10+ echo "A working NVIDIA driver with nvidia-smi is required." >&2
11+ exit 2
12+fi
13+gpu=$(nvidia-smi --query-gpu=name --format=csv,noheader)
14+if [ "$gpu" != 'NVIDIA GB10' ]; then
15+ echo 'The round40 backend requires one NVIDIA GB10 GPU.' >&2
16+ exit 2
17+fi
18+exec ./omega-tool-model "$@"

pack/round40.om

new file mode 100644
@@ -0,0 +1,11473 @@
1+note SPDX-License-Identifier: AGPL-3.0-only
2+note omegallm math.om (aien-train#52): first-party f64 exp and log, splitmix64, FNV-1a 64,
3+note little-endian byte packing and the stdout writer. First unit; see docs/omegallm/README.md.
4+note Constants (Wolfram, 2026-11-01):
5+note splitmix64: IntegerString[11400714819323198485,16] -> 9e3779b97f4a7c15,
6+note IntegerString[13787848793156543929,16] -> bf58476d1ce4e5b9,
7+note IntegerString[10723151780598845931,16] -> 94d049bb133111eb.
8+note FNV-1a 64: IntegerString[14695981039346656037,16] -> cbf29ce484222325, prime 100000001b3.
9+note 1/ln2: N[1/Log[2]] bits 4609176140021203710 -> 1.4426950408889634.
10+note ln2 hi = 0x3FE62E42FEE00000 (low 21 bits zero, so k*hi is exact for |k| < 2^20)
11+note = 0.6931471803691238; lo = N[Log[2]-hi] -> 1.9082149292705877*^-10.
12+note overflow: N[Log[$MaxMachineNumber]] -> 709.782712893384; underflow to 0 below
13+note N[Log[2^-1075]] -> -745.1332191019412.
14+note 1/k! for k=2..13 and 1/(2k+1) for k=1..11: Table[ToString[N[1/k!],InputForm],{k,2,13}],
15+note Table[ToString[N[1/(2k+1)],InputForm],{k,1,11}] (values below).
16+note Taylor remainder of exp at |r| <= ln2/2: N[0.3466^14/14!] -> 4.14*^-18 (degree 13).
17+note atanh series remainder at s <= 3-2Sqrt[2]: N[(3-2Sqrt[2])^2]^11/23 -> 6.25*^-19 (to s^23).
18+note bit patterns: 2^52 = 4503599627370496, +inf 9218868437227405312, -inf 18442240474082181120,
19+note NaN 9221120237041090560, exponent 1023 field 4607182418800017408, 2^54 = 18014398509481984.
20+
21+const OMOUT ulong 4096
22+global oout uchar 4096
23+global oon ulong 1
24+note og: run geometry shared by all units (slots listed in model.om).
25+global og ulong 64
26+
27+fn smx x ulong to ulong
28+z = add x 11400714819323198485
29+z = mul xor z shr z 30 13787848793156543929
30+z = mul xor z shr z 27 10723151780598845931
31+ret xor z shr z 31
32+end
33+
34+note e^x for every f64 x. Cody-Waite reduction x = k ln2 + r with |r| <= ln2/2, then the Taylor
35+note polynomial of degree 13 in Horner form, then scaling by 2^k1 * 2^k2 (k1 = k/2) so every
36+note intermediate is normal and a subnormal result is rounded once.
37+fn fexp x f64 to f64
38+if ne x x
39+ret x
40+end
41+if gt x 709.782712893384
42+ret bitcast f64 ulong 9218868437227405312
43+end
44+if lt x -745.1332191019412
45+ret 0.0f64
46+end
47+kf = mul x 1.4426950408889634
48+if ge kf 0.0
49+kf = add kf 0.5
50+else
51+kf = sub kf 0.5
52+end
53+k = long kf
54+fk = f64 k
55+r = sub sub x mul fk 0.6931471803691238 mul fk 1.9082149292705877e-10
56+p = add mul r 1.6059043836821613e-10 2.08767569878681e-9
57+p = add mul p r 2.505210838544172e-8
58+p = add mul p r 2.755731922398589e-7
59+p = add mul p r 2.7557319223985893e-6
60+p = add mul p r 0.0000248015873015873
61+p = add mul p r 0.0001984126984126984
62+p = add mul p r 0.001388888888888889
63+p = add mul p r 0.008333333333333333
64+p = add mul p r 0.041666666666666664
65+p = add mul p r 0.16666666666666666
66+p = add mul p r 0.5
67+p = add mul p r 1.0
68+p = add mul p r 1.0
69+k1 = div k 2
70+k2 = sub k k1
71+s1 = bitcast f64 shl ulong add k1 1023 52
72+s2 = bitcast f64 shl ulong add k2 1023 52
73+ret mul mul p s1 s2
74+end
75+
76+note natural log for every f64 x: NaN for x < 0 or NaN, -inf for 0, +inf for +inf. x = m 2^e with
77+note m in [sqrt(1/2), sqrt 2]; log m = 2 atanh(s), s = (m-1)/(m+1), series to s^23.
78+fn flog x f64 to f64
79+if ne x x
80+ret x
81+end
82+if lt x 0.0
83+ret bitcast f64 ulong 9221120237041090560
84+end
85+if eq x 0.0
86+ret bitcast f64 ulong 18442240474082181120
87+end
88+b = bitcast ulong x
89+if eq b 9218868437227405312
90+ret x
91+end
92+e = long 0
93+if lt b 4503599627370496
94+b = bitcast ulong mul x 18014398509481984.0
95+e = long -54
96+end
97+e = add e sub long shr b 52 1023
98+m = bitcast f64 or and b 4503599627370495 4607182418800017408
99+if gt m 1.4142135623730951
100+m = mul m 0.5
101+e = add e 1
102+end
103+f = sub m 1.0
104+s = div f add f 2.0
105+z = mul s s
106+q = add mul z 0.043478260869565216 0.047619047619047616
107+q = add mul q z 0.05263157894736842
108+q = add mul q z 0.058823529411764705
109+q = add mul q z 0.06666666666666667
110+q = add mul q z 0.07692307692307693
111+q = add mul q z 0.09090909090909091
112+q = add mul q z 0.1111111111111111
113+q = add mul q z 0.14285714285714285
114+q = add mul q z 0.2
115+q = add mul q z 0.3333333333333333
116+q = add mul q z 1.0
117+lm = mul add s s q
118+fe = f64 e
119+ret add mul fe 0.6931471803691238 add lm mul fe 1.9082149292705877e-10
120+end
121+
122+note round(v * 10^6) half away from zero (CONTRACT section 7).
123+fn rmic v f64 to long
124+x = mul v 1000000.0
125+q = long x
126+fr = sub x f64 q
127+if ge fr 0.5
128+q = add q 1
129+end
130+if le fr -0.5
131+q = sub q 1
132+end
133+ret q
134+end
135+
136+fn oputc c ulong to void
137+n = get oon 0
138+if ge n OMOUT
139+oflush
140+n = ulong 0
141+end
142+put oout n uchar and c 255
143+put oon 0 add n 1
144+end
145+
146+fn oputu v ulong to void
147+d = new uchar 24 0
148+n = ulong 0
149+x = v
150+while true bound 21
151+put d n uchar add rem x 10 48
152+n = add n 1
153+x = div x 10
154+if eq x 0
155+break
156+end
157+end
158+for i ulong n
159+oputc ulong get d sub sub n 1 i
160+end
161+end
162+
163+fn oputl v long to void
164+if lt v 0
165+oputc 45
166+if eq v -9223372036854775808
167+oputu 9223372036854775808
168+else
169+oputu ulong neg v
170+end
171+else
172+oputu ulong v
173+end
174+end
175+
176+note 16 lowercase hex digits.
177+fn oputx v ulong to void
178+for i ulong 16
179+h = and shr v mul sub 15 i 4 15
180+if lt h 10
181+oputc add h 48
182+else
183+oputc add h 87
184+end
185+end
186+end
187+note SPDX-License-Identifier: AGPL-3.0-only
188+note omegallm data.om (aien-train#52): argument paths, whole-file data reads, prep (CONTRACT
189+note section 4), the splitmix64 batch rule and the add-one byte baselines. Needs math.om before it.
190+note A data file lives in two byte pools (od0 then od1), 2^25 bytes in all; a byte is read by odb.
191+const ODHALF ulong 16777216
192+const ODCAP ulong 33554432
193+global od0 uchar 16777216
194+global od1 uchar 16777216
195+global odn ulong 1
196+global oarg uchar 4100
197+global ochk uchar 65536
198+global obx ulong 16384
199+global oby ulong 16384
200+global owb0 uchar 65536
201+global owb1 uchar 65536
202+global owbn ulong 2
203+global ocnt ulong 65536
204+global ocnu ulong 256
205+global ocnr ulong 256
206+global oseps uchar 16
207+note v1 (CONTRACT-V1 section 3): the window that the platform's positioned read oprd fills; prep,
208+note the bpe commands and the windowed data reads all use it.
209+const OPWCAP ulong 2097152
210+global opw uchar 2097152
211+
212+note exit codes of this unit: 2 usage, 20 data file empty or too short for T, 21 data file over
213+note 2^25 bytes, 70 I/O.
214+
215+note argument i into oarg; returns its length (1 .. 4096) or exits 2.
216+fn oargp i ulong to ulong
217+r = oargr i
218+if or le r 0 gt r 4096
219+exit 2
220+end
221+ret ulong r
222+end
223+
224+note argv[i] as an unsigned decimal of 1 .. 19 digits.
225+fn oargu i ulong to ulong
226+n = oargp i
227+if gt n 19
228+exit 2
229+end
230+v = ulong 0
231+for k ulong n
232+c = ulong get oarg k
233+if or lt c 48 gt c 57
234+exit 2
235+end
236+v = add mul v 10 sub c 48
237+end
238+ret v
239+end
240+
241+fn odb i ulong to ulong
242+if lt i ODHALF
243+ret ulong get od0 i
244+end
245+ret ulong get od1 sub i ODHALF
246+end
247+
248+fn odset i ulong c ulong to void
249+if lt i ODHALF
250+put od0 i uchar c
251+else
252+put od1 sub i ODHALF uchar c
253+end
254+end
255+
256+note prep output: two buffered writers (k = 0 TRAIN, k = 1 HELD); owopen, owflush and owclose are
257+note the platform unit's (lio.om, eio.om).
258+fn owbyte k ulong c ulong to void
259+n = get owbn k
260+if ge n 65536
261+owflush k
262+n = ulong 0
263+end
264+if eq k 0
265+put owb0 n uchar c
266+else
267+put owb1 n uchar c
268+end
269+put owbn k add n 1
270+end
271+
272+note 1 when the separator <|endoftext|> starts at opw[p] (p + 13 <= n).
273+fn oissep p ulong n ulong to bool
274+if gt add p 13 n
275+ret false
276+end
277+for k ulong 13
278+if ne get opw add p k get oseps k
279+ret false
280+end
281+end
282+ret true
283+end
284+
285+fn owsp c ulong to bool
286+ret or or eq c 9 eq c 10 or eq c 13 eq c 32
287+end
288+
289+note Emit story opw[a, e) after trimming whitespace; an empty story is dropped. st[0..4] count
290+note stories, train stories, held stories, train bytes, held bytes. A story over 999999 bytes is
291+note refused with exit 24 (cmdprep refuses it already while scanning; every loop here is bounded).
292+fn ostory a ulong e ulong st edit ulong 8 to void
293+if gt sub e a 999999
294+exit 24
295+end
296+lo = a
297+hi = e
298+while lt lo hi bound 999999
299+if not owsp ulong get opw lo
300+break
301+end
302+lo = add lo 1
303+end
304+while gt hi lo bound 999999
305+if not owsp ulong get opw sub hi 1
306+break
307+end
308+hi = sub hi 1
309+end
310+if lt lo hi
311+k = ulong 0
312+if eq rem get st 0 10 9
313+k = ulong 1
314+end
315+put st 0 add get st 0 1
316+put st add k 1 add get st add k 1 1
317+for i ulong sub hi lo
318+owbyte k ulong get opw add lo i
319+end
320+owbyte k 0
321+put st add k 3 add add get st add k 3 sub hi lo 1
322+end
323+end
324+
325+note `<|endoftext|>` = 60 124 101 110 100 111 102 116 101 120 116 124 62.
326+fn oseptab to void
327+put oseps 0 60
328+put oseps 1 124
329+put oseps 2 101
330+put oseps 3 110
331+put oseps 4 100
332+put oseps 5 111
333+put oseps 6 102
334+put oseps 7 116
335+put oseps 8 101
336+put oseps 9 120
337+put oseps 10 116
338+put oseps 11 124
339+put oseps 12 62
340+end
341+
342+note Stream window over argv[ai] (prep and the bpe commands): opw holds the file's bytes
343+note [wb, wb + wn) with opst = wb, wn, eof (1 once a refill came back short). opstart fills it from
344+note byte 0; opshift a moves opw[a, wn) to the front and refills the rest of the window.
345+global opst ulong 4
346+fn opstart ai ulong to void
347+wn = oprd ai 0 0 OPWCAP
348+put opst 0 0
349+put opst 1 wn
350+put opst 2 0
351+if lt wn OPWCAP
352+put opst 2 1
353+end
354+end
355+
356+fn opshift ai ulong a ulong to void
357+wn = get opst 1
358+cnt = sub wn a
359+i = ulong 0
360+for o ulong 3
361+lim = mul add o 1 999999
362+if gt lim cnt
363+lim = cnt
364+end
365+while lt i lim bound 999999
366+put opw i get opw add a i
367+i = add i 1
368+end
369+end
370+wb = add get opst 0 a
371+wn = sub wn a
372+r = oprd ai add wb wn wn sub OPWCAP wn
373+put opst 0 wb
374+put opst 1 add wn r
375+put opst 2 0
376+if lt r sub OPWCAP wn
377+put opst 2 1
378+end
379+end
380+
381+note prep IN TRAIN HELD (CONTRACT-V1 section 3): IN is read as a stream through the window opw.
382+note The current story starts at opw[a]; p scans. When the separator test at p needs bytes past
383+note the window, opshift moves [a, wn) to the front and refills. A story whose raw length passes
384+note 999999 is exit 24 as soon as the scan shows it (v0 refused the same story, after the same
385+note earlier stories, in ostory); so a moved story keeps at most 1000012 bytes and every refill
386+note reads at least 1097140. Each inner pass advances p by 1 or 13 or ends the scan: 999999 x 999999
387+note passes cover any IN below 2^39 bytes.
388+fn cmdprep to void
389+if ne oargc 5
390+exit 2
391+end
392+oseptab
393+opstart 2
394+wn = get opst 1
395+eof = ne get opst 2 0
396+owopen 0 3
397+owopen 1 4
398+st = new ulong 8 0
399+a = ulong 0
400+p = ulong 0
401+dn = false
402+while not dn bound 999999
403+q = ulong 0
404+while and not dn lt q 999999 bound 999999
405+q = add q 1
406+if and gt add p 13 wn not eof
407+opshift 2 a
408+p = sub p a
409+a = ulong 0
410+wn = get opst 1
411+eof = ne get opst 2 0
412+end
413+if ge p wn
414+dn = true
415+else
416+if eq get opw p 60
417+if oissep p wn
418+ostory a p st
419+p = add p 13
420+a = p
421+continue
422+end
423+end
424+p = add p 1
425+if gt sub p a 999999
426+exit 24
427+end
428+end
429+end
430+end
431+ostory a wn st
432+owclose 0
433+owclose 1
434+oputc 80
435+for i ulong 5
436+oputc 32
437+oputu get st i
438+end
439+oputc 10
440+oflush
441+end
442+
443+note Batch for step s (CONTRACT section 4): x and y for b = 0..B-1 into obx/oby (stride T); N, the
444+note data length in tokens, is odn[0] (odopen).
445+note OMUTB is the target offset: 1 in the product, 2 in mutant b (targets shifted by one).
446+const OMUTB ulong 1
447+fn obatch s ulong to void
448+bb = get og 6
449+tt = get og 1
450+n = get odn 0
451+sd = get og 40
452+for b ulong bb
453+o = rem smx xor sd mul add mul s bb b 11400714819323198485 sub n tt
454+obrow b o
455+end
456+end
457+
458+note v1 data file (CONTRACT-V1 section 3): argument ai holds bytes when V = 256 and u16
459+note little-endian tokens when V > 256 (og 49 = bytes per token, og 47 = the argument). Sets
460+note odn[0] = N tokens; exit 21 on an odd byte length at V > 256, 20 when N < T + 1. Rows are read
461+note through oprd when used, so a token >= V is refused (25) only in a row actually read.
462+fn odopen ai ulong to void
463+w = ulong 1
464+if gt get og 0 256
465+w = 2
466+end
467+nb = ofsz ai
468+if ne rem nb w 0
469+exit 21
470+end
471+put og 47 ai
472+put og 49 w
473+put odn 0 div nb w
474+if le get odn 0 get og 1
475+exit 20
476+end
477+end
478+
479+note token k of the window last read by odrow. OMUTE: 1 in the product (u16 little-endian); 0 in
480+note mutant e (big-endian), which the referee comparison must catch.
481+const OMUTE ulong 1
482+fn odtk k ulong to ulong
483+if eq get og 49 1
484+ret ulong get opw k
485+end
486+lo = ulong get opw mul k 2
487+hi = ulong get opw add mul k 2 1
488+if eq OMUTE 0
489+ret or shl lo 8 hi
490+end
491+ret or shl hi 8 lo
492+end
493+
494+note read tokens o .. o + n - 1 (fewer at the end of the file; the rest read as 0) into opw;
495+note exit 25 when a token read is >= V
496+fn odrow o ulong n ulong to void
497+w = get og 49
498+nb = mul n w
499+for i ulong nb
500+put opw i 0
501+end
502+r = oprd get og 47 mul o w 0 nb
503+m = div r w
504+v = get og 0
505+for k ulong m
506+if ge odtk k v
507+exit 25
508+end
509+end
510+end
511+
512+fn obrow b ulong o ulong to void
513+tt = get og 1
514+odrow o add tt OMUTB
515+for t ulong tt
516+put obx add mul b tt t odtk t
517+put oby add mul b tt t odtk add t OMUTB
518+end
519+end
520+
521+note base TRAIN HELD (CONTRACT section 7 and agreed reading A5): unigram and bigram add-one
522+note counts over all TRAIN bytes and all N-1 adjacent TRAIN pairs; scored on HELD targets 1..M-1.
523+fn cmdbase to void
524+if ne oargc 4
525+exit 2
526+end
527+n = odread 2
528+if lt n 2
529+exit 20
530+end
531+for o ulong 64
532+for i ulong 524288
533+j = add mul o 524288 i
534+if ge j n
535+break
536+end
537+obfit j
538+end
539+end
540+m = odread 3
541+if lt m 2
542+exit 20
543+end
544+su = 0.0f64
545+sb = 0.0f64
546+dn = f64 add n 256
547+for o2 ulong 64
548+for i2 ulong 524288
549+j2 = add mul o2 524288 i2
550+if ge j2 m
551+break
552+end
553+if gt j2 0
554+su = add su obsu j2 dn
555+sb = add sb obsb j2
556+end
557+end
558+end
559+fm = f64 sub m 1
560+oputc 66
561+oputc 32
562+oputl rmic div div neg su fm 0.6931471805599453
563+oputc 32
564+oputl rmic div div neg sb fm 0.6931471805599453
565+oputc 10
566+oflush
567+end
568+
569+note count TRAIN byte j and the pair (j-1, j)
570+fn obfit j ulong to void
571+c = odb j
572+put ocnu c add get ocnu c 1
573+if gt j 0
574+pv = odb sub j 1
575+q = add mul pv 256 c
576+put ocnt q add get ocnt q 1
577+put ocnr pv add get ocnr pv 1
578+end
579+end
580+
581+note log of the unigram probability (c+1)/(N+256) of HELD byte j
582+fn obsu j ulong dn f64 to f64
583+ret flog div f64 add get ocnu odb j 1 dn
584+end
585+
586+note log of the bigram probability (c+1)/(row+256) of HELD pair (j-1, j)
587+fn obsb j ulong to f64
588+pv = odb sub j 1
589+q = add mul pv 256 odb j
590+ret flog div f64 add get ocnt q 1 f64 add get ocnr pv 256
591+end
592+note SPDX-License-Identifier: AGPL-3.0-only
593+note omegallm lpool.om (aien-train#52, CONTRACT-V3 section 2): the bank-B pools of the Linux targets
594+note (aarch64, gb10, x86linux), concatenated after data.om and before model.om; eio targets use
595+note epool.om. Sizes at the CONTRACT-V3 maxima (V 4096, T 256, D 384, H 16, F 1536, L 16), Wolfram:
596+note oww parameters of layers LA .. L-1: (16 - 7) LSTR = 9 1770240 -> 15932160 (LA = 7)
597+note ogb BLK values past OGC: P - OGC = 29995392 - 15833472 -> 14161920
598+note oab activations of layers LAA .. L-1: (16 - 6) ASTR = 10 1573376 -> 15733760 (LAA = 6)
599+const OWWN ulong 15932160
600+global oww f64 15932160
601+const OGBN ulong 14161920
602+global ogb f64 14161920
603+const OABN ulong 15733760
604+global oab f64 15733760
605+note SPDX-License-Identifier: AGPL-3.0-only
606+note omegallm model.om (aien-train#52): geometry, forward pass and hand-derived backward pass
607+note (CONTRACT sections 1-3, CONTRACT-V1 section 1). Needs math.om and data.om before it.
608+note
609+note f64 values live in banked pools (one omc global holds at most 16777216 elements, and all
610+note globals together at most 2^30 bytes, omc error 23; CONTRACT-V3 section 2):
611+note ow parameter bank A: [0, SB) of the CONTRACT section 2 order, then a one-layer window of
612+note LSTR values at og[36] and the final gain gf at og[37]; oww (lpool.om/epool.om) bank B holds
613+note layers LA .. L-1 at (l - LA) LSTR. When P <= OWC, SB = GF, LA = L and ow is the v2 layout.
614+note Every index outside the kernels is virtual (the section 2 order); owg/owp map it, and opx
615+note maps a hot index of bank A or the staged layer (og[48]).
616+note the gradient pool [0, 2P): GRD [0, P) in the platform cold store (ocsg/ocsp), BLK [P, 2P) in
617+note ogr[j] below OGC and ogb[j - OGC] from OGC (j = i - P), reached through ogrg/ogrp/ogra
618+note the Adam pool [0, 2P) (omvg/omvp): m at 0, v at P, at cold-store index P + i
619+note oa activations: layers below LAA at l ASTR, later layers in two windows at AW + (l mod 2) ASTR
620+note flushed to bank B oab at (l - LAA) ASTR after their forward and staged back before their
621+note backward; HF and the backward temporaries follow. When END <= OAC, LAA = L (v2 layout).
622+note opa attention probabilities PA[l][n][t][j] of every layer
623+note og slots:
624+note 0 V 1 T 2 D 3 H 4 F 5 L 6 B 7 E=D/H 8 P 9 CT (positions in use, <= T)
625+note 10 GRD (base in the gradient pool of the gradient being accumulated: 0, or P for BLK) 11 MOM
626+note (0 in the Adam pool) 12 VEL (P) 13 ACT (0 in oa) 14 ASTR (per-layer activation stride) 15 HF
627+note 16 SF 17 Z 18 PR 19 DH 20 DA 21 DQ 22 DK 23 DU 24 DO 25 DZ 26 DF 27 TMP 28 END
628+note 29 PSTR (per-layer PA stride H T T in opa) 30 WPE 31 LAY0 32 LSTR (per-layer parameter
629+note stride) 33 GF 34 SB (first virtual index of bank B) 35 LA (first layer in bank B) 36 window base
630+note in ow 37 gf in ow 38 LAA (first layer in the activation windows) 39 AW (activation window
631+note base) 48 the layer staged in the parameter window (L: none)
632+note 40 seed S 41 step 42 TOTAL 43 lr bits 44 G (gradient blocks) 45 K (workers)
633+note 46 checkpoint magic of the run (1 OLLMCK01, 2 OLLMCK02)
634+note Per layer activations (T rows of width D unless noted): HIN, A, Q, K, U, O, HMID, BN, S1[T],
635+note S2[T], FP[T][F] (pre-relu), RL[T][F]. Every sum runs over its index ascending.
636+note Sizes at the CONTRACT-V2 maxima (V 4096, T 256, D 384, H 16, F 1536, L 8), Wolfram:
637+note P = V D + T D + L (4 D^2 + 2 D F + 2 D) + D -> 15833472
638+note ASTR = 8 T D + 2 T + 2 T F -> 1573376; END = L ASTR + 10 T D + T + T V + T F + 4096 -> 15016192
639+note and at the CONTRACT-V3 maxima (L 16): P 29995392, PA = L H T T -> 16777216; the bank-B sizes
640+note are in lpool.om. ogeom refuses any layout past a pool.
641+note Bank A capacities: OWC parameters, OGC BLK values, OAC activations (the bank-crossing test
642+note build of tests/omegallm/build.sh shrinks all three).
643+const OWC ulong 15833472
644+global ow f64 15833472
645+const OGC ulong 15833472
646+global ogr f64 15833472
647+const OAC ulong 15016192
648+global oa f64 15016192
649+const OPAN ulong 16777216
650+global opa f64 16777216
651+
652+note gradient pool element i (i < 2 P)
653+fn ogrg i ulong to f64
654+p = get og 8
655+if lt i p
656+ret ocsg i
657+end
658+j = sub i p
659+if lt j OGC
660+ret get ogr j
661+end
662+ret get ogb sub j OGC
663+end
664+
665+fn ogrp i ulong v f64 to void
666+p = get og 8
667+if lt i p
668+ocsp i v
669+ret
670+end
671+j = sub i p
672+if lt j OGC
673+put ogr j v
674+else
675+put ogb sub j OGC v
676+end
677+end
678+
679+note gradient pool element i += a
680+fn ogra i ulong a f64 to void
681+p = get og 8
682+if lt i p
683+ocsp i add ocsg i a
684+ret
685+end
686+j = sub i p
687+if lt j OGC
688+put ogr j add get ogr j a
689+else
690+k = sub j OGC
691+put ogb k add get ogb k a
692+end
693+end
694+
695+note Adam pool element i (i < 2 P), cold-store index P + i
696+fn omvg i ulong to f64
697+ret ocsg add get og 8 i
698+end
699+
700+fn omvp i ulong v f64 to void
701+ocsp add get og 8 i v
702+end
703+
704+note parameter at virtual index i (bank A, bank B or gf), and its store
705+fn owg i ulong to f64
706+sb = get og 34
707+if lt i sb
708+ret get ow i
709+end
710+g = get og 33
711+if ge i g
712+ret get ow add get og 37 sub i g
713+end
714+ret get oww sub i sb
715+end
716+
717+fn owp i ulong v f64 to void
718+sb = get og 34
719+if lt i sb
720+put ow i v
721+ret
722+end
723+g = get og 33
724+if ge i g
725+put ow add get og 37 sub i g v
726+ret
727+end
728+put oww sub i sb v
729+end
730+
731+note ow index of virtual index i for the kernels: bank A as is, gf at og[37], a bank-B layer only
732+note when it is the staged layer og[48] (else exit 16, a missing ostw)
733+fn opx i ulong to ulong
734+if lt i get og 34
735+ret i
736+end
737+g = get og 33
738+if ge i g
739+ret add get og 37 sub i g
740+end
741+l = div sub i get og 31 get og 32
742+if ne l get og 48
743+exit 16
744+end
745+ret add get og 36 sub i opg1 l
746+end
747+
748+note Mutant y (wrong bank-B layer: ostw stages layer LA's block for every layer): 1 in the product,
749+note 0 in the mutant.
750+const OMUTY ulong 1
751+
752+const OCH ulong 999999
753+
754+note stage bank-B layer l into the parameter window (layers below LA live in bank A)
755+fn ostw l ulong to void
756+la = get og 35
757+if lt l la
758+ret
759+end
760+n = get og 32
761+s = mul sub l la n
762+if eq OMUTY 0
763+s = 0
764+end
765+ostwc get og 36 s n
766+put og 48 l
767+end
768+
769+note ow[wb + i] = oww[s + i] for i < n (chunked: a loop runs at most 999999 passes)
770+fn ostwc wb ulong s ulong n ulong to void
771+for q ulong ocn n
772+i = mul q OCH
773+ie = oce q n
774+while lt i ie bound 999999
775+put ow add wb i get oww add s i
776+i = add i 1
777+end
778+end
779+end
780+
781+note copy layer l's activation block between its window and bank B oab (to oab when fo)
782+fn oaflip l ulong fo bool to void
783+la = get og 38
784+if lt l la
785+ret
786+end
787+n = get og 14
788+s = mul sub l la n
789+oaflc oahin l s n fo
790+end
791+
792+note oab[s + i] = oa[w + i] for i < n when fo, else the reverse
793+fn oaflc w ulong s ulong n ulong fo bool to void
794+if fo
795+oafo w s n
796+else
797+oafi w s n
798+end
799+end
800+
801+fn oafo w ulong s ulong n ulong to void
802+for q ulong ocn n
803+i = mul q OCH
804+ie = oce q n
805+while lt i ie bound 999999
806+put oab add s i get oa add w i
807+i = add i 1
808+end
809+end
810+end
811+
812+fn oafi w ulong s ulong n ulong to void
813+for q ulong ocn n
814+i = mul q OCH
815+ie = oce q n
816+while lt i ie bound 999999
817+put oa add w i get oab add s i
818+i = add i 1
819+end
820+end
821+end
822+
823+note TMP holds V logits (sample) or T attention dots
824+const OTMP ulong 4096
825+
826+note Mutant a (attention backward sign flip in the dK term): 1 in the product, 0 in the mutant.
827+const OMUTA ulong 1
828+
829+note Configure og from og[0..6]; false when a shape is zero, over the maximum, or D mod H != 0, or
830+note when the layout does not fit the pools.
831+fn ogeom to bool
832+if not ogok
833+ret false
834+end
835+t = get og 1
836+d = get og 2
837+put og 7 div d get og 3
838+u = mul 4 mul d d
839+u = add u mul 2 mul d get og 4
840+u = add u mul 2 d
841+put og 32 u
842+put og 30 mul get og 0 d
843+u = mul t d
844+put og 31 add get og 30 u
845+u = mul get og 5 get og 32
846+put og 33 add get og 31 u
847+u = add get og 33 d
848+put og 8 u
849+put og 9 t
850+put og 10 0
851+put og 11 0
852+put og 12 u
853+put og 13 0
854+put og 29 mul mul get og 3 t t
855+u = mul t d
856+put og 14 oastr u
857+if not ogwb
858+ret false
859+end
860+if not ogab u
861+ret false
862+end
863+p = get og 8
864+if gt p add OGC OGBN
865+ret false
866+end
867+if gt mul 3 p ocsinit
868+ret false
869+end
870+ret le mul get og 5 get og 29 OPAN
871+end
872+
873+note parameter banks: v2 layout when P <= OWC, else LA layers in bank A, the window and gf after
874+note them, layers LA .. L-1 in oww
875+fn ogwb to bool
876+ll = get og 5
877+g = get og 33
878+put og 34 g
879+put og 35 ll
880+put og 36 g
881+put og 37 g
882+put og 48 ll
883+if le get og 8 OWC
884+ret true
885+end
886+ls = get og 32
887+nd = add add get og 31 ls get og 2
888+if lt OWC nd
889+ret false
890+end
891+la = div sub OWC nd ls
892+if gt mul sub ll la ls OWWN
893+ret false
894+end
895+sb = add get og 31 mul la ls
896+put og 34 sb
897+put og 35 la
898+put og 36 sb
899+put og 37 add sb ls
900+ret true
901+end
902+
903+note activation banks: v2 layout when END <= OAC, else LAA layers, two windows, then HF and the
904+note backward temporaries
905+fn ogab td ulong to bool
906+ll = get og 5
907+st = get og 14
908+put og 38 ll
909+put og 39 mul ll st
910+ogeom3 td mul ll st
911+ogeom2 td
912+if le get og 28 OAC
913+ret true
914+end
915+w2 = add sub get og 28 get og 15 mul 2 st
916+if lt OAC w2
917+ret false
918+end
919+la = div sub OAC w2 st
920+if gt mul sub ll la st OABN
921+ret false
922+end
923+aw = mul la st
924+put og 38 la
925+put og 39 aw
926+ogeom3 td add aw mul 2 st
927+ogeom2 td
928+ret le get og 28 OAC
929+end
930+
931+fn ogeom3 td ulong hf ulong to void
932+put og 15 hf
933+put og 16 add get og 15 td
934+put og 17 add get og 16 get og 1
935+put og 18 add get og 17 td
936+end
937+
938+note per-layer activation stride: 8 T D + 2 T + 2 T F
939+fn oastr td ulong to ulong
940+t = get og 1
941+a = add mul 8 td mul 2 t
942+ret add a mul mul 2 t get og 4
943+end
944+
945+note shape limits (CONTRACT-V3 section 1): every shape >= 1, 256 <= V <= 4096, T <= 256,
946+note D <= 384, H <= 16, F <= 1536, L <= 16, B <= 64, D mod H = 0
947+fn ogok to bool
948+for i ulong 7
949+if eq get og i 0
950+ret false
951+end
952+end
953+if or lt get og 0 256 gt get og 0 4096
954+ret false
955+end
956+if or gt get og 1 256 gt get og 2 384
957+ret false
958+end
959+if or gt get og 3 16 gt get og 4 1536
960+ret false
961+end
962+if or gt get og 5 16 gt get og 6 64
963+ret false
964+end
965+ret eq rem get og 2 get og 3 0
966+end
967+
968+fn ogeom2 td ulong to void
969+t = get og 1
970+put og 19 add get og 18 mul t get og 0
971+put og 20 add get og 19 td
972+put og 21 add get og 20 td
973+put og 22 add get og 21 td
974+put og 23 add get og 22 td
975+put og 24 add get og 23 td
976+put og 25 add get og 24 td
977+put og 26 add get og 25 td
978+put og 27 add get og 26 mul t get og 4
979+put og 28 add get og 27 OTMP
980+end
981+
982+note parameter offsets of layer l
983+fn opg1 l ulong to ulong
984+ret add get og 31 mul l get og 32
985+end
986+fn opwq l ulong to ulong
987+ret add opg1 l get og 2
988+end
989+fn opwk l ulong to ulong
990+d = get og 2
991+ret add opwq l mul d d
992+end
993+fn opwv l ulong to ulong
994+d = get og 2
995+ret add opwk l mul d d
996+end
997+fn opwo l ulong to ulong
998+d = get og 2
999+ret add opwv l mul d d
1000+end
1001+fn opg2 l ulong to ulong
1002+d = get og 2
1003+ret add opwo l mul d d
1004+end
1005+fn opw1 l ulong to ulong
1006+ret add opg2 l get og 2
1007+end
1008+fn opw2 l ulong to ulong
1009+ret add opw1 l mul get og 2 get og 4
1010+end
1011+
1012+note activation offsets of layer l (layers from LAA in the two windows at AW)
1013+fn oahin l ulong to ulong
1014+if lt l get og 38
1015+ret add get og 13 mul l get og 14
1016+end
1017+ret add get og 39 mul rem l 2 get og 14
1018+end
1019+fn oatd n ulong to ulong
1020+ret mul n mul get og 1 get og 2
1021+end
1022+fn oaa l ulong to ulong
1023+ret add oahin l oatd 1
1024+end
1025+fn oaq l ulong to ulong
1026+ret add oahin l oatd 2
1027+end
1028+fn oak l ulong to ulong
1029+ret add oahin l oatd 3
1030+end
1031+fn oau l ulong to ulong
1032+ret add oahin l oatd 4
1033+end
1034+fn oao l ulong to ulong
1035+ret add oahin l oatd 5
1036+end
1037+fn oahm l ulong to ulong
1038+ret add oahin l oatd 6
1039+end
1040+fn oabn l ulong to ulong
1041+ret add oahin l oatd 7
1042+end
1043+fn oas1 l ulong to ulong
1044+ret add oahin l oatd 8
1045+end
1046+fn oas2 l ulong to ulong
1047+ret add oas1 l get og 1
1048+end
1049+fn oafp l ulong to ulong
1050+ret add oas2 l get og 1
1051+end
1052+fn oarl l ulong to ulong
1053+ret add oafp l mul get og 1 get og 4
1054+end
1055+note output rows of layer l: the next layer's HIN, or HF after the last layer
1056+fn oaout l ulong to ulong
1057+if lt add l 1 get og 5
1058+ret oahin add l 1
1059+end
1060+ret get og 15
1061+end
1062+
1063+note Kernels. The fast forms keep every v0 sum: each output is one accumulator that starts at
1064+note 0.0 and adds its terms in the v0 index order with the v0 expression (add s mul a w); four
1065+note outputs run side by side in separate accumulators, so every result has v0's bits. Results of
1066+note the four-wide blocks pass through oq.
1067+global oq f64 4
1068+
1069+note y[0..c) = x[0..r) . W[r][c] (overwrites y); x, y in oa, W in ow at virtual index wv
1070+fn kvm xo ulong wv ulong yo ulong r ulong c ulong to void
1071+wo = opx wv
1072+xe = add xo r
1073+j = ulong 0
1074+while le add j 4 c bound 999999
1075+kvmb xo xe add wo j c
1076+kput4 add yo j
1077+j = add j 4
1078+end
1079+while lt j c bound 4
1080+put oa add yo j kvm1 xo xe add wo j c
1081+j = add j 1
1082+end
1083+end
1084+
1085+note oa[yo..yo+4) = oq[0..4)
1086+fn kput4 yo ulong to void
1087+put oa yo get oq 0
1088+put oa add yo 1 get oq 1
1089+put oa add yo 2 get oq 2
1090+put oa add yo 3 get oq 3
1091+end
1092+
1093+note oq[m] = sum over xo <= i < xe of x[i] W[wi + m + (i - xo) c], m < 4
1094+fn kvmb xo ulong xe ulong wi ulong c ulong to void
1095+s0 = 0.0f64
1096+s1 = 0.0f64
1097+s2 = 0.0f64
1098+s3 = 0.0f64
1099+while lt xo xe bound 999999
1100+a = get oa xo
1101+s0 = add s0 mul a get ow wi
1102+s1 = add s1 mul a get ow add wi 1
1103+s2 = add s2 mul a get ow add wi 2
1104+s3 = add s3 mul a get ow add wi 3
1105+wi = add wi c
1106+xo = add xo 1
1107+end
1108+put oq 0 s0
1109+put oq 1 s1
1110+put oq 2 s2
1111+put oq 3 s3
1112+end
1113+
1114+note sum over xo <= i < xe of x[i] W[wi + (i - xo) c]
1115+fn kvm1 xo ulong xe ulong wi ulong c ulong to f64
1116+s = 0.0f64
1117+while lt xo xe bound 999999
1118+a = get oa xo
1119+s = add s mul a get ow wi
1120+wi = add wi c
1121+xo = add xo 1
1122+end
1123+ret s
1124+end
1125+
1126+note y[k] += sum_j dy[j] W[k][j] for k < r, j < c (y += dy . W^T); dy, y in oa, W in ow at virtual
1127+note index wv
1128+fn kvta dyo ulong wv ulong yo ulong r ulong c ulong to void
1129+wo = opx wv
1130+de = add dyo c
1131+k = ulong 0
1132+while le add k 4 r bound 999999
1133+kdot4 dyo de add wo mul k c c
1134+kacc4 add yo k
1135+k = add k 4
1136+end
1137+while lt k r bound 4
1138+a = add get oa add yo k kdot1 dyo de add wo mul k c
1139+put oa add yo k a
1140+k = add k 1
1141+end
1142+end
1143+
1144+note oa[yo + m] = oa[yo + m] + oq[m], m < 4
1145+fn kacc4 yo ulong to void
1146+put oa yo add get oa yo get oq 0
1147+q = add yo 1
1148+put oa q add get oa q get oq 1
1149+q = add yo 2
1150+put oa q add get oa q get oq 2
1151+q = add yo 3
1152+put oa q add get oa q get oq 3
1153+end
1154+
1155+note oq[m] = sum over xo <= i < xe of x[i] W[wi + m c + (i - xo)], m < 4; x in oa, W in ow
1156+fn kdot4 xo ulong xe ulong wi ulong c ulong to void
1157+s0 = 0.0f64
1158+s1 = 0.0f64
1159+s2 = 0.0f64
1160+s3 = 0.0f64
1161+w2 = add wi add c c
1162+while lt xo xe bound 999999
1163+a = get oa xo
1164+s0 = add s0 mul a get ow wi
1165+s1 = add s1 mul a get ow add wi c
1166+s2 = add s2 mul a get ow w2
1167+s3 = add s3 mul a get ow add w2 c
1168+wi = add wi 1
1169+w2 = add w2 1
1170+xo = add xo 1
1171+end
1172+put oq 0 s0
1173+put oq 1 s1
1174+put oq 2 s2
1175+put oq 3 s3
1176+end
1177+
1178+note sum over xo <= i < xe of x[i] W[wi + (i - xo)]; x in oa, W in ow
1179+fn kdot1 xo ulong xe ulong wi ulong to f64
1180+s = 0.0f64
1181+while lt xo xe bound 999999
1182+a = get oa xo
1183+s = add s mul a get ow wi
1184+wi = add wi 1
1185+xo = add xo 1
1186+end
1187+ret s
1188+end
1189+
1190+note kdot4 with both operands in oa
1191+fn kdot4a xo ulong xe ulong wi ulong c ulong to void
1192+s0 = 0.0f64
1193+s1 = 0.0f64
1194+s2 = 0.0f64
1195+s3 = 0.0f64
1196+w2 = add wi add c c
1197+while lt xo xe bound 999999
1198+a = get oa xo
1199+s0 = add s0 mul a get oa wi
1200+s1 = add s1 mul a get oa add wi c
1201+s2 = add s2 mul a get oa w2
1202+s3 = add s3 mul a get oa add w2 c
1203+wi = add wi 1
1204+w2 = add w2 1
1205+xo = add xo 1
1206+end
1207+put oq 0 s0
1208+put oq 1 s1
1209+put oq 2 s2
1210+put oq 3 s3
1211+end
1212+
1213+note kdot1 with both operands in oa
1214+fn kdot1a xo ulong xe ulong wi ulong to f64
1215+s = 0.0f64
1216+while lt xo xe bound 999999
1217+a = get oa xo
1218+s = add s mul a get oa wi
1219+wi = add wi 1
1220+xo = add xo 1
1221+end
1222+ret s
1223+end
1224+
1225+note G[k][j] += x[k] dy[j] for k < r, j < c (outer product into a gradient); x, dy in oa, G in ogr
1226+fn kger xo ulong dyo ulong go ulong r ulong c ulong to void
1227+for k ulong r
1228+kger1 get oa add xo k add go mul k c dyo c
1229+end
1230+end
1231+
1232+note G[gi + j] += xv dy[j] for j < c (elements independent, four per pass)
1233+fn kger1 xv f64 gi ulong yi ulong c ulong to void
1234+ye = add yi c
1235+while le add yi 4 ye bound 999999
1236+ogra gi mul xv get oa yi
1237+ogra add gi 1 mul xv get oa add yi 1
1238+ogra add gi 2 mul xv get oa add yi 2
1239+ogra add gi 3 mul xv get oa add yi 3
1240+gi = add gi 4
1241+yi = add yi 4
1242+end
1243+while lt yi ye bound 4
1244+ogra gi mul xv get oa yi
1245+gi = add gi 1
1246+yi = add yi 1
1247+end
1248+end
1249+
1250+note y[i] += a x[i]; x, y in oa
1251+fn kaxpy yo ulong xo ulong a f64 n ulong to void
1252+for i ulong n
1253+q = add yo i
1254+v = mul a get oa add xo i
1255+put oa q add get oa q v
1256+end
1257+end
1258+
1259+note y[i] += a x[i]; x in oa, y in the gradient pool
1260+fn kaxpg yo ulong xo ulong a f64 n ulong to void
1261+for i ulong n
1262+ogra add yo i mul a get oa add xo i
1263+end
1264+end
1265+
1266+fn ozero o ulong n ulong to void
1267+for i ulong n
1268+put oa add o i 0.0f64
1269+end
1270+end
1271+
1272+fn ozerog o ulong n ulong to void
1273+for q ulong ocn n
1274+i = mul q OCH
1275+ie = oce q n
1276+while lt i ie bound 999999
1277+ogrp add o i 0.0f64
1278+i = add i 1
1279+end
1280+end
1281+end
1282+
1283+note A loop runs at most 999999 passes (the while bound; a for over a name has the same guard), so
1284+note loops over P or more run in ocn n chunks of OCH; chunk q covers [q OCH, oce q n).
1285+fn ocn n ulong to ulong
1286+ret div add n 999998 OCH
1287+end
1288+fn oce q ulong n ulong to ulong
1289+e = mul add q 1 OCH
1290+if gt e n
1291+ret n
1292+end
1293+ret e
1294+end
1295+
1296+note y = rms(v, g): s = sqrt((sum v^2)/D + 1e-5) stored at so; y[i] = (v[i]/s) g[i]; g at virtual
1297+note index gv
1298+fn frms vo ulong gv ulong yo ulong so ulong to void
1299+go = opx gv
1300+d = get og 2
1301+ss = 0.0f64
1302+for i ulong d
1303+v = get oa add vo i
1304+ss = add ss mul v v
1305+end
1306+s = fsqrt add div ss f64 d 0.00001
1307+put oa so s
1308+for j ulong d
1309+v = div get oa add vo j s
1310+v = mul v get ow add go j
1311+put oa add yo j v
1312+end
1313+end
1314+
1315+note rms backward: n = v/s, dn = da g, c = (sum dn n)/D; dv = (dn - n c)/s added to dh;
1316+note dg += da n added to the gain's gradient. go is the gain's virtual index.
1317+global ofs f64 8
1318+fn brms vo ulong go ulong so ulong dao ulong dho ulong to void
1319+d = get og 2
1320+put ofs 0 get oa so
1321+gg = add get og 10 go
1322+go = opx go
1323+put ofs 1 div brmsc vo go dao get ofs 0 f64 d
1324+for j ulong d
1325+brms1 add vo j add go j add dao j add dho j add gg j
1326+end
1327+end
1328+
1329+note sum over i < D of (da[i] g[i]) (v[i]/s), ascending
1330+fn brmsc vo ulong go ulong dao ulong s f64 to f64
1331+c = 0.0f64
1332+for i ulong get og 2
1333+dn = get oa add dao i
1334+dn = mul dn get ow add go i
1335+v = div get oa add vo i s
1336+c = add c mul dn v
1337+end
1338+ret c
1339+end
1340+
1341+note one element of the rms backward; s = ofs[0], c = ofs[1]
1342+fn brms1 vp ulong gp ulong dap ulong dhp ulong ggp ulong to void
1343+s = get ofs 0
1344+n = div get oa vp s
1345+da = get oa dap
1346+ogra ggp mul da n
1347+dn = mul da get ow gp
1348+dn = div sub dn mul n get ofs 1 s
1349+put oa dhp add get oa dhp dn
1350+end
1351+
1352+note z[i] = a[i] + b[i] for i < n (all in oa)
1353+fn kadd3 zo ulong ao ulong bo ulong n ulong to void
1354+for i ulong n
1355+v = get oa add ao i
1356+v = add v get oa add bo i
1357+put oa add zo i v
1358+end
1359+end
1360+
1361+note z[i] = a[i] + b[i] for i < n (a, b in ow, z in oa)
1362+fn kadd3w zo ulong ao ulong bo ulong n ulong to void
1363+for i ulong n
1364+v = get ow add ao i
1365+v = add v get ow add bo i
1366+put oa add zo i v
1367+end
1368+end
1369+
1370+note h[t] = wte[x[t]] + wpe[t] for sequence b of the batch
1371+fn oemb b ulong to void
1372+d = get og 2
1373+tt = get og 1
1374+for t ulong get og 9
1375+wr = get obx add mul b tt t
1376+wr = mul wr d
1377+pr = add get og 30 mul t d
1378+kadd3w add oahin 0 mul t d wr pr d
1379+end
1380+end
1381+
1382+note offset in opa of PA[n][t][0] of layer l
1383+fn opar l ulong n ulong t ulong to ulong
1384+tt = get og 1
1385+p = add mul n tt t
1386+ret add mul l get og 29 mul p tt
1387+end
1388+
1389+note attention scores and softmax of head n, row t: PA[n][t][j] for j <= t
1390+fn fatts l ulong n ulong t ulong to void
1391+e = get og 7
1392+qo = add add oaq l mul t get og 2 mul n e
1393+ko = add oak l mul n e
1394+pa = opar l n t
1395+m = fattsk qo ko pa add t 1
1396+sm = osofp pa add t 1 m
1397+end
1398+
1399+note PA row: s[j] = q.k[j]/sqrt(E) for j < nj, k rows stepping by D; returns the max score
1400+fn fattsk qo ulong ko ulong pa ulong nj ulong to f64
1401+d = get og 2
1402+e = get og 7
1403+isq = fsqrt f64 e
1404+m = bitcast f64 ulong 18442240474082181120
1405+qe = add qo e
1406+j = ulong 0
1407+while le add j 4 nj bound 999999
1408+kdot4a qo qe ko d
1409+m = fattp4 add pa j isq m
1410+ko = add ko mul 4 d
1411+j = add j 4
1412+end
1413+while lt j nj bound 4
1414+s = div kdot1a qo qe ko isq
1415+put opa add pa j s
1416+m = fmax m s
1417+ko = add ko d
1418+j = add j 1
1419+end
1420+ret m
1421+end
1422+
1423+note PA[p + m] = oq[m] / isq for m < 4 in order; returns the running max
1424+fn fattp4 p ulong isq f64 m f64 to f64
1425+for i ulong 4
1426+s = div get oq i isq
1427+put opa add p i s
1428+m = fmax m s
1429+end
1430+ret m
1431+end
1432+
1433+note p = exp(x - m) / sum over n entries at o in oa (in place); returns sum
1434+fn osoft o ulong n ulong m f64 to f64
1435+sm = 0.0f64
1436+for j ulong n
1437+ev = fexp sub get oa add o j m
1438+put oa add o j ev
1439+sm = add sm ev
1440+end
1441+for k ulong n
1442+v = div get oa add o k sm
1443+put oa add o k v
1444+end
1445+ret sm
1446+end
1447+
1448+note osoft on opa
1449+fn osofp o ulong n ulong m f64 to f64
1450+sm = 0.0f64
1451+for j ulong n
1452+ev = fexp sub get opa add o j m
1453+put opa add o j ev
1454+sm = add sm ev
1455+end
1456+for k ulong n
1457+v = div get opa add o k sm
1458+put opa add o k v
1459+end
1460+ret sm
1461+end
1462+
1463+note o[t][nE + i] = sum_{j <= t} p[t][j] u[j][nE + i]
1464+fn fatto l ulong n ulong t ulong to void
1465+e = get og 7
1466+oo = add add oao l mul t get og 2 mul n e
1467+uo = add oau l mul n e
1468+pa = opar l n t
1469+ozero oo e
1470+fattok oo uo pa add t 1
1471+end
1472+
1473+note o[0..E) += p[j] u[j][0..E) for j < nj, u rows stepping by D
1474+fn fattok oo ulong uo ulong pa ulong nj ulong to void
1475+d = get og 2
1476+e = get og 7
1477+for j ulong nj
1478+kaxpy oo uo get opa add pa j e
1479+uo = add uo d
1480+end
1481+end
1482+
1483+fn olayf l ulong to void
1484+ostw l
1485+for t ulong get og 9
1486+olayf1 l t
1487+end
1488+for n ulong get og 3
1489+for t2 ulong get og 9
1490+fatts l n t2
1491+fatto l n t2
1492+end
1493+end
1494+for t3 ulong get og 9
1495+olayf2 l t3
1496+end
1497+oaflip l true
1498+end
1499+
1500+note row t: a = rms(h, g1); q, k, u = a.wq, a.wk, a.wv
1501+fn olayf1 l ulong t ulong to void
1502+d = get og 2
1503+r = mul t d
1504+a = add oaa l r
1505+frms add oahin l r opg1 l a add oas1 l t
1506+kvm a opwq l add oaq l r d d
1507+kvm a opwk l add oak l r d d
1508+kvm a opwv l add oau l r d d
1509+end
1510+
1511+note row t: h += o.wo; b = rms(h, g2); out = h + relu(b.w1).w2
1512+fn olayf2 l ulong t ulong to void
1513+d = get og 2
1514+f = get og 4
1515+r = mul t d
1516+hm = add oahm l r
1517+kvm add oao l r opwo l hm d d
1518+kadd3 hm add oahin l r hm d
1519+frms hm opg2 l add oabn l r add oas2 l t
1520+fp = add oafp l mul t f
1521+kvm add oabn l r opw1 l fp d f
1522+rl = add oarl l mul t f
1523+krelu rl fp f
1524+ou = add oaout l r
1525+kvm rl opw2 l ou f d
1526+kadd3 ou hm ou d
1527+end
1528+
1529+note y[j] = x[j] when x[j] > 0, else 0, for j < n
1530+fn krelu yo ulong xo ulong n ulong to void
1531+for j ulong n
1532+v = get oa add xo j
1533+if le v 0.0
1534+v = 0.0f64
1535+end
1536+put oa add yo j v
1537+end
1538+end
1539+
1540+note loss of row t: the logits at o (n entries) become probabilities; returns
1541+note log(sum exp(l - max)) - (l_y - max)
1542+fn olossrow o ulong n ulong y ulong to f64
1543+m = bitcast f64 ulong 18442240474082181120
1544+for j ulong n
1545+m = fmax m get oa add o j
1546+end
1547+ly = get oa add o y
1548+sm = osoft o n m
1549+ret sub flog sm sub ly m
1550+end
1551+
1552+note final rms, tied head and loss of sequence b; returns the mean loss over CT positions
1553+fn ohead b ulong to f64
1554+ls = 0.0f64
1555+for t ulong get og 9
1556+ls = add ls oheadr b t
1557+end
1558+ret div ls f64 get og 9
1559+end
1560+
1561+note row t of the head: z = rms(HF[t], gf); logits = z . wte^T; returns the row loss
1562+fn oheadr b ulong t ulong to f64
1563+d = get og 2
1564+r = mul t d
1565+z = add get og 17 r
1566+hf = add get og 15 r
1567+frms hf get og 33 z add get og 16 t
1568+pr = add get og 18 mul t get og 0
1569+klogit z pr get og 0 d
1570+y = get oby add mul b get og 1 t
1571+ret olossrow pr get og 0 y
1572+end
1573+
1574+note p[c] = z . wte[c] for c < n (wte rows of length d at offset 0 of ow; z, p in oa)
1575+fn klogit zo ulong po ulong n ulong d ulong to void
1576+ze = add zo d
1577+c = ulong 0
1578+while le add c 4 n bound 999999
1579+kdot4 zo ze mul c d d
1580+kput4 add po c
1581+c = add c 4
1582+end
1583+while lt c n bound 4
1584+put oa add po c kdot1 zo ze mul c d
1585+c = add c 1
1586+end
1587+end
1588+
1589+note ogkm[0] = 0: the backward ReLU mask is the sign of this pass's own FP (every command but
1590+note gcmp2); 1: offb calls ogmask, which gpu/gg.om defines for gcmp2 (the chip's FP sign) and
1591+note nogpu.om defines as a refusal on the CPU-only targets. ogkm[1] = the sequence in flight.
1592+global ogkm ulong 2
1593+
1594+note forward pass of sequence b (x in obx, y in oby); returns its mean loss
1595+fn ofwd b ulong to f64
1596+put ogkm 1 b
1597+oemb b
1598+for l ulong get og 5
1599+olayf l
1600+end
1601+ret ohead b
1602+end
1603+
1604+note backward of the head for row t: dl = (p - onehot(y)) sc; dwte += dl z; dz = dl . wte;
1605+note DH[t] = rms backward of HF[t].
1606+fn ohbwd b ulong t ulong sc f64 to void
1607+v = get og 0
1608+pr = add get og 18 mul t v
1609+y = get oby add mul b get og 1 t
1610+kdlog pr v y sc
1611+ohbwd2 pr t
1612+end
1613+
1614+note p[c] = (p[c] - [c = y]) sc for c < n
1615+fn kdlog po ulong n ulong y ulong sc f64 to void
1616+for c ulong n
1617+g = get oa add po c
1618+if eq c y
1619+g = sub g 1.0
1620+end
1621+put oa add po c mul g sc
1622+end
1623+end
1624+
1625+fn ohbwd2 pr ulong t ulong to void
1626+d = get og 2
1627+v = get og 0
1628+r = mul t d
1629+z = add get og 17 r
1630+kger pr z get og 10 v d
1631+dz = add get og 25 r
1632+kvm pr ulong 0 dz v d
1633+hf = add get og 15 r
1634+dh = add get og 19 r
1635+brms hf get og 33 add get og 16 t dz dh
1636+end
1637+
1638+note FFN backward of row t of layer l; DH[t] holds d(out) and gains the rms2 path
1639+fn offb l ulong t ulong to void
1640+f = get og 4
1641+dh = add get og 19 mul t get og 2
1642+df = add get og 26 mul t f
1643+ozero df f
1644+kvta dh opw2 l df f get og 2
1645+if eq get ogkm 0 0
1646+kdrelu df add oafp l mul t f f
1647+else
1648+ogmask df l t
1649+end
1650+g2 = add get og 10 opw2 l
1651+kger add oarl l mul t f dh g2 f get og 2
1652+offb2 l t df
1653+end
1654+
1655+note y[j] = 0 where x[j] <= 0, for j < n
1656+fn kdrelu yo ulong xo ulong n ulong to void
1657+for j ulong n
1658+if le get oa add xo j 0.0
1659+put oa add yo j 0.0f64
1660+end
1661+end
1662+end
1663+
1664+fn offb2 l ulong t ulong df ulong to void
1665+d = get og 2
1666+r = mul t d
1667+g1 = add get og 10 opw1 l
1668+kger add oabn l r df g1 d get og 4
1669+da = add get og 20 r
1670+ozero da d
1671+kvta df opw1 l da d get og 4
1672+hm = add oahm l r
1673+dh = add get og 19 r
1674+brms hm opg2 l add oas2 l t da dh
1675+end
1676+
1677+note dp[j] = dO[t].u[j] (head n) into TMP; returns sum_j p[j] dp[j]
1678+fn oattd l ulong n ulong t ulong to f64
1679+e = get og 7
1680+dor = add add get og 24 mul t get og 2 mul n e
1681+uo = add oau l mul n e
1682+pa = opar l n t
1683+ret kattd dor uo pa add t 1
1684+end
1685+
1686+fn kattd dor ulong uo ulong pa ulong nj ulong to f64
1687+d = get og 2
1688+de = add dor get og 7
1689+dt = 0.0f64
1690+j = ulong 0
1691+while le add j 4 nj bound 999999
1692+kdot4a dor de uo d
1693+dt = kattd4 add pa j add get og 27 j dt
1694+uo = add uo mul 4 d
1695+j = add j 4
1696+end
1697+while lt j nj bound 4
1698+dp = kdot1a dor de uo
1699+put oa add get og 27 j dp
1700+a = get opa add pa j
1701+dt = add dt mul a dp
1702+uo = add uo d
1703+j = add j 1
1704+end
1705+ret dt
1706+end
1707+
1708+note TMP[tm + m] = oq[m]; dt += PA[p + m] oq[m] for m < 4 in order
1709+fn kattd4 p ulong tm ulong dt f64 to f64
1710+for i ulong 4
1711+dp = get oq i
1712+put oa add tm i dp
1713+a = get opa add p i
1714+dt = add dt mul a dp
1715+end
1716+ret dt
1717+end
1718+
1719+note scratch offsets of oatta: 0 q[t], 1 dq[t], 2 dO[t], 3 k, 4 dk, 5 du (head n applied)
1720+global oz ulong 8
1721+
1722+note ds[j] = p[j](dp[j] - dot)/sqrt(E); dq[t] += ds k[j]; dk[j] += ds q[t]; du[j] += p[j] dO[t]
1723+fn oatta l ulong n ulong t ulong dt f64 to void
1724+e = get og 7
1725+hn = mul n e
1726+r = mul t get og 2
1727+put ofs 2 dt
1728+put oz 0 add add oaq l r hn
1729+put oz 1 add add get og 21 r hn
1730+put oz 2 add add get og 24 r hn
1731+put oz 3 add oak l hn
1732+put oz 4 add get og 22 hn
1733+put oz 5 add get og 23 hn
1734+pa = opar l n t
1735+oatta2 pa add t 1
1736+end
1737+
1738+fn oatta2 pa ulong nj ulong to void
1739+d = get og 2
1740+e = get og 7
1741+for j ulong nj
1742+jr = mul j d
1743+ds = oads pa j
1744+kaxpy get oz 1 add get oz 3 jr ds e
1745+if eq OMUTA 1
1746+kaxpy add get oz 4 jr get oz 0 ds e
1747+else
1748+kaxpy add get oz 4 jr get oz 0 neg ds e
1749+end
1750+pj = get opa add pa j
1751+kaxpy add get oz 5 jr get oz 2 pj e
1752+end
1753+end
1754+
1755+note ds[j] = p[j] (TMP[j] - dot) / sqrt(E), dot in ofs 2
1756+fn oads pa ulong j ulong to f64
1757+pj = get opa add pa j
1758+u = sub get oa add get og 27 j get ofs 2
1759+ret div mul pj u fsqrt f64 get og 7
1760+end
1761+
1762+fn olayb l ulong to void
1763+ostw l
1764+oaflip l false
1765+ct = get og 9
1766+for t ulong ct
1767+offb l t
1768+end
1769+for t2 ulong ct
1770+olaybo l t2
1771+end
1772+d = get og 2
1773+ozero get og 21 mul ct d
1774+ozero get og 22 mul ct d
1775+ozero get og 23 mul ct d
1776+for n ulong get og 3
1777+for t3 ulong ct
1778+dt = oattd l n t3
1779+oatta l n t3 dt
1780+end
1781+end
1782+for t4 ulong ct
1783+olayb2 l t4
1784+end
1785+end
1786+
1787+note row t: dO[t] = DH[t] . wo^T; dwo += o[t] DH[t]
1788+fn olaybo l ulong t ulong to void
1789+d = get og 2
1790+r = mul t d
1791+dor = add get og 24 r
1792+dh = add get og 19 r
1793+ozero dor d
1794+kvta dh opwo l dor d d
1795+kger add oao l r dh add get og 10 opwo l d d
1796+end
1797+
1798+note da = dq.wq^T + dk.wk^T + du.wv^T; weight gradients; DH[t] gains the rms1 path
1799+fn olayb2 l ulong t ulong to void
1800+d = get og 2
1801+r = mul t d
1802+da = add get og 20 r
1803+ozero da d
1804+kvta add get og 21 r opwq l da d d
1805+kvta add get og 22 r opwk l da d d
1806+kvta add get og 23 r opwv l da d d
1807+olayb3 l t
1808+end
1809+
1810+fn olayb3 l ulong t ulong to void
1811+d = get og 2
1812+r = mul t d
1813+a = add oaa l r
1814+g0 = get og 10
1815+kger a add get og 21 r add g0 opwq l d d
1816+kger a add get og 22 r add g0 opwk l d d
1817+kger a add get og 23 r add g0 opwv l d d
1818+hi = add oahin l r
1819+dh = add get og 19 r
1820+brms hi opg1 l add oas1 l t add get og 20 r dh
1821+end
1822+
1823+note backward pass of sequence b after ofwd b; sc = 1/(T B) scales dl so the gradient is that of
1824+note the step loss. Gradients accumulate into the gradient at og[10] in ogr.
1825+fn obwd b ulong sc f64 to void
1826+ct = get og 9
1827+ozero get og 19 mul ct get og 2
1828+for t ulong ct
1829+ohbwd b t sc
1830+end
1831+ll = get og 5
1832+for i ulong ll
1833+olayb sub sub ll 1 i
1834+end
1835+for t2 ulong ct
1836+obwde b t2
1837+end
1838+end
1839+
1840+note row t: dwte[x[t]] += DH[t]; dwpe[t] += DH[t]
1841+fn obwde b ulong t ulong to void
1842+d = get og 2
1843+c = get obx add mul b get og 1 t
1844+dh = add get og 19 mul t d
1845+g0 = get og 10
1846+kaxpg add g0 mul c d dh 1.0 d
1847+kaxpg add add g0 get og 30 mul t d dh 1.0 d
1848+end
1849+note SPDX-License-Identifier: AGPL-3.0-only
1850+note omegallm adam.om (aien-train#52): init, global-norm clip, Adam with bias correction, the LR
1851+note schedule, gradient blocks (CONTRACT-V1 section 5) and one training step (CONTRACT section 5).
1852+note Needs math.om, data.om, model.om.
1853+note Exact operation order (agreed with the oracle lane, recorded in CONTRACT section 5):
1854+note n = sqrt(sum_i g_i^2) (i ascending); if n > 1: c = 1/n, g_i = g_i c.
1855+note b^(s+1) by s+1 repeated multiplications starting from 1.0; bc = 1 - b^(s+1).
1856+note m = 0.9 m + (1.0 - 0.9) g; v = 0.99 v + (1.0 - 0.99)(g g);
1857+note p = p - (lr_s (m/bc1)) / (sqrt(v/bc2) + 1e-8).
1858+note lr_s = (lr (s+1))/100 for s < 100, else lr (1 - (0.9 (s-100))/max(1, TOTAL-100)).
1859+
1860+note Mutant c (bias correction removed): 1 in the product, 0 in the mutant.
1861+const OMUTC ulong 1
1862+
1863+note Mutant d (gradient blocks combined in reverse order, G-1 first): 1 in the product, 0 in the
1864+note mutant.
1865+const OMUTD ulong 1
1866+
1867+note per-sequence losses of the current batch
1868+global olos f64 64
1869+
1870+note init (CONTRACT section 5): r_i = (splitmix64(S+i) >> 11) 2^-53 (N[2^-53] ->
1871+note 1.1102230246251565*^-16); w = (2 r - 1) 0.034641016151377546 (N[0.02 Sqrt[3]]); wo and w2
1872+note times 1/sqrt(2L); gains 1; m = v = 0.
1873+fn oinit to void
1874+p = get og 8
1875+sd = get og 40
1876+for q ulong ocn p
1877+i = mul q OCH
1878+ie = oce q p
1879+while lt i ie bound 999999
1880+r = mul f64 shr smx add sd i 11 1.1102230246251565e-16
1881+owp i mul sub add r r 1.0 0.034641016151377546
1882+i = add i 1
1883+end
1884+end
1885+d = get og 2
1886+ri = div 1.0f64 fsqrt f64 mul 2 get og 5
1887+for l ulong get og 5
1888+oiset opg1 l d 1.0
1889+oiset opg2 l d 1.0
1890+oiscale opwo l mul d d ri
1891+oiscale opw2 l mul d get og 4 ri
1892+end
1893+oiset get og 33 d 1.0
1894+ozerom mul 2 p
1895+end
1896+
1897+fn ozerom n ulong to void
1898+for q ulong ocn n
1899+i = mul q OCH
1900+ie = oce q n
1901+while lt i ie bound 999999
1902+omvp i 0.0f64
1903+i = add i 1
1904+end
1905+end
1906+end
1907+
1908+fn oiset o ulong n ulong v f64 to void
1909+for i ulong n
1910+owp add o i v
1911+end
1912+end
1913+
1914+fn oiscale o ulong n ulong v f64 to void
1915+for i ulong n
1916+owp add o i mul owg add o i v
1917+end
1918+end
1919+
1920+fn olrs s ulong lr f64 to f64
1921+if lt s 100
1922+ret div mul lr f64 add s 1 100.0
1923+end
1924+den = ulong 1
1925+if gt get og 42 101
1926+den = sub get og 42 100
1927+end
1928+ret mul lr sub 1.0 div mul 0.9 f64 sub s 100 f64 den
1929+end
1930+
1931+fn opowb b f64 s ulong to f64
1932+pw = 1.0f64
1933+for i ulong add s 1
1934+pw = mul pw b
1935+end
1936+ret pw
1937+end
1938+
1939+note clip the gradient to global norm 1 and apply one Adam update at step og[41]
1940+fn oadam to void
1941+p = get og 8
1942+ss = 0.0f64
1943+for q ulong ocn p
1944+i = mul q OCH
1945+ie = oce q p
1946+while lt i ie bound 999999
1947+g = ocsg i
1948+ss = add ss mul g g
1949+i = add i 1
1950+end
1951+end
1952+nr = fsqrt ss
1953+if gt nr 1.0
1954+oclip div 1.0 nr
1955+end
1956+s = get og 41
1957+lrs = olrs s bitcast f64 get og 43
1958+bc1 = sub 1.0 opowb 0.9 s
1959+bc2 = sub 1.0 opowb 0.99 s
1960+if eq OMUTC 0
1961+bc1 = 1.0f64
1962+bc2 = 1.0f64
1963+end
1964+put ofs 3 lrs
1965+put ofs 4 bc1
1966+put ofs 5 bc2
1967+for q2 ulong ocn p
1968+k = mul q2 OCH
1969+ke = oce q2 p
1970+while lt k ke bound 999999
1971+oadam1 k
1972+k = add k 1
1973+end
1974+end
1975+end
1976+
1977+note g_i = g_i c for i < P
1978+fn oclip c f64 to void
1979+p = get og 8
1980+for q ulong ocn p
1981+j = mul q OCH
1982+je = oce q p
1983+while lt j je bound 999999
1984+ocsp j mul ocsg j c
1985+j = add j 1
1986+end
1987+end
1988+end
1989+
1990+note moments of parameter i: m = 0.9 m + (1 - 0.9) g; v = 0.99 v + (1 - 0.99) g^2
1991+fn oadam1 i ulong to void
1992+g = ogrg add get og 10 i
1993+mo = add get og 11 i
1994+m = omvg mo
1995+m = mul 0.9 m
1996+a = sub 1.0f64 0.9
1997+m = add m mul a g
1998+omvp mo m
1999+vo = add get og 12 i
2000+v = omvg vo
2001+v = mul 0.99 v
2002+a = sub 1.0f64 0.99
2003+v = add v mul a mul g g
2004+omvp vo v
2005+oadam2 i m v
2006+end
2007+
2008+note p[i] -= lr (m/bc1) / (sqrt(v/bc2) + 1e-8) with lr, bc1, bc2 in ofs 3, 4, 5
2009+fn oadam2 i ulong m f64 v f64 to void
2010+u = div m get ofs 4
2011+u = mul get ofs 3 u
2012+w = div v get ofs 5
2013+w = add fsqrt w 0.00000001
2014+u = div u w
2015+w = owg i
2016+owp i sub w u
2017+end
2018+
2019+note one training step at step og[41] on TRAIN; returns the batch loss at the pre-update
2020+note parameters and increments the step. ogradw (lio.om, eio.om) runs the K workers.
2021+fn ostep to f64
2022+s = get og 41
2023+obatch s
2024+ls = ogradw
2025+oadam
2026+put og 41 add s 1
2027+ret ls
2028+end
2029+
2030+note gradient of the step loss for the batch in obx/oby (no clip, no update); returns the loss.
2031+note CONTRACT-V1 section 5: G = og[44] blocks of R = B/G sequences; block j is accumulated from
2032+note zero for b ascending; GRD = block 0, then GRD[i] = GRD[i] + block j[i] for j ascending.
2033+note G = 1 is the v0 gradient bit for bit. Every block is accumulated in BLK (CONTRACT-V3 section 2)
2034+note and block 0 copied into GRD, the same bits as accumulating it in GRD.
2035+fn ograd to f64
2036+gg = get og 44
2037+for i ulong gg
2038+j = i
2039+if eq OMUTD 0
2040+j = sub sub gg 1 i
2041+end
2042+oblkc j
2043+ogfold eq i 0
2044+end
2045+ret olsum
2046+end
2047+
2048+note block j into BLK (gradient pool [P, 2P), zeroed first); losses into olos
2049+fn oblkc j ulong to void
2050+p = get og 8
2051+put og 10 p
2052+ozerog p p
2053+bb = get og 6
2054+sc = div 1.0f64 f64 mul get og 1 bb
2055+r = div bb get og 44
2056+b = mul j r
2057+for i ulong r
2058+put olos b ofwd b
2059+obwd b sc
2060+b = add b 1
2061+end
2062+put og 10 0
2063+end
2064+
2065+note BLK into GRD: copied for the first block, else GRD[i] = GRD[i] + BLK[i] (ogadd)
2066+fn ogfold first bool to void
2067+if not first
2068+ogadd
2069+ret
2070+end
2071+p = get og 8
2072+for q ulong ocn p
2073+i = mul q OCH
2074+ie = oce q p
2075+while lt i ie bound 999999
2076+ocsp i ogrg add p i
2077+i = add i 1
2078+end
2079+end
2080+end
2081+
2082+note GRD[i] = GRD[i] + BLK[i] for i ascending
2083+fn ogadd to void
2084+p = get og 8
2085+for q ulong ocn p
2086+i = mul q OCH
2087+ie = oce q p
2088+while lt i ie bound 999999
2089+ogadd1 i p
2090+i = add i 1
2091+end
2092+end
2093+end
2094+
2095+note GRD[i] += BLK[i] (BLK at P)
2096+fn ogadd1 i ulong p ulong to void
2097+x = ogrg add p i
2098+ocsp i add ocsg i x
2099+end
2100+
2101+note step loss: sum of olos[b] for b ascending from 0.0, divided by f64 B
2102+fn olsum to f64
2103+ls = 0.0f64
2104+bb = get og 6
2105+for b ulong bb
2106+ls = add ls get olos b
2107+end
2108+ret div ls f64 bb
2109+end
2110+
2111+note adamck: Adam self-check on the small shape. With a constant gradient g from zero moments,
2112+note exact arithmetic gives m/bc1 = g and v/bc2 = g^2 at every step, so after K steps
2113+note p = -sum_s lr_s g/(|g| + 1e-8). Prints `A K maxrel_bits_hex maxrel_e15` and exits 41 when
2114+note maxrel > 1e-12. Without bias correction (mutant c) the step-1 ratio is exactly 1 and the step-2
2115+note ratio is 1.347; the query and the predicted 0.63909 are in docs/omegallm/README.md, "Sizing".
2116+fn cmdadamck to void
2117+put og 0 256
2118+put og 1 8
2119+put og 2 16
2120+put og 3 2
2121+put og 4 32
2122+put og 5 2
2123+put og 6 2
2124+if not ogeom
2125+exit 12
2126+end
2127+put og 42 200
2128+put og 43 bitcast ulong 0.001f64
2129+put og 41 0
2130+p = get og 8
2131+for i ulong p
2132+x = sub f64 rem i 7 3.0
2133+x = mul x 0.0001
2134+ogrp add get og 10 i x
2135+end
2136+for k ulong 5
2137+oadam
2138+put og 41 add get og 41 1
2139+end
2140+mx = 0.0f64
2141+for j ulong p
2142+mx = fmax mx oadamex j
2143+end
2144+oputc 65
2145+oputc 32
2146+oputu 5
2147+oputc 32
2148+oputx bitcast ulong mx
2149+oputc 32
2150+oputl long mul mx 1000000000000000.0
2151+oputc 10
2152+oflush
2153+if gt mx 0.000000000001
2154+exit 41
2155+end
2156+end
2157+
2158+fn oadamex j ulong to f64
2159+g = ogrg add get og 10 j
2160+w = 0.0f64
2161+for s ulong 5
2162+w = sub w div mul olrs s bitcast f64 get og 43 g add fabs g 0.00000001
2163+end
2164+d = fabs sub owg j w
2165+if eq w 0.0
2166+ret d
2167+end
2168+ret div d fabs w
2169+end
2170+note SPDX-License-Identifier: AGPL-3.0-only
2171+note omegallm ckpt.om (aien-train#52): checkpoint OLLMCK01 / OLLMCK02 and gradient OLLMGR01 files
2172+note (CONTRACT section 6, CONTRACT-V1 section 6). Needs math.om, data.om, model.om, adam.om before it.
2173+note Files are streamed: a save encodes the f64 payload little-endian in chunks of OCKC values into
2174+note ock, hashes each chunk (FNV-1a 64) and writes it at its file position through the platform
2175+note unit (ocwopen, ocwput, ocwend; lio.om writes PATH.tmp, fsync, rename; eio.om the file in
2176+note place), then writes the header at position 0. A load reads the header and then the payload in
2177+note chunks through oprd into opw, hashing and decoding into the pools in one pass.
2178+note CK01 header 112 bytes (FNV at 104); CK02 120 bytes (G at 104, FNV at 112); GR01 112 bytes, step
2179+note field = B, one payload. Largest file at the v3 maxima: 120 + 24 P with P = 29995392 (Wolfram:
2180+note 120+24*29995392 -> 719889528); a file over OCKCAP = 719889529 bytes is refused with 15 before its
2181+note header is read, as v0 refused a file over its image (exactly OCKCAP bytes goes to the header
2182+note checks, then 15).
2183+note Magic as a little-endian u64 (Wolfram FromDigits[Reverse[ToCharacterCode["OLLMCK01"]],256]
2184+note -> 3544415659172318287; "OLLMGR01" -> 3544423372933581903; "OLLMCK02" -> 3616473253210246223).
2185+note Load refusals (exit codes): 10 file shorter than its header or than header + 24 P, 11 bad magic,
2186+note 12 a shape zero or over the maximum, V < 256, D mod H != 0, TOTAL > 999999, or (CK02) G = 0,
2187+note G > B or B mod G != 0, 13 P disagrees with the shapes, 14 FNV-1a mismatch, 15 file longer than
2188+note header + 24 P; 70 I/O.
2189+const OCKCAP ulong 719889529
2190+const OCKC ulong 65536
2191+global ock uchar 524288
2192+
2193+fn ocput8 o ulong v ulong to void
2194+for k ulong 8
2195+put ock add o k uchar and shr v mul k 8 255
2196+end
2197+end
2198+
2199+
2200+note little-endian u64 at ock[o .. o + 8)
2201+fn ocget8 o ulong to ulong
2202+v = ulong 0
2203+for k ulong 8
2204+v = or v shl ulong get ock add o k mul k 8
2205+end
2206+ret v
2207+end
2208+
2209+note decode m values from ock into gradient pool [o .. o + m) (a worker's gradient block, lio.om)
2210+fn ocdecb o ulong m ulong to void
2211+for j ulong m
2212+ogrp add o j bitcast f64 ocget8 mul j 8
2213+end
2214+end
2215+note little-endian u64 at opw[o .. o + 8)
2216+fn opw8 o ulong to ulong
2217+v = ulong 0
2218+for k ulong 8
2219+v = or v shl ulong get opw add o k mul k 8
2220+end
2221+ret v
2222+end
2223+
2224+note FNV-1a 64 step over ock[0 .. n) from state h (basis cbf29ce484222325, prime 100000001b3)
2225+fn ocfnvk h ulong n ulong to ulong
2226+x = h
2227+for i ulong n
2228+x = mul xor x ulong get ock i 1099511628211
2229+end
2230+ret x
2231+end
2232+
2233+fn ocfnvw h ulong n ulong to ulong
2234+x = h
2235+for i ulong n
2236+x = mul xor x ulong get opw i 1099511628211
2237+end
2238+ret x
2239+end
2240+
2241+note header fields from og into ock[0 .. 104); magic mg, step field st
2242+fn ochead mg ulong st ulong to void
2243+ocput8 0 mg
2244+for i ulong 7
2245+ocput8 add 8 mul i 8 get og i
2246+end
2247+ocput8 64 get og 40
2248+ocput8 72 st
2249+ocput8 80 get og 42
2250+ocput8 88 get og 43
2251+ocput8 96 get og 8
2252+end
2253+
2254+note encode m values of pool k (0 params, 1 m, 2 v, 3 GRD, 4 BLK) from value index i into ock
2255+fn ocenc k ulong i ulong m ulong to void
2256+if eq k 0
2257+for j ulong m
2258+ocput8 mul j 8 bitcast ulong owg add i j
2259+end
2260+ret
2261+end
2262+if ge k 3
2263+o = add get og 10 i
2264+if eq k 4
2265+o = add get og 8 i
2266+end
2267+for j ulong m
2268+ocput8 mul j 8 bitcast ulong ogrg add o j
2269+end
2270+ret
2271+end
2272+o = add get og add 10 k i
2273+for j ulong m
2274+ocput8 mul j 8 bitcast ulong omvg add o j
2275+end
2276+end
2277+
2278+note decode m values from opw into pool k (0 params, 1 m, 2 v) from value index i
2279+fn ocdec k ulong i ulong m ulong to void
2280+if eq k 0
2281+for j ulong m
2282+owp add i j bitcast f64 opw8 mul j 8
2283+end
2284+ret
2285+end
2286+o = add get og add 10 k i
2287+for j ulong m
2288+omvp add o j bitcast f64 opw8 mul j 8
2289+end
2290+end
2291+
2292+note stream pools ks .. ke-1 (ocenc numbering) after a header of hl bytes; returns the FNV-1a
2293+fn ocbody ks ulong ke ulong hl ulong to ulong
2294+p = get og 8
2295+h = ulong 14695981039346656037
2296+pos = hl
2297+nc = div add p sub OCKC 1 OCKC
2298+for k ulong sub ke ks
2299+for c ulong nc
2300+i = mul c OCKC
2301+m = sub p i
2302+if gt m OCKC
2303+m = OCKC
2304+end
2305+ocenc add ks k i m
2306+nb = mul m 8
2307+h = ocfnvk h nb
2308+ocwput pos nb
2309+pos = add pos nb
2310+end
2311+end
2312+ret h
2313+end
2314+
2315+note write checkpoint (params, m, v) to argv[ai] with the loaded or initialised magic (og 46)
2316+fn ocksave ai ulong to void
2317+ocwopen ai
2318+hl = ulong 112
2319+if eq get og 46 2
2320+hl = 120
2321+end
2322+h = ocbody 0 3 hl
2323+if eq hl 120
2324+ochead 3616473253210246223 get og 41
2325+ocput8 104 get og 44
2326+ocput8 112 h
2327+else
2328+ochead 3544415659172318287 get og 41
2329+ocput8 104 h
2330+end
2331+ocwput 0 hl
2332+ocwend ai
2333+end
2334+
2335+note write gradient file GR01: header with step = B, payload GRD (for either checkpoint magic)
2336+fn ogrsave ai ulong to void
2337+ocwopen ai
2338+h = ocbody 3 4 112
2339+ochead 3544423372933581903 get og 6
2340+ocput8 104 h
2341+ocwput 0 112
2342+ocwend ai
2343+end
2344+
2345+note load the checkpoint named by argv[ai] into og, ow (params) and omv (m, v)
2346+fn ockload ai ulong to void
2347+n = ofsz ai
2348+if gt n OCKCAP
2349+exit 15
2350+end
2351+hn = oprd ai 0 0 120
2352+if lt n 112
2353+exit 10
2354+end
2355+mg = opw8 0
2356+hl = ulong 112
2357+if eq mg 3616473253210246223
2358+hl = 120
2359+put og 46 2
2360+else
2361+if ne mg 3544415659172318287
2362+exit 11
2363+end
2364+put og 46 1
2365+end
2366+if lt n hl
2367+exit 10
2368+end
2369+for i ulong 7
2370+put og i opw8 add 8 mul i 8
2371+end
2372+if not ogeom
2373+exit 12
2374+end
2375+put og 40 opw8 64
2376+put og 41 opw8 72
2377+put og 42 opw8 80
2378+put og 43 opw8 88
2379+if gt get og 42 999999
2380+exit 12
2381+end
2382+g = ulong 1
2383+if eq hl 120
2384+g = opw8 104
2385+end
2386+if not ogok2 g
2387+exit 12
2388+end
2389+put og 44 g
2390+put og 45 1
2391+p = get og 8
2392+if ne opw8 96 p
2393+exit 13
2394+end
2395+hs = opw8 sub hl 8
2396+pb = mul 24 p
2397+if lt n add hl pb
2398+exit 10
2399+end
2400+if gt n add hl pb
2401+exit 15
2402+end
2403+if ne ocklp ai hl p hs 1
2404+exit 14
2405+end
2406+end
2407+
2408+note one pass over the payload at hl: read, hash and decode each chunk; exit 70 on a short read.
2409+note Returns 1 when the payload FNV equals hs, else 0.
2410+fn ocklp ai ulong hl ulong p ulong hs ulong to ulong
2411+h = ulong 14695981039346656037
2412+nc = div add p sub OCKC 1 OCKC
2413+pos = hl
2414+for k ulong 3
2415+for c ulong nc
2416+i = mul c OCKC
2417+m = sub p i
2418+if gt m OCKC
2419+m = OCKC
2420+end
2421+nb = mul m 8
2422+if ne oprd ai pos 0 nb nb
2423+exit 70
2424+end
2425+h = ocfnvw h nb
2426+ocdec k i m
2427+pos = add pos nb
2428+end
2429+end
2430+if eq h hs
2431+ret 1
2432+end
2433+ret 0
2434+end
2435+
2436+note G against the loaded shapes: 1 <= G <= B and B mod G = 0
2437+fn ogok2 g ulong to bool
2438+b = get og 6
2439+if or eq g 0 gt g b
2440+ret false
2441+end
2442+ret eq rem b g 0
2443+end
2444+note SPDX-License-Identifier: AGPL-3.0-only
2445+note omegallm bpe.om (aien-train#52, CONTRACT-V1 sections 2, 7 and 8): the byte-pair tokenizer.
2446+note Text is read as a stream through the opw window of data.om (opstart, opshift) and cut into
2447+note chunks (2.1). bpefit counts chunk types, then merges with a hashed pair table, per-pair lists
2448+note of the types that hold the pair and a lazy max-heap keyed count << 24 | (4095 - a) << 12 |
2449+note (4095 - b): the largest key is the largest count, then the smallest a, then the smallest b.
2450+note A pair's count only falls once its newest token exists, so a popped key whose count is still
2451+note current is the true maximum; a stale one is pushed again with its current count (if any).
2452+note Encode applies, per chunk, the present merge of lowest rank until none is left. That equals
2453+note merges 0, 1, ... in order (2.4): merge k's pair never reappears after merge k, since a later
2454+note merge only makes pairs that hold its own new token. Chunk types cache their tokens.
2455+note Exits: 2 usage (bpefit V outside 257..4096); 21 a capacity below, an odd token file, or
2456+note MAXBYTES with no byte 0 inside; 25 a token >= V; 26 a bad merges file; 27 V above the file's
2457+note V, or V <= 256; 28 no pair left to merge; 70 I/O.
2458+note Interface for the model units (section 8): obload, obtlen, obtput, obprompt and obtk.
2459+note Capacities: 524288 chunk types, 4194304 pool symbols (2 per byte of type), 900000 pairs,
2460+note 4194304 type-in-pair list nodes.
2461+const BQTCAP ulong 524288
2462+const BQHMSK ulong 1048575
2463+const BQSCAP ulong 4194304
2464+const BQPCAP ulong 900000
2465+const BQPMSK ulong 1048575
2466+const BQNCAP ulong 4194304
2467+const BQLCAP ulong 1099511627776
2468+global obtk ulong 4096
2469+global obm uchar 15384
2470+global bqma ushort 4096
2471+global bqmb ushort 4096
2472+global bqln ulong 4096
2473+global bqrk uint 8192
2474+global bqrt ushort 8192
2475+global bqv ulong 2
2476+global bqcl uchar 256
2477+global bqtm ushort 128
2478+global bqsk ushort 4100
2479+global bqst ulong 16
2480+global bqhs uint 1048576
2481+global bqto uint 524288
2482+global bqtl uchar 524288
2483+global bqtn uchar 524288
2484+global bqtc ulong 524288
2485+global bqts uint 524288
2486+global bqsy ushort 4194304
2487+global bqpk uint 1048576
2488+global bqpc ulong 1048576
2489+global bqph uint 1048576
2490+global bqpl uint 1048576
2491+global bqnl uint 1048576
2492+global bqnt uint 4194304
2493+global bqnx uint 4194304
2494+global bqhp ulong 1048576
2495+
2496+note bqst: 0 types, 1 pool symbols used, 2 chunks, 3 bytes read, 4 tokens, 5 pairs, 6 new pairs
2497+note of this merge (bqnl), 7 list nodes, 8 heap entries.
2498+
2499+note class table: bqcl[c] = 1 when A(c) (section 2)
2500+fn bpcls to void
2501+for c ulong 256
2502+v = uchar 0
2503+if or or and ge c 48 le c 57 and ge c 65 le c 90 or and ge c 97 le c 122 ge c 128
2504+v = uchar 1
2505+end
2506+put bqcl c v
2507+end
2508+end
2509+
2510+note length of the chunk at opw[i] (section 2.1, pieces of at most 64) in a text ending at n > i
2511+fn bpclen i ulong n ulong to ulong
2512+c = ulong get opw i
2513+j = add i 1
2514+if eq get bqcl c 0
2515+if ne c 32
2516+ret 1
2517+end
2518+if ge j n
2519+ret 1
2520+end
2521+if eq get bqcl get opw j 0
2522+ret 1
2523+end
2524+j = add j 1
2525+end
2526+e = add i 64
2527+if lt n e
2528+e = n
2529+end
2530+while lt j e bound 64
2531+if eq get bqcl get opw j 0
2532+break
2533+end
2534+j = add j 1
2535+end
2536+ret sub j i
2537+end
2538+
2539+note as bpclen, over the argument bytes oarg (obprompt)
2540+fn bpclena i ulong n ulong to ulong
2541+c = ulong get oarg i
2542+j = add i 1
2543+if eq get bqcl c 0
2544+if ne c 32
2545+ret 1
2546+end
2547+if ge j n
2548+ret 1
2549+end
2550+if eq get bqcl get oarg j 0
2551+ret 1
2552+end
2553+j = add j 1
2554+end
2555+e = add i 64
2556+if lt n e
2557+e = n
2558+end
2559+while lt j e bound 64
2560+if eq get bqcl get oarg j 0
2561+break
2562+end
2563+j = add j 1
2564+end
2565+ret sub j i
2566+end
2567+
2568+note FNV-1a 64 of opw[i .. i + l)
2569+fn bphash i ulong l ulong to ulong
2570+h = ulong 14695981039346656037
2571+for k ulong l
2572+h = mul xor h ulong get opw add i k 1099511628211
2573+end
2574+ret h
2575+end
2576+
2577+note does type y hold the bytes opw[i .. i + l)?
2578+fn bpsame y ulong i ulong l ulong to bool
2579+if ne ulong get bqtl y l
2580+ret false
2581+end
2582+o = ulong get bqto y
2583+for k ulong l
2584+if ne ulong get bqsy add o k ulong get opw add i k
2585+ret false
2586+end
2587+end
2588+ret true
2589+end
2590+
2591+note new type in empty slot s for opw[i .. i + l): its bytes become symbols at bqsy[o], 2 l pool
2592+note symbols are reserved (encode stores the tokens after the bytes); BQTCAP when full
2593+fn bpins s ulong i ulong l ulong to ulong
2594+nt = get bqst 0
2595+o = get bqst 1
2596+if ge nt BQTCAP
2597+ret BQTCAP
2598+end
2599+if gt add o mul 2 l BQSCAP
2600+ret BQTCAP
2601+end
2602+put bqhs s uint add nt 1
2603+put bqto nt uint o
2604+put bqtl nt uchar l
2605+put bqtn nt uchar l
2606+put bqtc nt 0
2607+put bqts nt 0
2608+for k ulong l
2609+put bqsy add o k ushort get opw add i k
2610+end
2611+put bqst 0 add nt 1
2612+put bqst 1 add o l
2613+ret nt
2614+end
2615+
2616+note type id of the chunk opw[i .. i + l), added when absent; BQTCAP when absent and full
2617+fn bpfind i ulong l ulong to ulong
2618+s = and bphash i l BQHMSK
2619+for q ulong 524289
2620+y = ulong get bqhs s
2621+if eq y 0
2622+ret bpins s i l
2623+end
2624+y = sub y 1
2625+if bpsame y i l
2626+ret y
2627+end
2628+s = and add s 1 BQHMSK
2629+end
2630+ret BQTCAP
2631+end
2632+
2633+note fit: count one chunk
2634+fn bpcount i ulong l ulong to void
2635+y = bpfind i l
2636+if eq y BQTCAP
2637+exit 21
2638+end
2639+put bqtc y add get bqtc y 1
2640+put bqst 2 add get bqst 2 1
2641+end
2642+
2643+note append token t to output 0 as u16 LE
2644+fn bpwtok t ulong to void
2645+owbyte 0 and t 255
2646+owbyte 0 shr t 8
2647+end
2648+
2649+note rank of pair (a, b): its merge token, or 65535 when it is not a merge below V
2650+fn bprank a ulong b ulong to ulong
2651+key = add mul a 4096 b
2652+s = shr mul add key 1 ulong 11400714819323198485 51
2653+for q ulong 8192
2654+k = ulong get bqrk s
2655+if eq k 0
2656+ret 65535
2657+end
2658+if eq k add key 1
2659+ret ulong get bqrt s
2660+end
2661+s = and add s 1 8191
2662+end
2663+ret 65535
2664+end
2665+
2666+note replace in bqtm[0 .. m) every (a, b) of merge token t, left to right without overlap
2667+fn bprepl m ulong t ulong to ulong
2668+a = get bqma t
2669+b = get bqmb t
2670+w = ulong 0
2671+j = ulong 0
2672+while lt j m bound 128
2673+x = get bqtm j
2674+z = ushort 65535
2675+if lt add j 1 m
2676+z = get bqtm add j 1
2677+end
2678+if and eq x a eq z b
2679+put bqtm w ushort t
2680+j = add j 2
2681+else
2682+put bqtm w x
2683+j = add j 1
2684+end
2685+w = add w 1
2686+end
2687+ret w
2688+end
2689+
2690+note encode bqtm[0 .. n) (bytes) in place (section 2.4); returns the token count
2691+fn bpenct n ulong to ulong
2692+m = n
2693+dn = false
2694+while and not dn gt m 1 bound 64
2695+best = ulong 65535
2696+for j ulong sub m 1
2697+r = bprank ulong get bqtm j ulong get bqtm add j 1
2698+if lt r best
2699+best = r
2700+end
2701+end
2702+if eq best 65535
2703+dn = true
2704+else
2705+m = bprepl m best
2706+end
2707+end
2708+ret m
2709+end
2710+
2711+note encode: one chunk opw[i .. i + l) to output 0, through the type cache when it has room
2712+fn bpenc1 i ulong l ulong to void
2713+put bqst 2 add get bqst 2 1
2714+y = bpfind i l
2715+if eq y BQTCAP
2716+for k ulong l
2717+put bqtm k ushort get opw add i k
2718+end
2719+m = bpenct l
2720+for k ulong m
2721+bpwtok ulong get bqtm k
2722+end
2723+put bqst 4 add get bqst 4 m
2724+else
2725+o = add ulong get bqto y l
2726+if eq get bqtc y 0
2727+for k ulong l
2728+put bqtm k ushort get opw add i k
2729+end
2730+mt = bpenct l
2731+for k ulong mt
2732+put bqsy add o k get bqtm k
2733+end
2734+put bqtn y uchar mt
2735+put bqtc y 1
2736+put bqst 1 add get bqst 1 mt
2737+end
2738+m = ulong get bqtn y
2739+for k ulong m
2740+bpwtok ulong get bqsy add o k
2741+end
2742+put bqst 4 add get bqst 4 m
2743+end
2744+end
2745+
2746+note the chunks of the first lim bytes of argument ai (all of it when shorter) through bpcount
2747+note (mode 0) or bpenc1 (mode 1, output 0 opened on argument bo once the text is open); bqst 3 =
2748+note bytes read. A chunk needs at most 65 bytes past its start, so the window moves to the
2749+note current chunk when fewer are left and the file goes on.
2750+note window position of i after a refill: when fewer than need bytes are left from opw[i] and the
2751+note file goes on, opw[i ..] moves to the front (0); else i
2752+fn bprefil ai ulong i ulong need ulong to ulong
2753+if gt add i need get opst 1
2754+if eq get opst 2 0
2755+opshift ai i
2756+ret 0
2757+end
2758+end
2759+ret i
2760+end
2761+
2762+fn bpscan ai ulong lim ulong mode ulong bo ulong to void
2763+opstart ai
2764+if eq mode 1
2765+owopen 0 bo
2766+end
2767+i = ulong 0
2768+dn = false
2769+while not dn bound 999999
2770+q = ulong 0
2771+while and not dn lt q 999999 bound 999999
2772+q = add q 1
2773+i = bprefil ai i 65
2774+wn = get opst 1
2775+wb = get opst 0
2776+n = wn
2777+if lt sub lim wb n
2778+n = sub lim wb
2779+end
2780+if ge i n
2781+dn = true
2782+put bqst 3 add wb i
2783+else
2784+l = bpclen i n
2785+if eq mode 0
2786+bpcount i l
2787+else
2788+bpenc1 i l
2789+end
2790+i = add i l
2791+end
2792+end
2793+end
2794+end
2795+
2796+note fitbytes for MAXBYTES m: just after the last byte 0 among the first m bytes of argument ai;
2797+note exit 21 when there is none
2798+fn bpcut ai ulong m ulong to ulong
2799+opstart ai
2800+last = ulong 0
2801+dn = false
2802+while not dn bound 999999
2803+wb = get opst 0
2804+wn = get opst 1
2805+e = wn
2806+if lt sub m wb e
2807+e = sub m wb
2808+end
2809+j = ulong 0
2810+for o ulong 3
2811+lj = mul add o 1 999999
2812+if gt lj e
2813+lj = e
2814+end
2815+while lt j lj bound 999999
2816+if eq get opw j 0
2817+last = add add wb j 1
2818+end
2819+j = add j 1
2820+end
2821+end
2822+if or lt e wn ne get opst 2 0
2823+dn = true
2824+else
2825+opshift ai wn
2826+end
2827+end
2828+if eq last 0
2829+exit 21
2830+end
2831+ret last
2832+end
2833+
2834+note pair table slot of key a 4096 + b, made (count 0, no list) when absent; new slots are listed
2835+note in bqnl; exit 21 past BQPCAP pairs
2836+fn bppslot key ulong to ulong
2837+s = shr mul add key 1 ulong 11400714819323198485 44
2838+for q ulong 1048577
2839+k = ulong get bqpk s
2840+if eq k 0
2841+nf = get bqst 5
2842+if ge nf BQPCAP
2843+exit 21
2844+end
2845+put bqpk s uint add key 1
2846+put bqpc s 0
2847+put bqph s 0
2848+put bqpl s 0
2849+put bqst 5 add nf 1
2850+nn = get bqst 6
2851+put bqnl nn uint s
2852+put bqst 6 add nn 1
2853+ret s
2854+end
2855+if eq k add key 1
2856+ret s
2857+end
2858+s = and add s 1 BQPMSK
2859+end
2860+ret s
2861+end
2862+
2863+note list type y under pair slot s (once per type in a row); exit 21 past BQNCAP nodes
2864+fn bpnode s ulong y ulong to void
2865+if ne ulong get bqpl s add y 1
2866+put bqpl s uint add y 1
2867+nn = get bqst 7
2868+if ge nn BQNCAP
2869+exit 21
2870+end
2871+put bqnt nn uint y
2872+put bqnx nn get bqph s
2873+put bqph s uint add nn 1
2874+put bqst 7 add nn 1
2875+end
2876+end
2877+
2878+fn bpkey c ulong a ulong b ulong to ulong
2879+ret or or shl c 24 shl sub 4095 a 12 sub 4095 b
2880+end
2881+
2882+fn bphpush x ulong to void
2883+i = get bqst 8
2884+put bqst 8 add i 1
2885+dn = false
2886+while and not dn gt i 0 bound 64
2887+p = div sub i 1 2
2888+v = get bqhp p
2889+if ge v x
2890+dn = true
2891+else
2892+put bqhp i v
2893+i = p
2894+end
2895+end
2896+put bqhp i x
2897+end
2898+
2899+note pop the heap's largest key (the heap is not empty)
2900+fn bphpop to ulong
2901+top = get bqhp 0
2902+n = sub get bqst 8 1
2903+put bqst 8 n
2904+x = get bqhp n
2905+i = ulong 0
2906+dn = false
2907+while not dn bound 64
2908+l = add mul i 2 1
2909+if ge l n
2910+dn = true
2911+else
2912+c = l
2913+r = add l 1
2914+if lt r n
2915+if gt get bqhp r get bqhp l
2916+c = r
2917+end
2918+end
2919+v = get bqhp c
2920+if ge x v
2921+dn = true
2922+else
2923+put bqhp i v
2924+i = c
2925+end
2926+end
2927+end
2928+if gt n 0
2929+put bqhp i x
2930+end
2931+ret top
2932+end
2933+
2934+note bqpc[s] += c and -= c, one big array per function (error 12 at this pin otherwise)
2935+fn bppcadd s ulong c ulong to void
2936+put bqpc s add get bqpc s c
2937+end
2938+fn bppcsub s ulong c ulong to void
2939+put bqpc s sub get bqpc s c
2940+end
2941+
2942+note count the pairs of type y and list y under them
2943+fn bpinty y ulong to void
2944+o = ulong get bqto y
2945+c = get bqtc y
2946+for j ulong sub ulong get bqtn y 1
2947+x = ulong get bqsy add o j
2948+z = ulong get bqsy add add o j 1
2949+s = bppslot add mul x 4096 z
2950+bppcadd s c
2951+bpnode s y
2952+end
2953+end
2954+
2955+note count the pairs of every type, list the types under them, heap every pair
2956+fn bpinit to void
2957+nt = get bqst 0
2958+for y ulong nt
2959+bpinty y
2960+end
2961+for s ulong 1048576
2962+k = ulong get bqpk s
2963+if ne k 0
2964+c = get bqpc s
2965+if gt c 0
2966+k = sub k 1
2967+bphpush bpkey c div k 4096 and k 4095
2968+end
2969+end
2970+end
2971+end
2972+
2973+note the pair to merge: the slot of the heap's largest current key; exit 28 when none is left.
2974+note Each pair has at most one heap entry, and one pop sequence meets a pair at most twice.
2975+fn bpsel to ulong
2976+for o ulong 3
2977+while gt get bqst 8 0 bound 999999
2978+x = bphpop
2979+c = shr x 24
2980+a = sub 4095 and shr x 12 4095
2981+b = sub 4095 and x 4095
2982+s = bppslot add mul a 4096 b
2983+cc = get bqpc s
2984+if eq cc c
2985+ret s
2986+end
2987+if gt cc 0
2988+bphpush bpkey cc a b
2989+end
2990+end
2991+end
2992+exit 28
2993+end
2994+
2995+fn bphas o ulong n ulong a ulong b ulong to bool
2996+for j ulong sub n 1
2997+x = ulong get bqsy add o j
2998+z = ulong get bqsy add add o j 1
2999+if and eq x a eq z b
3000+ret true
3001+end
3002+end
3003+ret false
3004+end
3005+
3006+note take count c of every pair in bqsy[o .. o + n)
3007+fn bpsub o ulong n ulong c ulong to void
3008+for j ulong sub n 1
3009+x = ulong get bqsy add o j
3010+z = ulong get bqsy add add o j 1
3011+s = bppslot add mul x 4096 z
3012+bppcsub s c
3013+end
3014+end
3015+
3016+note replace (a, b) by t in bqsy[o .. o + n), left to right without overlap; returns the length
3017+fn bprpool o ulong n ulong a ulong b ulong t ulong to ulong
3018+w = ulong 0
3019+j = ulong 0
3020+while lt j n bound 128
3021+x = ulong get bqsy add o j
3022+z = ulong 65535
3023+if lt add j 1 n
3024+z = ulong get bqsy add add o j 1
3025+end
3026+if and eq x a eq z b
3027+put bqsy add o w ushort t
3028+j = add j 2
3029+else
3030+put bqsy add o w ushort x
3031+j = add j 1
3032+end
3033+w = add w 1
3034+end
3035+ret w
3036+end
3037+
3038+note add count c of every pair in bqsy[o .. o + m) of type y; list y under the pairs holding t
3039+fn bpadd o ulong m ulong c ulong y ulong t ulong to void
3040+for j ulong sub m 1
3041+x = ulong get bqsy add o j
3042+z = ulong get bqsy add add o j 1
3043+s = bppslot add mul x 4096 z
3044+bppcadd s c
3045+if or eq x t eq z t
3046+bpnode s y
3047+end
3048+end
3049+end
3050+
3051+note apply merge (a, b) -> t to type y, moving its pair counts
3052+fn bpmtype y ulong a ulong b ulong t ulong to void
3053+o = ulong get bqto y
3054+n = ulong get bqtn y
3055+if bphas o n a b
3056+c = get bqtc y
3057+bpsub o n c
3058+m = bprpool o n a b t
3059+put bqtn y uchar m
3060+bpadd o m c y t
3061+end
3062+end
3063+
3064+note merge t over every type listed under pair slot s (each type once: stamp t - 255)
3065+fn bpwalk s ulong a ulong b ulong t ulong to void
3066+nd = ulong get bqph s
3067+st = uint sub t 255
3068+for o ulong 5
3069+while gt nd 0 bound 999999
3070+y = ulong get bqnt sub nd 1
3071+nd = ulong get bqnx sub nd 1
3072+if ne get bqts y st
3073+put bqts y st
3074+bpmtype y a b t
3075+end
3076+end
3077+end
3078+end
3079+
3080+note merges 0 .. v - 257 into bqma/bqmb and obm[24 ..)
3081+fn bpmerge v ulong to void
3082+for k ulong sub v 256
3083+s = bpsel
3084+key = sub ulong get bqpk s 1
3085+a = div key 4096
3086+b = and key 4095
3087+t = add 256 k
3088+put bqma t ushort a
3089+put bqmb t ushort b
3090+p = add 24 mul 4 k
3091+put obm p uchar and a 255
3092+put obm add p 1 uchar shr a 8
3093+put obm add p 2 uchar and b 255
3094+put obm add p 3 uchar shr b 8
3095+put bqst 6 0
3096+bpwalk s a b t
3097+bpnewp
3098+end
3099+end
3100+
3101+note heap every pair slot made by this merge (bqnl[0 .. bqst 6)) that has a count
3102+fn bpnewp to void
3103+nw = get bqst 6
3104+for i ulong nw
3105+q = ulong get bqnl i
3106+c = get bqpc q
3107+if gt c 0
3108+kq = sub ulong get bqpk q 1
3109+bphpush bpkey c div kq 4096 and kq 4095
3110+end
3111+end
3112+end
3113+
3114+note obm[p .. p + 8) = v as u64 LE
3115+fn bpu64 p ulong v ulong to void
3116+for k ulong 8
3117+put obm add p k uchar and shr v mul k 8 255
3118+end
3119+end
3120+
3121+note FNV-1a 64 of obm[24 .. e)
3122+fn bpfnvm e ulong to ulong
3123+h = ulong 14695981039346656037
3124+for k ulong sub e 24
3125+h = mul xor h ulong get obm add 24 k 1099511628211
3126+end
3127+ret h
3128+end
3129+
3130+note u16 LE at opw[p]
3131+fn bprd16 p ulong to ulong
3132+lo = ulong get opw p
3133+hi = ulong get opw add p 1
3134+ret or lo shl hi 8
3135+end
3136+
3137+note u64 LE at opw[p]
3138+fn bprd64 p ulong to ulong
3139+v = ulong 0
3140+for k ulong 8
3141+v = or v shl ulong get opw add p k mul k 8
3142+end
3143+ret v
3144+end
3145+
3146+note read and check the merges file of argument ai (section 2.3; refusals 26) into bqma/bqmb;
3147+note returns its V. Uses opw.
3148+fn bpload ai ulong to ulong
3149+n = oprd ai 0 0 15385
3150+if or lt n 24 gt n 15384
3151+exit 26
3152+end
3153+if ne bprd64 0 3550332126946413647
3154+exit 26
3155+end
3156+vm = bprd64 8
3157+if or lt vm 257 gt vm 4096
3158+exit 26
3159+end
3160+if ne n add 24 mul 4 sub vm 256
3161+exit 26
3162+end
3163+h = ulong 14695981039346656037
3164+for k ulong sub n 24
3165+h = mul xor h ulong get opw add 24 k 1099511628211
3166+end
3167+if ne h bprd64 16
3168+exit 26
3169+end
3170+for k ulong sub vm 256
3171+p = add 24 mul 4 k
3172+a = bprd16 p
3173+b = bprd16 add p 2
3174+lk = add 256 k
3175+if or ge a lk ge b lk
3176+exit 26
3177+end
3178+t = add 256 k
3179+put bqma t ushort a
3180+put bqmb t ushort b
3181+end
3182+put bqv 1 vm
3183+ret vm
3184+end
3185+
3186+note use the first v - 256 merges: decoded lengths (held at 2^40) and the rank table
3187+fn bpsetv v ulong to void
3188+for t ulong 256
3189+put bqln t 1
3190+end
3191+for k ulong sub v 256
3192+t = add 256 k
3193+ma = ulong get bqma t
3194+mb = ulong get bqmb t
3195+la = get bqln ma
3196+s = add la get bqln mb
3197+if gt s BQLCAP
3198+s = BQLCAP
3199+end
3200+put bqln t s
3201+end
3202+for s ulong 8192
3203+put bqrk s 0
3204+end
3205+for k ulong sub v 256
3206+t = add 256 k
3207+ma = ulong get bqma t
3208+mb = ulong get bqmb t
3209+key = add mul ma 4096 mb
3210+s = shr mul add key 1 ulong 11400714819323198485 51
3211+dn = false
3212+while not dn bound 8192
3213+if eq get bqrk s 0
3214+put bqrk s uint add key 1
3215+put bqrt s ushort t
3216+dn = true
3217+else
3218+s = and add s 1 8191
3219+end
3220+end
3221+end
3222+put bqv 0 v
3223+end
3224+
3225+note write the decoded bytes of token t to stdout (mode 0) or to output 0 (mode 1)
3226+fn bpexp t ulong mode ulong to void
3227+put bqsk 0 ushort t
3228+sp = ulong 1
3229+while gt sp 0 bound 999999
3230+q = ulong 0
3231+while and gt sp 0 lt q 999999 bound 999999
3232+q = add q 1
3233+sp = sub sp 1
3234+x = ulong get bqsk sp
3235+if lt x 256
3236+if eq mode 0
3237+oputc x
3238+else
3239+owbyte 0 x
3240+end
3241+else
3242+put bqsk sp get bqmb x
3243+put bqsk add sp 1 get bqma x
3244+sp = add sp 2
3245+end
3246+end
3247+end
3248+end
3249+
3250+note decode token t to output 0 and add its length to bqst 4
3251+fn bpdec1 t ulong to void
3252+bpexp t 1
3253+put bqst 4 add get bqst 4 get bqln t
3254+end
3255+
3256+note token file argument ai: mode 0 checks it (exit 21 odd length, then 25 a token >= v) and
3257+note returns its token count; mode 1 decodes it to output 0 (bqst 4 = bytes)
3258+fn bpdscan ai ulong v ulong mode ulong to ulong
3259+opstart ai
3260+i = ulong 0
3261+nt = ulong 0
3262+bad = false
3263+odd = false
3264+dn = false
3265+while not dn bound 999999
3266+q = ulong 0
3267+while and not dn lt q 999999 bound 999999
3268+q = add q 1
3269+i = bprefil ai i 2
3270+wn = get opst 1
3271+if gt add i 2 wn
3272+dn = true
3273+if lt i wn
3274+odd = true
3275+end
3276+else
3277+t = bprd16 i
3278+if ge t v
3279+bad = true
3280+end
3281+if eq mode 1
3282+bpdec1 t
3283+end
3284+nt = add nt 1
3285+i = add i 2
3286+end
3287+end
3288+end
3289+if odd
3290+exit 21
3291+end
3292+if bad
3293+exit 25
3294+end
3295+ret nt
3296+end
3297+
3298+note print C a b c (C = 77 M, 75 K, 68 D)
3299+fn bpline c ulong a ulong b ulong d ulong to void
3300+oputc c
3301+oputc 32
3302+oputu a
3303+oputc 32
3304+oputu b
3305+if ne c 75
3306+if ne c 68
3307+oputc 32
3308+oputu d
3309+end
3310+end
3311+oputc 10
3312+oflush
3313+end
3314+
3315+note section 8: load merges argument ai for vocabulary v (26, 27)
3316+fn obload ai ulong v ulong to void
3317+vm = bpload ai
3318+if or le v 256 gt v vm
3319+exit 27
3320+end
3321+bpsetv v
3322+bpcls
3323+end
3324+
3325+fn obtlen t ulong to ulong
3326+ret get bqln t
3327+end
3328+
3329+fn obtput t ulong to void
3330+bpexp t 0
3331+end
3332+
3333+note section 8: encode argument ai (at most 4000 bytes, CONTRACT 7.2; longer exit 2) into obtk;
3334+note returns the token count
3335+fn obprompt ai ulong to ulong
3336+n = oargp ai
3337+if gt n 4000
3338+exit 2
3339+end
3340+i = ulong 0
3341+m = ulong 0
3342+while lt i n bound 4097
3343+l = bpclena i n
3344+for k ulong l
3345+put bqtm k ushort get oarg add i k
3346+end
3347+c = bpenct l
3348+for k ulong c
3349+put obtk add m k ulong get bqtm k
3350+end
3351+m = add m c
3352+i = add i l
3353+end
3354+ret m
3355+end
3356+
3357+note bpefit TEXT MERGES V [MAXBYTES]
3358+fn cmdbpfit to void
3359+na = oargc
3360+if or lt na 5 gt na 6
3361+exit 2
3362+end
3363+v = oargu 4
3364+if or lt v 257 gt v 4096
3365+exit 2
3366+end
3367+lim = ulong 18446744073709551615
3368+if eq na 6
3369+lim = bpcut 2 oargu 5
3370+end
3371+bpcls
3372+bpscan 2 lim 0 0
3373+bpinit
3374+bpmerge v
3375+nb = add 24 mul 4 sub v 256
3376+bpu64 0 3550332126946413647
3377+bpu64 8 v
3378+bpu64 16 bpfnvm nb
3379+obmsave 3 nb
3380+oputc 77
3381+oputc 32
3382+oputu v
3383+oputc 32
3384+oputu get bqst 0
3385+oputc 32
3386+oputu get bqst 2
3387+oputc 32
3388+oputu get bqst 3
3389+oputc 10
3390+oflush
3391+end
3392+
3393+note bpeenc MERGES V TEXT TOKS
3394+fn cmdbpenc to void
3395+if ne oargc 6
3396+exit 2
3397+end
3398+v = oargu 3
3399+obload 2 v
3400+bpscan 4 ulong 18446744073709551615 1 5
3401+owclose 0
3402+bpline 75 get bqst 3 get bqst 4 0
3403+end
3404+
3405+note bpedec MERGES TOKS OUT (V = the file's V)
3406+fn cmdbpdec to void
3407+if ne oargc 5
3408+exit 2
3409+end
3410+vm = bpload 2
3411+bpsetv vm
3412+nt = bpdscan 3 vm 0
3413+owopen 0 4
3414+put bqst 4 0
3415+nd = bpdscan 3 vm 1
3416+owclose 0
3417+bpline 68 nt get bqst 4 0
3418+end
3419+note SPDX-License-Identifier: AGPL-3.0-only
3420+note omegallm cli.om (aien-train#52): the ollm commands (CONTRACT section 7). The platform unit
3421+note (lio.om or eio.om) follows it and holds main.
3422+note Command words are matched as little-endian u64 packings of their bytes (Wolfram
3423+note FromDigits[Reverse[ToCharacterCode[w]],256]): prep 1885696624, init 1953066601,
3424+note grad 1684107879, train 474214396532, eval 1818326629, base 1702060386, sample 111516417089907,
3425+note gcheck 118074647274343, mathck 118074698391917, adamck 118074781033569; v1: bpefit
3426+note 127996038312034, bpeenc 109325798699106, bpedec 109287127216226 (bpe.om).
3427+note Exit codes: 0 ok; 2 usage; 10-15 checkpoint refusals (ckpt.om); 20 data file shorter than
3428+note T + 1 bytes; 21 data over 2^25 bytes or OFFS not exactly 8 B bytes; 22 an OFFS offset with
3429+note o + T >= N; 23 eval W = 0 or W T + 1 > M; 24 prep story over 999999 bytes; 40 gcheck over
3430+note tolerance; 41 adamck over tolerance; 42 mathck over tolerance; 70 I/O.
3431+
3432+fn ocmdw to ulong
3433+r = oargr 1
3434+if or le r 0 gt r 8
3435+ret 0
3436+end
3437+v = ulong 0
3438+for k ulong ulong r
3439+v = or v shl ulong get oarg k mul k 8
3440+end
3441+ret v
3442+end
3443+
3444+note run the command named by argument 1; the platform main checks that it exists.
3445+fn ocmd to int
3446+w = ocmdw
3447+if eq w 1885696624
3448+cmdprep
3449+ret 0
3450+end
3451+if eq w 127996038312034
3452+cmdbpfit
3453+ret 0
3454+end
3455+if eq w 109325798699106
3456+cmdbpenc
3457+ret 0
3458+end
3459+if eq w 109287127216226
3460+cmdbpdec
3461+ret 0
3462+end
3463+if eq w 1953066601
3464+cmdinit
3465+ret 0
3466+end
3467+if eq w 1684107879
3468+cmdgrad
3469+ret 0
3470+end
3471+if eq w 474214396532
3472+cmdtrain
3473+ret 0
3474+end
3475+if eq w 1818326629
3476+cmdeval
3477+ret 0
3478+end
3479+if eq w 1702060386
3480+cmdbase
3481+ret 0
3482+end
3483+if eq w 111516417089907
3484+cmdsamp
3485+ret 0
3486+end
3487+if eq w 118074647274343
3488+cmdgchk
3489+ret 0
3490+end
3491+if eq w 118074698391917
3492+cmdmath
3493+ret 0
3494+end
3495+if eq w 118074781033569
3496+cmdadamck
3497+ret 0
3498+end
3499+exit 2
3500+end
3501+
3502+note init CK V T D H F L B SEED TOTAL LRMICRO [G]; lr = f64(LRMICRO)/1000000.0. Without G the file
3503+note is OLLMCK01 (G = 1); with G it is OLLMCK02 (CONTRACT-V1 section 6).
3504+fn cmdinit to void
3505+na = oargc
3506+if and ne na 13 ne na 14
3507+exit 2
3508+end
3509+for i ulong 7
3510+put og i oargu add 3 i
3511+end
3512+if not ogeom
3513+exit 12
3514+end
3515+put og 40 oargu 10
3516+tt = oargu 11
3517+if gt tt 999999
3518+exit 12
3519+end
3520+g = ulong 1
3521+put og 46 1
3522+if eq na 14
3523+g = oargu 13
3524+put og 46 2
3525+end
3526+if not ogok2 g
3527+exit 12
3528+end
3529+put og 44 g
3530+put og 42 tt
3531+put og 43 bitcast ulong div f64 oargu 12 1000000.0
3532+put og 41 0
3533+oinit
3534+ocksave 2
3535+oputc 73
3536+oputc 32
3537+oputu get og 8
3538+oputc 10
3539+oflush
3540+end
3541+
3542+note little-endian u64 at ochk[o..o+8)
3543+fn ochk64 o ulong to ulong
3544+v = ulong 0
3545+for k ulong 8
3546+x = ulong get ochk add o k
3547+v = or v shl x mul k 8
3548+end
3549+ret v
3550+end
3551+
3552+note row b from offset ochk[b] (checked by ooffok)
3553+fn ooffrow b ulong to void
3554+obrow b ochk64 mul b 8
3555+end
3556+
3557+note exit 22 when any offset has o >= N - T (written so that an offset near 2^64 cannot wrap)
3558+fn ooffok to void
3559+n = get odn 0
3560+tt = get og 1
3561+for b ulong get og 6
3562+if ge ochk64 mul b 8 sub n tt
3563+exit 22
3564+end
3565+end
3566+end
3567+
3568+note grad CK DATA OFFS OUT: the gradient of CONTRACT-V1 section 5 (G blocks, K = 1)
3569+fn cmdgrad to void
3570+if ne oargc 6
3571+exit 2
3572+end
3573+ockload 2
3574+odopen 3
3575+ooffrd 4
3576+ooffok
3577+for b ulong get og 6
3578+ooffrow b
3579+end
3580+ls = ograd
3581+ogrsave 5
3582+oputc 71
3583+oputc 32
3584+oputx bitcast ulong ls
3585+oputc 32
3586+oputl rmic ls
3587+oputc 10
3588+oflush
3589+end
3590+
3591+note train CK TRAIN UNTIL EVERY [K]: steps step .. min(UNTIL, TOTAL)-1; after step s, when
3592+note (s+1) mod EVERY = 0 or s+1 is the end, save CK and print `T s+1 loss_micro(step s)`.
3593+note K worker processes (CONTRACT-V1 section 5.1): K < 1, K > G or G mod K != 0 is exit 29.
3594+fn cmdtrain to void
3595+na = oargc
3596+if and ne na 6 ne na 7
3597+exit 2
3598+end
3599+ockload 2
3600+odopen 3
3601+un = oargu 4
3602+ev = oargu 5
3603+if eq ev 0
3604+exit 2
3605+end
3606+k = ulong 1
3607+if eq na 7
3608+k = oargu 6
3609+end
3610+gg = get og 44
3611+if or eq k 0 gt k gg
3612+exit 29
3613+end
3614+if ne rem gg k 0
3615+exit 29
3616+end
3617+put og 45 k
3618+en = get og 42
3619+if lt un en
3620+en = un
3621+end
3622+while lt get og 41 en bound 999999
3623+ls = ostep
3624+s1 = get og 41
3625+if or eq rem s1 ev 0 eq s1 en
3626+ocksave 2
3627+oputc 84
3628+oputc 32
3629+oputu s1
3630+oputc 32
3631+oputl rmic ls
3632+oputc 10
3633+oflush
3634+end
3635+end
3636+end
3637+
3638+note eval CK HELD W [MERGES]: windows at 0, T, .., (W-1)T; prints `E step W loss_micro bpb_micro`.
3639+note V > 256 needs MERGES (else exit 2); bpb = ((loss/ln2) f64(W T))/f64(nbytes) with nbytes the
3640+note decoded length of the W T targets (CONTRACT-V1 section 7.1); V = 256 keeps loss/ln2.
3641+fn cmdeval to void
3642+na = oargc
3643+if and ne na 5 ne na 6
3644+exit 2
3645+end
3646+ockload 2
3647+ww = oargu 4
3648+v = get og 0
3649+if gt v 256
3650+if eq na 5
3651+exit 2
3652+end
3653+obload 5 v
3654+end
3655+odopen 3
3656+tt = get og 1
3657+m = get odn 0
3658+note W T + 1 > N, tested as W > (N - 1) / T so that a huge W cannot wrap W T
3659+if eq ww 0
3660+exit 23
3661+end
3662+if gt ww div sub m 1 tt
3663+exit 23
3664+end
3665+ls = 0.0f64
3666+nb = ulong 0
3667+for w ulong ww
3668+obrow 0 mul w tt
3669+ls = add ls ofwd 0
3670+if gt v 256
3671+nb = add nb oevnb tt
3672+end
3673+end
3674+lo = div ls f64 ww
3675+bp = div lo 0.6931471805599453
3676+if gt v 256
3677+bp = div mul bp f64 mul ww tt f64 nb
3678+end
3679+oputc 69
3680+oputc 32
3681+oputu get og 41
3682+oputc 32
3683+oputu ww
3684+oputc 32
3685+oputl rmic lo
3686+oputc 32
3687+oputl rmic bp
3688+oputc 10
3689+oflush
3690+end
3691+
3692+note decoded bytes of the targets oby[0 .. n)
3693+fn oevnb n ulong to ulong
3694+s = ulong 0
3695+for t ulong n
3696+s = add s obtlen get oby t
3697+end
3698+ret s
3699+end
3700+
3701+note append token c to the context window obx[0 .. CT), keeping the last T tokens
3702+fn ocpush c ulong to void
3703+cn = get og 9
3704+tt = get og 1
3705+if lt cn tt
3706+put obx cn c
3707+put og 9 add cn 1
3708+else
3709+for i ulong sub tt 1
3710+put obx i get obx add i 1
3711+end
3712+put obx sub tt 1 c
3713+end
3714+end
3715+
3716+note sample CK SEED N TEMPMILLI PROMPT [MERGES]. Context = token 0, then PROMPT (bytes when V = 256,
3717+note its encoding when V > 256, CONTRACT-V1 section 7.2); the model reads the last T context tokens
3718+note at positions 0..; the next token comes from the logits at the last position.
3719+note TEMPMILLI 0: argmax (lowest index on ties), no draw. Otherwise softmax(logits/(TEMPMILLI/1000))
3720+note and u = (splitmix64 stream out >> 11) 2^-53 with state = SEED, out = mix(state += GOLD);
3721+note the token is the smallest c with u < p[0] + .. + p[c], else the last c with p[c] > 0.
3722+note A generated token 0 ends the output and is not written; another writes its decoded bytes.
3723+fn cmdsamp to void
3724+na = oargc
3725+if and ne na 7 ne na 8
3726+exit 2
3727+end
3728+ockload 2
3729+st = oargu 3
3730+nn = oargu 4
3731+if gt nn 999999
3732+exit 2
3733+end
3734+tm = oargu 5
3735+v = get og 0
3736+put og 9 0
3737+ocpush 0
3738+if gt v 256
3739+if eq na 7
3740+exit 2
3741+end
3742+r = oargr 6
3743+if or lt r 0 gt r 4000
3744+exit 2
3745+end
3746+obload 7 v
3747+np = obprompt 6
3748+for i ulong np
3749+ocpush get obtk i
3750+end
3751+else
3752+r = oargr 6
3753+if or lt r 0 gt r 4096
3754+exit 2
3755+end
3756+for i ulong ulong r
3757+ocpush ulong get oarg i
3758+end
3759+end
3760+for g ulong nn
3761+lf = ofwd 0
3762+olast
3763+c = ulong 0
3764+if eq tm 0
3765+c = oargmax
3766+else
3767+c = odraw st div f64 tm 1000.0
3768+st = add st 11400714819323198485
3769+end
3770+if eq c 0
3771+break
3772+end
3773+if gt v 256
3774+obtput c
3775+else
3776+oputc c
3777+end
3778+ocpush c
3779+end
3780+oflush
3781+end
3782+
3783+note logits of the last context position into TMP[0..V) (oa)
3784+fn olast to void
3785+d = get og 2
3786+z = add get og 17 mul sub get og 9 1 d
3787+klogit z get og 27 get og 0 d
3788+end
3789+
3790+fn oargmax to ulong
3791+tm = get og 27
3792+b = ulong 0
3793+bv = get oa tm
3794+for c ulong get og 0
3795+if gt get oa add tm c bv
3796+bv = get oa add tm c
3797+b = c
3798+end
3799+end
3800+ret b
3801+end
3802+
3803+fn odraw st ulong tau f64 to ulong
3804+tm = get og 27
3805+v = get og 0
3806+m = bitcast f64 ulong 18442240474082181120
3807+for c ulong v
3808+x = div get oa add tm c tau
3809+put oa add tm c x
3810+m = fmax m x
3811+end
3812+sm = osoft tm v m
3813+u = mul f64 shr smx st 11 1.1102230246251565e-16
3814+acc = 0.0f64
3815+la = ulong 0
3816+for k ulong v
3817+pk = get oa add tm k
3818+acc = add acc pk
3819+if lt u acc
3820+ret k
3821+end
3822+if gt pk 0.0
3823+la = k
3824+end
3825+end
3826+ret la
3827+end
3828+
3829+note gcheck constants (derivation and Wolfram queries in docs/omegallm/README.md). GCH2 = GCH/4 is
3830+note the retry step for an index whose difference at GCH crossed a ReLU kink.
3831+const GCH f64 0.000001
3832+const GCH2 f64 0.00000025
3833+const GCFLOOR f64 0.000001
3834+const GCTOL f64 0.001
3835+note mathck: max relative error allowed against the correctly rounded reference
3836+const MCTOL f64 0.000000000000001
3837+
3838+note gcheck CK TRAIN: the hand gradient of the step loss for the batch of the header step is
3839+note compared with central differences (L(p+h) - L(p-h))/((p+h) - (p-h)) at 4 indices of every
3840+note tensor instance (wte, wpe, per layer g1 wq wk wv wo g2 w1 w2, gf), drawn by splitmix64.
3841+note rel = |num - g| / max(|num|, |g|, GCFLOOR). An index with rel > GCTOL at GCH is measured
3842+note again at GCH2 and keeps the smaller rel (a wrong hand gradient fails at both steps). Prints
3843+note one `c index g_e12 num_e12` line per index (num of the kept step), then
3844+note `C count maxrel_bits_hex maxrel_e9 worst_index retries`; exit 40 when maxrel > GCTOL.
3845+fn cmdgchk to void
3846+if ne oargc 4
3847+exit 2
3848+end
3849+ockload 2
3850+odopen 3
3851+obatch get og 41
3852+ls = ograd
3853+d = get og 2
3854+cnt = ulong 0
3855+put og 50 0
3856+put og 51 0
3857+put og 52 0
3858+put og 53 0
3859+ogcten 0 mul get og 0 d 0
3860+ogcten get og 30 mul get og 1 d 1
3861+for l ulong get og 5
3862+ogcten opg1 l d 2
3863+ogcten opwq l mul d d 3
3864+ogcten opwk l mul d d 4
3865+ogcten opwv l mul d d 5
3866+ogcten opwo l mul d d 6
3867+ogcten opg2 l d 7
3868+ogcten opw1 l mul d get og 4 8
3869+ogcten opw2 l mul d get og 4 9
3870+end
3871+ogcten get og 33 d 10
3872+mx = bitcast f64 get og 51
3873+oputc 67
3874+oputc 32
3875+oputu get og 50
3876+oputc 32
3877+oputx get og 51
3878+oputc 32
3879+oputl long mul mx 1000000000.0
3880+oputc 32
3881+oputu get og 52
3882+oputc 32
3883+oputu get og 53
3884+oputc 10
3885+oflush
3886+if gt mx GCTOL
3887+exit 40
3888+end
3889+end
3890+
3891+note step-loss for the batch in obx/oby without touching the gradient
3892+fn oloss to f64
3893+ls = 0.0f64
3894+for b ulong get og 6
3895+ls = add ls ofwd b
3896+end
3897+ret div ls f64 get og 6
3898+end
3899+
3900+fn ogcten o ulong n ulong tag ulong to void
3901+for j ulong 4
3902+i = add o rem smx xor get og 40 add mul get og 50 4 j n
3903+ogc1 i
3904+end
3905+end
3906+
3907+note central difference (L(p+h) - L(p-h))/((p+h) - (p-h)) at parameter i; restores p
3908+fn ogcnum i ulong h f64 to f64
3909+p0 = owg i
3910+pp = add p0 h
3911+pm = sub p0 h
3912+owp i pp
3913+lp = oloss
3914+owp i pm
3915+lm = oloss
3916+owp i p0
3917+ret div sub lp lm sub pp pm
3918+end
3919+
3920+fn ogcrel num f64 g f64 to f64
3921+den = fmax fmax fabs num fabs g GCFLOOR
3922+ret div fabs sub num g den
3923+end
3924+
3925+note one gradient check at parameter i; updates og[50] count, og[51] max rel bits, og[52] worst
3926+note index, og[53] retries at GCH2
3927+fn ogc1 i ulong to void
3928+g = ocsg i
3929+num = ogcnum i GCH
3930+rel = ogcrel num g
3931+if gt rel GCTOL
3932+put og 53 add get og 53 1
3933+n2 = ogcnum i GCH2
3934+r2 = ogcrel n2 g
3935+if lt r2 rel
3936+num = n2
3937+rel = r2
3938+end
3939+end
3940+oputc 99
3941+oputc 32
3942+oputu i
3943+oputc 32
3944+oputl long mul g 1000000000000.0
3945+oputc 32
3946+oputl long mul num 1000000000000.0
3947+oputc 10
3948+put og 50 add get og 50 1
3949+if gt rel bitcast f64 get og 51
3950+put og 51 bitcast ulong rel
3951+put og 52 i
3952+end
3953+end
3954+
3955+note mathck REF: REF lines are `e XBITS RBITS` or `l XBITS RBITS` (16 lowercase hex digits each,
3956+note 36 bytes per line); RBITS is the correctly rounded Wolfram value of exp or log at the exact
3957+note f64 XBITS. Prints `M e count max_ulp maxrel_bits_hex maxrel_e18` and the same for l;
3958+note exit 42 when a special value (r = 0, inf or NaN) differs in bits, maxrel > MCTOL, or REF has
3959+note no e line or no l line (a check that checked nothing fails).
3960+fn cmdmath to void
3961+if ne oargc 3
3962+exit 2
3963+end
3964+n = odread 2
3965+if ne rem n 36 0
3966+exit 21
3967+end
3968+for k ulong 6
3969+put og add 50 k 0
3970+end
3971+for i ulong div n 36
3972+omline mul i 36
3973+end
3974+omprint 101 0
3975+omprint 108 3
3976+if or gt bitcast f64 get og 52 MCTOL gt bitcast f64 get og 55 MCTOL
3977+exit 42
3978+end
3979+if or eq get og 50 0 eq get og 53 0
3980+exit 42
3981+end
3982+end
3983+
3984+fn ohex o ulong to ulong
3985+v = ulong 0
3986+for k ulong 16
3987+c = odb add o k
3988+if lt c 58
3989+v = or shl v 4 sub c 48
3990+else
3991+v = or shl v 4 sub c 87
3992+end
3993+end
3994+ret v
3995+end
3996+
3997+note og[50+b]: count, max ulp, max rel bits (b = 0 for exp, 3 for log)
3998+fn omline o ulong to void
3999+kd = odb o
4000+x = bitcast f64 ohex add o 2
4001+rb = ohex add o 19
4002+b = ulong 0
4003+y = 0.0f64
4004+if eq kd 101
4005+y = fexp x
4006+else
4007+b = ulong 3
4008+y = flog x
4009+end
4010+yb = bitcast ulong y
4011+put og add 50 b add get og add 50 b 1
4012+r = bitcast f64 rb
4013+if or or eq r 0.0 ne r r eq fabs r bitcast f64 ulong 9218868437227405312
4014+if ne yb rb
4015+exit 42
4016+end
4017+ret
4018+end
4019+ul = ulong 0
4020+if gt yb rb
4021+ul = sub yb rb
4022+else
4023+ul = sub rb yb
4024+end
4025+if gt ul get og add 51 b
4026+put og add 51 b ul
4027+end
4028+rel = div fabs sub y r fabs r
4029+if gt rel bitcast f64 get og add 52 b
4030+put og add 52 b bitcast ulong rel
4031+end
4032+end
4033+
4034+fn omprint c ulong b ulong to void
4035+oputc 77
4036+oputc 32
4037+oputc c
4038+oputc 32
4039+oputu get og add 50 b
4040+oputc 32
4041+oputu get og add 51 b
4042+oputc 32
4043+oputx get og add 52 b
4044+oputc 32
4045+oputl long mul bitcast f64 get og add 52 b 1e18
4046+oputc 10
4047+oflush
4048+end
4049+note SPDX-License-Identifier: AGPL-3.0-only
4050+note omegallm lio.om (aien-train#52): the Linux platform unit, the only unit that uses host words.
4051+note It is concatenated last for targets aarch64 and x86linux; eio.om replaces it for x86efi. Both
4052+note define the same functions: oargc, oargr, oflush, ocwopen, ocwput, ocwend, ofsz, ogradw, ocsinit,
4053+note ocsg, ocsp, odread, owflush, owopen, owclose, ooffrd, oprd, obmsave and main. Every other unit is
4054+note free of host words.
4055+global owfd int 2
4056+global opth uchar 2100
4057+global opk uchar 4200
4058+global ocfd int 1
4059+global opp uchar 8
4060+global ost uchar 8
4061+global owrf int 64
4062+global owpd int 64
4063+
4064+note the argument count (argv[0] included)
4065+fn oargc to ulong
4066+ret ulong argcnt
4067+end
4068+
4069+note argv[i] into oarg (at most 4096 bytes); returns the argread result (negative: no such argument)
4070+fn oargr i ulong to long
4071+ret installedargs i
4072+end
4073+
4074+fn ocpumain to int
4075+if lt oargc 2
4076+exit 2
4077+end
4078+ret ocmd
4079+end
4080+
4081+fn oflush to void
4082+n = get oon 0
4083+p = ulong 0
4084+while lt p n bound 4097
4085+w = write 1 oout p sub n p
4086+if le w 0
4087+exit 70
4088+end
4089+p = add p ulong w
4090+end
4091+put oon 0 0
4092+end
4093+
4094+note v1 checkpoint stream (ckpt.om): open PATH.tmp for the path in argv[ai] (created, emptied)
4095+fn ocwopen ai ulong to void
4096+pl = oargp ai
4097+if gt pl 2000
4098+exit 2
4099+end
4100+for i ulong pl
4101+put opth i get oarg i
4102+end
4103+put opth pl 46
4104+put opth add pl 1 116
4105+put opth add pl 2 109
4106+put opth add pl 3 112
4107+fd = int fopen opth 0 add pl 4 577 420
4108+if lt fd 0
4109+exit 70
4110+end
4111+put ocfd 0 fd
4112+end
4113+
4114+note write ock[0 .. n) at byte position pos of the open PATH.tmp
4115+fn ocwput pos ulong n ulong to void
4116+fd = get ocfd 0
4117+p = ulong 0
4118+while lt p n bound 999999
4119+w = pwrite fd ock p sub n p long add pos p
4120+if le w 0
4121+exit 70
4122+end
4123+p = add p ulong w
4124+end
4125+end
4126+
4127+note fsync and close PATH.tmp, then rename it onto the path in argv[ai]
4128+fn ocwend ai ulong to void
4129+fd = get ocfd 0
4130+if ne fsync fd 0
4131+exit 70
4132+end
4133+if ne fclose fd 0
4134+exit 70
4135+end
4136+ocrename oargp ai
4137+end
4138+
4139+note rename opth[0 .. pl+4) to opth[0 .. pl) via a packed two-path list
4140+fn ocrename pl ulong to void
4141+put opk 0 2
4142+put opk 1 0
4143+put opk 2 uchar and add pl 4 255
4144+put opk 3 uchar shr add pl 4 8
4145+for i ulong add pl 4
4146+put opk add 4 i get opth i
4147+end
4148+q = add 8 pl
4149+put opk q uchar and pl 255
4150+put opk add q 1 uchar shr pl 8
4151+for j ulong pl
4152+put opk add add q 2 j get opth j
4153+end
4154+r = renames opk 0 add add q 2 pl 0
4155+if ne r 0
4156+exit 70
4157+end
4158+end
4159+
4160+note size in bytes of the file named by argv[ai] (seek to its end)
4161+fn ofsz ai ulong to ulong
4162+n = oargp ai
4163+fd = int fopen oarg 0 n 0 0
4164+if lt fd 0
4165+exit 70
4166+end
4167+sz = fseek fd 0 2
4168+if lt sz 0
4169+exit 70
4170+end
4171+if ne fclose fd 0
4172+exit 70
4173+end
4174+ret ulong sz
4175+end
4176+
4177+note cold store (CONTRACT-V3 section 2): GRD and the Adam moments, 3 P values at the CONTRACT-V3
4178+note maxima (3 29995392 -> 89986176 values, 719889408 bytes), in one private anonymous mapping made
4179+note on first use (mapnew prot 3 read/write, flags 34 private|anonymous; zero-filled, and inherited
4180+note copy-on-write by the gradient workers). A failed mapping is an I/O refusal (exit 70).
4181+const OCSN ulong 89986176
4182+global ocsh long 1
4183+global ocsf ulong 1
4184+
4185+note capacity of the cold store in f64 values (maps it on the first call)
4186+fn ocsinit to ulong
4187+if eq get ocsf 0 0
4188+h = mapnew int -1 0 mul OCSN 8 3 34
4189+if lt h 0
4190+exit 70
4191+end
4192+put ocsh 0 h
4193+put ocsf 0 1
4194+end
4195+ret OCSN
4196+end
4197+
4198+fn ocsg i ulong to f64
4199+ret bitcast f64 mapget get ocsh 0 mul i 8 8
4200+end
4201+
4202+fn ocsp i ulong v f64 to void
4203+x = mapset get ocsh 0 mul i 8 8 bitcast ulong v
4204+end
4205+
4206+note gradient of the step loss with K = og[45] processes (CONTRACT-V1 section 5.1). K = 1 is
4207+note ograd. For K > 1 the parent forks workers w = 1 .. K-1, each with its own pipe; worker w
4208+note computes blocks w G/K .. (w+1) G/K - 1 (each from zero into its own BLK copy), writes each
4209+note block's f64 bits and then its sequences' losses to the pipe and exits 0. The parent computes
4210+note blocks 0 .. G/K - 1 itself, then reads the workers in ascending w, adding each block into GRD
4211+note in ascending j as ograd does, and the losses into olos; olsum sums them in ascending b. So the
4212+note gradient and loss are the same bits for every K. A failed fork, pipe, read or a worker that
4213+note does not exit 0 is an I/O refusal (exit 70).
4214+fn ogradw to f64
4215+k = get og 45
4216+if le k 1
4217+ret ograd
4218+end
4219+per = div get og 44 k
4220+oflush
4221+for w ulong sub k 1
4222+ofork1 w per
4223+end
4224+for j ulong per
4225+oblkc j
4226+ogfold eq j 0
4227+end
4228+for w ulong sub k 1
4229+ojoin1 w per
4230+end
4231+ret olsum
4232+end
4233+
4234+note fork worker w (blocks (w+1) per .. (w+2) per - 1); keep its pipe read end and pid
4235+fn ofork1 w ulong per ulong to void
4236+r = pipefds opp 0 8 0
4237+if ne r 0
4238+exit 70
4239+end
4240+rf = int or ulong get opp 0 shl ulong get opp 1 8
4241+wf = int or ulong get opp 4 shl ulong get opp 5 8
4242+pid = fork
4243+if lt pid 0
4244+exit 70
4245+end
4246+if eq pid 0
4247+c = fclose rf
4248+owork wf mul add w 1 per per
4249+exit 0
4250+end
4251+c2 = fclose wf
4252+put owrf w rf
4253+put owpd w int pid
4254+end
4255+
4256+note read worker w's blocks into GRD in ascending j and its losses into olos, then reap it
4257+fn ojoin1 w ulong per ulong to void
4258+rf = get owrf w
4259+for j ulong per
4260+orecvb rf
4261+ogadd
4262+end
4263+r = div get og 6 get og 44
4264+m = mul per r
4265+orecvl rf mul add w 1 m m
4266+c = fclose rf
4267+pd = get owpd w
4268+x = waitpid pd ost 0 4 0
4269+if ne x long pd
4270+exit 70
4271+end
4272+s0 = get ost 0
4273+s1 = get ost 1
4274+if ne or s0 s1 0
4275+exit 70
4276+end
4277+end
4278+
4279+note worker: blocks j0 .. j0 + n - 1 into BLK (gradient pool from P), each sent as it is done, then
4280+note the losses of its sequences
4281+fn owork fd int j0 ulong n ulong to void
4282+for t ulong n
4283+oblkc add j0 t
4284+osendb fd
4285+end
4286+r = div get og 6 get og 44
4287+b0 = mul j0 r
4288+m = mul n r
4289+for i ulong m
4290+ocput8 mul i 8 bitcast ulong get olos add b0 i
4291+end
4292+owall fd mul m 8
4293+end
4294+
4295+fn owall fd int n ulong to void
4296+p = ulong 0
4297+while lt p n bound 999999
4298+w = write fd ock p sub n p
4299+if le w 0
4300+exit 70
4301+end
4302+p = add p ulong w
4303+end
4304+end
4305+
4306+fn ordall fd int n ulong to void
4307+p = ulong 0
4308+while lt p n bound 999999
4309+r = read fd ock p sub n p
4310+if le r 0
4311+exit 70
4312+end
4313+p = add p ulong r
4314+end
4315+end
4316+
4317+note send BLK (gradient pool [P .. 2P)) in chunks of OCKC values
4318+fn osendb fd int to void
4319+p = get og 8
4320+nc = div add p sub OCKC 1 OCKC
4321+for c ulong nc
4322+i = mul c OCKC
4323+m = sub p i
4324+if gt m OCKC
4325+m = OCKC
4326+end
4327+ocenc 4 i m
4328+owall fd mul m 8
4329+end
4330+end
4331+
4332+note receive one block into BLK (gradient pool [P .. 2P))
4333+fn orecvb fd int to void
4334+p = get og 8
4335+nc = div add p sub OCKC 1 OCKC
4336+for c ulong nc
4337+i = mul c OCKC
4338+m = sub p i
4339+if gt m OCKC
4340+m = OCKC
4341+end
4342+ordall fd mul m 8
4343+ocdecb add p i m
4344+end
4345+end
4346+
4347+note receive n losses into olos[b0 ..)
4348+fn orecvl fd int b0 ulong n ulong to void
4349+ordall fd mul n 8
4350+for i ulong n
4351+put olos add b0 i bitcast f64 ocget8 mul i 8
4352+end
4353+end
4354+
4355+note Read the file named by argv[i] into od0/od1; refuse more than ODCAP bytes (exit 21).
4356+note Each read pass adds at least one byte or ends, so ODCAP + 2 passes suffice: 64 outer passes
4357+note of at most 999999 inner passes each cover 2^25 + 2.
4358+fn odread i ulong to ulong
4359+n = oargp i
4360+fd = int fopen oarg 0 n 0 0
4361+if lt fd 0
4362+exit 70
4363+end
4364+p = ulong 0
4365+dn = false
4366+for o ulong 64
4367+q = ulong 0
4368+while and not dn lt q 999999 bound 999999
4369+q = add q 1
4370+r = long 0
4371+if lt p ODHALF
4372+r = read fd od0 p sub ODHALF p
4373+else
4374+if lt p ODCAP
4375+r = read fd od1 sub p ODHALF sub ODCAP p
4376+else
4377+r = read fd ochk 0 1
4378+end
4379+end
4380+if lt r 0
4381+exit 70
4382+end
4383+if eq r 0
4384+dn = true
4385+else
4386+p = add p ulong r
4387+if gt p ODCAP
4388+exit 21
4389+end
4390+end
4391+end
4392+if dn
4393+break
4394+end
4395+end
4396+if not dn
4397+exit 70
4398+end
4399+c = fclose fd
4400+put odn 0 p
4401+ret p
4402+end
4403+
4404+note prep output: two buffered writers (k = 0 TRAIN, k = 1 HELD).
4405+fn owflush k ulong to void
4406+n = get owbn k
4407+fd = get owfd k
4408+p = ulong 0
4409+while lt p n bound 65537
4410+w = long 0
4411+if eq k 0
4412+w = write fd owb0 p sub n p
4413+else
4414+w = write fd owb1 p sub n p
4415+end
4416+if le w 0
4417+exit 70
4418+end
4419+p = add p ulong w
4420+end
4421+put owbn k 0
4422+end
4423+
4424+fn owopen k ulong i ulong to void
4425+n = oargp i
4426+fd = int fopen oarg 0 n 577 420
4427+if lt fd 0
4428+exit 70
4429+end
4430+put owfd k fd
4431+put owbn k 0
4432+end
4433+
4434+fn owclose k ulong to void
4435+owflush k
4436+fd = get owfd k
4437+if ne fsync fd 0
4438+exit 70
4439+end
4440+if ne fclose fd 0
4441+exit 70
4442+end
4443+end
4444+
4445+note read argv[ai] (B little-endian u64 offsets) into ochk; exit 21 unless it holds exactly 8 B bytes
4446+fn ooffrd ai ulong to void
4447+n = oargp ai
4448+fd = int fopen oarg 0 n 0 0
4449+if lt fd 0
4450+exit 70
4451+end
4452+p = ulong 0
4453+dn = false
4454+while not dn bound 70000
4455+r = read fd ochk p sub 65536 p
4456+if lt r 0
4457+exit 70
4458+end
4459+if eq r 0
4460+dn = true
4461+else
4462+p = add p ulong r
4463+if ge p 65536
4464+exit 21
4465+end
4466+end
4467+end
4468+c = fclose fd
4469+if ne p mul 8 get og 6
4470+exit 21
4471+end
4472+end
4473+
4474+
4475+note v1 positioned read (CONTRACT-V1 section 3): read up to n bytes of the file named by argv[ai],
4476+note from byte pos, into opw[off .. off + n). The file is opened and closed on every call. Returns
4477+note the bytes read: n, fewer only when the file ends first, 0 at or after its end. Exit 70 on an
4478+note I/O error, 2 on a bad argument; off + n over len opw traps 3. Each pread pass adds at least one
4479+note byte or ends, so n + 1 <= 2097153 passes suffice: 3 outer passes of at most 999999 inner ones.
4480+fn oprd ai ulong pos ulong off ulong n ulong to ulong
4481+pl = oargp ai
4482+fd = int fopen oarg 0 pl 0 0
4483+if lt fd 0
4484+exit 70
4485+end
4486+p = ulong 0
4487+dn = false
4488+for o ulong 3
4489+while and not dn lt p n bound 999999
4490+r = pread fd opw add off p sub n p long add pos p
4491+if lt r 0
4492+exit 70
4493+end
4494+if eq r 0
4495+dn = true
4496+else
4497+p = add p ulong r
4498+end
4499+end
4500+end
4501+if ne fclose fd 0
4502+exit 70
4503+end
4504+ret p
4505+end
4506+
4507+note v1 (CONTRACT-V1 section 7): write obm[0 .. n) (bpe.om) to the path in argv[ai] as ocwrite does:
4508+note PATH.tmp, fsync, rename
4509+fn obmsave ai ulong n ulong to void
4510+pl = oargp ai
4511+if gt pl 2000
4512+exit 2
4513+end
4514+for i ulong pl
4515+put opth i get oarg i
4516+end
4517+put opth pl 46
4518+put opth add pl 1 116
4519+put opth add pl 2 109
4520+put opth add pl 3 112
4521+fd = int fopen opth 0 add pl 4 577 420
4522+if lt fd 0
4523+exit 70
4524+end
4525+p = ulong 0
4526+while lt p n bound 999999
4527+w = write fd obm p sub n p
4528+if le w 0
4529+exit 70
4530+end
4531+p = add p ulong w
4532+end
4533+if ne fsync fd 0
4534+exit 70
4535+end
4536+if ne fclose fd 0
4537+exit 70
4538+end
4539+ocrename pl
4540+end
4541+note SPDX-License-Identifier: AGPL-3.0-only
4542+note Native continuation of canonical nvrm/nvrm.c, source349913f1, NVIDIA ABI580.173.02.
4543+note One explicitly owned context, bounded buffers, channel, submission and completion.
4544+text nvctl "/dev/nvidiactl"
4545+text nvdev "/dev/nvidia0"
4546+text nvuvm "/dev/nvidia-uvm"
4547+global nvfd int 4
4548+global nvobj uint 16
4549+global nvhdr uchar 32
4550+global nvpar uchar 16384
4551+global nvcard uchar 4608
4552+global nvseq uint 1
4553+global nverr long 1
4554+global nvstage uint 1
4555+global nvid uint 1
4556+global nvinst uint 1
4557+global nvsm uint 1
4558+global nvcls uint 3
4559+global nvuuid uchar 16
4560+global nvreg uchar 2
4561+note Context phases:0 closed,1 driver resources live,2 descriptors closed with mapping cleanup pending.
4562+global nvstate uchar 1
4563+global nvmm uint 1
4564+global nvfault uchar 1
4565+global nvnext ulong 1
4566+global nvbar long 1
4567+global nvfifo long 1
4568+global nvnotify long 1
4569+global nvchva long 1
4570+note A mapping not yet published to its caller remains owned after failed cleanup.
4571+global nvunpub long 1
4572+global nvtoken uint 1
4573+global nvchreg uchar 1
4574+global nvsched uchar 1
4575+global nvput ulong 1
4576+global nvret ulong 1
4577+global nvretry uint 1
4578+global nvmobj uint 96
4579+global nvmmap long 96
4580+global nvmaddr ulong 96
4581+global nvmlen ulong 96
4582+global nvmflag uchar 96
4583+fn nhput off ulong v uint to void
4584+for i ulong 4
4585+put nvhdr add off i uchar and shr v mul i 8 255
4586+end
4587+end
4588+fn nhget off ulong to uint
4589+v = uint 0
4590+for i ulong 4
4591+v = or v shl uint get nvhdr add off i mul i 8
4592+end
4593+ret v
4594+end
4595+fn npput off ulong v uint to void
4596+for i ulong 4
4597+put nvpar add off i uchar and shr v mul i 8 255
4598+end
4599+end
4600+fn npget off ulong to uint
4601+v = uint 0
4602+for i ulong 4
4603+v = or v shl uint get nvpar add off i mul i 8
4604+end
4605+ret v
4606+end
4607+fn npp64 off ulong v ulong to void
4608+for i ulong 8
4609+put nvpar add off i uchar and shr v mul i 8 255
4610+end
4611+end
4612+fn npg64 off ulong to ulong
4613+v = ulong 0
4614+for i ulong 8
4615+v = or v shl ulong get nvpar add off i mul i 8
4616+end
4617+ret v
4618+end
4619+fn npuuid to void
4620+npzero
4621+for i ulong 16
4622+put nvpar i get nvuuid i
4623+end
4624+end
4625+fn ncget off ulong to uint
4626+v = uint 0
4627+for i ulong 4
4628+v = or v shl uint get nvcard add off i mul i 8
4629+end
4630+ret v
4631+end
4632+fn nhzero to void
4633+for i ulong len nvhdr
4634+put nvhdr i 0
4635+end
4636+end
4637+fn npzero to void
4638+for i ulong len nvpar
4639+put nvpar i 0
4640+end
4641+end
4642+fn nvos r long status uint to bool
4643+if ne r 0
4644+put nverr 0 r
4645+ret false
4646+end
4647+if ne status 0
4648+put nverr 0 long status
4649+ret false
4650+end
4651+ret true
4652+end
4653+fn nvalc parent uint cls uint n ulong to uint
4654+nhzero
4655+nhput 0 get nvobj 0
4656+nhput 4 parent
4657+if ne cls 65
4658+if eq get nvseq 0 4294967295
4659+put nverr 0 neg long 75
4660+ret 0
4661+end
4662+nhput 8 get nvseq 0
4663+put nvseq 0 add get nvseq 0 1
4664+end
4665+nhput 12 cls
4666+r = devcall get nvfd 0 uint 3223340587 nvhdr 0 32 nvpar 0 n
4667+if not nvos r nhget 28
4668+ret 0
4669+end
4670+ret nhget 8
4671+end
4672+fn nvctrl obj uint cmd uint n ulong to bool
4673+nhzero
4674+nhput 0 get nvobj 0
4675+nhput 4 obj
4676+nhput 8 cmd
4677+r = devcall get nvfd 0 uint 3223340586 nvhdr 0 32 nvpar 0 n
4678+ret nvos r nhget 28
4679+end
4680+fn nvfree parent uint obj uint to bool
4681+if eq obj 0
4682+ret true
4683+end
4684+nhzero
4685+nhput 0 get nvobj 0
4686+nhput 4 parent
4687+nhput 8 obj
4688+r = devctl get nvfd 0 uint 3222292009 nvhdr 0 16
4689+ret nvos r nhget 12
4690+end
4691+fn nvstop to bool
4692+if eq get nvstate 0 0
4693+ret true
4694+end
4695+if eq get nvstate 0 2
4696+ret nvmapfinish
4697+end
4698+ok = true
4699+if nvchstop
4700+if not nvmclose
4701+ok = false
4702+end
4703+else
4704+ok = false
4705+end
4706+if gt get nvbar 0 0
4707+r = mapdrop get nvbar 0
4708+if ne r 0
4709+put nverr 0 r
4710+ok = false
4711+else
4712+put nvbar 0 0
4713+end
4714+end
4715+if ne get nvobj 5 0
4716+if not nvfree get nvobj 2 get nvobj 5
4717+ok = false
4718+end
4719+put nvobj 5 0
4720+end
4721+if ne get nvreg 1 0
4722+npuuid
4723+r = uvmctl get nvfd 2 uint 26 nvpar 0 20
4724+if not nvos r npget 16
4725+ok = false
4726+end
4727+put nvreg 1 0
4728+end
4729+if ne get nvreg 0 0
4730+npuuid
4731+r = uvmctl get nvfd 2 uint 38 nvpar 0 20
4732+if not nvos r npget 16
4733+ok = false
4734+end
4735+put nvreg 0 0
4736+end
4737+if ne get nvobj 4 0
4738+if not nvfree get nvobj 1 get nvobj 4
4739+ok = false
4740+end
4741+put nvobj 4 0
4742+end
4743+if ne get nvobj 3 0
4744+if not nvfree get nvobj 1 get nvobj 3
4745+ok = false
4746+end
4747+put nvobj 3 0
4748+end
4749+if ne get nvobj 2 0
4750+if not nvfree get nvobj 1 get nvobj 2
4751+ok = false
4752+end
4753+put nvobj 2 0
4754+end
4755+if ne get nvobj 1 0
4756+if not nvfree get nvobj 0 get nvobj 1
4757+ok = false
4758+end
4759+put nvobj 1 0
4760+end
4761+if ne get nvobj 0 0
4762+if not nvfree get nvobj 0 get nvobj 0
4763+ok = false
4764+end
4765+put nvobj 0 0
4766+end
4767+for j ulong len nvfd
4768+i = sub 3 j
4769+if ge get nvfd i 0
4770+if ne fclose get nvfd i 0
4771+ok = false
4772+end
4773+put nvfd i neg int 1
4774+end
4775+end
4776+note Close is attempted once per descriptor. Never repeat driver calls after this phase.
4777+put nvstate 0 2
4778+put nvsched 0 0
4779+if not nvmapfinish
4780+ok = false
4781+end
4782+if not ok
4783+put nvfault 0 1
4784+end
4785+ret ok
4786+end
4787+note The driver descriptors are closed; only owned CPU mappings may be retried here.
4788+fn nvmapfinish to bool
4789+if ne get nvstate 0 2
4790+put nverr 0 neg long 16
4791+ret false
4792+end
4793+ok = true
4794+if gt get nvunpub 0 0
4795+r = mapdrop get nvunpub 0
4796+if ne r 0
4797+put nverr 0 r
4798+ok = false
4799+else
4800+put nvunpub 0 0
4801+end
4802+end
4803+if gt get nvbar 0 0
4804+r = mapdrop get nvbar 0
4805+if ne r 0
4806+put nverr 0 r
4807+ok = false
4808+else
4809+put nvbar 0 0
4810+end
4811+end
4812+if gt get nvchva 0 0
4813+r = mapdrop get nvchva 0
4814+if ne r 0
4815+put nverr 0 r
4816+ok = false
4817+else
4818+put nvchva 0 0
4819+end
4820+end
4821+if not nvmforce
4822+ok = false
4823+end
4824+if not ok
4825+put nvfault 0 1
4826+ret false
4827+end
4828+note Descriptor closure retired every driver object, including a refused channel teardown.
4829+for i ulong len nvobj
4830+put nvobj i 0
4831+end
4832+for i ulong len nvreg
4833+put nvreg i 0
4834+end
4835+put nvchreg 0 0
4836+put nvput 0 0
4837+put nvret 0 0
4838+put nvfifo 0 0
4839+put nvnotify 0 0
4840+put nvstate 0 0
4841+ret true
4842+end
4843+fn nvstart to bool
4844+if ne get nvfault 0 0
4845+put nverr 0 neg long 5
4846+ret false
4847+end
4848+if ne get nvstate 0 0
4849+put nverr 0 neg long 16
4850+ret false
4851+end
4852+put nvstate 0 1
4853+put nvnext 0 68719476736
4854+for i ulong len nvfd
4855+put nvfd i neg int 1
4856+end
4857+for i ulong len nvobj
4858+put nvobj i 0
4859+end
4860+put nvseq 0 3472883713
4861+put nverr 0 0
4862+put nvstage 0 1
4863+r = fopen nvctl 0 len nvctl 524290 0
4864+if lt r 0
4865+put nverr 0 r
4866+ret false
4867+end
4868+put nvfd 0 int r
4869+put nvstage 0 2
4870+npzero
4871+root = nvalc 0 65 0
4872+if eq root 0
4873+ret false
4874+end
4875+put nvobj 0 root
4876+put nvstage 0 3
4877+for i ulong len nvcard
4878+put nvcard i 0
4879+end
4880+r = devctl get nvfd 0 uint 3523233480 nvcard 0 4608
4881+if not nvos r 0
4882+ret false
4883+end
4884+found = uint 0
4885+for i ulong 64
4886+off = mul i 72
4887+if ne get nvcard off 0
4888+found = add found 1
4889+if ne ncget add off 56 0
4890+put nverr 0 neg long 19
4891+ret false
4892+end
4893+put nvid 0 ncget add off 16
4894+end
4895+end
4896+if ne found 1
4897+put nverr 0 neg long 19
4898+ret false
4899+end
4900+put nvstage 0 4
4901+r = fopen nvdev 0 len nvdev 524290 0
4902+if lt r 0
4903+put nverr 0 r
4904+ret false
4905+end
4906+put nvfd 1 int r
4907+npzero
4908+npput 0 uint get nvfd 0
4909+r = devctl get nvfd 1 uint 3221505737 nvpar 0 4
4910+if not nvos r 0
4911+ret false
4912+end
4913+put nvstage 0 5
4914+npzero
4915+npput 0 get nvid 0
4916+if not nvctrl root 517 32
4917+ret false
4918+end
4919+put nvinst 0 npget 8
4920+put nvstage 0 6
4921+npzero
4922+npput 0 get nvinst 0
4923+npput 4 root
4924+dev = nvalc root 128 56
4925+if eq dev 0
4926+ret false
4927+end
4928+put nvobj 1 dev
4929+put nvstage 0 7
4930+npzero
4931+subd = nvalc dev 8320 4
4932+if eq subd 0
4933+ret false
4934+end
4935+put nvobj 2 subd
4936+put nvstage 0 8
4937+npzero
4938+if not nvctrl dev 8389266 804
4939+ret false
4940+end
4941+n = npget 0
4942+if gt n 200
4943+put nverr 0 neg long 75
4944+ret false
4945+end
4946+for i ulong 3
4947+put nvcls i 0
4948+end
4949+for i ulong ulong n
4950+c = npget add 4 mul i 4
4951+if eq c 52928
4952+put nvcls 0 c
4953+end
4954+if eq c 51567
4955+put nvcls 1 c
4956+end
4957+if eq c 50017
4958+put nvcls 2 c
4959+end
4960+end
4961+for i ulong 3
4962+if eq get nvcls i 0
4963+put nverr 0 neg long 19
4964+ret false
4965+end
4966+end
4967+put nvstage 0 9
4968+npzero
4969+npput 0 1
4970+npput 4 12
4971+if not nvctrl subd 545264168 488
4972+ret false
4973+end
4974+put nvsm 0 npget 8
4975+note RM reports SM10.04 on GB10; CUDA target sm121 uses a different numbering contract.
4976+note Matches canonical evidence/m16-blackwell-native-path-requalification-receipt.json.
4977+if ne get nvsm 0 2564
4978+put nverr 0 neg long 19
4979+ret false
4980+end
4981+put nvstage 0 10
4982+ret true
4983+end
4984+
4985+note Low GPU command addresses are reserved without replacement before an owned bind.
4986+note BAR mappings may use Linux-selected addresses; GPU buffers stay below 40 bits.
4987+fn nvcpumap obj uint size ulong system bool flags uint to long
4988+if or ne get nvfault 0 0 ne get nvstate 0 1
4989+put nverr 0 neg long 5
4990+ret neg long 1
4991+end
4992+fd = neg long 1
4993+if system
4994+fd = fopen nvctl 0 len nvctl 524290 0
4995+else
4996+fd = fopen nvdev 0 len nvdev 524290 0
4997+end
4998+if lt fd 0
4999+put nverr 0 fd
5000+ret fd
5001+end
5002+if not system
5003+npzero
5004+npput 0 uint get nvfd 0
5005+r = devctl int fd uint 3221505737 nvpar 0 4
5006+if not nvos r 0
5007+r = fclose int fd
5008+ret neg long 1
5009+end
5010+end
5011+npzero
5012+npput 0 get nvobj 0
5013+npput 4 get nvobj 1
5014+npput 8 obj
5015+npp64 24 size
5016+npput 44 flags
5017+npput 48 uint fd
5018+r = devctl get nvfd 0 uint 3224913486 nvpar 0 56
5019+if not nvos r npget 40
5020+r = fclose int fd
5021+ret neg long 1
5022+end
5023+m = neg long 1
5024+if system
5025+if gt get nvnext 0 sub 1099511627776 size
5026+put nverr 0 neg long 75
5027+r = fclose int fd
5028+ret neg long 1
5029+end
5030+m = maphold get nvnext 0 size
5031+if gt m 0
5032+put nvnext 0 add get nvnext 0 size
5033+r = mapbind m int fd 0 3
5034+if ne r 0
5035+m = r
5036+end
5037+end
5038+else
5039+m = mapnew int fd 0 size 3 1
5040+end
5041+r = fclose int fd
5042+if ne r 0
5043+put nverr 0 r
5044+put nvfault 0 1
5045+if gt m 0
5046+r = mapdrop m
5047+if ne r 0
5048+put nvunpub 0 m
5049+end
5050+end
5051+ret neg long 1
5052+end
5053+if lt m 0
5054+put nverr 0 m
5055+end
5056+ret m
5057+end
5058+
5059+note Release flags change only after confirmed acceptance. Failed entries stay quarantined.
5060+fn nvmrel slot ulong to bool
5061+f = get nvmflag slot
5062+if ne and f 4 0
5063+npzero
5064+npp64 0 get nvmaddr slot
5065+npp64 8 get nvmlen slot
5066+r = uvmctl get nvfd 2 uint 34 nvpar 0 24
5067+if not nvos r npget 16
5068+put nvfault 0 1
5069+ret false
5070+end
5071+f = and f 251
5072+put nvmflag slot f
5073+end
5074+if ne and f 8 0
5075+npzero
5076+npput 0 get nvobj 0
5077+npput 4 get nvobj 1
5078+npput 8 get nvobj 3
5079+npput 12 get nvmobj slot
5080+npp64 24 get nvmaddr slot
5081+npp64 32 get nvmlen slot
5082+r = devctl get nvfd 0 uint 3224389208 nvpar 0 48
5083+if not nvos r npget 40
5084+put nvfault 0 1
5085+ret false
5086+end
5087+f = and f 247
5088+put nvmflag slot f
5089+end
5090+if ne and f 1 0
5091+if not nvfree get nvobj 1 get nvmobj slot
5092+put nvfault 0 1
5093+ret false
5094+end
5095+f = and f 254
5096+put nvmflag slot f
5097+end
5098+if ne and f 2 0
5099+r = mapdrop get nvmmap slot
5100+if not nvos r 0
5101+put nvfault 0 1
5102+ret false
5103+end
5104+end
5105+put nvmflag slot 0
5106+put nvmobj slot 0
5107+put nvmmap slot 0
5108+put nvmaddr slot 0
5109+put nvmlen slot 0
5110+ret true
5111+end
5112+fn nvmclose to bool
5113+ok = true
5114+for i ulong len nvmflag
5115+if ne get nvmflag i 0
5116+if not nvmrel i
5117+ok = false
5118+end
5119+end
5120+end
5121+ret ok
5122+end
5123+note Called only after the owning driver context is closed, including error teardown.
5124+fn nvmforce to bool
5125+if ne get nvstate 0 2
5126+put nverr 0 neg long 16
5127+ret false
5128+end
5129+ok = true
5130+for i ulong len nvmflag
5131+note Driver-owned flags and handles retired when the descriptors closed.
5132+put nvmflag i and get nvmflag i 2
5133+put nvmobj i 0
5134+if ne and get nvmflag i 2 0
5135+r = mapdrop get nvmmap i
5136+if ne r 0
5137+put nverr 0 r
5138+ok = false
5139+else
5140+put nvmflag i 0
5141+put nvmmap i 0
5142+put nvmaddr i 0
5143+put nvmlen i 0
5144+end
5145+else
5146+put nvmmap i 0
5147+put nvmaddr i 0
5148+put nvmlen i 0
5149+end
5150+end
5151+ret ok
5152+end
5153+fn nvdrop m long to bool
5154+if ne get nvput 0 get nvret 0
5155+put nverr 0 neg long 16
5156+ret false
5157+end
5158+if and ne get nvobj 8 0 or eq m get nvfifo 0 eq m get nvnotify 0
5159+put nverr 0 neg long 16
5160+ret false
5161+end
5162+for i ulong len nvmmap
5163+if and gt m 0 eq get nvmmap i m
5164+ret nvmrel i
5165+end
5166+end
5167+put nverr 0 neg long 22
5168+ret false
5169+end
5170+fn nvalloc want ulong cached bool to long
5171+if or eq get nvreg 1 0 ne get nvfault 0 0
5172+put nverr 0 neg long 5
5173+ret neg long 1
5174+end
5175+if or eq want 0 gt want 68719476736
5176+put nverr 0 neg long 22
5177+ret neg long 1
5178+end
5179+size = mul div add want 4095 4096 4096
5180+slot = ulong 96
5181+for i ulong 96
5182+if and eq slot 96 eq get nvmflag i 0
5183+slot = i
5184+end
5185+end
5186+if eq slot 96
5187+put nverr 0 neg long 24
5188+ret neg long 1
5189+end
5190+put nvstage 0 21
5191+npzero
5192+npput 0 get nvobj 0
5193+npput 8 49409
5194+npput 24 436207616
5195+attr = uint 9
5196+if cached
5197+attr = 5
5198+end
5199+npput 28 attr
5200+npput 32 6
5201+npp64 64 size
5202+npp64 72 4096
5203+npp64 88 sub size 1
5204+h = nvalc get nvobj 1 62 128
5205+if eq h 0
5206+put nvfault 0 1
5207+ret neg long 1
5208+end
5209+put nvmobj slot h
5210+put nvmlen slot size
5211+put nvmflag slot 1
5212+put nvstage 0 22
5213+m = nvcpumap h size true 0
5214+if le m 0
5215+put nvfault 0 1
5216+ret neg long 1
5217+end
5218+put nvmmap slot m
5219+put nvmaddr slot mapaddr m
5220+put nvmflag slot 3
5221+va = get nvmaddr slot
5222+if or ne and va 4095 0 gt va sub 562949869535232 size
5223+put nverr 0 neg long 75
5224+put nvfault 0 1
5225+ret neg long 1
5226+end
5227+put nvstage 0 23
5228+npzero
5229+npp64 0 va
5230+npp64 8 size
5231+r = uvmctl get nvfd 2 uint 73 nvpar 0 24
5232+if not nvos r npget 16
5233+put nvfault 0 1
5234+ret neg long 1
5235+end
5236+put nvmflag slot 7
5237+put nvstage 0 24
5238+npzero
5239+npput 0 get nvobj 0
5240+npput 4 get nvobj 1
5241+npput 8 get nvobj 3
5242+npput 12 h
5243+npp64 24 size
5244+npput 32 33040
5245+npp64 48 va
5246+r = devctl get nvfd 0 uint 3225437783 nvpar 0 64
5247+if not nvos r npget 56
5248+put nvfault 0 1
5249+ret neg long 1
5250+end
5251+put nvmflag slot 15
5252+if ne npg64 48 va
5253+put nverr 0 neg long 22
5254+put nvfault 0 1
5255+ret neg long 1
5256+end
5257+put nvstage 0 25
5258+npzero
5259+npp64 0 va
5260+npp64 8 size
5261+for i ulong 16
5262+put nvpar add i 24 get nvuuid i
5263+end
5264+npput 40 1
5265+npp64 9240 1
5266+npput 9248 uint get nvfd 0
5267+npput 9252 get nvobj 0
5268+npput 9256 h
5269+r = uvmctl get nvfd 2 uint 33 nvpar 0 9264
5270+if not nvos r npget 9260
5271+put nvfault 0 1
5272+ret neg long 1
5273+end
5274+npzero
5275+off = ulong 0
5276+while lt off size
5277+n = sub size off
5278+if gt n 16384
5279+n = ulong 16384
5280+end
5281+r = mapwrite m off nvpar 0 n
5282+off = add off n
5283+end
5284+put nvstage 0 26
5285+ret m
5286+end
5287+
5288+note Register this private GPU virtual address space with UVM, no buffers or kernels.
5289+fn nvfull to bool
5290+if not nvstart
5291+ret false
5292+end
5293+put nvstage 0 11
5294+for i ulong 2
5295+r = fopen nvuvm 0 len nvuvm 524290 0
5296+if lt r 0
5297+put nverr 0 r
5298+ret false
5299+end
5300+put nvfd add i 2 int r
5301+end
5302+npzero
5303+r = uvmctl get nvfd 2 uint 805306369 nvpar 0 16
5304+if not nvos r npget 8
5305+ret false
5306+end
5307+put nvstage 0 12
5308+npzero
5309+npput 0 uint get nvfd 2
5310+r = uvmctl get nvfd 3 uint 75 nvpar 0 8
5311+if ne r 0
5312+put nverr 0 r
5313+ret false
5314+end
5315+note Driver platforms may return NV_WARN_NOTHING_TO_DO. Retain the returned status.
5316+put nvmm 0 npget 4
5317+if and ne get nvmm 0 0 ne get nvmm 0 65542
5318+put nverr 0 long get nvmm 0
5319+ret false
5320+end
5321+put nvstage 0 13
5322+npzero
5323+npp64 8 562949953421311
5324+v = nvalc get nvobj 1 112 24
5325+if eq v 0
5326+ret false
5327+end
5328+put nvobj 3 v
5329+put nvstage 0 14
5330+npzero
5331+npput 4 72
5332+npp64 8 562949869535232
5333+npp64 40 4096
5334+v = nvalc get nvobj 1 37105 56
5335+if eq v 0
5336+ret false
5337+end
5338+put nvobj 4 v
5339+put nvstage 0 15
5340+npzero
5341+npput 4 2
5342+npput 8 16
5343+if not nvctrl get nvobj 2 545259850 268
5344+ret false
5345+end
5346+if ne npget 8 16
5347+put nverr 0 neg long 22
5348+ret false
5349+end
5350+for i ulong 16
5351+put nvuuid i get nvpar add i 12
5352+end
5353+npuuid
5354+npput 24 4294967295
5355+r = uvmctl get nvfd 2 uint 37 nvpar 0 40
5356+if not nvos r npget 36
5357+ret false
5358+end
5359+put nvreg 0 1
5360+put nvstage 0 16
5361+npuuid
5362+npput 16 uint get nvfd 0
5363+npput 20 get nvobj 0
5364+npput 24 get nvobj 4
5365+r = uvmctl get nvfd 2 uint 25 nvpar 0 32
5366+if not nvos r npget 28
5367+ret false
5368+end
5369+put nvreg 1 1
5370+put nvstage 0 17
5371+ret true
5372+end
5373+
5374+fn nvfind m long to ulong
5375+for i ulong len nvmmap
5376+if and gt m 0 eq get nvmmap i m
5377+ret i
5378+end
5379+end
5380+ret 96
5381+end
5382+note Reverse partial channel construction before releasing its buffers or address space.
5383+fn nvchstop to bool
5384+if ne get nvchreg 0 0
5385+npuuid
5386+npput 16 get nvobj 0
5387+npput 20 get nvobj 8
5388+r = uvmctl get nvfd 2 uint 28 nvpar 0 28
5389+if not nvos r npget 24
5390+put nvfault 0 1
5391+ret false
5392+end
5393+put nvchreg 0 0
5394+end
5395+for i ulong 4
5396+obj = sub 9 i
5397+parent = ulong 6
5398+if eq obj 9
5399+parent = ulong 8
5400+end
5401+if eq obj 6
5402+parent = ulong 1
5403+end
5404+if ne get nvobj obj 0
5405+if not nvfree get nvobj parent get nvobj obj
5406+put nvfault 0 1
5407+ret false
5408+end
5409+put nvobj obj 0
5410+end
5411+end
5412+if gt get nvchva 0 0
5413+r = mapdrop get nvchva 0
5414+if not nvos r 0
5415+put nvfault 0 1
5416+ret false
5417+end
5418+put nvchva 0 0
5419+end
5420+put nvsched 0 0
5421+put nvput 0 0
5422+put nvret 0 0
5423+put nvfifo 0 0
5424+put nvnotify 0 0
5425+ret true
5426+end
5427+
5428+note Empty queue creation and scheduling; this routine submits no command packets.
5429+fn nvchan to bool
5430+if or eq get nvreg 1 0 ne get nvfault 0 0
5431+put nverr 0 neg long 16
5432+ret false
5433+end
5434+if or ne get nvobj 6 0 ne get nvobj 5 0
5435+put nverr 0 neg long 16
5436+ret false
5437+end
5438+put nvstage 0 31
5439+npzero
5440+u = nvalc get nvobj 2 50017 0
5441+if eq u 0
5442+ret false
5443+end
5444+put nvobj 5 u
5445+m = nvcpumap u 65536 false 0
5446+if le m 0
5447+ret false
5448+end
5449+put nvbar 0 m
5450+put nvstage 0 32
5451+npzero
5452+npput 12 1
5453+g = nvalc get nvobj 1 41068 20
5454+if eq g 0
5455+ret false
5456+end
5457+put nvobj 6 g
5458+put nvstage 0 33
5459+npzero
5460+npput 0 get nvobj 4
5461+npput 4 1
5462+c = nvalc g 36967 12
5463+if eq c 0
5464+ret false
5465+end
5466+put nvobj 7 c
5467+m = nvalloc 65536 false
5468+if le m 0
5469+ret false
5470+end
5471+put nvfifo 0 m
5472+m = nvalloc 1048576 true
5473+if le m 0
5474+ret false
5475+end
5476+put nvnotify 0 m
5477+put nvstage 0 34
5478+npzero
5479+npput 0 get nvmobj nvfind get nvnotify 0
5480+npput 4 get nvmobj nvfind get nvfifo 0
5481+npp64 8 mapaddr get nvfifo 0
5482+npput 16 1024
5483+npput 24 c
5484+npput 32 get nvmobj nvfind get nvfifo 0
5485+npp64 64 8192
5486+f = nvalc g 51567 368
5487+if eq f 0
5488+ret false
5489+end
5490+put nvobj 8 f
5491+put nvstage 0 35
5492+npzero
5493+put nvretry 0 0
5494+k = nvalc f 52928 0
5495+while eq k 0
5496+if or ne get nverr 0 81 ge get nvretry 0 50
5497+ret false
5498+end
5499+put nvretry 0 add get nvretry 0 1
5500+r = sleepns long 100000000
5501+if ne r 0
5502+put nverr 0 r
5503+ret false
5504+end
5505+k = nvalc f 52928 0
5506+end
5507+put nvobj 9 k
5508+put nverr 0 0
5509+put nvstage 0 36
5510+npzero
5511+npput 0 4294967295
5512+if not nvctrl f 3278831880 4
5513+ret false
5514+end
5515+put nvtoken 0 npget 0
5516+put nvstage 0 37
5517+if gt get nvnext 0 1099444518912
5518+put nverr 0 neg long 75
5519+ret false
5520+end
5521+v = maphold get nvnext 0 67108864
5522+if le v 0
5523+put nverr 0 v
5524+ret false
5525+end
5526+put nvchva 0 v
5527+put nvnext 0 add get nvnext 0 67108864
5528+npuuid
5529+npput 16 uint get nvfd 0
5530+npput 20 get nvobj 0
5531+npput 24 f
5532+npp64 32 mapaddr v
5533+npp64 40 67108864
5534+r = uvmctl get nvfd 2 uint 27 nvpar 0 56
5535+if not nvos r npget 48
5536+ret false
5537+end
5538+put nvchreg 0 1
5539+put nvstage 0 38
5540+npzero
5541+put nvpar 0 1
5542+if not nvctrl g 2691432705 3
5543+ret false
5544+end
5545+put nvsched 0 1
5546+put nvstage 0 39
5547+ret true
5548+end
5549+
5550+note One outstanding packet. Completion must be observed before reuse or any buffer release.
5551+fn nvsend m long off ulong words uint to bool
5552+if or ne get nvfault 0 0 eq get nvsched 0 0
5553+put nverr 0 neg long 5
5554+ret false
5555+end
5556+if ne get nvput 0 get nvret 0
5557+put nverr 0 neg long 16
5558+ret false
5559+end
5560+s = nvfind m
5561+if eq s 96
5562+put nverr 0 neg long 22
5563+ret false
5564+end
5565+if or or eq words 0 gt words 2097151 ne and off 3 0
5566+put nverr 0 neg long 22
5567+ret false
5568+end
5569+size = get nvmlen s
5570+if gt off size
5571+put nverr 0 neg long 22
5572+ret false
5573+end
5574+if gt mul ulong words 4 sub size off
5575+put nverr 0 neg long 22
5576+ret false
5577+end
5578+va = add get nvmaddr s off
5579+if ge va 1099511627776
5580+put nverr 0 neg long 75
5581+ret false
5582+end
5583+p = get nvput 0
5584+if eq p 18446744073709551615
5585+put nverr 0 neg long 75
5586+ret false
5587+end
5588+entry = or or va shl ulong words 42 2199023255552
5589+ret nvpush p entry
5590+end
5591+fn nvpush p ulong entry ulong to bool
5592+r = mapset get nvfifo 0 mul rem p 1024 8 8 entry
5593+p = add p 1
5594+put nvput 0 p
5595+r = mapset get nvfifo 0 8332 4 rem p 1024
5596+r = mapset get nvbar 0 144 4 ulong get nvtoken 0
5597+ret true
5598+end
5599+
5600+fn nvwait m long off ulong value uint budget ulong to bool
5601+if or ne get nvfault 0 0 eq get nvsched 0 0
5602+put nverr 0 neg long 22
5603+ret false
5604+end
5605+if eq get nvput 0 get nvret 0
5606+put nverr 0 neg long 22
5607+ret false
5608+end
5609+s = nvfind m
5610+if or eq s 96 or eq budget 0 gt budget 30000000000
5611+put nverr 0 neg long 22
5612+ret false
5613+end
5614+size = get nvmlen s
5615+if or gt off sub size 4 ne and off 3 0
5616+put nverr 0 neg long 22
5617+ret false
5618+end
5619+start = monons
5620+if lt start 0
5621+put nverr 0 start
5622+put nvfault 0 1
5623+ret false
5624+end
5625+while true
5626+if eq mapget m off 4 ulong value
5627+put nvret 0 get nvput 0
5628+ret true
5629+end
5630+now = monons
5631+if or lt now start ge sub now start long budget
5632+put nverr 0 neg long 110
5633+put nvfault 0 1
5634+ret false
5635+end
5636+r = sleepns long 100000
5637+if ne r 0
5638+put nverr 0 r
5639+put nvfault 0 1
5640+ret false
5641+end
5642+end
5643+ret false
5644+end
5645+note SPDX-License-Identifier: AGPL-3.0-only
5646+note Seed vecadd.s2 instruction forms, handwritten Omega emission of the same512bytes.
5647+note Each quad is one128-bit instruction. References: Omega57f5f425 seed/sm121.
5648+global gbcode uchar 4096
5649+global gblen ulong 1
5650+fn gbword v uint to void
5651+for j ulong 4
5652+put gbcode get gblen 0 uchar and shr v mul j 8 255
5653+put gblen 0 add get gblen 0 1
5654+end
5655+end
5656+fn gbquad a uint b uint c uint d uint to void
5657+gbword a
5658+gbword b
5659+gbword c
5660+gbword d
5661+end
5662+fn gbvec to void
5663+put gblen 0 0
5664+note Stack constant, block ID, block size, thread ID and element count.
5665+gbquad 4278287234 57088 2048 1040896
5666+gbquad 620825 0 9472 926208
5667+gbquad 4278482860 27648 134219776 926208
5668+gbquad 31001 0 8448 926208
5669+gbquad 4278548396 29440 134219776 942592
5670+note Linear index and bounds predicate; inactive threads exit before any access.
5671+gbquad 151616548 4 260964864 2083328
5672+gbquad 151026700 5 200302704 3136000
5673+gbquad 2381 0 58720256 1042944
5674+note Input/output addresses and memory descriptor from the constant bank.
5675+gbquad 4278352770 57344 2560 926208
5676+gbquad 4278482860 27392 134220288 945664
5677+gbquad 4278483842 57856 2560 962560
5678+gbquad 4278614914 58368 2560 975360
5679+note Address formation and independent input loads, retaining scheduling waits.
5680+gbquad 151156773 4 126746626 2083840
5681+gbquad 33716609 4 203299072 3088896
5682+gbquad 151287845 4 126746628 5229568
5683+gbquad 67467649 4 203299072 991744
5684+gbquad 151418917 4 126746630 9429504
5685+note Unsigned32-bit addition, output store, exit and unreachable self-branch guard.
5686+gbquad 34173456 5 134209791 17811968
5687+gbquad 100694406 9 202381572 1040896
5688+gbquad 31053 0 58720256 1042944
5689+gbquad 16546119 4294967292 58982399 1032192
5690+for i ulong 11
5691+gbquad 31000 0 0 1032192
5692+end
5693+end
5694+note SPDX-License-Identifier: AGPL-3.0-only
5695+note Native VECTOR_1D launch ABI continued from canonical CUDApp349913f1.
5696+note QMD, driver constant template and packet sequence: omega_blackwell_qmd.c/engine.c.
5697+global gbmem long 8
5698+global gbmax uint 1
5699+global gbq0 uint 96
5700+global gbq1 uint 96
5701+global gbcb uint 224
5702+global gbargs uint 7
5703+global gbcmd uchar 65536
5704+global gbused ulong 1
5705+fn gblo v ulong to uint
5706+ret uint and v 4294967295
5707+end
5708+fn gbhi v ulong to uint
5709+ret uint shr v 32
5710+end
5711+fn gbemit w uint to void
5712+for i ulong 4
5713+put gbcmd get gbused 0 uchar and shr w mul i 8 255
5714+put gbused 0 add get gbused 0 1
5715+end
5716+end
5717+fn gbmth subc uint method uint count uint to void
5718+gbemit or or or uint 536870912 shl count 16 shl subc 13 shr method 2
5719+end
5720+fn gbptr va ulong to void
5721+gbemit gbhi va
5722+gbemit gblo va
5723+end
5724+fn gbdriver cb ulong to void
5725+for i ulong 224
5726+put gbcb i 0
5727+end
5728+note Nonzero words of the existing observed driver template, not a new inferred ABI.
5729+put gbcb 0 641859584
5730+put gbcb 1 3
5731+put gbcb 48 2499697920
5732+put gbcb 49 3893478299
5733+put gbcb 62 1048575
5734+put gbcb 64 536870912
5735+put gbcb 65 3
5736+put gbcb 66 570425344
5737+put gbcb 67 3
5738+put gbcb 68 gbhi cb
5739+put gbcb 69 gblo cb
5740+put gbcb 70 604045312
5741+put gbcb 71 3
5742+put gbcb 80 1
5743+put gbcb 82 67158016
5744+put gbcb 83 2
5745+put gbcb 91 1024
5746+put gbcb 108 79311560
5747+put gbcb 114 1
5748+put gbcb 118 288
5749+put gbcb 126 632711424
5750+put gbcb 127 3
5751+for i ulong 3
5752+put gbcb add 128 i 1
5753+put gbcb add 132 i 1065353216
5754+end
5755+put gbcb 135 1024
5756+for i ulong 4
5757+put gbcb add 136 i 1
5758+end
5759+put gbcb 148 79311560
5760+put gbcb 154 1
5761+put gbcb 158 288
5762+put gbcb 166 632711424
5763+put gbcb 167 3
5764+for i ulong 3
5765+put gbcb add 168 i 1
5766+put gbcb add 172 i 1065353216
5767+end
5768+put gbcb 175 1024
5769+for i ulong 4
5770+put gbcb add 176 i 1
5771+end
5772+put gbcb 180 48
5773+put gbcb 183 16777216
5774+put gbcb 185 59840
5775+put gbcb 189 59840
5776+put gbcb 190 16777216
5777+put gbcb 191 59839
5778+put gbcb 210 gbhi add cb 896
5779+put gbcb 211 gblo add cb 896
5780+put gbcb 212 gbhi add cb 924
5781+put gbcb 213 gblo add cb 924
5782+put gbcb 216 64
5783+put gbcb 217 1
5784+put gbcb 218 1
5785+put gbcb 220 1
5786+put gbcb 221 1
5787+put gbcb 222 1
5788+put gbcb 223 16776640
5789+end
5790+fn gbqmd n uint to void
5791+q = mapaddr get gbmem 2
5792+cb = mapaddr get gbmem 1
5793+code = mapaddr get gbmem 4
5794+scratch = add q 16384
5795+for i ulong 96
5796+put gbq0 i 0
5797+put gbq1 i 0
5798+end
5799+put gbq0 4 8388610
5800+put gbq0 10 2293760
5801+put gbq0 12 gblo shr add q 4096 8
5802+put gbq0 14 5243392
5803+put gbq0 34 65537
5804+put gbq0 35 2049
5805+put gbq1 4 20905984
5806+put gbq1 9 3
5807+put gbq1 10 1638400
5808+put gbq1 12 gblo shr q 8
5809+put gbq1 14 793772964
5810+put gbq1 15 gblo add q 8192
5811+put gbq1 16 gbhi add q 8192
5812+put gbq1 17 6
5813+put gbq1 19 2172715008
5814+put gbq1 20 56
5815+put gbq1 22 67108864
5816+put gbq1 32 gblo shr code 4
5817+put gbq1 33 or uint and shr code 36 2097151 20971520
5818+put gbq1 34 65600
5819+put gbq1 35 135169
5820+put gbq1 36 78923784
5821+put gbq1 39 div add n 63 64
5822+put gbq1 40 1
5823+put gbq1 41 1
5824+put gbq1 42 gblo shr cb 6
5825+put gbq1 43 or uint and shr cb 38 524287 33554432
5826+put gbq1 44 gblo shr scratch 6
5827+put gbq1 45 or uint and shr scratch 38 524287 75497472
5828+put gbq1 52 gblo shr add cb 768 6
5829+put gbq1 53 or uint and shr add cb 768 38 524287 8388608
5830+put gbq1 56 gblo shr scratch 6
5831+put gbq1 57 or uint and shr scratch 38 524287 2147483648
5832+put gbq1 58 271581201
5833+put gbq1 59 gblo shr code 8
5834+gbdriver cb
5835+for i ulong 3
5836+va = mapaddr get gbmem add i 5
5837+put gbargs mul i 2 gblo va
5838+put gbargs add mul i 2 1 gbhi va
5839+end
5840+put gbargs 6 n
5841+end
5842+fn gbsetup to void
5843+gbemit 536944737
5844+gbemit 52928
5845+gbemit 536944786
5846+gbemit 1
5847+gbemit 536944808
5848+gbemit 15
5849+gbemit 536946013
5850+gbemit 3
5851+gbemit 536946014
5852+gbemit 536870912
5853+gbemit 536946015
5854+gbemit 1048575
5855+gbemit 536946007
5856+gbemit 3
5857+gbemit 536946008
5858+gbemit 570425344
5859+gbemit 536946009
5860+gbemit 0
5861+end
5862+fn gbdma va ulong count uint to void
5863+gbmth 1 392 2
5864+gbptr va
5865+gbmth 1 384 2
5866+gbemit mul count 4
5867+gbemit 1
5868+gbmth 1 432 1
5869+gbemit 65
5870+gbemit or or uint 1610621037 shl count 16 0
5871+end
5872+fn gbinline va ulong which bool to void
5873+gbmth 1 792 98
5874+gbemit or uint 1073741824 uint and shr va 40 511
5875+gbemit gblo shr va 8
5876+for i ulong 96
5877+if which
5878+gbemit get gbq1 i
5879+else
5880+gbemit get gbq0 i
5881+end
5882+end
5883+end
5884+fn gbrel va ulong value uint to void
5885+gbmth 0 92 5
5886+gbemit gblo va
5887+gbemit gbhi va
5888+gbemit value
5889+gbemit 0
5890+gbemit 1048577
5891+end
5892+fn gbbuild n uint to void
5893+gbqmd n
5894+put gbused 0 0
5895+gbsetup
5896+cb = mapaddr get gbmem 1
5897+q = mapaddr get gbmem 2
5898+mark = mapaddr get gbmem 3
5899+gbdma cb 224
5900+for i ulong 224
5901+gbemit get gbcb i
5902+end
5903+gbdma add cb 896 7
5904+for i ulong 7
5905+gbemit get gbargs i
5906+end
5907+gbinline q false
5908+gbdma add q 8192 1
5909+gbemit 1
5910+gbinline add q 4096 true
5911+gbrel mark 1145324612
5912+note C3: flush GPU L2 dirty lines, then a second WFI release before CPU readback.
5913+gbmth 0 40 4
5914+gbemit 0
5915+gbemit 0
5916+gbemit 0
5917+gbemit 2147483648
5918+gbrel add mark 16 1179010630
5919+end
5920+fn gbopen n uint to bool
5921+if or eq n 0 gt n 1048576
5922+put nverr 0 neg long 22
5923+ret false
5924+end
5925+if not nvfull
5926+ret false
5927+end
5928+if not nvchan
5929+ret false
5930+end
5931+put gbmax 0 n
5932+for i ulong 8
5933+size = ulong 4096
5934+if or eq i 0 eq i 2
5935+size = ulong 65536
5936+end
5937+if ge i 5
5938+size = mul ulong n 4
5939+end
5940+m = nvalloc size true
5941+if le m 0
5942+ret false
5943+end
5944+put gbmem i m
5945+end
5946+gbvec
5947+r = mapwrite get gbmem 4 0 gbcode 0 get gblen 0
5948+ret eq r long get gblen 0
5949+end
5950+fn gblaunch n uint to bool
5951+if or or eq n 0 gt n get gbmax 0 ne get nvfault 0 0
5952+put nverr 0 neg long 22
5953+ret false
5954+end
5955+put nvstage 0 40
5956+gbbuild n
5957+r = mapset get gbmem 2 8192 4 0
5958+r = mapset get gbmem 3 0 4 0
5959+r = mapset get gbmem 3 16 4 0
5960+r = mapwrite get gbmem 0 0 gbcmd 0 get gbused 0
5961+put nvstage 0 41
5962+if not nvsend get gbmem 0 0 uint div get gbused 0 4
5963+ret false
5964+end
5965+put nvstage 0 42
5966+if not nvwait get gbmem 3 16 1179010630 5000000000
5967+ret false
5968+end
5969+put nvstage 0 43
5970+if ne mapget get gbmem 3 0 4 1145324612
5971+put nverr 0 neg long 5
5972+put nvfault 0 1
5973+ret false
5974+end
5975+if ne mapget get gbmem 2 8192 4 6
5976+put nverr 0 neg long 5
5977+put nvfault 0 1
5978+ret false
5979+end
5980+ret true
5981+end
5982+note SPDX-License-Identifier: AGPL-3.0-only
5983+note gk.om: general sm_121 launcher and f32 kernel emitters for the Omega LLM.
5984+note Needs context.om (CUDApp native/nvrm), vector.om and launch.om (CUDApp native/blackwell), vendored
5985+note unmodified from aien-dev/CUDApp dcba8bb8 (last change 2e242169) into native/omegallm/gpu/.
5986+note QMD words follow omega_blackwell_qmd.c; control words follow omega_blackwell_codegen_gemm.c.
5987+note Each kernel id owns a 16 KiB slot of gkcode (64 slots); gkthr/gkgpr/gksm describe its launch shape.
5988+global gkcode uchar 1048576
5989+global gkat ulong 1
5990+global gkpend uint 1
5991+global gkthr uint 64
5992+global gkgpr uint 64
5993+global gksm uint 64
5994+global gkarg uint 32
5995+global gkna uint 1
5996+global gkmd uint 2
5997+fn gkw v uint to void
5998+at = get gkat 0
5999+put gkcode at uchar and v 255
6000+put gkcode add at 1 uchar and shr v 8 255
6001+put gkcode add at 2 uchar and shr v 16 255
6002+put gkcode add at 3 uchar shr v 24
6003+put gkat 0 add at 4
6004+end
6005+fn gkq a uint b uint c uint d uint to void
6006+gkw a
6007+gkw b
6008+gkw c
6009+gkw d
6010+end
6011+fn gkcw st uint wb uint rb uint wt uint to uint
6012+ret or or or shl st 9 shl wb 14 shl rb 17 shl wt 20
6013+end
6014+note Scheduling kinds: fixed, predicate, variable load, more loads, uniform load, store, more stores.
6015+fn gkfx a uint b uint c uint to void
6016+gkq a b c gkcw 6 7 7 get gkpend 0
6017+put gkpend 0 0
6018+end
6019+fn gkpr a uint b uint c uint to void
6020+gkq a b c gkcw 14 7 7 get gkpend 0
6021+put gkpend 0 0
6022+end
6023+fn gkvr a uint b uint c uint to void
6024+gkq a b c or gkcw 4 0 1 get gkpend 0 8192
6025+put gkpend 0 3
6026+end
6027+fn gkvm a uint b uint c uint to void
6028+gkq a b c or gkcw 4 0 1 and get gkpend 0 2 8192
6029+put gkpend 0 or get gkpend 0 3
6030+end
6031+fn gkvar a uint b uint c uint more bool to void
6032+if more
6033+gkvm a b c
6034+else
6035+gkvr a b c
6036+end
6037+end
6038+fn gkst a uint b uint c uint more bool to void
6039+w = get gkpend 0
6040+if more
6041+w = and w 4294967293
6042+end
6043+gkq a b c or gkcw 4 7 1 w 8192
6044+put gkpend 0 or w 2
6045+end
6046+fn gksync to void
6047+gkq 31517 0 65536 gkcw 6 7 7 get gkpend 0
6048+put gkpend 0 0
6049+end
6050+fn gkexit to void
6051+gkq 31053 0 58720256 or gkcw 5 7 7 get gkpend 0 8192
6052+put gkpend 0 0
6053+gkq 16546119 4294967292 58982399 1032192
6054+end
6055+note Instruction words, verified against nvdisasm -b SM121.
6056+fn gkldc d uint off uint more bool to void
6057+gkvar or 4278221698 shl d 16 shl div off 4 8 2048 more
6058+end
6059+fn gkldc64 d uint off uint to void
6060+gkvr or 4278221698 shl d 16 shl div off 4 8 2560
6061+end
6062+fn gks2r d uint sr uint more bool to void
6063+gkvar or 31001 shl d 16 0 shl sr 8 more
6064+end
6065+fn gkimadi d uint a uint imm uint c uint to void
6066+gkfx or or 30756 shl d 16 shl a 24 imm or 126747136 c
6067+end
6068+fn gkimad d uint a uint b uint c uint to void
6069+gkfx or or 29220 shl d 16 shl a 24 b or 126747136 c
6070+end
6071+fn gkimadw d uint a uint imm uint c uint to void
6072+gkfx or or 30757 shl d 16 shl a 24 imm or 126746624 c
6073+end
6074+fn gkimadh d uint a uint b uint c uint to void
6075+gkfx or or 29223 shl d 16 shl a 24 b or 126746624 c
6076+end
6077+fn gkshr d uint a uint n uint to void
6078+gkfx or 4278220825 shl d 16 n or 71168 a
6079+end
6080+fn gkandi d uint a uint imm uint to void
6081+gkfx or or 30738 shl d 16 shl a 24 imm 126796031
6082+end
6083+fn gkmovi d uint imm uint to void
6084+gkfx or 30722 shl d 16 imm 3840
6085+end
6086+fn gkmovz d uint to void
6087+gkfx or 29186 shl d 16 255 3840
6088+end
6089+fn gkiadd d uint a uint b uint to void
6090+gkfx or or 29200 shl d 16 shl a 24 b 134209791
6091+end
6092+note ISETP.GE P p = R a >= imm (signed); the destination predicate is word 2 bit 17, as in gkisetp
6093+fn gkisge p uint a uint imm uint to void
6094+gkpr or 30732 shl a 24 imm or 66085488 shl p 17
6095+end
6096+note ISETP P p = R a cc R b (signed; cc LT 1, EQ 2, LE 3, GT 4, NE 5, GE 6)
6097+fn gkisetp p uint a uint b uint cc uint to void
6098+gkpr or 29196 shl a 24 b or or 66060400 shl cc 12 shl p 17
6099+end
6100+note IMAD.WIDE.U32 R d:d+1 = R a * R b + R c:c+1
6101+fn gkimadwr d uint a uint b uint c uint to void
6102+gkfx or or 29221 shl d 16 shl a 24 b or 126746624 c
6103+end
6104+note @P p EXIT (no trailing self-branch: the block continues when P p is false)
6105+fn gkexitp p uint to void
6106+gkq or 2381 shl p 12 0 58720256 or gkcw 5 7 7 get gkpend 0 8192
6107+put gkpend 0 0
6108+end
6109+note @P p LDG/STG: the guard predicate is bits 12-15 of word 0 (p 7 = PT, p + 8 negates), so the
6110+note unguarded forms below are p 7 and keep their words
6111+fn gkldgp d uint a uint off uint more bool p uint to void
6112+gkvar or or or 2433 shl p 12 shl d 16 shl a 24 or 4 shl off 8 203299072 more
6113+end
6114+fn gkstgp a uint b uint off uint more bool p uint to void
6115+gkst or or 2438 shl p 12 shl a 24 or b shl off 8 202381572 more
6116+end
6117+fn gkldg d uint a uint off uint more bool to void
6118+gkldgp d a off more 7
6119+end
6120+fn gkstg a uint b uint off uint more bool to void
6121+gkstgp a b off more 7
6122+end
6123+fn gksts a uint off uint b uint more bool to void
6124+gkst or 31112 shl a 24 or b shl off 8 134220031 more
6125+end
6126+fn gklds d uint a uint off uint sb uint wt uint to void
6127+gkq or or 31108 shl d 16 shl a 24 shl off 8 3072 or gkcw 2 sb 7 wt 8192
6128+end
6129+note FFMA d = a b + c; ru is the operand reuse mask (bit 0 Ra, bit 1 Rb, bit 2 Rc; control bits 122-124).
6130+note A nonzero mask also sets control bit 109 (word-3 bit 13): nvdisasm decodes .reuse only with it set,
6131+note and ptxas (CUDA 13 sm_121) emits every .reuse instruction with it set. gkcw never sets it.
6132+fn gkffma d uint a uint b uint c uint wt uint ru uint to void
6133+y = uint 0
6134+if ne ru 0
6135+y = or shl ru 26 8192
6136+end
6137+gkq or or 29219 shl d 16 shl a 24 b c or gkcw 1 7 7 wt y
6138+end
6139+fn gkfmulf d uint a uint b uint to void
6140+gkfx or or 29216 shl d 16 shl a 24 b 4194304
6141+end
6142+fn gkffmaf d uint a uint b uint c uint to void
6143+gkfx or or 29219 shl d 16 shl a 24 b c
6144+end
6145+fn gkbra p uint tgt ulong to void
6146+here = div get gkat 0 16
6147+d = uint and sub sub tgt here 1 4294967295
6148+w1 = shl shr d 6 2
6149+c = uint 58720256
6150+if le tgt here
6151+w1 = or w1 4026531840
6152+c = 58982399
6153+end
6154+gkq or or 2375 shl p 12 shl and d 63 18 w1 c gkcw 5 7 7 get gkpend 0
6155+put gkpend 0 0
6156+end
6157+note GEMM operand setup. Mode 0 (O): thread loads outer tid&31, depth tid>>5, step depth 2.
6158+note Mode 1 (K): outer tid>>4, depth tid&15, step outer 4. Args of operand ab start at word 8*ab:
6159+note ptr lo/hi, outer stride, depth stride, batch stride 1, batch stride 2, per-load delta, tile step.
6160+fn gkopnd mode uint blk uint ab uint to void
6161+o = add uint 896 mul ab 32
6162+if eq mode 0
6163+gkandi 11 0 31
6164+gkshr 12 0 5
6165+else
6166+gkshr 11 0 4
6167+gkandi 12 0 15
6168+end
6169+gkimadi 13 11 4 255
6170+gkimadi add 92 ab 12 144 13
6171+gkimadi 13 blk 32 11
6172+gkldc 14 add o 8 false
6173+gkldc 15 add o 12 true
6174+gkimad 13 13 14 255
6175+gkimad 13 12 15 13
6176+gkldc 14 add o 16 false
6177+gkldc 15 add o 20 true
6178+gkimad 13 6 14 13
6179+gkimad 13 7 15 13
6180+gkldc64 10 o
6181+gkimadw add 88 mul ab 2 13 1 10
6182+gkldc add 97 ab add o 24 false
6183+gkldc add 99 ab add o 28 true
6184+end
6185+note One k step of the tile: prefetch k+1 fragments into the other set, then 16 FFMA on set kk&1.
6186+note Set 0 is a R32-35 b R36-39 on SB3; set 1 is a R40-43 b R44-47 on SB4. Element k accumulates in
6187+note R16+k; the FFMAs of one row share a and carry gkru.
6188+fn gkfrag k uint a uint b uint w uint ru uint to void
6189+c = add 16 k
6190+gkffma c a b c w ru
6191+end
6192+fn gkstep kk uint to void
6193+s = and kk 1
6194+if lt kk 15
6195+o = sub 1 s
6196+gklds add 32 mul o 8 94 mul add kk 1 144 add 3 o 0
6197+gklds add 36 mul o 8 95 add 2304 mul add kk 1 144 add 3 o 0
6198+end
6199+w = shl uint 1 add 3 s
6200+ra = add 32 mul s 8
6201+k = uint 0
6202+for i uint 4
6203+for j uint 4
6204+gkfrag k add ra i add add ra 4 j w gkru j 3
6205+w = 0
6206+k = add k 1
6207+end
6208+end
6209+end
6210+note Causal attention (gkmm cz). gkczx: the block exits when its column tile R2 exceeds R3 + word 28
6211+note (word 28 is 0 when only C on and below the diagonal is needed, 2^30 otherwise). gkczk: K tile
6212+note count R kn = R3 word 26 + word 22, and both operand pointers (pa, pa + 2) start R3 word 27 tiles
6213+note in (tile steps st, st + 1). Dense: word 26 = word 27 = 0. Uses R12, R13 before the main loop.
6214+fn gkczx to void
6215+gkldc 12 1008 false
6216+gkiadd 12 12 3
6217+gkisetp 0 2 12 4
6218+gkexitp 0
6219+end
6220+fn gkczk kn uint st uint pa uint to void
6221+gkldc kn 984 false
6222+gkldc 12 1000 true
6223+gkldc 13 1004 true
6224+gkimad kn 3 12 kn
6225+gkimad 13 3 13 255
6226+gkimadwr pa 13 st pa
6227+gkimadwr add pa 2 13 add st 1 add pa 2
6228+end
6229+note One 32x32 tile of C per 64-thread block, 4x4 per thread, K tiles of 16 through shared memory.
6230+note C args: ptr words 16/17, row stride 18, col stride 19, batch strides 20/21; 22 ktiles (base),
6231+note 23 batch magic, 24 minus level-2 count, 25 alpha bits, 26-28 causal (gkczx, gkczk), 30/31 the
6232+note fused epilogue's second pointer (gkope). beta true adds the old C; ep is the fused epilogue.
6233+fn gkgemm ma uint mb uint beta bool ep uint to void
6234+put gkpend 0 0
6235+gkldc 1 892 false
6236+gks2r 0 33 true
6237+gks2r 2 37 true
6238+gks2r 3 38 true
6239+gks2r 5 39 true
6240+gkq 4278482860 27392 134220288 or gkcw 4 2 7 get gkpend 0 8192
6241+put gkpend 0 4
6242+gkldc 8 988 false
6243+gkldc 9 992 true
6244+gkimadh 6 5 8 255
6245+gkimad 7 6 9 5
6246+gkczx
6247+gkshr 94 0 3
6248+gkimadi 94 94 16 255
6249+gkandi 95 0 7
6250+gkimadi 95 95 16 255
6251+gkopnd ma 3 0
6252+gkopnd mb 2 1
6253+gkczk 96 99 88
6254+gkmovi 101 4294967295
6255+for i uint 16
6256+gkmovz add 16 i
6257+end
6258+da = uint 288
6259+if eq ma 1
6260+da = 16
6261+end
6262+db = uint 288
6263+if eq mb 1
6264+db = 16
6265+end
6266+top = div get gkat 0 16
6267+put gkpend 0 63
6268+for r uint 8
6269+gkimadw add 56 mul r 2 97 r 88
6270+end
6271+for r uint 8
6272+gkimadw add 72 mul r 2 98 r 90
6273+end
6274+gkimadw 88 99 1 88
6275+gkimadw 90 100 1 90
6276+for r uint 8
6277+gkldg add 40 r add 56 mul r 2 0 ne r 0
6278+end
6279+for r uint 8
6280+gkldg add 48 r add 72 mul r 2 0 true
6281+end
6282+gksync
6283+for r uint 8
6284+gksts 92 mul r da add 40 r ne r 0
6285+end
6286+for r uint 8
6287+gksts 93 add 2304 mul r db add 48 r true
6288+end
6289+gksync
6290+gklds 32 94 0 3 0
6291+gklds 36 95 2304 3 0
6292+for kk uint 16
6293+gkstep kk
6294+end
6295+gkiadd 96 96 101
6296+gkisge 0 96 1
6297+gkbra 0 ulong top
6298+gkepi beta ep
6299+end
6300+note GEMM epilogue: C address of the thread's 4x4 block, alpha scale, optional old-C add, store.
6301+note Fused epilogue ep (gkope, beta false): 1 also stores Y = C > 0 ? C : 0 at the second pointer
6302+note (words 30/31, base R90), the FFN ReLU forward; 2 loads M at the second pointer's same offsets
6303+note into R40-55 and stores M > 0 ? C : 0, the FFN ReLU backward mask (NaN M gives 0).
6304+fn gkepi beta bool ep uint to void
6305+gkshr 12 0 3
6306+gkimadi 12 12 4 255
6307+gkimadi 12 3 32 12
6308+gkandi 13 0 7
6309+gkimadi 13 13 4 255
6310+gkimadi 13 2 32 13
6311+gkldc 14 968 false
6312+gkldc 15 972 true
6313+gkimad 12 12 14 255
6314+gkimad 12 13 15 12
6315+gkldc 10 976 false
6316+gkldc 11 980 true
6317+gkimad 12 6 10 12
6318+gkimad 12 7 11 12
6319+gkldc64 10 960
6320+gkimadw 88 12 1 10
6321+if ne ep 0
6322+gkldc64 10 1016
6323+gkimadw 90 12 1 10
6324+end
6325+gkldc 12 996 false
6326+if eq ep 2
6327+gkadr32 90
6328+for k uint 16
6329+gkldg add 40 k add 56 mul k 2 0 ne k 0
6330+end
6331+end
6332+gkadr32 88
6333+if beta
6334+for k uint 16
6335+gkldg add 40 k add 56 mul k 2 0 ne k 0
6336+end
6337+for k uint 16
6338+gkffmaf add 16 k add 16 k 12 add 40 k
6339+end
6340+else
6341+for k uint 16
6342+gkfmulf add 16 k add 16 k 12
6343+end
6344+end
6345+if eq ep 2
6346+t = gkthm 92
6347+for k uint 16
6348+gkepm add 16 k add 40 k t
6349+end
6350+end
6351+for k uint 16
6352+gkstg add 56 mul k 2 add 16 k 0 ne k 0
6353+end
6354+if eq ep 1
6355+for k uint 16
6356+gkepm add 16 k add 16 k 255
6357+end
6358+gkadr32 90
6359+for k uint 16
6360+gkstg add 56 mul k 2 add 16 k 0 ne k 0
6361+end
6362+end
6363+gkexit
6364+end
6365+note Address pairs R56-87 of the thread's 4x4 block (row stride R14, column stride R15) from base b.
6366+fn gkadr32 b uint to void
6367+for i uint 4
6368+r0 = add 56 mul i 8
6369+gkimadw r0 14 i b
6370+for j uint 3
6371+gkimadw add r0 mul add j 1 2 15 add j 1 r0
6372+end
6373+end
6374+end
6375+note Fused epilogue select: P2 = R m > R t, R e = P2 ? R e : 0 (FSETP.GT, FSEL; gr.om's encoders,
6376+note verified against nvdisasm there). t is RZ (255) except under mutant m.
6377+fn gkepm e uint m uint t uint to void
6378+grfsetp 2 m t 4
6379+grfsel e e 255 2
6380+end
6381+note Mutant m: 1 in the product, 0 in the mutant, whose fused backward mask (ep 2) is M > 2^-10 (the
6382+note forward and its stored FP unchanged). gkthm loads that threshold into R r and returns the
6383+note mask's compare register: RZ (255) in the product, r in the mutant.
6384+const OMUTM ulong 1
6385+fn gkthm r uint to uint
6386+if eq OMUTM 0
6387+gkmovi r grf 0.0009765625f64
6388+ret r
6389+end
6390+ret 255
6391+end
6392+note 64x64 GEMM: one 64x64 tile of C per 64-thread block, 8x8 per thread at rows ty*4+i and 32+ty*4+i,
6393+note columns tx*4+j and 32+tx*4+j (ty = tid>>3, tx = tid&7), K tiles of 8 through shared memory
6394+note (272-byte k rows, B at 2176). Each C element is still one FFMA chain over k in order, so results
6395+note equal the 32x32 kernel bit for bit. The K loop is software pipelined (k8): two 4352-byte shared
6396+note buffers; each tile first issues the global loads of the next tile (guarded by P1 = ktiles left
6397+note >= 2) into staging registers, runs its 64 FFMA steps from the current buffer, then stores the
6398+note staging into the other buffer behind one barrier. Registers: R16-79 accumulators; R80-111
6399+note fragments (set 0 a R80-87 b R88-95 on SB3, set 1 a R96-103 b R104-111 on SB4), later epilogue
6400+note addresses; R112-127 load addresses, later old C; R128/R130 operand pointers, R132/133 STS bases,
6401+note R134/135 LDS bases, R136 ktiles, R137/138 per-load delta, R139/140 tile step, R141 minus one,
6402+note later alpha; R142-149 A staging, R150-157 B staging; R158 buffer step (+4352 or -4352).
6403+note OMUTT: 1 in the product; 0 in mutant t, whose 64x64 kernels drop the k = 7 product of the thread's
6404+note C element (row 0, column 0).
6405+const OMUTT ulong 1
6406+note OKREU: 1 sets the Ra reuse bit (control bit 122, with bit 109) on each GEMM FFMA whose successor
6407+note reads the same a fragment; 0 (variant r0) leaves both clear as k6 did. It does not change the FFMA
6408+note chain of any C element, so both builds give the same bits. k7's window also measured a remap of C
6409+note column j to accumulator column j xor 1, which moves every k-loop b fragment and accumulator to
6410+note different low two index bits; it made no measured difference on sm_121 and was removed
6411+note (evidence/OMEGALLM/gpu-k7/).
6412+const OKREU ulong 1
6413+note gkru j last: the reuse operand mask of the FFMA at column j of a run that ends at column last.
6414+fn gkru j uint last uint to uint
6415+if and eq OKREU 1 lt j last
6416+ret 1
6417+end
6418+ret 0
6419+end
6420+note Mode 0 (O): thread loads outer tid, depths 0..7. Mode 1 (K): outer tid>>3 step 8, depth tid&7.
6421+fn gkopnd64 mode uint blk uint ab uint to void
6422+o = add uint 896 mul ab 32
6423+if eq mode 0
6424+gkandi 11 0 63
6425+gkmovz 12
6426+else
6427+gkshr 11 0 3
6428+gkandi 12 0 7
6429+end
6430+gkimadi 13 11 4 255
6431+gkimadi add 132 ab 12 272 13
6432+gkimadi 13 blk 64 11
6433+gkldc 14 add o 8 false
6434+gkldc 15 add o 12 true
6435+gkimad 13 13 14 255
6436+gkimad 13 12 15 13
6437+gkldc 14 add o 16 false
6438+gkldc 15 add o 20 true
6439+gkimad 13 6 14 13
6440+gkimad 13 7 15 13
6441+gkldc64 10 o
6442+gkimadw add 128 mul ab 2 13 1 10
6443+gkldc add 137 ab add o 24 false
6444+gkldc add 139 ab add o 28 true
6445+end
6446+fn gkstep64 kk uint to void
6447+s = and kk 1
6448+if lt kk 7
6449+o = sub 1 s
6450+d = add 80 mul o 16
6451+ko = mul add kk 1 272
6452+gklds d 134 ko add 3 o 0
6453+gklds add d 4 134 add ko 128 add 3 o 0
6454+gklds add d 8 135 add 2176 ko add 3 o 0
6455+gklds add d 12 135 add 2304 ko add 3 o 0
6456+end
6457+w = shl uint 1 add 3 s
6458+ra = add 80 mul s 16
6459+cut = and eq OMUTT 0 eq kk 7
6460+for i uint 8
6461+for j uint 8
6462+k = add mul i 8 j
6463+if or not cut ne k 0
6464+c = add 16 k
6465+u = gkru j 7
6466+gkffma c add ra i add add ra 8 j c w u
6467+w = 0
6468+end
6469+end
6470+end
6471+end
6472+note Load round q of a K tile into staging under predicate p (7 = PT): four A and four B loads (tile
6473+note loads 4q..4q+3); round 1 advances the operand pointers after its addresses are formed. Round 1
6474+note waits only for round 0's address reads.
6475+fn gkld64 q uint p uint to void
6476+if gt q 0
6477+put gkpend 0 2
6478+end
6479+l = mul q 4
6480+for r uint 4
6481+gkimadw add 112 mul r 2 137 add l r 128
6482+end
6483+for r uint 4
6484+gkimadw add 120 mul r 2 138 add l r 130
6485+end
6486+if gt q 0
6487+gkimadw 128 139 1 128
6488+gkimadw 130 140 1 130
6489+end
6490+for r uint 4
6491+gkldgp add 142 add l r add 112 mul r 2 0 ne r 0 p
6492+end
6493+for r uint 4
6494+gkldgp add 150 add l r add 120 mul r 2 0 true p
6495+end
6496+end
6497+fn gksts64 ma uint mb uint to void
6498+da = uint 272
6499+if eq ma 1
6500+da = 32
6501+end
6502+db = uint 272
6503+if eq mb 1
6504+db = 32
6505+end
6506+for r uint 8
6507+gksts 132 mul r da add 142 r ne r 0
6508+end
6509+for r uint 8
6510+gksts 133 add 2176 mul r db add 150 r true
6511+end
6512+end
6513+fn gkgemm64 ma uint mb uint beta bool ep uint to void
6514+put gkpend 0 0
6515+gkldc 1 892 false
6516+gks2r 0 33 true
6517+gks2r 2 37 true
6518+gks2r 3 38 true
6519+gks2r 5 39 true
6520+gkq 4278482860 27392 134220288 or gkcw 4 2 7 get gkpend 0 8192
6521+put gkpend 0 4
6522+gkldc 8 988 false
6523+gkldc 9 992 true
6524+gkimadh 6 5 8 255
6525+gkimad 7 6 9 5
6526+gkczx
6527+gkshr 134 0 3
6528+gkimadi 134 134 16 255
6529+gkandi 135 0 7
6530+gkimadi 135 135 16 255
6531+gkopnd64 ma 3 0
6532+gkopnd64 mb 2 1
6533+gkczk 136 139 128
6534+gkmovi 141 4294967295
6535+for i uint 64
6536+gkmovz add 16 i
6537+end
6538+put gkpend 0 63
6539+gkld64 0 7
6540+gkld64 1 7
6541+gksts64 ma mb
6542+gksync
6543+gkmovi 158 4352
6544+gkiadd 132 132 158
6545+gkiadd 133 133 158
6546+top = div get gkat 0 16
6547+gkisge 1 136 2
6548+gklds 80 134 0 3 0
6549+gklds 84 134 128 3 0
6550+gklds 88 135 2176 3 0
6551+gklds 92 135 2304 3 0
6552+gkld64 0 1
6553+gkld64 1 1
6554+for kk uint 8
6555+gkstep64 kk
6556+end
6557+gksts64 ma mb
6558+gksync
6559+gkiadd 134 134 158
6560+gkiadd 135 135 158
6561+gkimad 158 158 141 255
6562+gkiadd 132 132 158
6563+gkiadd 133 133 158
6564+gkiadd 136 136 141
6565+gkisge 0 136 1
6566+gkbra 0 ulong top
6567+gkepi64 beta ep
6568+end
6569+note Accumulator register of element k (row k>>2, column k&3) of quadrant (rh, ch).
6570+fn gkacc64 rh uint ch uint k uint to uint
6571+ret add add 16 mul add mul rh 4 shr k 2 8 add mul ch 4 and k 3
6572+end
6573+note One 4x4 quadrant of the 64x64 epilogue: address pairs R80-111 from base R128 (+32 rows R10,
6574+note +32 columns R11), alpha R141, old C in R112-127 when beta, store. Fused epilogue ep as in gkepi,
6575+note second base R142 (quadrant base R144); ep 2 loads M into R112-127 (beta is false there).
6576+fn gkquad64 rh uint ch uint beta bool ep uint to void
6577+if eq ep 2
6578+gkqadr rh ch 142 144
6579+for k uint 16
6580+gkldg add 112 k add 80 mul k 2 0 ne k 0
6581+end
6582+end
6583+gkqadr rh ch 128 130
6584+if beta
6585+for k uint 16
6586+gkldg add 112 k add 80 mul k 2 0 ne k 0
6587+end
6588+end
6589+for k uint 16
6590+e = gkacc64 rh ch k
6591+if beta
6592+gkffmaf e e 141 add 112 k
6593+else
6594+gkfmulf e e 141
6595+end
6596+end
6597+if eq ep 2
6598+t = gkthm 140
6599+for k uint 16
6600+gkepm gkacc64 rh ch k add 112 k t
6601+end
6602+end
6603+for k uint 16
6604+e = gkacc64 rh ch k
6605+gkstg add 80 mul k 2 e 0 ne k 0
6606+end
6607+if eq ep 1
6608+for k uint 16
6609+e = gkacc64 rh ch k
6610+gkepm e e 255
6611+end
6612+gkqadr rh ch 142 144
6613+for k uint 16
6614+e = gkacc64 rh ch k
6615+gkstg add 80 mul k 2 e 0 ne k 0
6616+end
6617+end
6618+end
6619+note Address pairs R80-111 of quadrant (rh, ch) from thread base b (quadrant base in R t when moved).
6620+fn gkqadr rh uint ch uint b uint t uint to void
6621+qb = b
6622+if eq rh 1
6623+gkimadw t 10 1 b
6624+qb = t
6625+end
6626+if eq ch 1
6627+gkimadw t 11 1 qb
6628+qb = t
6629+end
6630+for i uint 4
6631+r0 = add 80 mul i 8
6632+gkimadw r0 14 i qb
6633+for j uint 3
6634+gkimadw add r0 mul add j 1 2 15 add j 1 r0
6635+end
6636+end
6637+end
6638+note 64x64 epilogue: C base of the thread (R128), second base R142 when fused, quadrant offsets
6639+note R10/R11, then four quadrants.
6640+fn gkepi64 beta bool ep uint to void
6641+gkshr 12 0 3
6642+gkimadi 12 12 4 255
6643+gkimadi 12 3 64 12
6644+gkandi 13 0 7
6645+gkimadi 13 13 4 255
6646+gkimadi 13 2 64 13
6647+gkldc 14 968 false
6648+gkldc 15 972 true
6649+gkimad 12 12 14 255
6650+gkimad 12 13 15 12
6651+gkldc 10 976 false
6652+gkldc 11 980 true
6653+gkimad 12 6 10 12
6654+gkimad 12 7 11 12
6655+gkldc64 10 960
6656+gkimadw 128 12 1 10
6657+if ne ep 0
6658+gkldc64 10 1016
6659+gkimadw 142 12 1 10
6660+end
6661+gkimadi 10 14 32 255
6662+gkimadi 11 15 32 255
6663+gkldc 141 996 false
6664+for h uint 4
6665+gkquad64 shr h 1 and h 1 beta ep
6666+end
6667+gkexit
6668+end
6669+note BF16 tensor-core GEMM (ids 27..36, docs/omegallm/CONTRACT-BF16.md): one 64x64 tile of C per
6670+note 128-thread block, K tiles of 32, f32 operands rounded to bf16 (round to nearest even) by F2FP while
6671+note staging into shared memory, HMMA.16816.F32.BF16 accumulating in f32, the f32 epilogue of gkepi64.
6672+note Words verified with nvdisasm -b SM121 against ptxas 13.0 sm_121 output for mma.sync.aligned
6673+note .m16n8k16.row.col.f32.bf16.bf16.f32, cvt.rn.bf16x2.f32 and ld.global.v4.f32.
6674+note HMMA d a b c: d, c and a 4-aligned, b 2-aligned. ptxas keeps 16 cycles between the HMMAs of a warp
6675+note (stall 15, then gknop of stall 1); a dependent HMMA comes 128 cycles later here.
6676+fn gkhmma d uint a uint b uint c uint wt uint to void
6677+gkq or or 29244 shl d 16 shl a 24 b or c 268288 gkcw 15 7 7 wt
6678+gknop
6679+end
6680+fn gknop to void
6681+gkq 31000 0 0 gkcw 1 7 7 0
6682+end
6683+note F2FP.BF16.F32.PACK_AB d a b: d = bf16(a) << 16 | bf16(b), round to nearest even; rz rounds toward
6684+note zero (mutant v only). Stall 2 as ptxas.
6685+const OMUTV ulong 1
6686+fn gkf2fp d uint a uint b uint wt uint to void
6687+c = uint 4351
6688+if eq OMUTV 0
6689+c = 102655
6690+end
6691+gkq or or 29246 shl d 16 shl a 24 b c or gkcw 2 7 7 wt 8192
6692+end
6693+note LDG.E.128 R d..d+3 = [R a:a+1] under predicate p (7 = PT)
6694+fn gkldgq d uint a uint more bool p uint to void
6695+gkvar or or or 2433 shl p 12 shl d 16 shl a 24 4 203300096 more
6696+end
6697+note R d = ((tid >> s) & m) mult (+ R d unless first); R8 is the scratch.
6698+fn gkbt d uint s uint m uint mult uint first bool to void
6699+if eq s 0
6700+gkandi 8 0 m
6701+else
6702+gkshr 8 0 s
6703+gkandi 8 8 m
6704+end
6705+c = d
6706+if first
6707+c = 255
6708+end
6709+gkimadi d 8 mult c
6710+end
6711+note Operand ab of the BF16 tile. Mode 0 (O, outer stride 4): thread loads outers 4 (tid & 15) + 0..3,
6712+note depths 4 (tid >> 4) + j, j < 4 (one LDG.128 per depth, delta = depth stride). Mode 1 (K, depth
6713+note stride 4): outers (tid >> 3) + 16 i, i < 4 (delta 16 outer strides), depths 4 (tid & 7) + 0..3.
6714+note Pointer R112 + 2 ab, delta R137 + ab, tile step (32 depths) R139 + ab.
6715+fn gkopndb mode uint blk uint ab uint to void
6716+o = add uint 896 mul ab 32
6717+if eq mode 0
6718+gkandi 11 0 15
6719+gkimadi 11 11 4 255
6720+gkshr 12 0 4
6721+gkimadi 12 12 4 255
6722+else
6723+gkshr 11 0 3
6724+gkandi 12 0 7
6725+gkimadi 12 12 4 255
6726+end
6727+gkimadi 13 blk 64 11
6728+gkldc 14 add o 8 false
6729+gkldc 15 add o 12 true
6730+gkimad 13 13 14 255
6731+gkimad 13 12 15 13
6732+gkldc 14 add o 16 false
6733+gkldc 15 add o 20 true
6734+gkimad 13 6 14 13
6735+gkimad 13 7 15 13
6736+gkldc64 10 o
6737+gkimadw add 112 mul ab 2 13 1 10
6738+gkldc add 137 ab add o 24 false
6739+gkldc add 139 ab add o 28 true
6740+end
6741+note Shared memory, per 8192-byte buffer, k pair p = k >> 1 (0..15), outer r (0..63): A at
6742+note 2048 (p >> 3) + 512 (r >> 4) + 64 (r & 7) + 16 (p & 3) + 4 ((r >> 3) & 1) + 8 ((p >> 2) & 1),
6743+note B at 4096 + the same with the weights 4 and 8 exchanged, so that one LDS.128 at 512 j + 16 lane
6744+note (+ 2048 per k16 step) gives a lane its four A registers (rows g, g + 8; k 2t, 2t + 8) of m16
6745+note block j, or the two B registers of n8 blocks 2j and 2j + 1 (g = lane >> 2, t = lane & 3).
6746+note gkstsb: the thread's STS base R132 + ab for operand mode (o = 4 for A, 8 for B is the r-bit-3 weight).
6747+fn gkstsb mode uint ab uint to void
6748+d = add 132 ab
6749+ro = add 4 mul ab 4
6750+ko = sub 12 ro
6751+if eq mode 1
6752+gkbt d 2 1 2048 true
6753+gkbt d 3 7 64 false
6754+gkbt d 0 1 32 false
6755+gkbt d 6 1 ro false
6756+gkbt d 1 1 ko false
6757+else
6758+gkbt d 6 1 2048 true
6759+gkbt d 2 3 512 false
6760+gkbt d 0 1 256 false
6761+gkbt d 4 1 32 false
6762+gkbt d 1 1 ro false
6763+gkbt d 5 1 ko false
6764+end
6765+end
6766+note LDS bases: R134 = 1024 wm + 16 lane (A, warp row wm = tid >> 6), R135 = 1024 wn + 16 lane (B, warp
6767+note column wn = (tid >> 5) & 1). Mutant l (OMUTL 0) swaps the A rows of lanes g and g ^ 1.
6768+const OMUTL ulong 1
6769+fn gkldsb to void
6770+gkbt 134 6 1 1024 true
6771+gkbt 134 0 31 16 false
6772+if eq OMUTL 0
6773+gkbt 134 2 1 4294967168 false
6774+gkmovi 8 64
6775+gkiadd 134 134 8
6776+end
6777+gkbt 135 5 1 1024 true
6778+gkbt 135 0 31 16 false
6779+end
6780+note Load a K tile into staging under predicate p: A in R80-95, B in R96-111 (addresses R116-131),
6781+note then advance both operand pointers by their tile steps.
6782+fn gkldb p uint to void
6783+for i uint 4
6784+gkimadw add 116 mul i 2 137 i 112
6785+end
6786+for i uint 4
6787+gkimadw add 124 mul i 2 138 i 114
6788+end
6789+gkimadw 112 139 1 112
6790+gkimadw 114 140 1 114
6791+for i uint 4
6792+gkldgq add 80 mul i 4 add 116 mul i 2 ne i 0 p
6793+end
6794+for i uint 4
6795+gkldgq add 96 mul i 4 add 124 mul i 2 true p
6796+end
6797+end
6798+note Packed word x (0..7) of a staged operand at s: mode 1 is (i, e) = (x >> 1, x & 1), the k pair e of
6799+note outer load i; mode 0 is (jp, c) = (x >> 2, x & 3), depth pair jp of outer c. gkcvd is the packed
6800+note register (in place), gkcvl the lower-k f32 register (the hi one follows it, + 1 or + 4), gkcvo
6801+note the STS offset.
6802+fn gkcvl md uint x uint s uint to uint
6803+if eq md 1
6804+ret add add s mul shr x 1 4 mul and x 1 2
6805+end
6806+ret add add s mul shr x 2 8 and x 3
6807+end
6808+fn gkcvd md uint x uint s uint to uint
6809+if eq md 1
6810+ret add add s mul shr x 1 4 and x 1
6811+end
6812+ret gkcvl md x s
6813+end
6814+fn gkcvo md uint x uint to uint
6815+if eq md 1
6816+ret add mul shr x 1 512 mul and x 1 16
6817+end
6818+ret add mul and x 3 64 mul shr x 2 16
6819+end
6820+note Round the staged tile to bf16 pairs (the first F2FP waits for the loads), then store it.
6821+fn gkcvsb ma uint mb uint to void
6822+w = get gkpend 0
6823+put gkpend 0 0
6824+for ab uint 2
6825+md = ma
6826+if eq ab 1
6827+md = mb
6828+end
6829+s = add 80 mul ab 16
6830+hd = uint 1
6831+if eq md 0
6832+hd = 4
6833+end
6834+for x uint 8
6835+l = gkcvl md x s
6836+gkf2fp gkcvd md x s add l hd l w
6837+w = 0
6838+end
6839+end
6840+for ab uint 2
6841+md = ma
6842+if eq ab 1
6843+md = mb
6844+end
6845+gkcvst md ab add 80 mul ab 16
6846+end
6847+end
6848+note gkcvsb's stores of operand ab (mode md, staged at register s)
6849+fn gkcvst md uint ab uint s uint to void
6850+for x uint 8
6851+gksts add 132 ab add gkcvo md x mul ab 4096 gkcvd md x s or ne x 0 ne ab 0
6852+end
6853+end
6854+note Mutant o (OMUTO 0) runs one K tile fewer than the tile count.
6855+const OMUTO ulong 1
6856+fn gkgemmb ma uint mb uint beta bool ep uint to void
6857+put gkpend 0 0
6858+gkldc 1 892 false
6859+gks2r 0 33 true
6860+gks2r 2 37 true
6861+gks2r 3 38 true
6862+gks2r 5 39 true
6863+gkq 4278482860 27392 134220288 or gkcw 4 2 7 get gkpend 0 8192
6864+put gkpend 0 4
6865+gkldc 8 988 false
6866+gkldc 9 992 true
6867+gkimadh 6 5 8 255
6868+gkimad 7 6 9 5
6869+gkczx
6870+gkldsb
6871+gkstsb ma 0
6872+gkstsb mb 1
6873+gkopndb ma 3 0
6874+gkopndb mb 2 1
6875+gkczk 136 139 112
6876+gkmovi 141 4294967295
6877+if eq OMUTO 0
6878+gkiadd 136 136 141
6879+end
6880+for i uint 32
6881+gkmovz add 16 i
6882+end
6883+put gkpend 0 63
6884+gkldb 7
6885+gkcvsb ma mb
6886+gksync
6887+gkmovi 142 8192
6888+gkiadd 132 132 142
6889+gkiadd 133 133 142
6890+top = div get gkat 0 16
6891+gkisge 1 136 2
6892+for kk uint 2
6893+s = add 3 kk
6894+f = add 48 mul kk 16
6895+ko = mul kk 2048
6896+gklds f 134 ko s 0
6897+gklds add f 4 134 add ko 512 s 0
6898+gklds add f 8 135 add ko 4096 s 0
6899+gklds add f 12 135 add ko 4608 s 0
6900+end
6901+gkldb 1
6902+for kk uint 2
6903+gkmmak kk
6904+end
6905+gkcvsb ma mb
6906+gksync
6907+gkiadd 134 134 142
6908+gkiadd 135 135 142
6909+gkimad 142 142 141 255
6910+gkiadd 132 132 142
6911+gkiadd 133 133 142
6912+gkiadd 136 136 141
6913+gkisge 0 136 1
6914+gkbra 0 ulong top
6915+gkepib beta ep
6916+end
6917+note gkgemmb's eight HMMAs of k16 step kk (the first waits on barrier 3 + kk of the LDS loads)
6918+fn gkmmak kk uint to void
6919+w = shl uint 1 add 3 kk
6920+f = add 48 mul kk 16
6921+for mi uint 2
6922+for ni uint 4
6923+d = add add 16 mul mi 16 mul ni 4
6924+b = add add add f 8 mul shr ni 1 4 mul and ni 1 2
6925+gkhmma d add f mul mi 4 b d w
6926+w = 0
6927+end
6928+end
6929+end
6930+note Address pairs R48 + 2 e of the 16 elements e = 4 ni + c of one m16 half (row g + 8 (c >> 1),
6931+note column 8 ni + 2t + (c & 1)) from half base hb; row stride R14, column stride R15.
6932+fn gkhadrb hb uint to void
6933+gkimadw 52 14 8 hb
6934+gkimadw 48 15 0 hb
6935+for e uint 16
6936+if and ne e 0 ne e 2
6937+src = hb
6938+if ge and e 3 2
6939+src = 52
6940+end
6941+gkimadw add 48 mul e 2 15 add mul shr e 2 8 and e 1 src
6942+end
6943+end
6944+end
6945+note One m16 half mi of the thread's accumulators (R16 + 16 mi + e): ep 2 loads M into R80-95, beta
6946+note the old C; alpha R141; fused epilogue as gkquad64. Half bases: C R112 + 4 mi, second R114 + 4 mi.
6947+fn gkhalfb mi uint beta bool ep uint to void
6948+hc = add 112 mul mi 4
6949+hy = add 114 mul mi 4
6950+a0 = add 16 mul mi 16
6951+if eq ep 2
6952+gkhadrb hy
6953+for e uint 16
6954+gkldg add 80 e add 48 mul e 2 0 ne e 0
6955+end
6956+end
6957+gkhadrb hc
6958+if beta
6959+for e uint 16
6960+gkldg add 80 e add 48 mul e 2 0 ne e 0
6961+end
6962+end
6963+for e uint 16
6964+if beta
6965+gkffmaf add a0 e add a0 e 141 add 80 e
6966+else
6967+gkfmulf add a0 e add a0 e 141
6968+end
6969+end
6970+if eq ep 2
6971+t = gkthm 140
6972+for e uint 16
6973+gkepm add a0 e add 80 e t
6974+end
6975+end
6976+for e uint 16
6977+gkstg add 48 mul e 2 add a0 e 0 ne e 0
6978+end
6979+if eq ep 1
6980+for e uint 16
6981+gkepm add a0 e add a0 e 255
6982+end
6983+gkhadrb hy
6984+for e uint 16
6985+gkstg add 48 mul e 2 add a0 e 0 ne e 0
6986+end
6987+end
6988+end
6989+note Thread C base R112 (and second base R114 when fused): row 64 R3 + 32 wm + g, column
6990+note 64 R2 + 32 wn + 2t; half 1 bases R116 / R118 are 16 rows further.
6991+fn gkepib beta bool ep uint to void
6992+gkbt 12 6 1 32 true
6993+gkbt 12 2 7 1 false
6994+gkimadi 12 3 64 12
6995+gkbt 13 5 1 32 true
6996+gkbt 13 0 3 2 false
6997+gkimadi 13 2 64 13
6998+gkldc 14 968 false
6999+gkldc 15 972 true
7000+gkimad 12 12 14 255
7001+gkimad 12 13 15 12
7002+gkldc 10 976 false
7003+gkldc 11 980 true
7004+gkimad 12 6 10 12
7005+gkimad 12 7 11 12
7006+gkldc64 10 960
7007+gkimadw 112 12 1 10
7008+if ne ep 0
7009+gkldc64 10 1016
7010+gkimadw 114 12 1 10
7011+end
7012+gkimadw 116 14 16 112
7013+if ne ep 0
7014+gkimadw 118 14 16 114
7015+end
7016+gkldc 141 996 false
7017+for mi uint 2
7018+gkhalfb mi beta ep
7019+end
7020+gkexit
7021+end
7022+note Kernel table: ids 0..7 = 32x32 gemm (one 16 KiB slot each), ids 40, 42, ..., 54 = 64x64 gemm
7023+note (40 + 2 (ma + 2 mb + 4 beta), two slots each: the pipelined K loop does not fit in one). Fused
7024+note epilogue kernels (gkope, beta false): 25 (32x32) and 56 (64x64) ep 1 with ma 1 mb 0 (the FFN W1
7025+note forward, ggxw), 26 and 58 ep 2 with ma 1 mb 1 (the FFN dF backward, ggxwt).
7026+note gkgpr must be at least the highest GPR the kernel touches + 3: the hardware burns 2 GPRs of the
7027+note QMD count for the program counter (Mesa NAK sm70.rs hw_reserved_gprs), so R(n-2) and R(n-1) are
7028+note outside the allocation and a kernel using them hangs (launch wait -110). The 64x64 kernels use
7029+note R158 (buffer step), so 160 hung (evidence/OMEGALLM/gpu-k8 attempt 1); 168 is the next multiple of 8.
7030+note tests/omegallm/gpu-disasm.sh checks this rule per slot.
7031+fn gkslot id uint to void
7032+put gkat 0 mul ulong id 16384
7033+end
7034+fn gkall to bool
7035+for g uint 20
7036+id = g
7037+ma = and g 1
7038+mb = and shr g 1 1
7039+bt = ne and g 4 0
7040+ep = uint 0
7041+if ge g 16
7042+ep = add and g 1 1
7043+ma = 1
7044+mb = sub ep 1
7045+bt = false
7046+end
7047+big = and ge g 8 and ne g 16 ne g 17
7048+if ge g 8
7049+id = add 40 mul sub g 8 2
7050+end
7051+if ge g 16
7052+id = add 24 ep
7053+end
7054+if ge g 18
7055+id = add 54 mul ep 2
7056+end
7057+if not gkemit id ma mb bt ep big
7058+ret false
7059+end
7060+end
7061+for g uint 10
7062+ma = and g 1
7063+mb = and shr g 1 1
7064+bt = ne and g 4 0
7065+ep = uint 0
7066+if ge g 8
7067+ep = sub g 7
7068+ma = 1
7069+mb = sub ep 1
7070+bt = false
7071+end
7072+if not gkemitb add 27 g ma mb bt ep
7073+ret false
7074+end
7075+end
7076+ret true
7077+end
7078+note Emit the BF16 kernel (ma, mb, beta, ep) into slot id: ids 27 + ma + 2 mb + 4 beta, 35 ep 1 (ma 1
7079+note mb 0), 36 ep 2 (ma 1 mb 1); highest GPR R142, so 152 (multiple of 8 at least 145).
7080+fn gkemitb id uint ma uint mb uint bt bool ep uint to bool
7081+gkslot id
7082+gkgemmb ma mb bt ep
7083+put gkgpr id 152
7084+put gksm id 16384
7085+if gt get gkat 0 mul ulong add id 1 16384
7086+ret false
7087+end
7088+put gkthr id 128
7089+ret true
7090+end
7091+note Emit one GEMM kernel into slot id (two slots when big, the 64x64 kernel); false on overflow.
7092+fn gkemit id uint ma uint mb uint bt bool ep uint big bool to bool
7093+gkslot id
7094+ns = uint 1
7095+if big
7096+gkgemm64 ma mb bt ep
7097+put gkgpr id 168
7098+put gksm id 8704
7099+ns = 2
7100+else
7101+gkgemm ma mb bt ep
7102+put gkgpr id 104
7103+put gksm id 4608
7104+end
7105+if gt get gkat 0 mul ulong add id ns 16384
7106+ret false
7107+end
7108+put gkthr id 64
7109+ret true
7110+end
7111+note Launch: QMD for kernel id with grid gx gy gz; args gkarg[0..gkna).
7112+fn gkqq to void
7113+q = mapaddr get gbmem 2
7114+for i ulong 96
7115+put gbq0 i 0
7116+put gbq1 i 0
7117+end
7118+put gbq0 4 8388610
7119+put gbq0 10 2293760
7120+put gbq0 12 gblo shr add q 4096 8
7121+put gbq0 14 5243392
7122+put gbq0 34 65537
7123+put gbq0 35 2049
7124+put gbq1 4 20905984
7125+put gbq1 9 3
7126+put gbq1 10 1638400
7127+put gbq1 12 gblo shr q 8
7128+put gbq1 14 793772964
7129+put gbq1 15 gblo add q 8192
7130+put gbq1 16 gbhi add q 8192
7131+put gbq1 17 6
7132+put gbq1 19 2172715008
7133+put gbq1 20 56
7134+put gbq1 22 67108864
7135+put gbq1 58 271581201
7136+end
7137+fn gkqcb to void
7138+cb = mapaddr get gbmem 1
7139+scr = add mapaddr get gbmem 2 16384
7140+put gbq1 42 gblo shr cb 6
7141+put gbq1 43 or uint and shr cb 38 524287 33554432
7142+put gbq1 44 gblo shr scr 6
7143+put gbq1 45 or uint and shr scr 38 524287 75497472
7144+put gbq1 52 gblo shr add cb 768 6
7145+put gbq1 53 or uint and shr add cb 768 38 524287 8388608
7146+put gbq1 56 gblo shr scr 6
7147+put gbq1 57 or uint and shr scr 38 524287 2147483648
7148+gbdriver cb
7149+e = add cb add 896 mul ulong get gkna 0 4
7150+put gbcb 212 gbhi e
7151+put gbcb 213 gblo e
7152+end
7153+note QMD word 36 (QMDV05_00 bits 1152-1180): SHARED_MEMORY_SIZE_SHIFTED7, then the MIN (32 KiB, hw 9),
7154+note MAX (100 KiB, hw 26) and TARGET SM shared-memory configs, hw value = KiB / 4 + 1 from the GB10
7155+note carveouts 0 8 16 32 64 100. TARGET is the smallest carveout that holds the kernel's shared memory
7156+note times its register-limited blocks per SM, 65536 / (gkgpr * gkthr). Mesa NAK qmd.rs
7157+note gv100_get_hw_smem_sizes sizes TARGET the same way from warp-limited workgroups; the register limit is
7158+note the tighter bound here. A fixed 32 KiB TARGET under-sized the 32x32 GEMM (9 blocks * 4608 B) and the
7159+note pipelined 64x64 GEMM (6 * 8704 B): under it the pipelined kernel was 1.12x slower than the k7b one,
7160+note under this rule 0.91x (evidence/OMEGALLM/gpu-k8). Only ids with gkthr set may call this.
7161+fn gksmw id uint to uint
7162+sm = div add get gksm id 127 128
7163+if lt sm 8
7164+sm = 8
7165+end
7166+need = mul mul div 65536 mul get gkgpr id get gkthr id sm 128
7167+tg = uint 9
7168+if gt need 32768
7169+tg = 17
7170+end
7171+if gt need 65536
7172+tg = 26
7173+end
7174+ret or or 3426304 shl tg 23 sm
7175+end
7176+fn gkqmd id uint gx uint gy uint gz uint to void
7177+gkqq
7178+code = add mapaddr get gbmem 4 mul ulong id 16384
7179+put gbq1 32 gblo shr code 4
7180+put gbq1 33 or uint and shr code 36 2097151 20971520
7181+put gbq1 59 gblo shr code 8
7182+put gbq1 34 or get gkthr id 65536
7183+put gbq1 35 or or 1 shl get gkgpr id 8 131072
7184+put gbq1 36 gksmw id
7185+put gbq1 39 gx
7186+put gbq1 40 gy
7187+put gbq1 41 gz
7188+gkqcb
7189+put gbcb 216 get gkthr id
7190+end
7191+note set after host writes to the arena; the next launch invalidates GPU L2 sysmem lines first
7192+global gkinv uint 1
7193+fn gkbuild id uint gx uint gy uint gz uint to void
7194+gkqmd id gx gy gz
7195+put gbused 0 0
7196+gbsetup
7197+if ne get gkinv 0 0
7198+note L2_SYSMEM_NCOH_INVALIDATE (clc96f.h:73, MEM_OP_D operation 0x11 in bits 31:27): drop L2 lines
7199+note the host rewrote in the arena; the previous launch ended with L2_FLUSH_DIRTY, so none is dirty
7200+gbmth 0 40 4
7201+gbemit 0
7202+gbemit 0
7203+gbemit 0
7204+gbemit 2281701376
7205+put gkinv 0 0
7206+end
7207+cb = mapaddr get gbmem 1
7208+q = mapaddr get gbmem 2
7209+mark = mapaddr get gbmem 3
7210+gbdma cb 224
7211+for i ulong 224
7212+gbemit get gbcb i
7213+end
7214+na = get gkna 0
7215+gbdma add cb 896 na
7216+for i uint 32
7217+if lt i na
7218+gbemit get gkarg i
7219+end
7220+end
7221+gbinline q false
7222+gbdma add q 8192 1
7223+gbemit 1
7224+gbinline add q 4096 true
7225+gbrel mark 1145324612
7226+gbmth 0 40 4
7227+gbemit 0
7228+gbemit 0
7229+gbemit 0
7230+gbemit 2147483648
7231+gbrel add mark 16 1179010630
7232+end
7233+note Launch profile (gprof): while gkpf[0] is 1, each launch appends id gx gy gz m n k wait_ns to gkpl
7234+note (8 words, at most 1024 launches); gkmm sets gkps to its m n k first, other launches log zeros.
7235+global gkpf uint 1
7236+global gkpc ulong 1
7237+global gkpl ulong 8192
7238+global gkps uint 3
7239+fn gkplog id uint gx uint gy uint gz uint w long to void
7240+c = get gkpc 0
7241+if and eq get gkpf 0 1 lt c 1024
7242+o = mul c 8
7243+put gkpl o ulong id
7244+put gkpl add o 1 ulong gx
7245+put gkpl add o 2 ulong gy
7246+put gkpl add o 3 ulong gz
7247+put gkpl add o 4 ulong get gkps 0
7248+put gkpl add o 5 ulong get gkps 1
7249+put gkpl add o 6 ulong get gkps 2
7250+put gkpl add o 7 ulong w
7251+put gkpc 0 add c 1
7252+end
7253+put gkps 0 0
7254+put gkps 1 0
7255+put gkps 2 0
7256+end
7257+note launch accounting: 0 launches, 1 build ns, 2 wait ns (send to the QMD release semaphore),
7258+note 3 max wait ns
7259+global gkt long 4
7260+note wait for the release: spin up to 2 ms on the mark (vendored context.om nvwait sleeps 100 us
7261+note between polls, which cost each launch about 110-150 us in the rung-6 gprof ZERO round trips),
7262+note then hand the rest of the budget to nvwait, which keeps its checks, sleep and timeout
7263+fn gkwait m long off ulong value uint budget ulong to bool
7264+if eq get nvfault 0 0
7265+start = monons
7266+while lt sub monons start 2000000
7267+if eq mapget m off 4 ulong value
7268+if ne get nvput 0 get nvret 0
7269+put nvret 0 get nvput 0
7270+ret true
7271+end
7272+end
7273+end
7274+end
7275+ret nvwait m off value budget
7276+end
7277+note wait for a word nvwait cannot (it refuses once nvret has caught up with nvput): spin 2 ms,
7278+note then poll every 100 us up to budget ns; a timeout sets nverr -110 and nvfault as nvwait does
7279+fn gkpoll m long off ulong value uint budget ulong to bool
7280+if ne get nvfault 0 0
7281+put nverr 0 neg long 22
7282+ret false
7283+end
7284+start = monons
7285+while true
7286+if eq mapget m off 4 ulong value
7287+ret true
7288+end
7289+el = sub monons start
7290+if or lt el 0 ge el long budget
7291+put nverr 0 neg long 110
7292+put nvfault 0 1
7293+ret false
7294+end
7295+if ge el 2000000
7296+r = sleepns long 100000
7297+if ne r 0
7298+put nverr 0 r
7299+put nvfault 0 1
7300+ret false
7301+end
7302+end
7303+end
7304+ret false
7305+end
7306+note submit the built pushbuffer and wait for the launch to retire; returns the wait in ns, or -1
7307+fn gksend to long
7308+r = mapset get gbmem 2 8192 4 0
7309+r = mapset get gbmem 3 0 4 0
7310+r = mapset get gbmem 3 16 4 0
7311+n = get gbused 0
7312+r = mapwrite get gbmem 0 0 gbcmd 0 n
7313+put nvstage 0 41
7314+if ne r long n
7315+put nverr 0 neg long 5
7316+ret neg long 1
7317+end
7318+t1 = monons
7319+if not nvsend get gbmem 0 0 uint div n 4
7320+ret neg long 1
7321+end
7322+put nvstage 0 42
7323+if not gkwait get gbmem 3 16 1179010630 5000000000
7324+ret neg long 1
7325+end
7326+put nvstage 0 43
7327+if ne mapget get gbmem 3 0 4 1145324612
7328+put nverr 0 neg long 5
7329+put nvfault 0 1
7330+ret neg long 1
7331+end
7332+note the QMD's own release semaphore is written after the grid retires and may land after the
7333+note second mark (omega_blackwell_engine.c be_wait_release_semaphore waits for it): wait, not peek
7334+put nvstage 0 44
7335+if not gkpoll get gbmem 2 8192 6 1000000000
7336+ret neg long 1
7337+end
7338+w = sub monons t1
7339+put gkt 2 add get gkt 2 w
7340+if gt w get gkt 3
7341+put gkt 3 w
7342+end
7343+ret w
7344+end
7345+fn gklaunch id uint gx uint gy uint gz uint to bool
7346+if or or or eq gx 0 eq gy 0 eq gz 0 ne get nvfault 0 0
7347+put nverr 0 neg long 22
7348+ret false
7349+end
7350+if or eq get gkna 0 0 gt get gkna 0 32
7351+put nverr 0 neg long 22
7352+ret false
7353+end
7354+put nvstage 0 40
7355+t0 = monons
7356+gkbuild id gx gy gz
7357+put gkt 0 add get gkt 0 1
7358+put gkt 1 add get gkt 1 sub monons t0
7359+w = gksend
7360+if lt w 0
7361+ret false
7362+end
7363+gkplog id gx gy gz w
7364+ret true
7365+end
7366+note Open the chip: slots 0 cmd, 1 cbank, 2 qmd, 3 marks, 4 code, 5 arena of asz bytes.
7367+fn gkopen asz ulong to bool
7368+if not nvfull
7369+ret false
7370+end
7371+if not nvchan
7372+ret false
7373+end
7374+for i ulong 6
7375+size = ulong 4096
7376+if or eq i 0 eq i 2
7377+size = 65536
7378+end
7379+if eq i 4
7380+size = 1048576
7381+end
7382+if eq i 5
7383+size = asz
7384+end
7385+note slots 0-4 (pushbuffer, cbank, QMD, marks, code) are CPU-written or host-polled: GPU-uncached
7386+note as in omega_blackwell_engine.c alloc_checked; cached, a release sat in L2 about 1.06 s (cortex
7387+note d03a999a). The arena (slot 5) stays cached: gkinv invalidates it after host writes and every
7388+note launch ends with L2_FLUSH_DIRTY before its second mark.
7389+m = nvalloc size eq i 5
7390+if le m 0
7391+ret false
7392+end
7393+put gbmem i m
7394+end
7395+if not gkall
7396+put nverr 0 neg long 7
7397+ret false
7398+end
7399+r = mapwrite get gbmem 4 0 gkcode 0 1048576
7400+ret eq r long 1048576
7401+end
7402+fn gkbase to ulong
7403+ret mapaddr get gbmem 5
7404+end
7405+note GEMM host side. Operand ab (0 = A, 1 = B): device address, outer/depth/batch strides in bytes.
7406+note Mode 1 (K) when the depth stride is 4; gkmm derives per-load delta and tile step for the tile.
7407+fn gkopa ab uint ptr ulong so uint sk uint s1 uint s2 uint to void
7408+o = mul ab 8
7409+put gkarg o gblo ptr
7410+put gkarg add o 1 gbhi ptr
7411+put gkarg add o 2 so
7412+put gkarg add o 3 sk
7413+put gkarg add o 4 s1
7414+put gkarg add o 5 s2
7415+md = uint 0
7416+if eq sk 4
7417+md = 1
7418+end
7419+put gkmd ab md
7420+end
7421+fn gkopc ptr ulong scm uint scn uint s1 uint s2 uint al f32 to void
7422+put gkarg 16 gblo ptr
7423+put gkarg 17 gbhi ptr
7424+put gkarg 18 scm
7425+put gkarg 19 scn
7426+put gkarg 20 s1
7427+put gkarg 21 s2
7428+put gkarg 25 bitcast uint al
7429+end
7430+note C[z] = alpha A[z] B[z] (+ C[z] if beta), z = z1 h2 + z2, z < nz; m n multiples of 32, k of 16.
7431+note m and n both multiples of 64 take the 64x64 kernels (ids 40..54 even), else the 32x32 ones (ids 0..7).
7432+fn gkshape m uint n uint k uint nz uint h2 uint to bool
7433+if ne rem m 32 0
7434+ret false
7435+end
7436+if ne rem n 32 0
7437+ret false
7438+end
7439+if or ne rem k 16 0 eq k 0
7440+ret false
7441+end
7442+if or eq nz 0 gt nz 65535
7443+ret false
7444+end
7445+ret ne h2 0
7446+end
7447+note Batch split magic: z1 = umulhi(z, mg) must equal z / h2 for every z < nz; else nverr -33.
7448+fn gkmagic nz uint h2 uint to uint
7449+mg = uint 0
7450+if gt h2 1
7451+mg = uint div add 4294967296 ulong sub h2 1 ulong h2
7452+end
7453+ok = true
7454+for z uint nz
7455+if ne uint shr mul ulong z ulong mg 32 div z h2
7456+ok = false
7457+end
7458+end
7459+if not ok
7460+put nverr 0 neg long 33
7461+end
7462+ret mg
7463+end
7464+note Per-load delta (word 6) and tile step (word 7) of operand ab for tile width t (32 or 64).
7465+fn gkopt ab uint t uint to void
7466+o = mul ab 8
7467+so = get gkarg add o 2
7468+sk = get gkarg add o 3
7469+gd = mul sk 2
7470+st = mul sk 16
7471+if eq t 64
7472+gd = sk
7473+st = mul sk 8
7474+end
7475+if eq get gkmd ab 1
7476+gd = mul so 4
7477+if eq t 64
7478+gd = mul so 8
7479+end
7480+end
7481+put gkarg add o 6 gd
7482+put gkarg add o 7 st
7483+end
7484+note Tile width: 64 when m and n are both multiples of 64, else 32.
7485+fn gktile m uint n uint to uint
7486+if and eq rem m 64 0 eq rem n 64 0
7487+ret 64
7488+end
7489+ret 32
7490+end
7491+note Causal mode cz, set by gkopz before gkmm (attention over a T x T score matrix, zero above its
7492+note diagonal); gkmm consumes it, so a GEMM without gkopz is dense. 0 dense; 1 A is
7493+note zero above its diagonal (C = P U, DQ = DS K), so row tile r stops after K column (r + 1) t;
7494+note 2 A is zero below its diagonal (C = P^T DO, DK = DS^T Q), so row tile r starts at K row r t;
7495+note 3 only C on and below the diagonal is needed (S = Q K^T, DP = DO U^T), so blocks above it exit.
7496+note Modes 1 and 2 need k = m, mode 3 needs m = n. Mutant q (OMUTQ 0) stops mode 1 one K tile short.
7497+const OMUTQ ulong 1
7498+note BF16 path (docs/omegallm/CONTRACT-BF16.md): gkbf 1 (gtrainb, gprofb and the gb.om gates) runs every
7499+note 64x64 gkmm on the tensor-core kernels 27..36; 32x32 GEMMs (n = E) stay f32. gkbck 1 checks each
7500+note BF16 launch against gb.om's sampled reference (gbpre before, gbpost after), with gkbp holding
7501+note m n k nz h2 beta ep cz. No fallback: a GEMM the kernels cannot take fails with nverr -22.
7502+global gkbf uint 1
7503+global gkbck uint 1
7504+global gkbp uint 8
7505+global gkcz uint 1
7506+fn gkopz cz uint to void
7507+put gkcz 0 cz
7508+end
7509+fn gkczok m uint n uint k uint cz uint to bool
7510+if eq cz 0
7511+ret true
7512+end
7513+if le cz 2
7514+ret eq k m
7515+end
7516+if eq cz 3
7517+ret eq m n
7518+end
7519+ret false
7520+end
7521+note GEMM K-loop words: 22 K tiles (kd deep), 26 ka, 27 kc, 28 X for tile t and the gkopz mode,
7522+note which it consumes
7523+fn gkczarg t uint k uint to void
7524+cz = get gkcz 0
7525+put gkcz 0 0
7526+kd = uint 16
7527+if eq t 64
7528+kd = 8
7529+if eq get gkbf 0 1
7530+kd = 32
7531+end
7532+end
7533+pt = div t kd
7534+put gkarg 22 div k kd
7535+put gkarg 26 0
7536+put gkarg 27 0
7537+put gkarg 28 1073741824
7538+if eq cz 1
7539+put gkarg 22 pt
7540+put gkarg 26 pt
7541+if eq OMUTQ 0
7542+put gkarg 22 sub pt 1
7543+end
7544+end
7545+if eq cz 2
7546+put gkarg 26 neg pt
7547+put gkarg 27 pt
7548+end
7549+if eq cz 3
7550+put gkarg 28 0
7551+end
7552+end
7553+note Fused epilogue ep for the next gkmm, which consumes it (see gkepi): 1 also stores
7554+note Y = C > 0 ? C : 0 at y, 2 stores M > 0 ? C : 0 with M read at y; y has C's strides. ep 1 needs
7555+note operand modes ma 1 mb 0 and ep 2 ma 1 mb 1 (the only fused kernels, gkall), both beta false.
7556+note Measured (k9, evidence/OMEGALLM/gpu-k9): replaces gr.om RELUF/RELUB, 16 launches per max-shape step
7557+note fewer; summed GPU wait 144.56 -> 133.54 ms/step (-7.62%, three rounds each, non-quiet); t200 checkpoint
7558+note bit-identical to the unfused k8 product.
7559+global gkep uint 1
7560+fn gkope ep uint y ulong to void
7561+put gkep 0 ep
7562+put gkarg 30 gblo y
7563+put gkarg 31 gbhi y
7564+end
7565+note true when fused epilogue ep may run with the pending operand modes and beta
7566+fn gkepok ep uint beta bool to bool
7567+if eq ep 0
7568+ret true
7569+end
7570+if or gt ep 2 beta
7571+ret false
7572+end
7573+if ne get gkmd 0 1
7574+ret false
7575+end
7576+ret eq get gkmd 1 sub ep 1
7577+end
7578+fn gkmm m uint n uint k uint nz uint h2 uint beta bool to bool
7579+ep = get gkep 0
7580+put gkep 0 0
7581+if not gkepok ep beta
7582+put gkcz 0 0
7583+put nverr 0 neg long 22
7584+ret false
7585+end
7586+if or not gkshape m n k nz h2 not gkczok m n k get gkcz 0
7587+put gkcz 0 0
7588+put nverr 0 neg long 22
7589+ret false
7590+end
7591+put nverr 0 0
7592+put gkarg 23 gkmagic nz h2
7593+if ne get nverr 0 0
7594+put gkcz 0 0
7595+ret false
7596+end
7597+put gkarg 24 neg h2
7598+t = gktile m n
7599+bf = and eq get gkbf 0 1 eq t 64
7600+if bf
7601+if not gkbfok k
7602+put gkcz 0 0
7603+put nverr 0 neg long 22
7604+ret false
7605+end
7606+else
7607+gkopt 0 t
7608+gkopt 1 t
7609+end
7610+put gkbp 7 get gkcz 0
7611+gkczarg t k
7612+put gkps 0 m
7613+put gkps 1 n
7614+put gkps 2 k
7615+id = gkmmid t beta ep
7616+if bf
7617+id = gkmmb beta ep
7618+end
7619+if not and bf eq get gkbck 0 1
7620+ret gklaunch id div n t div m t nz
7621+end
7622+gkbpset m n k nz h2 beta
7623+put gkbp 6 ep
7624+gbpre
7625+if not gklaunch id div n t div m t nz
7626+ret false
7627+end
7628+ret gbpost
7629+end
7630+note BF16 operands both take the 16-byte tile (gkbfop) and k is a multiple of the K tile 32
7631+fn gkbfok k uint to bool
7632+ba = gkbfop 0
7633+bb = gkbfop 1
7634+ret and and ba bb eq rem k 32 0
7635+end
7636+note gkbp words 0..5 for gb.om's check: m n k nz h2 beta
7637+fn gkbpset m uint n uint k uint nz uint h2 uint beta bool to void
7638+put gkbp 0 m
7639+put gkbp 1 n
7640+put gkbp 2 k
7641+put gkbp 3 nz
7642+put gkbp 4 h2
7643+put gkbp 5 0
7644+if beta
7645+put gkbp 5 1
7646+end
7647+end
7648+note Operand ab for the BF16 tile: 16-byte aligned pointer and batch strides, and 16-byte loads
7649+note (mode 1: so a multiple of 16; mode 0: so = 4, sk a multiple of 16); sets delta and tile step.
7650+fn gkbfop ab uint to bool
7651+o = mul ab 8
7652+so = get gkarg add o 2
7653+sk = get gkarg add o 3
7654+ok = eq rem get gkarg o 16 0
7655+ok = and ok eq rem get gkarg add o 4 16 0
7656+ok = and ok eq rem get gkarg add o 5 16 0
7657+if eq get gkmd ab 1
7658+ok = and ok eq rem so 16 0
7659+put gkarg add o 6 mul so 16
7660+else
7661+ok = and ok and eq so 4 eq rem sk 16 0
7662+put gkarg add o 6 sk
7663+end
7664+put gkarg add o 7 mul sk 32
7665+ret ok
7666+end
7667+note BF16 kernel id for beta and fused epilogue ep; sets the arg count
7668+fn gkmmb beta bool ep uint to uint
7669+if ne ep 0
7670+put gkna 0 32
7671+ret add 34 ep
7672+end
7673+put gkna 0 29
7674+id = add add 27 get gkmd 0 mul 2 get gkmd 1
7675+if beta
7676+id = add id 4
7677+end
7678+ret id
7679+end
7680+note kernel id of a gkmm (gkall's table) for tile t, beta and fused epilogue ep; sets the arg count
7681+fn gkmmid t uint beta bool ep uint to uint
7682+if ne ep 0
7683+put gkna 0 32
7684+if eq t 64
7685+ret add 54 mul ep 2
7686+end
7687+ret add 24 ep
7688+end
7689+put gkna 0 29
7690+id = add get gkmd 0 mul 2 get gkmd 1
7691+if beta
7692+id = add id 4
7693+end
7694+if eq t 64
7695+id = add 40 mul id 2
7696+end
7697+ret id
7698+end
7699+note SPDX-License-Identifier: AGPL-3.0-only
7700+note gr.om: row and elementwise SM121 kernels for the ollm GPU step (slots 9..24), emitted after
7701+note gkall with the model dimensions baked in as immediates. Words verified with nvdisasm -b SM121.
7702+note Launch shape: 64 threads per block, R4 = ctaid.x 64 + tid.x, R3 = ctaid.y, R7 = ctaid.z;
7703+note arg 0 is the R4 bound (threads at or past it exit). Registers: R8-R23 pointers and ints,
7704+note R24-R29 constants, R26 = 1, R30 loop counter, R31 = -1, R32-R63 data, R64-R69 scalars.
7705+global grd ulong 8
7706+note grd: 0 V 1 T 2 D 3 H 4 F 5 B
7707+fn grf v f64 to uint
7708+ret bitcast uint f32 v
7709+end
7710+fn grarg i uint to uint
7711+ret add 896 mul i 4
7712+end
7713+fn grfsetp p uint a uint b uint cc uint to void
7714+gkpr or 29195 shl a 24 b or or 66060288 shl cc 12 shl p 17
7715+end
7716+fn grfsel d uint a uint b uint p uint to void
7717+gkfx or or 29192 shl d 16 shl a 24 b shl p 23
7718+end
7719+fn grfmax d uint a uint b uint to void
7720+gkfx or or 29193 shl d 16 shl a 24 b 125829120
7721+end
7722+fn grfadd d uint a uint b uint to void
7723+gkfx or or 29217 shl d 16 shl a 24 b 0
7724+end
7725+fn grfsub d uint a uint b uint to void
7726+gkfx or or 29217 shl d 16 shl a 24 or b 2147483648 0
7727+end
7728+fn grmufu d uint a uint op uint to void
7729+gkvr or 29448 shl d 16 a op
7730+end
7731+note pointer number i (odd) sits in arg slots i+1, i+2 so its LDC.64 is 8-byte aligned
7732+fn grptr d uint i uint idx uint scale uint to void
7733+gkldc64 d grarg add i 1
7734+gkimadw d idx scale d
7735+end
7736+fn grtop c uint n uint to ulong
7737+gkmovi c n
7738+ret div get gkat 0 16
7739+end
7740+fn grbot c uint top ulong to void
7741+gkiadd c c 31
7742+gkisge 0 c 1
7743+gkbra 0 top
7744+end
7745+fn gradv p uint n uint to void
7746+gkimadw p 26 n p
7747+end
7748+fn grld8 d uint p uint first bool to void
7749+for u uint 8
7750+gkldg add d u p mul u 4 or ne u 0 not first
7751+end
7752+end
7753+fn grst8 p uint s uint to void
7754+for u uint 8
7755+gkstg p add s u mul u 4 ne u 0
7756+end
7757+end
7758+fn grpro to void
7759+gkldc 1 892 false
7760+gks2r 0 33 true
7761+gks2r 2 37 true
7762+gks2r 3 38 true
7763+gks2r 7 39 true
7764+gkq 4278482860 27392 134220288 or gkcw 4 2 7 get gkpend 0 8192
7765+put gkpend 0 4
7766+gkimadi 4 2 64 0
7767+gkldc 5 896 false
7768+gkisetp 0 4 5 6
7769+gkexitp 0
7770+gkmovi 31 4294967295
7771+gkmovi 26 1
7772+end
7773+note 9 EMB: out[b T + t][i] = wte[x[b T + t]][i] + wpe[t][i]; grid (D/64, T, B); args D out wte wpe x
7774+fn gremb to void
7775+d = uint get grd 2
7776+t = uint get grd 1
7777+grpro
7778+gkimadi 8 7 t 3
7779+grptr 12 7 8 4
7780+gkldg 9 12 0 false
7781+gkimadi 10 9 d 4
7782+grptr 14 3 10 4
7783+gkimadi 11 3 d 4
7784+grptr 16 5 11 4
7785+gkimadi 20 8 d 4
7786+grptr 18 1 20 4
7787+gkldg 32 14 0 false
7788+gkldg 33 16 0 true
7789+grfadd 34 32 33
7790+gkstg 18 34 0 false
7791+gkexit
7792+end
7793+note RMS kernels: one warp per row (threads R*32, arg 0); lane l holds columns l + 32 u, u < D/32,
7794+note so each warp load and store is one coalesced 128-byte line and every row is read once.
7795+note R8 = row, R9 = lane; ld/st w: n words at p + 128 u (gkldg/gkstg chain flag: first false).
7796+fn grrrow to void
7797+grpro
7798+gkshr 8 4 5
7799+gkandi 9 4 31
7800+end
7801+fn grrptr d uint i uint d4 uint to void
7802+grptr d i 8 d4
7803+gkimadw d 9 4 d
7804+end
7805+fn grldw d uint p uint n uint first bool to void
7806+for u uint n
7807+gkldg add d u p mul u 128 or ne u 0 not first
7808+end
7809+end
7810+fn grstw p uint s uint n uint to void
7811+for u uint n
7812+gkstg p add s u mul u 128 ne u 0
7813+end
7814+end
7815+note RMS register bank k (k < 4) of n = D/32 words: R32 + 8 k while n <= 8 (the v1 layout, scalars at
7816+note R64..R67 above it); from n = 9 the banks sit above the scalars at R72 + n k, so D <= 384 (n <= 12)
7817+note ends at R119 and grall raises the slot's GPR count to 128 (gk.om: highest GPR + 3, multiple of 8).
7818+fn grrb k uint n uint to uint
7819+if le n 8
7820+ret add 32 mul 8 k
7821+end
7822+ret add 72 mul n k
7823+end
7824+note 10 RMSF: per row r < R: s = 1/sqrt(sum v^2 / D + 1e-5); y = v s g; args R*32 v g y s
7825+fn grrmsf to void
7826+d = uint get grd 2
7827+d4 = mul d 4
7828+n = div d 32
7829+grrrow
7830+gkmovi 24 grf div 1.0f64 f64 d
7831+gkmovi 25 grf 0.00001f64
7832+grrptr 10 1 d4
7833+gkldc64 14 grarg 4
7834+gkimadw 14 9 4 14
7835+b0 = grrb 0 n
7836+b1 = grrb 1 n
7837+grldw b0 10 n true
7838+grldw b1 14 n false
7839+gkmovz 64
7840+for u uint n
7841+gkffmaf 64 add b0 u add b0 u 64
7842+end
7843+grwred 0 0
7844+gkffmaf 65 64 24 25
7845+grmufu 66 65 5120
7846+grptr 12 7 8 4
7847+gkstg 12 66 0 false
7848+grrptr 16 5 d4
7849+for u uint n
7850+gkfmulf add b0 u add b0 u 66
7851+gkfmulf add b0 u add b0 u add b1 u
7852+end
7853+grstw 16 b0 n
7854+gkexit
7855+end
7856+note Mutant r: 1 in the product, 0 in the mutant, whose RMSB row sum sum(da g v) skips the lane-1
7857+note butterfly step (k1 from half the row), which gcmp2 must catch.
7858+const OMUTR ulong 1
7859+note 11 RMSB: per row: k1 = s^2 sum(da g v)/D; dh += (da g - v k1) s; w = da v s; args R*32 v g s da dh w
7860+fn grrmsb to void
7861+d = uint get grd 2
7862+d4 = mul d 4
7863+n = div d 32
7864+grrrow
7865+gkmovi 24 grf div 1.0f64 f64 d
7866+grrptr 10 1 d4
7867+gkldc64 12 grarg 4
7868+gkimadw 12 9 4 12
7869+grrptr 14 7 d4
7870+grrptr 16 9 d4
7871+grptr 20 5 8 4
7872+b0 = grrb 0 n
7873+b1 = grrb 1 n
7874+b2 = grrb 2 n
7875+b3 = grrb 3 n
7876+grldw b0 14 n true
7877+grldw b1 12 n false
7878+grldw b2 10 n false
7879+grldw b3 16 n false
7880+gkldg 67 20 0 true
7881+gkmovz 64
7882+for u uint n
7883+gkfmulf add b1 u add b0 u add b1 u
7884+gkffmaf 64 add b1 u add b2 u 64
7885+end
7886+if eq OMUTR 0
7887+grwred 0 1
7888+else
7889+grwred 0 0
7890+end
7891+gkfmulf 66 67 67
7892+gkfmulf 66 66 64
7893+gkfmulf 66 66 24
7894+grrptr 18 11 d4
7895+for u uint n
7896+gkfmulf add b0 u add b0 u add b2 u
7897+gkfmulf add b0 u add b0 u 67
7898+gkfmulf add b2 u add b2 u 66
7899+grfsub add b1 u add b1 u add b2 u
7900+gkfmulf add b1 u add b1 u 67
7901+grfadd add b3 u add b3 u add b1 u
7902+end
7903+grstw 16 b3 n
7904+grstw 18 b0 n
7905+gkexit
7906+end
7907+note 13/14 COLSUM: out[c ostr + i] (=, +=) sum_k in[c cstr + k kstr + i]; grid (n/64, chunks)
7908+note arg slots: 0 n, 1 kcnt (>= 1), 2-3 in, 4 kstr, 5 cstr, 6-7 out, 8 ostr (strides in bytes)
7909+fn grcol acc bool to void
7910+grpro
7911+grptr 10 1 4 4
7912+gkldc 20 grarg 5 false
7913+gkimadwr 10 3 20 10
7914+gkldc 21 grarg 4 false
7915+gkldc 30 grarg 1 false
7916+gkmovz 64
7917+top = div get gkat 0 16
7918+gkldg 32 10 0 false
7919+grfadd 64 64 32
7920+gkimadwr 10 26 21 10
7921+grbot 30 top
7922+grptr 12 5 4 4
7923+gkldc 22 grarg 8 false
7924+gkimadwr 12 3 22 12
7925+if acc
7926+gkldg 33 12 0 false
7927+grfadd 64 64 33
7928+end
7929+gkstg 12 64 0 false
7930+gkexit
7931+end
7932+note SHFL.BFLY PT, Rd, Ra, lane, 0x1f (codegen.c:671 SHFL.DOWN with the BFLY mode bits, sm121.tbl:183)
7933+fn grshfl d uint a uint lane uint to void
7934+gkvr or or 32649 shl d 16 shl a 24 or 201334528 shl lane 21 917504
7935+end
7936+note Softmax kernels: one warp per row; lane l holds columns l + 32 u, u < T/32, in R32.. (p) and
7937+note R48.. (dp), so every load and store of a warp is one coalesced 128-byte line. Threads: T 32
7938+note per score block (arg 0), grid (T/2, Z); R21 = row t, R22 = lane, R10/R12 = row base + 4 lane.
7939+fn grsrow tt uint dp bool to void
7940+grpro
7941+gkshr 21 4 5
7942+gkandi 22 4 31
7943+gkimadi 8 3 tt 21
7944+grptr 10 1 8 mul tt 4
7945+gkimadw 10 22 4 10
7946+if dp
7947+grptr 12 3 8 mul tt 4
7948+gkimadw 12 22 4 12
7949+end
7950+end
7951+note sum (op 0) or max (op 1) of R64 across the warp by butterfly, R65 scratch
7952+fn grwred op uint skip uint to void
7953+for i uint 5
7954+o = uint shr 16 i
7955+if ne o skip
7956+grshfl 65 64 o
7957+if eq op 0
7958+grfadd 64 64 65
7959+else
7960+grfmax 64 64 65
7961+end
7962+end
7963+end
7964+end
7965+note Mutant s: 1 in the product, 0 in the mutant, whose SOFTF row sum skips the lane-1 butterfly step
7966+note (each lane then normalizes by half the row), which gcmp and gcmp2 must catch.
7967+const OMUTS ulong 1
7968+note Causal chunks (k6): row t (R21) loads 32-column chunk u only if t >= 32 u (P u-1 for u 1..7;
7969+note chunk 0 and chunks from 8 on stay PT, loaded and masked), and stores chunk u only if
7970+note t >= 64 floor(u/2). The causal GEMMs read a P or DS row only up to the end of its diagonal tile
7971+note (32 or 64 columns), so later chunks are neither read nor written and keep stale arena data.
7972+note Predicates are set before the loads: an ISETP between them would wait on their scoreboard.
7973+note Mutant w: 1 in the product, 0 in the mutant, whose loads also skip the diagonal chunk (t >= 32 u + 32).
7974+const OMUTW ulong 1
7975+fn grcpred n uint off uint to void
7976+for u uint n
7977+if and ge u 1 le u 7
7978+gkisge sub u 1 21 add mul u 32 off
7979+end
7980+end
7981+end
7982+fn grcp u uint to uint
7983+if or eq u 0 ge u 8
7984+ret 7
7985+end
7986+ret sub u 1
7987+end
7988+fn grcwo to uint
7989+if eq OMUTW 0
7990+ret 32
7991+end
7992+ret 0
7993+end
7994+note 15 SOFTF: causal softmax of row t of score block z in place, zero above t; args T*32 P
7995+fn grsoftf to void
7996+tt = uint get grd 1
7997+n = div tt 32
7998+grsrow tt false
7999+gkmovi 27 4286578688
8000+gkmovi 28 grf 1.4426950408889634f64
8001+gkmovi 29 grf neg 1.4426950408889634f64
8002+gkmovi 23 32
8003+grcpred n grcwo
8004+for u uint n
8005+gkldgp add 32 u 10 mul u 128 ne u 0 grcp u
8006+end
8007+gkimadi 20 22 1 255
8008+gkmovi 64 4286578688
8009+for u uint n
8010+gkisetp 1 20 21 4
8011+grfsel add 32 u 27 add 32 u 1
8012+gkiadd 20 20 23
8013+grfmax 64 64 add 32 u
8014+end
8015+grwred 1 0
8016+gkfmulf 66 64 29
8017+gkmovz 64
8018+for u uint n
8019+gkffmaf add 32 u add 32 u 28 66
8020+grmufu add 32 u add 32 u 2048
8021+grfadd 64 64 add 32 u
8022+end
8023+if eq OMUTS 0
8024+grwred 0 1
8025+else
8026+grwred 0 0
8027+end
8028+grmufu 67 64 4096
8029+grcpred n 0
8030+for u uint n
8031+gkfmulf add 32 u add 32 u 67
8032+gkstgp 10 add 32 u mul u 128 ne u 0 grcp and u 4294967294
8033+end
8034+gkexit
8035+end
8036+note 16 SOFTB: ds = p (dp - sum p dp) / sqrt(E) in place over dp; args T*32 P DP. p and dp above the
8037+note diagonal (column > t) read as 0: their chunks may be unloaded (k6) or stale (the causal DP GEMM,
8038+note gkmm cz 3, skips those tiles), and 0 times a stale Inf or NaN is NaN.
8039+fn grsoftb to void
8040+tt = uint get grd 1
8041+n = div tt 32
8042+e = div get grd 2 get grd 3
8043+grsrow tt true
8044+gkmovi 29 grf div 1.0f64 fsqrt f64 e
8045+grcpred n grcwo
8046+for u uint n
8047+gkldgp add 32 u 10 mul u 128 ne u 0 grcp u
8048+end
8049+for u uint n
8050+gkldgp add 48 u 12 mul u 128 true grcp u
8051+end
8052+gkmovi 23 32
8053+gkimadi 20 22 1 255
8054+for u uint n
8055+gkisetp 1 20 21 4
8056+grfsel add 48 u 255 add 48 u 1
8057+grfsel add 32 u 255 add 32 u 1
8058+gkiadd 20 20 23
8059+end
8060+gkmovz 64
8061+for u uint n
8062+gkffmaf 64 add 32 u add 48 u 64
8063+end
8064+grwred 0 0
8065+grcpred n 0
8066+for u uint n
8067+grfsub add 48 u add 48 u 64
8068+gkfmulf add 48 u add 48 u add 32 u
8069+gkfmulf add 48 u add 48 u 29
8070+gkstgp 12 add 48 u mul u 128 ne u 0 grcp and u 4294967294
8071+end
8072+gkexit
8073+end
8074+note 19 COPY y = x; 20 ZERO; args n then pointers. The FFN ReLU forward and backward mask are fused
8075+note into the GEMM epilogue (gk.om gkepi, kernels 25/26/56/58), so slots 17/18 stay empty.
8076+fn grew kind uint to void
8077+grpro
8078+grptr 10 1 4 4
8079+if eq kind 20
8080+gkstg 10 255 0 false
8081+else
8082+grptr 12 3 4 4
8083+gkldg 32 10 0 false
8084+grfadd 34 32 255
8085+gkstg 12 34 0 false
8086+end
8087+gkexit
8088+end
8089+note 21 CE: per row r of logits L (R x V): m, e = exp(l - m), sum; L <- (e/sum - [c = y]) sc;
8090+note stats[r] = (m, sum, l_y); sc = 1/(T B); args R*32 L y st. One warp per row (grrrow): lane l
8091+note holds columns l + 32 u, read as 8 coalesced 128-byte lines per step of 256 columns. Pass 1 takes
8092+note the lane max, then the warp max. Pass 2 sums e per lane without storing it, then the warp sum
8093+note of the lane totals. Pass 3 recomputes e with the same FFMA and MUFU (bit-identical to pass 2),
8094+note scales it and stores it. The lane that owns column y (lane y & 31, the thread that stored it)
8095+note subtracts sc there and writes stats, so no lane reads another lane's store. l_y is loaded
8096+note before any store, and the next instruction waits for that load.
8097+note Each lane's sum is compensated (Kahan: R40 carries the lost low part, R41 y, R42 t) and the
8098+note lane total is s - c. A plain sequential f32 sum over V 4096 in one thread biased log sum low by
8099+note about 1.04e-5 relative on trained checkpoints (gdiag receipt). Mutant k (OMUTK 0) keeps the
8100+note plain per-lane sum. Mutant x (OMUTX 0) skips the lane-1 butterfly step of the warp sum, which
8101+note gcmp2 and geval must catch. Kernels launch with 72 GPRs (gkgpr in grall); Kahan state in
8102+note R69-R71 hung this kernel (launch wait -110, evidence/OMEGALLM/geval/a1), so it uses R40-R42.
8103+note The warp sum overwrites R64 and its scratch R65 (grwred), so R43 keeps m and pass 3 recomputes
8104+note R65 = -m log2e from it (the same FMUL as pass 2, so the same value).
8105+const OMUTK ulong 1
8106+const OMUTX ulong 1
8107+fn grce to void
8108+v = uint get grd 0
8109+v4 = mul v 4
8110+it = div v 256
8111+sc = div 1.0f64 f64 mul get grd 1 get grd 5
8112+grrrow
8113+gkmovi 28 grf 1.4426950408889634f64
8114+gkmovi 29 grf neg 1.4426950408889634f64
8115+gkmovi 25 grf sc
8116+grptr 12 3 8 4
8117+gkldg 20 12 0 false
8118+grptr 14 1 8 v4
8119+gkimadw 14 20 4 14
8120+gkldg 68 14 0 false
8121+grrptr 10 1 v4
8122+gkmovi 64 4286578688
8123+top = grtop 30 it
8124+grldw 32 10 8 true
8125+for u uint 8
8126+grfmax 64 64 add 32 u
8127+end
8128+gradv 10 1024
8129+grbot 30 top
8130+grwred 1 0
8131+grfadd 43 64 255
8132+gkfmulf 65 64 29
8133+grrptr 10 1 v4
8134+gkmovz 66
8135+gkmovz 40
8136+top2 = grtop 30 it
8137+grldw 32 10 8 true
8138+for u uint 8
8139+gkffmaf add 32 u add 32 u 28 65
8140+grmufu add 32 u add 32 u 2048
8141+if eq OMUTK 0
8142+grfadd 66 66 add 32 u
8143+end
8144+if ne OMUTK 0
8145+grfsub 41 add 32 u 40
8146+grfadd 42 66 41
8147+grfsub 66 42 66
8148+grfsub 40 66 41
8149+grfadd 66 42 255
8150+end
8151+end
8152+gradv 10 1024
8153+grbot 30 top2
8154+grfsub 64 66 40
8155+if eq OMUTX 0
8156+grwred 0 1
8157+else
8158+grwred 0 0
8159+end
8160+grmufu 67 64 4096
8161+gkfmulf 67 67 25
8162+gkfmulf 65 43 29
8163+grrptr 10 1 v4
8164+top3 = grtop 30 it
8165+grldw 32 10 8 true
8166+for u uint 8
8167+gkffmaf add 32 u add 32 u 28 65
8168+grmufu add 32 u add 32 u 2048
8169+gkfmulf add 32 u add 32 u 67
8170+end
8171+grstw 10 32 8
8172+gradv 10 1024
8173+grbot 30 top3
8174+gkandi 21 20 31
8175+gkisetp 1 9 21 5
8176+gkexitp 1
8177+gkldg 33 14 0 false
8178+grfsub 33 33 25
8179+gkstg 14 33 0 false
8180+grptr 16 5 8 12
8181+gkstg 16 43 0 false
8182+gkstg 16 64 4 true
8183+gkstg 16 68 8 true
8184+gkexit
8185+end
8186+note 22 CSRG: gw[c][i] += sum over k in [start[c], start[c+1]) of dh[ord[k]][i]; grid (D/64, V)
8187+note args D dh start ord gw (start: V + 1 uints, ord: rows sorted by token, ascending within a token)
8188+fn grcsr to void
8189+d = uint get grd 2
8190+grpro
8191+grptr 10 3 3 4
8192+gkldg 20 10 0 false
8193+gkldg 21 10 4 true
8194+gkisetp 1 20 21 2
8195+gkexitp 1
8196+gkimad 30 20 31 21
8197+grptr 12 5 20 4
8198+grptr 16 1 4 4
8199+gkmovz 64
8200+top = div get gkat 0 16
8201+gkldg 22 12 0 false
8202+gkimadw 14 22 mul d 4 16
8203+gkldg 32 14 0 false
8204+grfadd 64 64 32
8205+gradv 12 4
8206+grbot 30 top
8207+gkimadi 23 3 d 4
8208+grptr 18 7 23 4
8209+gkldg 33 18 0 false
8210+grfadd 64 64 33
8211+gkstg 18 64 0 false
8212+gkexit
8213+end
8214+note 23 SQN: out[c] = sum over i in [512 c, 512 c + 512) of g[i]^2 (f32); args nchunk g out. The
8215+note gradient buffer is padded with zeros to a multiple of 512 (ggplan, ggbwd), so no tail guard.
8216+fn grsqn to void
8217+grpro
8218+grptr 10 1 4 2048
8219+gkmovz 64
8220+top = grtop 30 64
8221+grld8 32 10 true
8222+for u uint 8
8223+gkffmaf 64 add 32 u add 32 u 64
8224+end
8225+gradv 10 32
8226+grbot 30 top
8227+grptr 12 3 4 4
8228+gkstg 12 64 0 false
8229+gkexit
8230+end
8231+note OMUTH: 1 in the product; 0 in mutant h (beta1 0.8 on chip), which gacmp must catch.
8232+const OMUTH ulong 1
8233+note 24 ADAM (f32, on chip): g = g c; m = b1 m + (1 - b1) g; v = b2 v + (1 - b2) g^2;
8234+note w -= lrb m / (sqrt(v ibc2) + 1e-8), the f32 image of og.om oadam1/oadam2 with lrb = lr/bc1 and
8235+note ibc2 = 1/bc2 from the host. Args P w m v g c lrb ibc2 (slots 0, 2-9, 10, 11, 12).
8236+fn gradam to void
8237+grpro
8238+b1 = 0.9f64
8239+if eq OMUTH 0
8240+b1 = 0.8f64
8241+end
8242+gkmovi 24 grf b1
8243+gkmovi 25 grf sub 1.0f64 0.9
8244+gkmovi 27 grf 0.99f64
8245+gkmovi 28 grf sub 1.0f64 0.99
8246+gkmovi 29 grf 0.00000001f64
8247+grptr 10 1 4 4
8248+grptr 12 3 4 4
8249+grptr 14 5 4 4
8250+grptr 16 7 4 4
8251+gkldc 64 grarg 10 false
8252+gkldc 65 grarg 11 true
8253+gkldc 66 grarg 12 true
8254+gkldg 32 16 0 true
8255+gkldg 33 12 0 true
8256+gkldg 34 14 0 true
8257+gkldg 35 10 0 true
8258+gkfmulf 32 32 64
8259+gkfmulf 33 33 24
8260+gkffmaf 33 32 25 33
8261+gkfmulf 36 32 32
8262+gkfmulf 34 34 27
8263+gkffmaf 34 36 28 34
8264+gkfmulf 37 34 66
8265+grmufu 38 37 8192
8266+grfadd 38 38 29
8267+grmufu 39 38 4096
8268+gkfmulf 40 33 65
8269+gkfmulf 40 40 39
8270+grfsub 35 35 40
8271+gkstg 10 35 0 false
8272+gkstg 12 33 0 true
8273+gkstg 14 34 0 true
8274+gkexit
8275+end
8276+note emit slots 9..24 (12, 17 and 18 empty) for dims V T D H F B; false when a kernel overflows its 16 KiB slot
8277+fn grset v ulong t ulong d ulong h ulong f ulong b ulong to void
8278+put grd 0 v
8279+put grd 1 t
8280+put grd 2 d
8281+put grd 3 h
8282+put grd 4 f
8283+put grd 5 b
8284+end
8285+fn grall to bool
8286+note the warp-per-row RMS kernels hold D/32 columns per lane (D <= 384: four banks end at R119, grrb),
8287+note softmax T/32, and CE steps 256 columns
8288+if or ne rem get grd 2 32 0 ne rem get grd 1 32 0
8289+ret false
8290+end
8291+if gt get grd 2 384
8292+ret false
8293+end
8294+if ne rem get grd 0 256 0
8295+ret false
8296+end
8297+for k uint 16
8298+id = add k 9
8299+gkslot id
8300+if eq id 9
8301+gremb
8302+end
8303+if eq id 10
8304+grrmsf
8305+end
8306+if eq id 11
8307+grrmsb
8308+end
8309+if eq id 13
8310+grcol false
8311+end
8312+if eq id 14
8313+grcol true
8314+end
8315+if eq id 15
8316+grsoftf
8317+end
8318+if eq id 16
8319+grsoftb
8320+end
8321+if and ge id 19 le id 20
8322+grew id
8323+end
8324+if eq id 21
8325+grce
8326+end
8327+if eq id 22
8328+grcsr
8329+end
8330+if eq id 23
8331+grsqn
8332+end
8333+if eq id 24
8334+gradam
8335+end
8336+if gt get gkat 0 mul ulong add id 1 16384
8337+ret false
8338+end
8339+put gkthr id 64
8340+put gkgpr id 72
8341+if and gt get grd 2 256 or eq id 10 eq id 11
8342+put gkgpr id 128
8343+end
8344+put gksm id 0
8345+end
8346+ret true
8347+end
8348+note open the chip with the row slots 9..24 for the given dims, then gkopen's GEMM slots, arena asz bytes
8349+fn gropen asz ulong to bool
8350+if not grall
8351+put nverr 0 neg long 7
8352+ret false
8353+end
8354+ret gkopen asz
8355+end
8356+note SPDX-License-Identifier: AGPL-3.0-only
8357+note gb.om: ollm BF16 tensor-core path, commands and gates (docs/omegallm/CONTRACT-BF16.md). With gkbf 1,
8358+note gk.om runs every 64x64 gkmm on kernels 27..36 (HMMA.16816.F32.BF16; the kernel rounds both operands
8359+note to bf16 RN with F2FP and accumulates in f32). Commands: gtrainb and gprofb (gtrainf and gprof with
8360+note gkbf 1), gbcmp (sampled check of every BF16 GEMM of one step, exit 48), gbcmp2 (gcmp2's step parity
8361+note with the BF16 bound, exit 49) and gbfrag (exact check of all ten kernels on fixed data, exit 50).
8362+note Concatenated before gg.om: it calls gg.om functions but uses none of gg.om's globals.
8363+
8364+note OMUTU: 1 in the product; 0 in mutant u, whose reference skips the bf16 rounding (gbfrag must then
8365+note fail; gbcmp only reports it, CONTRACT-BF16 section 5).
8366+const OMUTU ulong 1
8367+
8368+note OMUTN: 1 in the product; 0 in mutant n, the negative control of gbfrag's padding check: after each
8369+note short case's launch the host writes 0.0 into C's first padding word (block 0, row 0, column n), so
8370+note only that check can fail (CONTRACT-BF16 section 4.1).
8371+const OMUTN ulong 1
8372+
8373+note bf16 round to nearest even of the f32 bits u, as f32 bits (NaN unchanged)
8374+fn gbbf u uint to uint
8375+if eq OMUTU 0
8376+ret u
8377+end
8378+if and eq and shr u 23 255 255 ne and u 8388607 0
8379+ret u
8380+end
8381+v = add add u 32767 and shr u 16 1
8382+ret and v 4294901760
8383+end
8384+
8385+note f32 bits x rounded to bf16, as f64
8386+fn gbrv x uint to f64
8387+ret f64 bitcast f32 gbbf x
8388+end
8389+
8390+fn gbabs x f64 to f64
8391+if lt x 0.0f64
8392+ret neg x
8393+end
8394+ret x
8395+end
8396+
8397+note arena word at host byte offset o (offset from gkbase)
8398+fn gbw o ulong to uint
8399+ret uint mapget get gbmem 5 o 4
8400+end
8401+
8402+fn gbput o ulong x uint to void
8403+m = mapset get gbmem 5 o 4 ulong x
8404+end
8405+
8406+note host byte offset of the device address held in arg words w, w + 1
8407+fn gbhp w uint to ulong
8408+lo = ulong get gkarg w
8409+hi = ulong get gkarg add w 1
8410+ret sub or lo shl hi 32 gkbase
8411+end
8412+
8413+note batch offset z1 s1 + z2 s2 (z = z1 h2 + z2) with s1, s2 in arg words w, w + 1
8414+fn gbzo z uint h2 uint w uint to ulong
8415+a = mul ulong div z h2 ulong get gkarg w
8416+ret add a mul ulong rem z h2 ulong get gkarg add w 1
8417+end
8418+
8419+note verdict line GBCMP (c 48), GBCMP2 (49) or GBFRAG (50): PASS, or FAIL and exit c
8420+fn gbvd c ulong ok bool to void
8421+oputc 71
8422+oputc 66
8423+if eq c 50
8424+oputc 70
8425+oputc 82
8426+oputc 65
8427+oputc 71
8428+else
8429+oputc 67
8430+oputc 77
8431+oputc 80
8432+if eq c 49
8433+oputc 50
8434+end
8435+end
8436+oputc 32
8437+if ok
8438+oputc 80
8439+oputc 65
8440+oputc 83
8441+oputc 83
8442+oputc 10
8443+oflush
8444+ret
8445+end
8446+oputc 70
8447+oputc 65
8448+oputc 73
8449+oputc 76
8450+oputc 10
8451+oflush
8452+if eq c 48
8453+exit 48
8454+end
8455+if eq c 49
8456+exit 49
8457+end
8458+exit 50
8459+end
8460+
8461+note gbcmp state. gbst: 0 BF16 GEMMs checked, 1 samples past their bound, 2 kernel id mask, 3 cz mask,
8462+note 4 ep mask, 5 beta mask, 6 the sample generator. gbmx[0]: the worst |chip - ref| / bound. gbsp: the
8463+note 66 sampled (z, r, c) of the launch in flight, gbold the old C there (beta), gbacc the reference sums.
8464+global gbst ulong 8
8465+global gbmx f64 1
8466+global gbsp uint 198
8467+global gbold f64 66
8468+global gbacc f64 3
8469+
8470+note next value in [0, n) of the sample generator (31-bit LCG)
8471+fn gbrnd n uint to uint
8472+s = rem add mul get gbst 6 1103515245 12345 2147483648
8473+put gbst 6 s
8474+ret uint rem shr s 8 ulong n
8475+end
8476+
8477+note host byte offset at sample i in C (w 16) or in the epilogue's Y / M (w 30, C's strides)
8478+fn gbco i ulong w uint to ulong
8479+o = mul i 3
8480+z = get gbsp o
8481+b = add gbhp w gbzo z get gkbp 4 20
8482+b = add b mul ulong get gbsp add o 1 ulong get gkarg 18
8483+ret add b mul ulong get gbsp add o 2 ulong get gkarg 19
8484+end
8485+
8486+note gkmm's hook before a checked BF16 launch (gkbp = m n k nz h2 beta ep cz): 64 random (z, r, c)
8487+note and the corners (0, 0, 0), (nz - 1, m - 1, n - 1); with cz 3 a position in a block above the
8488+note diagonal (not computed) is mirrored below it. Keeps the old C when beta.
8489+fn gbpre to void
8490+for i ulong 66
8491+gbpre1 i
8492+end
8493+end
8494+
8495+note one sample i of gbpre
8496+fn gbpre1 i ulong to void
8497+z = gbrnd get gkbp 3
8498+r = gbrnd get gkbp 0
8499+c = gbrnd get gkbp 1
8500+if eq i 64
8501+z = 0
8502+r = 0
8503+c = 0
8504+end
8505+if eq i 65
8506+z = sub get gkbp 3 1
8507+r = sub get gkbp 0 1
8508+c = sub get gkbp 1 1
8509+end
8510+if eq get gkbp 7 3
8511+if gt div c 64 div r 64
8512+t = r
8513+r = c
8514+c = t
8515+end
8516+end
8517+o = mul i 3
8518+put gbsp o z
8519+put gbsp add o 1 r
8520+put gbsp add o 2 c
8521+put gbold i 0.0f64
8522+if eq get gkbp 5 1
8523+put gbold i f64 bitcast f32 gbw gbco i 16
8524+end
8525+end
8526+
8527+note reference sums of sample i over the K range the kernel reads (cz 1: k < (r / 64 + 1) 64,
8528+note cz 2: k >= (r / 64) 64): gbacc 0 sum a b, 1 sum |a b|, 2 the term count; a, b rounded to bf16
8529+fn gbref i ulong to void
8530+r = ulong get gbsp add mul i 3 1
8531+k = ulong get gkbp 2
8532+lo = ulong 0
8533+hi = mul add div r 64 1 64
8534+cz = get gkbp 7
8535+if eq cz 1
8536+if lt hi k
8537+k = hi
8538+end
8539+end
8540+if eq cz 2
8541+lo = mul div r 64 64
8542+end
8543+gbdot gbsa i 0 gbsa i 1 lo sub k lo
8544+end
8545+
8546+note host address of sample i's A row (w 0) or B column (w 1) at k = 0
8547+fn gbsa i ulong w uint to ulong
8548+o = mul i 3
8549+z = get gbsp o
8550+h2 = get gkbp 4
8551+g = mul w 8
8552+x = ulong get gbsp add o ulong add w 1
8553+p = add gbhp g gbzo z h2 add g 4
8554+ret add p mul x ulong get gkarg add g 2
8555+end
8556+
8557+fn gbdot pa ulong pb ulong lo ulong kn ulong to void
8558+ka = ulong get gkarg 3
8559+kb = ulong get gkarg 11
8560+s = 0.0f64
8561+sa = 0.0f64
8562+for j ulong kn
8563+kk = add lo j
8564+p = mul gbrv gbw add pa mul kk ka gbrv gbw add pb mul kk kb
8565+s = add s p
8566+sa = add sa gbabs p
8567+end
8568+put gbacc 0 s
8569+put gbacc 1 sa
8570+put gbacc 2 f64 kn
8571+end
8572+
8573+note d / b, 0 when both are 0; 1e9 when d > b with b = 0 or the ratio non-finite or past 1e9
8574+fn gbrat d f64 b f64 to f64
8575+if le d b
8576+if gt b 0.0f64
8577+ret div d b
8578+end
8579+ret 0.0f64
8580+end
8581+if gt b 0.0f64
8582+q = div d b
8583+if lt q 1000000000.0f64
8584+ret q
8585+end
8586+end
8587+ret 1000000000.0f64
8588+end
8589+
8590+note |chip - ref| / bound at sample i, bound (kn 2^-23 + 2^-22)(|alpha| sum |a b| + |old C|); 1e9
8591+note for a wrong epilogue (ep 2: C must be +0 where M is not > 0; ep 1: Y = C when C > 0, else +0)
8592+fn gbchk i ulong to f64
8593+co = gbco i 16
8594+cb = gbw co
8595+ep = get gkbp 6
8596+if eq ep 2
8597+if not gt f64 bitcast f32 gbw gbco i 30 0.0f64
8598+if eq cb 0
8599+ret 0.0f64
8600+end
8601+ret 1000000000.0f64
8602+end
8603+end
8604+if eq ep 1
8605+w = uint 0
8606+if gt f64 bitcast f32 cb 0.0f64
8607+w = cb
8608+end
8609+if ne gbw gbco i 30 w
8610+ret 1000000000.0f64
8611+end
8612+end
8613+gbref i
8614+al = f64 bitcast f32 get gkarg 25
8615+old = get gbold i
8616+d = gbabs sub f64 bitcast f32 cb add mul al get gbacc 0 old
8617+e = add div get gbacc 2 8388608.0f64 div 1.0f64 4194304.0f64
8618+ret gbrat d mul e add mul gbabs al get gbacc 1 gbabs old
8619+end
8620+
8621+note kernel id of the launch in flight (gk.om gkmmb)
8622+fn gbid to ulong
8623+ep = ulong get gkbp 6
8624+if ne ep 0
8625+ret add 34 ep
8626+end
8627+id = add 27 ulong get gkmd 0
8628+id = add id mul 2 ulong get gkmd 1
8629+ret add id mul 4 ulong get gkbp 5
8630+end
8631+
8632+note gkmm's hook after a checked BF16 launch: check the samples, print
8633+note `GB id m n k nz h2 beta ep cz worst_e6 bad`; true (the step goes on, gbcmp gives the verdict)
8634+fn gbpost to bool
8635+bad = ulong 0
8636+mr = 0.0f64
8637+for i ulong 66
8638+q = gbchk i
8639+if gt q 1.0f64
8640+bad = add bad 1
8641+end
8642+if gt q mr
8643+mr = q
8644+end
8645+end
8646+gbnote bad mr
8647+ret true
8648+end
8649+
8650+fn gbnote bad ulong mr f64 to void
8651+id = gbid
8652+put gbst 0 add get gbst 0 1
8653+put gbst 1 add get gbst 1 bad
8654+put gbst 2 or get gbst 2 shl ulong 1 id
8655+put gbst 3 or get gbst 3 shl ulong 1 ulong get gkbp 7
8656+put gbst 4 or get gbst 4 shl ulong 1 ulong get gkbp 6
8657+put gbst 5 or get gbst 5 shl ulong 1 ulong get gkbp 5
8658+if gt mr get gbmx 0
8659+put gbmx 0 mr
8660+end
8661+oputc 71
8662+oputc 66
8663+oputc 32
8664+oputu id
8665+for j ulong 8
8666+oputc 32
8667+oputu ulong get gkbp j
8668+end
8669+oputc 32
8670+oputl rmic mr
8671+oputc 32
8672+oputu bad
8673+oputc 10
8674+end
8675+
8676+note gbcmp CK DATA S: one gtrainf step's gradient pass (gggrad, no Adam) with gkbf 1 and gkbck 1, so
8677+note gk.om checks every BF16 launch (gbpre, gbpost). Prints the GB lines, `GBC gemms bad worst_e6
8678+note idmask czmask epmask betamask` and GBCMP PASS, or GBCMP FAIL (exit 48): a sample past its bound,
8679+note no BF16 GEMM, or coverage short of the step's BF16 combinations (epilogues 1 and 2, cz 3, beta
8680+note false and true).
8681+fn gbcmp to void
8682+if ne oargc 5
8683+exit 2
8684+end
8685+ockload 2
8686+odopen 3
8687+obatch oargu 4
8688+put og 10 0
8689+put gbst 6 12345
8690+put gkbf 0 1
8691+put gkbck 0 1
8692+ggopen
8693+l = gggrad
8694+put gkbck 0 0
8695+r = nvstop
8696+oputc 71
8697+oputc 66
8698+oputc 67
8699+for j ulong 2
8700+oputc 32
8701+oputu get gbst j
8702+end
8703+oputc 32
8704+oputl rmic get gbmx 0
8705+for j ulong 4
8706+oputc 32
8707+oputu get gbst add j 2
8708+end
8709+oputc 10
8710+ok = and eq get gbst 1 0 gt get gbst 0 0
8711+cv = eq and get gbst 4 6 6
8712+cv = and cv ne and get gbst 3 8 0
8713+cv = and cv eq get gbst 5 3
8714+gbvd 48 and ok cv
8715+end
8716+
8717+note gbcmp2 bound: tolb = n_c (2u + u^2), u = 2^-8, n_c = 10 L + 3 BF16 GEMMs on a step's longest
8718+note dependency chain (per layer forward QKV, S, WO, W1, W2; backward dF, dA of W1, dN, DP, dA of QKV;
8719+note plus the logits, dlogits to dH and the last weight gradient); a first-order normwise heuristic
8720+fn gbtolb to f64
8721+nc = f64 add mul 10 get og 5 3
8722+ret mul nc add div 1.0f64 128.0f64 div 1.0f64 65536.0f64
8723+end
8724+
8725+note gcmp2's fail bits under gkbf 1: 1 non-finite, 2 rho >= tolb, 4 rel1 >= tolb, 8 |dloss| >= tolb loss
8726+fn gbc2fl lg f64 l0 f64 rho f64 nf ulong to ulong
8727+tb = gbtolb
8728+fl = ulong 0
8729+if ne nf 0
8730+fl = or fl 1
8731+end
8732+if not lt ggclp rho tb
8733+fl = or fl 2
8734+end
8735+if not lt gg2rl 2 tb
8736+fl = or fl 4
8737+end
8738+if not lt gbabs sub lg l0 mul tb gbabs l0
8739+fl = or fl 8
8740+end
8741+ret fl
8742+end
8743+
8744+note cosine of the chip gradient (omv[0, p)) and K0 (omv[p, 2p))
8745+global gbcq f64 3
8746+fn gbcos p ulong to f64
8747+for k ulong 3
8748+put gbcq k 0.0f64
8749+end
8750+for q ulong ocn p
8751+i0 = mul q OCH
8752+for j ulong sub oce q p i0
8753+gbcq1 add i0 j p
8754+end
8755+end
8756+d = fsqrt mul get gbcq 1 get gbcq 2
8757+if gt d 0.0f64
8758+ret ggclp div get gbcq 0 d
8759+end
8760+ret 0.0f64
8761+end
8762+
8763+fn gbcq1 i ulong p ulong to void
8764+x = omvg i
8765+y = omvg add p i
8766+put gbcq 0 add get gbcq 0 mul x y
8767+put gbcq 1 add get gbcq 1 mul x x
8768+put gbcq 2 add get gbcq 2 mul y y
8769+end
8770+
8771+note gbcmp2's line `GB2 L n_c tolb_e9 cos_e9 rel0_e9 rel1_e9 dlossrel_e9` and verdict (exit 49)
8772+fn gbc2vd lg f64 l0 f64 fl ulong to void
8773+cs = gbcos get og 8
8774+oputc 71
8775+oputc 66
8776+oputc 50
8777+oputc 32
8778+oputu get og 5
8779+oputc 32
8780+oputu add mul 10 get og 5 3
8781+oputc 32
8782+oputl rmic mul gbtolb 1000.0f64
8783+oputc 32
8784+oputl rmic mul cs 1000.0f64
8785+oputc 32
8786+oputl rmic mul gg2rl 0 1000.0f64
8787+oputc 32
8788+oputl rmic mul gg2rl 2 1000.0f64
8789+oputc 32
8790+oputl rmic mul ggclp div gbabs sub lg l0 gbabs l0 1000.0f64
8791+oputc 10
8792+gbvd 49 eq fl 0
8793+end
8794+
8795+note gbcmp2 CK DATA S: gcmp2 with gkbf 1 (gg.om ggc2fl and ggc2pr branch to gbc2fl and gbc2vd)
8796+fn gbcmp2 to void
8797+put gkbf 0 1
8798+ggcmp2
8799+end
8800+
8801+note gtrainb: gtrainf with gkbf 1 (geval stays f32)
8802+fn gbtrain to void
8803+put gkbf 0 1
8804+ggtrainf
8805+end
8806+
8807+note gprofb: gprof with gkbf 1
8808+fn gbprof to void
8809+put gkbf 0 1
8810+ggprof
8811+end
8812+
8813+note gbfrag CK: every BF16 kernel (ids 27..36) on fixed data with exact f32 sums, compared element by
8814+note element against a reference built from the bf16-rounded operands. One operand holds integers in
8815+note [-5, 5], the other values (n - 2) + j 2^-9 (n < 5, j < 64), so every product is a multiple of 2^-9
8816+note (2^-10 with alpha 1/2) and every sum stays below 2^11: the chip's f32 result is exact. Thirteen
8817+note cases (eight plain kernels, epilogues 1 and 2, cz 1, 2 and 3), each with the operand roles
8818+note swapped; nz 6, h2 3, padded strides, NaN in every operand word the kernel must not read and in the
8819+note eight padding columns of every C row (and of Y / M with a fused epilogue), which must still be NaN
8820+note afterwards. Data in the logits buffer (A, B, C, Y at 0, 1, 2, 3 MiB). Prints `GF pass case id k cz ep bad` per case,
8821+note then the eight long-K cases (gbfl, `GFL pass ma mb bad`), `GFS bad` and GBFRAG PASS, or GBFRAG FAIL
8822+note (exit 50). Needs a logits buffer of at least 4 Mi floats (B 8: 8 Mi).
8823+note gbcs: 0 ma, 1 mb (1 = K-contiguous), 2 beta, 3 ep, 4 cz, 5 k, 6 m, 7 n, 8 pass, 9 alpha 1/2
8824+global gbcs uint 10
8825+global gbfb ulong 1
8826+global gbha f64 98304
8827+global gbhb f64 147456
8828+
8829+fn gbfrag to void
8830+if ne oargc 3
8831+exit 2
8832+end
8833+ockload 2
8834+ggopen
8835+if lt mul ggr get og 0 4194304
8836+ggdie 3
8837+end
8838+put gkbf 0 1
8839+put gkbck 0 0
8840+put gbfb 0 sub gga1 11 gkbase
8841+bad = ulong 0
8842+for p ulong 2
8843+for i ulong 13
8844+bad = add bad gbf1 i p
8845+end
8846+end
8847+for p ulong 2
8848+for q uint 4
8849+bad = add bad gbfl p and q 1 shr q 1
8850+end
8851+end
8852+r = nvstop
8853+oputc 71
8854+oputc 70
8855+oputc 83
8856+oputc 32
8857+oputu bad
8858+oputc 10
8859+gbvd 50 eq bad 0
8860+end
8861+
8862+note case i of pass p into gbcs
8863+fn gbcase i ulong p ulong to void
8864+for j ulong 10
8865+put gbcs j 0
8866+end
8867+put gbcs 8 uint p
8868+put gbcs 6 128
8869+put gbcs 7 192
8870+put gbcs 5 128
8871+if eq rem i 3 0
8872+put gbcs 5 32
8873+end
8874+if eq rem i 3 1
8875+put gbcs 5 96
8876+put gbcs 9 1
8877+end
8878+if lt i 8
8879+put gbcs 0 uint and i 1
8880+put gbcs 1 uint and shr i 1 1
8881+put gbcs 2 uint and shr i 2 1
8882+ret
8883+end
8884+put gbcs 9 0
8885+put gbcs 0 1
8886+if eq i 8
8887+put gbcs 3 1
8888+put gbcs 5 96
8889+end
8890+if eq i 9
8891+put gbcs 1 1
8892+put gbcs 3 2
8893+end
8894+note cz 1 needs k = m
8895+if eq i 10
8896+put gbcs 4 1
8897+put gbcs 5 128
8898+end
8899+if eq i 11
8900+put gbcs 0 0
8901+put gbcs 2 1
8902+put gbcs 4 2
8903+end
8904+if eq i 12
8905+put gbcs 1 1
8906+put gbcs 4 3
8907+put gbcs 5 32
8908+put gbcs 7 128
8909+put gbcs 9 1
8910+end
8911+end
8912+
8913+note one case: write the data, launch, compare; the mismatch count
8914+fn gbf1 i ulong p ulong to ulong
8915+gbcase i p
8916+gbnan
8917+gbwop 0
8918+gbwop 1
8919+gbwc
8920+put gkinv 0 1
8921+if not gbflch
8922+ggdie 98
8923+end
8924+if eq OMUTN 0
8925+n = ulong get gbcs 7
8926+gbput add gbcro 0 0 mul ulong get gbcs 6 add n 8 mul n 4 0
8927+end
8928+b = gbfcmp
8929+ep = ulong get gbcs 3
8930+id = add 27 ulong get gbcs 0
8931+id = add id mul 2 ulong get gbcs 1
8932+id = add id mul 4 ulong get gbcs 2
8933+if ne ep 0
8934+id = add 34 ep
8935+end
8936+oputc 71
8937+oputc 70
8938+oputc 32
8939+oputu p
8940+oputc 32
8941+oputu i
8942+oputc 32
8943+oputu id
8944+oputc 32
8945+oputu ulong get gbcs 5
8946+oputc 32
8947+oputu ulong get gbcs 4
8948+oputc 32
8949+oputu ep
8950+oputc 32
8951+oputu b
8952+oputc 10
8953+ret b
8954+end
8955+
8956+note NaN in every A and B word (2 MiB)
8957+fn gbnan to void
8958+b = get gbfb 0
8959+for i ulong 524288
8960+gbput add b mul i 4 2143289344
8961+end
8962+end
8963+
8964+note operand layout in floats: mode 1 rows of k + 8, mode 0 rows of w + 16 (w = m for A, n for B);
8965+note batch z at (z / 3)(3 zs + 64) + (z % 3) zs
8966+fn gbzs md uint w ulong kd ulong to ulong
8967+if eq md 1
8968+ret mul w add kd 8
8969+end
8970+ret mul kd add w 16
8971+end
8972+
8973+fn gbzof z ulong zs ulong to ulong
8974+ret add mul div z 3 add mul zs 3 64 mul rem z 3 zs
8975+end
8976+
8977+fn gbopf md uint x ulong kk ulong w ulong kd ulong to ulong
8978+if eq md 1
8979+ret add mul x add kd 8 kk
8980+end
8981+ret add x mul kk add w 16
8982+end
8983+
8984+note the fractional value (n - 2) + j 2^-9 and the integer value at (x, kk, z)
8985+fn gbfr x ulong kk ulong z ulong to f64
8986+s = add mul x 7 mul kk 3
8987+s = add s mul z 5
8988+a = sub f64 rem s 5 2.0f64
8989+s = add mul x 13 mul kk 11
8990+s = add s mul z 3
8991+b = f64 rem s 64
8992+ret add a div b 512.0f64
8993+end
8994+
8995+fn gbint x ulong kk ulong z ulong to f64
8996+s = add mul x 5 mul kk 7
8997+s = add s mul z 3
8998+ret sub f64 rem s 11 5.0f64
8999+end
9000+
9001+note write operand ab (0 A, 1 B) for z < 6, x < m (A) or n (B), kk < k; keep the bf16-rounded
9002+note values in gbha / gbhb (index (z rows + x) 128 + kk). Pass 0: A fractional, B integer; pass 1 swapped.
9003+fn gbwop ab uint to void
9004+md = get gbcs ab
9005+k = ulong get gbcs 5
9006+w = ulong get gbcs add 6 ab
9007+zs = gbzs md w k
9008+base = add get gbfb 0 mul ulong ab 1048576
9009+fr = eq get gbcs 8 ab
9010+for z ulong 6
9011+for x ulong w
9012+gbwrow ab z x base zs fr
9013+end
9014+end
9015+end
9016+
9017+fn gbwrow ab uint z ulong x ulong base ulong zs ulong fr bool to void
9018+zb = add base mul gbzof z zs 4
9019+t = add 128 mul 64 ulong ab
9020+hi = mul add mul z t x 128
9021+k = ulong get gbcs 5
9022+for kk ulong k
9023+gbwel ab zb x kk gbwv x kk z fr hi
9024+end
9025+end
9026+
9027+note operand value at (x, kk, z) as f32 bits: fractional when fr, else integer
9028+fn gbwv x ulong kk ulong z ulong fr bool to uint
9029+v = gbint x kk z
9030+if fr
9031+v = gbfr x kk z
9032+end
9033+ret bitcast uint f32 v
9034+end
9035+
9036+note store bits u of operand ab at (x, kk) of the z block at zb; keep its bf16 value at gbha / gbhb hi + kk
9037+fn gbwel ab uint zb ulong x ulong kk ulong u uint hi ulong to void
9038+md = get gbcs ab
9039+w = ulong get gbcs add 6 ab
9040+o = gbopf md x kk w ulong get gbcs 5
9041+gbput add zb mul o 4 u
9042+if eq ab 0
9043+put gbha add hi kk gbrv u
9044+else
9045+put gbhb add hi kk gbrv u
9046+end
9047+end
9048+
9049+note old C ((3 r + 5 c + z) mod 17) - 8, and the epilogue-2 mask M: NaN when (5 r + c) mod 13 = 0,
9050+note else ((r + 3 c + z) mod 3) - 1, as f32 bits
9051+fn gbold3 z ulong r ulong c ulong to f64
9052+s = add mul r 3 mul c 5
9053+s = add s z
9054+ret sub f64 rem s 17 8.0f64
9055+end
9056+
9057+fn gbmv z ulong r ulong c ulong to uint
9058+if eq rem add mul r 5 c 13 0
9059+ret 2143289344
9060+end
9061+ret bitcast uint f32 sub f64 rem add add r mul c 3 z 3 1.0f64
9062+end
9063+
9064+note C (old values) and Y: 77 for epilogue 1, M for epilogue 2; C rows of n + 8, whose 8 padding columns
9065+note (and Y's / M's with a fused epilogue) are NaN
9066+fn gbwc to void
9067+m = ulong get gbcs 6
9068+zs = mul m add ulong get gbcs 7 8
9069+for z ulong 6
9070+for r ulong m
9071+gbwcr z r zs
9072+end
9073+end
9074+end
9075+
9076+fn gbwcr z ulong r ulong zs ulong to void
9077+n = ulong get gbcs 7
9078+ep = get gbcs 3
9079+o = gbcro z r zs
9080+for c ulong n
9081+oc = add o mul c 4
9082+gbput oc bitcast uint f32 gbold3 z r c
9083+if eq ep 1
9084+gbput add oc 1048576 bitcast uint f32 77.0f64
9085+end
9086+if eq ep 2
9087+gbput add oc 1048576 gbmv z r c
9088+end
9089+end
9090+for c ulong 8
9091+op = add o mul add n c 4
9092+gbput op 2143289344
9093+if ne ep 0
9094+gbput add op 1048576 2143289344
9095+end
9096+end
9097+end
9098+
9099+note operand ab's gkopa for the case at device address p
9100+fn gbfopa ab uint p ulong to void
9101+md = get gbcs ab
9102+k = get gbcs 5
9103+w = get gbcs add 6 ab
9104+zs = uint gbzs md ulong w ulong k
9105+so = mul add k 8 4
9106+sk = uint 4
9107+if eq md 0
9108+so = 4
9109+sk = mul add w 16 4
9110+end
9111+gkopa ab p so sk mul add mul zs 3 64 4 mul zs 4
9112+end
9113+
9114+fn gbflch to bool
9115+d = add gkbase get gbfb 0
9116+gbfopa 0 d
9117+gbfopa 1 add d 1048576
9118+n = get gbcs 7
9119+zc = mul get gbcs 6 add n 8
9120+al = 1.0f64
9121+if eq get gbcs 9 1
9122+al = 0.5f64
9123+end
9124+sm = mul add n 8 4
9125+s1 = mul add mul zc 3 64 4
9126+s2 = mul zc 4
9127+gkopc add d 2097152 sm 4 s1 s2 f32 al
9128+if ne get gbcs 3 0
9129+gkope get gbcs 3 add d 3145728
9130+end
9131+if ne get gbcs 4 0
9132+gkopz get gbcs 4
9133+end
9134+m = get gbcs 6
9135+k = get gbcs 5
9136+bt = eq get gbcs 2 1
9137+ret gkmm m n k 6 3 bt
9138+end
9139+
9140+note mismatches over every C element of the case (and Y for epilogue 1), plus padding words no longer NaN
9141+fn gbfcmp to ulong
9142+bad = ulong 0
9143+for z ulong 6
9144+for r ulong ulong get gbcs 6
9145+bad = add bad gbfrow z r
9146+end
9147+end
9148+ret bad
9149+end
9150+
9151+note host byte offset of C row r of block z (row stride n + 8 f32)
9152+fn gbcro z ulong r ulong zs ulong to ulong
9153+n = ulong get gbcs 7
9154+b = add get gbfb 0 2097152
9155+b = add b mul gbzof z zs 4
9156+ret add b mul mul r add n 8 4
9157+end
9158+
9159+fn gbfrow z ulong r ulong to ulong
9160+n = ulong get gbcs 7
9161+zs = mul ulong get gbcs 6 add n 8
9162+o = gbcro z r zs
9163+bad = ulong 0
9164+for c ulong n
9165+if not gbfel z r c add o mul c 4
9166+bad = add bad 1
9167+end
9168+end
9169+for c ulong 8
9170+op = add o mul add n c 4
9171+if ne gbw op 2143289344
9172+bad = add bad 1
9173+end
9174+if ne get gbcs 3 0
9175+if ne gbw add op 1048576 2143289344
9176+bad = add bad 1
9177+end
9178+end
9179+end
9180+ret bad
9181+end
9182+
9183+note element (z, r, c) at host offset oc: cz 3 blocks above the diagonal keep the old C; epilogue 2
9184+note gives +0 where M is not > 0; else C equals the exact reference; epilogue 1 Y = C when C > 0, else +0
9185+fn gbfel z ulong r ulong c ulong oc ulong to bool
9186+cb = gbw oc
9187+old = gbold3 z r c
9188+if and eq get gbcs 4 3 gt div c 64 div r 64
9189+ret eq cb bitcast uint f32 old
9190+end
9191+ep = get gbcs 3
9192+if eq ep 2
9193+if not gt f64 bitcast f32 gbmv z r c 0.0f64
9194+ret eq cb 0
9195+end
9196+end
9197+s = gbfref z r c
9198+if eq get gbcs 2 1
9199+s = add s old
9200+end
9201+if ne f64 bitcast f32 cb s
9202+ret false
9203+end
9204+if eq ep 1
9205+w = uint 0
9206+if gt s 0.0f64
9207+w = cb
9208+end
9209+ret eq gbw add oc 1048576 w
9210+end
9211+ret true
9212+end
9213+
9214+note exact reference alpha sum a b over the K range the kernel reads (bf16-rounded operands)
9215+fn gbfref z ulong r ulong c ulong to f64
9216+k = ulong get gbcs 5
9217+lo = ulong 0
9218+hi = mul add div r 64 1 64
9219+cz = get gbcs 4
9220+if and eq cz 1 lt hi k
9221+k = hi
9222+end
9223+if eq cz 2
9224+lo = mul div r 64 64
9225+end
9226+ia = mul add mul z 128 r 128
9227+s = gbfdot ia mul add mul z 192 c 128 lo sub k lo
9228+if eq get gbcs 9 1
9229+s = mul s 0.5f64
9230+end
9231+ret s
9232+end
9233+
9234+fn gbfdot ia ulong ib ulong lo ulong kn ulong to f64
9235+s = 0.0f64
9236+for j ulong kn
9237+kk = add lo j
9238+a = get gbha add ia kk
9239+s = add s mul a get gbhb add ib kk
9240+end
9241+ret s
9242+end
9243+
9244+note long-K case of pass p, operand modes ma, mb: m = n = 64, k = 16384 (512 K tiles), nz 1, beta 0,
9245+note alpha 1. Operands are zero except kk = 0 mod 29 (565 terms, at least one in every 32-wide K tile),
9246+note so a dropped, repeated or misplaced K tile changes C; sums stay below 565 5 17/8 < 2^13 in multiples
9247+note of 2^-9, so the f32 result is exact. Logits buffer: A at 0, B at 6 MiB, C (rows of 72) at 12 MiB,
9248+note NaN everywhere first; C's padding columns must stay NaN. Prints `GFL pass ma mb bad`.
9249+fn gbfl p ulong ma uint mb uint to ulong
9250+for j ulong 10
9251+put gbcs j 0
9252+end
9253+put gbcs 0 ma
9254+put gbcs 1 mb
9255+put gbcs 5 16384
9256+put gbcs 6 64
9257+put gbcs 7 64
9258+put gbcs 8 uint p
9259+b = get gbfb 0
9260+for i ulong 3162112
9261+gbput add b mul i 4 2143289344
9262+end
9263+gblw 0
9264+gblw 1
9265+put gkinv 0 1
9266+if not gbllch
9267+ggdie 98
9268+end
9269+bad = ulong 0
9270+for r ulong 64
9271+bad = add bad gblrow r
9272+end
9273+oputc 71
9274+oputc 70
9275+oputc 76
9276+oputc 32
9277+oputu p
9278+oputc 32
9279+oputu ulong ma
9280+oputc 32
9281+oputu ulong mb
9282+oputc 32
9283+oputu bad
9284+oputc 10
9285+ret bad
9286+end
9287+
9288+note operand ab of the long case: nonzero words only at kk = 0 mod 29
9289+fn gblw ab uint to void
9290+md = get gbcs ab
9291+base = add get gbfb 0 mul ulong ab 6291456
9292+fr = eq get gbcs 8 ab
9293+for x ulong 64
9294+gblwr md base x fr
9295+end
9296+end
9297+
9298+fn gblwr md uint base ulong x ulong fr bool to void
9299+w = ulong 64
9300+kd = ulong 16384
9301+for kk ulong kd
9302+u = uint 0
9303+if eq rem kk 29 0
9304+u = gbwv x kk 0 fr
9305+end
9306+o = gbopf md x kk w kd
9307+gbput add base mul o 4 u
9308+end
9309+end
9310+
9311+note launch the long case: mode 1 rows of k + 8 floats, mode 0 K rows of 80 floats
9312+fn gbllch to bool
9313+d = add gkbase get gbfb 0
9314+gblopa 0 d
9315+gblopa 1 add d 6291456
9316+gkopc add d 12582912 288 4 0 0 f32 1.0f64
9317+gkopz 0
9318+ret gkmm 64 64 16384 1 1 false
9319+end
9320+
9321+fn gblopa ab uint p ulong to void
9322+so = uint 65568
9323+sk = uint 4
9324+if eq get gbcs ab 0
9325+so = 4
9326+sk = 320
9327+end
9328+gkopa ab p so sk 0 0
9329+end
9330+
9331+note row r of the long case: 64 exact values, then 8 padding words that must still be NaN
9332+fn gblrow r ulong to ulong
9333+o = add add get gbfb 0 12582912 mul r 288
9334+bad = ulong 0
9335+for c ulong 64
9336+cb = gbw add o mul c 4
9337+if ne f64 bitcast f32 cb gblref r c
9338+bad = add bad 1
9339+end
9340+end
9341+for c ulong 8
9342+if ne gbw add o mul add 64 c 4 2143289344
9343+bad = add bad 1
9344+end
9345+end
9346+ret bad
9347+end
9348+
9349+note exact reference: sum over kk = 29 j of the bf16-rounded A(r, kk) B(c, kk)
9350+fn gblref r ulong c ulong to f64
9351+fa = eq get gbcs 8 0
9352+fb = eq get gbcs 8 1
9353+s = 0.0f64
9354+for j ulong 565
9355+kk = mul j 29
9356+a = gbrv gbwv r kk 0 fa
9357+s = add s mul a gbrv gbwv c kk 0 fb
9358+end
9359+ret s
9360+end
9361+note SPDX-License-Identifier: AGPL-3.0-only
9362+note gg.om: ollm GPU gradient (aien-train#52, rung 4). The step loss and its gradient for the batch in
9363+note obx/oby, computed on the GB10 by gk.om GEMMs and gr.om row kernels over all R = B T rows at
9364+note once in f32; the gradient lands in ogr[0..P) as f64 and the loss in olos, so oadam and olsum run
9365+note unchanged on the CPU. Arena (gbmem 5) byte offsets in gga: 0 W, 1 G, 2 X, 3 Y, 4 CSR start
9366+note (V + 1), 5 CSR order (R), 6 first layer, 7 layer stride, 8 HF, 9 SF, 10 Z, 11 logits (R V),
9367+note 12 CE stats (R 3), 13 DH, 14 DA, 15 DQ DK DU (contiguous), 16 DO, 17 DF, 18 DP, 19 WS, 20 CS;
9368+note layer fields 24 + f: 0 hin 1 a 2 q 3 k 4 u 5 o 6 hm 7 bn 8 s1 9 s2 10 fp 11 rl 12 P;
9369+note 40 the arena device address, 47 the plan cursor. s1, s2 and SF hold 1/s (the CPU keeps s).
9370+global gga ulong 48
9371+global ggcn ulong 65537
9372+note byte strides: 0 D, 1 T D, 2 E, 3 T, 4 T T, 5 H T T, 6 R D, 7 D D (each times 4)
9373+global ggs uint 8
9374+note OMUTG: 1 in the product; 0 in mutant g (DK = -DS^T Q), which the parity check must catch.
9375+const OMUTG ulong 1
9376+
9377+fn ggdie c ulong to void
9378+oputc 71
9379+oputc 88
9380+oputc 32
9381+oputu c
9382+oputc 32
9383+oputl get nverr 0
9384+oputc 32
9385+oputu ulong get nvstage 0
9386+note diagnostics: launches done, mark1, mark2, QMD semaphore
9387+oputc 32
9388+oputl get gkt 0
9389+oputc 32
9390+oputu mapget get gbmem 3 0 4
9391+oputc 32
9392+oputu mapget get gbmem 3 16 4
9393+oputc 32
9394+oputu mapget get gbmem 2 8192 4
9395+oputc 10
9396+oflush
9397+exit 84
9398+end
9399+
9400+fn ggcd n ulong k ulong to ulong
9401+ret div add n sub k 1 k
9402+end
9403+
9404+fn ggr to ulong
9405+ret mul get og 6 get og 1
9406+end
9407+
9408+note P rounded up to the SQN chunk of 512; the gradient buffer is that long and zeroed to it
9409+fn ggp5 to ulong
9410+ret mul ggcd get og 8 512 512
9411+end
9412+
9413+fn ggput i ulong n ulong to void
9414+c = get gga 47
9415+put gga i c
9416+b = mul div add mul n 4 255 256 256
9417+put gga 47 add c b
9418+end
9419+
9420+note arena plan for the dims in og; returns its size in bytes
9421+fn ggplan to ulong
9422+tt = get og 1
9423+d = get og 2
9424+h = get og 3
9425+f = get og 4
9426+bb = get og 6
9427+r = ggr
9428+rd = mul r d
9429+pp = mul mul bb h mul tt tt
9430+put gga 47 0
9431+for i ulong 8
9432+ggput add 24 i rd
9433+end
9434+ggput 32 r
9435+ggput 33 r
9436+ggput 34 mul r f
9437+ggput 35 mul r f
9438+ggput 36 pp
9439+put gga 7 get gga 47
9440+put gga 47 0
9441+ggput 0 get og 8
9442+ggput 1 ggp5
9443+ggput 2 r
9444+ggput 3 r
9445+ggput 4 add get og 0 1
9446+ggput 5 r
9447+put gga 6 get gga 47
9448+ls = mul get gga 7 get og 5
9449+put gga 47 add get gga 47 ls
9450+ggput 8 rd
9451+ggput 9 r
9452+ggput 10 rd
9453+ggput 11 mul r get og 0
9454+ggput 12 mul r 3
9455+ggput 13 rd
9456+ggput 14 rd
9457+ggput 15 mul rd 3
9458+ggput 16 rd
9459+ggput 17 mul r f
9460+ggput 18 pp
9461+ggput 19 rd
9462+ggput 20 mul bb d
9463+ggput 21 get og 8
9464+ggput 22 get og 8
9465+ggput 23 div ggp5 512
9466+note 37: split-K partials for ggsk, at most 2^21 floats (ggks stops doubling once a grid has 1024 blocks)
9467+ggput 37 2097152
9468+ret get gga 47
9469+end
9470+
9471+fn gga1 i ulong to ulong
9472+ret add get gga 40 get gga i
9473+end
9474+
9475+fn ggl l ulong f ulong to ulong
9476+o = add get gga 6 mul l get gga 7
9477+ret add add get gga 40 o get gga add 24 f
9478+end
9479+
9480+note address of the output of layer l (the next hin, or HF after the last layer)
9481+fn ggout l ulong to ulong
9482+if lt add l 1 get og 5
9483+ret ggl add l 1 0
9484+end
9485+ret gga1 8
9486+end
9487+
9488+fn ggw o ulong to ulong
9489+ret add gga1 0 mul o 4
9490+end
9491+
9492+fn ggg o ulong to ulong
9493+ret add gga1 1 mul o 4
9494+end
9495+
9496+note GEMM tiles need m, n multiples of 32 and k of 16: V T D F E multiples of 32; R = B T;
9497+note softmax rows keep T/32 values per lane in R32..R47, so T is at most 512
9498+fn ggok to bool
9499+for i ulong 5
9500+v = get og i
9501+if eq i 3
9502+v = get og 7
9503+end
9504+if eq i 1
9505+v = get og 1
9506+end
9507+if or eq v 0 ne rem v 32 0
9508+ret false
9509+end
9510+end
9511+if gt get og 0 65536
9512+ret false
9513+end
9514+if gt get og 1 512
9515+ret false
9516+end
9517+ret le mul get og 6 get og 3 65535
9518+end
9519+
9520+note open the chip and emit the kernels for the dims in og
9521+fn ggopen to void
9522+if not ggok
9523+ggdie 1
9524+end
9525+asz = ggplan
9526+v = get og 0
9527+tt = get og 1
9528+d = get og 2
9529+h = get og 3
9530+f = get og 4
9531+bb = get og 6
9532+grset v tt d h f bb
9533+if not gropen asz
9534+ggdie 2
9535+end
9536+put gga 40 gkbase
9537+ggstr
9538+end
9539+
9540+note pointer j (1-based) into arg slots 2j, 2j + 1
9541+fn ggpt j uint a ulong to void
9542+put gkarg mul j 2 gblo a
9543+put gkarg add mul j 2 1 gbhi a
9544+end
9545+
9546+fn ggu s uint v ulong to void
9547+put gkarg s uint v
9548+end
9549+
9550+fn ggrun id uint na uint gx ulong gy ulong gz ulong to void
9551+put gkna 0 na
9552+x = uint gx
9553+y = uint gy
9554+z = uint gz
9555+if not gklaunch id x y z
9556+ggdie add 100 ulong id
9557+end
9558+end
9559+
9560+fn ggmm m ulong n ulong k ulong nz ulong h2 ulong bt bool to void
9561+a = uint m
9562+b = uint n
9563+c = uint k
9564+e = uint nz
9565+g = uint h2
9566+if not gkmm a b c e g bt
9567+ggdie 99
9568+end
9569+end
9570+
9571+note C[R][n] (+)= X[R][k] W[k][n]
9572+fn ggxw x ulong w ulong c ulong n ulong k ulong bt bool to void
9573+k4 = uint mul k 4
9574+n4 = uint mul n 4
9575+gkopa 0 x k4 4 0 0
9576+gkopa 1 w 4 n4 0 0
9577+gkopc c n4 4 0 0 f32 1.0f64
9578+ggmm ggr n k 1 1 bt
9579+end
9580+
9581+note C[R][n] (+)= X[R][k] W[n][k]^T
9582+fn ggxwt x ulong w ulong c ulong n ulong k ulong bt bool to void
9583+k4 = uint mul k 4
9584+n4 = uint mul n 4
9585+gkopa 0 x k4 4 0 0
9586+gkopa 1 w k4 4 0 0
9587+gkopc c n4 4 0 0 f32 1.0f64
9588+ggmm ggr n k 1 1 bt
9589+end
9590+
9591+note G[kk][n] += X[R][kk]^T DY[R][n], split over R when the grid is small (ggks)
9592+fn ggwg x ulong dy ulong g ulong kk ulong n ulong to void
9593+k4 = uint mul kk 4
9594+n4 = uint mul n 4
9595+s = ggks kk n 1
9596+if eq s 1
9597+gkopa 0 x 4 k4 0 0
9598+gkopa 1 dy 4 n4 0 0
9599+gkopc g n4 4 0 0 f32 1.0f64
9600+ggmm kk n ggr 1 1 true
9601+ret
9602+end
9603+sl = div ggr s
9604+gkopa 0 x 4 k4 0 uint mul mul sl kk 4
9605+gkopa 1 dy 4 n4 0 uint mul mul sl n 4
9606+ggsk kk n 1 s g 0
9607+end
9608+
9609+note Split-K for the weight gradients, whose depth is R = B T. A grid of (m/32)(n/32)nz blocks
9610+note leaves most SMs idle (rung-6 gprof: id 4 at 1.7-6 TFLOP/s on 64-256 blocks), so ggks doubles
9611+note the split s (up to 16, R/s a multiple of 16) until the grid reaches 1024 blocks: then
9612+note blocks s < 2048, so the m n nz s partials fit the 2^21 floats of slot 37.
9613+fn ggks m ulong n ulong nz ulong to ulong
9614+b = mul mul div m 32 div n 32 nz
9615+r = ggr
9616+s = ulong 1
9617+while lt mul b s 1024 bound 8
9618+s2 = mul s 2
9619+if or gt s2 16 ne rem r mul s2 16 0
9620+ret s
9621+end
9622+s = s2
9623+end
9624+ret s
9625+end
9626+
9627+note Operands A and B are set (gkopa) with the slice stride as s2. Batch z = z1 s + z2 (z1 < nz the
9628+note caller's batch, z2 < s the slice) writes partial P[z1][z2] = A[z1 z2]^T B[z1 z2] into slot 37,
9629+note then COLSUM 14 adds G[z1] (byte stride gz) += sum over z2 of P[z1][z2], in slice order.
9630+note Mutant p (OMUTP 0) adds one slice fewer, which gcmp2 must catch.
9631+const OMUTP ulong 1
9632+fn ggsk m ulong n ulong nz ulong s ulong g ulong gz uint to void
9633+mn = mul m n
9634+p = gga1 37
9635+n4 = uint mul n 4
9636+c1 = uint mul mul mn s 4
9637+c2 = uint mul mn 4
9638+gkopc p n4 4 c1 c2 f32 1.0f64
9639+k = div ggr s
9640+z = mul nz s
9641+ggmm m n k z s false
9642+ggskr mn nz s g gz
9643+end
9644+
9645+note COLSUM 14 over slot 37: G[c gz + i] += sum over z2 < s of P[c][z2][i], i < mn, c < nz
9646+fn ggskr mn ulong nz ulong s ulong g ulong gz uint to void
9647+kc = s
9648+if eq OMUTP 0
9649+kc = sub s 1
9650+end
9651+ggu 0 mn
9652+ggu 1 kc
9653+ggpt 1 gga1 37
9654+ggu 4 mul mn 4
9655+ggu 5 mul mul mn s 4
9656+ggpt 3 g
9657+ggu 8 ulong gz
9658+ggrun 14 9 ggcd mn 64 nz 1
9659+end
9660+
9661+note 19 COPY (x into y), 20 ZERO (x) over n elements (the FFN ReLU is fused into the GEMMs, gkope)
9662+fn ggew id uint n ulong x ulong y ulong to void
9663+ggu 0 n
9664+ggpt 1 x
9665+ggpt 2 y
9666+ggrun id 6 ggcd n 64 1 1
9667+end
9668+
9669+note RMSF/RMSB run one warp per row: arg 0 and the thread count are R*32
9670+fn ggrmsf v ulong g ulong y ulong s ulong to void
9671+r = mul ggr 32
9672+ggu 0 r
9673+ggpt 1 v
9674+ggpt 2 g
9675+ggpt 3 y
9676+ggpt 4 s
9677+ggrun 10 10 ggcd r 64 1 1
9678+end
9679+
9680+note dh += RMS backward of da; the gain terms go to WS
9681+fn ggrmsb v ulong g ulong s ulong to void
9682+r = mul ggr 32
9683+ggu 0 r
9684+ggpt 1 v
9685+ggpt 2 g
9686+ggpt 3 s
9687+ggpt 4 gga1 14
9688+ggpt 5 gga1 13
9689+ggpt 6 gga1 19
9690+ggrun 11 14 ggcd r 64 1 1
9691+end
9692+
9693+note gain gradient at parameter offset go += column sums of WS: per sequence (slot 13), then over B
9694+fn ggain go ulong to void
9695+d = get og 2
9696+tt = get og 1
9697+bb = get og 6
9698+ggu 0 d
9699+ggu 1 tt
9700+ggpt 1 gga1 19
9701+ggu 4 mul d 4
9702+ggu 5 mul mul tt d 4
9703+ggpt 3 gga1 20
9704+ggu 8 mul d 4
9705+ggrun 13 9 ggcd d 64 bb 1
9706+ggu 0 d
9707+ggu 1 bb
9708+ggpt 1 gga1 20
9709+ggu 4 mul d 4
9710+ggu 5 0
9711+ggpt 3 ggg go
9712+ggu 8 0
9713+ggrun 14 9 ggcd d 64 1 1
9714+end
9715+
9716+note softmax rows: one warp per row, T 32 threads per score block, grid (T/2, B H)
9717+fn ggsoft id uint p ulong dp ulong to void
9718+tt = get og 1
9719+ggu 0 mul tt 32
9720+ggpt 1 p
9721+ggpt 2 dp
9722+ggrun id 6 div tt 2 mul get og 6 get og 3 1
9723+end
9724+
9725+note attention GEMM over the B H (sequence, head) batch; operands set by gghd/ggpl/ggch/ggcp first;
9726+note cz: gkopz causal mode (1 A = P or DS, 2 A = P^T or DS^T, 3 C = S or DP)
9727+fn ggbh m ulong n ulong k ulong bt bool cz uint to void
9728+gkopz cz
9729+ggmm m n k mul get og 6 get og 3 get og 3 bt
9730+end
9731+
9732+note head-layout operand (rows of E inside D-wide rows, batch b then head); tr: k runs along rows
9733+fn gghd ab uint p ulong tr bool to void
9734+s0 = get ggs 0
9735+s1 = get ggs 1
9736+s2 = get ggs 2
9737+if tr
9738+gkopa ab p 4 s0 s1 s2
9739+else
9740+gkopa ab p s0 4 s1 s2
9741+end
9742+end
9743+
9744+note T x T score operand (batch b then head); tr: transposed
9745+fn ggpl ab uint p ulong tr bool to void
9746+s3 = get ggs 3
9747+s4 = get ggs 4
9748+s5 = get ggs 5
9749+if tr
9750+gkopa ab p 4 s3 s5 s4
9751+else
9752+gkopa ab p s3 4 s5 s4
9753+end
9754+end
9755+
9756+fn ggch c ulong al f64 to void
9757+s0 = get ggs 0
9758+s1 = get ggs 1
9759+s2 = get ggs 2
9760+gkopc c s0 4 s1 s2 f32 al
9761+end
9762+
9763+fn ggcp c ulong al f64 to void
9764+s3 = get ggs 3
9765+s4 = get ggs 4
9766+s5 = get ggs 5
9767+gkopc c s3 4 s5 s4 f32 al
9768+end
9769+
9770+note stride table for the dims in og (bytes)
9771+fn ggstr to void
9772+d = get og 2
9773+tt = get og 1
9774+put ggs 0 uint mul d 4
9775+put ggs 1 uint mul tt mul d 4
9776+put ggs 2 uint mul get og 7 4
9777+put ggs 3 uint mul tt 4
9778+put ggs 4 uint mul tt mul tt 4
9779+put ggs 5 uint mul get og 3 mul tt mul tt 4
9780+put ggs 6 uint mul ggr mul d 4
9781+put ggs 7 uint mul d mul d 4
9782+end
9783+
9784+note q, k, u = a wq, a wk, a wv as one batched GEMM (wq wk wv and q k u are contiguous)
9785+fn ggqkv l ulong to void
9786+d = get og 2
9787+a = ggl l 1
9788+w = ggw opwq l
9789+c = ggl l 2
9790+s0 = get ggs 0
9791+s6 = get ggs 6
9792+s7 = get ggs 7
9793+gkopa 0 a s0 4 0 0
9794+gkopa 1 w 4 s0 0 s7
9795+gkopc c s0 4 0 s6 f32 1.0f64
9796+ggmm ggr d d 3 3 false
9797+end
9798+
9799+note P = softmax(q k^T / sqrt(E)), o = P u; causal: only S on and below the diagonal is formed
9800+note (gkmm cz 3) and o stops at the diagonal (cz 1), since P is exactly 0 above it
9801+fn ggatt l ulong to void
9802+tt = get og 1
9803+e = get og 7
9804+pp = ggl l 12
9805+q = ggl l 2
9806+k = ggl l 3
9807+gghd 0 q false
9808+gghd 1 k false
9809+isq = div 1.0f64 fsqrt f64 e
9810+ggcp pp isq
9811+ggbh tt tt e false 3
9812+ggsoft 15 pp pp
9813+u = ggl l 4
9814+o = ggl l 5
9815+ggpl 0 pp false
9816+gghd 1 u true
9817+ggch o 1.0f64
9818+ggbh tt e tt false 1
9819+end
9820+
9821+note hm = hin + o wo; bn = rms(hm) g2; out = hm + relu(bn w1) w2
9822+fn ggffn l ulong to void
9823+d = get og 2
9824+f = get og 4
9825+rd = mul ggr d
9826+hm = ggl l 6
9827+x = ggl l 0
9828+ggew 19 rd x hm
9829+x = ggl l 5
9830+w = ggw opwo l
9831+ggxw x w hm d d true
9832+x = ggl l 7
9833+w = ggw opg2 l
9834+s = ggl l 9
9835+ggrmsf hm w x s
9836+fp = ggl l 10
9837+w = ggw opw1 l
9838+y = ggl l 11
9839+gkope 1 y
9840+ggxw x w fp f d false
9841+x = y
9842+o = ggout l
9843+ggew 19 rd hm o
9844+w = ggw opw2 l
9845+ggxw x w o d f true
9846+end
9847+
9848+fn ggfl l ulong to void
9849+x = ggl l 0
9850+w = ggw opg1 l
9851+a = ggl l 1
9852+s = ggl l 8
9853+ggrmsf x w a s
9854+ggqkv l
9855+ggatt l
9856+ggffn l
9857+end
9858+
9859+fn ggfwd to void
9860+d = get og 2
9861+x = ggl 0 0
9862+ggu 0 d
9863+ggpt 1 x
9864+x = ggw 0
9865+ggpt 2 x
9866+x = ggw get og 30
9867+ggpt 3 x
9868+x = gga1 2
9869+ggpt 4 x
9870+tt = get og 1
9871+bb = get og 6
9872+ggrun 9 10 ggcd d 64 tt bb
9873+for l ulong get og 5
9874+ggfl l
9875+end
9876+x = gga1 8
9877+w = ggw get og 33
9878+z = gga1 10
9879+s = gga1 9
9880+ggrmsf x w z s
9881+x = gga1 11
9882+w = ggw 0
9883+v = get og 0
9884+ggxwt z w x v d false
9885+r = mul ggr 32
9886+ggu 0 r
9887+ggpt 1 x
9888+x = gga1 3
9889+ggpt 2 x
9890+x = gga1 12
9891+ggpt 3 x
9892+ggrun 21 8 ggcd r 64 1 1
9893+end
9894+
9895+note layer l FFN backward: DH holds d out on entry, d hm on exit. The dF GEMM's fused epilogue (gkope 2)
9896+note applies the ReLU mask FP > 0; the forward W1 GEMM (ggffn, gkope 1) stores both FP and Y.
9897+fn ggbffn l ulong to void
9898+d = get og 2
9899+f = get og 4
9900+dh = gga1 13
9901+df = gga1 17
9902+da = gga1 14
9903+w = ggw opw2 l
9904+x = ggl l 10
9905+gkope 2 x
9906+ggxwt dh w df f d false
9907+x = ggl l 11
9908+w = ggg opw2 l
9909+ggwg x dh w f d
9910+x = ggl l 7
9911+w = ggg opw1 l
9912+ggwg x df w d f
9913+w = ggw opw1 l
9914+ggxwt df w da d f false
9915+x = ggl l 6
9916+w = ggw opg2 l
9917+s = ggl l 9
9918+ggrmsb x w s
9919+ggain opg2 l
9920+end
9921+
9922+note layer l attention backward: DO, DP, DU, DS (in DP), DQ, DK, then DA. Causal as in ggatt: DP
9923+note on and below the diagonal (cz 3, SOFTB reads dp above it as 0), DU and DK start at the diagonal
9924+note (cz 2), DQ stops there (cz 1).
9925+fn ggbatt l ulong to void
9926+d = get og 2
9927+tt = get og 1
9928+e = get og 7
9929+dh = gga1 13
9930+dn = gga1 16
9931+w = ggw opwo l
9932+ggxwt dh w dn d d false
9933+x = ggl l 5
9934+w = ggg opwo l
9935+ggwg x dh w d d
9936+dp = gga1 18
9937+x = ggl l 4
9938+gghd 0 dn false
9939+gghd 1 x false
9940+ggcp dp 1.0f64
9941+ggbh tt tt e false 3
9942+pp = ggl l 12
9943+dq = gga1 15
9944+dk = add dq ulong get ggs 6
9945+du = add dk ulong get ggs 6
9946+ggpl 0 pp true
9947+gghd 1 dn true
9948+ggch du 1.0f64
9949+ggbh tt e tt false 2
9950+ggsoft 16 pp dp
9951+x = ggl l 3
9952+ggpl 0 dp false
9953+gghd 1 x true
9954+ggch dq 1.0f64
9955+ggbh tt e tt false 1
9956+al = 1.0f64
9957+if eq OMUTG 0
9958+al = neg 1.0f64
9959+end
9960+x = ggl l 2
9961+ggpl 0 dp true
9962+gghd 1 x true
9963+ggch dk al
9964+ggbh tt e tt false 2
9965+end
9966+
9967+note DA = DQ wq^T + DK wk^T + DU wv^T; G wq wk wv += a^T (DQ DK DU) batched; then the rms backward
9968+fn ggbqkv l ulong to void
9969+d = get og 2
9970+da = gga1 14
9971+dq = gga1 15
9972+dk = add dq ulong get ggs 6
9973+du = add dk ulong get ggs 6
9974+w = ggw opwq l
9975+ggxwt dq w da d d false
9976+w = ggw opwk l
9977+ggxwt dk w da d d true
9978+w = ggw opwv l
9979+ggxwt du w da d d true
9980+a = ggl l 1
9981+w = ggg opwq l
9982+s0 = get ggs 0
9983+s6 = get ggs 6
9984+s7 = get ggs 7
9985+k = ggks d d 3
9986+if eq k 1
9987+gkopa 0 a 4 s0 0 0
9988+gkopa 1 dq 4 s0 0 s6
9989+gkopc w s0 4 0 s7 f32 1.0f64
9990+ggmm d d ggr 3 3 true
9991+else
9992+sl = uint div ggr k
9993+gkopa 0 a 4 s0 0 mul sl s0
9994+gkopa 1 dq 4 s0 s6 mul sl s0
9995+ggsk d d 3 k w s7
9996+end
9997+x = ggl l 0
9998+w = ggw opg1 l
9999+s = ggl l 8
10000+ggrmsb x w s
10001+ggain opg1 l
10002+end
10003+
10004+fn ggbl l ulong to void
10005+ggbffn l
10006+ggbatt l
10007+ggbqkv l
10008+end
10009+
10010+fn ggbwd to void
10011+d = get og 2
10012+v = get og 0
10013+tt = get og 1
10014+r = ggr
10015+ggew 20 ggp5 gga1 1 gga1 1
10016+ggew 20 mul r d gga1 13 gga1 13
10017+ggxw gga1 11 ggw 0 gga1 14 d v false
10018+ggwg gga1 11 gga1 10 ggg 0 v d
10019+ggrmsb gga1 8 ggw get og 33 gga1 9
10020+ggain get og 33
10021+l = get og 5
10022+while gt l 0 bound 1000
10023+l = sub l 1
10024+ggbl l
10025+end
10026+td = mul tt d
10027+ggu 0 td
10028+ggu 1 get og 6
10029+ggpt 1 gga1 13
10030+ggu 4 mul td 4
10031+ggu 5 0
10032+ggpt 3 ggg get og 30
10033+ggu 8 0
10034+ggrun 14 9 ggcd td 64 1 1
10035+ggu 0 d
10036+ggpt 1 gga1 13
10037+ggpt 2 gga1 4
10038+ggpt 3 gga1 5
10039+ggpt 4 ggg 0
10040+ggrun 22 10 ggcd d 64 v 1
10041+end
10042+
10043+note Mutant f: 1 in the product, 0 in the mutant, which uploads layer 0's W1 times 1 + 2^-10 (a
10044+note forward error on the chip that gcmp2's FP check must name).
10045+const OMUTF ulong 1
10046+
10047+note W[i] as f32 for i in chunk q of P (a loop runs at most 999999 passes)
10048+fn ggupc q ulong p ulong to void
10049+h = get gbmem 5
10050+wo = get gga 0
10051+i0 = mul q OCH
10052+for j ulong sub oce q p i0
10053+i = add i0 j
10054+x = bitcast uint f32 ggupv i
10055+m = mapset h add wo mul i 4 4 ulong x
10056+end
10057+end
10058+
10059+note the uploaded value of W[i]: the parameter at virtual index i in the product
10060+fn ggupv i ulong to f64
10061+w = owg i
10062+if eq OMUTF 0
10063+a = opw1 0
10064+if and ge i a lt i add a mul get og 2 get og 4
10065+w = mul w 1.0009765625f64
10066+end
10067+end
10068+ret w
10069+end
10070+
10071+note upload the f32 parameters
10072+fn ggupw to void
10073+p = get og 8
10074+for q ulong ocn p
10075+ggupc q p
10076+end
10077+put gkinv 0 1
10078+end
10079+
10080+note upload the batch tokens and the CSR of x (rows sorted by token)
10081+fn ggupb to void
10082+ggupt
10083+ggcsr
10084+put gkinv 0 1
10085+end
10086+
10087+note n values of the Adam pool from s as f32 to arena byte offset o + 4 j, and back (one chunk of an
10088+note Adam moment)
10089+fn ggupmc s ulong o ulong n ulong to void
10090+h = get gbmem 5
10091+for j ulong n
10092+x = bitcast uint f32 omvg add s j
10093+m = mapset h add o mul j 4 4 ulong x
10094+end
10095+end
10096+
10097+fn ggdnmc s ulong o ulong n ulong to void
10098+h = get gbmem 5
10099+for j ulong n
10100+x = uint mapget h add o mul j 4 4
10101+omvp add s j f64 bitcast f32 x
10102+end
10103+end
10104+
10105+note chip parameters of chunk q back into the parameter banks as f64
10106+fn ggdnwc q ulong p ulong to void
10107+h = get gbmem 5
10108+o = get gga 0
10109+i0 = mul q OCH
10110+for j ulong sub oce q p i0
10111+i = add i0 j
10112+x = uint mapget h add o mul i 4 4
10113+owp i f64 bitcast f32 x
10114+end
10115+end
10116+
10117+note optimizer state (parameters, Adam pool) to the chip as f32 (m in gga 21, v in gga 22), and back
10118+fn ggupst to void
10119+ggupw
10120+p = get og 8
10121+for q ulong ocn p
10122+i0 = mul q OCH
10123+n = sub oce q p i0
10124+ggupmc add get og 11 i0 add get gga 21 mul i0 4 n
10125+ggupmc add get og 12 i0 add get gga 22 mul i0 4 n
10126+end
10127+put gkinv 0 1
10128+end
10129+
10130+fn ggdnst to void
10131+p = get og 8
10132+for q ulong ocn p
10133+ggdnwc q p
10134+i0 = mul q OCH
10135+n = sub oce q p i0
10136+ggdnmc add get og 11 i0 add get gga 21 mul i0 4 n
10137+ggdnmc add get og 12 i0 add get gga 22 mul i0 4 n
10138+end
10139+end
10140+
10141+fn ggupt to void
10142+h = get gbmem 5
10143+xo = get gga 2
10144+yo = get gga 3
10145+for i ulong ggr
10146+tk = get obx i
10147+m = mapset h add xo mul i 4 4 tk
10148+ty = get oby i
10149+m = mapset h add yo mul i 4 4 ty
10150+end
10151+end
10152+
10153+note counting sort of the R rows by input token: start (V + 1) and order (R) arrays
10154+fn ggcsr to void
10155+h = get gbmem 5
10156+v = get og 0
10157+for c ulong add v 1
10158+put ggcn c 0
10159+end
10160+for i ulong ggr
10161+c1 = add get obx i 1
10162+n = add get ggcn c1 1
10163+put ggcn c1 n
10164+end
10165+for c ulong v
10166+c1 = add c 1
10167+n = add get ggcn c1 get ggcn c
10168+put ggcn c1 n
10169+end
10170+so = get gga 4
10171+for c ulong add v 1
10172+n = get ggcn c
10173+m = mapset h add so mul c 4 4 n
10174+end
10175+so = get gga 5
10176+for i ulong ggr
10177+c = get obx i
10178+p = get ggcn c
10179+m = mapset h add so mul p 4 4 i
10180+put ggcn c add p 1
10181+end
10182+end
10183+
10184+note GRD[i] = G[i] for i in chunk q of P
10185+fn ggdnc q ulong p ulong to void
10186+h = get gbmem 5
10187+go = get gga 1
10188+i0 = mul q OCH
10189+for j ulong sub oce q p i0
10190+i = add i0 j
10191+x = uint mapget h add go mul i 4 4
10192+ocsp i f64 bitcast f32 x
10193+end
10194+end
10195+
10196+fn ggdown to void
10197+p = get og 8
10198+for q ulong ocn p
10199+ggdnc q p
10200+end
10201+end
10202+
10203+note one f32 at arena byte offset o, as f64
10204+fn ggf o ulong to f64
10205+x = uint mapget get gbmem 5 o 4
10206+ret f64 bitcast f32 x
10207+end
10208+
10209+note row loss from the CE stats (m, sum, l_y) of row r: log(sum) + m - l_y
10210+fn ggrl r ulong to f64
10211+o = add get gga 12 mul r 12
10212+m = ggf o
10213+sm = ggf add o 4
10214+ly = ggf add o 8
10215+ret sub add flog sm m ly
10216+end
10217+
10218+fn ggloss to f64
10219+tt = get og 1
10220+for b ulong get og 6
10221+s = 0.0f64
10222+for t ulong tt
10223+s = add s ggrl add mul b tt t
10224+end
10225+put olos b div s f64 tt
10226+end
10227+ret olsum
10228+end
10229+
10230+note the step loss and gradient for the batch in obx/oby; ggopen first. full: upload the f32
10231+note parameters first and download the gradient into ogr[0..P) after; otherwise both stay on chip.
10232+note phase wall times (ns): 0 upload, 1 forward, 2 backward, 3 download, 4 loss, 5 on-chip Adam
10233+global ggtm long 6
10234+fn ggpass full bool to f64
10235+t = monons
10236+if full
10237+ggupw
10238+end
10239+ggupb
10240+u = monons
10241+put ggtm 0 sub u t
10242+ggfwd
10243+t = monons
10244+put ggtm 1 sub t u
10245+ggbwd
10246+u = monons
10247+put ggtm 2 sub u t
10248+if full
10249+ggdown
10250+end
10251+t = monons
10252+put ggtm 3 sub t u
10253+l = ggloss
10254+u = monons
10255+put ggtm 4 sub u t
10256+ret l
10257+end
10258+
10259+fn gggrad to f64
10260+ret ggpass true
10261+end
10262+
10263+note clip and Adam on chip at step og[41], the f32 image of oadam: SQN partial sums of g^2, the
10264+note norm, clip factor, schedule and bias corrections on the host in f64, then ADAM over P
10265+fn ggadam to void
10266+p = get og 8
10267+nc = div ggp5 512
10268+ggu 0 nc
10269+ggpt 1 gga1 1
10270+ggpt 2 gga1 23
10271+ggrun 23 6 ggcd nc 64 1 1
10272+h = get gbmem 5
10273+so = get gga 23
10274+ss = 0.0f64
10275+for c ulong nc
10276+x = uint mapget h add so mul c 4 4
10277+ss = add ss f64 bitcast f32 x
10278+end
10279+nr = fsqrt ss
10280+cl = 1.0f64
10281+if gt nr 1.0
10282+cl = div 1.0 nr
10283+end
10284+s = get og 41
10285+lrs = olrs s bitcast f64 get og 43
10286+bc1 = sub 1.0 opowb 0.9 s
10287+bc2 = sub 1.0 opowb 0.99 s
10288+if eq OMUTC 0
10289+bc1 = 1.0f64
10290+bc2 = 1.0f64
10291+end
10292+ggu 0 p
10293+ggpt 1 gga1 0
10294+ggpt 2 gga1 21
10295+ggpt 3 gga1 22
10296+ggpt 4 gga1 1
10297+ggu 10 ulong grf cl
10298+ggu 11 ulong grf div lrs bc1
10299+ggu 12 ulong grf div 1.0f64 bc2
10300+ggrun 24 13 ggcd p 64 1 1
10301+end
10302+
10303+fn ggtms to void
10304+oputc 71
10305+oputc 84
10306+for i ulong 6
10307+oputc 32
10308+oputl get ggtm i
10309+end
10310+for i ulong 4
10311+oputc 32
10312+oputl get gkt i
10313+end
10314+oputc 10
10315+end
10316+
10317+note keep the CPU gradient: BLK[i] = GRD[i] (chunked: a loop runs at most 999999 passes)
10318+fn ggkeep p ulong to void
10319+for q ulong ocn p
10320+i0 = mul q OCH
10321+for j ulong sub oce q p i0
10322+i = add i0 j
10323+ogrp add p i ocsg i
10324+end
10325+end
10326+end
10327+
10328+note ggcm[0] = max |GRD[i] - BLK[i]|, ggcm[1] = max |BLK[i]| over i < P
10329+global ggcm f64 2
10330+fn ggmaxd p ulong to void
10331+put ggcm 0 0.0f64
10332+put ggcm 1 0.0f64
10333+for q ulong ocn p
10334+i0 = mul q OCH
10335+for j ulong sub oce q p i0
10336+i = add i0 j
10337+rf = ogrg add p i
10338+dd = sub ocsg i rf
10339+if lt dd 0.0f64
10340+dd = neg dd
10341+end
10342+if lt rf 0.0f64
10343+rf = neg rf
10344+end
10345+if gt dd get ggcm 0
10346+put ggcm 0 dd
10347+end
10348+if gt rf get ggcm 1
10349+put ggcm 1 rf
10350+end
10351+end
10352+end
10353+end
10354+
10355+note gcmp CK DATA S: CPU ograd and GPU gradient for batch S; prints
10356+note `GC P maxabs_e12 maxref_e12 rel_e9 cpu_loss_micro gpu_loss_micro gpu_ns tol_e9` and GCMP PASS
10357+note (rel < tol, |loss difference| < 1e-5) or GCMP FAIL (exit 45). tol = R 2^-24 is the first-order
10358+note bound gamma_R on the f32 error of a sum of R = B T rows, the longest reduction of the weight
10359+note gradients, relative to the sum of magnitudes; the CPU reference is f64. ReLU units whose f64
10360+note pre-activation lies within f32 resolution of zero can take the other side on the GPU (receipt:
10361+note evidence/OMEGALLM/gpu/README.md), and both sides are valid subgradients.
10362+fn ggcmp to void
10363+if ne oargc 5
10364+exit 2
10365+end
10366+ockload 2
10367+odopen 3
10368+obatch oargu 4
10369+put og 10 0
10370+lc = ograd
10371+p = get og 8
10372+ggkeep p
10373+ggopen
10374+t0 = monons
10375+lg = gggrad
10376+t1 = monons
10377+ggmaxd p
10378+mx = get ggcm 0
10379+mr = get ggcm 1
10380+rel = div mx mr
10381+dl = sub lg lc
10382+if lt dl 0.0f64
10383+dl = neg dl
10384+end
10385+oputc 71
10386+oputc 67
10387+oputc 32
10388+oputu p
10389+oputc 32
10390+oputl rmic mul mx 1000000.0f64
10391+oputc 32
10392+oputl rmic mul mr 1000000.0f64
10393+oputc 32
10394+oputl rmic mul rel 1000.0f64
10395+oputc 32
10396+oputl rmic lc
10397+oputc 32
10398+oputl rmic lg
10399+oputc 32
10400+oputl sub t1 t0
10401+oputc 32
10402+tol = div f64 ggr 16777216.0f64
10403+oputl rmic mul tol 1000.0f64
10404+oputc 10
10405+ggtms
10406+ok = and lt rel tol lt dl 0.00001f64
10407+r = nvstop
10408+oputc 71
10409+oputc 67
10410+oputc 77
10411+oputc 80
10412+oputc 32
10413+if ok
10414+oputc 80
10415+oputc 65
10416+oputc 83
10417+oputc 83
10418+oputc 10
10419+oflush
10420+ret
10421+end
10422+oputc 70
10423+oputc 65
10424+oputc 73
10425+oputc 76
10426+oputc 10
10427+oflush
10428+exit 45
10429+end
10430+
10431+note true when x is finite (every comparison with NaN is false)
10432+fn ggfin x f64 to bool
10433+a = x
10434+if lt a 0.0f64
10435+a = neg a
10436+end
10437+ret le a bitcast f64 ulong 9218868437227405311
10438+end
10439+
10440+note x for printing: 1e6 when x is not finite or above 1e6 (rmic traps past the long range)
10441+fn ggclp x f64 to f64
10442+if ggfin x
10443+if lt x 1000000.0f64
10444+ret x
10445+end
10446+end
10447+ret 1000000.0f64
10448+end
10449+
10450+note gcmp2 state. ggfs[j] = s_j for the row in flight (F <= 1536, CONTRACT-V2). ggk2: [0] ReLU
10451+note flips (sign of FP_chip differs from FP_ref), [1] non-finite FP values (chip or reference),
10452+note [2] layer of the largest rho, [3] non-finite gradient values (chip, K0 or K1).
10453+note ggk3: [0] rho max, [1] max |FP_ref| at a flip, [2] max |FP_chip - FP_ref|.
10454+global ggfs f64 1536
10455+global ggk2 ulong 4
10456+global ggk3 f64 3
10457+
10458+note s_j = sum_k |bn_k| |W1[k F + j]| for row t of layer l (k < D ascending), the magnitude sum of
10459+note the FP dot product the chip rounds (layer l is staged: ogmask runs inside olayb)
10460+fn ggsj l ulong t ulong to void
10461+d = get og 2
10462+f = get og 4
10463+bn = add oabn l mul t d
10464+w = opx opw1 l
10465+for j ulong f
10466+put ggfs j 0.0f64
10467+end
10468+for k ulong d
10469+a = get oa add bn k
10470+if lt a 0.0f64
10471+a = neg a
10472+end
10473+ggsjk a add w mul k f f
10474+end
10475+end
10476+
10477+fn ggsjk a f64 wo ulong f ulong to void
10478+for j ulong f
10479+x = get ow add wo j
10480+if lt x 0.0f64
10481+x = neg x
10482+end
10483+put ggfs j add get ggfs j mul a x
10484+end
10485+end
10486+
10487+note model.om's ReLU mask hook (ogkm[0] = 1, gcmp2 only): for row t of layer l of the sequence in
10488+note flight, check every unit's reference FP against the chip's FP (left in the arena by gggrad),
10489+note then zero dF where the chip's FP is not > 0, the chip's fused dF mask (gkope 2; NaN included).
10490+fn ogmask yo ulong l ulong t ulong to void
10491+f = get og 4
10492+xo = add oafp l mul t f
10493+ggsj l t
10494+row = add mul get ogkm 1 get og 1 t
10495+c0 = add sub ggl l 10 get gga 40 mul mul row f 4
10496+for j ulong f
10497+c = ggf add c0 mul j 4
10498+ggk1 get oa add xo j c j l
10499+if not gt c 0.0f64
10500+put oa add yo j 0.0f64
10501+end
10502+end
10503+end
10504+
10505+note one unit: non-finite count, |FP_chip - FP_ref|, rho = that over s_j (inf when s_j = 0 and the
10506+note values differ), and the flip count
10507+fn ggk1 v f64 c f64 j ulong l ulong to void
10508+if not and ggfin v ggfin c
10509+put ggk2 1 add get ggk2 1 1
10510+ret
10511+end
10512+dd = sub c v
10513+if lt dd 0.0f64
10514+dd = neg dd
10515+end
10516+if gt dd get ggk3 2
10517+put ggk3 2 dd
10518+end
10519+ggkr dd get ggfs j l
10520+ggkfl v c
10521+end
10522+
10523+fn ggkr dd f64 s f64 l ulong to void
10524+if not gt dd 0.0f64
10525+ret
10526+end
10527+rho = bitcast f64 ulong 9218868437227405312
10528+if gt s 0.0f64
10529+rho = div dd s
10530+end
10531+if gt rho get ggk3 0
10532+put ggk3 0 rho
10533+put ggk2 2 l
10534+end
10535+end
10536+
10537+fn ggkfl v f64 c f64 to void
10538+a = gt v 0.0f64
10539+b = gt c 0.0f64
10540+if eq a b
10541+ret
10542+end
10543+put ggk2 0 add get ggk2 0 1
10544+if lt v 0.0f64
10545+v = neg v
10546+end
10547+if gt v get ggk3 1
10548+put ggk3 1 v
10549+end
10550+end
10551+
10552+note Adam pool [o + i] = GRD[i] for i < P (the chip gradient at o = 0, K0 at o = P; gcmp2 has no
10553+note Adam step)
10554+fn ggstash p ulong o ulong to void
10555+for q ulong ocn p
10556+ggstq q p o
10557+end
10558+end
10559+
10560+fn ggstq q ulong p ulong o ulong to void
10561+i0 = mul q OCH
10562+for j ulong sub oce q p i0
10563+i = add i0 j
10564+omvp add o i ocsg i
10565+end
10566+end
10567+
10568+note ggc2: [0] max |G_chip - K0|, [1] max |K0|, [2] max |G_chip - K1| over i < P, with the chip
10569+note gradient in the Adam pool [0, P), K0 in [P, 2P) and K1 in GRD; non-finite values count in ggk2[3]
10570+global ggc2 f64 3
10571+fn gg2cmp p ulong to void
10572+for k ulong 3
10573+put ggc2 k 0.0f64
10574+end
10575+for q ulong ocn p
10576+i0 = mul q OCH
10577+for j ulong sub oce q p i0
10578+gg2c1 add i0 j p
10579+end
10580+end
10581+end
10582+
10583+fn gg2c1 i ulong p ulong to void
10584+c = omvg i
10585+r0 = omvg add p i
10586+r1 = ocsg i
10587+if not and and ggfin c ggfin r0 ggfin r1
10588+put ggk2 3 add get ggk2 3 1
10589+ret
10590+end
10591+gg2mx 0 sub c r0
10592+gg2mx 1 r0
10593+gg2mx 2 sub c r1
10594+end
10595+
10596+fn gg2mx k ulong x f64 to void
10597+if lt x 0.0f64
10598+x = neg x
10599+end
10600+if gt x get ggc2 k
10601+put ggc2 k x
10602+end
10603+end
10604+
10605+note gcmp2 CK DATA S: branch-conditioned parity (evidence/OMEGALLM/gpu-max/README.md). The chip
10606+note gradient runs first and stays in the Adam pool; K0 is the CPU ograd with its own ReLU mask (gcmp's
10607+note reference), K1 the CPU ograd with the chip's FP sign as the ReLU mask (ogmask), which also checks
10608+note every FFN pre-activation. Prints `GC2 P rel0_e9 rel1_e9 tolr_e9 rho_e9 tolf_e9 rho_layer flips
10609+note vflip_e12 fpdiff_e12 nonfinite cpu_loss_micro gpu_loss_micro gpu_ns fails` and GCMP2 PASS, or
10610+note GCMP2 FAIL (exit 47). rel0 = max|G_chip - K0| / max|K0| (gcmp's rel), rel1 = max|G_chip - K1| /
10611+note max|K0|; rho = max |FP_chip - FP_ref| / s_j. fails is a bit set: 1 a non-finite FP or gradient
10612+note value, 2 rho >= tolf = max(D, F, T) 2^-24, 4 rel1 >= tolr = R 2^-24 (gcmp's gamma_R), 8 |chip
10613+note loss - K0 loss| >= 1e-5.
10614+fn ggcmp2 to void
10615+if ne oargc 5
10616+exit 2
10617+end
10618+ockload 2
10619+odopen 3
10620+obatch oargu 4
10621+p = get og 8
10622+ggopen
10623+t0 = monons
10624+lg = gggrad
10625+t1 = monons
10626+ggstash p 0
10627+put og 10 0
10628+put ogkm 0 0
10629+l0 = ograd
10630+ggstash p p
10631+for k ulong 4
10632+put ggk2 k 0
10633+end
10634+for k ulong 3
10635+put ggk3 k 0.0f64
10636+end
10637+put ogkm 0 1
10638+put og 10 0
10639+l1 = ograd
10640+put ogkm 0 0
10641+gg2cmp p
10642+ggc2pr lg l0 sub t1 t0
10643+end
10644+
10645+note the GC2 line and verdict of gcmp2 (lg chip loss, l0 K0 loss, ns chip gradient time); gbcmp2
10646+note (gkbf 1) takes gb.om's verdict
10647+fn ggc2pr lg f64 l0 f64 ns long to void
10648+fl = ggc2fl lg l0
10649+ggc2ln lg l0 ns fl
10650+ggtms
10651+r = nvstop
10652+if eq get gkbf 0 1
10653+gbc2vd lg l0 fl
10654+ret
10655+end
10656+ggc2vd eq fl 0
10657+end
10658+
10659+note ggc2[k] / max |K0|, clamped for printing
10660+fn gg2rl k ulong to f64
10661+ret ggclp div get ggc2 k get ggc2 1
10662+end
10663+
10664+note tolf = max(D, F, T) 2^-24
10665+fn gg2tf to f64
10666+n = fmax f64 get og 2 fmax f64 get og 4 f64 get og 1
10667+ret div n 16777216.0f64
10668+end
10669+
10670+note tolr = R 2^-24 (gcmp's gamma_R)
10671+fn gg2tr to f64
10672+ret div f64 ggr 16777216.0f64
10673+end
10674+
10675+note the gcmp2 fail bits (see ggcmp2); gb.om's BF16 bits under gkbf 1
10676+fn ggc2fl lg f64 l0 f64 to ulong
10677+if eq get gkbf 0 1
10678+ret gbc2fl lg l0 get ggk3 0 add get ggk2 1 get ggk2 3
10679+end
10680+dl = sub lg l0
10681+if lt dl 0.0f64
10682+dl = neg dl
10683+end
10684+fl = ulong 0
10685+if ne add get ggk2 1 get ggk2 3 0
10686+fl = or fl 1
10687+end
10688+if not lt ggclp get ggk3 0 gg2tf
10689+fl = or fl 2
10690+end
10691+if not lt gg2rl 2 gg2tr
10692+fl = or fl 4
10693+end
10694+if not lt dl 0.00001f64
10695+fl = or fl 8
10696+end
10697+ret fl
10698+end
10699+
10700+fn ggc2ln lg f64 l0 f64 ns long fl ulong to void
10701+oputc 71
10702+oputc 67
10703+oputc 50
10704+oputc 32
10705+oputu get og 8
10706+gg2pe gg2rl 0
10707+gg2pe gg2rl 2
10708+gg2pe gg2tr
10709+gg2pe ggclp get ggk3 0
10710+gg2pe gg2tf
10711+oputc 32
10712+oputu get ggk2 2
10713+oputc 32
10714+oputu get ggk2 0
10715+oputc 32
10716+oputl rmic mul ggclp get ggk3 1 1000000.0f64
10717+oputc 32
10718+oputl rmic mul ggclp get ggk3 2 1000000.0f64
10719+oputc 32
10720+oputu add get ggk2 1 get ggk2 3
10721+oputc 32
10722+oputl rmic ggclp l0
10723+oputc 32
10724+oputl rmic ggclp lg
10725+oputc 32
10726+oputl ns
10727+oputc 32
10728+oputu fl
10729+oputc 10
10730+end
10731+
10732+note " x_e9"
10733+fn gg2pe x f64 to void
10734+oputc 32
10735+oputl rmic mul x 1000.0f64
10736+end
10737+
10738+fn ggc2vd ok bool to void
10739+oputc 71
10740+oputc 67
10741+oputc 77
10742+oputc 80
10743+oputc 50
10744+oputc 32
10745+if ok
10746+oputc 80
10747+oputc 65
10748+oputc 83
10749+oputc 83
10750+oputc 10
10751+oflush
10752+ret
10753+end
10754+oputc 70
10755+oputc 65
10756+oputc 73
10757+oputc 76
10758+oputc 10
10759+oflush
10760+exit 47
10761+end
10762+
10763+note gtrain CK TRAIN UNTIL EVERY: cmdtrain with the gradient from the GPU (K = 1, no workers); prints
10764+note `T s+1 loss_micro` at save points like train
10765+fn ggtrain to void
10766+if ne oargc 6
10767+exit 2
10768+end
10769+ockload 2
10770+odopen 3
10771+un = oargu 4
10772+ev = oargu 5
10773+if eq ev 0
10774+exit 2
10775+end
10776+en = get og 42
10777+if lt un en
10778+en = un
10779+end
10780+ggopen
10781+while lt get og 41 en bound 999999
10782+s = get og 41
10783+obatch s
10784+put og 10 0
10785+ls = gggrad
10786+oadam
10787+s1 = add s 1
10788+put og 41 s1
10789+if or eq rem s1 ev 0 eq s1 en
10790+ocksave 2
10791+oputc 84
10792+oputc 32
10793+oputu s1
10794+oputc 32
10795+oputl rmic ls
10796+oputc 10
10797+oflush
10798+end
10799+end
10800+r = nvstop
10801+end
10802+
10803+note BLK[i] = the f64 parameter i before the step
10804+fn ggkw q ulong p ulong to void
10805+i0 = mul q OCH
10806+for j ulong sub oce q p i0
10807+i = add i0 j
10808+ogrp add p i owg i
10809+end
10810+end
10811+
10812+note GRD[i] = chip parameter i as f64
10813+fn ggdw q ulong p ulong to void
10814+h = get gbmem 5
10815+wo = get gga 0
10816+i0 = mul q OCH
10817+for j ulong sub oce q p i0
10818+i = add i0 j
10819+x = uint mapget h add wo mul i 4 4
10820+ocsp i f64 bitcast f32 x
10821+end
10822+end
10823+
10824+note over i < P with w0 = BLK[i], c = parameter i (CPU f64 result), g = GRD[i] (chip f32 result):
10825+note u = c - w0, d = (g - f32(w0)) - u; ggam 0 max |d|, 1 max |u|, 2 max (|d| - 2^-24 |c|), the part
10826+note of d above the half-ulp rounding of the f32 master weight
10827+global ggam f64 3
10828+fn ggad1 i ulong p ulong to void
10829+w0 = ogrg add p i
10830+c = owg i
10831+u = sub c w0
10832+g = sub ocsg i f64 f32 w0
10833+d = fabs sub g u
10834+u = fabs u
10835+e = sub d mul fabs c 0.000000059604644775390625f64
10836+if gt d get ggam 0
10837+put ggam 0 d
10838+end
10839+if gt u get ggam 1
10840+put ggam 1 u
10841+end
10842+if gt e get ggam 2
10843+put ggam 2 e
10844+end
10845+end
10846+
10847+fn ggadc q ulong p ulong to void
10848+i0 = mul q OCH
10849+for j ulong sub oce q p i0
10850+ggad1 add i0 j p
10851+end
10852+end
10853+
10854+note gacmp CK DATA S: one step from CK on batch S with the chip gradient, Adam on the CPU (oadam, f64)
10855+note and on the chip (ggadam, f32); prints `GA P maxd_e12 maxu_e12 rel_e9 resid_e9 tol_e9 adam_ns
10856+note loss_micro` and GACMP PASS (resid = max(|d| - 2^-24 |c|) / max |u| < tol = 2^-16) or GACMP FAIL
10857+note (exit 46)
10858+fn ggacmp to void
10859+if ne oargc 5
10860+exit 2
10861+end
10862+ockload 2
10863+odopen 3
10864+obatch oargu 4
10865+put og 10 0
10866+p = get og 8
10867+ggopen
10868+ggupst
10869+lg = ggpass true
10870+for q ulong ocn p
10871+ggkw q p
10872+end
10873+oadam
10874+t0 = monons
10875+ggadam
10876+t1 = monons
10877+for q2 ulong ocn p
10878+ggdw q2 p
10879+end
10880+for k ulong 3
10881+put ggam k 0.0f64
10882+end
10883+for q3 ulong ocn p
10884+ggadc q3 p
10885+end
10886+mu = get ggam 1
10887+rel = div get ggam 0 mu
10888+res = div get ggam 2 mu
10889+tol = div 1.0f64 65536.0f64
10890+oputc 71
10891+oputc 65
10892+oputc 32
10893+oputu p
10894+oputc 32
10895+oputl rmic mul get ggam 0 1000000.0f64
10896+oputc 32
10897+oputl rmic mul mu 1000000.0f64
10898+oputc 32
10899+oputl rmic mul rel 1000.0f64
10900+oputc 32
10901+oputl rmic mul res 1000.0f64
10902+oputc 32
10903+oputl rmic mul tol 1000.0f64
10904+oputc 32
10905+oputl sub t1 t0
10906+oputc 32
10907+oputl rmic lg
10908+oputc 10
10909+r = nvstop
10910+oputc 71
10911+oputc 65
10912+oputc 67
10913+oputc 77
10914+oputc 80
10915+oputc 32
10916+if lt res tol
10917+oputc 80
10918+oputc 65
10919+oputc 83
10920+oputc 83
10921+oputc 10
10922+oflush
10923+ret
10924+end
10925+oputc 70
10926+oputc 65
10927+oputc 73
10928+oputc 76
10929+oputc 10
10930+oflush
10931+exit 46
10932+end
10933+
10934+note gtrainf CK TRAIN UNTIL EVERY: gtrain with the parameters and Adam state resident on the chip in
10935+note f32 (ggadam); only the batch goes up each step. At save points the state comes back to f64 for
10936+note ocksave and `T s+1 loss_micro` is printed; GT at the end has the last step's phase times.
10937+fn ggtrainf to void
10938+if ne oargc 6
10939+exit 2
10940+end
10941+ockload 2
10942+odopen 3
10943+un = oargu 4
10944+ev = oargu 5
10945+if eq ev 0
10946+exit 2
10947+end
10948+en = get og 42
10949+if lt un en
10950+en = un
10951+end
10952+ggopen
10953+ggupst
10954+while lt get og 41 en bound 999999
10955+s = get og 41
10956+obatch s
10957+put og 10 0
10958+ls = ggpass false
10959+t = monons
10960+ggadam
10961+put ggtm 5 sub monons t
10962+s1 = add s 1
10963+put og 41 s1
10964+if or eq rem s1 ev 0 eq s1 en
10965+ggdnst
10966+ocksave 2
10967+oputc 84
10968+oputc 32
10969+oputu s1
10970+oputc 32
10971+oputl rmic ls
10972+oputc 10
10973+oflush
10974+end
10975+end
10976+ggtms
10977+oflush
10978+r = nvstop
10979+end
10980+
10981+note gprof CK TRAIN N: N gtrainf steps from CK, nothing saved. The last step runs with the launch
10982+note profile on (gk.om gkpf), then 64 ZERO launches of 64 floats measure the bare launch round trip.
10983+note Prints `T steps loss_micro`, one `GP id gx gy gz m n k wait_ns` per profiled launch, then GT.
10984+fn ggprof to void
10985+if ne oargc 5
10986+exit 2
10987+end
10988+ockload 2
10989+odopen 3
10990+nn = oargu 4
10991+if eq nn 0
10992+exit 2
10993+end
10994+ggopen
10995+ggupst
10996+ls = 0.0f64
10997+for i ulong nn
10998+s = get og 41
10999+obatch s
11000+put og 10 0
11001+if eq add i 1 nn
11002+put gkpf 0 1
11003+end
11004+ls = ggpass false
11005+t = monons
11006+ggadam
11007+put ggtm 5 sub monons t
11008+put og 41 add s 1
11009+end
11010+for i ulong 64
11011+ggew 20 64 ggg 0 ggg 0
11012+end
11013+put gkpf 0 0
11014+oputc 84
11015+oputc 32
11016+oputu get og 41
11017+oputc 32
11018+oputl rmic ls
11019+oputc 10
11020+for q ulong get gkpc 0
11021+o = mul q 8
11022+oputc 71
11023+oputc 80
11024+for j ulong 8
11025+oputc 32
11026+oputu get gkpl add o j
11027+end
11028+oputc 10
11029+end
11030+ggtms
11031+oflush
11032+r = nvstop
11033+end
11034+
11035+note geval CK HELD W [MERGES]: eval (cli.om cmdeval) with the forward pass on the chip in f32. Windows
11036+note w < W at offsets w T, B per forward (og 6); the last set pads its spare rows with window 0 and
11037+note does not count them. Each window adds its mean row loss in window order and, when V > 256, the
11038+note decoded length of its T targets, as cmdeval does. Prints `E step W loss_micro bpb_micro` like
11039+note eval, then `GE gpu_ns` (upload, forwards and loss reads). Exits 2 and 23 as eval.
11040+global ggev f64 1
11041+global ggen ulong 1
11042+fn ggeval to void
11043+ww = ggevw
11044+t = monons
11045+ggopen
11046+ggupw
11047+put ggev 0 0.0f64
11048+put ggen 0 0
11049+bb = get og 6
11050+nc = div add ww sub bb 1 bb
11051+for c ulong nc
11052+ggevc c ww
11053+end
11054+ns = sub monons t
11055+ggevpr ww
11056+oputc 71
11057+oputc 69
11058+oputc 32
11059+oputl ns
11060+oputc 10
11061+oflush
11062+r = nvstop
11063+end
11064+
11065+note eval's argument checks; loads CK, MERGES and HELD and returns W
11066+fn ggevw to ulong
11067+na = oargc
11068+if and ne na 5 ne na 6
11069+exit 2
11070+end
11071+ockload 2
11072+ww = oargu 4
11073+v = get og 0
11074+if gt v 256
11075+if eq na 5
11076+exit 2
11077+end
11078+obload 5 v
11079+end
11080+odopen 3
11081+tt = get og 1
11082+m = get odn 0
11083+if eq ww 0
11084+exit 23
11085+end
11086+if gt ww div sub m 1 tt
11087+exit 23
11088+end
11089+ret ww
11090+end
11091+
11092+note window set c: rows b < B hold window c B + b (window 0 past W), one chip forward, then the sums
11093+fn ggevc c ulong ww ulong to void
11094+bb = get og 6
11095+tt = get og 1
11096+for b ulong bb
11097+w = add mul c bb b
11098+if ge w ww
11099+w = 0
11100+end
11101+obrow b mul w tt
11102+end
11103+ggupb
11104+ggfwd
11105+for b ulong bb
11106+w = add mul c bb b
11107+if lt w ww
11108+ggevr b
11109+end
11110+end
11111+end
11112+
11113+note add row b's mean loss (CE stats left by ggfwd) and, when V > 256, its decoded target bytes
11114+fn ggevr b ulong to void
11115+tt = get og 1
11116+o = mul b tt
11117+s = 0.0f64
11118+for t ulong tt
11119+s = add s ggrl add o t
11120+end
11121+put ggev 0 add get ggev 0 div s f64 tt
11122+if gt get og 0 256
11123+n = get ggen 0
11124+for t ulong tt
11125+n = add n obtlen get oby add o t
11126+end
11127+put ggen 0 n
11128+end
11129+end
11130+
11131+note the E line of cmdeval from the sums
11132+fn ggevpr ww ulong to void
11133+lo = div get ggev 0 f64 ww
11134+bp = div lo 0.6931471805599453
11135+if gt get og 0 256
11136+tt = get og 1
11137+bp = div mul bp f64 mul ww tt f64 get ggen 0
11138+end
11139+oputc 69
11140+oputc 32
11141+oputu get og 41
11142+oputc 32
11143+oputu ww
11144+oputc 32
11145+oputl rmic lo
11146+oputc 32
11147+oputl rmic bp
11148+oputc 10
11149+end
11150+
11151+note gdiag CK DATA S: the CE kernel's per-row stats (max m, sum of exp(l - m), logit l_y) on the chip
11152+note in f32 against the CPU's f64 values for the same forward of batch S. Prints R, then the mean
11153+note over rows times 1e9 of: d m, d l_y, d log sum, d loss, (sum of the chip's p) - 1, d (l_y - m).
11154+note It located the CE sum bias that the Kahan sum in gr.om grce removes.
11155+global gdacc f64 6
11156+global gdcpu f64 3
11157+fn ggdiag to void
11158+if ne oargc 5
11159+exit 2
11160+end
11161+ockload 2
11162+odopen 3
11163+obatch oargu 4
11164+put og 10 0
11165+ggopen
11166+ggupw
11167+ggupb
11168+ggfwd
11169+for i ulong 6
11170+put gdacc i 0.0f64
11171+end
11172+for b ulong get og 6
11173+x = ofwd b
11174+for t ulong get og 1
11175+ggdrow b t
11176+end
11177+end
11178+n = f64 ggr
11179+oputu ggr
11180+for i ulong 6
11181+oputc 32
11182+oputl rmic mul div get gdacc i n 1000.0f64
11183+end
11184+oputc 10
11185+oflush
11186+r = nvstop
11187+end
11188+
11189+fn ggdrow b ulong t ulong to void
11190+r = add mul b get og 1 t
11191+ggdcpu t get oby r
11192+ggdacc r
11193+put gdacc 4 add get gdacc 4 sub ggdps r 1.0f64
11194+end
11195+
11196+note CPU f64 stats of row t of the last ofwd: max logit, log sum exp(l - max), logit of y
11197+fn ggdcpu t ulong y ulong to void
11198+d = get og 2
11199+v = get og 0
11200+pr = add get og 18 mul t v
11201+klogit add get og 17 mul t d pr v d
11202+m = bitcast f64 ulong 18442240474082181120
11203+for j ulong v
11204+m = fmax m get oa add pr j
11205+end
11206+s = 0.0f64
11207+for k ulong v
11208+s = add s fexp sub get oa add pr k m
11209+end
11210+put gdcpu 0 m
11211+put gdcpu 1 flog s
11212+put gdcpu 2 get oa add pr y
11213+end
11214+
11215+fn ggdacc r ulong to void
11216+o = add get gga 12 mul r 12
11217+mg = sub ggf o get gdcpu 0
11218+ls = sub flog ggf add o 4 get gdcpu 1
11219+lg = sub ggf add o 8 get gdcpu 2
11220+put gdacc 0 add get gdacc 0 mg
11221+put gdacc 1 add get gdacc 1 lg
11222+put gdacc 2 add get gdacc 2 ls
11223+put gdacc 3 add get gdacc 3 sub add ls mg lg
11224+put gdacc 5 add get gdacc 5 sub lg mg
11225+end
11226+
11227+note sum of the chip's probabilities of row r, recovered from the CE output L = (p - [j = y]) sc
11228+fn ggdps r ulong to f64
11229+v = get og 0
11230+sc = div 1.0f64 f64 ggr
11231+o = add get gga 11 mul mul r v 4
11232+s = 0.0f64
11233+for j ulong v
11234+s = add s div ggf add o mul j 4 sc
11235+end
11236+ret add s 1.0f64
11237+end
11238+
11239+note gb10 platform main: gcmp, gcmp2, gtrain, gtrainf, gacmp, geval, gdiag and gprof here, the BF16
11240+note commands gtrainb, gprofb, gbcmp, gbcmp2 and gbfrag in gb.om, every other command through the CPU
11241+note main (lio.om)
11242+fn trainingmain to int
11243+if lt oargc 2
11244+exit 2
11245+end
11246+w = ocmdw
11247+if eq w 1886217063
11248+ggcmp
11249+ret 0
11250+end
11251+if eq w 216634581863
11252+ggcmp2
11253+ret 0
11254+end
11255+if eq w 121398885512295
11256+ggtrain
11257+ret 0
11258+end
11259+if eq w 28831846509999207
11260+ggtrainf
11261+ret 0
11262+end
11263+if eq w 482871566695
11264+ggacmp
11265+ret 0
11266+end
11267+if eq w 465491617127
11268+ggeval
11269+ret 0
11270+end
11271+if eq w 444015928423
11272+ggdiag
11273+ret 0
11274+end
11275+if eq w 439956435047
11276+ggprof
11277+ret 0
11278+end
11279+if eq w 27705946603156583
11280+gbtrain
11281+ret 0
11282+end
11283+if eq w 108192095957095
11284+gbprof
11285+ret 0
11286+end
11287+if eq w 482871566951
11288+gbcmp
11289+ret 0
11290+end
11291+if eq w 55458452955751
11292+gbcmp2
11293+ret 0
11294+end
11295+if eq w 113668228801127
11296+gbfrag
11297+ret 0
11298+end
11299+ret ocpumain
11300+end
11301+note Experimental greedy GPU sampler; existing f32 forward kernels, one sequence.
11302+note Args CK N PROMPT MERGES. No checkpoint writes and no tool execution.
11303+fn gsforward to void
11304+d = get og 2
11305+x = ggl 0 0
11306+ggu 0 d
11307+ggpt 1 x
11308+x = ggw 0
11309+ggpt 2 x
11310+x = ggw get og 30
11311+ggpt 3 x
11312+x = gga1 2
11313+ggpt 4 x
11314+ggrun 9 10 ggcd d 64 get og 1 get og 6
11315+for l ulong get og 5
11316+ggfl l
11317+end
11318+x = gga1 8
11319+w = ggw get og 33
11320+z = gga1 10
11321+s = gga1 9
11322+ggrmsf x w z s
11323+x = gga1 11
11324+w = ggw 0
11325+ggxwt z w x get og 0 d false
11326+end
11327+fn gsnext to ulong
11328+ggupb
11329+gsforward
11330+o = add get gga 11 mul mul sub get og 9 1 get og 0 4
11331+best = ulong 0
11332+bv = ggf o
11333+for c ulong get og 0
11334+v = ggf add o mul c 4
11335+if gt v bv
11336+bv = v
11337+best = c
11338+end
11339+end
11340+ret best
11341+end
11342+fn main to int
11343+iread
11344+nn = ulong 160
11345+ockload 1
11346+put og 6 1
11347+put og 9 0
11348+ocpush 0
11349+obload 4 get og 0
11350+np = obprompt 3
11351+if ge np get og 1
11352+ret 3
11353+end
11354+for i ulong np
11355+ocpush get obtk i
11356+end
11357+ggopen
11358+ggupw
11359+for i ulong nn
11360+c = gsnext
11361+if eq c 0
11362+break
11363+end
11364+obtput c
11365+ocpush c
11366+end
11367+oflush
11368+r = nvstop
11369+ret 0
11370+end
11371+
11372+text imodel "model.ck"
11373+text imerges "merges.bin"
11374+text example "Task: Fix the error in this file and confirm its goal.\nFile: jobs/item76666.om\nGoal: calc starts with x and adds 4 exactly 3 times in a for loop. main calls calc with 70.\nState: unread\nAnswer:\n"
11375+note Native installed sampler input. No generated text is executed.
11376+global iprom uchar 4001
11377+global icount ulong 1
11378+global iflag uchar 16
11379+text idemo "--demo"
11380+text ihelp "--help"
11381+text iusage "Omega tool model, round40.\nUse: omega-tool-model < prompt.txt\nDemo: omega-tool-model --demo\nReads one compact current-state prompt and prints model text.\nNo tools are executed. No chat service is started.\nInput: 1..4000 bytes, no zero bytes, fewer than 256 tokens.\n"
11382+
11383+fn iusageout to void
11384+off = ulong 0
11385+while lt off len iusage bound 4096
11386+r = write 1 iusage off sub len iusage off
11387+if le r 0
11388+exit 70
11389+end
11390+off = add off ulong r
11391+end
11392+end
11393+
11394+fn iread to void
11395+if eq argcnt 2
11396+r = argread 1 iflag 0 len iflag
11397+if ne r 6
11398+iusageout
11399+exit 2
11400+end
11401+d = true
11402+h = true
11403+for i ulong 6
11404+if ne get iflag i get idemo i
11405+d = false
11406+end
11407+if ne get iflag i get ihelp i
11408+h = false
11409+end
11410+end
11411+if h
11412+iusageout
11413+exit 0
11414+end
11415+if not d
11416+iusageout
11417+exit 2
11418+end
11419+for i ulong len example
11420+put iprom i get example i
11421+end
11422+put icount 0 len example
11423+ret
11424+end
11425+if ne argcnt 1
11426+iusageout
11427+exit 2
11428+end
11429+n = ulong 0
11430+while lt n len iprom bound 4001
11431+r = read 0 iprom n sub len iprom n
11432+if lt r 0
11433+exit 70
11434+end
11435+if eq r 0
11436+break
11437+end
11438+n = add n ulong r
11439+end
11440+if or eq n 0 gt n 4000
11441+iusageout
11442+exit 2
11443+end
11444+for i ulong n
11445+if eq get iprom i 0
11446+exit 2
11447+end
11448+end
11449+put icount 0 n
11450+end
11451+
11452+note Preserve the sampler's internal argument-index interface, with installed paths.
11453+fn installedargs i ulong to long
11454+if eq i 1
11455+for k ulong len imodel
11456+put oarg k get imodel k
11457+end
11458+ret long len imodel
11459+end
11460+if eq i 4
11461+for k ulong len imerges
11462+put oarg k get imerges k
11463+end
11464+ret long len imerges
11465+end
11466+if eq i 3
11467+for k ulong get icount 0
11468+put oarg k get iprom k
11469+end
11470+ret long get icount 0
11471+end
11472+ret neg long 22
11473+end