Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/om/qwen-native/blocks/attention.om # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / om / qwen-native / blocks / attention.om` 202 lines ยท 4101 bytes Raw Blame History 1 note Two full layers per bank. One uint packs the BF16 K and V of the same lane. 2 global qa_cache0 uint `16777216` 3 global qa_cache1 uint `16777216` 4 global qa_cache2 uint `16777216` 5 global qa_cache3 uint `16777216` 6 global qa_cache4 uint `16777216` 7 global qa_cache5 uint `16777216` 8 global qa_cache6 uint `16777216` 9 global qa_cache7 uint `16777216` 10 11 fn qa_cache_get bank ulong idx ulong to uint 12 if eq bank 0 13 ret get qa_cache0 idx 14 end 15 if eq bank 1 16 ret get qa_cache1 idx 17 end 18 if eq bank 2 19 ret get qa_cache2 idx 20 end 21 if eq bank 3 22 ret get qa_cache3 idx 23 end 24 if eq bank 4 25 ret get qa_cache4 idx 26 end 27 if eq bank 5 28 ret get qa_cache5 idx 29 end 30 if eq bank 6 31 ret get qa_cache6 idx 32 end 33 ret get qa_cache7 idx 34 end 35 36 fn qa_cache_put bank ulong idx ulong value uint to void 37 if eq bank 0 38 put qa_cache0 idx value 39 ret 40 end 41 if eq bank 1 42 put qa_cache1 idx value 43 ret 44 end 45 if eq bank 2 46 put qa_cache2 idx value 47 ret 48 end 49 if eq bank 3 50 put qa_cache3 idx value 51 ret 52 end 53 if eq bank 4 54 put qa_cache4 idx value 55 ret 56 end 57 if eq bank 5 58 put qa_cache5 idx value 59 ret 60 end 61 if eq bank 6 62 put qa_cache6 idx value 63 ret 64 end 65 put qa_cache7 idx value 66 ret 67 end 68 69 global qa_row f32 256 70 global qa_cos f32 32 71 global qa_sin f32 32 72 global qa_score f32 8192 73 74 fn qa_angles pos ulong to void 75 for i ulong 32 76 freq = qb_exp mul f64 i -0.`50369048909244749f64` 77 angle = mul f64 pos freq 78 put qa_cos i f32 qb_sc angle true 79 put qa_sin i f32 qb_sc angle false 80 end 81 end 82 83 fn qa_prepare x view f32 14336 start ulong w view f32 512 off ulong to void 84 s = 0.0f32 85 for j ulong 256 86 v = get x add start j 87 s = add s mul v v 88 end 89 inv = div 1.0f32 fsqrt add div s 256.0f32 1.0e-6f32 90 for j ulong 256 91 v = mul get x add start j inv 92 put qa_row j mul v add 1.0f32 get w add off j 93 end 94 for i ulong 32 95 a = get qa_row i 96 b = get qa_row add i 32 97 co = get qa_cos i 98 si = get qa_sin i 99 put qa_row i sub mul a co mul b si 100 put qa_row add i 32 add mul b co mul a si 101 end 102 end 103 104 fn qa_bf x f32 to uint 105 bits = bitcast uint x 106 ret shr add bits add 32767 and shr bits 16 1 16 107 end 108 109 fn qa_row_offset slot ulong pos ulong nk ulong h ulong to ulong 110 ret mul add mul rem slot 2 32768 add mul pos nk h 256 111 end 112 113 fn qa_store bank ulong row ulong x view f32 14336 vsrc ulong to void 114 for j ulong 256 115 k = qa_bf get qa_row j 116 v = qa_bf get x add vsrc j 117 qa_cache_put bank add row j or shl k 16 v 118 end 119 end 120 121 fn qa_append x view f32 14336 w view f32 512 nh ulong nk ulong slot ulong pos ulong to void 122 for h ulong nk 123 src = add mul nh 512 mul h 256 124 qa_prepare x src w 256 125 row = qa_row_offset slot pos nk h 126 src = add src mul nk 256 127 qa_store div slot 2 row x src 128 end 129 end 130 131 fn qa_dot bank ulong row ulong to f32 132 s = 0.0f32 133 for j ulong 256 134 k = bitcast f32 and qa_cache_get bank add row j `4294901760` 135 s = add s mul get qa_row j k 136 end 137 ret div s 16.0f32 138 end 139 140 fn qa_weighted bank ulong row ulong wt f32 to void 141 for j ulong 256 142 v = bitcast f32 shl qa_cache_get bank add row j 16 143 put qa_row j add get qa_row j mul wt v 144 end 145 end 146 147 fn qa_attend bank ulong first ulong stride ulong span ulong to void 148 peak = -3.`40282346e38f32` 149 for p ulong span 150 score = qa_dot bank add first mul p stride 151 put qa_score p score 152 peak = fmax peak score 153 end 154 for j ulong 256 155 put qa_row j 0.0f32 156 end 157 den = 0.0f32 158 for p ulong span 159 wt = f32 qb_exp f64 sub get qa_score p peak 160 den = add den wt 161 row = add first mul p stride 162 qa_weighted bank row wt 163 end 164 for j ulong 256 165 put qa_row j div get qa_row j den 166 end 167 end 168 169 fn qa_gate h ulong x view f32 14336 y edit f32 6144 to void 170 for j ulong 256 171 gate = get x add add mul h 512 256 j 172 put y add mul h 256 j mul get qa_row j f32 qb_sig f64 gate 173 end 174 end 175 176 fn qa_query slot ulong h ulong nh ulong nk ulong pos ulong to void 177 first = qa_row_offset slot 0 nk div h div nh nk 178 qa_attend div slot 2 first mul nk 256 add pos 1 179 end 180 181 fn qwen_attention_step model ulong layer ulong pos ulong x view f32 14336 w view f32 512 y edit f32 6144 to void 182 qb_check model layer pos true 183 nh = ulong 24 184 nk = ulong 4 185 if eq model 35 186 nh = ulong 16 187 nk = ulong 2 188 end 189 if eq model 38 190 nk = ulong 2 191 end 192 slot = div layer 4 193 qa_angles pos 194 qa_append x w nh nk slot pos 195 for h ulong nh 196 qa_prepare x mul h 512 w 0 197 qa_query slot h nh nk pos 198 qa_gate h x y 199 end 200 put qb_model layer model 201 put qb_next layer add pos 1 202 end