Skip to main content

ModelGen

Struct ModelGen 

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

Builder for generating Burn model code from ONNX files.

ModelGen converts ONNX models into Burn-compatible Rust source code and model weights. It can be used from both build scripts and CLI applications.

§Conversion Process

  1. Parses ONNX model file(s)
  2. Converts ONNX operations to Burn nodes using the node registry
  3. Generates Rust source code with type-safe tensor operations
  4. Saves model weights in BurnPack (.bpk) format

§Examples

§Using in a build script (build.rs)

use burn_onnx::ModelGen;

ModelGen::new()
    .input("path/to/model.onnx")
    .out_dir("model/")
    .run_from_script();

This generates code in $OUT_DIR/model/model.rs which can be included in your crate:

include!(concat!(env!("OUT_DIR"), "/model/model.rs"));

§Using from CLI

use burn_onnx::ModelGen;

ModelGen::new()
    .input("path/to/model.onnx")
    .out_dir("src/model/")
    .run_from_cli();

§Development mode for debugging

use burn_onnx::ModelGen;

ModelGen::new()
    .input("path/to/model.onnx")
    .out_dir("model/")
    .development(true)  // Generates .onnx.txt and .graph.txt debug files
    .run_from_cli();

Implementations§

Source§

impl ModelGen

Source

pub fn new() -> Self

Creates a new ModelGen builder with default settings.

Default configuration:

§Examples
use burn_onnx::ModelGen;

ModelGen::new()
    .input("model.onnx")
    .out_dir("./out")
    .run_from_cli();
Source

pub fn out_dir(&mut self, out_dir: &str) -> &mut Self

Sets the output directory for generated files.

When used with run_from_script, this path is appended to $OUT_DIR. When used with run_from_cli, this is the absolute or relative path where files will be written.

§Arguments
  • out_dir - Directory path where generated .rs and record files will be saved
§Examples
use burn_onnx::ModelGen;

ModelGen::new()
    .out_dir("model/")  // In build.rs: $OUT_DIR/model/
    .input("model.onnx")
    .run_from_script();
Source

pub fn input(&mut self, input: &str) -> &mut Self

Adds an ONNX model file to convert.

Multiple input files can be added by calling this method multiple times. Each input file will generate a separate .rs file with the same base name.

§Arguments
  • input - Path to the ONNX model file (.onnx)
§Examples
use burn_onnx::ModelGen;

ModelGen::new()
    .input("encoder.onnx")
    .input("decoder.onnx")  // Generate multiple models
    .out_dir("models/")
    .run_from_cli();
Source

pub fn development(&mut self, development: bool) -> &mut Self

Enables development mode for debugging.

When enabled, generates additional debug files alongside the Rust source:

  • <model>.onnx.txt - Debug representation of the parsed ONNX graph
  • <model>.graph.txt - Debug representation of the converted Burn graph
§Arguments
  • development - If true, generate debug files
§Examples
use burn_onnx::ModelGen;

ModelGen::new()
    .input("model.onnx")
    .out_dir("debug/")
    .development(true)  // Generates model.onnx.txt and model.graph.txt
    .run_from_cli();
Source

pub fn load_strategy(&mut self, strategy: LoadStrategy) -> &mut Self

Sets the weight loading strategy for the generated model.

See LoadStrategy for available options.

§Examples
use burn_onnx::{ModelGen, LoadStrategy};

// WASM or embedded: load weights from bytes at runtime
ModelGen::new()
    .input("model.onnx")
    .out_dir("model/")
    .load_strategy(LoadStrategy::Bytes)
    .run_from_script();
Source

pub fn simplify(&mut self, simplify: bool) -> &mut Self

Enable or disable graph simplification passes (default: true).

When enabled, optimization passes like dead node elimination, common subexpression elimination (CSE), and pattern-based simplifications are applied to the ONNX IR before code generation.

Source

pub fn partition(&mut self, partition: bool) -> &mut Self

Enable or disable submodule partitioning for large models (default: true).

When enabled, models with more than 200 nodes are automatically split into smaller submodule structs to keep generated code compilable. Each submodule gets its own forward() method, and the top-level Model delegates to them.

Source

pub fn run_from_script(&self)

Runs code generation from a build script context.

Use this method when calling from build.rs. The output directory will be $OUT_DIR/<out_dir>, allowing the generated code to be included with:

include!(concat!(env!("OUT_DIR"), "/<out_dir>/<model>.rs"));
§Panics

Panics if OUT_DIR environment variable is not set (should be set by Cargo).

§Examples

In build.rs:

use burn_onnx::ModelGen;

ModelGen::new()
    .input("path/to/model.onnx")
    .out_dir("model/")
    .run_from_script();
Source

pub fn run_from_cli(&self)

Runs code generation from a CLI or application context.

Use this method when calling from a CLI tool or regular application. The output directory is used as-is (relative or absolute path).

§Panics

Panics if out_dir was not set via out_dir.

§Examples
use burn_onnx::ModelGen;

ModelGen::new()
    .input("model.onnx")
    .out_dir("./generated/")
    .run_from_cli();

Trait Implementations§

Source§

impl Debug for ModelGen

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ModelGen

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more