Is it possible use a executorch model with rust code?

nimit0: <p>Hello, I am new to executorch and I wanted to know if it is possible to use a model created and optimized with executorch to be used in a code written in Rust for an embedded system, or is it nowadays possible only with C/C++?<br>
Ty for your answers.</p>

guangy10: <p>I think you are referring to the runtime. So far it’s possible only with C++.</p>

dbort: <p>If you wanted to call the ExecuTorch runtime from Rust code, you could try wrapping it with <a href="https://docs.rs/cxx/latest/cxx/" class="inline-onebox" rel="noopener nofollow ugc">cxx - Rust</a>.</p>

barakugav: <p>After search the web a bit for a solution, I decided to write a crate for it:<br>
<a href="https://github.com/barakugav/executorch-rs/" rel="noopener nofollow ugc">https://github.com/barakugav/executorch-rs/</a></p>

dbort: <p>Whoa that’s great <a class="mention" href="/u/barakugav">@barakugav</a>! How difficult was it to do that? Do you have any suggestions for ways to improve the ET API to make this sort of thing easier?</p>

barakugav: <p>hey <a class="mention" href="/u/dbort">@dbort</a>, are you one of ExecuTorch maintainers?</p>
<p>It wasn’t super hard to do, but took me a few days in total. Most difficulties arose because its hard to write bindings to c++, and its much easier to do for c api. The API itself is very straight forward, and kind of minimal, which is great for what I tried to do. Few concrete points:</p>
<ul>
<li>The documentation usually stated exactly which argument or field should outlive which object, which was very helpful especially when trying to express that precisely for the Rust borrow checker using lifetimes. Sometimes I had to check the code to make sure or if the docs were missing.</li>
<li>The compilation flags to cmake are very straight forward, but I did not find any documentation for them.</li>
<li>I think there are some errors in the docs about the generated static libs, maybe it worth creating a detailed page for it in the tutorial.</li>
<li>ArrayRef and Span are great, i would love to see use for these in places that expect <code>const chat *</code>. When called from Rust (that does not have <code>\0</code> at the end of strings) it require additional allocation.</li>
<li>I had to solve some very weird phenomenas when calling cpp from Rust: for small Result objects, usually with 16 bytes total, the bytes were altered (??) between returning from the cpp func to the Rust caller. Very weird. My guess is that my cpp compiler chose some unexpected ABI, maybe for optimization, and Rust didnt handle it well.</li>
<li>Functions implementations in headers are not callable from Rust, as Rust bindgen only generate function declarations and it expect the implementations to be in the static lib (doesnt happen with header impls). To solve it and the previous point, I had to compile additional c++ static lib that expose a C API for these methods. Its a big limitation for templates, unfortunate.</li>
</ul>
<p>Last items, kind of requests:</p>
<ul>
<li>It would be great to expose some precompiled binaries for ExecuTorch, maybe with the most common operations. Would be great for CI and for easy hello_word examples.</li>
<li>As ExecuTorch target edge devices, even embedded system, I think it crucial to be able to compile without the standard library, currently I wasnt able to do so</li>
</ul>

dbort: <p>Thank you for taking the time to write this up, it’s super helpful! I am one of the <a href="https://github.com/pytorch/executorch/commits/main/?author=dbort" rel="noopener nofollow ugc">maintainers</a>, and I’ll pass this along to other people on the project.</p>
<blockquote>
<p>It would be great to expose some precompiled binaries for ExecuTorch, maybe with the most common operations. Would be great for CI and for easy hello_word examples.</p>
</blockquote>
<p>This is something we want to do, similar to PyTorch’s <code>libtorch</code>: a zip file with prebuilt binaries, headers, and CMake stubs for linking against them.</p>
<blockquote>
<p>As ExecuTorch target edge devices, even embedded system, I think it crucial to be able to compile with the standard library, currently I wasnt able to do so</p>
</blockquote>
<p>If you have more details here, could you create a <a href="https://github.com/pytorch/executorch/issues/new/choose" rel="noopener nofollow ugc">github issue</a>? We do have users who use ET on bare-metal embedded systems, but they use an internal build system instead of CMake. So, the code should be good, but maybe we need to add more control to the CMake system.</p>

barakugav: <p>Interesting, I wonder how they link malloc for example, maybe they just write a stub and dont use it.<br>
Anyway, opened an issue: <a href="https://github.com/pytorch/executorch/issues/4561" class="inline-onebox" rel="noopener nofollow ugc">Option to build without the standard library · Issue #4561 · pytorch/executorch · GitHub</a>.<br>
Thanks for the support! Keep it going <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>