Type conversion in Libtorch for C++

Hi all,
I am trying to do inference for my model in C++ using Libtorch.
Following is the code which I am using.

        torch::Tensor inputs = torch::zeros({in_1.size()*2, 1, 2}, torch::dtype(torch::kFloat32));
        int i = 0;
        const cellList &cells = mesh.cells();
        forAll(cells, celli)
        {
            inputs[i][0][0] = in_1[celli];
            inputs[i][0][1] = in_2[celli];
	    i = i + 1;
        }

	    at::Tensor output_real = torch::zeros({in_1.size()*2,1,7}, torch::dtype(torch::kFloat32));
	    std::vector<torch::jit::IValue> input;

	    //* * * * * * * Inference * * * * * * * *//

	    for(int j = 0; j<i; j++)
		{
			input.push_back(inputs[j]);
	        output_real[j] = module->forward(input).toTensor();
	        input.clear();
		}
	    std::cout<<"Inference has been completed"<<'\n';
	
	    int j = 0;
	
	    forAll(cells, celli)
		{
			if(j != i){
				out_1[celli] = output_real[j][0][celli * 7];
				out_2[celli] = output_real[j][0][celli * 7 + 1];
				out_3[celli] = output_real[j][0][celli * 7 + 2];
				out_4[celli] = output_real[j][0][celli * 7 + 3];
				out_5[celli] = output_real[j][0][celli * 7 + 4];
				out_6[celli] = output_real[j][0][celli * 7 + 5];
				out_7[celli] = output_real[j][0][celli * 7 + 6];
			}
			j = j+1;
        }

in this code neural network’s input is 1x2 dim and output is 1x7.
Now the problem is in the last loop it gives error of type conversion which is as follows:

inferFoam.C: In function ‘int main(int, char**)’:
inferFoam.C:95:46: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
    out_1[celli] = output_real[j][0][celli * 7];
                                              ^
inferFoam.C:96:59: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
             out_2[celli] = output_real[j][0][celli * 7 + 1];
                                                           ^
inferFoam.C:97:59: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
             out_3[celli] = output_real[j][0][celli * 7 + 2];
                                                           ^
inferFoam.C:98:59: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
             out_4[celli] = output_real[j][0][celli * 7 + 3];
                                                           ^
inferFoam.C:99:50: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
    out_5[celli] = output_real[j][0][celli * 7 + 4];
                                                  ^
inferFoam.C:100:56: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
          out_6[celli] = output_real[j][0][celli * 7 + 5];
                                                        ^
inferFoam.C:101:56: error: cannot convert ‘at::Tensor’ to ‘double’ in assignment
          out_7[celli] = output_real[j][0][celli * 7 + 6];
                                                        ^

I tried explicit conversion like (double)output_real[j][0][celli * 7 + 5] but it doesn’t seem to work and says:

error: invalid cast from type ‘at::Tensor’ to type ‘double’
      std::cout << (double)output[i][0][1] << '\n';
                                         ^

Can anybody please tell me what’s the solution to this problem is?

Secondly, I am confused that, earlier in the first loop when I am reading the input values from in_1[celli] and in_2[celli] into inputs it doesn’t give this kind of (type conversion) error there even though there also I am doing the same thing; exchanging two different datatypes.

Can anybody tell me please what’s seems to the problem, what is my mistake and also please suggest a solution.

Regards.

1 Like

torch::Double can’t be casted to double directly, you should use output_tensor[i][j][k].item().to<double>(). It first converts a Tensor with a single value to a ScalarType then to a double.

3 Likes

Thank you so much, it works :blush:.

Hi,
Say we have two tensor priors, and loc_cat,
I want to set the type of priors same as loc_cat,
in python, we can use the following code:

priors.type(loc_cat.data.type)

So how to do that in cpp libtorch?
Thanks.

I was looking for exactly the same. For anyone looking at this in future, here is how you can cast one tensor with dtype of another in C++:

priors.type_as(loc_cat)