Hi Team,
I have a very simple fizbuz model made using pytorch python APIs which I have exported as ScriptModule. I am loading the same module from python and CPP and passing same input but getting wrong output in CPP. In fact, regardless of what ever input I pass, I get the exactly same values in the output from CPP
Here is my python and CPP code for the same
PS: I am a CPP noob
# ~/myp/HOD/8.P/FizBuzTorchScript> python fizbuz.py fizbuz_model.pt 2
import sys
import torch
def main():
net = torch.jit.load(sys.argv[1])
temp = [int(i) for i in '{0:b}'.format(int(sys.argv[2]))]
array = [0] * (10 - len(temp)) + temp
inputs = torch.Tensor([array])
print(inputs) # tensor([[0., 0., 0., 0., 0., 0., 0., 0., 1., 0.]])
output = net(inputs)
print(output) # tensor([[ -1.8873, -17.1001, -3.7774, 3.7985]], ...
if __name__ == '__main__':
main()
// ~/myp/HOD/8.P/FizBuzTorchScript/build> ./fizbuz ../fizbuz_model.pt 2
#include <torch/script.h>
#include <iostream>
#include <memory>
#include <string>
int main(int argc, const char* argv[]) {
if (argc != 3) {
std::cerr << "usage: <appname> <path> <int>\n";
return -1;
}
std::string arg = argv[2];
int x = std::stoi(arg);
int array[10];
int i;
int j = 9;
for (i = 0; i < 10; ++i) {
array[j] = (x >> i) & 1;
j--;
}
std::shared_ptr<torch::jit::script::Module> module = torch::jit::load(argv[1]);
torch::Tensor tensor_in = torch::from_blob(array, {1, 10});
std::vector<torch::jit::IValue> inputs;
inputs.push_back(tensor_in);
std::cout << inputs << '\n';
/*
1e-45 *
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1.4013 0.0000
[ Variable[CPUFloatType]{1,10} ]
*/
at::Tensor output = module->forward(inputs).toTensor();
std::cout << output << '\n';
/*
3.7295 -23.8977 -8.2652 -1.3901
[ Variable[CPUFloatType]{1,4} ]
*/
}