Skip to main content

virtio_accel_tosa/
lib.rs

1//! Safe, bounded TOSA graph ingestion for `virtio-accel` providers.
2//!
3//! This crate validates a TOSA FlatBuffer before exposing zero-copy borrowed views. The public API
4//! contains no generated FlatBuffers types and no unchecked accessors. Validation is deliberately
5//! split into two layers:
6//!
7//! - [`parse`] enforces the serialization envelope, resource limits, stable TOSA 1.0 schema
8//!   boundary, names, references, and operator/attribute correspondence;
9//! - [`Model::validate_for`] exhaustively enforces the stable operator arities, operand kinds,
10//!   ranks, profiles, extensions, level limits, attributes, compile-time constants, static shapes,
11//!   control-flow signatures, and numerical parameter rules for a declared [`Target`]; and
12//! - provider-specific capability and [`Op::CUSTOM`] policy can be layered through
13//!   [`ModelValidator`].
14//!
15//! Semantic validation proves model legality; it does not execute tensor data or replace a
16//! backend's runtime checks for data-dependent TOSA `REQUIRE` conditions.
17//!
18//! The checked-in schema is from TOSA Tools `v2026.05.0`. The tools release implements TOSA
19//! specification 1.0.1 and appends draft TOSA 1.1 entries; [`parse`] accepts stable graph version
20//! 1.0.0 and rejects those appended entries.
21
22#![no_std]
23#![deny(unsafe_code)]
24
25extern crate alloc;
26
27mod analysis;
28mod artifact;
29mod attribute;
30mod capability;
31// Generated by upstream `flatc` 25.2.10 from `schema/tosa.fbs`. The module is private, and every
32// generated accessor is reached only after the generated verifier succeeds. See SAFETY.md.
33#[allow(
34    unsafe_code,
35    unsafe_op_in_unsafe_fn,
36    unknown_lints,
37    dead_code,
38    deprecated,
39    unused_imports,
40    non_camel_case_types,
41    mismatched_lifetime_syntaxes,
42    non_snake_case,
43    non_upper_case_globals,
44    clippy::extra_unused_lifetimes
45)]
46#[rustfmt::skip]
47mod generated;
48mod integer;
49mod numeric;
50mod runtime;
51mod semantic;
52mod specialize;
53mod types;
54mod validate;
55mod view;
56
57pub use analysis::{
58    AnalysisError, AnalysisOptions, AnalysisSpan, AnalyzedBlock, AnalyzedOperator, AnalyzedRegion,
59    AnalyzedValue, AnalyzedValueKind, BlockId, ConstantState, OperatorId, OptimizationHints,
60    RegionId, RuntimeCondition, TosaAnalysis, ValueId,
61};
62pub use artifact::{
63    ARTIFACT_FORMAT, ArtifactError, ExtensionSet, Level, LevelLimits, ProfileSet, Target,
64    TargetError,
65};
66pub use attribute::{
67    I32List, I32Values, NanPropagationMode, OpAttributes, ResizeMode, RoundingMode,
68};
69pub use capability::{
70    CapabilityDescriptor, DTypeCapability, DTypeConstraints, GraphCapabilities, OperatorCapability,
71    OperatorConstraints, RuntimeConditionSupport, TosaCapabilityProvider, ValueRoles,
72};
73pub use integer::{IntegerError, apply_scale_16, apply_scale_32, dot_i8_i32, rescale_i32_to_i8};
74pub use numeric::{
75    fp8e4m3_to_bf16_bits, fp8e4m3_to_f32, fp8e5m2_to_bf16_bits, fp8e5m2_to_f32,
76    low_precision_storage_bytes, pack_int4, unpack_int4,
77};
78pub use runtime::{
79    RuntimeError, RuntimeErrorKind, RuntimeValidation, RuntimeValue, validate_runtime_values,
80};
81pub use semantic::{
82    OperandRole, SemanticError, SemanticErrorKind, SemanticValidator, validate_semantics,
83};
84pub use specialize::{
85    CacheInsertError, SpecializationCache, SpecializationError, SpecializationKey,
86    SpecializationKeyBuilder,
87};
88pub use types::{Arity, AttributeKind, DType, Op, Version};
89pub use validate::{Error, Limits, NameKind, Resource, Stats, parse, parse_with_limits};
90pub use view::{
91    BasicBlock, BasicBlocks, Model, ModelValidator, Operator, Operators, Region, Regions, Shape,
92    ShapeValues, Shapes, StringList, Tensor, Tensors,
93};
94
95/// TOSA schema source pinned into this crate.
96pub const SCHEMA: &str = include_str!("../schema/tosa.fbs");
97
98/// SHA-256 of [`SCHEMA`] as shipped by TOSA Tools `v2026.05.0`.
99pub const SCHEMA_SHA256: &str = "a1d6383bdecddf9cc6a00c33e5a2fac7e8479dc560a9da2f68ba41da35e143f8";
100
101/// Upstream TOSA Tools release from which [`SCHEMA`] was copied.
102pub const SCHEMA_RELEASE: &str = "v2026.05.0";
103
104#[cfg(test)]
105mod tests;