Skip to main content

tract_core/
lib.rs

1#![allow(clippy::len_zero)]
2#![allow(clippy::missing_safety_doc)]
3#![allow(clippy::redundant_closure_call)]
4//! # Tract
5//!
6//! Tiny, no-nonsense, self contained, portable TensorFlow and ONNX inference.
7//!
8//! ## Example
9//!
10//! ```
11//! # extern crate tract_core;
12//! # fn main() {
13//! use tract_core::internal::*;
14//!
15//! // build a simple model that just add 3 to each input component
16//! let mut model = TypedModel::default();
17//!
18//! let input_fact = f32::fact(&[3]);
19//! let input = model.add_source("input", input_fact).unwrap();
20//! let three = model.add_const("three".to_string(), tensor1(&[3f32])).unwrap();
21//! let add = model.wire_node("add".to_string(),
22//!     tract_core::ops::math::add(),
23//!     [input, three].as_ref()
24//!     ).unwrap();
25//!
26//! model.auto_outputs().unwrap();
27//!
28//! // We build an execution plan. Default inputs and outputs are inferred from
29//! // the model graph.
30//! let plan = model.into_runnable().unwrap();
31//!
32//! // run the computation.
33//! let input = tensor1(&[1.0f32, 2.5, 5.0]);
34//! let mut outputs = plan.run(tvec![input.into()]).unwrap();
35//!
36//! // take the first and only output tensor
37//! let mut tensor = outputs.pop().unwrap();
38//!
39//! assert_eq!(tensor, tensor1(&[4.0f32, 5.5, 8.0]).into());
40//! # }
41//! ```
42//!
43//! While creating a model from Rust code is useful for testing the library,
44//! real-life use-cases will usually load a TensorFlow or ONNX model using
45//! tract-tensorflow or tract-onnx crates.
46//!
47
48extern crate bit_set;
49#[macro_use]
50extern crate derive_new;
51#[macro_use]
52pub extern crate downcast_rs;
53#[allow(unused_imports)]
54#[macro_use]
55extern crate log;
56#[allow(unused_imports)]
57#[macro_use]
58pub extern crate ndarray;
59#[cfg(test)]
60extern crate env_logger;
61pub extern crate num_traits;
62#[cfg(test)]
63extern crate proptest;
64
65pub extern crate tract_data;
66pub extern crate tract_linalg;
67
68#[macro_use]
69pub mod macros;
70#[macro_use]
71pub mod ops;
72
73pub mod axes;
74pub mod broadcast;
75pub mod floats;
76pub mod framework;
77pub mod model;
78pub mod optim;
79pub mod plan;
80#[macro_use]
81pub mod runtime;
82#[macro_use]
83pub mod transform;
84pub mod value;
85
86pub use dyn_clone;
87
88mod late_bind;
89
90/// This prelude is meant for code using tract.
91pub mod prelude {
92    pub use crate::framework::Framework;
93    pub use crate::model::*;
94    pub use crate::runtime::{
95        FrozenState, RunOptions, Runnable, Runtime, State, runtime_for_name, runtimes,
96    };
97    pub use crate::value::{IntoTValue, TValue};
98    pub use std::sync::Arc;
99    pub use tract_data::prelude::*;
100
101    pub use ndarray as tract_ndarray;
102    pub use num_traits as tract_num_traits;
103    pub use tract_data;
104    pub use tract_linalg;
105    pub use tract_linalg::multithread;
106}
107
108/// This prelude is meant for code extending tract (like implementing new ops).
109pub mod internal {
110    pub extern crate inventory;
111    pub use crate::axes::{AxesMapping, Axis};
112    pub use crate::late_bind::*;
113    pub use crate::model::*;
114    pub use crate::ops::change_axes::*;
115    pub use crate::ops::element_wise::ElementWiseMiniOp;
116    pub use crate::ops::{Cost, EvalOp, FrozenOpState, Op, OpState, Validation};
117    pub use crate::plan::{SessionStateHandler, SimplePlan, SimpleState, TurnState};
118    pub use crate::prelude::*;
119    pub use crate::runtime::{
120        DefaultRuntime, Runnable, Runtime, State, runtime_for_name, runtimes,
121    };
122    pub use dims;
123    pub use downcast_rs as tract_downcast_rs;
124    pub use register_model_transform;
125    pub use register_runtime;
126    pub use register_simple_model_transform;
127    pub use std::borrow::Cow;
128    pub use std::collections::HashMap;
129    pub use std::hash::Hash;
130    pub use std::marker::PhantomData;
131    pub use tract_data::internal::*;
132    pub use tract_data::{
133        dispatch_copy, dispatch_datum, dispatch_datum_by_size, dispatch_floatlike, dispatch_numbers,
134    };
135    pub use tvec;
136    pub use {args_1, args_2, args_3, args_4, args_5, args_6, args_7, args_8};
137    pub use {as_op, not_a_typed_op, op_as_typed_op};
138    pub use {bin_to_super_type, element_wise, element_wise_oop};
139    pub use {rule_if, rule_if_let, rule_if_some};
140}
141
142#[cfg(test)]
143#[allow(dead_code)]
144fn setup_test_logger() {
145    let _ = env_logger::Builder::from_env("TRACT_LOG").try_init();
146}