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
//! Backend implementations for different SIMD instruction sets
//!
//! This module contains the actual SIMD implementations for each backend.
//! All backends implement the same trait-based interface to ensure API consistency.
//!
//! # Safety
//!
//! All `unsafe` code is isolated within backend implementations. The public API
//! remains 100% safe.
//!
//! # Backends
//!
//! - `scalar`: Portable baseline implementation (no SIMD)
//! - `sse2`: x86_64 baseline SIMD (128-bit)
//! - `avx2`: x86_64 advanced SIMD (256-bit with FMA)
//! - `avx512`: x86_64 maximum SIMD (512-bit)
//! - `neon`: ARM SIMD (128-bit)
//! - `wasm`: WebAssembly SIMD128

pub mod q4k;
pub mod q6k;
pub mod scalar;

#[cfg(target_arch = "x86_64")]
pub mod sse2;

#[cfg(target_arch = "x86_64")]
pub mod avx2;

#[cfg(target_arch = "x86_64")]
#[cfg(test)]
mod avx2_tests;

#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
pub mod neon;

#[cfg(target_arch = "wasm32")]
pub mod wasm;

// GPU module - always available for TensorView/PartitionView abstractions
// Actual GPU compute requires "gpu" feature
pub mod gpu;

#[cfg(target_arch = "x86_64")]
pub mod avx512;

/// Backend trait defining common operations
///
/// All backend implementations must implement this trait to ensure
/// consistent behavior across different SIMD instruction sets.
///
/// # Safety
///
/// Implementations may use unsafe SIMD intrinsics. Callers must ensure:
/// - Input slices are valid
/// - Result slice has sufficient capacity
/// - Slices `a` and `b` have the same length
pub trait VectorBackend {
    /// Element-wise addition: a\[i\] + b\[i\]
    ///
    /// # Safety
    ///
    /// - `a` and `b` must have the same length
    /// - `result` must have length >= `a.len()`
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn add(a: &[f32], b: &[f32], result: &mut [f32]);

    /// Element-wise subtraction: a\[i\] - b\[i\]
    ///
    /// # Safety
    ///
    /// - `a` and `b` must have the same length
    /// - `result` must have length >= `a.len()`
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn sub(a: &[f32], b: &[f32], result: &mut [f32]);

    /// Element-wise multiplication: a\[i\] * b\[i\]
    ///
    /// # Safety
    ///
    /// - `a` and `b` must have the same length
    /// - `result` must have length >= `a.len()`
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn mul(a: &[f32], b: &[f32], result: &mut [f32]);

    /// Element-wise division: a\[i\] / b\[i\]
    ///
    /// # Safety
    ///
    /// - `a` and `b` must have the same length
    /// - `result` must have length >= `a.len()`
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn div(a: &[f32], b: &[f32], result: &mut [f32]);

    /// Dot product: sum(a\[i\] * b\[i\])
    ///
    /// # Safety
    ///
    /// - `a` and `b` must have the same length
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn dot(a: &[f32], b: &[f32]) -> f32;

    /// Sum reduction: sum(a\[i\])
    ///
    /// # Safety
    ///
    /// - `a` must not be empty
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn sum(a: &[f32]) -> f32;

    /// Max reduction: max(a\[i\])
    ///
    /// # Safety
    ///
    /// - `a` must not be empty
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn max(a: &[f32]) -> f32;

    /// Min reduction: min(a\[i\])
    ///
    /// # Safety
    ///
    /// - `a` must not be empty
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn min(a: &[f32]) -> f32;

    /// Argmax: index of maximum value
    ///
    /// Returns the index of the first occurrence of the maximum value.
    ///
    /// # Safety
    ///
    /// - `a` must not be empty
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn argmax(a: &[f32]) -> usize;

    /// Argmin: index of minimum value
    ///
    /// Returns the index of the first occurrence of the minimum value.
    ///
    /// # Safety
    ///
    /// - `a` must not be empty
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn argmin(a: &[f32]) -> usize;

    /// Kahan summation: numerically stable sum(a\[i\])
    ///
    /// Uses the Kahan summation algorithm to reduce floating-point rounding errors
    /// when summing many numbers. Tracks a running compensation for lost low-order bits.
    ///
    /// # Safety
    ///
    /// - Can handle empty slice (returns 0.0)
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn sum_kahan(a: &[f32]) -> f32;

    /// L2 norm (Euclidean norm): sqrt(sum(a\[i\]^2))
    ///
    /// Computes the Euclidean length of the vector. This is equivalent to sqrt(dot(a, a)).
    ///
    /// # Safety
    ///
    /// - Can handle empty slice (returns 0.0)
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn norm_l2(a: &[f32]) -> f32;

    /// L1 norm (Manhattan norm): sum(|a\[i\]|)
    ///
    /// Computes the sum of absolute values of all elements.
    /// Used in machine learning (L1 regularization), distance metrics, and sparse modeling.
    ///
    /// # Safety
    ///
    /// - Can handle empty slice (returns 0.0)
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn norm_l1(a: &[f32]) -> f32;

    /// L-infinity norm (maximum absolute value): max(|a\[i\]|)
    ///
    /// Computes the maximum absolute value of all elements.
    /// Used in optimization (constraint checking), numerical analysis, and error bounds.
    ///
    /// # Safety
    ///
    /// - Can handle empty slice (returns 0.0)
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn norm_linf(a: &[f32]) -> f32;

    /// Scalar multiplication: result\[i\] = a\[i\] * scalar
    ///
    /// Multiplies all elements by a scalar value.
    /// Used in vector scaling, normalization, and linear transformations.
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slice
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn scale(a: &[f32], scalar: f32, result: &mut [f32]);

    /// Absolute value: result\[i\] = |a\[i\]|
    ///
    /// Computes the absolute value of each element.
    /// Used in distance metrics (L1 norm), numerical stability, and signal processing.
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slice
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn abs(a: &[f32], result: &mut [f32]);

    /// Clamp elements to range [min_val, max_val]: result\[i\] = max(min_val, min(a\[i\], max_val))
    ///
    /// Constrains each element to the specified range.
    /// Used in neural networks (gradient clipping), graphics (color clamping), and signal processing.
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slice
    /// - Assumes min_val <= max_val (caller must validate)
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn clamp(a: &[f32], min_val: f32, max_val: f32, result: &mut [f32]);

    /// Linear interpolation: result\[i\] = a\[i\] + t * (b\[i\] - a\[i\])
    ///
    /// Computes element-wise linear interpolation between two vectors.
    /// When t=0, returns a; when t=1, returns b; values outside \[0,1\] extrapolate.
    /// Used in graphics, animation, neural networks, and signal processing.
    ///
    /// # Safety
    ///
    /// - `a` and `b` must have the same length
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn lerp(a: &[f32], b: &[f32], t: f32, result: &mut [f32]);

    /// Fused multiply-add: result\[i\] = a\[i\] * b\[i\] + c\[i\]
    ///
    /// Computes element-wise fused multiply-add operation.
    /// On hardware with FMA support, this is a single instruction with better performance
    /// and numerical accuracy (no intermediate rounding).
    /// Used in neural networks, matrix multiplication, and scientific computing.
    ///
    /// # Safety
    ///
    /// - `a`, `b`, and `c` must all have the same length
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn fma(a: &[f32], b: &[f32], c: &[f32], result: &mut [f32]);

    /// ReLU activation: result\[i\] = max(0, a\[i\])
    ///
    /// Rectified Linear Unit - the most common activation function in neural networks.
    /// Sets negative values to zero, passes positive values unchanged.
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn relu(a: &[f32], result: &mut [f32]);

    /// Exponential function: result\[i\] = exp(a\[i\])
    ///
    /// Computes e^x for each element using range reduction for numerical accuracy.
    /// Foundation for sigmoid, softmax, GELU, and other activation functions.
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn exp(a: &[f32], result: &mut [f32]);

    /// Sigmoid activation: result\[i\] = 1 / (1 + exp(-a\[i\]))
    ///
    /// Logistic sigmoid function - maps inputs to (0, 1) range.
    /// Used in binary classification and as gating mechanism.
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn sigmoid(a: &[f32], result: &mut [f32]);

    /// GELU activation: result\[i\] = 0.5 * x * (1 + tanh(sqrt(2/π) * (x + 0.044715 * x³)))
    ///
    /// Gaussian Error Linear Unit - smooth non-monotonic activation.
    /// Used in BERT, GPT, and modern transformers.
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn gelu(a: &[f32], result: &mut [f32]);

    /// Swish activation: result\[i\] = x * sigmoid(x) = x / (1 + exp(-x))
    ///
    /// Self-gated activation function (also called SiLU).
    /// Used in EfficientNet, MobileNetV3.
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn swish(a: &[f32], result: &mut [f32]);

    /// Hyperbolic tangent activation: result\[i\] = tanh(a\[i\]) = (exp(2x) - 1) / (exp(2x) + 1)
    ///
    /// Hyperbolic tangent - maps inputs to (-1, 1) range.
    /// Classic activation function from early neural networks.
    /// Used in RNNs, LSTMs, and as smooth alternative to ReLU.
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn tanh(a: &[f32], result: &mut [f32]);

    /// Square root: result\[i\] = sqrt(a\[i\])
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn sqrt(a: &[f32], result: &mut [f32]);

    /// Reciprocal: result\[i\] = 1 / a\[i\]
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn recip(a: &[f32], result: &mut [f32]);

    /// Natural logarithm: result\[i\] = ln(a\[i\])
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn ln(a: &[f32], result: &mut [f32]);

    /// Base-2 logarithm: result\[i\] = log2(a\[i\])
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn log2(a: &[f32], result: &mut [f32]);

    /// Base-10 logarithm: result\[i\] = log10(a\[i\])
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn log10(a: &[f32], result: &mut [f32]);

    /// Sine: result\[i\] = sin(a\[i\])
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn sin(a: &[f32], result: &mut [f32]);

    /// Cosine: result\[i\] = cos(a\[i\])
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn cos(a: &[f32], result: &mut [f32]);

    /// Tangent: result\[i\] = tan(a\[i\])
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn tan(a: &[f32], result: &mut [f32]);

    /// Floor: result\[i\] = floor(a\[i\])
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn floor(a: &[f32], result: &mut [f32]);

    /// Ceiling: result\[i\] = ceil(a\[i\])
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn ceil(a: &[f32], result: &mut [f32]);

    /// Round: result\[i\] = round(a\[i\])
    ///
    /// # Safety
    ///
    /// - `result` must have the same length as `a`
    /// - Can handle empty slices
    // SAFETY: Caller must satisfy the documented preconditions for slice validity
    unsafe fn round(a: &[f32], result: &mut [f32]);
}