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