Expand description
§CFAVML
CF’s Accelerated Vector Math Library
Various accelerated vector operations over Rust primitives with SIMD.
This is the core base library, it has no dependencies and only depends on the core library,
it does not perform any allocations.
This library is guaranteed to be no-std compatible and can be adjusted by disabling the std
feature flag:
§Default Setup
cfavml = "0.3.0" §No-std Setup
cfavml = { version = "0.3.0", default-features = false }§Important Version Upgrade Notes
If you are upgrading on a breaking release, i.e. 0.2.0 to 0.3.0 there may be some important
changes that affects your system, although the public safe APIs I try my best to avoid breaking.
- AVX512 required CPU features changed in
0.3.0+- In versions older than
0.3.0avx512 was used when only theavx512fcpu feature was available since this is the base/foundation version of AVX512. However, in0.3.0we introduced more extensive cmp operations (eq/neq/lt/lte/gt/gte) which changed our required CPU features to includeavx512bw - This means on unsafe APIs you must update your feature checks to include
avx512bw. - Safe APIs do not require changes but may fallback to AVX2 on some of the first gen AVX512 CPUs, i.e. Skylake
- In versions older than
§Available SIMD Architectures
- AVX2
- AVX2 + FMA
- AVX512 (
avx512f+avx512bw) nightly only - NEON
- Fallback (Typically optimized to SSE automatically by LLVM on x86)
§Supported Primitives
f32f64i8i16i32i64u8u16u32u64
§Note on non-f32/f64 division
Division operations on non-floating point primitives are currently still scalar operations, as performing integer division is incredibly hard to do anymore efficiently with SIMD and adds a significant amount of cognitive overhead when reading the code.
Although to be honest I have some serious questions about your application if you’re doing heavy integer division…
§Supported Operations
§Spacial distances
These are routines that can be used for things like KNN classification or index building.
- Dot product of two vectors
- Cosine distance of two vectors
- Squared Euclidean distance of two vectors
§Arithmetic
- Add single value to vector
- Sub single value from vector
- Mul vector by single value
- Div vector by single value
- Add two vectors vertically
- Sub two vectors vertically
- Mul two vectors vertically
- Div two vectors vertically
§Comparison
- Horizontal max element in a vector
- Horizontal min element in a vector
- Vertical max element of two vectors
- Vertical min element of two vectors
- Vertical max element of a vector and broadcast value
- Vertical min element of a vector and broadcast value
- EQ/NEQ/LT/LTE/GT/GTE cmp of a vector and broadcast value
- EQ/NEQ/LT/LTE/GT/GTE cmp of two vectors
§Aggregation
- Horizontal sum of a vector
§Misc
- Squared L2 norm of a vector
§Dangerous routine naming convention
If you’ve looked at the danger folder at all, you’ll notice a few things, one SIMD operations
are gated behind the SimdRegister<T> trait, this provides us with a generic abstraction
over the various SIMD register types and architectures.
This trait, combined with the Math<T> trait form the core of all operations and are
provided as generic functions (with no target features):
generic_dotgeneric_squared_euclideangeneric_cosinegeneric_squared_normgeneric_cmp_maxgeneric_cmp_max_vectorgeneric_cmp_max_valuegeneric_cmp_mingeneric_cmp_min_vectorgeneric_cmp_min_valuegeneric_cmp_eq_vectorgeneric_cmp_eq_valuegeneric_cmp_neq_vectorgeneric_cmp_neq_valuegeneric_cmp_lt_vectorgeneric_cmp_lt_valuegeneric_cmp_lte_vectorgeneric_cmp_lte_valuegeneric_cmp_gt_vectorgeneric_cmp_gt_valuegeneric_cmp_gte_vectorgeneric_cmp_gte_valuegeneric_sumgeneric_add_valuegeneric_sub_valuegeneric_mul_valuegeneric_div_valuegeneric_add_vectorgeneric_sub_vectorgeneric_mul_vectorgeneric_div_vector
We also export functions with the target_features pre-specified for
each SIMD register type and is found under the cfavml::danger::export_*
modules. Although it is not recommended to use these routines directly
unless you know what you are doing.
§Features
nightlyEnables optimizations available only on nightly platforms.- This is required for AVX512 support due to it currently being unstable.
§Is this a replacement for BLAS?
No. At least, not unless you’re only doing dot product… BLAS and LAPACK are huge and I am certainly not in the market for implementing all BLAS routines in Rust, but that being said if your application is similar to that of ndarray where it is only using BLAS for the dot product, then maybe.
Modules§
- buffer
- The core buffer trait used to represent writeable buffers.
- danger
- dispatch
- math
- safe_
trait_ agg_ ops - Safe but somewhat low-level variants of the aggregation operations in CFAVML.
- safe_
trait_ arithmetic_ ops - Safe but somewhat low-level variants of the arithmetic operations in CFAVML.
- safe_
trait_ cmp_ ops - Safe but somewhat low-level variants of the comparison operations in CFAVML.
- safe_
trait_ distance_ ops - Safe but somewhat low-level variants of the distance operations in CFAVML.
Macros§
- dispatch
- Dispatches a set of functions based on the available CPU features.
Functions§
- add_
value - Performs an element wise addition of each element of vector
aand the provided broadcast value, writing the result toresult. - add_
vector - Performs an element wise addition of each element pair of vector
aandb, writing the result toresult. - cosine
- Calculates the cosine similarity distance of vectors
aandb. - div_
value - Performs an element wise division of each element of vector
aand the provided broadcast value, writing the result toresult. - div_
vector - Performs an element wise division on each element pair from vectors
aandb, writing the result toresult. - dot
- Calculates the cosine similarity distance of vectors
aandb. - eq_
value - Checks each element within vector
aof sizedimsagainst a provided broadcast value comparing if they are equal returning a mask vector of the same type. - eq_
vector - Checks each element pair from vectors
aandbof sizedimscomparing if elementais equal to elementbreturning a mask vector of the same type. - gt_
value - Checks each element within vector
aof sizedimsagainst a provided broadcast value comparing if they are less than or equal returning a mask vector of the same type. - gt_
vector - Checks each element pair from vectors
aandbof sizedimscomparing if elementais greater than elementbreturning a mask vector of the same type. - gte_
value - Checks each element within vector
aof sizedimsagainst a provided broadcast value comparing if they are less than or equal returning a mask vector of the same type. - gte_
vector - Checks each element pair from vectors
aandbof sizedimscomparing if elementais greater than elementbreturning a mask vector of the same type. - lt_
value - Checks each element within vector
aof sizedimsagainst a provided broadcast value comparing if they are less than returning a mask vector of the same type. - lt_
vector - Checks each element pair from vectors
aandbof sizedimscomparing if elementais less than elementbreturning a mask vector of the same type. - lte_
value - Checks each element within vector
aof sizedimsagainst a provided broadcast value comparing if they are less than or equal returning a mask vector of the same type. - lte_
vector - Checks each element pair from vectors
aandbof sizedimscomparing if elementais less than or equal to elementbreturning a mask vector of the same type. - max
- Finds the horizontal max element of a given vector and returns the result.
- max_
value - Performs an element wise max on each element of vector
aand the provided broadcast value, writing the result toresult. - max_
vector - Performs an element wise max on each element pair from vectors
aandb, writing the result toresult. - min
- Finds the horizontal min element of a given vector and returns the result.
- min_
value - Performs an element wise min on each element of vector
aand the provided broadcast value, writing the result toresult. - min_
vector - Performs an element wise min on each element pair from vectors
aandb, writing the result toresult. - mul_
value - Performs an element wise multiplication of each element of vector
aand the provided broadcast value, writing the result toresult. - mul_
vector - Performs an element wise multiplication of each element pair from vectors
aandb, writing the result toresult. - neq_
value - Checks each element within vector
aof sizedimsagainst a provided broadcast value comparing if they are not equal returning a mask vector of the same type. - neq_
vector - Checks each element pair from vectors
aandbof sizedimscomparing if elementais not equal to elementbreturning a mask vector of the same type. - squared_
euclidean - Calculates the squared Euclidean distance of vectors
aandb. - squared_
norm - Calculates the squared L2 norm of vector
a. - sub_
value - Performs an element wise subtraction of each element of vector
aand the provided broadcast value, writing the result toresult. - sub_
vector - Performs an element wise subtraction of each element pair from vectors
aandb, writing the result toresult. - sum
- Performs a horizontal sum of all elements in a returning the result.