{"id":17652542,"url":"https://github.com/barakugav/executorch-rs","last_synced_at":"2025-04-14T10:33:18.448Z","repository":{"id":249568476,"uuid":"827413833","full_name":"barakugav/executorch-rs","owner":"barakugav","description":"Rust bindings for ExecuTorch - On-device AI across mobile, embedded and edge for PyTorch","archived":false,"fork":false,"pushed_at":"2025-03-08T18:12:11.000Z","size":873,"stargazers_count":27,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-27T23:41:52.510Z","etag":null,"topics":["edge-device","embedded","pytorch","rust-bindings"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/executorch","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/barakugav.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-11T15:56:16.000Z","updated_at":"2025-03-26T16:55:54.000Z","dependencies_parsed_at":"2024-09-07T17:38:42.893Z","dependency_job_id":"04bb8f75-3c44-4028-8260-446d0ec4ebe4","html_url":"https://github.com/barakugav/executorch-rs","commit_stats":null,"previous_names":["barakugav/executorch-rs"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barakugav%2Fexecutorch-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barakugav%2Fexecutorch-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barakugav%2Fexecutorch-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barakugav%2Fexecutorch-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/barakugav","download_url":"https://codeload.github.com/barakugav/executorch-rs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248862829,"owners_count":21173893,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["edge-device","embedded","pytorch","rust-bindings"],"created_at":"2024-10-23T11:47:18.611Z","updated_at":"2025-04-14T10:33:18.430Z","avatar_url":"https://github.com/barakugav.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ExecuTorch-rs\n\n[![Crates.io](https://img.shields.io/crates/v/executorch.svg)](https://crates.io/crates/executorch/)\n[![Documentation](https://docs.rs/executorch/badge.svg)](https://docs.rs/executorch/)\n![License](https://img.shields.io/crates/l/executorch)\n\n\n`executorch` is a Rust library for executing PyTorch models in Rust.\nIt is a Rust wrapper around the [ExecuTorch C++ API](https://pytorch.org/executorch).\nIt depends on version `0.5.0` of the Cpp API, but will advance as the API does.\nThe underlying C++ library is still in Beta, and its API is subject to change together with the Rust API.\n\n## Usage\nCreate a model in Python and export it:\n```python\nimport torch\nfrom executorch.exir import to_edge\nfrom torch.export import export\n\nclass Add(torch.nn.Module):\n    def __init__(self):\n        super(Add, self).__init__()\n\n    def forward(self, x: torch.Tensor, y: torch.Tensor):\n        return x + y\n\n\naten_dialect = export(Add(), (torch.ones(1), torch.ones(1)))\nedge_program = to_edge(aten_dialect)\nexecutorch_program = edge_program.to_executorch()\nwith open(\"model.pte\", \"wb\") as file:\n    file.write(executorch_program.buffer)\n```\nExecute the model in Rust:\n```rust\nuse executorch::evalue::IntoEValue;\nuse executorch::module::Module;\nuse executorch::tensor_ptr;\nuse ndarray::array;\n\nlet mut module = Module::new(\"model.pte\", None);\n\nlet (tensor1, tensor2) = (tensor_ptr![1.0_f32], tensor_ptr![1.0_f32]);\nlet inputs = [tensor1.into_evalue(), tensor2.into_evalue()];\n\nlet outputs = module.forward(\u0026inputs).unwrap();\nassert_eq!(outputs.len(), 1);\nlet output = outputs.into_iter().next().unwrap();\nlet output = output.as_tensor().into_typed::\u003cf32\u003e();\n\nprintln!(\"Output tensor computed: {:?}\", output);\nassert_eq!(array![2.0], output.as_array());\n```\nSee `example/hello_world` for a complete example.\n\n## Build\nTo build the library, you need to build the C++ library first.\nThe C++ library allow for great flexibility with many flags, customizing which modules, kernels, and extensions are built.\nMultiple static libraries are built, and the Rust library links to them.\nIn the following example we build the C++ library with the necessary flags to run example `hello_world`:\n```bash\n# Clone the C++ library\ncd ${EXECUTORCH_CPP_DIR}\ngit clone --depth 1 --branch v0.5.0 https://github.com/pytorch/executorch.git .\ngit submodule sync --recursive\ngit submodule update --init --recursive\n\n# Install requirements\n./install_requirements.sh\n\n# Build C++ library\nmkdir cmake-out \u0026\u0026 cd cmake-out\ncmake \\\n    -DDEXECUTORCH_SELECT_OPS_LIST=aten::add.out \\\n    -DEXECUTORCH_BUILD_EXECUTOR_RUNNER=OFF \\\n    -DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=OFF \\\n    -DBUILD_EXECUTORCH_PORTABLE_OPS=ON \\\n    -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \\\n    -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \\\n    -DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \\\n    -DEXECUTORCH_ENABLE_PROGRAM_VERIFICATION=ON \\\n    -DEXECUTORCH_ENABLE_LOGGING=ON \\\n    ..\nmake -j\n\n# Static libraries are in cmake-out/\n# core:\n#   cmake-out/libexecutorch.a\n#   cmake-out/libexecutorch_core.a\n# kernels implementations:\n#   cmake-out/kernels/portable/libportable_ops_lib.a\n#   cmake-out/kernels/portable/libportable_kernels.a\n# extension data loader, enabled with EXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON:\n#   cmake-out/extension/data_loader/libextension_data_loader.a\n# extension module, enabled with EXECUTORCH_BUILD_EXTENSION_MODULE=ON:\n#   cmake-out/extension/module/libextension_module_static.a\n# extension tensor, enabled with EXECUTORCH_BUILD_EXTENSION_TENSOR=ON:\n#   cmake-out/extension/tensor/libextension_tensor.a\n# extension tensor, enabled with EXECUTORCH_BUILD_DEVTOOLS=ON:\n#   cmake-out/devtools/libetdump.a\n\n# Run example\n# We set EXECUTORCH_RS_EXECUTORCH_LIB_DIR to the path of the C++ build output\ncd ${EXECUTORCH_RS_DIR}/examples/hello_world\npython export_model.py\nEXECUTORCH_RS_EXECUTORCH_LIB_DIR=${EXECUTORCH_CPP_DIR}/cmake-out cargo run\n```\n\nThe `executorch` crate will always look for the following static libraries:\n- `libexecutorch.a`\n- `libexecutorch_core.a`\n\nAdditional libs are required if feature flags are enabled (see next section):\n- `libextension_data_loader.a`\n- `libextension_module_static.a`\n- `libextension_tensor.a`\n- `libetdump.a`\n\nThe static libraries of the kernels implementations are required only if your model uses them, and they should be **linked manually** by the binary that uses the `executorch` crate.\nFor example, the `hello_world` example uses a model with a single addition operation, so it compile the C++ library with `DEXECUTORCH_SELECT_OPS_LIST=aten::add.out` and contain the following lines in its `build.rs`:\n```rust\nprintln!(\"cargo::rustc-link-lib=static:+whole-archive=portable_kernels\");\nprintln!(\"cargo::rustc-link-lib=static:+whole-archive=portable_ops_lib\");\n\nlet libs_dir = std::env::var(\"EXECUTORCH_RS_EXECUTORCH_LIB_DIR\").unwrap();\nprintln!(\"cargo::rustc-link-search=native={libs_dir}/kernels/portable/\");\n```\nNote that the ops and kernels libs are linked with `+whole-archive` to ensure that all symbols are included in the binary.\n\nThe build (and library) is tested on Ubuntu and MacOS, not on Windows.\n\n## Cargo Features\n- `data-loader`\n\n    Includes the `FileDataLoader` and `MmapDataLoader` structs. Without this feature the only available data loader is `BufferDataLoader`. The `libextension_data_loader.a` static library is required, compile C++ `executorch` with `EXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON`.\n\n- `module`\n\n    Includes the `Module` struct. The `libextension_module_static.a` static library is required, compile C++ `executorch` with `EXECUTORCH_BUILD_EXTENSION_MODULE=ON`.\n    Also includes the `std` feature.\n\n- `tensor-ptr`\n\n    Includes the `TensorPtr` struct, a smart pointer for tensors that manage the lifetime of the tensor\n    object alongside the lifetimes of the data buffer and additional metadata. The `libextension_tensor.a`\n    static library is required, compile C++ `executorch` with `EXECUTORCH_BUILD_EXTENSION_TENSOR=ON`.\n    Also includes the `std` feature.\n\n- `etdump`\n\n    Includes the `ETDumpGen` struct, an implementation of an `EventTracer`, used for debugging and profiling.\n    The `libetdump.a` static library is required, compile C++ `executorch` with `EXECUTORCH_BUILD_DEVTOOLS=ON` and\n    `EXECUTORCH_ENABLE_EVENT_TRACER=ON`.\n    In addition, the `flatcc` (or `flatcc_d`) library is required, available at `{CPP_EXECUTORCH_DIR}/third-party/flatcc/lib/`,\n    and should be linked by the user.\n\n- `ndarray`\n\n    Conversions between `executorch` tensors and `ndarray` arrays.\n    Adds a dependency to the `ndarray` crate.\n    This feature is enabled by default.\n\n- `half`\n\n    Adds a dependency to the `half` crate, which provides a fully capable `f16` and `bf16` types.\n    Without this feature enabled, both of these types are available with a simple conversions to/from `u16` only.\n    Note that this only affect input/output tensors, the internal computations always have the capability to operate on such scalars.\n\n- `num-complex`\n\n    Adds a dependency to the `num-complex` crate, which provides a fully capable complex number type.\n    Without this feature enabled, complex numbers are available as a simple struct with two public fields without any operations.\n    Note that this only affect input/output tensors, the internal computations always have the capability to operate on such scalars.\n\n- `std`\n\n    Enable the standard library. This feature is enabled by default, but can be disabled to build `executorch` in a `no_std` environment.\n    See the `examples/no_std` example.\n    Also includes the `alloc` feature.\n    NOTE: no_std is still WIP, see https://github.com/pytorch/executorch/issues/4561\n\n- `alloc`\n\n    Enable allocations.\n    When this feature is disabled, all methods that require allocations will not be compiled.\n    This feature is enabled by the `std` feature, which is enabled by default.\n    Its possible to enable this feature without the `std` feature, and the allocations will be done using the `alloc` crate, that requires a global allocator to be set.\n\nBy default the `std` and `ndarray` features are enabled.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbarakugav%2Fexecutorch-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbarakugav%2Fexecutorch-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbarakugav%2Fexecutorch-rs/lists"}