Is there a way to read .pt files in C++?


(Joong Kun Lee) #1

Is there a way to read .pt files in C++ without a python wrapper?


(Nilay Shrivastava) #2

Yes there is

int main(int argc, const char* argv[])
{
    if(argc != 2) {
        std::cerr << "path to the model not mentioned!!";
        return -1;
    }

    std::shared_ptr<torch::jit::script::Module> module = torch::jit::load(argv[1]);

    std::vector<torch::jit::IValue> inputs;
    inputs.push_back(torch::ones({1,1,29,22,22}));

    for (int i=0; i<COUNT; ++i) {
        auto output = at::argmax(module->forward(inputs).toTensor());
    }
    
 
    return 0;
}

(Joong Kun Lee) #5

Thanks for your helpful answer.

  1. Could you share a link to the C++ torch::jit documentation? I can’t find it on the pytorch website.
  2. is this supported for pytorch 1.0 as well?
  3. what would you have to import?

(Will Feng) #6

@0xFFFFFFFF It is supported for PyTorch 1.0, and this tutorial https://pytorch.org/tutorials/advanced/cpp_export.html best explains how to load a PyTorch model in C++.