trueno 0.17.2

High-performance SIMD compute library with GPU support for matrix operations
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
#![allow(missing_docs)]
//! Built-in Compute Operations
//!
//! Pre-defined operations that implement the ComputeOp trait:
//! - DotOp: Vector dot product
//! - AddOp: Element-wise vector addition
//! - MatmulOp: Matrix multiplication (SIMD-optimized)
//! - SoftmaxOp: Softmax with SIMD exp approximation (SIMD-EXP)

use super::{Backend, ComputeOp};
use crate::error::TruenoError;

// ============================================================================
// DotOp: Dot Product
// ============================================================================

/// Dot product operation.
#[derive(Debug, Clone)]
pub struct DotOp {
    /// Expected vector length
    pub len: usize,
}

impl DotOp {
    pub fn new(len: usize) -> Self {
        Self { len }
    }
}

impl ComputeOp for DotOp {
    type Input = (Vec<f32>, Vec<f32>);
    type Output = f32;

    fn name(&self) -> &'static str {
        "dot"
    }

    fn execute(&self, input: Self::Input, _backend: Backend) -> Result<Self::Output, TruenoError> {
        let (a, b) = input;
        if a.len() != b.len() {
            return Err(TruenoError::SizeMismatch { expected: a.len(), actual: b.len() });
        }
        // Simple scalar implementation for now
        let sum: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
        Ok(sum)
    }

    fn tokens(&self, input: &Self::Input) -> usize {
        // Each element pair is roughly 1 "token" of work
        input.0.len()
    }
}

// ============================================================================
// AddOp: Element-wise Addition
// ============================================================================

/// Element-wise add operation.
#[derive(Debug, Clone)]
pub struct AddOp {
    /// Expected vector length
    pub len: usize,
}

impl AddOp {
    pub fn new(len: usize) -> Self {
        Self { len }
    }
}

impl ComputeOp for AddOp {
    type Input = (Vec<f32>, Vec<f32>);
    type Output = Vec<f32>;

    fn name(&self) -> &'static str {
        "add"
    }

    fn execute(&self, input: Self::Input, _backend: Backend) -> Result<Self::Output, TruenoError> {
        let (a, b) = input;
        if a.len() != b.len() {
            return Err(TruenoError::SizeMismatch { expected: a.len(), actual: b.len() });
        }
        Ok(a.iter().zip(b.iter()).map(|(x, y)| x + y).collect())
    }

    fn tokens(&self, input: &Self::Input) -> usize {
        input.0.len()
    }
}

// ============================================================================
// MatmulOp: Matrix Multiplication
// ============================================================================

/// Matrix multiplication operation.
#[derive(Debug, Clone)]
pub struct MatmulOp {
    /// M dimension (rows of A)
    pub m: usize,
    /// K dimension (cols of A = rows of B)
    pub k: usize,
    /// N dimension (cols of B)
    pub n: usize,
}

impl MatmulOp {
    pub fn new(m: usize, k: usize, n: usize) -> Self {
        Self { m, k, n }
    }
}

impl ComputeOp for MatmulOp {
    type Input = (Vec<f32>, Vec<f32>);
    type Output = Vec<f32>;

    fn name(&self) -> &'static str {
        "matmul"
    }

    fn execute(&self, input: Self::Input, _backend: Backend) -> Result<Self::Output, TruenoError> {
        let (a, b) = input;
        let expected_a = self.m * self.k;
        let expected_b = self.k * self.n;

        if a.len() != expected_a {
            return Err(TruenoError::SizeMismatch { expected: expected_a, actual: a.len() });
        }
        if b.len() != expected_b {
            return Err(TruenoError::SizeMismatch { expected: expected_b, actual: b.len() });
        }

        // SIMD-optimized matrix multiplication via Matrix type
        // Uses AVX2/AVX-512 with cache blocking for ~10-50x speedup
        let simd_backend = crate::Backend::select_best();
        let mat_a = crate::Matrix::from_vec_with_backend(self.m, self.k, a, simd_backend);
        let mat_b = crate::Matrix::from_vec_with_backend(self.k, self.n, b, simd_backend);

        let result = mat_a.matmul(&mat_b)?;
        Ok(result.as_slice().to_vec())
    }

    fn tokens(&self, _input: &Self::Input) -> usize {
        // For matmul, "tokens" = number of output elements
        // Each output requires K multiply-adds
        self.m * self.n
    }
}

// ============================================================================
// SoftmaxOp: Softmax with SIMD Exp (SIMD-EXP)
// ============================================================================

/// Softmax operation.
#[derive(Debug, Clone)]
pub struct SoftmaxOp {
    /// Expected vector length
    pub len: usize,
}

impl SoftmaxOp {
    pub fn new(len: usize) -> Self {
        Self { len }
    }
}

impl ComputeOp for SoftmaxOp {
    type Input = Vec<f32>;
    type Output = Vec<f32>;

    fn name(&self) -> &'static str {
        "softmax"
    }

    fn execute(&self, input: Self::Input, backend: Backend) -> Result<Self::Output, TruenoError> {
        if input.is_empty() {
            return Ok(vec![]);
        }

        // SIMD-EXP: Use SIMD backends for 2-3x speedup on softmax
        // The exp() is the bottleneck in softmax - SIMD polynomial approximation
        // matches llama.cpp's ggml_v_expf performance.

        // Step 1: Find max for numerical stability (SIMD max)
        let max = Self::simd_max(&input, backend);

        // Step 2: Subtract max and compute exp (SIMD exp)
        let shifted: Vec<f32> = input.iter().map(|x| x - max).collect();
        let n = shifted.len();
        // Uninit: simd_exp writes output[i] = exp(input[i]) for every i.
        let mut exp_vals: Vec<f32> = Vec::with_capacity(n);
        // SAFETY: simd_exp writes every element (SET, not accumulate).
        unsafe {
            exp_vals.set_len(n);
        }
        Self::simd_exp(&shifted, &mut exp_vals, backend);

        // Step 3: Sum (SIMD sum)
        let exp_sum = Self::simd_sum(&exp_vals, backend);

        // Step 4: Normalize (SIMD scale, guard against sum=0)
        let inv_sum = 1.0 / exp_sum.max(f32::EPSILON);
        // Uninit: simd_scale writes output[i] = input[i] * scalar for every i.
        let mut result: Vec<f32> = Vec::with_capacity(n);
        // SAFETY: simd_scale writes every element (SET, not accumulate).
        unsafe {
            result.set_len(n);
        }
        Self::simd_scale(&exp_vals, inv_sum, &mut result, backend);

        Ok(result)
    }

    fn tokens(&self, input: &Self::Input) -> usize {
        input.len()
    }
}

impl SoftmaxOp {
    /// Check if backend supports SIMD acceleration
    #[inline]
    pub fn is_simd_backend(backend: Backend) -> bool {
        matches!(
            backend,
            Backend::Avx2 | Backend::Avx512 | Backend::Sse2 | Backend::Neon | Backend::Auto
        )
    }

    /// SIMD-accelerated max reduction
    #[inline]
    fn simd_max(input: &[f32], backend: Backend) -> f32 {
        #[cfg(target_arch = "x86_64")]
        {
            if Self::is_simd_backend(backend) && is_x86_feature_detected!("avx2") {
                // SAFETY: AVX2 availability confirmed by is_x86_feature_detected!() on preceding line.
                return unsafe { Self::avx2_max(input) };
            }
        }
        let _ = backend; // suppress warning on non-x86
                         // Scalar fallback
        input.iter().cloned().fold(f32::NEG_INFINITY, f32::max)
    }

    /// SIMD-accelerated exp using polynomial approximation (SIMD-EXP)
    ///
    /// Uses 6th-degree Remez minimax polynomial matching llama.cpp's ggml_v_expf.
    /// Range reduction: exp(x) = 2^k * e^r where r in [-ln(2)/2, ln(2)/2]
    #[inline]
    fn simd_exp(input: &[f32], output: &mut [f32], backend: Backend) {
        #[cfg(target_arch = "x86_64")]
        {
            if Self::is_simd_backend(backend) && is_x86_feature_detected!("avx2") {
                // SAFETY: AVX2 availability confirmed by is_x86_feature_detected!() on preceding line.
                unsafe { Self::avx2_exp(input, output) };
                return;
            }
        }
        let _ = backend; // suppress warning on non-x86
                         // Scalar fallback
        for (i, &x) in input.iter().enumerate() {
            output[i] = x.exp();
        }
    }

    /// SIMD-accelerated sum reduction
    #[inline]
    fn simd_sum(input: &[f32], backend: Backend) -> f32 {
        #[cfg(target_arch = "x86_64")]
        {
            if Self::is_simd_backend(backend) && is_x86_feature_detected!("avx2") {
                // SAFETY: AVX2 availability confirmed by is_x86_feature_detected!() on preceding line.
                return unsafe { Self::avx2_sum(input) };
            }
        }
        let _ = backend; // suppress warning on non-x86
                         // Scalar fallback
        input.iter().sum()
    }

    /// SIMD-accelerated scale
    #[inline]
    fn simd_scale(input: &[f32], scalar: f32, output: &mut [f32], backend: Backend) {
        #[cfg(target_arch = "x86_64")]
        {
            if Self::is_simd_backend(backend) && is_x86_feature_detected!("avx2") {
                // SAFETY: AVX2 availability confirmed by is_x86_feature_detected!() on preceding line.
                unsafe { Self::avx2_scale(input, scalar, output) };
                return;
            }
        }
        let _ = backend; // suppress warning on non-x86
                         // Scalar fallback
        for (i, &x) in input.iter().enumerate() {
            output[i] = x * scalar;
        }
    }

    // AVX2 implementations

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    // SAFETY: caller verifies AVX2 support, input slices meet alignment/length requirements
    unsafe fn avx2_max(input: &[f32]) -> f32 {
        unsafe {
            use std::arch::x86_64::*;
            let len = input.len();
            let mut i = 0;
            let mut vmax = _mm256_set1_ps(f32::NEG_INFINITY);

            while i + 8 <= len {
                let v = _mm256_loadu_ps(input.as_ptr().add(i));
                vmax = _mm256_max_ps(vmax, v);
                i += 8;
            }

            // Horizontal max
            let high = _mm256_extractf128_ps(vmax, 1);
            let low = _mm256_castps256_ps128(vmax);
            let max128 = _mm_max_ps(high, low);
            let max64 = _mm_max_ps(max128, _mm_movehl_ps(max128, max128));
            let max32 = _mm_max_ss(max64, _mm_shuffle_ps(max64, max64, 1));
            let mut result = _mm_cvtss_f32(max32);

            // Handle remainder
            for &val in &input[i..] {
                result = result.max(val);
            }
            result
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2", enable = "fma")]
    // SAFETY: caller verifies AVX2 support, input slices meet alignment/length requirements
    unsafe fn avx2_exp(input: &[f32], output: &mut [f32]) {
        unsafe {
            use std::arch::x86_64::*;

            let len = input.len();
            let mut i = 0;

            // Constants for range reduction (matches llama.cpp ggml_v_expf)
            let log2e = _mm256_set1_ps(std::f32::consts::LOG2_E);
            let ln2 = _mm256_set1_ps(std::f32::consts::LN_2);
            let half = _mm256_set1_ps(0.5);
            let one = _mm256_set1_ps(1.0);

            // Remez minimax polynomial coefficients for e^r on [-ln(2)/2, ln(2)/2]
            let c1 = _mm256_set1_ps(1.0);
            let c2 = _mm256_set1_ps(0.5);
            let c3 = _mm256_set1_ps(0.166_666_67);
            let c4 = _mm256_set1_ps(0.041_666_668);
            let c5 = _mm256_set1_ps(0.008_333_334);
            let c6 = _mm256_set1_ps(0.001_388_889);

            let exp_hi = _mm256_set1_ps(88.376_26);
            let exp_lo = _mm256_set1_ps(-87.336_55);

            while i + 8 <= len {
                let x = _mm256_loadu_ps(input.as_ptr().add(i));
                let x = _mm256_max_ps(_mm256_min_ps(x, exp_hi), exp_lo);

                // Range reduction: x' = x * log2(e), k = round(x'), r = (x' - k) * ln2
                let fx = _mm256_fmadd_ps(x, log2e, half);
                let fx = _mm256_floor_ps(fx);
                let r = _mm256_fnmadd_ps(fx, ln2, x);

                // Polynomial: e^r ≈ 1 + r + r²/2 + r³/6 + r⁴/24 + r⁵/120 + r⁶/720
                // Using Horner's method for efficient evaluation
                let p = _mm256_fmadd_ps(c6, r, c5);
                let p = _mm256_fmadd_ps(p, r, c4);
                let p = _mm256_fmadd_ps(p, r, c3);
                let p = _mm256_fmadd_ps(p, r, c2);
                let p = _mm256_fmadd_ps(p, r, c1);
                let p = _mm256_fmadd_ps(p, r, one);

                // Scale by 2^k using integer exponent manipulation
                let k = _mm256_cvtps_epi32(fx);
                let k = _mm256_add_epi32(k, _mm256_set1_epi32(127));
                let k = _mm256_slli_epi32(k, 23);
                let pow2k = _mm256_castsi256_ps(k);
                let result = _mm256_mul_ps(p, pow2k);

                _mm256_storeu_ps(output.as_mut_ptr().add(i), result);
                i += 8;
            }

            // Scalar remainder
            for j in i..len {
                output[j] = input[j].exp();
            }
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    // SAFETY: caller verifies AVX2 support, input slices meet alignment/length requirements
    unsafe fn avx2_sum(input: &[f32]) -> f32 {
        unsafe {
            use std::arch::x86_64::*;
            let len = input.len();
            let mut i = 0;
            let mut acc = _mm256_setzero_ps();

            while i + 8 <= len {
                let v = _mm256_loadu_ps(input.as_ptr().add(i));
                acc = _mm256_add_ps(acc, v);
                i += 8;
            }

            // Horizontal sum
            let high = _mm256_extractf128_ps(acc, 1);
            let low = _mm256_castps256_ps128(acc);
            let sum128 = _mm_add_ps(high, low);
            let sum64 = _mm_add_ps(sum128, _mm_movehl_ps(sum128, sum128));
            let sum32 = _mm_add_ss(sum64, _mm_shuffle_ps(sum64, sum64, 1));
            let mut result = _mm_cvtss_f32(sum32);

            // Handle remainder
            for &val in &input[i..] {
                result += val;
            }
            result
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    // SAFETY: caller verifies AVX2 support, input slices meet alignment/length requirements
    unsafe fn avx2_scale(input: &[f32], scalar: f32, output: &mut [f32]) {
        unsafe {
            use std::arch::x86_64::*;
            let len = input.len();
            let mut i = 0;
            let vscalar = _mm256_set1_ps(scalar);

            while i + 8 <= len {
                let v = _mm256_loadu_ps(input.as_ptr().add(i));
                let result = _mm256_mul_ps(v, vscalar);
                _mm256_storeu_ps(output.as_mut_ptr().add(i), result);
                i += 8;
            }

            // Scalar remainder
            for j in i..len {
                output[j] = input[j] * scalar;
            }
        }
    }
}

#[cfg(test)]
mod tests;