virtio_accel_coreml/
lib.rs1#![cfg_attr(not(target_os = "macos"), forbid(unsafe_code))]
11
12mod artifact;
13mod lower;
14mod mlprogram;
15
16pub use artifact::{ArtifactBuildError, CoreMlArtifact, FeatureRole};
17pub use lower::{
18 COREML_TOSA_CAPABILITY, COREML_TOSA_TARGET, LoweringError, supports_tosa_dtype,
19 supports_tosa_operator,
20};
21pub use mlprogram::{COREML_TOSA_INTEGER_CAPABILITY, COREML_TOSA_INTEGER_TARGET};
22
23use virtio_accel_tosa::CapabilityDescriptor;
24#[cfg(not(target_os = "macos"))]
25use virtio_accel_tosa::TosaCapabilityProvider;
26
27#[cfg(target_os = "macos")]
28const FLOAT_CAPABILITIES: &[CapabilityDescriptor] = &[COREML_TOSA_CAPABILITY];
29#[cfg(target_os = "macos")]
30const ALL_CAPABILITIES: &[CapabilityDescriptor] =
31 &[COREML_TOSA_CAPABILITY, COREML_TOSA_INTEGER_CAPABILITY];
32#[cfg(not(target_os = "macos"))]
33const NO_CAPABILITIES: &[CapabilityDescriptor] = &[];
34
35use virtio_accel_core::{ArtifactFormat, TargetIdentity};
36
37pub const ARTIFACT_FORMAT: ArtifactFormat = match ArtifactFormat::new(0x434d_4c50) {
39 Some(format) => format,
40 None => panic!("Core ML artifact format must be nonzero"),
41};
42
43pub const TARGET_IDENTITY: TargetIdentity = TargetIdentity([
45 0x434f_5245,
46 0x4d4c_0001,
47 0x414e_4503,
48 0x4d41_434f,
49 0x000e_0000,
50 0,
51 0,
52 0,
53 0,
54 0,
55 0,
56 0,
57]);
58
59pub const REQUIRED_RESIDENT_BYTES: u64 = u64::MAX;
65
66#[derive(Clone, Copy, Debug, PartialEq, Eq)]
68pub enum InitError {
69 UnsupportedPlatform,
71 InvalidModelRoot,
73 NeuralEngineUnavailable,
75}
76
77impl std::fmt::Display for InitError {
78 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79 write!(formatter, "{self:?}")
80 }
81}
82
83impl std::error::Error for InitError {}
84
85#[cfg(target_os = "macos")]
86mod macos;
87#[cfg(target_os = "macos")]
88pub use macos::{
89 CoreMlAccelerator, CoreMlBuffer, CoreMlContext, CoreMlEvent, CoreMlProgram, CoreMlQueue,
90};
91
92#[cfg(not(target_os = "macos"))]
94#[derive(Clone, Copy, Debug, Default)]
95pub struct CoreMlAccelerator;
96
97#[cfg(not(target_os = "macos"))]
98impl CoreMlAccelerator {
99 pub fn new(_model_root: impl AsRef<std::path::Path>) -> Result<Self, InitError> {
101 Err(InitError::UnsupportedPlatform)
102 }
103
104 pub fn new_tosa() -> Result<Self, InitError> {
106 Err(InitError::UnsupportedPlatform)
107 }
108}
109
110#[cfg(not(target_os = "macos"))]
111impl TosaCapabilityProvider for CoreMlAccelerator {
112 fn tosa_capabilities(&self) -> &'static [CapabilityDescriptor] {
113 NO_CAPABILITIES
114 }
115}