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
48#[cfg(feature = "accelerate")]
49extern crate accelerate_src;
50#[cfg(feature = "blis")]
51extern crate blis_src;
52#[cfg(feature = "blas")]
53extern crate cblas;
54#[cfg(feature = "openblas")]
55extern crate openblas_src;
56
57extern crate bit_set;
58#[macro_use]
59extern crate derive_new;
60#[macro_use]
61pub extern crate downcast_rs;
62#[allow(unused_imports)]
63#[macro_use]
64extern crate log;
65#[allow(unused_imports)]
66#[macro_use]
67pub extern crate ndarray;
68#[cfg(test)]
69extern crate env_logger;
70pub extern crate num_traits;
71#[cfg(test)]
72extern crate proptest;
73
74pub extern crate tract_data;
75pub extern crate tract_linalg;
76
77#[macro_use]
78pub mod macros;
79#[macro_use]
80pub mod ops;
81
82pub mod axes;
83pub mod broadcast;
84pub mod floats;
85pub mod framework;
86pub mod model;
87pub mod optim;
88pub mod plan;
89#[macro_use]
90pub mod runtime;
91#[macro_use]
92pub mod transform;
93pub mod value;
94
95pub use dyn_clone;
96
97mod late_bind;
98
99/// This prelude is meant for code using tract.
100pub mod prelude {
101    pub use crate::framework::Framework;
102    pub use crate::model::*;
103    pub use crate::runtime::{
104        FrozenState, RunOptions, Runnable, Runtime, State, runtime_for_name, runtimes,
105    };
106    pub use crate::value::{IntoTValue, TValue};
107    pub use std::sync::Arc;
108    pub use tract_data::prelude::*;
109
110    pub use ndarray as tract_ndarray;
111    pub use num_traits as tract_num_traits;
112    pub use tract_data;
113    pub use tract_linalg;
114    pub use tract_linalg::multithread;
115}
116
117/// This prelude is meant for code extending tract (like implementing new ops).
118pub mod internal {
119    pub extern crate inventory;
120    pub use crate::axes::{AxesMapping, Axis};
121    pub use crate::late_bind::*;
122    pub use crate::model::*;
123    pub use crate::ops::change_axes::*;
124    pub use crate::ops::element_wise::ElementWiseMiniOp;
125    pub use crate::ops::{Cost, EvalOp, FrozenOpState, Op, OpState, Validation};
126    pub use crate::plan::{SessionStateHandler, SimplePlan, SimpleState, TurnState};
127    pub use crate::prelude::*;
128    pub use crate::runtime::{
129        DefaultRuntime, Runnable, Runtime, State, runtime_for_name, runtimes,
130    };
131    pub use dims;
132    pub use downcast_rs as tract_downcast_rs;
133    pub use register_runtime;
134    pub use register_simple_model_transform;
135    pub use std::borrow::Cow;
136    pub use std::collections::HashMap;
137    pub use std::hash::Hash;
138    pub use std::marker::PhantomData;
139    pub use tract_data::internal::*;
140    pub use tract_data::{
141        dispatch_copy, dispatch_datum, dispatch_datum_by_size, dispatch_floatlike, dispatch_numbers,
142    };
143    pub use tvec;
144    pub use {args_1, args_2, args_3, args_4, args_5, args_6, args_7, args_8};
145    pub use {as_op, impl_op_same_as, not_a_typed_op, op_as_typed_op};
146    pub use {bin_to_super_type, element_wise, element_wise_oop};
147    pub use {rule_if, rule_if_let, rule_if_some};
148}
149
150#[cfg(test)]
151#[allow(dead_code)]
152fn setup_test_logger() {
153    let _ = env_logger::Builder::from_env("TRACT_LOG").try_init();
154}