Files
pytorch/test/mobile/custom_build/predictor.cpp
Ailing Zhang 24c904951c Replace AutoNonVariableTypeMode with InferenceMode in fbcode. (#55114)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/55114

Test Plan: CI

Reviewed By: ezyang, bhosmer

Differential Revision: D27472768

fbshipit-source-id: 76f17ef7de40f6e04e2968f8958027b5f93e1c0c
2021-04-02 11:45:53 -07:00

48 lines
1.2 KiB
C++

// This is a simple predictor binary that loads a TorchScript CV model and runs
// a forward pass with fixed input `torch::ones({1, 3, 224, 224})`.
// It's used for end-to-end integration test for custom mobile build.
#include <iostream>
#include <string>
#include <torch/script.h>
using namespace std;
namespace {
struct MobileCallGuard {
// Set InferenceMode for inference only use case.
c10::InferenceMode guard;
// Disable graph optimizer to ensure list of unused ops are not changed for
// custom mobile build.
torch::jit::GraphOptimizerEnabledGuard no_optimizer_guard{false};
};
torch::jit::Module loadModel(const std::string& path) {
MobileCallGuard guard;
auto module = torch::jit::load(path);
module.eval();
return module;
}
} // namespace
int main(int argc, const char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <model_path>\n";
return 1;
}
auto module = loadModel(argv[1]);
auto input = torch::ones({1, 3, 224, 224});
auto output = [&]() {
MobileCallGuard guard;
return module.forward({input}).toTensor();
}();
std::cout << std::setprecision(3) << std::fixed;
for (int i = 0; i < 5; i++) {
std::cout << output.data_ptr<float>()[i] << std::endl;
}
return 0;
}