<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nikhil Ranjan</title>
    <description>The latest articles on DEV Community by Nikhil Ranjan (@nikhil_ranjan).</description>
    <link>https://dev.to/nikhil_ranjan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4265028%2F8e44a883-1de6-4a87-888f-fe633d461060.jpg</url>
      <title>DEV Community: Nikhil Ranjan</title>
      <link>https://dev.to/nikhil_ranjan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nikhil_ranjan"/>
    <language>en</language>
    <item>
      <title>downshift-server: Serve a pytorch / HF-embedding model in 3 steps</title>
      <dc:creator>Nikhil Ranjan</dc:creator>
      <pubDate>Wed, 25 Nov 2026 15:53:09 +0000</pubDate>
      <link>https://dev.to/nikhil_ranjan/downshift-server-serve-a-pytorch-hf-embedding-model-in-3-steps-4a3l</link>
      <guid>https://dev.to/nikhil_ranjan/downshift-server-serve-a-pytorch-hf-embedding-model-in-3-steps-4a3l</guid>
      <description>&lt;h2&gt;
  
  
  Quick Setup
&lt;/h2&gt;

&lt;p&gt;A hands-on walkthrough of downshift-server 1.0: an embedding model behind the OpenAI route, your own PyTorch model, and a check that catches a wrong ONNX export.&lt;/p&gt;

&lt;p&gt;Step 1:Install torch (the CPU build is fine) and downshift with the Hugging Face extra:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install &lt;span class="s2"&gt;"downshift-server[hf]"&lt;/span&gt; openai
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Download an encoder type embedding model from HF&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;hf download BAAI/bge-small-en-v1.5 --local-dir ./bge-small-en-v1.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3: Serve it out!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;downshift serve ./bge-small-en-v1.5 --port 8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;downshift-server exports the model to ONNX and checks it against the original weights to ensure that the model still performs the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  What just happened?
&lt;/h2&gt;

&lt;p&gt;You are now the creator of a tiny FastAPI server which is serving the model in an OpenAI compatible API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;base_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"http://localhost:8000/v1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"not-used"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;embeddings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"bge-small-en-v1.5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"world"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;embedding&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Ok... But,the title promised me PyTorch too
&lt;/h2&gt;

&lt;p&gt;You don't need a Hugging Face model. Any nn.Module works. Write a &lt;code&gt;model.py&lt;/code&gt; with two small functions: one that builds the model, one that makes an example input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Linear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_inputs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before you serve it, run the check by hand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;downshift check model:make_model
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Export          CLEAN  (strict=False, opset 20)
Numerics        max abs err 5.96e-08 over 8 samples  OK
Rows            independent
Shape-general   yes
Backend         onnxruntime
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"Rows independent" means one row in a batch doesn't change the answer for another. "Shape-general" means the graph accepts other batch sizes, not only the one you exported with. Now serve it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;downshift serve model:make_model --port 8000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now send all the requests you want!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -s localhost:8000/schema &lt;span class="o"&gt;|&lt;/span&gt; python -m json.tool
curl -s localhost:8000/predict -H &lt;span class="s1"&gt;'content-type: application/json'&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{"inputs": {"input": [[1.0, 2.0, 3.0, 4.0]]}}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  This is cute... but SPEED IS KEY!
&lt;/h2&gt;

&lt;p&gt;I benchmarked it on one laptop (AMD Ryzen 7 7840HS, CPU only, Windows 11) against a plain FastAPI + PyTorch server that I wrote for the comparison. Same weights, same request bytes, 8 clients, and every answer checked against PyTorch. &lt;strong&gt;Commence the "trust me bro" benchmarks&lt;/strong&gt;...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fignviotklwmcfywg6oh7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fignviotklwmcfywg6oh7.png" alt="GPU Benchmarks" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftbpa4dgc5vwihalz335p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftbpa4dgc5vwihalz335p.png" alt="CPU Benchmarks on big models" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's not a clean sweep, and I'd rather tell you now:&lt;/p&gt;

&lt;p&gt;Against a hand-written FastAPI + ONNX Runtime server, downshift was slower on small models (0.67x to 0.76x on a small MLP, a GNN and a tiny BERT). It was faster on large ones (1.88x on a large CNN, 1.75x on a small BERT).&lt;/p&gt;

&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;downshift-server works in two phases: &lt;/p&gt;

&lt;p&gt;At boot &lt;code&gt;downshift server "model"&lt;/code&gt;, it loads your model (a Hugging Face folder, or a PyTorch nn.Module from a model.py function), exports it to ONNX, and runs the ONNX graph and the original PyTorch model on the same inputs. If there is a mismatch it serves the torch model instead. It then warms the model up, and GET /ready turns to 200 only when it's safe to send traffic. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxw3l6s1nuuo69pu8mcws.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxw3l6s1nuuo69pu8mcws.png" alt="downshift-server startup" width="799" height="283"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, each request goes through a short hot path: the server parses the body, prepares the input, runs the model, and encodes the answer through ONNX Runtime.  A request queue with limits and a timeout guards that path, and an optional result cache lets a row that keeps coming back skip the model.&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Code: &lt;a href="https://github.com/uNRealCoder/downshift-server" rel="noopener noreferrer"&gt;github.com/uNRealCoder/downshift-server&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Docs: &lt;a href="https://downshift-server.readthedocs.io/en/stable/" rel="noopener noreferrer"&gt;downshift-server.readthedocs.io&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Package: &lt;a href="https://pypi.org/project/downshift-server/" rel="noopener noreferrer"&gt;pypi.org/project/downshift-server&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want longer articles - I wrote two earlier posts on Medium:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@nrnikhilranjan1997/serve-a-pytorch-model-over-http-s-with-downshift-server-110eaa5e988f" rel="noopener noreferrer"&gt;Serve a PyTorch model over HTTP(S) with downshift-server&lt;/a&gt; &lt;br&gt;
and&lt;br&gt;
&lt;a href="https://medium.com/@nrnikhilranjan1997/downshift-server-serve-pytorch-or-hugging-face-models-correctly-in-three-commands-47650b712bef" rel="noopener noreferrer"&gt;downshift-server: serve PyTorch or Hugging Face models correctly in three commands&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The numbers are in &lt;a href="https://medium.com/@nrnikhilranjan1997/downshift-1-0-0-serving-models-with-onnx-over-https-2d83b1c423f8" rel="noopener noreferrer"&gt;downshift 1.0.0: serving models with ONNX over HTTPS&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If downshift gives a wrong answer on your model, open an issue and paste the output of &lt;code&gt;downshift check&lt;/code&gt;. I want those reports.&lt;/p&gt;

&lt;p&gt;Disclaimer:  downshift-server is a one-person project, and I built it with Claude Code as a pair programmer.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>ai</category>
      <category>fastapi</category>
      <category>pytorch</category>
    </item>
  </channel>
</rss>
