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