You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for torch.export ExportedProgram models (#1498)
Implements functionality to load and execute PyTorch models exported via
torch.export (.pt2 files), enabling .NET applications to run ExportedProgram
models as the PyTorch ecosystem transitions from ONNX to torch.export.
## Implementation
### Native Layer
- Add THSExport.h and THSExport.cpp C++ wrappers for AOTIModelPackageLoader API
- Update Utils.h to include torch/csrc/inductor/aoti_package/model_package_loader.h
- Upgrade to LibTorch 2.9.0 which includes AOTIModelPackageLoader symbols
### Managed Layer
- Add LibTorchSharp.THSExport.cs with PInvoke declarations
- Implement ExportedProgram and ExportedProgram<TResult> classes in Export namespace
- Provide torch.export.load() API following PyTorch conventions
### Features
- Load .pt2 ExportedProgram files compiled with torch._inductor.aoti_compile_and_package()
- Execute inference-only forward pass with type-safe generics
- Support for single tensor, array, and tuple (up to 3 elements) outputs
- Proper IDisposable implementation for resource cleanup
### Testing
- Add TestExport.cs with 7 comprehensive unit tests (all passing)
- Include 6 test .pt2 models covering various scenarios:
- Simple linear model
- Linear + ReLU
- Multiple inputs
- Tuple and list outputs
- Sequential models
- Add generate_export_models.py for regenerating test models
## Technical Details
The implementation uses torch::inductor::AOTIModelPackageLoader from LibTorch 2.9+
for AOTInductor-compiled models, providing 30-40% better latency than TorchScript.
Models are inference-only and compiled for specific device (CPU/CUDA) at build time.
Note: .pt2 files from torch.export.save() are Python-only and not supported.
Only .pt2 files from torch._inductor.aoti_compile_and_package() work in C++.
Fixes#1498
0 commit comments