Strange issue returning torch::Tensors in libtorch/C++

I am trying to convert the version of poseNet code form python to c++.

https://github.com/rwightman/posenet-pytorch

I created two torch::Tensor objects and return them in a function and get them in another function. But the return of one tensor succeed while another failed( [Tensor (undefined)] ) .

does it because the local area of C++? but I can’t make sure of that, because both tensor was constructed in called function, and even I add “static” before where I construct “scores_vec”.

So Would anyone help me figure out what cause that?
And is there a way to get both returned tensor successfully?
Thanks!

Here is the C++ code and commented python code for easy compare?

torch::Tensor build_part_with_score_torch(float score_threshold, int local_max_radius, torch::Tensor &scores)
{
   // lmd = 2 * local_max_radius + 1
    int   lmd = 2 * local_max_radius + 1;

   // max_vals = F.max_pool2d(scores, lmd, stride=1, padding=1)
   torch::Tensor   max_vals = torch::max_pool2d(scores, lmd, 1, 1,1,false);
  // max_loc = (scores == max_vals) & (scores >= score_threshold)
   torch::Tensor   max_loc = torch::__and__((scores == max_vals),  (scores >= score_threshold));
 
 // max_loc_idx = max_loc.nonzero()
   torch::Tensor  max_loc_idx = max_loc.nonzero();

   // scores_vec = scores[max_loc];  
   torch::Tensor scores_vec =  torch::randn(max_loc_idx.sizes()[0]);
   for(int i=0; i<max_loc_idx.sizes()[0];i++){
       auto dim1 = max_loc_idx[i][0];
       auto dim2 = max_loc_idx[i][1];
       auto dim3 = max_loc_idx[i][2];
       scores_vec[i] = scores[dim1][dim2][dim3];
    }      
   cout<<"scores_vec"<<endl<<scores_vec<<endl;
   //sort_idx = torch.argsort(scores_vec, descending=True)
   torch::Tensor  sort_idx = torch::argsort(scores_vec,-1, true);
 
 
 //return scores_vec[sort_idx], max_loc_idx[sort_idx]
   torch::Tensor sorted_scores_vec = torch::index_select(scores_vec,0,sort_idx);
   torch::Tensor sorted_max_loc_idx  = torch::index_select(max_loc_idx,0,sort_idx);
   cout<<"sorted_scores_vec"<<endl<<sorted_scores_vec<<endl;
   cout<<"sorted_max_loc_idx"<<endl<<sorted_max_loc_idx<<endl;
  return sorted_scores_vec,sorted_max_loc_idx;

}


torch::Tensor decode_multiple_poses(torch::Tensor& scores,torch::Tensor& offsets,torch::Tensor& displacements_fwd,torch::Tensor& displacements_bwd,int max_pose_detections=10,float score_threshold=0.5,int nms_radius=20, float min_pose_score=0.5)
{
  
   torch::Tensor  part_scores, part_idx = build_part_with_score_torch(score_threshold, LOCAL_MAXIMUM_RADIUS, scores); 
  cout<<"part_scores"<<endl<<part_scores<<endl<<"part_idx"<<endl<<part_idx<<endl;
}

here is the output of the code

scores_vec
 0.9944
 0.9816
 0.9857
 0.6776
 0.8208
 0.9903
 0.9785
 0.8932
 0.9345
 0.8307
 0.6399
 0.5537
[ Variable[CPUFloatType]{12} ]
sorted_scores_vec
 0.9944
 0.9903
 0.9857
 0.9816
 0.9785
 0.9345
 0.8932
 0.8307
 0.8208
 0.6776
 0.6399
 0.5537
[ Variable[CPUFloatType]{12} ]
sorted_max_loc_idx
  0   2   6
  5   5   8
  2   1   6
  1   2   7
  6   5   3
  8   8   2
  7  10   9
  9  10   8
  4   2   5
  3   3   7
 10  10   1
 12  10   3
[ Variable[CPULongType]{12,3} ]
part_scores
[ Tensor (undefined) ]
part_idx
  0   2   6
  5   5   8
  2   1   6
  1   2   7
  6   5   3
  8   8   2
  7  10   9
  9  10   8
  4   2   5
  3   3   7
 10  10   1
 12  10   3
[ Variable[CPULongType]{12,3} ]
ok
Segmentation fault (core dumped)

Maybe you can use vector or tuple?
Can you tell me your libtorch version?
I have the error in my libtorch version.
error: ‘class at::Tensor’ has no member named ‘argsort’

Thk you

Your function is returning two tensors and not one. To return multiple tensors in C++ return tuple of tensors as follows

// c++14
std::tuple<torch::Tensor, torch::Tensor> foo(const torch::Tensor& a, const torch::Tensor& b)
{
  torch::Tensor op1 = a + b / a - b;
  torch::Tensor op2 = torch::randn({1}) * op1;

  return std::make_tuple(op1, op2); 
}

// On the calling side
torch::Tensor tensor1, tensor2;
std::tie(tensor1, tensor2) = foo(torch::randn({3, 2, 2}), torch::randn({3, 2, 2}));