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 / engine / safetensors.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note om/gpu/engine/safetensors.om: Universal Pure Seeded-OM-2 Safetensors Tensor Registry. |
| 3 | note Dynamically discovers and indexes tensors across ALL 3 checkpoint layouts with ZERO hardcoded offsets: |
| 4 | note 1) Standard single-file HuggingFace: model.safetensors |
| 5 | note 2) Standard sharded HuggingFace: model.safetensors.index.json -> model-00001-of-*.safetensors |
| 6 | note 3) Omega per-layer packed safetensors: outside.safetensors + layers-0..L-1.safetensors |
| 7 | note Stores every tensor's (file_id, byte_offset_from_file_start, byte_len, dtype, dim0, dim1) |
| 8 | note and resolves any tensor by canonical name (auto-handling model.* vs model.language_model.*). |
| 9 | |
| 10 | const ST_MAX_FILES ulong 256 |
| 11 | const ST_MAX_TENSORS ulong 163840 |
| 12 | const ST_HASH_SZ ulong 524288 |
| 13 | const ST_HASH_MASK ulong 524287 |
| 14 | |
| 15 | const DT_BF16 ulong 0 |
| 16 | const DT_F16 ulong 1 |
| 17 | const DT_F32 ulong 2 |
| 18 | const DT_F8_E4M3 ulong 3 |
| 19 | const DT_OTHER ulong 4 |
| 20 | const DT_I64 ulong 5 |
| 21 | |
| 22 | global stFileNamePool uchar 16384 |
| 23 | global stFileNameOff ulong 256 |
| 24 | global stFileNameLen ulong 256 |
| 25 | global stFileDataOff ulong 256 |
| 26 | global stFileCount ulong 1 |
| 27 | global stLayoutMode ulong 1 |
| 28 | |
| 29 | global stNamePool uchar 16777216 |
| 30 | global stNamePoolUsed ulong 1 |
| 31 | global stNameOff ulong 163840 |
| 32 | global stNameLen ulong 163840 |
| 33 | global stFileId ulong 163840 |
| 34 | global stByteOff ulong 163840 |
| 35 | global stByteLen ulong 163840 |
| 36 | global stDtype ulong 163840 |
| 37 | global stDim0 ulong 163840 |
| 38 | global stDim1 ulong 163840 |
| 39 | global stTensorCount ulong 1 |
| 40 | global stHashTab uint 524288 |
| 41 | |
| 42 | global stHdrBuf uchar 4194304 |
| 43 | global stIdxBuf uchar 262400 |
| 44 | global stIdxLen ulong 1 |
| 45 | global stPathBuf uchar 1024 |
| 46 | global stOpenFd int 1 |
| 47 | global stOpenFid ulong 1 |
| 48 | |
| 49 | text stSufSingle "/model.safetensors" |
| 50 | text stSufIndex "/model.safetensors.index.json" |
| 51 | text stSufOutside "/outside.safetensors" |
| 52 | text stSufLayerPre "layers-" |
| 53 | text stSufSt ".safetensors" |
| 54 | text stKeyDtype "\"dtype\"" |
| 55 | text stKeyShape "\"shape\"" |
| 56 | text stKeyOffs "\"data_offsets\"" |
| 57 | text stKeyMeta "__metadata__" |
| 58 | text stKeyWMap "\"weight_map\"" |
| 59 | text stPrefLang "model.language_model." |
| 60 | text stPrefPlain "model." |
| 61 | text stLayPre "layers." |
| 62 | |
| 63 | fn stAddFile name view uchar * nlen ulong to ulong |
| 64 | cnt = get stFileCount 0 |
| 65 | for i ulong cnt bound 256 |
| 66 | if eq get stFileNameLen i nlen |
| 67 | off = get stFileNameOff i |
| 68 | same = true |
| 69 | for j ulong nlen bound 128 |
| 70 | if ne get stFileNamePool add off j get name j |
| 71 | same = false |
| 72 | break |
| 73 | end |
| 74 | end |
| 75 | if same |
| 76 | ret i |
| 77 | end |
| 78 | end |
| 79 | end |
| 80 | if ge cnt ST_MAX_FILES |
| 81 | ret ST_MAX_FILES |
| 82 | end |
| 83 | off = ulong 0 |
| 84 | if gt cnt 0 |
| 85 | off = add get stFileNameOff sub cnt 1 get stFileNameLen sub cnt 1 |
| 86 | end |
| 87 | if gt add off nlen 16300 |
| 88 | ret ST_MAX_FILES |
| 89 | end |
| 90 | for j ulong nlen bound 128 |
| 91 | put stFileNamePool add off j get name j |
| 92 | end |
| 93 | put stFileNameOff cnt off |
| 94 | put stFileNameLen cnt nlen |
| 95 | put stFileCount 0 add cnt 1 |
| 96 | ret cnt |
| 97 | end |
| 98 | |
| 99 | fn stBuildFilePath fid ulong to ulong |
| 100 | dlen = get engModelDirLen 0 |
| 101 | for i ulong dlen bound 512 |
| 102 | put stPathBuf i get engModelDir i |
| 103 | end |
| 104 | p = dlen |
| 105 | put stPathBuf p 47 |
| 106 | p = add p 1 |
| 107 | off = get stFileNameOff fid |
| 108 | nlen = get stFileNameLen fid |
| 109 | for i ulong nlen bound 128 |
| 110 | put stPathBuf p get stFileNamePool add off i |
| 111 | p = add p 1 |
| 112 | end |
| 113 | put stPathBuf p 0 |
| 114 | ret p |
| 115 | end |
| 116 | |
| 117 | fn stOpenFile fid ulong to int |
| 118 | ofd = get stOpenFd 0 |
| 119 | if and ge ofd int 0 eq get stOpenFid 0 fid |
| 120 | ret ofd |
| 121 | end |
| 122 | if ge ofd int 0 |
| 123 | c0 = fclose ofd |
| 124 | put stOpenFd 0 neg 1 |
| 125 | end |
| 126 | plen = stBuildFilePath fid |
| 127 | fd = int fopen stPathBuf 0 plen 0 0 |
| 128 | if ge fd int 0 |
| 129 | put stOpenFd 0 fd |
| 130 | put stOpenFid 0 fid |
| 131 | end |
| 132 | ret fd |
| 133 | end |
| 134 | |
| 135 | fn stCloseCached to void |
| 136 | ofd = get stOpenFd 0 |
| 137 | if ge ofd int 0 |
| 138 | c0 = fclose ofd |
| 139 | put stOpenFd 0 neg 1 |
| 140 | end |
| 141 | end |
| 142 | |
| 143 | fn stParseU64Hdr pos ulong lim ulong to ulong |
| 144 | v = ulong 0 |
| 145 | p = pos |
| 146 | while lt p lim bound 32 |
| 147 | c = get stHdrBuf p |
| 148 | if and ge c 48 le c 57 |
| 149 | v = add mul v 10 ulong sub c 48 |
| 150 | p = add p 1 |
| 151 | else |
| 152 | break |
| 153 | end |
| 154 | end |
| 155 | ret v |
| 156 | end |
| 157 | |
| 158 | fn stSkipWsHdr pos ulong lim ulong to ulong |
| 159 | p = pos |
| 160 | while lt p lim bound 128 |
| 161 | c = get stHdrBuf p |
| 162 | if or eq c 32 or eq c 9 or eq c 10 eq c 13 |
| 163 | p = add p 1 |
| 164 | else |
| 165 | break |
| 166 | end |
| 167 | end |
| 168 | ret p |
| 169 | end |
| 170 | |
| 171 | fn stSkipDigitsHdr pos ulong lim ulong to ulong |
| 172 | p = pos |
| 173 | while lt p lim bound 32 |
| 174 | c = get stHdrBuf p |
| 175 | if and ge c 48 le c 57 |
| 176 | p = add p 1 |
| 177 | else |
| 178 | break |
| 179 | end |
| 180 | end |
| 181 | ret p |
| 182 | end |
| 183 | |
| 184 | fn stSkipSepHdr pos ulong lim ulong to ulong |
| 185 | p = pos |
| 186 | while lt p lim bound 16 |
| 187 | c = get stHdrBuf p |
| 188 | if or eq c 44 eq c 32 |
| 189 | p = add p 1 |
| 190 | else |
| 191 | break |
| 192 | end |
| 193 | end |
| 194 | ret p |
| 195 | end |
| 196 | |
| 197 | fn stParseTensorMeta tid ulong oStart ulong oEnd ulong dataOff ulong to void |
| 198 | put stDtype tid DT_OTHER |
| 199 | put stDim0 tid 0 |
| 200 | put stDim1 tid 0 |
| 201 | put stByteOff tid dataOff |
| 202 | put stByteLen tid 0 |
| 203 | q = oStart |
| 204 | while lt add q 14 oEnd bound 2048 |
| 205 | if eq get stHdrBuf q 34 |
| 206 | c1 = get stHdrBuf add q 1 |
| 207 | c2 = get stHdrBuf add q 2 |
| 208 | if and eq c1 100 eq c2 116 |
| 209 | k = add q 7 |
| 210 | while and lt k oEnd ne get stHdrBuf k 34 bound 16 |
| 211 | k = add k 1 |
| 212 | end |
| 213 | k = add k 1 |
| 214 | if lt add k 2 oEnd |
| 215 | x0 = get stHdrBuf k |
| 216 | x1 = get stHdrBuf add k 1 |
| 217 | if and eq x0 66 eq x1 70 |
| 218 | put stDtype tid DT_BF16 |
| 219 | else |
| 220 | if and eq x0 70 eq x1 56 |
| 221 | put stDtype tid DT_F8_E4M3 |
| 222 | else |
| 223 | if and eq x0 70 eq x1 49 |
| 224 | put stDtype tid DT_F16 |
| 225 | else |
| 226 | if and eq x0 70 eq x1 51 |
| 227 | put stDtype tid DT_F32 |
| 228 | else |
| 229 | if and eq x0 73 eq x1 54 |
| 230 | put stDtype tid DT_I64 |
| 231 | end |
| 232 | end |
| 233 | end |
| 234 | end |
| 235 | end |
| 236 | end |
| 237 | end |
| 238 | if and eq c1 115 eq c2 104 |
| 239 | k = add q 7 |
| 240 | while and lt k oEnd ne get stHdrBuf k 91 bound 16 |
| 241 | k = add k 1 |
| 242 | end |
| 243 | k = stSkipWsHdr add k 1 oEnd |
| 244 | put stDim0 tid stParseU64Hdr k oEnd |
| 245 | k = stSkipSepHdr stSkipDigitsHdr k oEnd oEnd |
| 246 | if lt k oEnd |
| 247 | put stDim1 tid stParseU64Hdr k oEnd |
| 248 | end |
| 249 | end |
| 250 | if and eq c1 100 eq c2 97 |
| 251 | k = add q 14 |
| 252 | while and lt k oEnd ne get stHdrBuf k 91 bound 16 |
| 253 | k = add k 1 |
| 254 | end |
| 255 | k = stSkipWsHdr add k 1 oEnd |
| 256 | b0 = stParseU64Hdr k oEnd |
| 257 | k = stSkipSepHdr stSkipDigitsHdr k oEnd oEnd |
| 258 | b1 = stParseU64Hdr k oEnd |
| 259 | put stByteOff tid add dataOff b0 |
| 260 | put stByteLen tid sub b1 b0 |
| 261 | end |
| 262 | end |
| 263 | q = add q 1 |
| 264 | end |
| 265 | end |
| 266 | |
| 267 | fn stCopyName used ulong nStart ulong nLen ulong to void |
| 268 | for k ulong nLen bound 256 |
| 269 | put stNamePool add used k get stHdrBuf add nStart k |
| 270 | end |
| 271 | end |
| 272 | |
| 273 | fn stHashHdr nStart ulong nLen ulong to ulong |
| 274 | h = ulong 14695981039346656037 |
| 275 | for k ulong nLen bound 256 |
| 276 | h = mul xor h ulong get stHdrBuf add nStart k 1099511628211 |
| 277 | end |
| 278 | ret and h ST_HASH_MASK |
| 279 | end |
| 280 | |
| 281 | fn stHashView name view uchar * nlen ulong to ulong |
| 282 | h = ulong 14695981039346656037 |
| 283 | for k ulong nlen bound 256 |
| 284 | h = mul xor h ulong get name k 1099511628211 |
| 285 | end |
| 286 | ret and h ST_HASH_MASK |
| 287 | end |
| 288 | |
| 289 | fn stHashInsert tid ulong slot0 ulong to void |
| 290 | slot = slot0 |
| 291 | for probe ulong 1024 |
| 292 | if eq get stHashTab slot 0 |
| 293 | put stHashTab slot uint add tid 1 |
| 294 | ret |
| 295 | end |
| 296 | slot = and add slot 1 ST_HASH_MASK |
| 297 | end |
| 298 | end |
| 299 | |
| 300 | fn stRecordTensor fid ulong dataOff ulong nStart ulong nLen ulong oStart ulong oEnd ulong to void |
| 301 | if and eq nLen 12 eq get stHdrBuf nStart 95 |
| 302 | ret |
| 303 | end |
| 304 | if ge nLen 13 |
| 305 | if eq get stHdrBuf add nStart 6 118 |
| 306 | if eq get stHdrBuf add nStart 7 105 |
| 307 | ret |
| 308 | end |
| 309 | end |
| 310 | end |
| 311 | if ge nLen 4 |
| 312 | if eq get stHdrBuf nStart 109 |
| 313 | if eq get stHdrBuf add nStart 1 116 |
| 314 | if eq get stHdrBuf add nStart 2 112 |
| 315 | if eq get stHdrBuf add nStart 3 46 |
| 316 | ret |
| 317 | end |
| 318 | end |
| 319 | end |
| 320 | end |
| 321 | end |
| 322 | tid = get stTensorCount 0 |
| 323 | used = get stNamePoolUsed 0 |
| 324 | if or ge tid ST_MAX_TENSORS ge add used nLen 16770000 |
| 325 | ret |
| 326 | end |
| 327 | stCopyName used nStart nLen |
| 328 | put stNameOff tid used |
| 329 | put stNameLen tid nLen |
| 330 | put stNamePoolUsed 0 add used nLen |
| 331 | put stFileId tid fid |
| 332 | stParseTensorMeta tid oStart oEnd dataOff |
| 333 | stHashInsert tid stHashHdr nStart nLen |
| 334 | put stTensorCount 0 add tid 1 |
| 335 | end |
| 336 | |
| 337 | fn stSkipObj oStart ulong hlen ulong to ulong |
| 338 | depth = ulong 1 |
| 339 | p = add oStart 1 |
| 340 | while and lt p hlen gt depth 0 bound 4096 |
| 341 | ch = get stHdrBuf p |
| 342 | if eq ch 123 |
| 343 | depth = add depth 1 |
| 344 | else |
| 345 | if eq ch 125 |
| 346 | depth = sub depth 1 |
| 347 | end |
| 348 | end |
| 349 | p = add p 1 |
| 350 | end |
| 351 | ret p |
| 352 | end |
| 353 | |
| 354 | fn stIndexOneFile fid ulong to bool |
| 355 | fd = stOpenFile fid |
| 356 | if lt fd int 0 |
| 357 | ret false |
| 358 | end |
| 359 | hdr8 = new uchar 8 0 |
| 360 | if ne pread fd hdr8 0 8 0 8 |
| 361 | ret false |
| 362 | end |
| 363 | hlen = ulong 0 |
| 364 | for i ulong 8 |
| 365 | hlen = or hlen shl ulong get hdr8 i mul i 8 |
| 366 | end |
| 367 | if or eq hlen 0 gt hlen 4194000 |
| 368 | ret false |
| 369 | end |
| 370 | dataOff = add hlen 8 |
| 371 | put stFileDataOff fid dataOff |
| 372 | tot = ulong 0 |
| 373 | while lt tot hlen bound 1024 |
| 374 | got = pread fd stHdrBuf tot sub hlen tot long add 8 tot |
| 375 | if le got 0 |
| 376 | ret false |
| 377 | end |
| 378 | tot = add tot ulong got |
| 379 | end |
| 380 | p = ulong 0 |
| 381 | while lt p hlen bound 4194304 |
| 382 | if eq get stHdrBuf p 34 |
| 383 | nStart = add p 1 |
| 384 | p = add p 1 |
| 385 | while and lt p hlen ne get stHdrBuf p 34 bound 256 |
| 386 | p = add p 1 |
| 387 | end |
| 388 | nLen = sub p nStart |
| 389 | p = stSkipWsHdr add p 1 hlen |
| 390 | if and lt p hlen eq get stHdrBuf p 58 |
| 391 | p = stSkipWsHdr add p 1 hlen |
| 392 | if and lt p hlen eq get stHdrBuf p 123 |
| 393 | oStart = p |
| 394 | p = stSkipObj oStart hlen |
| 395 | stRecordTensor fid dataOff nStart nLen oStart p |
| 396 | end |
| 397 | end |
| 398 | else |
| 399 | p = add p 1 |
| 400 | end |
| 401 | end |
| 402 | ret true |
| 403 | end |
| 404 | |
| 405 | fn stAddFromIdx s ulong ln ulong to void |
| 406 | fnBuf = new uchar 128 0 |
| 407 | for k ulong ln bound 128 |
| 408 | put fnBuf k get stIdxBuf add s k |
| 409 | end |
| 410 | fid = stAddFile fnBuf ln |
| 411 | end |
| 412 | |
| 413 | fn stIsStSuf p ulong to bool |
| 414 | for k ulong 12 |
| 415 | if ne get stIdxBuf add sub p 12 k get stSufSt k |
| 416 | ret false |
| 417 | end |
| 418 | end |
| 419 | ret true |
| 420 | end |
| 421 | |
| 422 | fn stScanIdxChunk tot ulong to void |
| 423 | p = ulong 0 |
| 424 | while lt p tot bound 262400 |
| 425 | if eq get stIdxBuf p 34 |
| 426 | s = add p 1 |
| 427 | p = add p 1 |
| 428 | while lt p tot bound 256 |
| 429 | if eq get stIdxBuf p 34 |
| 430 | break |
| 431 | end |
| 432 | p = add p 1 |
| 433 | end |
| 434 | if lt p tot |
| 435 | ln = sub p s |
| 436 | if and gt ln 12 lt ln 120 |
| 437 | if stIsStSuf p |
| 438 | stAddFromIdx s ln |
| 439 | end |
| 440 | end |
| 441 | end |
| 442 | end |
| 443 | p = add p 1 |
| 444 | end |
| 445 | end |
| 446 | |
| 447 | fn stFindLastNl tot ulong to ulong |
| 448 | k = tot |
| 449 | while gt k 0 bound 512 |
| 450 | k = sub k 1 |
| 451 | if eq get stIdxBuf k 10 |
| 452 | ret add k 1 |
| 453 | end |
| 454 | end |
| 455 | ret tot |
| 456 | end |
| 457 | |
| 458 | fn stShiftCarry cut ulong tot ulong to ulong |
| 459 | carry = sub tot cut |
| 460 | for j ulong carry bound 512 |
| 461 | put stIdxBuf j get stIdxBuf add cut j |
| 462 | end |
| 463 | ret carry |
| 464 | end |
| 465 | |
| 466 | fn stDiscoverFromIndex to bool |
| 467 | plen = engJoinPath stSufIndex |
| 468 | fd = int fopen engPathTmp 0 plen 0 0 |
| 469 | if lt fd int 0 |
| 470 | ret false |
| 471 | end |
| 472 | foff = ulong 0 |
| 473 | carry = ulong 0 |
| 474 | while true bound 512 |
| 475 | got = pread fd stIdxBuf carry sub 262144 carry long foff |
| 476 | if le got 0 |
| 477 | break |
| 478 | end |
| 479 | tot = add carry ulong got |
| 480 | cut = stFindLastNl tot |
| 481 | stScanIdxChunk cut |
| 482 | foff = add foff ulong got |
| 483 | carry = stShiftCarry cut tot |
| 484 | end |
| 485 | if gt carry 0 |
| 486 | stScanIdxChunk carry |
| 487 | end |
| 488 | c0 = fclose fd |
| 489 | ret gt get stFileCount 0 0 |
| 490 | end |
| 491 | |
| 492 | fn stBuildLayerFilename L ulong dst edit uchar 64 to ulong |
| 493 | p = ulong 0 |
| 494 | for i ulong len stSufLayerPre bound 16 |
| 495 | put dst p get stSufLayerPre i |
| 496 | p = add p 1 |
| 497 | end |
| 498 | if ge L 100 |
| 499 | put dst p uchar add 48 div L 100 |
| 500 | p = add p 1 |
| 501 | end |
| 502 | if ge L 10 |
| 503 | put dst p uchar add 48 rem div L 10 10 |
| 504 | p = add p 1 |
| 505 | end |
| 506 | put dst p uchar add 48 rem L 10 |
| 507 | p = add p 1 |
| 508 | for i ulong len stSufSt bound 16 |
| 509 | put dst p get stSufSt i |
| 510 | p = add p 1 |
| 511 | end |
| 512 | ret p |
| 513 | end |
| 514 | |
| 515 | text stNmOutside "outside.safetensors" |
| 516 | text stNmSingle "model.safetensors" |
| 517 | |
| 518 | fn stAddOmegaLayers to void |
| 519 | lnBuf = new uchar 64 0 |
| 520 | f0 = stAddFile stNmOutside len stNmOutside |
| 521 | nL = get engSpec MS_LAYERS |
| 522 | for L ulong nL bound 256 |
| 523 | lnLen = stBuildLayerFilename L lnBuf |
| 524 | fl = stAddFile lnBuf lnLen |
| 525 | end |
| 526 | end |
| 527 | |
| 528 | fn stDiscoverLayout to bool |
| 529 | plen = engJoinPath stSufOutside |
| 530 | fd = int fopen engPathTmp 0 plen 0 0 |
| 531 | if ge fd int 0 |
| 532 | c0 = fclose fd |
| 533 | put stLayoutMode 0 2 |
| 534 | stAddOmegaLayers |
| 535 | ret true |
| 536 | end |
| 537 | if stDiscoverFromIndex |
| 538 | put stLayoutMode 0 1 |
| 539 | ret true |
| 540 | end |
| 541 | plen2 = engJoinPath stSufSingle |
| 542 | fd2 = int fopen engPathTmp 0 plen2 0 0 |
| 543 | if lt fd2 int 0 |
| 544 | ret false |
| 545 | end |
| 546 | c1 = fclose fd2 |
| 547 | put stLayoutMode 0 0 |
| 548 | f1 = stAddFile stNmSingle len stNmSingle |
| 549 | ret true |
| 550 | end |
| 551 | |
| 552 | fn stIndexModel to bool |
| 553 | put stFileCount 0 0 |
| 554 | put stTensorCount 0 0 |
| 555 | put stNamePoolUsed 0 0 |
| 556 | put stOpenFd 0 neg 1 |
| 557 | put stOpenFid 0 0 |
| 558 | for hi ulong ST_HASH_SZ |
| 559 | put stHashTab hi 0 |
| 560 | end |
| 561 | if not stDiscoverLayout |
| 562 | ret false |
| 563 | end |
| 564 | fc = get stFileCount 0 |
| 565 | for fid ulong fc bound 256 |
| 566 | if not stIndexOneFile fid |
| 567 | stCloseCached |
| 568 | ret false |
| 569 | end |
| 570 | end |
| 571 | stCloseCached |
| 572 | ret gt get stTensorCount 0 0 |
| 573 | end |
| 574 | |
| 575 | fn stFindExact name view uchar * nlen ulong to long |
| 576 | slot = stHashView name nlen |
| 577 | for probe ulong 1024 |
| 578 | ent = get stHashTab slot |
| 579 | if eq ent 0 |
| 580 | ret neg 1 |
| 581 | end |
| 582 | i = ulong sub ent 1 |
| 583 | if eq get stNameLen i nlen |
| 584 | off = get stNameOff i |
| 585 | same = true |
| 586 | for j ulong nlen bound 256 |
| 587 | if ne get stNamePool add off j get name j |
| 588 | same = false |
| 589 | break |
| 590 | end |
| 591 | end |
| 592 | if same |
| 593 | ret long i |
| 594 | end |
| 595 | end |
| 596 | slot = and add slot 1 ST_HASH_MASK |
| 597 | end |
| 598 | ret neg 1 |
| 599 | end |
| 600 | |
| 601 | fn stFindCanon suffix view uchar * slen ulong to long |
| 602 | note Tries exact suffix, then "model.language_model." + suffix, then "model." + suffix |
| 603 | r = stFindExact suffix slen |
| 604 | if ge r 0 |
| 605 | ret r |
| 606 | end |
| 607 | tmp = new uchar 256 0 |
| 608 | p = ulong 0 |
| 609 | for i ulong len stPrefLang bound 32 |
| 610 | put tmp p get stPrefLang i |
| 611 | p = add p 1 |
| 612 | end |
| 613 | for i ulong slen bound 200 |
| 614 | put tmp p get suffix i |
| 615 | p = add p 1 |
| 616 | end |
| 617 | r2 = stFindExact tmp p |
| 618 | if ge r2 0 |
| 619 | ret r2 |
| 620 | end |
| 621 | p = ulong 0 |
| 622 | for i ulong len stPrefPlain bound 16 |
| 623 | put tmp p get stPrefPlain i |
| 624 | p = add p 1 |
| 625 | end |
| 626 | for i ulong slen bound 200 |
| 627 | put tmp p get suffix i |
| 628 | p = add p 1 |
| 629 | end |
| 630 | ret stFindExact tmp p |
| 631 | end |
| 632 | |
| 633 | fn stBuildLayerKey L ulong suf view uchar * dst edit uchar 256 to ulong |
| 634 | p = ulong 0 |
| 635 | for i ulong len stLayPre bound 16 |
| 636 | put dst p get stLayPre i |
| 637 | p = add p 1 |
| 638 | end |
| 639 | if ge L 100 |
| 640 | put dst p uchar add 48 div L 100 |
| 641 | p = add p 1 |
| 642 | end |
| 643 | if ge L 10 |
| 644 | put dst p uchar add 48 rem div L 10 10 |
| 645 | p = add p 1 |
| 646 | end |
| 647 | put dst p uchar add 48 rem L 10 |
| 648 | p = add p 1 |
| 649 | put dst p 46 |
| 650 | p = add p 1 |
| 651 | for i ulong len suf bound 128 |
| 652 | put dst p get suf i |
| 653 | p = add p 1 |
| 654 | end |
| 655 | ret p |
| 656 | end |
| 657 | |
| 658 | fn stFindLayer L ulong suf view uchar * to long |
| 659 | tmp = new uchar 256 0 |
| 660 | n = stBuildLayerKey L suf tmp |
| 661 | ret stFindCanon tmp n |
| 662 | end |
| 663 | |
| 664 | text stExpMid "mlp.experts." |
| 665 | text stPleMid "ple.ple_embedding.ngram_embedding.shard_" |
| 666 | text stPleEnd ".weight" |
| 667 | |
| 668 | fn stAppU64 dst edit uchar 256 p0 ulong v ulong to ulong |
| 669 | p = p0 |
| 670 | if ge v 100 |
| 671 | put dst p uchar add 48 div v 100 |
| 672 | p = add p 1 |
| 673 | end |
| 674 | if ge v 10 |
| 675 | put dst p uchar add 48 rem div v 10 10 |
| 676 | p = add p 1 |
| 677 | end |
| 678 | put dst p uchar add 48 rem v 10 |
| 679 | ret add p 1 |
| 680 | end |
| 681 | |
| 682 | fn stFindExpert L ulong E ulong suf view uchar * to long |
| 683 | tmp = new uchar 256 0 |
| 684 | p = stBuildLayerKey L stExpMid tmp |
| 685 | p = stAppU64 tmp p E |
| 686 | put tmp p 46 |
| 687 | p = add p 1 |
| 688 | for i ulong len suf bound 64 |
| 689 | put tmp p get suf i |
| 690 | p = add p 1 |
| 691 | end |
| 692 | ret stFindCanon tmp p |
| 693 | end |
| 694 | |
| 695 | fn stFindPleShard L ulong P ulong to long |
| 696 | tmp = new uchar 256 0 |
| 697 | p = stBuildLayerKey L stPleMid tmp |
| 698 | p = stAppU64 tmp p P |
| 699 | for i ulong len stPleEnd bound 16 |
| 700 | put tmp p get stPleEnd i |
| 701 | p = add p 1 |
| 702 | end |
| 703 | ret stFindCanon tmp p |
| 704 | end |