sklears-simd 0.1.1

High-performance SIMD acceleration primitives for the Sklears machine learning ecosystem
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
//! Basic SIMD Vector Operations
//!
//! This module provides fundamental SIMD-accelerated vector operations including
//! dot products, norms, scaling, and basic arithmetic operations optimized for
//! different SIMD instruction sets.

#[cfg(feature = "no-std")]
use alloc::vec;
#[cfg(feature = "no-std")]
use alloc::vec::Vec;
#[cfg(not(feature = "no-std"))]
use std::vec::Vec;

use crate::simd_types::*;
use crate::simd_utils::*;

/// SIMD-optimized dot product computation
///
/// Computes the dot product of two vectors using the best available SIMD
/// instruction set. Falls back to optimized scalar implementation if SIMD
/// is not available.
///
/// # Arguments
/// * `a` - First input vector
/// * `b` - Second input vector
///
/// # Returns
/// The dot product of the two vectors
///
/// # Panics
/// Panics if the vectors have different lengths
///
/// # Examples
/// ```rust,ignore
/// let a = vec![1.0, 2.0, 3.0, 4.0];
/// let b = vec![5.0, 6.0, 7.0, 8.0];
/// let result = dot_product(&a, &b);
/// assert_eq!(result, 70.0); // 1*5 + 2*6 + 3*7 + 4*8
/// ```
pub fn dot_product(a: &[f32], b: &[f32]) -> f32 {
    assert_eq!(a.len(), b.len(), "Vectors must have the same length");

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if crate::simd_feature_detected!("avx512f") {
            return unsafe { dot_product_avx512(a, b) };
        } else if crate::simd_feature_detected!("avx2") {
            return unsafe { dot_product_avx2(a, b) };
        } else if crate::simd_feature_detected!("sse2") {
            return unsafe { dot_product_sse2(a, b) };
        }
    }

    #[cfg(target_arch = "aarch64")]
    {
        return unsafe { dot_product_neon(a, b) };
    }

    #[cfg(not(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")))]
    {
        dot_product_scalar(a, b)
    }
}

/// SIMD-optimized L2 norm computation
///
/// Computes the L2 (Euclidean) norm of a vector: √(Σᵢ xᵢ²)
///
/// # Arguments
/// * `x` - Input vector
///
/// # Returns
/// The L2 norm of the vector
///
/// # Examples
/// ```rust,ignore
/// let x = vec![3.0, 4.0];
/// let result = norm_l2(&x);
/// assert_eq!(result, 5.0); // √(3² + 4²) = √25 = 5
/// ```
pub fn norm_l2(x: &[f32]) -> f32 {
    dot_product(x, x).sqrt()
}

/// Alternative norm function for compatibility
pub fn norm(vector: &[f32]) -> f32 {
    norm_l2(vector)
}

/// SIMD-optimized vector scaling
///
/// Scales all elements of a vector by a scalar value in-place.
///
/// # Arguments
/// * `vector` - Vector to scale (modified in-place)
/// * `scalar` - Scaling factor
///
/// # Examples
/// ```rust,ignore
/// let mut x = vec![1.0, 2.0, 3.0, 4.0];
/// scale(&mut x, 2.0);
/// assert_eq!(x, vec![2.0, 4.0, 6.0, 8.0]);
/// ```
pub fn scale(vector: &mut [f32], scalar: f32) {
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if crate::simd_feature_detected!("avx512f") {
            return unsafe { scale_avx512(vector, scalar) };
        } else if crate::simd_feature_detected!("avx2") {
            return unsafe { scale_avx2(vector, scalar) };
        } else if crate::simd_feature_detected!("sse2") {
            return unsafe { scale_sse2(vector, scalar) };
        }
    }

    #[cfg(target_arch = "aarch64")]
    {
        return unsafe { scale_neon(vector, scalar) };
    }

    // Scalar fallback
    for element in vector.iter_mut() {
        *element *= scalar;
    }
}

/// SIMD-optimized fused multiply-add operation
///
/// Computes a\[i\] = a\[i\] * b\[i\] + c\[i\] for all elements.
/// This is equivalent to a\[i\] * b\[i\] + c\[i\] but may be more efficient
/// on platforms with dedicated FMA instructions.
///
/// # Arguments
/// * `a` - First input vector (modified in-place)
/// * `b` - Second input vector
/// * `c` - Third input vector
///
/// # Panics
/// Panics if vectors have different lengths
///
/// # Examples
/// ```rust,ignore
/// let mut a = vec![1.0, 2.0, 3.0];
/// let b = vec![4.0, 5.0, 6.0];
/// let c = vec![7.0, 8.0, 9.0];
/// fma(&mut a, &b, &c);
/// // a[0] = 1.0 * 4.0 + 7.0 = 11.0
/// // a[1] = 2.0 * 5.0 + 8.0 = 18.0
/// // a[2] = 3.0 * 6.0 + 9.0 = 27.0
/// assert_eq!(a, vec![11.0, 18.0, 27.0]);
/// ```
pub fn fma(a: &mut [f32], b: &[f32], c: &[f32]) {
    assert_eq!(a.len(), b.len(), "Vectors must have the same length");
    assert_eq!(a.len(), c.len(), "Vectors must have the same length");

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if crate::simd_feature_detected!("fma") {
            return unsafe { fma_avx2_fma(a, b, c) };
        } else if crate::simd_feature_detected!("avx2") {
            return unsafe { fma_avx2(a, b, c) };
        } else if crate::simd_feature_detected!("sse2") {
            return unsafe { fma_sse2(a, b, c) };
        }
    }

    #[cfg(target_arch = "aarch64")]
    {
        return unsafe { fma_neon(a, b, c) };
    }

    // Scalar fallback
    for i in 0..a.len() {
        a[i] = a[i] * b[i] + c[i];
    }
}

/// Vector addition returning a new vector
///
/// Computes element-wise addition of two vectors.
///
/// # Arguments
/// * `a` - First input vector
/// * `b` - Second input vector
///
/// # Returns
/// New vector containing the element-wise sum
///
/// # Panics
/// Panics if vectors have different lengths
pub fn add_vectors(a: &[f32], b: &[f32]) -> Vec<f32> {
    assert_eq!(a.len(), b.len(), "Vectors must have the same length");

    let mut result = vec![0.0; a.len()];

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if crate::simd_feature_detected!("avx2") {
            unsafe { add_avx2(a, b, &mut result) };
            return result;
        } else if crate::simd_feature_detected!("sse2") {
            unsafe { add_sse2(a, b, &mut result) };
            return result;
        }
    }

    #[cfg(target_arch = "aarch64")]
    {
        unsafe { add_neon(a, b, &mut result) };
        return result;
    }

    // Scalar fallback
    for i in 0..a.len() {
        result[i] = a[i] + b[i];
    }

    result
}

/// Fused multiply-add returning a new vector
///
/// Computes a\[i\] * b\[i\] + c\[i\] for all elements, returning a new vector.
///
/// # Arguments
/// * `a` - First input vector
/// * `b` - Second input vector
/// * `c` - Third input vector
///
/// # Returns
/// New vector containing the FMA results
pub fn fused_multiply_add(a: &[f32], b: &[f32], c: &[f32]) -> Vec<f32> {
    assert_eq!(a.len(), b.len(), "Vectors must have the same length");
    assert_eq!(a.len(), c.len(), "Vectors must have the same length");

    let mut result = a.to_vec();
    fma(&mut result, b, c);
    result
}

/// Vector subtraction returning a new vector
///
/// Computes element-wise subtraction of two vectors.
///
/// # Arguments
/// * `a` - First input vector
/// * `b` - Second input vector
///
/// # Returns
/// New vector containing a\[i\] - b\[i\]
pub fn subtract_vectors(a: &[f32], b: &[f32]) -> Vec<f32> {
    assert_eq!(a.len(), b.len(), "Vectors must have the same length");

    let mut result = vec![0.0; a.len()];

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if crate::simd_feature_detected!("avx2") {
            unsafe { subtract_avx2(a, b, &mut result) };
            return result;
        } else if crate::simd_feature_detected!("sse2") {
            unsafe { subtract_sse2(a, b, &mut result) };
            return result;
        }
    }

    #[cfg(target_arch = "aarch64")]
    {
        unsafe { subtract_neon(a, b, &mut result) };
        return result;
    }

    // Scalar fallback
    for i in 0..a.len() {
        result[i] = a[i] - b[i];
    }

    result
}

/// Element-wise vector multiplication
///
/// Computes element-wise multiplication of two vectors.
///
/// # Arguments
/// * `a` - First input vector
/// * `b` - Second input vector
///
/// # Returns
/// New vector containing a\[i\] * b\[i\]
pub fn multiply_vectors(a: &[f32], b: &[f32]) -> Vec<f32> {
    assert_eq!(a.len(), b.len(), "Vectors must have the same length");

    let mut result = vec![0.0; a.len()];

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if crate::simd_feature_detected!("avx2") {
            unsafe { multiply_avx2(a, b, &mut result) };
            return result;
        } else if crate::simd_feature_detected!("sse2") {
            unsafe { multiply_sse2(a, b, &mut result) };
            return result;
        }
    }

    #[cfg(target_arch = "aarch64")]
    {
        unsafe { multiply_neon(a, b, &mut result) };
        return result;
    }

    // Scalar fallback
    for i in 0..a.len() {
        result[i] = a[i] * b[i];
    }

    result
}

/// Vector scaling returning a new vector
///
/// Scales all elements by a scalar value, returning a new vector.
///
/// # Arguments
/// * `vector` - Input vector
/// * `scalar` - Scaling factor
///
/// # Returns
/// New vector with scaled values
pub fn scale_vector(vector: &[f32], scalar: f32) -> Vec<f32> {
    let mut result = vector.to_vec();
    scale(&mut result, scalar);
    result
}

// Scalar implementations
fn dot_product_scalar(a: &[f32], b: &[f32]) -> f32 {
    a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
}

#[allow(non_snake_case)]
#[cfg(all(test, not(feature = "no-std")))]
mod tests {
    use super::*;

    #[test]
    fn test_dot_product() {
        let a = vec![1.0, 2.0, 3.0, 4.0];
        let b = vec![5.0, 6.0, 7.0, 8.0];
        let result = dot_product(&a, &b);
        assert!((result - 70.0).abs() < 1e-6);
    }

    #[test]
    fn test_norm_l2() {
        let x = vec![3.0, 4.0];
        let result = norm_l2(&x);
        assert!((result - 5.0).abs() < 1e-6);
    }

    #[test]
    fn test_scale() {
        let mut x = vec![1.0, 2.0, 3.0, 4.0];
        scale(&mut x, 2.0);
        assert_eq!(x, vec![2.0, 4.0, 6.0, 8.0]);
    }

    #[test]
    fn test_fma() {
        let mut a = vec![1.0, 2.0, 3.0];
        let b = vec![4.0, 5.0, 6.0];
        let c = vec![7.0, 8.0, 9.0];
        fma(&mut a, &b, &c);
        assert_eq!(a, vec![11.0, 18.0, 27.0]);
    }

    #[test]
    fn test_add_vectors() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![4.0, 5.0, 6.0];
        let result = add_vectors(&a, &b);
        assert_eq!(result, vec![5.0, 7.0, 9.0]);
    }

    #[test]
    fn test_subtract_vectors() {
        let a = vec![5.0, 7.0, 9.0];
        let b = vec![1.0, 2.0, 3.0];
        let result = subtract_vectors(&a, &b);
        assert_eq!(result, vec![4.0, 5.0, 6.0]);
    }

    #[test]
    fn test_multiply_vectors() {
        let a = vec![2.0, 3.0, 4.0];
        let b = vec![5.0, 6.0, 7.0];
        let result = multiply_vectors(&a, &b);
        assert_eq!(result, vec![10.0, 18.0, 28.0]);
    }

    #[test]
    fn test_fused_multiply_add() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![4.0, 5.0, 6.0];
        let c = vec![7.0, 8.0, 9.0];
        let result = fused_multiply_add(&a, &b, &c);
        assert_eq!(result, vec![11.0, 18.0, 27.0]);
    }

    #[test]
    fn test_scale_vector() {
        let x = vec![1.0, 2.0, 3.0];
        let result = scale_vector(&x, 3.0);
        assert_eq!(result, vec![3.0, 6.0, 9.0]);
    }

    #[test]
    #[should_panic(expected = "Vectors must have the same length")]
    fn test_dot_product_mismatched_lengths() {
        let a = vec![1.0, 2.0];
        let b = vec![1.0, 2.0, 3.0];
        dot_product(&a, &b);
    }
}