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/omega-mv/blob/main/gpu/nvidia/native/models/fp8.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / gpu / nvidia / native / models / fp8.om` 165 lines ยท 3429 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 2 note OCP OFP8 rev 1.0 (2023-12-01): E4M3 (bias 7) and E5M2 (bias 15) to and 3 note from exact IEEE binary32 bits. Integer bit operations only, no device access. 4 note Decode: E4M3 S.1111.111 is NaN, no infinities; E5M2 S.11111.00 is Inf, 5 note S.11111.{01,10,11} NaN. A NaN decodes to quiet binary32 NaN with its sign. 6 note Encode (spec 5.2.1): round to nearest even, then sat 1 clamps to max normal 7 note (E4M3 126 = 448, E5M2 123 = 57344) and sat 0 gives E4M3 NaN 127 or E5M2 8 note Inf 124. Infinity follows Table 3. Below half the minimum subnormal gives 9 note signed zero. A NaN source keeps its sign: E4M3 127, E5M2 126. 10 note Refusals trap 5: a code above 255, sat above 1, f32norm out of range. 11 note Wolfram: (1+6/8) 2^(15-7) -> 448; (1+3/4) 2^(30-15) -> 57344; 12 note 2^-9 -> 1/512 (E4M3 min subnormal); 2^-16 -> 1/65536 (E5M2 min subnormal). 13 note Wolfram: FromDigits hex 7FC00000 7F800000 7FFFFF 800000 7FFFFFFF -> 14 note `2143289344` `2139095040` `8388607` `8388608` `2147483647`. 15 fn f32norm n uint p long to uint 16 if eq n 0 17 trap 5 18 end 19 if gt n `16777215` 20 trap 5 21 end 22 b = long 0 23 t = shr n 1 24 while ne t 0 bound 24 25 b = add b 1 26 t = shr t 1 27 end 28 ex = add add b p 127 29 if lt ex 1 30 trap 5 31 end 32 if gt ex 254 33 trap 5 34 end 35 sh = uint sub 23 b 36 m = and shl n sh `8388607` 37 ret or shl uint ex 23 m 38 end 39 fn e4m3bits code uint to uint 40 if gt code 255 41 trap 5 42 end 43 s = shl and code 128 24 44 ex = and shr code 3 15 45 m = and code 7 46 if eq ex 15 47 if eq m 7 48 ret or s `2143289344` 49 end 50 end 51 if eq ex 0 52 if eq m 0 53 ret s 54 end 55 ret or s f32norm m -9 56 end 57 ret or s or shl add ex 120 23 shl m 20 58 end 59 fn e5m2bits code uint to uint 60 if gt code 255 61 trap 5 62 end 63 s = shl and code 128 24 64 ex = and shr code 2 31 65 m = and code 3 66 if eq ex 31 67 if eq m 0 68 ret or s `2139095040` 69 end 70 ret or s `2143289344` 71 end 72 if eq ex 0 73 if eq m 0 74 ret s 75 end 76 ret or s f32norm m -16 77 end 78 ret or s or shl add ex 112 23 shl m 21 79 end 80 note rnesh returns sig / 2^sh rounded to nearest, ties to even (1 <= sh <= 24). 81 note This is the single rounding step of every encoder in this unit. 82 fn rnesh sig uint sh uint to uint 83 q = shr sig sh 84 rm = and sig sub shl uint 1 sh 1 85 hf = shl uint 1 sub sh 1 86 if gt rm hf 87 ret add q 1 88 end 89 if eq rm hf 90 if eq and q 1 1 91 ret add q 1 92 end 93 end 94 ret q 95 end 96 note fp8mag rounds a finite binary32 magnitude a to mb mantissa bits with 97 note minimum exponent emin, nearest even, and returns the unsigned code 98 note (biased exponent << mb) + mantissa. A carry out of the mantissa moves into 99 note the exponent field by the add. Callers compare against their max code. 100 fn fp8mag a uint mb long emin long bias long to uint 101 ef = shr a 23 102 if eq ef 0 103 ret 0 104 end 105 e = sub long ef 127 106 if gt e 40 107 ret 4095 108 end 109 ue = e 110 if lt ue emin 111 ue = emin 112 end 113 sh = sub add sub ue e 23 mb 114 if gt sh 24 115 ret 0 116 end 117 q = rnesh or and a `8388607` `8388608` uint sh 118 if lt e emin 119 ret q 120 end 121 umb = uint mb 122 ret add shl uint add e bias umb sub q shl uint 1 umb 123 end 124 fn e4m3enc bits uint sat uint to uint 125 if gt sat 1 126 trap 5 127 end 128 s = and shr bits 24 128 129 a = and bits `2147483647` 130 if gt a `2139095040` 131 ret or s 127 132 end 133 c = uint 127 134 if lt a `2139095040` 135 c = fp8mag a 3 -6 7 136 end 137 if gt c 126 138 if eq sat 1 139 ret or s 126 140 end 141 ret or s 127 142 end 143 ret or s c 144 end 145 fn e5m2enc bits uint sat uint to uint 146 if gt sat 1 147 trap 5 148 end 149 s = and shr bits 24 128 150 a = and bits `2147483647` 151 if gt a `2139095040` 152 ret or s 126 153 end 154 c = uint 124 155 if lt a `2139095040` 156 c = fp8mag a 2 -14 15 157 end 158 if gt c 123 159 if eq sat 1 160 ret or s 123 161 end 162 ret or s 124 163 end 164 ret or s c 165 end