virtio_accel_coreml/
lib.rs1#![cfg_attr(not(target_os = "macos"), forbid(unsafe_code))]
12
13mod artifact;
14mod fp8;
15mod lower;
16mod mlprogram;
17pub use fp8::{
18 COREML_TOSA_FP8_CAPABILITY, COREML_TOSA_FP8_TARGET, COREML_TOSA_QUANTIZED_CAPABILITY,
19 COREML_TOSA_QUANTIZED_TARGET,
20};
21
22pub use artifact::{ArtifactBuildError, CoreMlArtifact, FeatureRole};
23pub use lower::{
24 COREML_TOSA_CAPABILITY, COREML_TOSA_TARGET, LoweringError, supports_tosa_dtype,
25 supports_tosa_operator,
26};
27pub use mlprogram::{COREML_TOSA_INTEGER_CAPABILITY, COREML_TOSA_INTEGER_TARGET};
28
29use virtio_accel_tosa::CapabilityDescriptor;
30#[cfg(not(target_os = "macos"))]
31use virtio_accel_tosa::TosaCapabilityProvider;
32
33#[cfg(target_os = "macos")]
34const FLOAT_CAPABILITIES: &[CapabilityDescriptor] = &[COREML_TOSA_CAPABILITY];
35#[cfg(target_os = "macos")]
36const ALL_CAPABILITIES: &[CapabilityDescriptor] = &[
37 COREML_TOSA_CAPABILITY,
38 COREML_TOSA_INTEGER_CAPABILITY,
39 COREML_TOSA_FP8_CAPABILITY,
40 COREML_TOSA_QUANTIZED_CAPABILITY,
41];
42#[cfg(not(target_os = "macos"))]
43const NO_CAPABILITIES: &[CapabilityDescriptor] = &[];
44
45use virtio_accel_core::{ArtifactFormat, TargetIdentity};
46
47pub const ARTIFACT_FORMAT: ArtifactFormat = match ArtifactFormat::new(0x434d_4c50) {
49 Some(format) => format,
50 None => panic!("Core ML artifact format must be nonzero"),
51};
52
53pub const TARGET_IDENTITY: TargetIdentity = TargetIdentity([
55 0x434f_5245,
56 0x4d4c_0001,
57 0x414e_4503,
58 0x4d41_434f,
59 0x000e_0000,
60 0,
61 0,
62 0,
63 0,
64 0,
65 0,
66 0,
67]);
68
69pub const REQUIRED_RESIDENT_BYTES: u64 = u64::MAX;
75
76#[repr(u32)]
79#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
80pub enum CoreMlComputeUnits {
81 #[default]
83 CpuAndNeuralEngine = 0,
84 CpuAndGpu = 1,
86 All = 2,
88}
89
90impl CoreMlComputeUnits {
91 #[cfg(target_os = "macos")]
95 fn native_policy(self, requires_fp32: bool) -> u32 {
96 match (self, requires_fp32) {
97 (Self::CpuAndNeuralEngine, true) => 3, (Self::All, true) => Self::CpuAndGpu as u32,
99 _ => self as u32,
100 }
101 }
102}
103
104#[derive(Clone, Copy, Debug, PartialEq, Eq)]
106pub enum InitError {
107 UnsupportedPlatform,
109 InvalidModelRoot,
111 NeuralEngineUnavailable,
113 GpuUnavailable,
115}
116
117impl std::fmt::Display for InitError {
118 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119 write!(formatter, "{self:?}")
120 }
121}
122
123impl std::error::Error for InitError {}
124
125#[cfg(target_os = "macos")]
126mod macos;
127#[cfg(target_os = "macos")]
128pub use macos::{
129 CoreMlAccelerator, CoreMlBuffer, CoreMlContext, CoreMlEvent, CoreMlProgram, CoreMlQueue,
130};
131
132#[cfg(not(target_os = "macos"))]
134#[derive(Clone, Copy, Debug, Default)]
135pub struct CoreMlAccelerator;
136
137#[cfg(not(target_os = "macos"))]
138impl CoreMlAccelerator {
139 pub fn new(_model_root: impl AsRef<std::path::Path>) -> Result<Self, InitError> {
141 Err(InitError::UnsupportedPlatform)
142 }
143
144 pub fn new_tosa() -> Result<Self, InitError> {
146 Self::new_tosa_with_compute_units(CoreMlComputeUnits::default())
147 }
148
149 pub fn new_tosa_with_compute_units(_units: CoreMlComputeUnits) -> Result<Self, InitError> {
151 Err(InitError::UnsupportedPlatform)
152 }
153}
154
155#[cfg(not(target_os = "macos"))]
156impl TosaCapabilityProvider for CoreMlAccelerator {
157 fn tosa_capabilities(&self) -> &'static [CapabilityDescriptor] {
158 NO_CAPABILITIES
159 }
160}