CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / om / codegen / cg_moe_emit.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note Shared emit helpers for the FP8 e4m3 block-scaled GEMV builders. |
| 3 | note Scratch x: op dst src1 src2 src3 ureg imm ctrl uni p0 pnot dsub s1sub s2sub s3sub bar. |
| 4 | note Meta: 0 count, 1 nv, 2 nu, 3 pending, 4 err, 5 nloops, 6 nextv, 7 nextu. |
| 5 | const NR ulong 18446744073709551615 |
| 6 | const SBN ulong 7 |
| 7 | const SBG ulong 0 |
| 8 | const SBR ulong 1 |
| 9 | const SBU ulong 2 |
| 10 | const SBB ulong 3 |
| 11 | const CYLD ulong 8192 |
| 12 | |
| 13 | fn moe_log2 n ulong to ulong |
| 14 | r = ulong 0 |
| 15 | v = n |
| 16 | while gt v ulong 1 |
| 17 | r = add r ulong 1 |
| 18 | v = shr v ulong 1 |
| 19 | end |
| 20 | ret r |
| 21 | end |
| 22 | |
| 23 | fn moe_cw st ulong wb ulong rb ulong wt ulong yld ulong to ulong |
| 24 | w = shl st ulong 9 |
| 25 | w = or w shl wb ulong 14 |
| 26 | w = or w shl rb ulong 17 |
| 27 | w = or w shl wt ulong 20 |
| 28 | if eq yld ulong 1 |
| 29 | w = or w CYLD |
| 30 | end |
| 31 | ret w |
| 32 | end |
| 33 | |
| 34 | fn moe_alloc m edit ulong 512 kind ulong wide ulong to ulong |
| 35 | if eq wide ulong 1 |
| 36 | n = get m 7 |
| 37 | if ge n ulong 32 |
| 38 | put m 4 ulong 1 |
| 39 | ret NR |
| 40 | end |
| 41 | put m add ulong 320 n kind |
| 42 | put m 7 add n ulong 1 |
| 43 | ret n |
| 44 | end |
| 45 | n = get m 6 |
| 46 | if ge n ulong 256 |
| 47 | put m 4 ulong 1 |
| 48 | ret NR |
| 49 | end |
| 50 | put m add ulong 64 n kind |
| 51 | put m 6 add n ulong 1 |
| 52 | ret n |
| 53 | end |
| 54 | |
| 55 | fn moe_i x edit ulong 16 op ulong to void |
| 56 | for i ulong 16 |
| 57 | put x i ulong 0 |
| 58 | end |
| 59 | put x 0 op |
| 60 | for i ulong 5 |
| 61 | put x add i ulong 1 NR |
| 62 | end |
| 63 | end |
| 64 | |
| 65 | fn moe_pack b edit ulong 8192 x view ulong 16 n ulong to void |
| 66 | off = mul n ulong 8 |
| 67 | for i ulong 6 |
| 68 | put b add off i get x i |
| 69 | end |
| 70 | put b add off ulong 6 or get x ulong 6 shl get x ulong 7 ulong 32 |
| 71 | fl = or get x ulong 8 or shl get x ulong 9 ulong 1 shl get x ulong 10 ulong 2 |
| 72 | for i ulong 5 |
| 73 | fl = or fl shl get x add i ulong 11 add ulong 16 mul i ulong 8 |
| 74 | end |
| 75 | put b add off ulong 7 fl |
| 76 | end |
| 77 | |
| 78 | fn moe_emit b edit ulong 8192 m edit ulong 512 x edit ulong 16 kind ulong to void |
| 79 | if eq get m 4 ulong 1 |
| 80 | ret |
| 81 | end |
| 82 | pn = get m 3 |
| 83 | cw = ulong 0 |
| 84 | np = ulong 0 |
| 85 | if eq kind ulong 0 |
| 86 | cw = moe_cw ulong 6 SBN SBN pn ulong 0 |
| 87 | end |
| 88 | if eq kind ulong 1 |
| 89 | cw = moe_cw ulong 14 SBN SBN pn ulong 0 |
| 90 | end |
| 91 | if eq kind ulong 2 |
| 92 | cw = moe_cw ulong 4 SBG SBR pn ulong 1 |
| 93 | np = or shl ulong 1 SBG shl ulong 1 SBR |
| 94 | end |
| 95 | if eq kind ulong 3 |
| 96 | wt = and pn shl ulong 1 SBR |
| 97 | cw = moe_cw ulong 4 SBG SBR wt ulong 1 |
| 98 | np = or pn or shl ulong 1 SBG shl ulong 1 SBR |
| 99 | end |
| 100 | if eq kind ulong 4 |
| 101 | cw = moe_cw ulong 4 SBU SBN pn ulong 1 |
| 102 | np = shl ulong 1 SBU |
| 103 | end |
| 104 | if eq kind ulong 5 |
| 105 | cw = moe_cw ulong 4 SBN SBR pn ulong 1 |
| 106 | np = shl ulong 1 SBR |
| 107 | end |
| 108 | if eq kind ulong 6 |
| 109 | cw = moe_cw ulong 6 SBN SBN pn ulong 0 |
| 110 | end |
| 111 | if eq kind ulong 7 |
| 112 | cw = moe_cw ulong 5 SBN SBN pn ulong 0 |
| 113 | if eq get x ulong 9 ulong 1 |
| 114 | np = pn |
| 115 | end |
| 116 | end |
| 117 | if eq kind ulong 8 |
| 118 | cw = moe_cw ulong 5 SBN SBN pn ulong 0 |
| 119 | if eq get x ulong 9 ulong 1 |
| 120 | np = pn |
| 121 | end |
| 122 | end |
| 123 | if eq kind ulong 9 |
| 124 | cw = moe_cw ulong 1 SBN SBN pn ulong 1 |
| 125 | end |
| 126 | if eq kind ulong 10 |
| 127 | cw = moe_cw ulong 5 SBN SBN pn ulong 1 |
| 128 | end |
| 129 | if eq kind ulong 11 |
| 130 | cw = moe_cw ulong 4 SBB SBR pn ulong 1 |
| 131 | np = shl ulong 1 SBR |
| 132 | end |
| 133 | if eq kind ulong 12 |
| 134 | wt = and pn shl ulong 1 SBR |
| 135 | cw = moe_cw ulong 4 SBB SBR wt ulong 1 |
| 136 | np = shl ulong 1 SBR |
| 137 | end |
| 138 | if eq kind ulong 13 |
| 139 | cw = moe_cw ulong 6 SBN SBN or pn shl ulong 1 SBB ulong 0 |
| 140 | end |
| 141 | put x ulong 7 cw |
| 142 | put m 3 np |
| 143 | n = get m 0 |
| 144 | if ge n ulong 1024 |
| 145 | put m 4 ulong 1 |
| 146 | ret |
| 147 | end |
| 148 | moe_pack b x n |
| 149 | put m 0 add n ulong 1 |
| 150 | end |
| 151 | |
| 152 | fn moe_patch b edit ulong 8192 at ulong tgt ulong to void |
| 153 | off = add mul at ulong 8 ulong 6 |
| 154 | imm = and sub tgt at ulong 4294967295 |
| 155 | ctrl = shr get b off ulong 32 |
| 156 | put b off or imm shl ctrl ulong 32 |
| 157 | end |
| 158 |