PyTorch is officially binded with Rust (tch-rs)

rchavezj: <p>The rust community has grown so much over the years, a library ‘tch-rs’ (<a href="https://docs.rs/tch/0.2.0/tch/" rel="nofollow noopener">https://docs.rs/tch/0.2.0/tch/</a>) has had a successful build (<a href="https://travis-ci.org/LaurentMazare/tch-rs" rel="nofollow noopener">https://travis-ci.org/LaurentMazare/tch-rs</a>) to bind C++ API of PyTorch.</p>
<p>I am posting this to have the PyTorch community expand and self aware if anyone else has experience with rust and open to hear if anyone has tried to deploy PyTorch algorithms with tch-rs.</p>
<aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/LaurentMazare/tch-rs" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars0.githubusercontent.com/u/1041292?s=400&amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/LaurentMazare/tch-rs" target="_blank" rel="nofollow noopener">LaurentMazare/tch-rs</a></h3>

<p>Rust bindings for the C++ api of PyTorch. Contribute to LaurentMazare/tch-rs development by creating an account on GitHub.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


mingfeng.zhang: <p>what is the benefit for binding with rust language ?</p>

rchavezj: <p>Right now Libtorch C++ API deployment is somewhat difficult unless you have an open source library/software (i.e. torchserve) than can wrap serialize model’s into production instead of python web frameworks (flask/django/fastapi). Torchserve is nice for it has an RFC for a high performance Cpp PyTorch serving platform</p>
<p>If you look at the techempower benchmarks, you see in the top 10 we have 4/10 libraries made in rust. This site is used to measure who has the fastest request done for web frameworks.<br>
</p><aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://www.techempower.com/benchmarks/../images/icon.png" class="site-icon" width="16" height="16">
      <a href="https://www.techempower.com/benchmarks/" target="_blank" rel="nofollow noopener">www.techempower.com</a>
  </header>
  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:529/500;"><img src="https://www.techempower.com/benchmarks/img/filter-panel.png" class="thumbnail" width="529" height="500"></div>

<h3><a href="https://www.techempower.com/benchmarks/" target="_blank" rel="nofollow noopener">TechEmpower Web Framework Performance Comparison</a></h3>

<p>Performance comparison of a wide spectrum of web application frameworks and platforms using community-contributed test implementations.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>Right now two popular web frameworks from rust, Rocket and Actix, are gaining traction in the web community. Once you finish creating your algorithms in pytorch using pytorch library, torchscript the model to be loaded up using tch-rs.</p>

legendu: <p>I have a GUI application which was written in Python + Java. Both the GUI and deep learning models are handled by Python and some core logics was written in Java. It has some performance issues and I’m trying to rewrite it using Rust. Basically, I want rewrite the GUI using Rust and load the trained models into Rust using tch-rs.</p>

rchavezj: <p>So you’re writing you’re client side (front end) using Rust, assuming the framework is yew ( <a href="https://github.com/yewstack/yew" class="inline-onebox" rel="noopener nofollow ugc">GitHub - yewstack/yew: Rust / Wasm framework for building client web apps</a> ), to load up you’re PyTorch models using tch-rs?</p>

legendu: <p>I’m learning and trying to use Tauri to build the GUI, using Rust to re-implement some core logic which was originally implemented in Java. And yes, use tch-rs to load trained models.</p>

Mohamed_Taha: <p>i made something like this but it’s more readable like PyTorch</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/AhmedBoin/Perceptrons">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/AhmedBoin/Perceptrons" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/345;"><img src="https://opengraph.githubassets.com/4c832fcf92b63dfa42068fb48b639c144da8cc6b569c137ce22252541d0a99e2/AhmedBoin/Perceptrons" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/AhmedBoin/Perceptrons" target="_blank" rel="noopener nofollow ugc">GitHub - AhmedBoin/Perceptrons</a></h3>

  <p>Contribute to AhmedBoin/Perceptrons development by creating an account on GitHub.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


thistlillo: <p>As we are now in 2025, I would like to ask if there are news on rust-torch bindings. Looking around on the web, I end up visiting <code>tch-rs</code> whose development has not been very active in the past years.</p>