Files
burn/examples/import-model-weights
Dilshod Tadjibaev 933fdf4f69 Move ONNX crates to burn-onnx repository (#4393)
* Move ONNX inference MNIST example to burn-onnx

Moving to https://github.com/tracel-ai/burn-onnx/tree/main/examples/onnx-inference

* Move Raspberry Pi Pico example project to burn-onnx

Moving to https://github.com/tracel-ai/burn-onnx/tree/main/examples/raspberry-pi-pico

* Move image-classification-web example to burn-onnx

Moving to https://github.com/tracel-ai/burn-onnx/tree/main/examples/image-classification-web

* Refactor import-model-weights example to use burn-store

Replace burn-import with burn-store for loading PyTorch and Safetensors
model weights. Convert from NamedMpk (.mpk) format to Burnpack (.bpk)
format for native Burn model storage.

- Use PytorchStore and SafetensorsStore for weight loading
- Use BurnpackStore for saving/loading converted models
- Rename namedmpk binary to burnpack
- Add PyTorchToBurnAdapter for Safetensors files exported from PyTorch
- Update inference to accept Model directly instead of ModelRecord

* Remove ONNX to Burn development guide from contributor-book

The guide has been moved to the burn-onnx repository:
https://github.com/tracel-ai/burn-onnx/blob/main/DEVELOPMENT-GUIDE.md

* Update Cargo.lock

* Move pytorch-tests and safetensors-tests from burn-import to burn-store

Migrate test directories from crates/burn-import/ to crates/burn-store/
and update all tests to use the new burn-store API.

Changes:
- Update Cargo.toml dependencies from burn-import to burn-store
- Replace PyTorchFileRecorder/SafetensorsFileRecorder with PytorchStore/SafetensorsStore
- Convert record-based loading to direct model loading via ModuleSnapshot::load_from()
- Convert LoadArgs options to fluent builder pattern (.with_key_remapping(), etc.)
- Fix model configurations to match actual PyTorch weight file dimensions
- Add init() methods for models that previously only had new_with(record)

All 37 pytorch tests and 1 safetensors test pass.

* Fix test file paths after moving test directories to burn-store

Update paths in burn-store/src tests to reference the new locations
of pytorch-tests and safetensors-tests directories.

* Add migration guide for burn-import to burn-store

Document migration path from deprecated PyTorchFileRecorder and
SafetensorsFileRecorder to the new PytorchStore and SafetensorsStore APIs.
Cover API mapping, code examples, and common migration issues.

Include details on printing LoadResult for debugging and helpful suggestions.

* Remove burn-import crate (moved to burn-onnx repo)

The burn-import crate has been moved to a separate repository:
https://github.com/tracel-ai/burn-onnx/tree/main/crates/burn-import

For loading PyTorch and SafeTensors model weights, use burn-store instead
with PytorchStore and SafetensorsStore.

* Replace pytorch/safetensors docs with unified model-weights page

Consolidate PyTorch and SafeTensors model import documentation into a
single comprehensive page covering burn-store usage. The new page covers:

- All supported formats (Burnpack, SafeTensors, PyTorch)
- Loading and saving workflows
- Advanced features (filtering, remapping, partial loading, zero-copy)
- API reference and troubleshooting

* Simplify burn-store README and point to Burn Book

* Consolidate model weights docs into saving-and-loading page

Merge the model-weights documentation into the main saving-and-loading
page, providing a single comprehensive guide for all model persistence
operations using burn-store.

- Remove separate import/model-weights.md page
- Update saving-and-loading.md with full burn-store documentation
- Remove unused SVG images from import folder
- Update navigation in SUMMARY.md and import/README.md

* Move ONNX import to standalone section, point to burn-onnx repo

- Create new onnx-import.md as standalone top-level section
- Update links to point to burn-onnx repo (github.com/tracel-ai/burn-onnx)
- Remove import/ folder (no longer needed)
- Streamline documentation with quick start focus

* Restore full ONNX import documentation content

Restored detailed content including:
- Understanding ONNX section
- Burn's ONNX Support advantages
- ONNX compatibility and opset version guidance
- Step-by-step guide with code examples
- Advanced configuration options
- Troubleshooting section
- Examples and resources with links to burn-onnx repo

* Update ONNX examples links to burn-onnx repo

* Update ONNX-related example links in examples.md to burn-onnx repo

* Fix Burnpack extension: .burnpack -> .bpk

* Remove ONNX Tests README reference from docs

* Improve table formatting in ONNX import and saving docs

Reformats markdown tables in onnx-import.md and saving-and-loading.md for better readability and consistency. No content changes, only improved alignment and line breaks.

* Remove onnx-ir and onnx-ir-derive crates

Moved to https://github.com/tracel-ai/burn-onnx

* Remove burn-onnx crate

Moved to https://github.com/tracel-ai/burn-onnx

* Remove ONNX references from workspace config and README

- Remove burn-onnx entries from Cargo.toml workspace members/exclude
- Remove RUSTSEC-2024-0437 (protobuf) from audit.toml
- Update README ONNX section to point to burn-onnx repo
- Update README model loading section to point to new docs
- Remove ONNX example from README (moved to burn-onnx)

* Clean up remaining ONNX references

- Remove ONNX publish jobs from publish.yml
- Update semver-checks exclude list
- Update burn-book overview.md links
- Update burn-book no-std.md links to burn-onnx repo
- Remove ONNX proto license from NOTICES.md
- Update .gitignore comment

* Remove unused protobuf and rust-format dependencies

* Remove onnx-tests example from contributor-book

* Update Cargo.lock

* Format long line in test for readability

Split a long line in the should_fail_if_struct_field_is_missing test to improve code readability. No functional changes were made.

* Restore Record-based saving/loading sections per PR feedback

* Explain burn-store motivation in saving-and-loading docs

* Remove em dash from burn-store intro

* Format long line in test for readability
2026-01-28 14:38:11 -06:00
..

Import Model Weights

This crate provides examples for importing model weights from different formats to Burn.

Examples

PyTorch

Imports weights from a PyTorch .pt file using burn-store.

cargo run --bin pytorch -- <image_index>

Example:

cargo run --bin pytorch -- 15

Loading PyTorch model weights from file: weights/mnist.pt
Image index: 15
Success!
Predicted: 5
Actual: 5
See the image online, click the link below:
https://huggingface.co/datasets/ylecun/mnist/viewer/mnist/test?row=15

Safetensors

Imports weights from a Safetensors file using burn-store.

cargo run --bin safetensors -- <image_index>

Example:

cargo run --bin safetensors -- 42

Loading Safetensors model weights from file: weights/mnist.safetensors
Image index: 42
Success!
Predicted: 4
Actual: 4
See the image online, click the link below:
https://huggingface.co/datasets/ylecun/mnist/viewer/mnist/test?row=42

Convert

Converts between different weight formats (PyTorch or Safetensors) to Burn's native Burnpack format.

cargo run --bin convert -- <format> <output_directory>

Where:

  • <format>: Either pytorch or safetensors
  • <output_directory>: Path to save the converted model file

Example with PyTorch:

cargo run --bin convert -- pytorch /tmp/burn-convert

Loading PyTorch weights from 'weights/mnist.pt'...
Saving model to '/tmp/burn-convert/mnist.bpk'...
Model successfully saved to '/tmp/burn-convert/mnist.bpk'.

Example with Safetensors:

cargo run --bin convert -- safetensors /tmp/burn-convert

Loading Safetensors weights from 'weights/mnist.safetensors'...
Saving model to '/tmp/burn-convert/mnist.bpk'...
Model successfully saved to '/tmp/burn-convert/mnist.bpk'.

Burnpack

Demonstrates loading and using a model from Burn's native Burnpack format.

cargo run --bin burnpack -- <image_index> <model_path>

Where:

  • <image_index>: Index of the MNIST test image to classify
  • <model_path>: Path to the model file (without extension)

Example:

cargo run --bin burnpack -- 35 /tmp/burn-convert/mnist

Loading model weights from file: /tmp/burn-convert/mnist.bpk
Image index: 35
Success!
Predicted: 2
Actual: 2
See the image online, click the link below:
https://huggingface.co/datasets/ylecun/mnist/viewer/mnist/test?row=35

Workflow

A typical workflow using these examples:

  1. Start with pre-trained weights in either PyTorch or Safetensors format
  2. Use the convert example to convert to Burn's native Burnpack format
  3. Load and use the converted model with the burnpack example