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 / src / tensor / tensor_shape.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Sizes and strides are in elements. Native tensor callers check pointer |
| 3 | note arguments before supplying these views. Return tensor status as data. |
| 4 | om2 |
| 5 | const max_elems ulong 1073741824 |
| 6 | const shape_error long -3 |
| 7 | const bounds_error long -6 |
| 8 | const capacity_error long -8 |
| 9 | |
| 10 | export fn tensor_shape_size dims view ulong * elems edit ulong 1 to long |
| 11 | requires le len dims 8 |
| 12 | total = ulong 1 |
| 13 | for axis ulong len dims |
| 14 | extent = get dims axis |
| 15 | if eq extent 0 |
| 16 | ret shape_error |
| 17 | end |
| 18 | if gt extent max_elems |
| 19 | ret capacity_error |
| 20 | end |
| 21 | note Both factors are at most max_elems before multiplication. |
| 22 | total = mul total extent |
| 23 | if gt total max_elems |
| 24 | ret capacity_error |
| 25 | end |
| 26 | end |
| 27 | put elems 0 total |
| 28 | ret 0 |
| 29 | end |
| 30 | |
| 31 | export fn tensor_dense_strides dims view ulong * strides edit ulong * to void |
| 32 | requires eq len dims len strides |
| 33 | requires le len dims 8 |
| 34 | stride = ulong 1 |
| 35 | for trailing ulong len dims |
| 36 | axis = sub sub len dims 1 trailing |
| 37 | put strides axis stride |
| 38 | stride = mul stride get dims axis |
| 39 | end |
| 40 | end |
| 41 | |
| 42 | export fn tensor_view_bounds dims view ulong * strides view ulong * offset ulong storage ulong to long |
| 43 | requires eq len dims len strides |
| 44 | requires le len dims 8 |
| 45 | if ge offset storage |
| 46 | ret bounds_error |
| 47 | end |
| 48 | remaining = sub sub storage 1 offset |
| 49 | for axis ulong len dims |
| 50 | extent = get dims axis |
| 51 | if eq extent 0 |
| 52 | ret bounds_error |
| 53 | end |
| 54 | stride = get strides axis |
| 55 | if ne stride 0 |
| 56 | steps = sub extent 1 |
| 57 | if gt steps div remaining stride |
| 58 | ret bounds_error |
| 59 | end |
| 60 | remaining = sub remaining mul steps stride |
| 61 | end |
| 62 | end |
| 63 | ret 0 |
| 64 | end |
| 65 | |
| 66 | fn trailing_dim dims view ulong * trailing ulong to ulong |
| 67 | if ge trailing len dims |
| 68 | ret 1 |
| 69 | end |
| 70 | ret get dims sub sub len dims 1 trailing |
| 71 | end |
| 72 | |
| 73 | export fn tensor_broadcast_dims lhs view ulong * rhs view ulong * result_dims edit ulong * to long |
| 74 | requires le len result_dims 8 |
| 75 | requires ge len result_dims len lhs |
| 76 | requires ge len result_dims len rhs |
| 77 | for axis ulong len result_dims |
| 78 | a = trailing_dim lhs axis |
| 79 | b = trailing_dim rhs axis |
| 80 | if or eq a 0 eq b 0 |
| 81 | ret shape_error |
| 82 | end |
| 83 | if ne a b |
| 84 | if and ne a 1 ne b 1 |
| 85 | ret shape_error |
| 86 | end |
| 87 | end |
| 88 | out_axis = sub sub len result_dims 1 axis |
| 89 | if gt a b |
| 90 | put result_dims out_axis a |
| 91 | else |
| 92 | put result_dims out_axis b |
| 93 | end |
| 94 | end |
| 95 | ret 0 |
| 96 | end |
| 97 | |
| 98 | note Axes of length one may have an arbitrary stride in a contiguous view. |
| 99 | export fn tensor_is_row_major dims view ulong * strides view ulong * to ulong |
| 100 | requires eq len dims len strides |
| 101 | requires le len dims 8 |
| 102 | expected = ulong 1 |
| 103 | for trailing ulong len dims |
| 104 | axis = sub sub len dims 1 trailing |
| 105 | extent = get dims axis |
| 106 | if and ne extent 1 ne get strides axis expected |
| 107 | ret 0 |
| 108 | end |
| 109 | expected = mul expected extent |
| 110 | end |
| 111 | ret 1 |
| 112 | end |
| 113 | |
| 114 | note The tensor value domain preserves signed zero, infinities and finite |
| 115 | note payloads. Every NaN of a given dtype has a single representation. |
| 116 | export fn tensor_canonical_word dtype ulong word ulong to ulong |
| 117 | if eq dtype 1 |
| 118 | if gt and word 2147483647 2139095040 |
| 119 | ret 2143289344 |
| 120 | end |
| 121 | else |
| 122 | if eq dtype 2 |
| 123 | if gt and word 32767 31744 |
| 124 | ret 32256 |
| 125 | end |
| 126 | else |
| 127 | if gt and word 32767 32640 |
| 128 | ret 32704 |
| 129 | end |
| 130 | end |
| 131 | end |
| 132 | ret word |
| 133 | end |
| 134 | |
| 135 | fn tensor_native_word data view uchar * first ulong width ulong native_le ulong to ulong |
| 136 | word = ulong 0 |
| 137 | for i ulong width |
| 138 | j = i |
| 139 | if eq native_le 0 |
| 140 | j = sub sub width 1 i |
| 141 | end |
| 142 | word = or word shl ulong get data add first j mul i 8 |
| 143 | end |
| 144 | ret word |
| 145 | end |
| 146 | |
| 147 | fn tensor_put_word data edit uchar * first ulong width ulong value ulong to void |
| 148 | for j ulong width |
| 149 | put data add first j uchar and shr value mul j 8 255 |
| 150 | end |
| 151 | end |
| 152 | |
| 153 | note Serialize a chunk of gathered elements in canonical little-endian order. |
| 154 | export fn tensor_value_bytes data edit uchar * dtype ulong native_le ulong to void |
| 155 | requires and ge dtype 1 le dtype 3 |
| 156 | requires eq rem len data 2 0 |
| 157 | requires or ne dtype 1 eq rem len data 4 0 |
| 158 | width = ulong 2 |
| 159 | if eq dtype 1 |
| 160 | width = 4 |
| 161 | end |
| 162 | for i ulong div len data width |
| 163 | first = mul i width |
| 164 | raw = tensor_native_word data first width native_le |
| 165 | value = tensor_canonical_word dtype raw |
| 166 | if or eq native_le 0 ne value raw |
| 167 | tensor_put_word data first width value |
| 168 | end |
| 169 | end |
| 170 | end |
| 171 | |
| 172 | const tstore_jrn_magic ulong 13878389200081999 |
| 173 | const tstore_rec_magic ulong 13866238871819343 |
| 174 | |
| 175 | fn tstore_get_u32 data view uchar * first ulong to ulong |
| 176 | v = ulong 0 |
| 177 | for i ulong 4 |
| 178 | v = or v shl ulong get data add first i mul i 8 |
| 179 | end |
| 180 | ret v |
| 181 | end |
| 182 | |
| 183 | fn tstore_get_u64 data view uchar * first ulong to ulong |
| 184 | v = ulong 0 |
| 185 | for i ulong 8 |
| 186 | v = or v shl ulong get data add first i mul i 8 |
| 187 | end |
| 188 | ret v |
| 189 | end |
| 190 | |
| 191 | fn tstore_hex_nibble v ulong to uchar |
| 192 | if lt v 10 |
| 193 | ret uchar add 48 v |
| 194 | end |
| 195 | ret uchar add 87 v |
| 196 | end |
| 197 | |
| 198 | export fn tstore_hex_name id view uchar 32 out_name out uchar 68 to void |
| 199 | for i ulong 32 |
| 200 | b = ulong get id i |
| 201 | put out_name mul i 2 tstore_hex_nibble shr b 4 |
| 202 | put out_name add mul i 2 1 tstore_hex_nibble and b 15 |
| 203 | end |
| 204 | put out_name 64 46 |
| 205 | put out_name 65 98 |
| 206 | put out_name 66 105 |
| 207 | put out_name 67 110 |
| 208 | end |
| 209 | |
| 210 | export fn tstore_is_payload_name name view uchar * to ulong |
| 211 | if ne len name 68 |
| 212 | ret 0 |
| 213 | end |
| 214 | if or ne get name 64 46 ne get name 65 98 |
| 215 | ret 0 |
| 216 | end |
| 217 | if or ne get name 66 105 ne get name 67 110 |
| 218 | ret 0 |
| 219 | end |
| 220 | for i ulong 64 |
| 221 | c = ulong get name i |
| 222 | if not or and ge c 48 le c 57 and ge c 97 le c 102 |
| 223 | ret 0 |
| 224 | end |
| 225 | end |
| 226 | ret 1 |
| 227 | end |
| 228 | |
| 229 | export fn tstore_encode_header cap ulong count ulong out_hdr out uchar 24 to void |
| 230 | tensor_put_word out_hdr 0 8 tstore_jrn_magic |
| 231 | tensor_put_word out_hdr 8 4 1 |
| 232 | tensor_put_word out_hdr 12 4 cap |
| 233 | tensor_put_word out_hdr 16 8 count |
| 234 | end |
| 235 | |
| 236 | note Return 1 if valid, 2 if capacity mismatch, 0 if torn magic or version. |
| 237 | export fn tstore_decode_header in_hdr view uchar 24 expect_cap ulong count_out edit ulong 1 to ulong |
| 238 | if ne tstore_get_u64 in_hdr 0 tstore_jrn_magic |
| 239 | ret 0 |
| 240 | end |
| 241 | if ne tstore_get_u32 in_hdr 8 1 |
| 242 | ret 0 |
| 243 | end |
| 244 | if ne tstore_get_u32 in_hdr 12 expect_cap |
| 245 | ret 2 |
| 246 | end |
| 247 | put count_out 0 tstore_get_u64 in_hdr 16 |
| 248 | ret 1 |
| 249 | end |
| 250 | |
| 251 | export fn tstore_encode_record meta view ulong 6 shape view ulong 8 value_id view uchar 32 payload_sha view uchar 32 out_rec out uchar 168 to void |
| 252 | tensor_put_word out_rec 0 8 tstore_rec_magic |
| 253 | tensor_put_word out_rec 8 4 get meta 0 |
| 254 | tensor_put_word out_rec 12 4 get meta 1 |
| 255 | tensor_put_word out_rec 16 8 get meta 2 |
| 256 | tensor_put_word out_rec 24 8 get meta 3 |
| 257 | tensor_put_word out_rec 32 4 get meta 4 |
| 258 | tensor_put_word out_rec 36 4 get meta 5 |
| 259 | for d ulong 8 |
| 260 | tensor_put_word out_rec add 40 mul d 8 8 get shape d |
| 261 | end |
| 262 | for i ulong 32 |
| 263 | put out_rec add 104 i get value_id i |
| 264 | put out_rec add 136 i get payload_sha i |
| 265 | end |
| 266 | end |
| 267 | |
| 268 | export fn tstore_decode_record in_rec view uchar 168 meta edit ulong 6 shape edit ulong 8 value_id out uchar 32 payload_sha out uchar 32 to ulong |
| 269 | if ne tstore_get_u64 in_rec 0 tstore_rec_magic |
| 270 | ret 0 |
| 271 | end |
| 272 | put meta 0 tstore_get_u32 in_rec 8 |
| 273 | put meta 1 tstore_get_u32 in_rec 12 |
| 274 | put meta 2 tstore_get_u64 in_rec 16 |
| 275 | put meta 3 tstore_get_u64 in_rec 24 |
| 276 | put meta 4 tstore_get_u32 in_rec 32 |
| 277 | put meta 5 tstore_get_u32 in_rec 36 |
| 278 | for d ulong 8 |
| 279 | put shape d tstore_get_u64 in_rec add 40 mul d 8 |
| 280 | end |
| 281 | for i ulong 32 |
| 282 | put value_id i get in_rec add 104 i |
| 283 | put payload_sha i get in_rec add 136 i |
| 284 | end |
| 285 | ret 1 |
| 286 | end |