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
//! SIMD-vectorized implementations of various math functions that are commonly
//! used in neural networks.
//!
//! For each function in this library there are multiple variants, which
//! typically include:
//!
//!  - A version that operates on scalars
//!  - A version that reads values from an input slice and writes to the
//!    corresponding position in an equal-length output slice. These have a
//!    `vec_` prefix.
//!  - A version that reads values from a mutable input slice and writes
//!    the computed values back in-place. These have a `vec_` prefix and
//!    `_in_place` suffix.
//!
//! All variants use the same underlying implementation and should have the
//! same accuracy.
//!
//! See the source code for comments on accuracy.

mod erf;
mod exp;
mod simd_vec;
mod softmax;
mod tanh;
mod ulp;

#[cfg(test)]
mod testing;

pub use erf::{erf, vec_erf, vec_erf_in_place};
pub use exp::{exp, sigmoid, vec_exp, vec_exp_in_place, vec_sigmoid, vec_sigmoid_in_place};
use simd_vec::SimdFloat;
pub use softmax::{vec_softmax, vec_softmax_in_place};
pub use tanh::{tanh, vec_tanh, vec_tanh_in_place};

/// Maximum SIMD vector size supported by this library, in units of 32-byte lanes.
///
/// Chosen as 16 to match AVX-512.
const MAX_LEN: usize = 16;

/// Const pointer to a range of `T`s.
///
/// This is like an `&[T]`, but without the guarantee that no mutable aliases
/// exist. This is useful as it enables re-using the same unsafe code for
/// mutating and non-mutating variants of a function.
#[derive(Copy, Clone)]
struct PtrLen<T> {
    ptr: *const T,
    len: usize,
}

impl<'a, T> From<&'a [T]> for PtrLen<T> {
    fn from(val: &'a [T]) -> PtrLen<T> {
        PtrLen {
            ptr: val.as_ptr(),
            len: val.len(),
        }
    }
}

impl<'a, T> From<&'a mut [T]> for PtrLen<T> {
    fn from(val: &'a mut [T]) -> PtrLen<T> {
        PtrLen {
            ptr: val.as_ptr(),
            len: val.len(),
        }
    }
}

impl<T> From<MutPtrLen<T>> for PtrLen<T> {
    fn from(val: MutPtrLen<T>) -> PtrLen<T> {
        PtrLen {
            ptr: val.ptr,
            len: val.len,
        }
    }
}

/// Mutable pointer to a range of `T`s.
///
/// This is like an `&mut [T]`, but without the guarantee that no aliases exist.
#[derive(Copy, Clone)]
struct MutPtrLen<T> {
    ptr: *mut T,
    len: usize,
}

impl<'a, T> From<&'a mut [T]> for MutPtrLen<T> {
    fn from(val: &'a mut [T]) -> MutPtrLen<T> {
        MutPtrLen {
            ptr: val.as_mut_ptr(),
            len: val.len(),
        }
    }
}

/// Apply a unary operation to each element in `xs` and store the results in
/// `out`.
///
/// The operation is applied to SIMD vector-sized groups of elements at
/// a time using `simd_op`. If the final group has a size that is smaller than
/// the SIMD vector width, `simd_op` will be called with a SIMD vector that
/// is padded with `pad` on the right.
///
/// Safety: The caller must ensure that `xs` and `out` are valid pointers
/// to buffers of the expected lengths.
#[cfg_attr(target_arch = "x86_64", target_feature(enable = "avx2"))]
#[cfg_attr(target_arch = "x86_64", target_feature(enable = "fma"))]
unsafe fn vec_unary_op<S: SimdFloat, Op: FnMut(S) -> S>(
    xs: PtrLen<f32>,
    out: MutPtrLen<f32>,
    mut simd_op: Op,
    pad: f32,
) {
    assert!(xs.len == out.len);

    let mut n = xs.len;
    let mut x_ptr = xs.ptr;
    let mut out_ptr = out.ptr;

    // S::LEN can't be used as the array size due to const generics limitations.
    const MAX_LEN: usize = 16;
    assert!(S::LEN <= MAX_LEN);
    let mut remainder = [pad; MAX_LEN];

    // Main loop over full vectors.
    while n >= S::LEN {
        let x = S::load(x_ptr);
        let y = simd_op(x);
        y.store(out_ptr);

        n -= S::LEN;
        x_ptr = x_ptr.add(S::LEN);
        out_ptr = out_ptr.add(S::LEN);
    }

    // Handler remainder with a padded vector.
    if n > 0 {
        for i in 0..n {
            remainder[i] = *x_ptr.add(i);
        }

        let x = S::load(remainder.as_ptr());
        let y = simd_op(x);
        y.store(remainder.as_mut_ptr());

        for i in 0..n {
            *out_ptr.add(i) = remainder[i];
        }
    }
}

#[cfg_attr(target_arch = "x86_64", target_feature(enable = "avx2"))]
#[cfg_attr(target_arch = "x86_64", target_feature(enable = "fma"))]
unsafe fn vec_fold<S: SimdFloat, Op: Fn(S, S) -> S>(
    xs: PtrLen<f32>,
    mut accum: S,
    simd_op: Op,
    pad: f32,
) -> S {
    let mut n = xs.len;
    let mut x_ptr = xs.ptr;

    // S::LEN can't be used as the array size due to const generics limitations.
    assert!(S::LEN <= MAX_LEN);
    let mut remainder = [pad; MAX_LEN];

    // Main loop over full vectors.
    while n >= S::LEN {
        let x = S::load(x_ptr);
        accum = simd_op(accum, x);
        n -= S::LEN;
        x_ptr = x_ptr.add(S::LEN);
    }

    // Handler remainder with a padded vector.
    if n > 0 {
        for i in 0..n {
            remainder[i] = *x_ptr.add(i);
        }
        let x = S::load(remainder.as_ptr());
        accum = simd_op(accum, x);
    }

    accum
}

/// Invoke the best available implementation of a unary operator on the current
/// platform.
///
/// This generates a call to [vec_unary_op] for each of the supported
/// instruction sets, passing in a version of `$op_func` that is parametrized by
/// the corresponding SIMD vector type. At runtime the appropriate
/// `vec_unary_op` call will be invoked.
macro_rules! dispatch_unary_op {
    ($in:ident, $out:ident, $op_func:ident, $fallback_func:ident) => {
        #[allow(unused_imports)]
        use crate::{vec_unary_op, MutPtrLen, PtrLen};

        assert!($in.len() == $out.len());

        #[allow(unreachable_code)] // Ignore fallback, if unused
        {
            // Non-generic wrapper for `vec_unary_op` which instantiates the
            // AVX + FMA version.
            #[cfg(target_arch = "x86_64")]
            #[target_feature(enable = "avx2")]
            #[target_feature(enable = "fma")]
            unsafe fn vec_unary_op_avx(xs: PtrLen<f32>, out: MutPtrLen<f32>) {
                use std::arch::x86_64::__m256;
                vec_unary_op(xs, out, |x: __m256| $op_func(x), 0. /* pad */);
            }

            #[cfg(target_arch = "x86_64")]
            if is_x86_feature_detected!("fma") && is_x86_feature_detected!("avx2") {
                // Safety: We've checked that AVX2 + FMA are available.
                unsafe {
                    vec_unary_op_avx($in.into(), $out.into());
                }

                return;
            }

            #[cfg(target_arch = "wasm32")]
            {
                use crate::simd_vec::wasm::v128f;

                // Safety: The WASM runtime will have verified SIMD instructions
                // are accepted when loading the binary.
                unsafe {
                    vec_unary_op(
                        $in.into(),
                        $out.into(),
                        |x: v128f| $op_func(x),
                        0., /* pad */
                    );
                }
                return;
            }

            #[cfg(target_arch = "aarch64")]
            {
                use std::arch::aarch64::float32x4_t;

                unsafe {
                    vec_unary_op(
                        $in.into(),
                        $out.into(),
                        |x: float32x4_t| $op_func(x),
                        0., /* pad */
                    );
                }
                return;
            }

            // Generic fallback.
            for (x, y) in $in.iter().zip($out.iter_mut()) {
                *y = $fallback_func(*x);
            }
        }
    };

    ($out:ident, $op_func:ident, $fallback_func:ident) => {
        #[allow(unused_imports)]
        use crate::{vec_unary_op, MutPtrLen, PtrLen};

        #[allow(unreachable_code)] // Ignore fallback, if unused
        {
            // Non-generic wrapper for `vec_unary_op` which instantiates the
            // AVX + FMA version.
            #[cfg(target_arch = "x86_64")]
            #[target_feature(enable = "avx2")]
            #[target_feature(enable = "fma")]
            unsafe fn vec_unary_op_avx(xs: PtrLen<f32>, out: MutPtrLen<f32>) {
                use std::arch::x86_64::__m256;
                vec_unary_op(xs, out, |x: __m256| $op_func(x), 0. /* pad */);
            }

            #[cfg(target_arch = "x86_64")]
            if is_x86_feature_detected!("fma") && is_x86_feature_detected!("avx2") {
                // Safety: We've checked that AVX2 + FMA are available.
                unsafe {
                    vec_unary_op_avx($out.into(), $out.into());
                }
                return;
            }

            #[cfg(target_arch = "wasm32")]
            {
                use crate::simd_vec::wasm::v128f;

                // Safety: The WASM runtime will have verified SIMD instructions
                // are accepted when loading the binary.
                unsafe {
                    vec_unary_op(
                        $out.into(),
                        $out.into(),
                        |x: v128f| $op_func(x),
                        0., /* pad */
                    );
                }
                return;
            }

            #[cfg(target_arch = "aarch64")]
            {
                use std::arch::aarch64::float32x4_t;

                unsafe {
                    vec_unary_op(
                        $out.into(),
                        $out.into(),
                        |x: float32x4_t| $op_func(x),
                        0., /* pad */
                    );
                }
                return;
            }

            // Generic fallback.
            for x in $out.iter_mut() {
                *x = $fallback_func(*x);
            }
        }
    };
}

pub(crate) use dispatch_unary_op;

/// Dispatch a SIMD function using the best available `SimdFloat` implementation
/// on the current system.
///
/// `$func` should be a function with a generic argument `S: SimdFloat`. `$in`
/// and `$out` are the function arguments.
macro_rules! dispatch_simd {
    ($func:ident, $in:expr, $out:expr) => {
        #[allow(unused_imports)]
        use crate::{MutPtrLen, PtrLen};

        #[allow(unreachable_code)] // Ignore fallback, if unused
        {
            // Non-generic wrapper for `$func` which instantiates the AVX + FMA version.
            #[cfg(target_arch = "x86_64")]
            #[target_feature(enable = "avx2")]
            #[target_feature(enable = "fma")]
            unsafe fn simd_op_avx(xs: PtrLen<f32>, out: MutPtrLen<f32>) {
                use std::arch::x86_64::__m256;
                $func::<__m256>(xs, out);
            }

            #[cfg(target_arch = "x86_64")]
            if is_x86_feature_detected!("fma") && is_x86_feature_detected!("avx2") {
                // Safety: We've checked that AVX2 + FMA are available.
                unsafe { simd_op_avx($in, $out) };
                return;
            }

            #[cfg(target_arch = "wasm32")]
            {
                use crate::simd_vec::wasm::v128f;

                // Safety: The WASM runtime will have verified SIMD instructions
                // are accepted when loading the binary.
                unsafe { $func::<v128f>($in, $out) };
                return;
            }

            #[cfg(target_arch = "aarch64")]
            {
                use std::arch::aarch64::float32x4_t;

                unsafe { $func::<float32x4_t>($in, $out) };
                return;
            }

            // Generic fallback.
            unsafe { $func::<f32>($in, $out) };
        }
    };
}

pub(crate) use dispatch_simd;