downshift-server: Serve a pytorch / HF-embedding model in 3 steps
Quick Setup 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. Step 1:Install torch (the CPU build is fine
Quick Setup
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.
Step 1:Install torch (the CPU build is fine) and downshift with the Hugging Face extra:
pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install "downshift-server[hf]" openai
Step 2: Download an encoder type embedding model from HF
hf download BAAI/bge-small-en-v1.5 --local-dir ./bge-small-en-v1.5
Step 3: Serve it out!
downshift serve ./bge-small-en-v1.5 --port 8000
downshift-server exports the model to ONNX and checks it against the original weights to ensure that the model still performs the same.
What just happened?
You are now the creator of a tiny FastAPI server which is serving the model in an OpenAI compatible API.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1", api_key="not-used")
response = client.embeddings.create(model="bge-small-en-v1.5", input=["hello", "world"])
print(len(response.data), len(response.data[0].embedding))
Ok... But,the title promised me PyTorch too
You don't need a Hugging Face model. Any nn.Module works. Write a model.py with two small functions: one that builds the model, one that makes an example input.
import torch
def make_model():
return torch.nn.Linear(4, 2).eval()
def make_inputs():
return (torch.randn(1, 4),)
Before you serve it, run the check by hand:
downshift check model:make_model
Export CLEAN (strict=False, opset 20)
Numerics max abs err 5.96e-08 over 8 samples OK
Rows independent
Shape-general yes
Backend onnxruntime
"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:
downshift serve model:make_model --port 8000
Now send all the requests you want!
curl -s localhost:8000/schema | python -m json.tool
curl -s localhost:8000/predict -H 'content-type: application/json' \
-d '{"inputs": {"input": [[1.0, 2.0, 3.0, 4.0]]}}'
This is cute... but SPEED IS KEY!
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. Commence the "trust me bro" benchmarks...
It's not a clean sweep, and I'd rather tell you now:
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).
How does it work?
downshift-server works in two phases:
At boot downshift server "model", 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.
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.
Links
- Code: github.com/uNRealCoder/downshift-server
- Docs: downshift-server.readthedocs.io
- Package: pypi.org/project/downshift-server
If you want longer articles - I wrote two earlier posts on Medium:
Serve a PyTorch model over HTTP(S) with downshift-server
and
downshift-server: serve PyTorch or Hugging Face models correctly in three commands.
The numbers are in downshift 1.0.0: serving models with ONNX over HTTPS.
If downshift gives a wrong answer on your model, open an issue and paste the output of downshift check. I want those reports.
Disclaimer: downshift-server is a one-person project, and I built it with Claude Code as a pair programmer.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.

