Sample scripts for testing PyTorch models in C++. Both CPU & GPU implementations are provided.
Author: Muralikrishnan Gopalakrishnan Meena (Oak Ridge National Laboratory), https://sites.google.com/view/muraligm/
Contributors:
- Murali Gopalakrishnan Meena
- Matt Norman (Oak Ridge National Laboratory), https://mrnorman.github.io/
These are sample scripts to load a PyTorch model and run a forward step. These are adapted from PyTorch's docs for loading a Torch Script model in C++. They also have instructions in the doc to convert a PyTorch model to torch script in Python.
There are 2 examples:
- CPU implementation
example_pytorchNN_cpu.cpp
- GPU implementation
example_pytorchNN_gpu.cpp
For more details on the PyTorch C++ frontend API used in these scripts, refer to torch_interface.h
.
The sample model used here, PyTorch_model_Cpp.pt
, is for a fluid flow emulator which takes 36 inputs and gives 4 outputs.
- Go to the build folder
cd build
- Activate binaries using environment file corresponding to your machine
Example: For running on GPU on Summit
source <machineName>_<cpu/gpu>.env
source summit_gpu.env
- Compile:
./cmakescript make -j
- Run:
./example
- Sample output for the GPU usage (
example_pytorchNN_gpu.cpp
)Model loaded succefully! Number of CUDA devices: 1 Sample output (scaled flow properties): -0.95627 4.08107 -2.88555 -0.970117