#[macro_export]
macro_rules! coreml_feature {
($($item:item)*) => {
$(
#[cfg(any(feature = "coreml", feature = "coreml-hybrid", feature = "coreml-fallback"))]
$item
)*
};
({ $($stmt:stmt)* }) => {
#[cfg(any(feature = "coreml", feature = "coreml-hybrid", feature = "coreml-fallback"))]
{
$($stmt)*
}
};
}
pub use coreml_feature;
#[macro_export]
macro_rules! coreml_available {
() => {
cfg!(any(
feature = "coreml",
feature = "coreml-hybrid",
feature = "coreml-fallback"
)) && cfg!(target_os = "macos")
};
}
pub type CoreMLResult<T> = crate::error::RusTorchResult<T>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CoreMLOpType {
MatrixMultiplication,
Convolution,
Activation,
ElementWise,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CoreMLActivationType {
ReLU,
Sigmoid,
Tanh,
Softmax,
GELU,
LeakyReLU,
ELU,
Swish,
}
#[derive(Debug, Clone)]
pub struct CoreMLCapabilities {
pub max_tensor_size: usize,
pub supports_f32: bool,
pub supports_f64: bool,
pub supports_complex: bool,
pub neural_engine_available: bool,
pub gpu_acceleration_available: bool,
}
impl Default for CoreMLCapabilities {
fn default() -> Self {
Self {
max_tensor_size: 100 * 1024 * 1024, supports_f32: true,
supports_f64: false, supports_complex: false, neural_engine_available: false, gpu_acceleration_available: false, }
}
}
pub const COREML_NOT_AVAILABLE: &str = "CoreML not available on this system";
pub const COREML_FEATURE_DISABLED: &str = "CoreML feature not enabled at compile time";
pub const COREML_MACOS_ONLY: &str = "CoreML is only available on macOS";
pub const COREML_UNSUPPORTED_OP: &str = "Operation not supported by CoreML";
pub mod error_helpers {
use crate::error::RusTorchError;
pub fn not_available() -> RusTorchError {
RusTorchError::BackendUnavailable {
backend: "CoreML".to_string(),
}
}
pub fn feature_disabled() -> RusTorchError {
RusTorchError::BackendUnavailable {
backend: "CoreML (feature disabled)".to_string(),
}
}
pub fn unsupported_operation(operation: &str) -> RusTorchError {
RusTorchError::UnsupportedOperation(format!(
"{}: {}",
super::COREML_UNSUPPORTED_OP,
operation
))
}
pub fn device_error(message: &str) -> RusTorchError {
RusTorchError::Device {
device: "CoreML".to_string(),
message: message.to_string(),
}
}
pub fn tensor_op_error(message: &str) -> RusTorchError {
RusTorchError::TensorOp {
message: format!("CoreML: {}", message),
source: None,
}
}
}
pub fn is_coreml_available() -> bool {
coreml_available!() && check_runtime_availability()
}
fn check_runtime_availability() -> bool {
#[cfg(all(
any(
feature = "coreml",
feature = "coreml-hybrid",
feature = "coreml-fallback"
),
target_os = "macos"
))]
{
use crate::backends::DeviceManager;
DeviceManager::is_coreml_available()
}
#[cfg(not(all(
any(
feature = "coreml",
feature = "coreml-hybrid",
feature = "coreml-fallback"
),
target_os = "macos"
)))]
false
}