Skip to main content

Engine

Struct Engine 

Source
pub struct Engine { /* private fields */ }
Expand description

LiteRT-LM Engine - the main entry point for loading models

The Engine loads a model file and prepares it for inference. Create sessions from the engine to perform text generation.

Implementations§

Source§

impl Engine

Source

pub fn new(model_path: &str, backend: Backend) -> Result<Self>

Create a new Engine from a model file

§Arguments
  • model_path - Path to the .tflite model file
  • backend - Backend to use (Cpu or Gpu)
§Example
use litert_lm::{Engine, Backend};

let engine = Engine::new("model.tflite", Backend::Cpu)?;
Examples found in repository?
examples/batch_inference.rs (line 16)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    // Get model path from command line argument
5    let args: Vec<String> = std::env::args().collect();
6    if args.len() < 2 {
7        eprintln!("Usage: {} <model_path>", args[0]);
8        eprintln!("Example: {} model.tflite", args[0]);
9        std::process::exit(1);
10    }
11    let model_path = &args[1];
12
13    println!("Loading model from: {}", model_path);
14
15    // Create engine
16    let engine = Engine::new(model_path, Backend::Cpu)?;
17    println!("Engine created successfully!\n");
18
19    // Test prompts
20    let prompts = vec![
21        "What is the capital of France?",
22        "Explain quantum computing in simple terms.",
23        "Write a haiku about programming.",
24        "What is 2 + 2?",
25    ];
26
27    println!("Running batch inference...\n");
28    println!("========================================");
29
30    // Process each prompt in a separate session
31    for (i, prompt) in prompts.iter().enumerate() {
32        println!("\n[{}] Prompt: {}", i + 1, prompt);
33
34        // Create a new session for each prompt
35        let session = engine.create_session()?;
36
37        match session.generate(prompt) {
38            Ok(response) => {
39                println!("Response: {}", response);
40            }
41            Err(e) => {
42                eprintln!("Error: {}", e);
43            }
44        }
45
46        println!("----------------------------------------");
47    }
48
49    println!("\nBatch inference complete!");
50
51    Ok(())
52}
More examples
Hide additional examples
examples/simple_chat.rs (line 17)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    // Get model path from command line argument
6    let args: Vec<String> = std::env::args().collect();
7    if args.len() < 2 {
8        eprintln!("Usage: {} <model_path>", args[0]);
9        eprintln!("Example: {} model.tflite", args[0]);
10        std::process::exit(1);
11    }
12    let model_path = &args[1];
13
14    println!("Loading model from: {}", model_path);
15
16    // Create engine with CPU backend
17    let engine = Engine::new(model_path, Backend::Cpu)?;
18    println!("Engine created successfully!");
19
20    // Create a session (conversation)
21    let session = engine.create_session()?;
22    println!("Session created successfully!");
23    println!();
24    println!("You can now chat with the model. Type 'quit' or 'exit' to stop.");
25    println!("========================================");
26    println!();
27
28    // Interactive chat loop
29    loop {
30        print!("You: ");
31        io::stdout().flush()?;
32
33        let mut input = String::new();
34        io::stdin().read_line(&mut input)?;
35        let input = input.trim();
36
37        if input.is_empty() {
38            continue;
39        }
40
41        if input.eq_ignore_ascii_case("quit") || input.eq_ignore_ascii_case("exit") {
42            println!("Goodbye!");
43            break;
44        }
45
46        // Generate response
47        print!("Assistant: ");
48        io::stdout().flush()?;
49
50        match session.generate(input) {
51            Ok(response) => {
52                println!("{}", response);
53                println!();
54            }
55            Err(e) => {
56                eprintln!("Error generating response: {}", e);
57                println!();
58            }
59        }
60    }
61
62    Ok(())
63}
Source

pub fn create_session(&self) -> Result<Session>

Create a new session for text generation

Sessions maintain conversation history and state. You can create multiple sessions from the same engine.

§Example
use litert_lm::{Engine, Backend};

let engine = Engine::new("model.tflite", Backend::Cpu)?;
let session = engine.create_session()?;
Examples found in repository?
examples/batch_inference.rs (line 35)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    // Get model path from command line argument
5    let args: Vec<String> = std::env::args().collect();
6    if args.len() < 2 {
7        eprintln!("Usage: {} <model_path>", args[0]);
8        eprintln!("Example: {} model.tflite", args[0]);
9        std::process::exit(1);
10    }
11    let model_path = &args[1];
12
13    println!("Loading model from: {}", model_path);
14
15    // Create engine
16    let engine = Engine::new(model_path, Backend::Cpu)?;
17    println!("Engine created successfully!\n");
18
19    // Test prompts
20    let prompts = vec![
21        "What is the capital of France?",
22        "Explain quantum computing in simple terms.",
23        "Write a haiku about programming.",
24        "What is 2 + 2?",
25    ];
26
27    println!("Running batch inference...\n");
28    println!("========================================");
29
30    // Process each prompt in a separate session
31    for (i, prompt) in prompts.iter().enumerate() {
32        println!("\n[{}] Prompt: {}", i + 1, prompt);
33
34        // Create a new session for each prompt
35        let session = engine.create_session()?;
36
37        match session.generate(prompt) {
38            Ok(response) => {
39                println!("Response: {}", response);
40            }
41            Err(e) => {
42                eprintln!("Error: {}", e);
43            }
44        }
45
46        println!("----------------------------------------");
47    }
48
49    println!("\nBatch inference complete!");
50
51    Ok(())
52}
More examples
Hide additional examples
examples/simple_chat.rs (line 21)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    // Get model path from command line argument
6    let args: Vec<String> = std::env::args().collect();
7    if args.len() < 2 {
8        eprintln!("Usage: {} <model_path>", args[0]);
9        eprintln!("Example: {} model.tflite", args[0]);
10        std::process::exit(1);
11    }
12    let model_path = &args[1];
13
14    println!("Loading model from: {}", model_path);
15
16    // Create engine with CPU backend
17    let engine = Engine::new(model_path, Backend::Cpu)?;
18    println!("Engine created successfully!");
19
20    // Create a session (conversation)
21    let session = engine.create_session()?;
22    println!("Session created successfully!");
23    println!();
24    println!("You can now chat with the model. Type 'quit' or 'exit' to stop.");
25    println!("========================================");
26    println!();
27
28    // Interactive chat loop
29    loop {
30        print!("You: ");
31        io::stdout().flush()?;
32
33        let mut input = String::new();
34        io::stdin().read_line(&mut input)?;
35        let input = input.trim();
36
37        if input.is_empty() {
38            continue;
39        }
40
41        if input.eq_ignore_ascii_case("quit") || input.eq_ignore_ascii_case("exit") {
42            println!("Goodbye!");
43            break;
44        }
45
46        // Generate response
47        print!("Assistant: ");
48        io::stdout().flush()?;
49
50        match session.generate(input) {
51            Ok(response) => {
52                println!("{}", response);
53                println!();
54            }
55            Err(e) => {
56                eprintln!("Error generating response: {}", e);
57                println!();
58            }
59        }
60    }
61
62    Ok(())
63}

Trait Implementations§

Source§

impl Drop for Engine

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for Engine

Source§

impl Sync for Engine

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.