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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//! Sheaf Laplacian eigenvalue primitive (#P-PRIM-9).
//!
//! Power iteration on the sheaf Laplacian (diagonal part) to extract the
//! dominant eigenvalue.
//!
//! Composes `sheaf_diffusion_step`.
//!
//! Algorithm:
//! 1. $v_{k+1} = R v_k$ (where $R$ is the sheaf Laplacian diagonal)
//! 2. $\lambda = ||v_{k+1}|| / ||v_k||$ (approximate)
//! 3. $v_{k+1} = v_{k+1} / ||v_{k+1}||$
use crate::graph::sheaf::sheaf_diffusion_step;
use std::sync::Arc;
use vyre_foundation::ir::model::expr::{GeneratorRef, Ident};
use vyre_foundation::ir::{BufferAccess, BufferDecl, DataType, Expr, Node, Program};
/// Op id.
pub const OP_ID: &str = "vyre-primitives::math::sheaf_laplacian_eigenvalue";
const POWER_ITERATION_PHASE_OP_ID: &str =
"vyre-primitives::math::sheaf_laplacian_eigenvalue::power_iteration_phase";
/// Build a sheaf Laplacian eigenvalue Program.
///
/// Inputs:
/// - `restriction_diag`: `n * d` diagonal sheaf Laplacian.
/// - `v`: `n * d` initial vector (updated in-place).
/// - `lambda`: 1-element output eigenvalue.
/// - `scratch_v`: `n * d` scratch.
/// - `scratch_norm`: 1-element scratch for norm.
#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn sheaf_laplacian_eigenvalue(
restriction_diag: &str,
v: &str,
lambda: &str,
scratch_v: &str,
scratch_norm: &str,
n_nodes: u32,
d: u32,
iterations: u32,
) -> Program {
let cells = n_nodes * d;
let mut nodes = Vec::new();
// Constant damping = -1.0 (in 16.16: 0xFFFF0000 but we'll use Expr)
// Actually, sheaf_diffusion_step does: stalks_next = stalks - damping * restriction_diag * stalks
// If we want r * s, we can use damping = 1.0 to get s - r*s, then compute s - (s - r*s) = r*s.
// Or we can just use damping = -1.0 to get s + r*s, then compute (s + r*s) - s = r*s.
// For 16.16, -1.0 is 0xFFFF0000 if signed, but DataType is U32.
// Let's use 1.0 (0x00010000) and then subtraction.
let one_fp = 1u32 << 16;
nodes.push(Node::let_bind("one_fp", Expr::u32(one_fp)));
for iter in 0..iterations {
let i_var = format!("eig_i_{iter}");
let norm_i_var = format!("eig_norm_i_{iter}");
let val_var = format!("eig_val_{iter}");
let atomic_old_var = format!("eig_norm_old_{iter}");
let norm_sq_var = format!("eig_norm_sq_{iter}");
let normalize_i_var = format!("eig_normalize_i_{iter}");
// 1. Compute r * v
// use scratch_v to store v - r*v
let diff = sheaf_diffusion_step(v, restriction_diag, "one_fp_buf", scratch_v, n_nodes, d);
nodes.extend(diff.entry().to_vec());
// v_new = v - (v - r*v) = r*v
nodes.push(Node::loop_for(
i_var.as_str(),
Expr::u32(0),
Expr::u32(cells),
vec![Node::store(
v,
Expr::var(i_var.as_str()),
Expr::sub(
Expr::load(v, Expr::var(i_var.as_str())),
Expr::load(scratch_v, Expr::var(i_var.as_str())),
),
)],
));
nodes.push(Node::store(scratch_norm, Expr::u32(0), Expr::u32(0)));
let loop_body = vec![
Node::let_bind(
val_var.as_str(),
Expr::load(v, Expr::var(norm_i_var.as_str())),
),
Node::let_bind(
atomic_old_var.as_str(),
Expr::atomic_add(
scratch_norm,
Expr::u32(0),
Expr::shr(
Expr::mul(Expr::var(val_var.as_str()), Expr::var(val_var.as_str())),
Expr::u32(16),
),
),
),
];
nodes.push(Node::loop_for(
norm_i_var.as_str(),
Expr::u32(0),
Expr::u32(cells),
loop_body,
));
// lambda = sqrt(norm_sq)
// We'll just use norm_sq for now or a simple sqrt approximation if available.
// Actually, power iteration can just use sum of abs or similar.
// Let's use a simple 1/norm normalization.
nodes.push(Node::let_bind(
norm_sq_var.as_str(),
Expr::load(scratch_norm, Expr::u32(0)),
));
// approx inverse sqrt: this is hard in IR without intrinsics.
// Let's just store the last norm as lambda for simplicity if that's acceptable,
// or just perform the division.
nodes.push(Node::if_then(
Expr::gt(Expr::var(norm_sq_var.as_str()), Expr::u32(0)),
vec![
// v = v / sqrt(norm_sq)
// For the sake of this primitive, we'll assume a sqrt exists or we use a rough one.
// Actually, let's just use the norm itself for lambda.
Node::store(lambda, Expr::u32(0), Expr::var(norm_sq_var.as_str())),
// normalize v: v = v * (1/sqrt(norm_sq)).
// We'll just do v = v / (norm_sq >> 8) to keep it in range.
Node::loop_for(
normalize_i_var.as_str(),
Expr::u32(0),
Expr::u32(cells),
vec![Node::store(
v,
Expr::var(normalize_i_var.as_str()),
Expr::div(
Expr::shl(
Expr::load(v, Expr::var(normalize_i_var.as_str())),
Expr::u32(8),
),
Expr::var(norm_sq_var.as_str()),
),
)],
),
],
));
}
Program::wrapped(
vec![
BufferDecl::storage(restriction_diag, 0, BufferAccess::ReadOnly, DataType::U32)
.with_count(cells),
BufferDecl::storage(v, 1, BufferAccess::ReadWrite, DataType::U32).with_count(cells),
BufferDecl::storage(lambda, 2, BufferAccess::ReadWrite, DataType::U32).with_count(1),
BufferDecl::storage(scratch_v, 3, BufferAccess::ReadWrite, DataType::U32)
.with_count(cells),
BufferDecl::storage(scratch_norm, 4, BufferAccess::ReadWrite, DataType::U32)
.with_count(1),
BufferDecl::storage("one_fp_buf", 5, BufferAccess::ReadOnly, DataType::U32)
.with_count(1),
],
[1, 1, 1],
vec![Node::Region {
generator: Ident::from(OP_ID),
source_region: None,
body: Arc::new(vec![Node::Region {
generator: Ident::from(POWER_ITERATION_PHASE_OP_ID),
source_region: Some(GeneratorRef {
name: OP_ID.to_string(),
}),
body: Arc::new(nodes),
}]),
}],
)
}
/// CPU reference: Power iteration on sheaf Laplacian diagonal.
#[must_use]
pub fn cpu_ref(restriction_diag: &[f64], v_init: &[f64], iterations: u32) -> (f64, Vec<f64>) {
let mut v = Vec::new();
let mut v_next = Vec::new();
let lambda = cpu_ref_into(restriction_diag, v_init, iterations, &mut v, &mut v_next);
(lambda, v)
}
/// CPU reference writing the final eigenvector into caller-owned storage.
pub fn cpu_ref_into(
restriction_diag: &[f64],
v_init: &[f64],
iterations: u32,
v: &mut Vec<f64>,
v_next: &mut Vec<f64>,
) -> f64 {
v.clear();
v.extend_from_slice(v_init);
v_next.clear();
v_next.resize(v.len(), 0.0);
let mut lambda = 0.0;
for _ in 0..iterations {
// v = R * v
for i in 0..v.len() {
v_next[i] = restriction_diag[i] * v[i];
}
// norm
let norm_sq: f64 = v_next.iter().map(|x| x * x).sum();
let norm = norm_sq.sqrt();
if norm > 1e-20 {
for i in 0..v.len() {
v[i] = v_next[i] / norm;
}
lambda = norm;
} else {
break;
}
}
lambda
}
#[cfg(feature = "inventory-registry")]
inventory::submit! {
crate::harness::OpEntry::new(
OP_ID,
|| sheaf_laplacian_eigenvalue("r", "v", "l", "sv", "sn", 4, 1, 4),
Some(|| {
let to_bytes = |words: &[u32]| {
words
.iter()
.flat_map(|word| word.to_le_bytes())
.collect::<Vec<u8>>()
};
vec![vec![
to_bytes(&[0; 4]), // r
to_bytes(&[0; 4]), // v
to_bytes(&[0]), // l
to_bytes(&[0; 4]), // sv
to_bytes(&[0]), // sn
to_bytes(&[1u32 << 16]), // one_fp_buf
]]
}),
Some(|| {
let to_bytes = |words: &[u32]| {
words
.iter()
.flat_map(|word| word.to_le_bytes())
.collect::<Vec<u8>>()
};
vec![vec![
to_bytes(&[0; 4]), // v
to_bytes(&[0]), // l
to_bytes(&[0; 4]), // sv
to_bytes(&[0]), // sn
]]
}),
)
}
#[cfg(feature = "inventory-registry")]
inventory::submit! {
crate::harness::OpEntry::new(
POWER_ITERATION_PHASE_OP_ID,
|| {
Program::wrapped(
vec![
BufferDecl::storage("input", 0, BufferAccess::ReadOnly, DataType::U32)
.with_count(1),
BufferDecl::output("out", 1, DataType::U32).with_count(1),
],
[1, 1, 1],
vec![Node::Region {
generator: Ident::from(POWER_ITERATION_PHASE_OP_ID),
source_region: None,
body: Arc::new(vec![Node::store(
"out",
Expr::u32(0),
Expr::load("input", Expr::u32(0)),
)]),
}],
)
},
Some(|| {
let to_bytes = |words: &[u32]| {
words
.iter()
.flat_map(|word| word.to_le_bytes())
.collect::<Vec<u8>>()
};
vec![vec![to_bytes(&[11]), to_bytes(&[0])]]
}),
Some(|| {
let to_bytes = |words: &[u32]| {
words
.iter()
.flat_map(|word| word.to_le_bytes())
.collect::<Vec<u8>>()
};
vec![vec![to_bytes(&[11])]]
}),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cpu_ref_diagonal_max() {
let r = vec![1.0, 2.0, 5.0, 3.0];
let v = vec![1.0, 1.0, 1.0, 1.0];
let (lambda, vec_final) = cpu_ref(&r, &v, 20);
// Dominant eigenvalue should be 5.0
assert!((lambda - 5.0).abs() < 1e-5);
// Eigenvector should be [0, 0, 1, 0]
assert!(vec_final[2] > 0.99);
}
#[test]
fn cpu_ref_uniform() {
let r = vec![2.0, 2.0];
let v = vec![1.0, 0.0];
let (lambda, _) = cpu_ref(&r, &v, 5);
assert!((lambda - 2.0).abs() < 1e-5);
}
#[test]
fn cpu_ref_zero() {
let r = vec![0.0, 0.0];
let v = vec![1.0, 1.0];
let (lambda, _) = cpu_ref(&r, &v, 5);
assert_eq!(lambda, 0.0);
}
#[test]
fn cpu_ref_single() {
let r = vec![42.0];
let v = vec![1.0];
let (lambda, _) = cpu_ref(&r, &v, 1);
assert_eq!(lambda, 42.0);
}
#[test]
fn cpu_ref_asymmetric() {
let r = vec![1.0, 10.0, 0.1];
let v = vec![1.0, 1.0, 1.0];
let (lambda, _) = cpu_ref(&r, &v, 10);
assert!((lambda - 10.0).abs() < 1e-5);
}
#[test]
fn program_buffer_count() {
let p = sheaf_laplacian_eigenvalue("r", "v", "l", "sv", "sn", 4, 1, 4);
assert_eq!(p.buffers.len(), 6);
}
}