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
GitHub aien-dev/aien-sovereign-core-mv#243 (code only)
Apply it
git checkout -b patch-27a1ba18b217cc40 39ef3ee8a02204d97bbba6809ae6ff178c263e68 curl -fsSL https://interplane.io/drakestapleton/aien-sovereign-core-mv/patches/27a1ba18b217cc40.patch | git am
Provenance
github-import 1
aien-dev/aien-sovereign-core-mv
243
a72873ffa1a313cfb9b46a13303e41c9b2210f92
{"kind":"github-import","owner":"aien-dev","repo":"aien-sovereign-core-mv","number":243,"url":"https://github.com/aien-dev/aien-sovereign-core-mv/pull/243","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:30Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index d1c93e37..b376a131 100644 --- a/.crumb +++ b/.crumb @@ -71,20 +71,20 @@ }, { "digest": "ef1840de79311e5684993b0e0b66e0a9fce42a2d7966506201686ae5f8084a98", - "last_commit": "8b5caed", + "last_commit": "ffc5555", "name": "skills", "purpose": "" } ], "children_root": "sha256:009593670dc7accc2b442a6a8c6f3b848ab73de4d6e61df57401bf4bf00b294a", - "compiled_at": "2026-10-06T22:12:40Z", + "compiled_at": "2026-10-06T22:23:22Z", "compiler": "crumb-compile/1", - "digest": "sha256:d7e76e71ecc067f264bb0d03f11a386ce65c5f5fc8a5f39dad306bc7b67f92a4", + "digest": "sha256:760e097e8b7bfb9b4c1e40e3f6fe20d367f5c17642b3ffb7fda54e7bbac8cc47", "evidence_files": 80, "evidence_root": "sha256:8920c1b8b9d7bddb4f92b61c3855179967991bed932999d2ba8cc98ef32f7837", - "files": 926, - "generated_at_commit": "c15b136fc30992e1ba915553d6ec1c680e5a0e61", - "source_tree": "sha256:c81a6a1bb7b66f289b9ee5ae87020183ff533ed1c4b51483e13f8e396c6b0f14" + "files": 934, + "generated_at_commit": "a5e40ecbc4a1a6ef072cd11a1c0938ea93e76629", + "source_tree": "sha256:204cda12fa53cd9522be83b36fda7af792ec29038fc3a837e5caaf73dcc24aa7" } }, "history": [], diff --git a/crates/aien-inference-abi/fixtures/openwaldo-byte/config.json b/crates/aien-inference-abi/fixtures/openwaldo-byte/config.json new file mode 100644 index 00000000..47dcdf60 --- /dev/null +++ b/crates/aien-inference-abi/fixtures/openwaldo-byte/config.json @@ -0,0 +1,21 @@ +{ + "architectures": [ + "LlamaForCausalLM" + ], + "bos_token_id": 1, + "eos_token_id": 2, + "hidden_size": 32, + "intermediate_size": 64, + "max_position_embeddings": 16, + "model_type": "llama", + "num_attention_heads": 4, + "num_hidden_layers": 1, + "num_key_value_heads": 2, + "pad_token_id": 0, + "rms_norm_eps": 0.00001, + "rope_theta": 10000, + "tie_word_embeddings": true, + "torch_dtype": "bfloat16", + "use_cache": true, + "vocab_size": 259 +} diff --git a/crates/aien-inference-abi/fixtures/openwaldo-byte/forward_reference.json b/crates/aien-inference-abi/fixtures/openwaldo-byte/forward_reference.json new file mode 100644 index 00000000..830c034e --- /dev/null +++ b/crates/aien-inference-abi/fixtures/openwaldo-byte/forward_reference.json @@ -0,0 +1 @@ +{"ids": [1, 75, 104, 111], "dtype": "float32 (bf16 weights upcast)", "transformers": "5.17.0", "logits": [0.03686109185218811, 0.06719756871461868, 0.054843757301568985, 0.10597755759954453, -0.10723552107810974, -0.14361147582530975, 0.04801269620656967, -0.10506268590688705, 0.16670101881027222, 0.18509754538536072, 0.09313393384218216, 0.21648848056793213, -0.11582600325345993, -0.05383507162332535, -0.04077399522066116, -0.07165858894586563, 0.02648058533668518, -0.0005170119693502784, 0.08148275315761566, -0.018301013857126236, -0.3083108365535736, 0.1390567570924759, -0.011848483234643936, -0.04182020574808121, -0.11792173236608505, -0.001996342558413744, -0.06566878408193588, 0.011800372041761875, 0.09672336280345917, 0.01529783196747303, 0.15639393031597137, 0.2987211048603058, 0.015702977776527405, 0.14028006792068481, -0.11075185984373093, 0.06666936725378036, 0.13703453540802002, -0.06052513048052788, -0.06270117312669754, -0.05303909629583359, -0.039495326578617096, 0.07272356748580933, -0.17556840181350708, -0.13302360475063324, 0.08016306161880493, -0.05381551384925842, 0.07198549062013626, 0.06802826374769211, -0.12547779083251953, -0.07378401607275009, -0.06065328046679497, -0.08340740203857422, -0.0014055968495085835, -0.08570805191993713, -0.013424940407276154, -0.1851963847875595, 0.06061027944087982, -0.10636365413665771, -0.1408076137304306, 0.03424616903066635, -0.04436194896697998, 0.09022366255521774, -0.027662303298711777, -0.07485117018222809, 0.01869950443506241, -0.06719858944416046, -0.11216308921575546, 0.02802221290767193, 0.18386025726795197, 0.11492548882961273, 0.14249645173549652, -0.07078590244054794, -0.0005705542862415314, 0.08009794354438782, -0.025222333148121834, 0.1262693852186203, 0.012952182441949844, -0.08621194958686829, 0.17954999208450317, -0.09859459102153778, -0.3176308870315552, -0.0014327851822599769, -0.016298923641443253, 0.021063225343823433, 0.02601466327905655, -0.0937512218952179, -0.11089102923870087, -0.0033633627463132143, 0.038832057267427444, 0.11760824918746948, 0.11711594462394714, 0.048364557325839996, -0.059150662273168564, 0.05098741129040718, -0.20337732136249542, 0.05415009334683418, -0.08064083755016327, 0.13412094116210938, -0.15353035926818848, 0.0854140892624855, -0.058337923139333725, 0.027657873928546906, -0.04403180256485939, -0.15234634280204773, 0.08942767232656479, -0.030122309923171997, -0.10981512814760208, -0.17278233170509338, -0.16307920217514038, 0.027935057878494263, 0.16127216815948486, 0.6936052441596985, -0.1779002845287323, 0.19994157552719116, 0.2137518674135208, -0.09906470775604248, 0.023275310173630714, -0.22069454193115234, -0.08924701809883118, -0.02037210389971733, -0.03132119029760361, 0.022263173013925552, -0.013858935795724392, 0.1899939328432083, 0.0006296652718447149, 0.10548534244298935, -0.035450004041194916, 0.13836830854415894, 0.14692795276641846, -0.03481940180063248, 0.18196287751197815, 0.2318314015865326, -0.02432520128786564, -0.17715227603912354, 0.15405306220054626, 0.20165464282035828, -0.06480468809604645, 0.13426809012889862, -0.005072183441370726, -0.04859992861747742, 0.03157094120979309, 0.07990792393684387, -0.031337056308984756, -0.1266934722661972, -0.010811065323650837, -0.13281852006912231, 0.3286910355091095, -0.0857689157128334, -0.0012114912969991565, 0.11433655768632889, -0.030031362548470497, 0.02661379799246788, 0.03684643656015396, 0.0049749878235161304, -0.08901112526655197, 0.04174346849322319, -0.023440077900886536, 0.04420075938105583, 0.04555141553282738, 0.005193099845200777, 0.014836525544524193, 0.19811826944351196, 0.1542014330625534, -0.03210318833589554, -0.016818847507238388, 0.1543891876935959, 0.03071008250117302, -0.027317127212882042, 0.004829246085137129, 0.03223080188035965, -0.02179602161049843, 0.07570453733205795, -0.05142970755696297, 0.08353918045759201, 0.22404925525188446, -0.17175282537937164, -0.028765393421053886, -0.0933588445186615, -0.05530545860528946, -0.074859119951725, -0.022473938763141632, 0.058466773480176926, -0.08429165929555893, -0.07659710198640823, -0.11277800053358078, 0.22170943021774292, -0.19165857136249542, -0.035271503031253815, 0.03366241231560707, -0.09873504936695099, -0.022149700671434402, -0.1777334064245224, -0.13198307156562805, -0.29751354455947876, -0.06926601380109787, 0.051210496574640274, -0.003827588399872184, 0.18874098360538483, -0.08528946340084076, 0.1260918229818344, 0.18137860298156738, 0.1574290692806244, -0.15350252389907837, 0.02715352736413479, -0.05251274257898331, 0.11517137289047241, 0.03674396499991417, -0.029276171699166298, -0.002046679612249136, -0.0295750480145216, 0.16343264281749725, 0.09257590025663376, -0.00021815145737491548, -0.0936274603009224, -0.06835975497961044, -0.18886391818523407, -0.15978892147541046, -0.2544933259487152, 0.1468595266342163, -0.07531987875699997, -0.07598959654569626, 0.14526043832302094, -0.11831163614988327, -0.03449155390262604, -0.0515945628285408, 0.15598678588867188, -0.03705718740820885, -0.08926965296268463, 0.013915364630520344, 0.0555524043738842, -0.029206909239292145, 0.07056856155395508, -0.06075640767812729, 0.05750679597258568, -0.04428062587976456, -0.0716182291507721, 0.019489767029881477, 0.053606368601322174, -0.049692343920469284, 0.0797848179936409, -0.030155936256051064, 0.06855534762144089, 0.07521671801805496, -0.038465552031993866, -0.014261594973504543, -0.03025013394653797, -0.09973245859146118, -0.022979503497481346, 0.16149155795574188, -0.2255270928144455, -0.056311286985874176, 0.06763747334480286, -0.021301595494151115, 0.10081198811531067, 0.04202473908662796, 0.02081272564828396, -0.10466238111257553, -0.1838335394859314, -0.02043093927204609], "argmax": 111} \ No newline at end of file diff --git a/crates/aien-inference-abi/fixtures/openwaldo-byte/generation_config.json b/crates/aien-inference-abi/fixtures/openwaldo-byte/generation_config.json new file mode 100644 index 00000000..31b7dd6a --- /dev/null +++ b/crates/aien-inference-abi/fixtures/openwaldo-byte/generation_config.json @@ -0,0 +1 @@ +{"_from_model_config":true,"bos_token_id":1,"eos_token_id":2,"pad_token_id":0} diff --git a/crates/aien-inference-abi/fixtures/openwaldo-byte/model.safetensors b/crates/aien-inference-abi/fixtures/openwaldo-byte/model.safetensors new file mode 100644 index 00000000..19e9b3d1 Binary files /dev/null and b/crates/aien-inference-abi/fixtures/openwaldo-byte/model.safetensors differ diff --git a/crates/aien-inference-abi/fixtures/openwaldo-byte/reference.json b/crates/aien-inference-abi/fixtures/openwaldo-byte/reference.json new file mode 100644 index 00000000..bf001c77 --- /dev/null +++ b/crates/aien-inference-abi/fixtures/openwaldo-byte/reference.json @@ -0,0 +1,269 @@ +[ + { + "text": "", + "ids": [], + "decoded": "" + }, + { + "text": "Hello, world!", + "ids": [ + 75, + 104, + 111, + 111, + 114, + 47, + 35, + 122, + 114, + 117, + 111, + 103, + 36 + ], + "decoded": "Hello, world!" + }, + { + "text": "café ünïcödé", + "ids": [ + 102, + 100, + 105, + 198, + 172, + 35, + 198, + 191, + 113, + 198, + 178, + 102, + 198, + 185, + 103, + 198, + 172 + ], + "decoded": "café ünïcödé" + }, + { + "text": "日本語のテキスト", + "ids": [ + 233, + 154, + 168, + 233, + 159, + 175, + 235, + 173, + 161, + 230, + 132, + 177, + 230, + 134, + 137, + 230, + 133, + 176, + 230, + 133, + 188, + 230, + 134, + 139 + ], + "decoded": "日本語のテキスト" + }, + { + "text": "emoji 🙂👍🏽", + "ids": [ + 104, + 112, + 114, + 109, + 108, + 35, + 243, + 162, + 156, + 133, + 243, + 162, + 148, + 144, + 243, + 162, + 146, + 192 + ], + "decoded": "emoji 🙂👍🏽" + }, + { + "text": "tab\tnew\nline\r\n", + "ids": [ + 119, + 100, + 101, + 12, + 113, + 104, + 122, + 13, + 111, + 108, + 113, + 104, + 16, + 13 + ], + "decoded": "tab\tnew\nline\r\n" + }, + { + "text": " leading and trailing ", + "ids": [ + 35, + 35, + 35, + 111, + 104, + 100, + 103, + 108, + 113, + 106, + 35, + 100, + 113, + 103, + 35, + 119, + 117, + 100, + 108, + 111, + 108, + 113, + 106, + 35, + 35, + 35 + ], + "decoded": " leading and trailing " + }, + { + "text": "a<bos>b<eos>c<pad>", + "ids": [ + 100, + 1, + 101, + 2, + 102, + 0 + ], + "decoded": "a<bos>b<eos>c<pad>" + }, + { + "text": "<0x41> is not A", + "ids": [ + 63, + 51, + 123, + 55, + 52, + 65, + 35, + 108, + 118, + 35, + 113, + 114, + 119, + 35, + 68 + ], + "decoded": "<0x41> is not A" + }, + { + "text": "The quick brown fox jumps over the lazy dog 0123456789 ~!@#$%^&*()", + "ids": [ + 87, + 107, + 104, + 35, + 116, + 120, + 108, + 102, + 110, + 35, + 101, + 117, + 114, + 122, + 113, + 35, + 105, + 114, + 123, + 35, + 109, + 120, + 112, + 115, + 118, + 35, + 114, + 121, + 104, + 117, + 35, + 119, + 107, + 104, + 35, + 111, + 100, + 125, + 124, + 35, + 103, + 114, + 106, + 35, + 51, + 52, + 53, + 54, + 55, + 56, + 57, + 58, + 59, + 60, + 35, + 129, + 36, + 67, + 38, + 39, + 40, + 97, + 41, + 45, + 43, + 44 + ], + "decoded": "The quick brown fox jumps over the lazy dog 0123456789 ~!@#$%^&*()" + }, + { + "text": null, + "ids": [ + 229, + 133, + 68 + ], + "decoded": "�A" + } +] diff --git a/crates/aien-inference-abi/fixtures/openwaldo-byte/tokenizer.json b/crates/aien-inference-abi/fixtures/openwaldo-byte/tokenizer.json new file mode 100644 index 00000000..7076ba5f --- /dev/null +++ b/crates/aien-inference-abi/fixtures/openwaldo-byte/tokenizer.json @@ -0,0 +1,320 @@ +{ + "version": "1.0", + "truncation": null, + "padding": null, + "added_tokens": [ + { + "id": 0, + "content": "<pad>", + "single_word": false, + "lstrip": false, + "rstrip": false, + "normalized": false, + "special": true + }, + { + "id": 1, + "content": "<bos>", + "single_word": false, + "lstrip": false, + "rstrip": false, + "normalized": false, + "special": true + }, + { + "id": 2, + "content": "<eos>", + "single_word": false, + "lstrip": false, + "rstrip": false, + "normalized": false, + "special": true + } + ], + "normalizer": null, + "pre_tokenizer": null, + "post_processor": null, + "decoder": { + "type": "Sequence", + "decoders": [ + { + "type": "ByteFallback" + }, + { + "type": "Fuse" + } + ] + }, + "model": { + "type": "BPE", + "dropout": null, + "unk_token": null, + "continuing_subword_prefix": null, + "end_of_word_suffix": null, + "fuse_unk": false, + "byte_fallback": true, + "ignore_merges": false, + "vocab": { + "<pad>": 0, + "<bos>": 1, + "<eos>": 2, + "<0x00>": 3, + "<0x01>": 4, + "<0x02>": 5, + "<0x03>": 6, + "<0x04>": 7, + "<0x05>": 8, + "<0x06>": 9, + "<0x07>": 10, + "<0x08>": 11, + "<0x09>": 12, + "<0x0A>": 13, + "<0x0B>": 14, + "<0x0C>": 15, + "<0x0D>": 16, + "<0x0E>": 17, + "<0x0F>": 18, + "<0x10>": 19, + "<0x11>": 20, + "<0x12>": 21, + "<0x13>": 22, + "<0x14>": 23, + "<0x15>": 24, + "<0x16>": 25, + "<0x17>": 26, + "<0x18>": 27, + "<0x19>": 28, + "<0x1A>": 29, + "<0x1B>": 30, + "<0x1C>": 31, + "<0x1D>": 32, + "<0x1E>": 33, + "<0x1F>": 34, + "<0x20>": 35, + "<0x21>": 36, + "<0x22>": 37, + "<0x23>": 38, + "<0x24>": 39, + "<0x25>": 40, + "<0x26>": 41, + "<0x27>": 42, + "<0x28>": 43, + "<0x29>": 44, + "<0x2A>": 45, + "<0x2B>": 46, + "<0x2C>": 47, + "<0x2D>": 48, + "<0x2E>": 49, + "<0x2F>": 50, + "<0x30>": 51, + "<0x31>": 52, + "<0x32>": 53, + "<0x33>": 54, + "<0x34>": 55, + "<0x35>": 56, + "<0x36>": 57, + "<0x37>": 58, + "<0x38>": 59, + "<0x39>": 60, + "<0x3A>": 61, + "<0x3B>": 62, + "<0x3C>": 63, + "<0x3D>": 64, + "<0x3E>": 65, + "<0x3F>": 66, + "<0x40>": 67, + "<0x41>": 68, + "<0x42>": 69, + "<0x43>": 70, + "<0x44>": 71, + "<0x45>": 72, + "<0x46>": 73, + "<0x47>": 74, + "<0x48>": 75, + "<0x49>": 76, + "<0x4A>": 77, + "<0x4B>": 78, + "<0x4C>": 79, + "<0x4D>": 80, + "<0x4E>": 81, + "<0x4F>": 82, + "<0x50>": 83, + "<0x51>": 84, + "<0x52>": 85, + "<0x53>": 86, + "<0x54>": 87, + "<0x55>": 88, + "<0x56>": 89, + "<0x57>": 90, + "<0x58>": 91, + "<0x59>": 92, + "<0x5A>": 93, + "<0x5B>": 94, + "<0x5C>": 95, + "<0x5D>": 96, + "<0x5E>": 97, + "<0x5F>": 98, + "<0x60>": 99, + "<0x61>": 100, + "<0x62>": 101, + "<0x63>": 102, + "<0x64>": 103, + "<0x65>": 104, + "<0x66>": 105, + "<0x67>": 106, + "<0x68>": 107, + "<0x69>": 108, + "<0x6A>": 109, + "<0x6B>": 110, + "<0x6C>": 111, + "<0x6D>": 112, + "<0x6E>": 113, + "<0x6F>": 114, + "<0x70>": 115, + "<0x71>": 116, + "<0x72>": 117, + "<0x73>": 118, + "<0x74>": 119, + "<0x75>": 120, + "<0x76>": 121, + "<0x77>": 122, + "<0x78>": 123, + "<0x79>": 124, + "<0x7A>": 125, + "<0x7B>": 126, + "<0x7C>": 127, + "<0x7D>": 128, + "<0x7E>": 129, + "<0x7F>": 130, + "<0x80>": 131, + "<0x81>": 132, + "<0x82>": 133, + "<0x83>": 134, + "<0x84>": 135, + "<0x85>": 136, + "<0x86>": 137, + "<0x87>": 138, + "<0x88>": 139, + "<0x89>": 140, + "<0x8A>": 141, + "<0x8B>": 142, + "<0x8C>": 143, + "<0x8D>": 144, + "<0x8E>": 145, + "<0x8F>": 146, + "<0x90>": 147, + "<0x91>": 148, + "<0x92>": 149, + "<0x93>": 150, + "<0x94>": 151, + "<0x95>": 152, + "<0x96>": 153, + "<0x97>": 154, + "<0x98>": 155, + "<0x99>": 156, + "<0x9A>": 157, + "<0x9B>": 158, + "<0x9C>": 159, + "<0x9D>": 160, + "<0x9E>": 161, + "<0x9F>": 162, + "<0xA0>": 163, + "<0xA1>": 164, + "<0xA2>": 165, + "<0xA3>": 166, + "<0xA4>": 167, + "<0xA5>": 168, + "<0xA6>": 169, + "<0xA7>": 170, + "<0xA8>": 171, + "<0xA9>": 172, + "<0xAA>": 173, + "<0xAB>": 174, + "<0xAC>": 175, + "<0xAD>": 176, + "<0xAE>": 177, + "<0xAF>": 178, + "<0xB0>": 179, + "<0xB1>": 180, + "<0xB2>": 181, + "<0xB3>": 182, + "<0xB4>": 183, + "<0xB5>": 184, + "<0xB6>": 185, + "<0xB7>": 186, + "<0xB8>": 187, + "<0xB9>": 188, + "<0xBA>": 189, + "<0xBB>": 190, + "<0xBC>": 191, + "<0xBD>": 192, + "<0xBE>": 193, + "<0xBF>": 194, + "<0xC0>": 195, + "<0xC1>": 196, + "<0xC2>": 197, + "<0xC3>": 198, + "<0xC4>": 199, + "<0xC5>": 200, + "<0xC6>": 201, + "<0xC7>": 202, + "<0xC8>": 203, + "<0xC9>": 204, + "<0xCA>": 205, + "<0xCB>": 206, + "<0xCC>": 207, + "<0xCD>": 208, + "<0xCE>": 209, + "<0xCF>": 210, + "<0xD0>": 211, + "<0xD1>": 212, + "<0xD2>": 213, + "<0xD3>": 214, + "<0xD4>": 215, + "<0xD5>": 216, + "<0xD6>": 217, + "<0xD7>": 218, + "<0xD8>": 219, + "<0xD9>": 220, + "<0xDA>": 221, + "<0xDB>": 222, + "<0xDC>": 223, + "<0xDD>": 224, + "<0xDE>": 225, + "<0xDF>": 226, + "<0xE0>": 227, + "<0xE1>": 228, + "<0xE2>": 229, + "<0xE3>": 230, + "<0xE4>": 231, + "<0xE5>": 232, + "<0xE6>": 233, + "<0xE7>": 234, + "<0xE8>": 235, + "<0xE9>": 236, + "<0xEA>": 237, + "<0xEB>": 238, + "<0xEC>": 239, + "<0xED>": 240, + "<0xEE>": 241, + "<0xEF>": 242, + "<0xF0>": 243, + "<0xF1>": 244, + "<0xF2>": 245, + "<0xF3>": 246, + "<0xF4>": 247, + "<0xF5>": 248, + "<0xF6>": 249, + "<0xF7>": 250, + "<0xF8>": 251, + "<0xF9>": 252, + "<0xFA>": 253, + "<0xFB>": 254, + "<0xFC>": 255, + "<0xFD>": 256, + "<0xFE>": 257, + "<0xFF>": 258 + }, + "merges": [] + } +} diff --git a/crates/aien-inference-abi/tests/openwaldo_byte_tokenizer.rs b/crates/aien-inference-abi/tests/openwaldo_byte_tokenizer.rs new file mode 100644 index 00000000..d217f5df --- /dev/null +++ b/crates/aien-inference-abi/tests/openwaldo_byte_tokenizer.rs @@ -0,0 +1,85 @@ +//! OpenWALDO byte tokenizer as a plain `tokenizer.json`. +//! +//! OpenWALDO exports ship a custom Python tokenizer (`OpenWALDOByteTokenizer`): +//! `<pad>`=0, `<bos>`=1, `<eos>`=2, then byte `b` -> id `b + 3`. The fixture +//! `tokenizer.json` states the same mapping as a BPE model with no merges and +//! byte fallback, so AIEN loads it with no tokenizer code change. The +//! reference ids and decoded strings in `reference.json` were produced by the +//! exported Python class itself (transformers 5.17.0, CPU). + +use aien_inference_abi::tokenizer::ChatTokenizer; +use serde_json::Value; + +const DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/openwaldo-byte"); + +fn load() -> (ChatTokenizer, Vec<Value>) { + let tok = + ChatTokenizer::from_file(format!("{DIR}/tokenizer.json")).expect("load tokenizer.json"); + let text = std::fs::read_to_string(format!("{DIR}/reference.json")).expect("read reference"); + let cases: Vec<Value> = serde_json::from_str(&text).expect("parse reference"); + (tok, cases) +} + +fn ids(v: &Value) -> Vec<u32> { + v["ids"] + .as_array() + .unwrap() + .iter() + .map(|i| i.as_u64().unwrap() as u32) + .collect() +} + +#[test] +fn encode_matches_openwaldo_reference() { + let (tok, cases) = load(); + let mut checked = 0; + for case in cases.iter().filter(|c| c["text"].is_string()) { + let text = case["text"].as_str().unwrap(); + let got = tok.encode_with_special(text, false).expect("encode"); + assert_eq!(got, ids(case), "encode mismatch for {text:?}"); + checked += 1; + } + assert_eq!(checked, 10); +} + +#[test] +fn decode_matches_openwaldo_reference_on_valid_utf8() { + let (tok, cases) = load(); + for case in cases.iter().filter(|c| c["text"].is_string()) { + let got = tok.decode_opts(&ids(case), false).expect("decode"); + assert_eq!( + got, + case["decoded"].as_str().unwrap(), + "decode mismatch for {:?}", + ids(case) + ); + } +} + +#[test] +fn every_byte_maps_to_byte_plus_three() { + let (tok, _) = load(); + for b in 0u8..=255 { + let id = u32::from(b) + 3; + let s = format!("<0x{b:02X}>"); + assert_eq!(tok.token_to_id(&s), Some(id), "{s}"); + } +} + +/// Known limit: on an invalid UTF-8 byte run the two decoders disagree. +/// Python (`errors="replace"`) emits one U+FFFD per maximal invalid subpart and +/// keeps the following valid byte; the `tokenizers` ByteFallback decoder emits +/// one U+FFFD per byte token of the whole run. Token ids are identical, so +/// receipts that bind token ids are unaffected; decoded text is not. +/// This test pins today's behaviour so a decoder change is noticed. +#[test] +fn decode_diverges_on_invalid_utf8_run() { + let (tok, cases) = load(); + let case = cases + .iter() + .find(|c| c["text"].is_null()) + .expect("invalid-run case"); + assert_eq!(case["decoded"].as_str().unwrap(), "\u{FFFD}A"); + let got = tok.decode_opts(&ids(case), false).expect("decode"); + assert_eq!(got, "\u{FFFD}\u{FFFD}\u{FFFD}"); +} diff --git a/crates/aien-inference-abi/tests/openwaldo_forward_parity.rs b/crates/aien-inference-abi/tests/openwaldo_forward_parity.rs new file mode 100644 index 00000000..6d19aafe --- /dev/null +++ b/crates/aien-inference-abi/tests/openwaldo_forward_parity.rs @@ -0,0 +1,59 @@ +//! OpenWALDO tiny model (1 layer, hidden 32, 4 heads, 2 KV heads, vocab 259, tied +//! embeddings, bf16) loaded by the CPU reference engine from its Hugging Face directory, +//! compared with last-position logits from transformers 5.17.0 (CPU, float32 after +//! upcasting the bf16 weights). Input ids [1, 75, 104, 111] = bos + bytes "Hel". +//! Tolerance: absolute 1e-4, the ABSOLUTE_TOLERANCE of tinyllama_parity.rs (set before +//! the first comparison, not tuned). + +use aien_inference_abi::{load_model_config, TransformerWeights}; +use serde_json::Value; +use std::path::PathBuf; + +const ABSOLUTE_TOLERANCE: f32 = 1e-4; + +fn dir() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("fixtures/openwaldo-byte") +} + +#[test] +fn openwaldo_cpu_logits_match_transformers_reference() { + let dir = dir(); + let config = load_model_config(&dir).expect("config.json"); + let weights = TransformerWeights::load_from_safetensors(dir.join("model.safetensors"), &config) + .expect("weights"); + let r: Value = + serde_json::from_str(&std::fs::read_to_string(dir.join("forward_reference.json")).unwrap()) + .unwrap(); + let ids: Vec<u32> = r["ids"] + .as_array() + .unwrap() + .iter() + .map(|v| v.as_u64().unwrap() as u32) + .collect(); + assert_eq!(ids, vec![1, 75, 104, 111]); + let want: Vec<f32> = r["logits"] + .as_array() + .unwrap() + .iter() + .map(|v| v.as_f64().unwrap() as f32) + .collect(); + let got = + weights.forward_sequence_with_diagnostics(&ids).activations["last_token_logits"].clone(); + assert_eq!(got.len(), 259); + assert_eq!(want.len(), 259); + let max_abs = got + .iter() + .zip(&want) + .map(|(a, b)| (a - b).abs()) + .fold(0.0f32, f32::max); + eprintln!("max abs diff = {max_abs:e}"); + assert!(max_abs <= ABSOLUTE_TOLERANCE, "max abs diff {max_abs:e}"); + let argmax = |v: &[f32]| { + v.iter() + .enumerate() + .max_by(|a, b| a.1.total_cmp(b.1)) + .unwrap() + .0 + }; + assert_eq!(argmax(&got) as u64, r["argmax"].as_u64().unwrap()); +}