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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! Kernel specification — what the runtime executes.
//!
//! Fate's forward pass produces logits. KernelSpec interprets those logits
//! as a structured kernel specification: which dimensions to preserve,
//! what decomposition to apply, what precision floor.
//!
//! Always available (no feature gate). The `lapack` feature controls
//! whether dispatch goes to Fortran or Rust — the spec is just a type.
use crate::Precision;
/// Which decomposition the kernel should apply.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Decomposition {
/// Eigenvalue decomposition (dsyev). For symmetric/spectral data.
Eigenvalue,
/// Singular value decomposition (dgesvd). For general matrices.
Svd,
/// Matrix-vector multiply only. Cheapest path.
MatVec,
/// Full projection: preview + modify + review cycle.
FullProjection,
}
/// The specification that Fate's routing produces.
/// Determines what the runtime executes — Fortran or Rust.
#[derive(Clone, Debug)]
pub struct KernelSpec {
/// Which dimensions to preserve during transport.
/// Derived from logits: dimensions where activation > threshold.
pub dimensions: Vec<usize>,
/// Which decomposition to apply.
pub decomposition: Decomposition,
/// Precision floor — eigenvalues below this are zeroed.
pub precision: Precision,
}
impl KernelSpec {
/// Create a kernel spec with explicit dimensions.
pub fn new(dimensions: Vec<usize>, decomposition: Decomposition, precision: Precision) -> Self {
KernelSpec {
dimensions,
decomposition,
precision,
}
}
/// Construct from logits: dimensions where logit > threshold are preserved.
/// The logits ARE the dimension selector.
pub fn from_logits(
logits: &[f64],
threshold: f64,
decomposition: Decomposition,
precision: Precision,
) -> Self {
let dimensions: Vec<usize> = logits
.iter()
.enumerate()
.filter(|(_, &l)| l > threshold)
.map(|(i, _)| i)
.collect();
// Guard: never produce empty dimensions. Keep the best one.
let dimensions = if dimensions.is_empty() && !logits.is_empty() {
let best = logits
.iter()
.enumerate()
.max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
.map(|(i, _)| i)
.unwrap();
vec![best]
} else {
dimensions
};
KernelSpec {
dimensions,
decomposition,
precision,
}
}
/// Number of preserved dimensions.
pub fn rank(&self) -> usize {
self.dimensions.len()
}
/// Build a diagonal projection matrix (n×n, row-major) that preserves
/// only the specified dimensions. Everything else is zeroed.
pub fn projection_matrix(&self, n: usize) -> Vec<f64> {
let mut matrix = vec![0.0f64; n * n];
for &d in &self.dimensions {
if d < n {
matrix[d * n + d] = 1.0;
}
}
matrix
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn kernel_spec_from_dimensions() {
let spec = KernelSpec::new(
vec![0, 2, 4, 6],
Decomposition::Eigenvalue,
Precision::new(0.01),
);
assert_eq!(spec.dimensions.len(), 4);
assert_eq!(spec.decomposition, Decomposition::Eigenvalue);
assert_eq!(spec.rank(), 4);
}
#[test]
fn kernel_spec_from_logits_filters_by_threshold() {
let logits = [
1.0, -0.5, 2.0, -1.0, 0.3, -0.2, 1.5, -0.8, 0.1, -0.3, 0.8, -0.1, 0.5, -0.4, 1.2, -0.6,
];
let spec = KernelSpec::from_logits(
&logits,
0.0,
Decomposition::Eigenvalue,
Precision::new(0.01),
);
// Positive logits at indices: 0, 2, 4, 6, 8, 10, 12, 14
assert_eq!(spec.dimensions, vec![0, 2, 4, 6, 8, 10, 12, 14]);
assert_eq!(spec.rank(), 8);
}
#[test]
fn kernel_spec_from_logits_high_threshold() {
let logits = [
1.0, -0.5, 2.0, -1.0, 0.3, -0.2, 1.5, -0.8, 0.1, -0.3, 0.8, -0.1, 0.5, -0.4, 1.2, -0.6,
];
let spec = KernelSpec::from_logits(&logits, 1.0, Decomposition::Svd, Precision::new(0.1));
// Logits > 1.0: indices 2 (2.0), 6 (1.5), 14 (1.2)
assert_eq!(spec.dimensions, vec![2, 6, 14]);
assert_eq!(spec.decomposition, Decomposition::Svd);
}
#[test]
fn projection_matrix_diagonal() {
let spec = KernelSpec::new(vec![0, 2], Decomposition::MatVec, Precision::new(0.01));
let matrix = spec.projection_matrix(4);
// 4×4 matrix, 1s at (0,0) and (2,2)
assert_eq!(matrix[0 * 4 + 0], 1.0); // (0,0)
assert_eq!(matrix[1 * 4 + 1], 0.0); // (1,1) not preserved
assert_eq!(matrix[2 * 4 + 2], 1.0); // (2,2)
assert_eq!(matrix[3 * 4 + 3], 0.0); // (3,3) not preserved
}
#[test]
fn projection_matrix_out_of_bounds_ignored() {
let spec = KernelSpec::new(
vec![0, 5], // 5 is out of bounds for n=4
Decomposition::MatVec,
Precision::new(0.01),
);
let matrix = spec.projection_matrix(4);
assert_eq!(matrix[0 * 4 + 0], 1.0);
// Index 5 silently ignored (d < n check)
let sum: f64 = matrix.iter().sum();
assert_eq!(sum, 1.0); // Only one 1.0 in the matrix
}
#[test]
fn empty_logits_empty_spec() {
let spec =
KernelSpec::from_logits(&[], 0.0, Decomposition::Eigenvalue, Precision::new(0.01));
assert_eq!(spec.rank(), 0);
assert!(spec.dimensions.is_empty());
}
#[test]
fn from_logits_never_empty() {
let logits = [-1.0, -2.0, -3.0, -4.0];
let spec = KernelSpec::from_logits(
&logits,
0.0,
Decomposition::Eigenvalue,
Precision::new(0.01),
);
assert_eq!(spec.rank(), 1); // keeps the best one (-1.0 at index 0)
assert_eq!(spec.dimensions, vec![0]);
}
}