Skip to content

Inference & Deployment ​

SoraSR models export cleanly from PyTorch checkpoints to ONNX and TensorRT engines for predictable production serving.

1. Export to ONNX ​

Export a checkpoint with dynamic spatial axes (recommended for general serving) or fixed tile dimensions (optimal for TensorRT kernel selection):

bash
sorasr export onnx \
  --checkpoint checkpoints/sorasr_s_x4_ema.pt \
  --output artifacts/sorasr_s_x4.onnx \
  --opset 17 \
  --dynamic-hw \
  --verify

The --verify flag runs random test tensors through both PyTorch and ONNX Runtime and asserts maximum absolute error < 1e-4 (FP32) or < 1e-2 (FP16).

2. Build a TensorRT Engine ​

Compile the verified ONNX graph into a hardware-tuned TensorRT engine with explicit optimization profiles:

bash
trtexec \
  --onnx=artifacts/sorasr_s_x4.onnx \
  --saveEngine=artifacts/sorasr_s_x4_fp16.engine \
  --fp16 \
  --minShapes=input:1x3x64x64 \
  --optShapes=input:1x3x256x256 \
  --maxShapes=input:1x3x512x512 \
  --memPoolSize=workspace:4096MiB

Fixed-Shape Tile Engines

If your deployment always runs tiled inference with a fixed 256 × 256 tile window, compiling a static-shape engine (--shapes=input:1x3x256x256) yields 10–18% lower latency than a dynamic-shape profile.

3. Overlap-Blended Tiled Inference ​

Full-frame 4x super-resolution of a 1080p frame produces a 3840 × 2160 output whose intermediate feature tensors can exceed 16 GB VRAM. TiledInferencer splits inputs into overlapping tiles, runs batched inference, and blends overlapping borders with a separable raised-cosine (Hann) weight mask:

python
from sorasr.export import OnnxTiledRunner

runner = OnnxTiledRunner(
    onnx_path="artifacts/sorasr_s_x4.onnx",
    providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
    scale=4,
    tile_size=256,
    tile_overlap=16,
    batch_size=4,
)

sr_rgb = runner.run(lr_rgb_uint8)

Selecting Tile Parameters ​

Tile SizeOverlapPeak VRAM (FP16, SoraSR-S)Relative Overhead
128 × 12816 px~0.7 GB+28% border compute
256 × 25616 px~1.4 GB+13% border compute
512 × 51224 px~3.2 GB+9% border compute

4. Production Serving Checklist ​

  • Warm up the TensorRT / CUDA Execution Provider with 3–5 dummy tiles during worker startup to avoid first-request compilation latency.
  • Pin host input/output buffers (cudaHostAlloc) when serving high-FPS video streams to overlap H2D/D2H transfers with kernel execution.
  • Enforce strict request dimension caps and timeout budgets at the API gateway before enqueueing GPU work.

Released under the Apache-2.0 License.