primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
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
// SPDX-License-Identifier: AGPL-3.0-or-later
// ! Workload Analysis Module for BarraCuda v2.0
//!
//! Analyzes workloads and selects optimal compute device (CPU, GPU, NPU)
//! based on validated performance data from 96+ actual hardware tests.
//!
//! **Deep Debt Principles**:
//! - Runtime analysis (no hardcoding)
//! - Data-driven decisions (from actual measurements)
//! - Capability-based selection
//! - Pure Rust, zero unsafe

use std::collections::HashMap;

mod thresholds {
    /// Near-zero threshold for sparsity detection.
    pub const NEAR_ZERO: f32 = 0.01;
    pub const HIGH_SPARSITY: f32 = 0.75;
    pub const LOW_SPARSITY: f32 = 0.25;
    /// Canonical sparsity threshold for NPU routing (re-exported).
    pub const NPU_SPARSITY_THRESHOLD: f32 = 0.50;
    pub const RELU_THRESHOLD_SPARSITY: f32 = 0.75;
    pub const RELU_MASK_SPARSITY: f32 = 0.60;
    pub const RELU_ONLY_SPARSITY: f32 = 0.50;
    pub const THRESHOLD_ONLY_SPARSITY: f32 = 0.40;
    pub const MASK_ONLY_SPARSITY: f32 = 0.30;
    pub const MINIMAL_SPARSITY: f32 = 0.10;
    /// Below this element count, CPU dispatch overhead beats GPU for dense ops.
    pub const DENSE_CPU_THRESHOLD: usize = 1024;
}

/// Canonical sparsity threshold for NPU routing (re-exported for `npu_bridge`, npu matmul).
pub use thresholds::{DENSE_CPU_THRESHOLD, NPU_SPARSITY_THRESHOLD};

/// Workload type classifier
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum WorkloadType {
    /// Machine learning inference
    ML,
    /// Homomorphic encryption
    HE,
    /// Genomics (K-mer counting, sequence analysis)
    Genomics,
    /// Cryptography (AES, `ChaCha20`)
    Crypto,
    /// Dense arithmetic operations
    Dense,
    /// Sparse operations
    Sparse,
    /// Unknown workload
    Unknown,
}

/// Compute device types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ComputeDevice {
    /// Central processing unit (host)
    CPU,
    /// Graphics processing unit (discrete or integrated)
    GPU,
    /// Neural processing unit (dedicated inference accelerator)
    NPU,
}

/// Performance priority
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Priority {
    /// Minimize energy consumption (mobile/IoT)
    Energy,
    /// Maximize throughput (server/batch)
    Throughput,
    /// Minimize latency (real-time)
    Latency,
    /// Balance all factors
    Balanced,
}

/// Device selection hint
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeviceHint {
    /// Let analyzer decide
    Auto,
    /// Prefer energy efficiency
    PreferEnergy,
    /// Prefer throughput
    PreferSpeed,
    /// Prefer latency
    PreferLatency,
    /// Force specific device
    Force(ComputeDevice),
}

/// Sparsity analysis result
#[derive(Debug, Clone, Copy)]
pub struct SparsityProfile {
    /// Actual sparsity (0.0-1.0)
    pub actual_sparsity: f32,
    /// Potential sparsity after operations
    pub potential_sparsity: f32,
    /// Recommendation
    pub recommendation: DeviceRecommendation,
}

/// Device recommendation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeviceRecommendation {
    /// Consider NPU for sparse operations
    ConsiderNPU,
    /// Prefer dense compute (CPU/GPU)
    PreferDense,
    /// No strong preference
    Neutral,
}

/// Sparsity analyzer
pub struct SparsityAnalyzer;

impl SparsityAnalyzer {
    /// Analyze data for sparsity
    ///
    /// **Deep Debt**: Runtime analysis, no assumptions
    #[must_use]
    pub fn analyze_data(data: &[f32]) -> SparsityProfile {
        let zeros = data.iter().filter(|&&x| x == 0.0).count();
        let near_zeros = data
            .iter()
            .filter(|&&x| x.abs() < thresholds::NEAR_ZERO)
            .count();
        let total = data.len();

        if total == 0 {
            return SparsityProfile {
                actual_sparsity: 0.0,
                potential_sparsity: 0.0,
                recommendation: DeviceRecommendation::Neutral,
            };
        }

        let actual = zeros as f32 / total as f32;
        let potential = near_zeros as f32 / total as f32;

        // Recommendation based on validated NPU behavior
        let recommendation = if potential > thresholds::HIGH_SPARSITY {
            DeviceRecommendation::ConsiderNPU
        } else if potential < thresholds::LOW_SPARSITY {
            DeviceRecommendation::PreferDense
        } else {
            DeviceRecommendation::Neutral
        };

        SparsityProfile {
            actual_sparsity: actual,
            potential_sparsity: potential,
            recommendation,
        }
    }

    /// Analyze operation for sparsity potential
    ///
    /// **Deep Debt**: Pattern detection, no hardcoding
    #[must_use]
    pub fn analyze_operation(op_name: &str) -> SparsityProfile {
        // Detect sparsity-producing operations
        let has_relu = op_name.contains("relu") || op_name.contains("ReLU");
        let has_threshold = op_name.contains("threshold") || op_name.contains("clamp");
        let has_mask = op_name.contains("mask") || op_name.contains("dropout");

        let estimated_sparsity = match (has_relu, has_threshold, has_mask) {
            (true, true, _) => thresholds::RELU_THRESHOLD_SPARSITY,
            (true, false, true) => thresholds::RELU_MASK_SPARSITY,
            (true, false, false) => thresholds::RELU_ONLY_SPARSITY,
            (false, true, _) => thresholds::THRESHOLD_ONLY_SPARSITY,
            (false, false, true) => thresholds::MASK_ONLY_SPARSITY,
            _ => thresholds::MINIMAL_SPARSITY,
        };

        let recommendation = if estimated_sparsity > thresholds::NPU_SPARSITY_THRESHOLD {
            DeviceRecommendation::ConsiderNPU
        } else {
            DeviceRecommendation::PreferDense
        };

        SparsityProfile {
            actual_sparsity: 0.0,
            potential_sparsity: estimated_sparsity,
            recommendation,
        }
    }
}

/// Workload classifier
pub struct WorkloadClassifier;

impl WorkloadClassifier {
    /// Classify workload from operation name
    ///
    /// **Deep Debt**: Pattern matching, extensible
    #[must_use]
    pub fn classify_op(op_name: &str) -> WorkloadType {
        let name_lower = op_name.to_lowercase();

        // ML patterns
        if name_lower.contains("mlp")
            || name_lower.contains("conv")
            || name_lower.contains("matmul")
            || name_lower.contains("attention")
            || name_lower.contains("layer_norm")
        {
            return WorkloadType::ML;
        }

        // HE patterns
        if name_lower.contains("fhe")
            || name_lower.contains("tfhe")
            || name_lower.contains("homomorphic")
            || name_lower.contains("bootstrap")
        {
            return WorkloadType::HE;
        }

        // Genomics patterns
        if name_lower.contains("kmer")
            || name_lower.contains("dna")
            || name_lower.contains("sequence")
            || name_lower.contains("align")
        {
            return WorkloadType::Genomics;
        }

        // Crypto patterns
        if name_lower.contains("aes")
            || name_lower.contains("chacha")
            || name_lower.contains("encrypt")
            || name_lower.contains("hash")
        {
            return WorkloadType::Crypto;
        }

        // Sparse patterns
        if name_lower.contains("sparse") {
            return WorkloadType::Sparse;
        }

        // Dense patterns
        if name_lower.contains("dense") || name_lower.contains("vector_add") {
            return WorkloadType::Dense;
        }

        WorkloadType::Unknown
    }
}

/// Decision matrix from validated hardware tests
///
/// **Data Source**: 96+ tests on actual hardware (Feb 2026)
/// - MNIST NPU: 88 tests (3 NPU + 85 CPU/GPU)
/// - K-mer: 8 tests (CPU/GPU)
/// - AES: 8 tests (CPU/GPU)
/// - HE: 15 tests (CPU/GPU/NPU)
/// - Dense/Sparse: 48 tests
pub struct DecisionMatrix {
    /// Energy efficiency (ops/joule)
    energy: HashMap<(WorkloadType, ComputeDevice), f32>,
    /// Throughput (ops/sec or items/sec)
    throughput: HashMap<(WorkloadType, ComputeDevice), f64>,
    /// Latency (milliseconds)
    latency: HashMap<(WorkloadType, ComputeDevice), f32>,
}

impl DecisionMatrix {
    /// Build decision matrix from validation data
    ///
    /// **Deep Debt**: Data-driven, measured values only
    #[must_use]
    pub fn from_validation_data() -> Self {
        let mut energy = HashMap::new();
        let mut throughput = HashMap::new();
        let mut latency = HashMap::new();

        // ML Inference (from MNIST NPU validation - Feb 1, 2026)
        energy.insert((WorkloadType::ML, ComputeDevice::CPU), 1.22); // 1/0.82mJ
        energy.insert((WorkloadType::ML, ComputeDevice::GPU), 5.26); // 1/0.19mJ @ batch=128
        energy.insert((WorkloadType::ML, ComputeDevice::NPU), 9.09); // 1/0.11mJ 🏆

        throughput.insert((WorkloadType::ML, ComputeDevice::CPU), 6_223.0);
        throughput.insert((WorkloadType::ML, ComputeDevice::GPU), 1_330_679.0); // @ batch=128
        throughput.insert((WorkloadType::ML, ComputeDevice::NPU), 17_490.0);

        latency.insert((WorkloadType::ML, ComputeDevice::CPU), 0.161);
        latency.insert((WorkloadType::ML, ComputeDevice::GPU), 0.001); // @ batch=128
        latency.insert((WorkloadType::ML, ComputeDevice::NPU), 0.057); // 🏆 @ batch=1

        // HE (from original validation)
        energy.insert((WorkloadType::HE, ComputeDevice::CPU), 0.3);
        energy.insert((WorkloadType::HE, ComputeDevice::GPU), 0.9);
        energy.insert((WorkloadType::HE, ComputeDevice::NPU), 467.0); // 🏆 1,557× CPU!

        throughput.insert((WorkloadType::HE, ComputeDevice::CPU), 859.0);
        throughput.insert((WorkloadType::HE, ComputeDevice::GPU), 4_078.0);
        throughput.insert((WorkloadType::HE, ComputeDevice::NPU), 2_482.0);

        // Genomics (from K-mer CPU/GPU validation)
        throughput.insert((WorkloadType::Genomics, ComputeDevice::CPU), 5.21); // MB/s
        throughput.insert((WorkloadType::Genomics, ComputeDevice::GPU), 8_007.91); // MB/s 🏆
        // NPU genomics: awaiting K-mer NPU results

        // Crypto (from AES CPU/GPU validation)
        throughput.insert((WorkloadType::Crypto, ComputeDevice::CPU), 132.0); // MB/s
        throughput.insert((WorkloadType::Crypto, ComputeDevice::GPU), 12_669.0); // MB/s @ 16MB

        // Dense operations (from characterization)
        energy.insert((WorkloadType::Dense, ComputeDevice::CPU), 95_000.0); // 95M ops/J
        energy.insert((WorkloadType::Dense, ComputeDevice::GPU), 33.0); // GPU inefficient for small

        Self {
            energy,
            throughput,
            latency,
        }
    }

    /// Get energy efficiency for workload-device combination
    #[must_use]
    pub fn get_energy(&self, workload: WorkloadType, device: ComputeDevice) -> Option<f32> {
        self.energy.get(&(workload, device)).copied()
    }

    /// Get throughput for workload-device combination
    #[must_use]
    pub fn get_throughput(&self, workload: WorkloadType, device: ComputeDevice) -> Option<f64> {
        self.throughput.get(&(workload, device)).copied()
    }

    /// Get latency for workload-device combination
    #[must_use]
    pub fn get_latency(&self, workload: WorkloadType, device: ComputeDevice) -> Option<f32> {
        self.latency.get(&(workload, device)).copied()
    }
}

/// Device selector using validated performance data
pub struct DeviceSelector {
    available_devices: Vec<ComputeDevice>,
    // Pending: Use for Pareto-optimal selection (energy vs throughput vs latency trade-offs)
    _decision_matrix: DecisionMatrix,
}

impl DeviceSelector {
    /// Create selector with available devices
    ///
    /// **Deep Debt**: Runtime discovery, no assumptions
    #[must_use]
    pub fn new(available_devices: Vec<ComputeDevice>) -> Self {
        Self {
            available_devices,
            _decision_matrix: DecisionMatrix::from_validation_data(),
        }
    }

    /// Select optimal device
    ///
    /// **Deep Debt**: Data-driven selection from 96+ tests
    #[must_use]
    pub fn select(
        &self,
        workload: WorkloadType,
        sparsity: f32,
        data_size: usize,
        priority: Priority,
        hint: DeviceHint,
    ) -> ComputeDevice {
        // Honor force hint
        if let DeviceHint::Force(device) = hint {
            return device;
        }

        // Use validation data to decide
        match (workload, priority) {
            // ML Inference (from MNIST NPU validation!)
            (WorkloadType::ML, Priority::Energy) => {
                // NPU is 7× more energy efficient!
                if self.has_device(ComputeDevice::NPU) {
                    ComputeDevice::NPU
                } else {
                    ComputeDevice::CPU
                }
            }

            (WorkloadType::ML, Priority::Latency) => {
                // NPU has best single-item latency (0.057 ms)
                if self.has_device(ComputeDevice::NPU) {
                    ComputeDevice::NPU
                } else if self.has_device(ComputeDevice::GPU) {
                    ComputeDevice::GPU
                } else {
                    ComputeDevice::CPU
                }
            }

            (WorkloadType::ML, Priority::Throughput) if data_size > 32 => {
                // GPU dominates at batch >32 (76× faster!)
                if self.has_device(ComputeDevice::GPU) {
                    ComputeDevice::GPU
                } else {
                    ComputeDevice::CPU
                }
            }

            (WorkloadType::ML, Priority::Balanced) => {
                // NPU: decent throughput + best energy
                if self.has_device(ComputeDevice::NPU) {
                    ComputeDevice::NPU
                } else {
                    ComputeDevice::CPU
                }
            }

            // HE (from original validation!)
            (WorkloadType::HE, _) => {
                // NPU ALWAYS for HE (1,557× better!)
                if self.has_device(ComputeDevice::NPU) {
                    ComputeDevice::NPU
                } else {
                    ComputeDevice::CPU // Fallback (slow!)
                }
            }

            // Genomics (from K-mer CPU/GPU validation)
            (WorkloadType::Genomics, Priority::Throughput) if data_size > 1_000_000 => {
                // GPU dominates (1,537× faster!)
                if self.has_device(ComputeDevice::GPU) {
                    ComputeDevice::GPU
                } else {
                    ComputeDevice::CPU
                }
            }

            // Crypto (from AES CPU/GPU validation)
            (WorkloadType::Crypto, _) if data_size < 500_000 => {
                // CPU wins for small data (13× more efficient!)
                ComputeDevice::CPU
            }

            (WorkloadType::Crypto, Priority::Throughput) if data_size > 1_000_000 => {
                // GPU scales massively (96× faster!)
                if self.has_device(ComputeDevice::GPU) {
                    ComputeDevice::GPU
                } else {
                    ComputeDevice::CPU
                }
            }

            // Dense operations (from characterization)
            (WorkloadType::Dense, _) if data_size < thresholds::DENSE_CPU_THRESHOLD => {
                // CPU dominates small dense (2,857× better!)
                ComputeDevice::CPU
            }

            // Sparse operations
            (WorkloadType::Sparse, Priority::Energy) if sparsity > 0.9 => {
                // High sparsity: NPU might win
                if self.has_device(ComputeDevice::NPU) {
                    ComputeDevice::NPU
                } else {
                    ComputeDevice::CPU
                }
            }

            // Default: prefer GPU if available, else CPU
            _ => {
                if self.has_device(ComputeDevice::GPU) {
                    ComputeDevice::GPU
                } else {
                    ComputeDevice::CPU
                }
            }
        }
    }

    /// Check if device is available
    fn has_device(&self, device: ComputeDevice) -> bool {
        self.available_devices.contains(&device)
    }
}

#[cfg(test)]
#[path = "workload_tests.rs"]
mod tests;