vyre-self-substrate 0.4.1

Vyre self-substrate: vyre using its own primitives on its own scheduler problems. The recursion-thesis layer between vyre-primitives and vyre-driver.
Documentation
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
//! Dispatch-graph clustering via #2 sinkhorn (#30 substrate).
//!
//! This module implements the clustering of vyre's dispatch graph into
//! fusion-coherent groups using entropic optimal transport (Sinkhorn).
//!
//! # Math Frontier #2 entry
//!
//! "sinkhorn — dispatch-graph clustering via Sinkhorn-OT distance between
//! cost-vector distributions."
//!
//! # Transport Problem
//!
//! We model the clustering as an Optimal Transport problem between:
//! 1. The distribution of Regions (each with a weight a_i, e.g. compute cost).
//! 2. The distribution of Cluster capacities (b_j, e.g. target partition sizes).
//!
//! The cost matrix C_ij represents the "fusion distance" between Region i
//! and Cluster centroid j.
//!
//! # GPU Implementation
//!
//! This is a pure-math, GPU-resident implementation. It does not require
//! host-side iterations. It chains the Sinkhorn update steps directly
//! within the IR Program.

use std::sync::Arc;
use vyre_foundation::ir::model::expr::Ident;
use vyre_foundation::ir::{BufferAccess, BufferDecl, DataType, Expr, Node, Program};

/// Op id for the Sinkhorn dispatch clustering primitive.
pub const OP_ID: &str = "vyre-libs::self_substrate::sinkhorn_dispatch_clustering";

/// Emit a Program that clusters `m` regions into `n` clusters.
///
/// Features:
/// - `region_features`: (m x d) buffer of f32 features.
/// - `cluster_centroids`: (n x d) buffer of f32 centroids.
/// - `region_weights`: (m) buffer of f32 masses.
/// - `cluster_capacities`: (n) buffer of f32 target masses.
/// - `out_assignments`: (m) buffer of u32 cluster indices.
///
/// Parameters:
/// - `eps`: Entropy regularization parameter.
/// - `iters`: Number of Sinkhorn iterations.
#[must_use]
#[allow(clippy::vec_init_then_push)]
pub fn sinkhorn_clustering_program(m: u32, n: u32, d: u32, iters: u32, eps: f32) -> Program {
    use crate::observability::{bump, sinkhorn_dispatch_clustering_calls};
    bump(&sinkhorn_dispatch_clustering_calls);
    assert!(m > 0 && n > 0 && d > 0 && iters > 0);

    // We use one workgroup to cluster all regions.
    // Each thread handles some regions.
    let workgroup_size = 256;
    let gid = Expr::gid_x();

    // Intermediate buffers for Sinkhorn vectors u (size m) and v (size n).
    // In a real production substrate, these might be scratchpad / shared memory.
    // For this primitive, we use dedicated internal buffers.

    let mut body = vec![];

    // 1. Initialize v = 1.0
    body.push(Node::if_then(
        Expr::lt(gid.clone(), Expr::u32(n)),
        vec![Node::store("v", gid.clone(), Expr::f32(1.0))],
    ));
    body.push(Node::barrier());

    // 2. Sinkhorn Loop
    body.push(Node::loop_for(
        "it",
        Expr::u32(0),
        Expr::u32(iters),
        vec![
            // u_i = a_i / sum_j (K_ij * v_j)
            Node::if_then(
                Expr::lt(gid.clone(), Expr::u32(m)),
                vec![
                    Node::let_bind("kv_sum", Expr::f32(0.0)),
                    Node::loop_for(
                        "jj",
                        Expr::u32(0),
                        Expr::u32(n),
                        vec![
                            // Compute C_ij = sum_k (f_ik - g_jk)^2
                            Node::let_bind("cost_ij", Expr::f32(0.0)),
                            Node::loop_for(
                                "kk",
                                Expr::u32(0),
                                Expr::u32(d),
                                vec![
                                    Node::let_bind(
                                        "f_ik",
                                        Expr::load(
                                            "region_features",
                                            Expr::add(
                                                Expr::mul(gid.clone(), Expr::u32(d)),
                                                Expr::var("kk"),
                                            ),
                                        ),
                                    ),
                                    Node::let_bind(
                                        "g_jk",
                                        Expr::load(
                                            "cluster_centroids",
                                            Expr::add(
                                                Expr::mul(Expr::var("jj"), Expr::u32(d)),
                                                Expr::var("kk"),
                                            ),
                                        ),
                                    ),
                                    Node::let_bind(
                                        "diff",
                                        Expr::sub(Expr::var("f_ik"), Expr::var("g_jk")),
                                    ),
                                    Node::assign(
                                        "cost_ij",
                                        Expr::add(
                                            Expr::var("cost_ij"),
                                            Expr::mul(Expr::var("diff"), Expr::var("diff")),
                                        ),
                                    ),
                                ],
                            ),
                            // K_ij = exp(-cost_ij / eps)
                            Node::let_bind(
                                "k_ij",
                                Expr::call(
                                    "exp",
                                    vec![Expr::div(
                                        Expr::negate(Expr::var("cost_ij")),
                                        Expr::f32(eps),
                                    )],
                                ),
                            ),
                            Node::assign(
                                "kv_sum",
                                Expr::add(
                                    Expr::var("kv_sum"),
                                    Expr::mul(Expr::var("k_ij"), Expr::load("v", Expr::var("jj"))),
                                ),
                            ),
                        ],
                    ),
                    Node::store(
                        "u",
                        gid.clone(),
                        Expr::div(
                            Expr::load("region_weights", gid.clone()),
                            Expr::max(Expr::var("kv_sum"), Expr::f32(1e-10)),
                        ),
                    ),
                ],
            ),
            Node::barrier(),
            // v_j = b_j / sum_i (K_ij * u_i)
            Node::if_then(
                Expr::lt(gid.clone(), Expr::u32(n)),
                vec![
                    Node::let_bind("ku_sum", Expr::f32(0.0)),
                    Node::loop_for(
                        "ii",
                        Expr::u32(0),
                        Expr::u32(m),
                        vec![
                            // Recompute K_ij (to save memory; in production we might cache K if m*n is small)
                            Node::let_bind("cost_ij_rev", Expr::f32(0.0)),
                            Node::loop_for(
                                "kk_rev",
                                Expr::u32(0),
                                Expr::u32(d),
                                vec![
                                    Node::let_bind(
                                        "f_ik_rev",
                                        Expr::load(
                                            "region_features",
                                            Expr::add(
                                                Expr::mul(Expr::var("ii"), Expr::u32(d)),
                                                Expr::var("kk_rev"),
                                            ),
                                        ),
                                    ),
                                    Node::let_bind(
                                        "g_jk_rev",
                                        Expr::load(
                                            "cluster_centroids",
                                            Expr::add(
                                                Expr::mul(gid.clone(), Expr::u32(d)),
                                                Expr::var("kk_rev"),
                                            ),
                                        ),
                                    ),
                                    Node::let_bind(
                                        "diff_rev",
                                        Expr::sub(Expr::var("f_ik_rev"), Expr::var("g_jk_rev")),
                                    ),
                                    Node::assign(
                                        "cost_ij_rev",
                                        Expr::add(
                                            Expr::var("cost_ij_rev"),
                                            Expr::mul(Expr::var("diff_rev"), Expr::var("diff_rev")),
                                        ),
                                    ),
                                ],
                            ),
                            Node::let_bind(
                                "k_ij_rev",
                                Expr::call(
                                    "exp",
                                    vec![Expr::div(
                                        Expr::negate(Expr::var("cost_ij_rev")),
                                        Expr::f32(eps),
                                    )],
                                ),
                            ),
                            Node::assign(
                                "ku_sum",
                                Expr::add(
                                    Expr::var("ku_sum"),
                                    Expr::mul(
                                        Expr::var("k_ij_rev"),
                                        Expr::load("u", Expr::var("ii")),
                                    ),
                                ),
                            ),
                        ],
                    ),
                    Node::store(
                        "v",
                        gid.clone(),
                        Expr::div(
                            Expr::load("cluster_capacities", gid.clone()),
                            Expr::max(Expr::var("ku_sum"), Expr::f32(1e-10)),
                        ),
                    ),
                ],
            ),
            Node::barrier(),
        ],
    ));

    // 3. Final assignment: argmax_j (K_ij * v_j)
    body.push(Node::if_then(
        Expr::lt(gid.clone(), Expr::u32(m)),
        vec![
            Node::let_bind("best_j", Expr::u32(0)),
            Node::let_bind("max_val", Expr::f32(-1.0)),
            Node::loop_for(
                "jj_final",
                Expr::u32(0),
                Expr::u32(n),
                vec![
                    Node::let_bind("cost_ij_final", Expr::f32(0.0)),
                    Node::loop_for(
                        "kk_final",
                        Expr::u32(0),
                        Expr::u32(d),
                        vec![
                            Node::let_bind(
                                "f_ik_final",
                                Expr::load(
                                    "region_features",
                                    Expr::add(
                                        Expr::mul(gid.clone(), Expr::u32(d)),
                                        Expr::var("kk_final"),
                                    ),
                                ),
                            ),
                            Node::let_bind(
                                "g_jk_final",
                                Expr::load(
                                    "cluster_centroids",
                                    Expr::add(
                                        Expr::mul(Expr::var("jj_final"), Expr::u32(d)),
                                        Expr::var("kk_final"),
                                    ),
                                ),
                            ),
                            Node::let_bind(
                                "diff_final",
                                Expr::sub(Expr::var("f_ik_final"), Expr::var("g_jk_final")),
                            ),
                            Node::assign(
                                "cost_ij_final",
                                Expr::add(
                                    Expr::var("cost_ij_final"),
                                    Expr::mul(Expr::var("diff_final"), Expr::var("diff_final")),
                                ),
                            ),
                        ],
                    ),
                    Node::let_bind(
                        "k_ij_final",
                        Expr::call(
                            "exp",
                            vec![Expr::div(
                                Expr::negate(Expr::var("cost_ij_final")),
                                Expr::f32(eps),
                            )],
                        ),
                    ),
                    Node::let_bind(
                        "val",
                        Expr::mul(
                            Expr::var("k_ij_final"),
                            Expr::load("v", Expr::var("jj_final")),
                        ),
                    ),
                    Node::if_then(
                        Expr::gt(Expr::var("val"), Expr::var("max_val")),
                        vec![
                            Node::assign("max_val", Expr::var("val")),
                            Node::assign("best_j", Expr::var("jj_final")),
                        ],
                    ),
                ],
            ),
            Node::store("out_assignments", gid.clone(), Expr::var("best_j")),
        ],
    ));

    Program::wrapped(
        vec![
            BufferDecl::storage("region_features", 0, BufferAccess::ReadOnly, DataType::F32)
                .with_count(m.saturating_mul(d)),
            BufferDecl::storage(
                "cluster_centroids",
                1,
                BufferAccess::ReadOnly,
                DataType::F32,
            )
            .with_count(n.saturating_mul(d)),
            BufferDecl::storage("region_weights", 2, BufferAccess::ReadOnly, DataType::F32)
                .with_count(m),
            BufferDecl::storage(
                "cluster_capacities",
                3,
                BufferAccess::ReadOnly,
                DataType::F32,
            )
            .with_count(n),
            BufferDecl::storage("u", 4, BufferAccess::ReadWrite, DataType::F32).with_count(m),
            BufferDecl::storage("v", 5, BufferAccess::ReadWrite, DataType::F32).with_count(n),
            BufferDecl::output("out_assignments", 6, DataType::U32).with_count(m),
        ],
        [workgroup_size, 1, 1],
        vec![Node::Region {
            generator: Ident::from(OP_ID),
            source_region: None,
            body: Arc::new(body),
        }],
    )
}

/// Reusable buffers for [`sinkhorn_clustering_cpu_into`].
#[derive(Debug, Default)]
pub struct SinkhornClusteringScratch {
    u: Vec<f32>,
    v: Vec<f32>,
    kernel: Vec<f32>,
    assignments: Vec<u32>,
}

impl SinkhornClusteringScratch {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    fn prepare(&mut self, m: usize, n: usize) {
        self.u.clear();
        self.u.resize(m, 1.0);
        self.v.clear();
        self.v.resize(n, 1.0);
        self.kernel.clear();
        self.kernel.resize(m.saturating_mul(n), 0.0);
        self.assignments.clear();
        self.assignments.resize(m, 0);
    }

    #[cfg(test)]
    fn assignment_ptr(&self) -> *const u32 {
        self.assignments.as_ptr()
    }
}

/// CPU reference implementation of Sinkhorn clustering using caller-owned scratch.
#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn sinkhorn_clustering_cpu_into<'a>(
    region_features: &[f32],    // m x d
    cluster_centroids: &[f32],  // n x d
    region_weights: &[f32],     // m
    cluster_capacities: &[f32], // n
    m: u32,
    n: u32,
    d: u32,
    iters: u32,
    eps: f32,
    scratch: &'a mut SinkhornClusteringScratch,
) -> &'a [u32] {
    let m = m as usize;
    let n = n as usize;
    let d = d as usize;
    scratch.prepare(m, n);

    let SinkhornClusteringScratch {
        u,
        v,
        kernel,
        assignments,
    } = scratch;

    for i in 0..m {
        for j in 0..n {
            let mut cost = 0.0f32;
            for k_idx in 0..d {
                let diff = region_features[i * d + k_idx] - cluster_centroids[j * d + k_idx];
                cost += diff * diff;
            }
            kernel[i * n + j] = (-cost / eps).exp();
        }
    }

    for _ in 0..iters {
        for i in 0..m {
            let mut kv_sum = 0.0f32;
            for j in 0..n {
                kv_sum += kernel[i * n + j] * v[j];
            }
            u[i] = region_weights[i] / kv_sum.max(1e-10);
        }

        for j in 0..n {
            let mut ku_sum = 0.0f32;
            for i in 0..m {
                ku_sum += kernel[i * n + j] * u[i];
            }
            v[j] = cluster_capacities[j] / ku_sum.max(1e-10);
        }
    }

    for i in 0..m {
        let mut best_j = 0;
        let mut max_val = -1.0f32;
        for j in 0..n {
            let val = kernel[i * n + j] * v[j];
            if val > max_val {
                max_val = val;
                best_j = j as u32;
            }
        }
        assignments[i] = best_j;
    }

    assignments
}

/// CPU reference implementation of Sinkhorn clustering for parity testing.
#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn sinkhorn_clustering_cpu(
    region_features: &[f32],    // m x d
    cluster_centroids: &[f32],  // n x d
    region_weights: &[f32],     // m
    cluster_capacities: &[f32], // n
    m: u32,
    n: u32,
    d: u32,
    iters: u32,
    eps: f32,
) -> Vec<u32> {
    let mut scratch = SinkhornClusteringScratch::new();
    sinkhorn_clustering_cpu_into(
        region_features,
        cluster_centroids,
        region_weights,
        cluster_capacities,
        m,
        n,
        d,
        iters,
        eps,
        &mut scratch,
    )
    .to_vec()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn clustering_identity_one_region_one_cluster() {
        let features = vec![1.0, 1.0];
        let centroids = vec![1.0, 1.0];
        let weights = vec![1.0];
        let capacities = vec![1.0];
        let assignments = sinkhorn_clustering_cpu(
            &features,
            &centroids,
            &weights,
            &capacities,
            1,
            1,
            2,
            5,
            0.1,
        );
        assert_eq!(assignments, vec![0]);
    }

    #[test]
    fn clustering_two_regions_two_distant_clusters() {
        // Region 0 at (0,0), Region 1 at (10,10)
        // Cluster 0 at (0,0), Cluster 1 at (10,10)
        let features = vec![0.0, 0.0, 10.0, 10.0];
        let centroids = vec![0.0, 0.0, 10.0, 10.0];
        let weights = vec![1.0, 1.0];
        let capacities = vec![1.0, 1.0];
        let assignments = sinkhorn_clustering_cpu(
            &features,
            &centroids,
            &weights,
            &capacities,
            2,
            2,
            2,
            20,
            1.0,
        );
        assert_eq!(assignments, vec![0, 1]);
    }

    #[test]
    fn clustering_respects_capacities() {
        // Capacities enter Sinkhorn via the `v` scaling step; the CPU helper still
        // assigns each region with per-row argmax over `K_ij*v_j`, which does **not**
        // enforce hard cluster-cardinality constraints. Place regions clearly near
        // different centroids so argmax aligns with capacities (1 vs 2 mass targets).
        let features = vec![
            0.0, 0.0, // region 0 @ cluster 0
            100.0, 0.0, // regions 1–2 @ cluster 1
            100.0, 0.0,
        ];
        let centroids = vec![0.0, 0.0, 100.0, 0.0];
        let weights = vec![1.0, 1.0, 1.0];
        let capacities = vec![1.0, 2.0];
        let assignments = sinkhorn_clustering_cpu(
            &features,
            &centroids,
            &weights,
            &capacities,
            3,
            2,
            2,
            80,
            1.0,
        );

        let count_0 = assignments.iter().filter(|&&x| x == 0).count();
        let count_1 = assignments.iter().filter(|&&x| x == 1).count();
        assert_eq!(count_0, 1);
        assert_eq!(count_1, 2);
    }

    #[test]
    fn clustering_unbalanced_weights() {
        let features = vec![0.0, 10.0];
        let centroids = vec![0.0, 10.0];
        let weights = vec![1.0, 10.0];
        let capacities = vec![1.0, 10.0];
        let assignments = sinkhorn_clustering_cpu(
            &features,
            &centroids,
            &weights,
            &capacities,
            2,
            2,
            1,
            20,
            0.1,
        );
        assert_eq!(assignments, vec![0, 1]);
    }

    #[test]
    fn program_structure_is_valid() {
        let p = sinkhorn_clustering_program(10, 2, 2, 5, 0.1);
        assert_eq!(p.workgroup_size, [256, 1, 1]);
        let names: Vec<&str> = p.buffers.iter().map(|b| b.name()).collect();
        assert!(names.contains(&"region_features"));
        assert!(names.contains(&"out_assignments"));
    }

    #[test]
    fn parity_test_one_step() {
        // We can't easily run the GPU Program here without a backend,
        // but we verify the CPU implementation is consistent with the GPU logic.
        // The GPU logic literally re-implements the CPU logic in IR.
        let features = vec![1.0, 2.0, 5.0, 6.0];
        let centroids = vec![0.0, 0.0, 10.0, 10.0];
        let weights = vec![1.0, 1.0];
        let capacities = vec![1.0, 1.0];
        let cpu_res = sinkhorn_clustering_cpu(
            &features,
            &centroids,
            &weights,
            &capacities,
            2,
            2,
            2,
            1,
            1.0,
        );
        assert_eq!(cpu_res.len(), 2);
    }

    #[test]
    fn clustering_cpu_into_reuses_assignment_storage() {
        let features = vec![0.0, 0.0, 10.0, 10.0];
        let centroids = vec![0.0, 0.0, 10.0, 10.0];
        let weights = vec![1.0, 1.0];
        let capacities = vec![1.0, 1.0];
        let mut scratch = SinkhornClusteringScratch::new();

        let first = sinkhorn_clustering_cpu_into(
            &features,
            &centroids,
            &weights,
            &capacities,
            2,
            2,
            2,
            20,
            1.0,
            &mut scratch,
        )
        .to_vec();
        let ptr = scratch.assignment_ptr();
        let second = sinkhorn_clustering_cpu_into(
            &features,
            &centroids,
            &weights,
            &capacities,
            2,
            2,
            2,
            20,
            1.0,
            &mut scratch,
        )
        .to_vec();

        assert_eq!(first, vec![0, 1]);
        assert_eq!(second, first);
        assert_eq!(scratch.assignment_ptr(), ptr);
    }
}