Running PyTorch on microcontrollers has never been simpler.
Deploy to a microcontroller without leaving PyTorch: the DeepGate SDK adds built-in preprocessing, one-line PTQ and QAT quantization, and model export.
PyTorch is the world’s most popular machine learning framework for building AI – but deploying models on microcontrollers remains a fragmented workflow. Taking a trained model off the GPU and onto a microcontroller involves converting, quantizing, and compiling it through a chain of separate tools. Every handoff introduces another opportunity for conversion failures or for the deployed model to differ from the one you trained. Recovering accuracy after quantization often becomes an iterative workflow.
Preprocessing creates another source of errors. The image or audio pipeline used during training is typically written in Python, but deploying it on a microcontroller often requires recreating or matching that pipeline in embedded code. Even small differences can quietly degrade model accuracy.
The DeepGate SDK extends PyTorch with everything needed to build AI for microcontrollers. Keep writing standard PyTorch, define preprocessing, quantize, and export from a single workflow – then upload to the DeepGate platform to build for your target microcontroller from hundreds of supported devices, or benchmark your model on supported hardware with detailed inference statistics.
Built-in data preprocessing
Define image and audio preprocessing once in PyTorch and export it with your model. On-device AI starts with raw camera or microphone data, which must be preprocessed before it reaches your model. The preprocessing used during training is typically written in Python, but it isn’t included with the exported model – so deploying to a microcontroller often requires recreating or matching that pipeline in embedded code. Even small differences can quietly degrade model accuracy. With the DeepGate SDK, your preprocessing is exported alongside the model, ensuring the exact same pipeline is used during both training and deployment.
import dg
from dg.preprocess import (AudioPipeline, DCT, Log, MelFilterbank,
PeakNormalize, Spectrum, Window)
# 1. Define the MFCC frontend once, in Python.
pipe = AudioPipeline([
PeakNormalize(eps=1e-6),
Window(frame_length=480, hop=320), # 30 ms frames, 20 ms hop
Spectrum(n_fft=512, power=1),
MelFilterbank(num_mels=40, sample_rate=16000),
Log(eps=1e-6),
DCT(num_coefficients=10), # -> [49, 10] MFCC
], num_samples=16000)
# 2. Train on its output with a standard PyTorch loop.
features = pipe.features(waveforms, batch_size=2048)
# 3. Export the pipeline with the model. The same steps compile into
# the graph, so the deployed model takes raw int16 PCM.
schema = dg.export(quantized_model, preprocess=pipe)Train and quantize
Quantize and export your models from PyTorch with one-line Post-Training Quantization (PTQ) and Quantization-Aware Training (QAT). AI models for microcontrollers almost always need to be quantized before deployment to fit within the device’s limited memory and compute resources. Traditionally, that means leaving PyTorch, using separate quantization tools, and iteratively recovering lost accuracy.
The DeepGate SDK keeps quantization inside PyTorch. Perform both PTQ and QAT with a single line of code – no separate tooling or model conversion required. PTQ quickly quantizes an existing model for deployment, while QAT trains your model to compensate for quantization and maximise accuracy. Compare the accuracy of your original and quantized models before deployment, then export the quantized model directly from PyTorch, ready to upload to the DeepGate platform.
import dg
# Post-training quantization: one line, calibrated on representative data.
quantized = dg.post_training_quantize(model.eval(), train_ds, num_samples=1024)
# Or quantization-aware training: one line, then keep your own loop.
qat_model = dg.enable_quantization(model, (example_input,))
for x, y in loader:
optimizer.zero_grad()
loss = F.cross_entropy(qat_model(x), y)
loss.backward()
optimizer.step()
quantized = dg.freeze_quantization(qat_model.cpu())
# Compare the two before you commit, then export.
print(accuracy(model), accuracy(quantized))
schema = dg.export(quantized)Explore novel DeepGate layers
One of DeepGate’s core missions is to push the efficiency frontier of AI. As part of this mission, we’re developing new machine learning building blocks that enable more efficient model architectures – and making them available in the SDK as they mature.
- Train Logic Networks → that perform inference using pure logic operations instead of multiplications. We’re particularly excited about these models because they could enable an entirely new class of AI hardware architectures built without multiplication circuits.
- Explore in-place Linear layers → that dramatically reduce peak RAM usage, enabling larger models to fit on memory-constrained hardware.
These are early releases, but they’re already delivering significant efficiency gains in our internal research. Over time we’ll publish more code examples, benchmarks, and technical reports – but we’d love for the developer community to start experimenting today.
Start from a working example
The fastest way to get started is with one of our GitHub tutorials. Explore end-to-end examples for image classification, keyword spotting, and visual wake words. Each tutorial demonstrates built-in data preprocessing, one-line quantization, and exporting a model ready to upload to the DeepGate platform.