20 min
Lesson 3 of 4
Edge Deployment and Optimization
From the course Real-Time AI Inference
Deploy AI models at the edge for ultra-low latency without network round-trips.
Edge AI Deployment
Model Optimization for Edge
import torch
from torch.quantization import quantize_dynamic
# Dynamic quantization for edge deployment
model_fp32 = YourModel()
model_int8 = quantize_dynamic(
model_fp32,
{torch.nn.Linear}, # Quantize linear layers
dtype=torch.qint8
)
# Save optimized model
torch.save(model_int8.state_dict(), "edge_model_int8.pt")
# Typically 4x smaller, 2x faster on CPU
# TorchScript for deployment
scripted_model = torch.jit.script(model_int8)
scripted_model.save("edge_model_scripted.pt")
ONNX Runtime for Cross-Platform
import onnxruntime as ort
import numpy as np
# Load ONNX model (runs on CPU, GPU, mobile, edge)
session = ort.InferenceSession(
"model.onnx",
providers=["CUDAExecutionProvider", "CPUExecutionProvider"]
)
# Run inference
input_name = session.get_inputs()[0].name
output = session.run(None, {input_name: input_array})
# ONNX Runtime optimizations enabled automatically:
# - Operator fusion
# - Memory optimization
# - CPU-specific vectorization
WebAssembly for Browser
// Run AI model directly in browser (no server)
import { pipeline } from '@xenova/transformers';
// Model downloads and caches in browser
const classifier = await pipeline('sentiment-analysis');
const result = await classifier('I love this product!');
// [{label: 'POSITIVE', score: 0.999}]
// Zero latency after initial download
Architecture Comparison
| Architecture | Latency | Throughput | Privacy | Cost |
|---|---|---|---|---|
| Cloud API | 200-2000ms | High | Low | Per-token |
| Self-hosted | 50-500ms | Medium | High | Fixed |
| Edge device | 10-100ms | Low | Highest | HW cost |
| Browser (WASM) | 100-2000ms | Lowest | Highest | Free |
When to Use Edge AI
- Privacy-sensitive data (healthcare, legal)
- Offline requirements
- Consistent sub-100ms latency requirements
- High volume making cloud API cost prohibitive
- Regulatory data residency requirements
Key Takeaways
- •Dynamic quantization reduces model size 4x and increases CPU speed 2x for edge
- •ONNX Runtime provides cross-platform inference on CPU, GPU, and mobile
- •WebAssembly enables in-browser AI with zero server costs and maximum privacy
- •Edge deployment choice depends on latency requirements, privacy needs, and data volume
0 of 4 lessons marked complete
Progress is kept in this browser only.