onnx_std/lib.rs
1//! # `onnx-std`
2//!
3//! A pure-Rust ONNX **standard library** — model I/O, textual format, and an
4//! extensible validator — built on the shared runtime IR. This is the first
5//! wave of the design captured in `docs/ONNX_RS.md`.
6//!
7//! ## Relationship to the rest of the workspace
8//!
9//! `onnx-std` does **not** define its own IR. Per ONNX_RS §4.1 ("Shared Crate"),
10//! the `Graph` / `Node` / `Value` / `Tensor` / `WeightRef` types are reused
11//! verbatim from [`onnx_runtime_ir`], and the protobuf parse/encode stack is
12//! reused from [`onnx_runtime_loader`]. `onnx-std` is the *standard-library*
13//! layer on top: ergonomic model I/O, a human-readable text format, and a
14//! checker — the concerns the design assigns to the ONNX library rather than the
15//! inference runtime.
16//!
17//! ```text
18//! onnx-std ──(standard lib: load/save/print/check)
19//! │
20//! └─ depends on ─▶ onnx-runtime-ir (shared Graph/Node/Value IR)
21//! └─ depends on ─▶ onnx-runtime-loader (protobuf serde + weight mmap)
22//! ```
23//!
24//! ## What this wave covers
25//!
26//! | ONNX_RS § | Feature | Status |
27//! |------------|---------|--------|
28//! | §3.4 / §4 | [`load_model`] / [`save_model`] round-trip | ✅ |
29//! | §5 | [`Model::to_text`] / [`Model::from_text`] | ✅ |
30//! | §7 | [`schema`] op schemas and opset-aware registry | ✅ |
31//! | §8 | [`check::OnnxChecker`] extensible validator | ✅ (schema-aware) |
32//! | §9 | [`shape::infer_shapes`] symbolic shape inference | ✅ |
33//! | §10 | [`version::VersionConverter`] opset conversion | ✅ |
34//! | §6.2 | [`textproto`] protobuf TextFormat I/O | ✅ |
35//!
36//! JSON and protobuf TextFormat are descriptor-driven from the exact vendored
37//! ONNX proto, so every bound message, field, oneof, and enum is covered. The
38//! readable text DSL appends a protobuf-TextFormat extension containing only
39//! fields outside the graph syntax. The parsed DSL is authoritative for every
40//! field it represents, while the explicit extension preserves the remainder.
41//!
42//! Deferred to later waves (see `// FOLLOW-UP` markers): the complete operator
43//! catalog, remaining function/IR-gate checker rules, custom-op registration
44//! (§11), and Python bindings (§12). Training semantics are out of scope.
45//!
46//! ## Example
47//!
48//! ```no_run
49//! let model = onnx_std::load_model("model.onnx")?;
50//! println!("{}", model.to_text());
51//! let report = model.validate();
52//! assert!(report.is_valid());
53//! onnx_std::save_model(&model, "roundtrip.onnx")?;
54//! # Ok::<(), onnx_std::Error>(())
55//! ```
56
57#![forbid(unsafe_code)]
58
59pub mod check;
60mod codec;
61mod error;
62pub mod json;
63mod model;
64mod proto_serde;
65pub mod schema;
66pub mod shape;
67pub mod text;
68pub mod textproto;
69pub mod version;
70
71pub use error::{Error, Result};
72pub use model::{
73 DeviceConfigurationProto, IntIntListEntryProto, Model, NodeDeviceConfigurationProto,
74 OpaqueProto, ShardedDimProto, ShardingSpecProto, SimpleShardedDimProto, load_model, save_model,
75 simple_sharded_dim_proto,
76};
77
78// Re-export the shared IR and the metadata/weight types so downstream users can
79// build and inspect models without depending on the runtime crates directly
80// (ONNX_RS §4.1: the IR is shared, not re-defined here).
81pub use onnx_runtime_ir as ir;
82pub use onnx_runtime_loader::{ModelMetadata, WeightStore};
83
84pub use check::{OnnxChecker, Severity, ValidationResult, ValidationRule, Violation};
85pub use codec::{Json, Text, TextCodec, TextProto};
86pub use schema::{
87 AttributeDefault, AttributeSpec, AttributeType, InputSpec, OpSchema, OutputSpec, SchemaError,
88 SchemaRegistry, TypeConstraint,
89};
90pub use shape::{ShapeError, ShapeInferenceResult, infer_shapes};
91pub use text::{PrintOptions, from_text, to_text, to_text_with};
92pub use version::{
93 AdaptResult, ConvertError, ConvertReport, IncompatibleOp, OpAdapter, VersionConverter,
94};