I am working on a cpp extension for pytorch, and followed the official tutorial, using libtorch and cmake to compile the program. But I met the problem of creating tensor. This code can work.
#include <torch/torch.h>
#include <iostream>
int main(){
std::vector<int64_t> test_data = {1, 2, 3, 4, 5, 6, 7, 8, 9};
torch::Tensor s = torch::from_blob(test_data.data(), {3, 3}, torch::kInt64);
std::cout << "test case pass" << std::endl;
}
But this code can't work.
int main(){
auto option = torch::TensorOptions().dtype(torch::KInt16);
auto b = torch::zeros({2,3}, option);
std::cout << "test case pass" << std::endl;
}
and the compile error log is here.
error: no member named 'KInt16' in namespace 'torch'; did you mean 'kInt16'?
As explicitly stated by the error message, you made a typo : you should have written
torch::kInt16
instead of torch::KInt16
. The k
should not be capitalized.