File size: 2,901 Bytes
fdb3040
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! 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

#[derive(Clone)]
pub struct GridContext {
    pub cell_dim: usize,
    pub cells: Vec<f32>,      // Flat [GRID_ROWS * GRID_COLS * cell_dim]
    pub row_energy: [f32; GRID_ROWS],
}

#[allow(dead_code)]
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];
    }

    #[inline]
    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];
                }
            }
        }
    }
}