interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-kv-store / tests / common / mod.rs

167 lines · 5473 bytes Raw Blame History
1#![allow(dead_code)]
2// SPDX-License-Identifier: Apache-2.0
3// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17use aien_kv_store::{
18 BackendFormat, ChunkHeader, ChunkKey, ComputeConfig, EngineConfig, EngineIdentity, HiddenAct,
19 KvDtype, LayoutStrides, NamespaceFields, RopeScaling, WeightFormat,
20};
21
22/// Llama-3.2-1B-like compute configuration (ADR 1.2.1), the base of every
23/// `sample()`.
24pub fn sample_compute() -> ComputeConfig<'static> {
25 ComputeConfig {
26 num_heads: 32,
27 hidden_dim: 2048,
28 intermediate_dim: 8192,
29 vocab_size: 128_256,
30 rms_norm_eps: 1e-5,
31 tie_word_embeddings: true,
32 hidden_act: HiddenAct::Silu,
33 arith_id: "ReferenceCpuBackend",
34 arith_epoch: 1,
35 }
36}
37
38/// A dense model on the host reference path (ADR 1.2.2): no experts, every
39/// run-time switch off.
40pub fn sample_engine_config() -> EngineConfig<'static> {
41 EngineConfig {
42 num_experts: 0,
43 experts_per_token: 0,
44 expert_intermediate_dim: 0,
45 whole_prompt_prefill: false,
46 device_router: false,
47 matmul_path: "host-tensor-backend",
48 }
49}
50
51/// The engine identity of every `sample()` (ADR 1.2.2).
52pub fn sample_engine() -> EngineIdentity<'static> {
53 EngineIdentity {
54 weight_format: WeightFormat::AsFile,
55 engine_build_id: "aien-runtime/0.1.0+git:test",
56 rounding_class: "ReferenceCpuBackend@kv-arith-epoch-1",
57 engine_config_digest: sample_engine_config().digest().unwrap(),
58 }
59}
60
61/// Llama-3.2-1B-like host geometry (16 layers, 8 kv heads, head_dim 64, bf16,
62/// 16-token blocks, 16-block chunks = 256 tokens).
63pub fn sample() -> NamespaceFields<'static> {
64 NamespaceFields {
65 model_sha256: [0xAB; 32],
66 model_digest_kind: "sha256-file",
67 model_id: "llama-3.2-1b-instruct",
68 num_layers: 16,
69 num_kv_heads: 8,
70 head_dim: 64,
71 block_size: 16,
72 rope_theta: 500000.0,
73 rope_scaling: Some(RopeScaling {
74 factor: 32.0,
75 low_freq_factor: 1.0,
76 high_freq_factor: 4.0,
77 original_max_position_embeddings: 8192,
78 }),
79 qk_norm: false,
80 dtype: KvDtype::Bf16,
81 layout: LayoutStrides {
82 head_stride: 128,
83 token_stride: 1024,
84 kv_plane_stride: 16384,
85 layer_stride: 32768,
86 block_stride: 524288,
87 element_bytes: 2,
88 },
89 backend_format: BackendFormat::HostPagedPoolV1,
90 chunk_tokens: 256,
91 sharing_namespace: "default",
92 compute_config_digest: sample_compute()
93 .digest_with_engine(&sample_engine())
94 .unwrap(),
95 }
96}
97
98/// Small geometry for chain tests: 4-token blocks, 8-token chunks.
99pub fn small() -> NamespaceFields<'static> {
100 let mut f = sample();
101 f.block_size = 4;
102 f.chunk_tokens = 8;
103 f.layout.kv_plane_stride = 4 * 1024;
104 f.layout.layer_stride = 8 * 1024;
105 f.layout.block_stride = 16 * 8 * 1024;
106 f
107}
108
109pub fn hex(bytes: &[u8]) -> String {
110 const D: &[u8; 16] = b"0123456789abcdef";
111 let mut s = String::with_capacity(bytes.len() * 2);
112 for b in bytes {
113 s.push(D[(b >> 4) as usize] as char);
114 s.push(D[(b & 15) as usize] as char);
115 }
116 s
117}
118
119pub fn unhex(s: &str) -> Vec<u8> {
120 assert!(s.len().is_multiple_of(2), "odd hex length");
121 (0..s.len())
122 .step_by(2)
123 .map(|i| u8::from_str_radix(&s[i..i + 2], 16).expect("hex digit"))
124 .collect()
125}
126
127pub fn unhex32(s: &str) -> [u8; 32] {
128 unhex(s).try_into().expect("32 bytes")
129}
130
131/// A fully populated, internally consistent header for the `sample()` geometry
132/// (first chunk, full 256 tokens).
133pub fn sample_chunk_header() -> ChunkHeader {
134 let f = sample();
135 let ns = f.namespace_id().unwrap();
136 let tokens: Vec<u32> = (0..256).collect();
137 let key = aien_kv_store::chunk_key(&ns, None, 0, &tokens).unwrap();
138 f.chunk_header(&ns, key, None, 0, 256, 1_700_000_000_000_000_000)
139 .unwrap()
140}
141
142pub fn key(b: u8) -> ChunkKey {
143 ChunkKey(aien_platform::ObjectId([b; 32]))
144}
145
146/// Deterministic segment digests: SHA-256 of "<tag>/seg/<j>".
147pub fn seg_digests(tag: &str, n: u32) -> Vec<[u8; 32]> {
148 use sha2::{Digest, Sha256};
149 (0..n)
150 .map(|j| Sha256::digest(format!("{tag}/seg/{j}").as_bytes()).into())
151 .collect()
152}
153
154/// A consistent record for the `sample()` geometry: 16 segments of one pool
155/// block each, `payload_sha256` = the root over them.
156pub fn sample_record() -> (aien_kv_store::RecordHeader, Vec<[u8; 32]>) {
157 let chunk = sample_chunk_header();
158 let n = sample().segment_count(chunk.token_count).unwrap();
159 let digests = seg_digests("sample", n);
160 let rec = aien_kv_store::RecordHeader {
161 chunk,
162 payload_sha256: aien_kv_store::payload_root(&digests),
163 };
164 (rec, digests)
165}
166#[cfg(feature = "std")]
167pub mod rig;