PAI-Blade accelerates PyTorch model inference by applying hardware-specific optimizations through a single Python call. This guide walks through a complete optimization workflow — from loading a model to benchmarking results — using a ResNet50 model on NVIDIA Tesla T4 GPUs as an example.
What you'll learn:
How to convert a PyTorch model to ScriptModule format for PAI-Blade
How to call
blade.optimizeand interpret its parameters and return valuesHow to read the optimization report and measure inference speedup
Prerequisites
Before you begin, ensure that you have:
PyTorch and the PAI-Blade wheel packages installed. See Install PAI-Blade
A trained PyTorch model. This guide uses the public ResNet50 model from torchvision
Optimize a PyTorch model
Step 1: Import dependencies
import os
import time
import torch
import torchvision.models as models
import bladeStep 2: Load and convert the model
PAI-Blade only accepts torch.jit.ScriptModule objects — it does not work with standard nn.Module models. Convert your model using torch.jit.script before passing it to PAI-Blade.
model = models.resnet50().float().cuda() # Load the ResNet50 model.
model = torch.jit.script(model).eval() # Convert to ScriptModule.
dummy = torch.rand(1, 3, 224, 224).cuda() # Create test input.Step 3: Run optimization
Call blade.optimize to optimize the model. The method accepts the following key parameters:
| Parameter | Description | Value in this example |
|---|---|---|
model | The model to optimize | model (in-memory ScriptModule) |
'o1' | Optimization level. Valid values: o1 and o2 | 'o1' |
device_type | The device type for inference. Valid values: gpu, cpu | 'gpu' |
test_data | Representative input tensors. Provide a list of tuples — each tuple is one set of inputs | [(dummy,)] |
For the full parameter reference, see Python method.
optimized_model, opt_spec, report = blade.optimize(
model, # The model to optimize.
'o1', # Optimization level.
device_type='gpu', # Target device type.
test_data=[(dummy,)], # Test input data.
)blade.optimize returns three objects:
`optimized_model`: The optimized model as a
torch.jit.ScriptModuleobject, ready for inference.`opt_spec`: The external dependencies (configuration, environment variables, resource files) needed to reproduce the optimization. Use a
withstatement in Python to activate these dependencies.`report`: The optimization report, which you can print directly. See Optimization report for details on each field.
Optimization takes time. PAI-Blade runs multiple analysis phases before producing the optimized model. Progress is shown as optimization runs:
[Progress] 5%, phase: user_test_data_validation.
[Progress] 10%, phase: test_data_deduction.
[Progress] 15%, phase: CombinedSwitch_4.
[Progress] 95%, phase: model_collecting.This compilation is a one-time cost. Once optimization completes, inference with optimized_model is consistently faster than the original model.
Step 4: Review the optimization report
print("Report: {}".format(report))The report shows which optimizations were applied and their effect on latency. A sample report:
{
"optimizations": [
{
"name": "PtTrtPassFp32",
"status": "effective",
"speedup": "1.50", // Speedup ratio for this optimization.
"pre_run": "5.29 ms", // Latency before optimization.
"post_run": "3.54 ms" // Latency after optimization.
}
],
"overall": {
"baseline": "5.30 ms", // Original model latency.
"optimized": "3.59 ms", // Optimized model latency.
"speedup": "1.48" // End-to-end speedup ratio.
}
}In this example, the end-to-end speedup is 1.48x — the optimized model runs in 3.59 ms versus 5.30 ms for the original. Check "status": "effective" to identify which optimization passes had a measurable impact.
Step 5: Benchmark inference performance
Use the following function to measure steady-state latency for both the original and optimized models. The first 100 runs warm up the GPU; the subsequent 200 runs are timed.
@torch.no_grad()
def benchmark(model, inp):
for i in range(100): # Warm up.
model(inp)
start = time.time()
for i in range(200): # Timed runs.
model(inp)
elapsed_ms = (time.time() - start) * 1000
print("Latency: {:.2f}".format(elapsed_ms / 200))
benchmark(model, dummy) # Original model.
benchmark(optimized_model, dummy) # Optimized model.Load a model from a file
In the example above, the model is passed as an in-memory torch.jit.ScriptModule. You can also pass a file path to a model saved with torch.jit.save:
optimized_model, opt_spec, report = blade.optimize(
'path/to/torch_model.pt', # Path to a saved ScriptModule file.
'o1',
device_type='gpu',
)What's next
After optimization, you have three options for running the model:
Python inference: Run
optimized_modeldirectly in Python using the same API as anytorch.jit.ScriptModule.Deploy as a service: Deploy the optimized model to Elastic Algorithm Service (EAS) in Machine Learning Platform for AI (PAI) for production-scale serving.
C++ integration: Use the PAI-Blade C++ SDK to embed the optimized model in your own application.
For deployment instructions, see Use an SDK to deploy a PyTorch model for inference.
Note: If you have questions during optimization, join the DingTalk group for PAI-Blade users to reach technical support. To get access, see Obtain an access token.