Download src/grid_context.rs from VoidOSS/Cosmic-v0.6-1B-MoE-Preview: direct link, hf CLI and curl.
- Browser
- Download file 2.9 kB
-
https://huggingface.co/VoidOSS/Cosmic-v0.6-1B-MoE-Preview/resolve/main/src/grid_context.rs
- Command line
-
hf download hf://VoidOSS/Cosmic-v0.6-1B-MoE-Preview/src/grid_context.rs
-
curl -L -o grid_context.rs https://huggingface.co/VoidOSS/Cosmic-v0.6-1B-MoE-Preview/resolve/main/src/grid_context.rs
2.9 kB
| //! 2D Spreadsheet / Grid Context (`grid_context.rs`). | |
| //! Replaces flat 1D context tapes with a structured 2D Grid of Cells `(ROWS x COLS)`. | |
| //! Rows = ordered list items / events; Columns = structured attributes/facets. | |
| //! The learned core decides which `(row, col)` cell to write when a token flushes | |
| //! and which `(row, col)` cell to read back in O(1) time. | |
| pub const GRID_ROWS: usize = 8; | |
| pub const GRID_COLS: usize = 4; | |
| pub const GRID_CELLS: usize = GRID_ROWS * GRID_COLS; // 32 structured cells | |
| pub struct GridContext { | |
| pub cell_dim: usize, | |
| pub cells: Vec<f32>, // Flat [GRID_ROWS * GRID_COLS * cell_dim] | |
| pub row_energy: [f32; GRID_ROWS], | |
| } | |
| impl GridContext { | |
| pub fn new(cell_dim: usize) -> Self { | |
| Self { | |
| cell_dim, | |
| cells: vec![0.0; GRID_CELLS * cell_dim], | |
| row_energy: [0.0; GRID_ROWS], | |
| } | |
| } | |
| pub fn reset(&mut self) { | |
| self.cells.fill(0.0); | |
| self.row_energy = [0.0; GRID_ROWS]; | |
| } | |
| pub fn cell_slice(&self, row: usize, col: usize) -> &[f32] { | |
| let start = (row * GRID_COLS + col) * self.cell_dim; | |
| &self.cells[start..start + self.cell_dim] | |
| } | |
| /// Differentiable soft 2D write: updates cells weighted by learned row & col probabilities. | |
| /// Keeps working memory bounded forever regardless of sequence length! | |
| pub fn write_soft( | |
| &mut self, | |
| row_probs: &[f32; GRID_ROWS], | |
| col_probs: &[f32; GRID_COLS], | |
| write_strength: f32, | |
| val: &[f32], | |
| ) { | |
| let d = self.cell_dim; | |
| for r in 0..GRID_ROWS { | |
| let rp = row_probs[r]; | |
| let mut r_norm = 0.0f32; | |
| for c in 0..GRID_COLS { | |
| let w = (rp * col_probs[c] * write_strength).clamp(0.0, 0.95); | |
| let keep = 1.0 - w; | |
| let base = (r * GRID_COLS + c) * d; | |
| for i in 0..d { | |
| let nv = keep * self.cells[base + i] + w * val[i]; | |
| self.cells[base + i] = nv; | |
| r_norm += nv * nv; | |
| } | |
| } | |
| self.row_energy[r] = (r_norm / ((GRID_COLS * d) as f32)).sqrt(); | |
| } | |
| } | |
| /// Differentiable soft 2D read: pulls a focused cell vector from `(row_probs, col_probs)`. | |
| pub fn read_soft( | |
| &self, | |
| row_probs: &[f32; GRID_ROWS], | |
| col_probs: &[f32; GRID_COLS], | |
| out: &mut [f32], | |
| ) { | |
| let d = self.cell_dim; | |
| out.fill(0.0); | |
| for r in 0..GRID_ROWS { | |
| let rp = row_probs[r]; | |
| for c in 0..GRID_COLS { | |
| let w = rp * col_probs[c]; | |
| if w < 1e-6 { continue; } | |
| let base = (r * GRID_COLS + c) * d; | |
| for i in 0..d { | |
| out[i] += w * self.cells[base + i]; | |
| } | |
| } | |
| } | |
| } | |
| } | |