virtio_accel_openvino/lib.rs
1//! Intel OpenVINO host backend for NPU, GPU, and CPU execution.
2//!
3//! The production path accepts device-neutral TOSA 1.0 FlatBuffers, validates and analyzes them
4//! with `virtio-accel-tosa`, and lowers supported static floating-point and exact INT8 graphs to
5//! in-memory OpenVINO IR inside this host-native crate. Models are compiled with the `ACCURACY` execution
6//! mode hint so plugins may not silently execute a declared-FP32 graph at reduced precision.
7//! Program buffers are page-aligned allocations wrapped directly by OpenVINO tensors created
8//! from host pointers; output execution is accepted only when the runtime uses the same
9//! allocation as its output tensor.
10//!
11//! By default, the native modules compile when the build script detects an OpenVINO C runtime
12//! (`libopenvino_c`) via pkg-config or the `VIRTIO_ACCEL_OPENVINO_LIB_DIR` override; the
13//! `VIRTIO_ACCEL_OPENVINO` variable forces the probe on (`1`, failing loudly when the runtime is
14//! missing) or off (`0`). Builds without the runtime keep the portable lowering module and an
15//! unsupported-runtime placeholder. The opt-in `runtime-loading` feature compiles the native
16//! adapter without linking a vendor library; discovery resolves the C API at runtime.
17//! `VIRTIO_ACCEL_OPENVINO=0` still disables it.
18
19#![cfg_attr(not(va_openvino), forbid(unsafe_code))]
20
21mod lower;
22
23pub use lower::{
24 LoweringError, OPENVINO_TOSA_CAPABILITY, OPENVINO_TOSA_FP8_CAPABILITY,
25 OPENVINO_TOSA_FP8_TARGET, OPENVINO_TOSA_INTEGER_CAPABILITY, OPENVINO_TOSA_INTEGER_TARGET,
26 OPENVINO_TOSA_QUANTIZED_CAPABILITY, OPENVINO_TOSA_QUANTIZED_FP16_CAPABILITY,
27 OPENVINO_TOSA_QUANTIZED_TARGET, OPENVINO_TOSA_TARGET, supports_tosa_dtype,
28 supports_tosa_operator,
29};
30
31use virtio_accel_tosa::CapabilityDescriptor;
32#[cfg(not(va_openvino))]
33use virtio_accel_tosa::TosaCapabilityProvider;
34
35#[cfg(va_openvino)]
36const TOSA_CAPABILITIES: &[CapabilityDescriptor] = &[
37 OPENVINO_TOSA_CAPABILITY,
38 OPENVINO_TOSA_INTEGER_CAPABILITY,
39 OPENVINO_TOSA_QUANTIZED_CAPABILITY,
40];
41/// What an instance advertises when its device's compiler accepted the FP8 probe.
42///
43/// FP8 support is a property of the device, not of this backend: Intel NPU arch 5010 (Panther
44/// Lake) compiles FP8 graphs and arch 40XX (Lunar Lake) refuses even an FP8 `IDENTITY`. An
45/// unconditional descriptor would have a placement layer admit an FP8 graph and then fail it at
46/// load, which is exactly the silent-then-explode behaviour this crate refuses everywhere else.
47#[cfg(va_openvino)]
48const TOSA_CAPABILITIES_WITH_FP8: &[CapabilityDescriptor] = &[
49 OPENVINO_TOSA_CAPABILITY,
50 OPENVINO_TOSA_INTEGER_CAPABILITY,
51 OPENVINO_TOSA_FP8_CAPABILITY,
52 OPENVINO_TOSA_QUANTIZED_CAPABILITY,
53];
54#[cfg(va_openvino)]
55const TOSA_CAPABILITIES_FP16: &[CapabilityDescriptor] = &[
56 OPENVINO_TOSA_CAPABILITY,
57 OPENVINO_TOSA_INTEGER_CAPABILITY,
58 OPENVINO_TOSA_QUANTIZED_FP16_CAPABILITY,
59];
60#[cfg(va_openvino)]
61const TOSA_CAPABILITIES_FP16_WITH_FP8: &[CapabilityDescriptor] = &[
62 OPENVINO_TOSA_CAPABILITY,
63 OPENVINO_TOSA_INTEGER_CAPABILITY,
64 lower::OPENVINO_TOSA_FP8_FP16_CAPABILITY,
65 OPENVINO_TOSA_QUANTIZED_FP16_CAPABILITY,
66];
67#[cfg(not(va_openvino))]
68const TOSA_CAPABILITIES: &[CapabilityDescriptor] = &[];
69
70/// The OpenVINO runtime does not publish a finite upper bound for compiled-model residency.
71///
72/// Requiring the maximal charge makes the provider promise truthful: a process cannot retain
73/// `u64::MAX` bytes for one model. Device integrations must set their aggregate program-residency
74/// policy accordingly when admitting an OpenVINO program.
75pub const REQUIRED_RESIDENT_BYTES: u64 = u64::MAX;
76
77/// Failure to initialize an OpenVINO backend instance.
78#[derive(Clone, Copy, Debug, PartialEq, Eq)]
79pub enum InitError {
80 /// No compatible OpenVINO C runtime (`libopenvino_c`) is available.
81 RuntimeUnavailable,
82 /// The OpenVINO core object could not be created.
83 CoreCreationFailed,
84 /// Device enumeration failed inside the OpenVINO runtime.
85 DeviceEnumerationFailed,
86 /// No requested or known inference device is available on this host.
87 DeviceUnavailable,
88}
89
90impl std::fmt::Display for InitError {
91 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92 write!(formatter, "{self:?}")
93 }
94}
95
96impl std::error::Error for InitError {}
97
98#[cfg(va_openvino)]
99mod ffi;
100#[cfg(va_openvino)]
101mod native;
102#[cfg(va_openvino)]
103pub use native::{
104 OpenVinoAccelerator, OpenVinoBuffer, OpenVinoContext, OpenVinoEvent, OpenVinoProgram,
105 OpenVinoQueue,
106};
107
108/// Placeholder that keeps workspace consumers portable when no OpenVINO runtime was detected.
109#[cfg(not(va_openvino))]
110#[derive(Clone, Copy, Debug, Default)]
111pub struct OpenVinoAccelerator;
112
113#[cfg(not(va_openvino))]
114impl OpenVinoAccelerator {
115 /// Runtime loading is unavailable on this build target.
116 #[cfg(feature = "runtime-loading")]
117 pub fn load_runtime_from(_path: &std::path::Path) -> Result<(), InitError> {
118 Err(InitError::RuntimeUnavailable)
119 }
120
121 /// Report that no OpenVINO runtime was detected when this crate was built.
122 pub fn new() -> Result<Self, InitError> {
123 Err(InitError::RuntimeUnavailable)
124 }
125
126 /// Report that no OpenVINO runtime was detected when this crate was built.
127 pub fn with_device(_device: &str) -> Result<Self, InitError> {
128 Err(InitError::RuntimeUnavailable)
129 }
130
131 /// Report that no OpenVINO runtime was detected when this crate was built.
132 pub fn available_devices() -> Result<Vec<String>, InitError> {
133 Err(InitError::RuntimeUnavailable)
134 }
135}
136
137#[cfg(not(va_openvino))]
138impl TosaCapabilityProvider for OpenVinoAccelerator {
139 fn tosa_capabilities(&self) -> &'static [CapabilityDescriptor] {
140 TOSA_CAPABILITIES
141 }
142}