Skip to content

Add PyTorch TorchScript backend for vision benchmarks#2578

Open
amd-arsuresh wants to merge 1 commit into
mlcommons:masterfrom
amd-arsuresh:add-pytorch-torchscript-backend
Open

Add PyTorch TorchScript backend for vision benchmarks#2578
amd-arsuresh wants to merge 1 commit into
mlcommons:masterfrom
amd-arsuresh:add-pytorch-torchscript-backend

Conversation

@amd-arsuresh

Copy link
Copy Markdown

Summary

Add a new pytorch-torchscript backend for the vision classification and detection benchmarks that loads TorchScript models exported via torch.jit.trace or torch.jit.script.

Problem

The existing resnet50-pytorch profile in SUPPORTED_PROFILES incorrectly sets "backend": "tensorflow", making it non-functional. The pytorch-native backend uses torch.load() and then tries to access ONNX graph attributes (model.graph.initializer, model.graph.input), which fails on TorchScript models; and the pytorch backend uses caffe2/ONNX, requiring an ONNX model file rather than a native PyTorch format.

There is no backend that can load standard TorchScript .pt files (the most common way to serialize PyTorch models for deployment).

Changes

  • New file: vision/classification_and_detection/python/backend_pytorch_torchscript.py
    • BackendPytorchTorchScript class that uses torch.jit.load() to load TorchScript models
    • Supports both single-tensor and tuple/list outputs
    • Automatic CPU/CUDA device selection
  • Modified: vision/classification_and_detection/python/main.py
    • Register pytorch-torchscript in get_backend()
    • Fix resnet50-pytorch profile: change backend from tensorflow to pytorch-torchscript

Usage

# Export a TorchScript model
python -c "
import torch, torchvision
model = torchvision.models.resnet50(pretrained=True).eval()
traced = torch.jit.trace(model, torch.randn(1, 3, 224, 224))
traced.save('resnet50_torchscript.pt')
"

# Run with the new backend
cd vision/classification_and_detection
./run_local.sh pytorch-torchscript resnet50 cpu --scenario Offline

Testing

Tested with resnet50 TorchScript model on CPU - loads and runs inference correctly.

Add a new backend (pytorch-torchscript) that loads TorchScript models
exported via torch.jit.trace or torch.jit.script. This enables running
inference with pre-traced PyTorch models (.pt files) without requiring
ONNX conversion.

Changes:
- Add backend_pytorch_torchscript.py with BackendPytorchTorchScript class
- Register the new backend in main.py get_backend()
- Fix resnet50-pytorch profile: change backend from 'tensorflow' (incorrect)
  to 'pytorch-torchscript'
@amd-arsuresh amd-arsuresh requested a review from a team as a code owner April 20, 2026 22:38
@github-actions

Copy link
Copy Markdown
Contributor

MLCommons CLA bot All contributors have signed the MLCommons CLA ✍️ ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant