README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈
ExecuTorch-rs
ExecuTorch 的绑定 - 面向 PyTorch 的移动、嵌入式和边缘设备上的端侧 AI。
提供了一个高级 Rust API,用于使用 ExecuTorch 库,具体是 C++ API,在移动、嵌入式和边缘设备上执行 PyTorch 模型。 PyTorch 模型在 Python 中创建和导出,然后使用 ExecuTorch 库在设备端加载和执行。
以下示例在 Python 中创建一个简单模型,导出它,然后在 Rust 中执行它:
在 Python 中创建模型并导出:
import torch
from torch.export import export
from executorch.exir import to_edge_transform_and_lower
class Add(torch.nn.Module):
def __init__(self):
super(Add, self).__init__()
def forward(self, x: torch.Tensor, y: torch.Tensor):
return x + y
model = Add()
exported_program = export(model, (torch.ones(1), torch.ones(1)))
executorch_program = to_edge_transform_and_lower(exported_program).to_executorch()
with open("model.pte", "wb") as file:
file.write(executorch_program.buffer)
在 Rust 中执行模型:
use executorch::evalue::{EValue, IntoEValue};
use executorch::module::Module;
use executorch::tensor_ptr;
use ndarray::array;
let mut module = Module::new("model.pte");
let (tensor1, tensor2) = (tensor_ptr![1.0_f32], tensor_ptr![1.0_f32]);
let inputs = [tensor1.into_evalue(), tensor2.into_evalue()];
let outputs = module.forward(&inputs).unwrap();
let [output]: [EValue; 1] = outputs.try_into().expect("not a single output");
let output = output.as_tensor().into_typed::<f32>();
println!("Output tensor computed: {:?}", output);
assert_eq!(array![2.0], output.as_array());
参见 example/hello_world 获取完整示例。
构建
要使用该库,您必须自行编译 C++ executorch 库,因为有许多配置
决定了支持哪些模块、后端和操作。有关更多信息,请参阅 executorch-sys crate。
当前支持的 Cpp executorch 版本是 1.3.1。
下表显示了哪个版本的 Rust crate 与哪个 C++ ExecuTorch 版本兼容:
| Rust crate | C++ 库 |
|---|---|
| 0.11.x | 1.3.1 |
| 0.10.x | 1.2.0 |
| 0.9.x | 1.1.0 |
| 0.8.x | 1.0.1 |
| 0.7.x | 0.7.0 |
| 0.6.x | 0.6.0 |
| 0.5.x | 0.5.0 |
| 0.4.x | 0.4.0 |
| 0.3.x | 0.3.0 |
| 0.2.x | 0.3.0 |
| 0.1.x | 0.2.1 |
Cargo 特性
data-loader: 在data_loader模块中包含用于加载数据的额外结构体。如果没有此特性,唯一 可用的数据加载器是BufferDataLoader。需要libextension_data_loader.a静态库, 使用EXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON编译 C++executorch。module: 包含moduleAPI,这是一个用于加载和执行 PyTorch 模型的高级 API。它是 低级别ProgramAPI 的替代方案,后者更适合嵌入式系统。 需要libextension_module_static.a静态库,使用EXECUTORCH_BUILD_EXTENSION_MODULE=ON编译 C++executorch。 还包含std、data-loader和flat-tensor特性。tensor-ptr: 包含tensor::TensorPtr结构体,一种用于张量的智能指针,它管理张量 对象的生命周期,同时管理数据缓冲区以及额外元数据的生命周期。需要extension_tensor.a静态库,使用EXECUTORCH_BUILD_EXTENSION_TENSOR=ON编译 C++executorch。 还包含std特性。flat-tensor: 包含FlatTensorDataMap结构体,可以读取带有外部张量的模型的.ptd文件。 需要libextension_flat_tensor.a静态库, 使用EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON编译 C++executorch。etdump: 包含ETDumpGen结构体,它是EventTracer的一种实现,用于调试和分析。 需要libetdump.a静态库,使用EXECUTORCH_BUILD_DEVTOOLS=ON和EXECUTORCH_ENABLE_EVENT_TRACER=ON编译 C++executorch。 此外,需要flatcc(或flatcc_d)库,可在{CMAKE_DIR}/third-party/flatcc_ep/lib/获取, 并且应由用户进行链接。ndarray:executorch张量与ndarray数组之间的转换。 添加对ndarraycrate 的依赖。 此特性默认启用。f16: 添加对halfcrate 的依赖,该 crate 提供功能完整的f16和bf16类型。 如果未启用此功能,这两种类型仅可通过简单的转换与u16相互转换。 请注意,这仅影响输入/输出张量,内部计算始终具备处理此类标量的能力。num-complex: 添加对num-complexcrate 的依赖,该 crate 提供了功能完备的复数类型。 如果未启用此功能,复数将作为一个具有两个公开字段且无任何操作的简单结构体可用。 请注意,这仅影响输入/输出张量,内部计算始终具备处理此类标量的能力。std: 启用标准库。此功能默认启用,但可以禁用以在no_std环境中构建executorch。 参见examples/no_std示例。 同时包含alloc功能。 注意:no_std 仍处于开发中,参见 https://github.com/pytorch/executorch/issues/4561alloc: 启用内存分配。 当此功能被禁用时,所有需要内存分配的方法将不会被编译。 此功能由std功能启用,而该功能默认启用。 可以在不启用std功能的情况下启用此功能,此时内存分配将使用alloccrate 完成,该 crate 要求设置全局分配器。
默认情况下,std 和 ndarray 功能是启用的。