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 / crates / aien-omega-dattn / src / rows.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Row access to the decode attention cache (persistent KV park/restore, sc#505). New file. |
| 17 | //! |
| 18 | //! omega keeps each layer's K and V planes as bf16 rows of [`HEAD_DIM`] values, row |
| 19 | //! `(kv_head, pos)` at `(kv_head * max_pos + pos) * 256` bytes into the plane |
| 20 | //! (`omega_gpu_decode_attn_api.h`, `OmegaGpuDecodeAttnCacheView`). The two calls here are the |
| 21 | //! header's test helpers: `cache_read` returns one K row and one V row, `cache_write` stores |
| 22 | //! `count` consecutive rows of one layer and KV head in one sync. Both take omega's session lock |
| 23 | //! and sync the layer between host and chip, so they must not run while a launch on this cache |
| 24 | //! is in flight (the engine calls them between generations, under the hive's GPU lock). |
| 25 | //! Without the native library both return `Unavailable`. |
| 26 | use super::{bad, check_len, DecodeAttnCache, HEAD_DIM, KV_HEADS}; |
| 27 | use aien_omega_gpu::OmegaGpuError; |
| 28 | |
| 29 | impl DecodeAttnCache { |
| 30 | fn check_row( |
| 31 | &self, |
| 32 | layer: usize, |
| 33 | kv_head: usize, |
| 34 | pos: usize, |
| 35 | count: usize, |
| 36 | ) -> Result<(), OmegaGpuError> { |
| 37 | if layer >= self.layers |
| 38 | || kv_head >= KV_HEADS |
| 39 | || count == 0 |
| 40 | || pos >= self.max_pos |
| 41 | || count > self.max_pos - pos |
| 42 | { |
| 43 | return Err(bad(format!( |
| 44 | "decode attn rows layer {layer} head {kv_head} pos {pos} count {count} outside {} layers x {KV_HEADS} heads x {} positions", |
| 45 | self.layers, self.max_pos |
| 46 | ))); |
| 47 | } |
| 48 | Ok(()) |
| 49 | } |
| 50 | |
| 51 | /// Copies the bf16 K and V rows of (`layer`, `kv_head`, `pos`) into `k` and `v` |
| 52 | /// ([`HEAD_DIM`] values each, bf16 bit patterns). |
| 53 | pub fn read_row( |
| 54 | &mut self, |
| 55 | layer: usize, |
| 56 | kv_head: usize, |
| 57 | pos: usize, |
| 58 | k: &mut [u16], |
| 59 | v: &mut [u16], |
| 60 | ) -> Result<(), OmegaGpuError> { |
| 61 | check_len("decode attn read k", HEAD_DIM, k.len())?; |
| 62 | check_len("decode attn read v", HEAD_DIM, v.len())?; |
| 63 | self.check_row(layer, kv_head, pos, 1)?; |
| 64 | #[cfg(has_omega_dattn)] |
| 65 | { |
| 66 | // SAFETY: ranges and lengths checked above; the cache is live for &mut self. |
| 67 | let rc = unsafe { |
| 68 | super::ffi::omega_gpu_decode_attn_cache_read( |
| 69 | self.ptr, |
| 70 | layer as u32, |
| 71 | kv_head as u32, |
| 72 | pos as u32, |
| 73 | k.as_mut_ptr(), |
| 74 | v.as_mut_ptr(), |
| 75 | ) |
| 76 | }; |
| 77 | if rc != 0 { |
| 78 | return Err(super::rc_err(rc)); |
| 79 | } |
| 80 | Ok(()) |
| 81 | } |
| 82 | #[cfg(not(has_omega_dattn))] |
| 83 | { |
| 84 | Err(OmegaGpuError::Unavailable) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Stores `k.len() / HEAD_DIM` consecutive bf16 K rows and as many V rows of (`layer`, |
| 89 | /// `kv_head`) starting at position `pos`, in one sync. |
| 90 | pub fn write_rows( |
| 91 | &mut self, |
| 92 | layer: usize, |
| 93 | kv_head: usize, |
| 94 | pos: usize, |
| 95 | k: &[u16], |
| 96 | v: &[u16], |
| 97 | ) -> Result<(), OmegaGpuError> { |
| 98 | if k.is_empty() || !k.len().is_multiple_of(HEAD_DIM) { |
| 99 | return Err(bad(format!( |
| 100 | "decode attn write: {} K values is not a whole number of {HEAD_DIM}-wide rows", |
| 101 | k.len() |
| 102 | ))); |
| 103 | } |
| 104 | check_len("decode attn write v", k.len(), v.len())?; |
| 105 | let count = k.len() / HEAD_DIM; |
| 106 | self.check_row(layer, kv_head, pos, count)?; |
| 107 | #[cfg(has_omega_dattn)] |
| 108 | { |
| 109 | // SAFETY: ranges and lengths checked above; the cache is live for &mut self. |
| 110 | let rc = unsafe { |
| 111 | super::ffi::omega_gpu_decode_attn_cache_write( |
| 112 | self.ptr, |
| 113 | layer as u32, |
| 114 | kv_head as u32, |
| 115 | pos as u32, |
| 116 | count as u32, |
| 117 | k.as_ptr(), |
| 118 | v.as_ptr(), |
| 119 | ) |
| 120 | }; |
| 121 | if rc != 0 { |
| 122 | return Err(super::rc_err(rc)); |
| 123 | } |
| 124 | Ok(()) |
| 125 | } |
| 126 | #[cfg(not(has_omega_dattn))] |
| 127 | { |
| 128 | Err(OmegaGpuError::Unavailable) |
| 129 | } |
| 130 | } |
| 131 | } |