Crates.io | rten |
lib.rs | rten |
version | 0.14.0 |
source | src |
created_at | 2023-12-31 20:37:19.71133 |
updated_at | 2024-10-27 17:51:28.051549 |
description | Machine learning runtime |
homepage | https://github.com/robertknight/rten |
repository | https://github.com/robertknight/rten |
max_upload_size | |
id | 1084999 |
size | 1,537,123 |
RTen (the Rust Tensor engine) † is a runtime for machine learning models converted from ONNX format, which you can export from PyTorch and other frameworks.
The project also provides supporting libraries for common pre-processing and post-processing tasks in various domains. This makes RTen a more complete toolkit for running models in Rust applications.
† The name is also a reference to PyTorch's ATen library.
This project has a number of limitations to be aware of. Addressing them is planned for the future:
OperatorType
in
src/schema.fbs and this issue for currently supported operators. For
implemented operators, some attributes or input shapes may not be supported.The best way to get started is to clone this repository and try running some of the examples locally. The conversion scripts use popular Python machine learning libraries, so you will need Python >= 3.10 installed.
The examples are located in the rten-examples/ directory. See the README for descriptions of all the examples and steps to run them. As a quick-start, here are the steps to run the image classification example:
git clone https://github.com/robertknight/rten.git
cd rten
# Install model conversion tool
pip install -e rten-convert
# Install dependencies for Python scripts
pip install -r tools/requirements.txt
# Export an ONNX model. We're using resnet-50, a classic image classification model.
python -m tools.export-timm-model timm/resnet50.a1_in1k
# Convert model to this library's format
rten-convert resnet50.a1_in1k.onnx resnet50.rten
# Run image classification example. Replace `image.png` with your own image.
cargo run -p rten-examples --release --bin imagenet mobilenet resnet50.rten image.png
RTen does not load ONNX models directly. ONNX models must be run through a
conversion tool which produces an optimized model in a
FlatBuffers-based format (.rten
) that
the engine can load. This is conceptually similar to the .tflite
and .ort
formats that TensorFlow Lite and ONNX Runtime use.
The conversion tool requires Python >= 3.10. To convert an existing ONNX model, run:
pip install rten-convert
rten-convert your-model.onnx
See the rten-convert README for more information about usage and version compatibility.
To use this library in a JavaScript application, there are two approaches:
Prepare model inputs in JavaScript and use the rten library's built-in WebAssembly API to run the model and return a tensor which will then need to be post-processed in JS. This approach may be easiest for tasks where the pre-processing is simple.
The image classification example uses this approach.
Create a Rust library that uses rten and does pre-processing of inputs and post-processing of outputs on the Rust side, exposing a domain-specific WebAssembly API. This approach is more suitable if you have complex and/or computationally intensive pre/post-processing to do.
Before running the examples, you will need to follow the steps under "Building the WebAssembly library" below.
The general steps for using RTen's built-in WebAssembly API to run models in a JavaScript project are:
rten-convert
package in this repository to convert the model
to the optimized format RTen uses. See the section above on converting models.init
function..rten
model and use it to an instantiate the Model
class from this library.Float32Array
s
containing input data in the format expected by the model, and call
Model.run
. This will return a TensorList
that provides access to the
shapes and data of the outputs.After building the library, API documentation for the Model
and TensorList
classes is available in dist/rten.d.ts
.
To build RTen for WebAssembly you will need:
make
wasm-opt
tool from Binaryen
can be used to optimize .wasm
binaries for improved performancegit clone https://github.com/robertknight/rten.git
cd rten
make wasm
The build created by make wasm
requires support for WebAssembly SIMD,
available since Chrome 91, Firefox 89 and Safari 16.4. It is possible to
build the library without WebAssembly SIMD support using make wasm-nosimd
,
or both using make wasm-all
. The non-SIMD builds are significantly slower.
At runtime, you can find out which build is supported by calling the
binaryName()
function exported by this package.