ruvector-attention 2.1.0

Attention mechanisms for ruvector - geometric, graph, and sparse attention
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
661
662
663
664
665
//! Token Router for Coherence-Gated Transformer
//!
//! Routes tokens to different compute lanes based on coherence energy:
//!
//! - **Reflex** (Lane 0): E < theta_reflex, minimal compute (<0.1ms)
//! - **Standard** (Lane 1): E < theta_standard, normal compute (~1ms)
//! - **Deep** (Lane 2): E >= theta_standard, maximum compute (~5ms)
//! - **Escalate** (Lane 3): Irreconcilable incoherence, return uncertainty
//!
//! ## Routing Thresholds
//!
//! | Threshold | Default | Meaning |
//! |-----------|---------|---------|
//! | theta_reflex | 0.01 | Token highly coherent with context |
//! | theta_standard | 0.1 | Minor inconsistencies |
//! | theta_deep | 1.0 | Major inconsistencies |
//! | theta_escalate | 10.0 | Irreconcilable (escalate) |

use crate::error::{AttentionError, AttentionResult};
use crate::sheaf::SheafAttention;
use serde::{Deserialize, Serialize};

/// Compute lane for token processing
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum ComputeLane {
    /// Minimal compute (<0.1ms): 1-2 layers, local attention, no FFN
    /// Use case: Common tokens, clear context
    Reflex = 0,

    /// Standard compute (~1ms): 6 layers, sparse sheaf attention
    /// Use case: Normal tokens requiring context integration
    Standard = 1,

    /// Deep compute (~5ms): 12+ layers, full sheaf + MoE
    /// Use case: Ambiguous, contradictory, or complex tokens
    Deep = 2,

    /// Escalate: Return uncertainty, request clarification
    /// Use case: Irreconcilable incoherence
    Escalate = 3,
}

impl ComputeLane {
    /// Get human-readable description
    pub fn description(&self) -> &'static str {
        match self {
            Self::Reflex => "Reflex (minimal compute)",
            Self::Standard => "Standard (normal compute)",
            Self::Deep => "Deep (maximum compute)",
            Self::Escalate => "Escalate (return uncertainty)",
        }
    }

    /// Get typical latency in milliseconds
    pub fn typical_latency_ms(&self) -> f32 {
        match self {
            Self::Reflex => 0.1,
            Self::Standard => 1.0,
            Self::Deep => 5.0,
            Self::Escalate => 0.0, // Async/immediate return
        }
    }

    /// Get typical number of layers
    pub fn typical_layers(&self) -> usize {
        match self {
            Self::Reflex => 2,
            Self::Standard => 6,
            Self::Deep => 12,
            Self::Escalate => 0,
        }
    }

    /// Check if this lane requires full attention
    pub fn requires_full_attention(&self) -> bool {
        matches!(self, Self::Deep)
    }

    /// Check if this lane uses MoE routing
    pub fn uses_moe(&self) -> bool {
        matches!(self, Self::Deep)
    }
}

/// Configuration for token router
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenRouterConfig {
    /// Energy threshold for reflex lane (E < theta_reflex -> Reflex)
    pub theta_reflex: f32,
    /// Energy threshold for standard lane (E < theta_standard -> Standard)
    pub theta_standard: f32,
    /// Energy threshold for deep lane (E < theta_deep -> Deep)
    pub theta_deep: f32,
    /// Energy threshold for escalation (E >= theta_escalate -> Escalate)
    pub theta_escalate: f32,
    /// Whether to use average energy (true) or total energy (false)
    pub use_average_energy: bool,
    /// Minimum context size for routing (smaller contexts default to Standard)
    pub min_context_size: usize,
}

impl Default for TokenRouterConfig {
    fn default() -> Self {
        Self {
            theta_reflex: 0.01,
            theta_standard: 0.1,
            theta_deep: 1.0,
            theta_escalate: 10.0,
            use_average_energy: true,
            min_context_size: 4,
        }
    }
}

impl TokenRouterConfig {
    /// Create config with custom thresholds
    pub fn new(theta_reflex: f32, theta_standard: f32, theta_deep: f32) -> Self {
        Self {
            theta_reflex,
            theta_standard,
            theta_deep,
            theta_escalate: theta_deep * 10.0,
            ..Default::default()
        }
    }

    /// Builder: set reflex threshold
    pub fn with_theta_reflex(mut self, theta: f32) -> Self {
        self.theta_reflex = theta;
        self
    }

    /// Builder: set standard threshold
    pub fn with_theta_standard(mut self, theta: f32) -> Self {
        self.theta_standard = theta;
        self
    }

    /// Builder: set deep threshold
    pub fn with_theta_deep(mut self, theta: f32) -> Self {
        self.theta_deep = theta;
        self
    }

    /// Builder: set escalate threshold
    pub fn with_theta_escalate(mut self, theta: f32) -> Self {
        self.theta_escalate = theta;
        self
    }

    /// Builder: set energy computation method
    pub fn with_average_energy(mut self, use_avg: bool) -> Self {
        self.use_average_energy = use_avg;
        self
    }

    /// Builder: set minimum context size
    pub fn with_min_context_size(mut self, size: usize) -> Self {
        self.min_context_size = size;
        self
    }

    /// Validate configuration
    pub fn validate(&self) -> AttentionResult<()> {
        if self.theta_reflex <= 0.0 {
            return Err(AttentionError::InvalidConfig(
                "theta_reflex must be positive".to_string(),
            ));
        }
        if self.theta_standard <= self.theta_reflex {
            return Err(AttentionError::InvalidConfig(
                "theta_standard must be greater than theta_reflex".to_string(),
            ));
        }
        if self.theta_deep <= self.theta_standard {
            return Err(AttentionError::InvalidConfig(
                "theta_deep must be greater than theta_standard".to_string(),
            ));
        }
        if self.theta_escalate <= self.theta_deep {
            return Err(AttentionError::InvalidConfig(
                "theta_escalate must be greater than theta_deep".to_string(),
            ));
        }
        Ok(())
    }
}

/// Routing decision for a token
#[derive(Debug, Clone)]
pub struct RoutingDecision {
    /// Token index in sequence
    pub token_idx: usize,
    /// Computed energy for the token
    pub energy: f32,
    /// Assigned compute lane
    pub lane: ComputeLane,
    /// Confidence in the routing decision (0-1)
    pub confidence: f32,
    /// Optional sparse mask indices (for Standard lane)
    pub sparse_indices: Option<Vec<usize>>,
}

impl RoutingDecision {
    /// Create a new routing decision
    pub fn new(token_idx: usize, energy: f32, lane: ComputeLane) -> Self {
        // Confidence based on how clearly the energy falls into a lane
        let confidence = 1.0; // Can be refined based on energy distance to thresholds

        Self {
            token_idx,
            energy,
            lane,
            confidence,
            sparse_indices: None,
        }
    }

    /// Set sparse indices for this decision
    pub fn with_sparse_indices(mut self, indices: Vec<usize>) -> Self {
        self.sparse_indices = Some(indices);
        self
    }

    /// Check if this token needs attention
    pub fn needs_attention(&self) -> bool {
        !matches!(self.lane, ComputeLane::Escalate)
    }
}

/// Token router for coherence-gated transformer
pub struct TokenRouter {
    config: TokenRouterConfig,
}

impl TokenRouter {
    /// Create a new token router
    pub fn new(config: TokenRouterConfig) -> Self {
        Self { config }
    }

    /// Create with default configuration
    pub fn default_router() -> Self {
        Self::new(TokenRouterConfig::default())
    }

    /// Get configuration
    pub fn config(&self) -> &TokenRouterConfig {
        &self.config
    }

    /// Get mutable configuration (for SONA tuning)
    pub fn config_mut(&mut self) -> &mut TokenRouterConfig {
        &mut self.config
    }

    /// Route a single token based on energy
    ///
    /// # Arguments
    ///
    /// * `energy` - Pre-computed energy for the token
    ///
    /// # Returns
    ///
    /// Compute lane for this token
    pub fn route_by_energy(&self, energy: f32) -> ComputeLane {
        if energy < self.config.theta_reflex {
            ComputeLane::Reflex
        } else if energy < self.config.theta_standard {
            ComputeLane::Standard
        } else if energy < self.config.theta_escalate {
            ComputeLane::Deep
        } else {
            ComputeLane::Escalate
        }
    }

    /// Route a single token using sheaf attention
    ///
    /// # Arguments
    ///
    /// * `token` - Token embedding
    /// * `context` - Context embeddings (keys)
    /// * `attention` - Sheaf attention layer for energy computation
    ///
    /// # Returns
    ///
    /// Routing decision for this token
    pub fn route_token(
        &self,
        token_idx: usize,
        token: &[f32],
        context: &[&[f32]],
        attention: &SheafAttention,
    ) -> AttentionResult<RoutingDecision> {
        // Handle small contexts
        if context.len() < self.config.min_context_size {
            return Ok(RoutingDecision::new(token_idx, 0.0, ComputeLane::Standard));
        }

        // Compute energy
        let energy = if self.config.use_average_energy {
            attention.average_token_energy(token, context)?
        } else {
            attention.token_energy(token, context)?
        };

        let lane = self.route_by_energy(energy);

        Ok(RoutingDecision::new(token_idx, energy, lane))
    }

    /// Route a batch of tokens
    ///
    /// # Arguments
    ///
    /// * `tokens` - Token embeddings
    /// * `context` - Shared context embeddings
    /// * `attention` - Sheaf attention layer
    ///
    /// # Returns
    ///
    /// Vector of routing decisions
    pub fn route_batch(
        &self,
        tokens: &[&[f32]],
        context: &[&[f32]],
        attention: &SheafAttention,
    ) -> AttentionResult<Vec<RoutingDecision>> {
        tokens
            .iter()
            .enumerate()
            .map(|(idx, token)| self.route_token(idx, token, context, attention))
            .collect()
    }

    /// Group tokens by their assigned lane
    ///
    /// Returns (reflex_indices, standard_indices, deep_indices, escalate_indices)
    pub fn group_by_lane(
        decisions: &[RoutingDecision],
    ) -> (Vec<usize>, Vec<usize>, Vec<usize>, Vec<usize>) {
        let mut reflex = Vec::new();
        let mut standard = Vec::new();
        let mut deep = Vec::new();
        let mut escalate = Vec::new();

        for decision in decisions {
            match decision.lane {
                ComputeLane::Reflex => reflex.push(decision.token_idx),
                ComputeLane::Standard => standard.push(decision.token_idx),
                ComputeLane::Deep => deep.push(decision.token_idx),
                ComputeLane::Escalate => escalate.push(decision.token_idx),
            }
        }

        (reflex, standard, deep, escalate)
    }

    /// Compute lane statistics for a batch of decisions
    pub fn lane_statistics(decisions: &[RoutingDecision]) -> LaneStatistics {
        let total = decisions.len();
        let (reflex, standard, deep, escalate) = Self::group_by_lane(decisions);

        let avg_energy = if total > 0 {
            decisions.iter().map(|d| d.energy).sum::<f32>() / total as f32
        } else {
            0.0
        };

        let max_energy = decisions.iter().map(|d| d.energy).fold(0.0f32, f32::max);

        let min_energy = decisions
            .iter()
            .map(|d| d.energy)
            .fold(f32::INFINITY, f32::min);

        LaneStatistics {
            total_tokens: total,
            reflex_count: reflex.len(),
            standard_count: standard.len(),
            deep_count: deep.len(),
            escalate_count: escalate.len(),
            average_energy: avg_energy,
            max_energy,
            min_energy: if min_energy.is_infinite() {
                0.0
            } else {
                min_energy
            },
        }
    }

    /// Estimate total latency for a batch based on routing
    pub fn estimate_latency_ms(decisions: &[RoutingDecision]) -> f32 {
        decisions.iter().map(|d| d.lane.typical_latency_ms()).sum()
    }

    /// Update thresholds based on desired lane distribution
    ///
    /// This can be used by SONA for adaptive tuning.
    pub fn tune_thresholds(
        &mut self,
        current_stats: &LaneStatistics,
        target_reflex_ratio: f32,
        target_standard_ratio: f32,
    ) {
        let total = current_stats.total_tokens as f32;
        if total == 0.0 {
            return;
        }

        let current_reflex_ratio = current_stats.reflex_count as f32 / total;
        let current_standard_ratio = current_stats.standard_count as f32 / total;

        // Adjust thresholds to move towards target ratios
        // More reflex needed -> increase theta_reflex
        // Less reflex needed -> decrease theta_reflex
        let reflex_adjustment = (target_reflex_ratio - current_reflex_ratio) * 0.1;
        let standard_adjustment = (target_standard_ratio - current_standard_ratio) * 0.1;

        // Apply adjustments while maintaining ordering
        self.config.theta_reflex = (self.config.theta_reflex * (1.0 + reflex_adjustment))
            .max(0.001)
            .min(self.config.theta_standard * 0.9);

        self.config.theta_standard = (self.config.theta_standard * (1.0 + standard_adjustment))
            .max(self.config.theta_reflex * 1.1)
            .min(self.config.theta_deep * 0.9);
    }
}

/// Statistics about lane distribution
#[derive(Debug, Clone)]
pub struct LaneStatistics {
    /// Total number of tokens routed
    pub total_tokens: usize,
    /// Tokens routed to Reflex lane
    pub reflex_count: usize,
    /// Tokens routed to Standard lane
    pub standard_count: usize,
    /// Tokens routed to Deep lane
    pub deep_count: usize,
    /// Tokens escalated
    pub escalate_count: usize,
    /// Average energy across all tokens
    pub average_energy: f32,
    /// Maximum energy
    pub max_energy: f32,
    /// Minimum energy
    pub min_energy: f32,
}

impl LaneStatistics {
    /// Get ratio of tokens in reflex lane
    pub fn reflex_ratio(&self) -> f32 {
        if self.total_tokens == 0 {
            0.0
        } else {
            self.reflex_count as f32 / self.total_tokens as f32
        }
    }

    /// Get ratio of tokens in standard lane
    pub fn standard_ratio(&self) -> f32 {
        if self.total_tokens == 0 {
            0.0
        } else {
            self.standard_count as f32 / self.total_tokens as f32
        }
    }

    /// Get ratio of tokens in deep lane
    pub fn deep_ratio(&self) -> f32 {
        if self.total_tokens == 0 {
            0.0
        } else {
            self.deep_count as f32 / self.total_tokens as f32
        }
    }

    /// Get ratio of escalated tokens
    pub fn escalate_ratio(&self) -> f32 {
        if self.total_tokens == 0 {
            0.0
        } else {
            self.escalate_count as f32 / self.total_tokens as f32
        }
    }

    /// Estimated speedup compared to all-deep processing
    pub fn estimated_speedup(&self) -> f32 {
        if self.total_tokens == 0 {
            1.0
        } else {
            let deep_latency = self.total_tokens as f32 * ComputeLane::Deep.typical_latency_ms();
            let actual_latency = self.reflex_count as f32
                * ComputeLane::Reflex.typical_latency_ms()
                + self.standard_count as f32 * ComputeLane::Standard.typical_latency_ms()
                + self.deep_count as f32 * ComputeLane::Deep.typical_latency_ms();

            if actual_latency > 0.0 {
                deep_latency / actual_latency
            } else {
                1.0
            }
        }
    }
}

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

    #[test]
    fn test_compute_lane_ordering() {
        assert!(ComputeLane::Reflex < ComputeLane::Standard);
        assert!(ComputeLane::Standard < ComputeLane::Deep);
        assert!(ComputeLane::Deep < ComputeLane::Escalate);
    }

    #[test]
    fn test_lane_properties() {
        assert_eq!(ComputeLane::Reflex.typical_layers(), 2);
        assert_eq!(ComputeLane::Standard.typical_layers(), 6);
        assert_eq!(ComputeLane::Deep.typical_layers(), 12);

        assert!(!ComputeLane::Reflex.requires_full_attention());
        assert!(!ComputeLane::Standard.requires_full_attention());
        assert!(ComputeLane::Deep.requires_full_attention());

        assert!(!ComputeLane::Reflex.uses_moe());
        assert!(ComputeLane::Deep.uses_moe());
    }

    #[test]
    fn test_config_default() {
        let config = TokenRouterConfig::default();
        assert!(config.theta_reflex < config.theta_standard);
        assert!(config.theta_standard < config.theta_deep);
        assert!(config.theta_deep < config.theta_escalate);
    }

    #[test]
    fn test_config_validation() {
        assert!(TokenRouterConfig::default().validate().is_ok());

        let bad_config = TokenRouterConfig {
            theta_reflex: 0.1,
            theta_standard: 0.05, // Less than reflex
            ..Default::default()
        };
        assert!(bad_config.validate().is_err());
    }

    #[test]
    fn test_route_by_energy() {
        let router = TokenRouter::default_router();

        assert_eq!(router.route_by_energy(0.001), ComputeLane::Reflex);
        assert_eq!(router.route_by_energy(0.05), ComputeLane::Standard);
        assert_eq!(router.route_by_energy(0.5), ComputeLane::Deep);
        assert_eq!(router.route_by_energy(100.0), ComputeLane::Escalate);
    }

    #[test]
    fn test_route_token() {
        let router = TokenRouter::default_router();
        let config = SheafAttentionConfig::new(8);
        let attention = SheafAttention::new(config);

        let token = vec![1.0; 8];
        let c1 = vec![1.0; 8];
        let c2 = vec![1.0; 8];
        let c3 = vec![1.0; 8];
        let c4 = vec![1.0; 8];
        let context: Vec<&[f32]> = vec![&c1, &c2, &c3, &c4];

        let decision = router.route_token(0, &token, &context, &attention).unwrap();
        assert_eq!(decision.token_idx, 0);
        assert!(decision.energy >= 0.0);
    }

    #[test]
    fn test_route_batch() {
        let router = TokenRouter::default_router();
        let config = SheafAttentionConfig::new(8);
        let attention = SheafAttention::new(config);

        let t1 = vec![1.0; 8];
        let t2 = vec![0.5; 8];
        let tokens: Vec<&[f32]> = vec![&t1, &t2];

        let c1 = vec![1.0; 8];
        let c2 = vec![1.0; 8];
        let c3 = vec![1.0; 8];
        let c4 = vec![1.0; 8];
        let context: Vec<&[f32]> = vec![&c1, &c2, &c3, &c4];

        let decisions = router.route_batch(&tokens, &context, &attention).unwrap();
        assert_eq!(decisions.len(), 2);
    }

    #[test]
    fn test_group_by_lane() {
        let decisions = vec![
            RoutingDecision::new(0, 0.001, ComputeLane::Reflex),
            RoutingDecision::new(1, 0.05, ComputeLane::Standard),
            RoutingDecision::new(2, 0.5, ComputeLane::Deep),
            RoutingDecision::new(3, 0.002, ComputeLane::Reflex),
        ];

        let (reflex, standard, deep, escalate) = TokenRouter::group_by_lane(&decisions);

        assert_eq!(reflex, vec![0, 3]);
        assert_eq!(standard, vec![1]);
        assert_eq!(deep, vec![2]);
        assert!(escalate.is_empty());
    }

    #[test]
    fn test_lane_statistics() {
        let decisions = vec![
            RoutingDecision::new(0, 0.001, ComputeLane::Reflex),
            RoutingDecision::new(1, 0.05, ComputeLane::Standard),
            RoutingDecision::new(2, 0.5, ComputeLane::Deep),
            RoutingDecision::new(3, 0.002, ComputeLane::Reflex),
        ];

        let stats = TokenRouter::lane_statistics(&decisions);

        assert_eq!(stats.total_tokens, 4);
        assert_eq!(stats.reflex_count, 2);
        assert_eq!(stats.standard_count, 1);
        assert_eq!(stats.deep_count, 1);
        assert_eq!(stats.escalate_count, 0);

        assert!((stats.reflex_ratio() - 0.5).abs() < 1e-6);
        assert!(stats.estimated_speedup() > 1.0);
    }

    #[test]
    fn test_routing_decision_builder() {
        let decision =
            RoutingDecision::new(0, 0.1, ComputeLane::Standard).with_sparse_indices(vec![1, 3, 5]);

        assert!(decision.sparse_indices.is_some());
        assert_eq!(decision.sparse_indices.unwrap(), vec![1, 3, 5]);
    }

    #[test]
    fn test_small_context_default() {
        let router = TokenRouter::default_router();
        let config = SheafAttentionConfig::new(8);
        let attention = SheafAttention::new(config);

        let token = vec![1.0; 8];
        let c1 = vec![1.0; 8];
        let context: Vec<&[f32]> = vec![&c1]; // Small context

        let decision = router.route_token(0, &token, &context, &attention).unwrap();
        assert_eq!(decision.lane, ComputeLane::Standard); // Default for small context
    }
}