Skip to main content

litert/
lib.rs

1// Copyright 2026 Google LLC.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! # LiteRT Rust Bindings
16//!
17//! This crate provides the public API for LiteRT Rust bindings, allowing for the execution
18//! of TensorFlow Lite models.
19//!
20//! ## Minimal Usage Example
21//!
22//! The following example demonstrates how to create an environment, load a model from a file,
23//! compile it, create input and output buffers, and run the model for inference.
24//!
25//! ```rust,no_run
26//! use litert::{CompiledModel, EnvironmentBuilder, LiteRtHwAccelerator, Model, Options};
27//!
28//! // 1. Create an environment.
29//! let environment = EnvironmentBuilder::build_default().expect("Failed to create environment");
30//!
31//! // 2. Load the model from a file.
32//! let model_path = "/path/to/your/model.tflite";
33//! let model = Model::create_model_from_file(model_path)
34//!     .expect("Failed to load model");
35//!
36//! // 3. Set compilation options, for example, to use a hardware accelerator.
37//! let options = Options::create_with_accelerator(LiteRtHwAccelerator::Cpu)
38//!     .expect("Failed to create options");
39//!
40//! // 4. Compile the model.
41//! let compiled_model = CompiledModel::create(&environment, &model, &options)
42//!     .expect("Failed to compile model");
43//!
44//! // 5. Create input and output tensor buffers.
45//! // We are using the first signature (index 0).
46//! let signature_index = 0;
47//! let input_buffers = compiled_model
48//!     .create_input_tensor_buffers(&environment, &model, signature_index)
49//!     .expect("Failed to create input buffers");
50//!
51//! let output_buffers = compiled_model
52//!     .create_output_tensor_buffers(&environment, &model, signature_index)
53//!     .expect("Failed to create output buffers");
54//!
55//! // 6. (Optional) Fill input buffers with data.
56//! // This is just an example, actual data should match the model's requirements.
57//! let input_data: Vec<f32> = vec![1.0, 2.0, 3.0];
58//! input_buffers[0].write(&input_data).expect("Failed to write to input buffer");
59//!
60//! // 7. Run inference.
61//! compiled_model.run(signature_index, &input_buffers, &output_buffers)
62//!     .expect("Failed to run model");
63//!
64//! // 8. (Optional) Read results from output buffers.
65//! let mut output_data: Vec<f32> = vec![0.0; 10]; // Pre-allocate space for results
66//! output_buffers[0].read(&mut output_data).expect("Failed to read from output buffer");
67//!
68//! println!("Inference successful. Output data: {:?}", output_data);
69//! ```
70
71mod bindings;
72pub mod compiled_model;
73pub mod environment;
74pub mod error;
75mod helper_funs;
76pub mod model;
77pub mod tensor_buffer;
78#[macro_use]
79mod macros;
80
81// Make some types available to the user.
82pub use bindings::LiteRtStatus;
83pub use compiled_model::CompiledModel;
84pub use compiled_model::LiteRtHwAccelerator;
85pub use compiled_model::Options;
86pub use environment::Environment;
87pub use environment::EnvironmentBuilder;
88pub use error::Error;
89pub use error::ErrorCause;
90pub use model::Model;
91pub use tensor_buffer::ElementType;
92pub use tensor_buffer::TensorBuffer;
93pub use tensor_buffer::TensorBufferRequirements;
94pub use tensor_buffer::TensorBufferType;