interplane.Get Omega
Read in your language

omega-mv

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

Brought by drakestapleton

branch main Branches Tags History

omega-mv / om / gpu / q38_plan.om

186 lines · 4167 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/gpu/q38_plan.om: Pure OM-2 Qwen3.8-27B GPU multi-head batching, overlap, exact-head,
3note attn256 L2-cache, and QMMA prefill tile planner.
4note Output record layout in out[384] (up to 96 ops x 4 uint words):
5note out[4*k + 0] = op, out[4*k + 1] = mark_independent, out[4*k + 2] = arg0, out[4*k + 3] = arg1
6om2
7
8const OP_NORM_R ulong 1
9const OP_PERM_XQ ulong 2
10const OP_AB_ROWS ulong 3
11const OP_MIX_GEMM ulong 4
12const OP_GDN ulong 5
13const OP_PERM_O_ONE ulong 6
14const OP_GATHER_R ulong 7
15const OP_ATTN ulong 8
16const OP_GATE_ONE ulong 9
17const OP_PERM_O_ALL ulong 10
18const OP_O_GEMM ulong 11
19const OP_ADDNORM_POST ulong 12
20const OP_GU_GEMM ulong 13
21const OP_SWIGLU_R ulong 14
22const OP_PERM_ACT ulong 15
23const OP_DOWN_GEMM ulong 16
24const OP_ADDNORM_END ulong 17
25
26const FOP_XS_ALL ulong 101
27const FOP_PERM_XH ulong 102
28const FOP_GEMM_SCREEN ulong 103
29const FOP_XB ulong 104
30const FOP_TAU_AM0 ulong 105
31const FOP_TAU_AM1 ulong 106
32const FOP_BF16_MASK ulong 107
33const FOP_BF16_XBATCH ulong 108
34const FOP_LOG_AM0 ulong 109
35const FOP_LOG_AM1 ulong 110
36const FOP_HEAD_FINAL ulong 111
37
38fn emit_op dst edit uint 384 idx ulong op ulong in_f ulong p0 ulong p1 ulong to ulong
39if ge idx 96
40ret 96
41end
42b = mul idx 4
43put dst b uint op
44put dst add b 1 uint in_f
45put dst add b 2 uint p0
46put dst add b 3 uint p1
47ret add idx 1
48end
49
50fn emit_heads dst edit uint 384 k ulong op ulong n ulong to ulong
51for h ulong n bound 8
52inf = ulong 0
53if gt h 0
54inf = ulong 1
55end
56k = emit_op dst k op inf h 1
57end
58ret k
59end
60
61note cfg: 0=N_heads (1..8), 1=is_attn, 2=overlap_ab, 3=need_in_norm
62export fn q38_plan_layer cfg view ulong 8 dst edit uint 384 to ulong
63n = get cfg 0
64if eq n 0
65ret 0
66end
67if gt n 8
68ret 0
69end
70is_attn = get cfg 1
71ov_ab = get cfg 2
72need_norm = get cfg 3
73k = ulong 0
74if ne need_norm 0
75k = emit_op dst k OP_NORM_R 0 0 n
76end
77k = emit_op dst k OP_PERM_XQ 0 0 n
78if eq is_attn 0
79k = emit_op dst k OP_AB_ROWS 0 0 n
80k = emit_op dst k OP_MIX_GEMM ov_ab 0 n
81k = emit_heads dst k OP_GDN n
82k = emit_heads dst k OP_PERM_O_ONE n
83else
84k = emit_op dst k OP_MIX_GEMM 0 0 n
85k = emit_op dst k OP_GATHER_R 0 0 n
86k = emit_heads dst k OP_ATTN n
87k = emit_heads dst k OP_GATE_ONE n
88k = emit_op dst k OP_PERM_O_ALL 0 0 n
89end
90k = emit_op dst k OP_O_GEMM 0 0 n
91k = emit_op dst k OP_ADDNORM_POST 0 0 n
92k = emit_op dst k OP_PERM_XQ 0 0 n
93k = emit_op dst k OP_GU_GEMM 0 0 n
94k = emit_op dst k OP_SWIGLU_R 0 0 n
95k = emit_op dst k OP_PERM_ACT 0 0 n
96k = emit_op dst k OP_DOWN_GEMM 0 0 n
97k = emit_op dst k OP_ADDNORM_END 0 0 n
98ret k
99end
100
101export fn q38_bf16_xbatch_max hd ulong max_smem ulong to ulong
102if eq hd 0
103ret 0
104end
105row_b = mul hd 4
106if lt max_smem row_b
107ret 0
108end
109xb = div max_smem row_b
110if gt xb 4
111xb = ulong 4
112end
113ret xb
114end
115
116note cfg: 0=N_heads (1..8), 1=H, 2=max_smem, 3=all_exact, 4=any_exact, 5=head8
117export fn q38_plan_final cfg view ulong 8 dst edit uint 384 to ulong
118n = get cfg 0
119if eq n 0
120ret 0
121end
122if gt n 8
123ret 0
124end
125hd = get cfg 1
126max_sm = get cfg 2
127all_ex = get cfg 3
128any_ex = get cfg 4
129head8 = get cfg 5
130k = ulong 0
131if ne all_ex 0
132if ge n 2
133k = emit_op dst k FOP_XS_ALL 0 0 n
134k = emit_op dst k FOP_PERM_XH 0 0 n
135k = emit_op dst k FOP_GEMM_SCREEN 0 0 n
136k = emit_heads dst k FOP_XB n
137k = emit_heads dst k FOP_TAU_AM0 n
138k = emit_heads dst k FOP_TAU_AM1 n
139k = emit_heads dst k FOP_BF16_MASK n
140k = emit_heads dst k FOP_LOG_AM0 n
141k = emit_heads dst k FOP_LOG_AM1 n
142ret k
143end
144end
145max_xb = q38_bf16_xbatch_max hd max_sm
146can_xb = ulong 1
147if lt max_xb 2
148can_xb = ulong 0
149end
150if ne any_ex 0
151can_xb = ulong 0
152end
153if ne head8 0
154can_xb = ulong 0
155end
156if eq can_xb 1
157h0 = ulong 0
158while lt h0 n bound 8
159xb = sub n h0
160if gt xb max_xb
161xb = max_xb
162end
163k = emit_op dst k FOP_BF16_XBATCH 0 h0 xb
164h0 = add h0 xb
165end
166k = emit_heads dst k FOP_LOG_AM0 n
167k = emit_heads dst k FOP_LOG_AM1 n
168ret k
169end
170for h ulong n bound 8
171k = emit_op dst k FOP_HEAD_FINAL 0 h 1
172end
173ret k
174end
175
176note attn256 L2 cache policy: env_mode 0=default (part/rope/norms cached), 1=also cache kv, 2=all uncached
177note Returns bitmask: bit 0 = cache part/rope/norms, bit 1 = cache kv
178export fn q38_attn256_cache_policy env_mode ulong to ulong
179if eq env_mode 2
180ret 0
181end
182if eq env_mode 1
183ret 3
184end
185ret 1
186end