Skip to content

Commit

Permalink
Change the C++ example.
Browse files Browse the repository at this point in the history
  • Loading branch information
datumbox committed Sep 6, 2021
1 parent 5f7c50a commit 9c88a2b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
45 changes: 32 additions & 13 deletions examples/cpp/hello_world/main.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
#include <iostream>
#include <torch/script.h>
#include <torch/torch.h>
#include <torchvision/vision.h>
#include <torchvision/models/resnet.h>

int main()
{
auto model = vision::models::ResNet18();
model->eval();
int main() {
torch::DeviceType device_type;
device_type = torch::kCPU;

// Create a random input tensor and run it through the model.
auto in = torch::rand({1, 3, 10, 10});
auto out = model->forward(in);
torch::jit::script::Module model;
try {
std::cout << "Loading model\n";
// Deserialize the ScriptModule from a file using torch::jit::load().
model = torch::jit::load("resnet18.pt");
std::cout << "Model loaded\n";
} catch (const torch::Error& e) {
std::cout << "error loading the model\n";
return -1;
} catch (const std::exception& e) {
std::cout << "Other error: " << e.what() << "\n";
return -1;
}

std::cout << out.sizes();
// TorchScript models require a List[IValue] as input
std::vector<torch::jit::IValue> inputs;

// Create a random input tensor and run it through the model.
inputs.push_back(torch::rand({1, 3, 10, 10}));
auto out = model.forward(inputs);
std::cout << out << "\n";

if (torch::cuda::is_available()) {
// Move model and inputs to GPU
model->to(torch::kCUDA);
auto gpu_in = in.to(torch::kCUDA);
auto gpu_out = model->forward(gpu_in);
model.to(torch::kCUDA);

// Add GPU inputs
inputs.clear();
torch::TensorOptions options = torch::TensorOptions{torch::kCUDA};
inputs.push_back(torch::rand({1, 3, 10, 10}, options));

std::cout << gpu_out.sizes();
auto gpu_out = model.forward(inputs);
std::cout << gpu_out << "\n";
}
}
13 changes: 13 additions & 0 deletions examples/cpp/hello_world/trace_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os.path as osp

import torch
import torchvision

HERE = osp.dirname(osp.abspath(__file__))
ASSETS = osp.dirname(osp.dirname(HERE))

model = torchvision.models.resnet18(pretrained=False)
model.eval()

traced_model = torch.jit.script(model)
traced_model.save("resnet18.pt")
7 changes: 6 additions & 1 deletion packaging/build_cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,18 @@ fi
# Compile and run the CPP example
popd
cd examples/cpp/hello_world

mkdir build

# Trace model
python trace_model.py
cp resnet18.pt build

cd build
cmake .. -DTorch_DIR=$TORCH_PATH/share/cmake/Torch

if [[ "$OSTYPE" == "msys" ]]; then
"$script_dir/windows/internal/vc_env_helper.bat" "$script_dir/windows/internal/build_cpp_example.bat" $PARALLELISM
mv resnet18.pt Release
cd Release
else
make -j$PARALLELISM
Expand Down

0 comments on commit 9c88a2b

Please sign in to comment.