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
aien-sovereign-core-mv / evidence / qwen38-omega-full-20261018T1705Z / change.diff
| 1 | diff --git a/crates/aien-inference-abi/src/qwen38.rs b/crates/aien-inference-abi/src/qwen38.rs |
| 2 | index 023b9695..02f0ff61 100644 |
| 3 | --- a/crates/aien-inference-abi/src/qwen38.rs |
| 4 | +++ b/crates/aien-inference-abi/src/qwen38.rs |
| 5 | @@ -213,6 +213,18 @@ impl Qwen38Config { |
| 6 | Some(c) |
| 7 | } |
| 8 | |
| 9 | + /// Layers `first .. first + count` of the model as a stack of their own (a window for parity |
| 10 | + /// checks that fit the device one window at a time). `None` when empty or past the end. |
| 11 | + pub fn window(&self, first: usize, count: usize) -> Option<Self> { |
| 12 | + if count == 0 || first + count > self.layers { |
| 13 | + return None; |
| 14 | + } |
| 15 | + let mut c = self.clone(); |
| 16 | + c.layers = count; |
| 17 | + c.full_attention = self.full_attention[first..first + count].to_vec(); |
| 18 | + Some(c) |
| 19 | + } |
| 20 | + |
| 21 | /// Width of the DeltaNet conv input: q | k | v. |
| 22 | pub fn conv_dim(&self) -> usize { |
| 23 | 2 * self.lin_k_heads * self.lin_k_dim + self.lin_v_heads * self.lin_v_dim |
| 24 | @@ -652,6 +664,20 @@ impl<S: TensorSource> Qwen38Reference<S> { |
| 25 | state: &mut Qwen38State, |
| 26 | tokens: &[u32], |
| 27 | logits: Logits, |
| 28 | + ) -> Result<Vec<f32>, String> { |
| 29 | + self.forward_tapped(state, tokens, logits, &[], &mut Vec::new()) |
| 30 | + } |
| 31 | + |
| 32 | + /// [`Self::forward`] that also copies the residual stream entering layer `b` (`[t][hidden]`, |
| 33 | + /// before its input norm) into `taps` for every `b` in `at` (ascending; `b == layers` is the |
| 34 | + /// residual before the final norm). The chip checks a model window by window against these. |
| 35 | + pub fn forward_tapped( |
| 36 | + &mut self, |
| 37 | + state: &mut Qwen38State, |
| 38 | + tokens: &[u32], |
| 39 | + logits: Logits, |
| 40 | + at: &[usize], |
| 41 | + taps: &mut Vec<Vec<f32>>, |
| 42 | ) -> Result<Vec<f32>, String> { |
| 43 | let c = self.config.clone(); |
| 44 | let (t, h) = (tokens.len(), c.hidden); |
| 45 | @@ -668,7 +694,13 @@ impl<S: TensorSource> Qwen38Reference<S> { |
| 46 | *o = bf16_to_f32(b); |
| 47 | } |
| 48 | } |
| 49 | + if at.windows(2).any(|p| p[0] >= p[1]) || at.iter().any(|&b| b > c.layers) { |
| 50 | + return Err(format!("forward: taps {at:?} not ascending within 0..={}", c.layers)); |
| 51 | + } |
| 52 | for l in 0..c.layers { |
| 53 | + if at.contains(&l) { |
| 54 | + taps.push(x.clone()); |
| 55 | + } |
| 56 | let w = self.read_layer(l)?; |
| 57 | let mut xn = x.clone(); |
| 58 | rms_norm_zc(&mut xn, &w.input_norm, c.eps); |
| 59 | @@ -690,6 +722,9 @@ impl<S: TensorSource> Qwen38Reference<S> { |
| 60 | let down = w.down.matmul(&act, t); |
| 61 | x.iter_mut().zip(&down).for_each(|(a, b)| *a += b); |
| 62 | } |
| 63 | + if at.contains(&c.layers) { |
| 64 | + taps.push(x.clone()); |
| 65 | + } |
| 66 | state.pos += t; |
| 67 | let rows = match logits { |
| 68 | Logits::Last => &x[(t - 1) * h..], |
| 69 | diff --git a/crates/aien-inference-abi/src/qwen38_omega.rs b/crates/aien-inference-abi/src/qwen38_omega.rs |
| 70 | index 4988e99c..e048790e 100644 |
| 71 | --- a/crates/aien-inference-abi/src/qwen38_omega.rs |
| 72 | +++ b/crates/aien-inference-abi/src/qwen38_omega.rs |
| 73 | @@ -165,11 +165,38 @@ impl Qwen38Omega { |
| 74 | Self::load(config, &mut ShardSource::open(dir)?, max_pos) |
| 75 | } |
| 76 | |
| 77 | + /// Opens a checkpoint directory and uploads model layers `first .. first + count` as a stack |
| 78 | + /// of their own (final norm and lm_head on top): a window, fed and read through |
| 79 | + /// [`Self::step_residual`] / [`Self::residual`] so a model larger than free device memory is |
| 80 | + /// checked one window at a time. |
| 81 | + pub fn open_window( |
| 82 | + dir: &Path, |
| 83 | + first: usize, |
| 84 | + count: usize, |
| 85 | + max_pos: usize, |
| 86 | + ) -> Result<Self, String> { |
| 87 | + let full = Qwen38Config::from_checkpoint(dir)?; |
| 88 | + let config = full.window(first, count).ok_or_else(|| { |
| 89 | + format!("window {first}+{count}: outside the {} layers", full.layers) |
| 90 | + })?; |
| 91 | + Self::load_at(config, first, &mut ShardSource::open(dir)?, max_pos) |
| 92 | + } |
| 93 | + |
| 94 | /// Uploads `config.layers` layers from `source`. |
| 95 | pub fn load( |
| 96 | config: Qwen38Config, |
| 97 | source: &mut dyn TensorSource, |
| 98 | max_pos: usize, |
| 99 | + ) -> Result<Self, String> { |
| 100 | + Self::load_at(config, 0, source, max_pos) |
| 101 | + } |
| 102 | + |
| 103 | + /// Uploads `config.layers` layers from `source`, reading model layer `first + l` for layer `l`. |
| 104 | + fn load_at( |
| 105 | + config: Qwen38Config, |
| 106 | + first: usize, |
| 107 | + source: &mut dyn TensorSource, |
| 108 | + max_pos: usize, |
| 109 | ) -> Result<Self, String> { |
| 110 | use aien_omega_q38 as q; |
| 111 | let c = &config; |
| 112 | @@ -230,7 +257,7 @@ impl Qwen38Omega { |
| 113 | let mut norms = Vec::with_capacity(c.layers); |
| 114 | let (mut gi, mut ai) = (0usize, 0usize); |
| 115 | for l in 0..c.layers { |
| 116 | - let p = format!("{prefix}layers.{l}"); |
| 117 | + let p = format!("{prefix}layers.{}", first + l); |
| 118 | let rm = |s: &mut dyn TensorSource, name: String, n, k| read_matrix(s, &name, n, k); |
| 119 | let (mix, ab, o) = if c.full_attention[l] { |
| 120 | let a = format!("{p}.self_attn"); |
| 121 | @@ -440,6 +467,37 @@ impl Qwen38Omega { |
| 122 | Ok(out) |
| 123 | } |
| 124 | |
| 125 | + /// Feeds the residual stream `x` (`[hidden]`, what enters this stack's first layer: the |
| 126 | + /// previous window's [`Self::residual`]) at the next position instead of a token's embedding. |
| 127 | + pub fn step_residual(&mut self, x: &[f32]) -> Result<(u32, GraphStats), String> { |
| 128 | + if x.len() != self.config.hidden { |
| 129 | + return Err(format!( |
| 130 | + "step_residual: {} values (hidden {})", |
| 131 | + x.len(), |
| 132 | + self.config.hidden |
| 133 | + )); |
| 134 | + } |
| 135 | + if self.pos >= self.max_pos { |
| 136 | + return Err(format!( |
| 137 | + "step: the KV cache holds {} positions", |
| 138 | + self.max_pos |
| 139 | + )); |
| 140 | + } |
| 141 | + let out = self.graph.token(x, self.pos).map_err(gpu("q38 token"))?; |
| 142 | + self.pos += 1; |
| 143 | + Ok(out) |
| 144 | + } |
| 145 | + |
| 146 | + /// The last step's residual stream after this stack's last layer (`[hidden]`, before the |
| 147 | + /// final norm). |
| 148 | + pub fn residual(&mut self) -> Result<Vec<f32>, String> { |
| 149 | + let mut x = vec![0f32; self.config.hidden]; |
| 150 | + self.graph |
| 151 | + .read(Some(&mut x), None, None) |
| 152 | + .map_err(gpu("q38 read residual"))?; |
| 153 | + Ok(x) |
| 154 | + } |
| 155 | + |
| 156 | /// The last step's logits (`[vocab]`). |
| 157 | pub fn logits(&mut self) -> Result<Vec<f32>, String> { |
| 158 | let mut l = vec![0f32; self.config.vocab]; |