#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CpuBackend {
OneDnn,
#[cfg(target_os = "android")]
Xnnpack,
#[cfg(any(target_os = "macos", target_os = "ios"))]
Accelerate,
Generic,
}
impl CpuBackend {
pub fn auto_detect() -> Self {
#[cfg(target_os = "android")]
{
Self::Xnnpack
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
{
Self::Accelerate
}
#[cfg(all(
not(target_os = "android"),
not(target_os = "macos"),
not(target_os = "ios")
))]
{
if has_onednn() {
Self::OneDnn
} else {
Self::Generic
}
}
}
}
#[inline]
pub fn has_onednn() -> bool {
cfg!(feature = "onednn")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn has_onednn_matches_feature() {
assert_eq!(has_onednn(), cfg!(feature = "onednn"));
}
#[test]
fn auto_detect_is_stable() {
assert_eq!(CpuBackend::auto_detect(), CpuBackend::auto_detect());
}
#[cfg(all(
not(target_os = "android"),
not(target_os = "macos"),
not(target_os = "ios")
))]
#[test]
fn auto_detect_tracks_onednn_feature() {
let expected = if cfg!(feature = "onednn") {
CpuBackend::OneDnn
} else {
CpuBackend::Generic
};
assert_eq!(CpuBackend::auto_detect(), expected);
}
}