Question about c++ and libtorch error

#include <torch/script.h> // One-stop header.

#include <iostream>
#include <memory>
const int64_t kNoiseSize = 128;


const int64_t kBatchSize = 64;

using namespace torch;
int main() {
torch::manual_seed(1);

//auto options = torch::TensorOptions().device(torch::kCUDA,0);

//torch::Tensor z_save = torch::randn({kBatchSize, kNoiseSize, 1, 1},options);
torch::Tensor z_save = torch::randn({kBatchSize, kNoiseSize-10, 1, 1});
z_save = z_save.to(torch::kCUDA);


int data[] ={1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,9,3,5};
torch::Tensor f = torch::from_blob(data, {64},torch::kInt64);
f = f.to(torch::kCUDA);


std::shared_ptr<torch::jit::script::Module> module = torch::jit::load("/home/np9207/c_pytorch/example-app/build/model.pt");


module->to(torch::kCUDA);

at::Tensor output = module->forward({z_save,f}).toTensor();


std::cout<<f<<std::endl;
    


std::cout << "ok\n";
return 0;
}



The above code is that I am using, It requires two inputs one is random generator, and the other one is labels for the MNIST data to generate.

I am getting device asserted error.

/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo<T, IndexType>, TensorInfo<T, IndexType>, TensorInfo<long, IndexType>, int, int, IndexType, IndexType, long) [with T = float, IndexType = unsigned int, DstDim = 2, SrcDim = 2, IdxDim = -2, IndexIsMajor = true]: block: [4,0,0], thread: [0,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo<T, IndexType>, TensorInfo<T, IndexType>, TensorInfo<long, IndexType>, int, int, IndexType, IndexType, long) [with T = float, IndexType = unsigned int, DstDim = 2, SrcDim = 2, IdxDim = -2, IndexIsMajor = true]: block: [4,0,0], thread: [1,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo<T, IndexType>, TensorInfo<T, IndexType>, TensorInfo<long, IndexType>, int, int, IndexType, IndexType, long) [with T = float, IndexType = unsigned int, DstDim = 2, SrcDim = 2, IdxDim = -2, IndexIsMajor = true]: block: [4,0,0], thread: [2,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo<T, IndexType>, TensorInfo<T, IndexType>, TensorInfo<long, IndexType>, int, int, IndexType, IndexType, long) [with T = float, IndexType = unsigned int, DstDim = 2, SrcDim = 2, IdxDim = -2, IndexIsMajor = true]: block: [4,0,0], thread: [3,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo<T, IndexType>, TensorInfo<T, IndexType>, TensorInfo<long, IndexType>, int, int, IndexType, IndexType, long) [with T = float, IndexType = unsigned int, DstDim = 2, SrcDim = 2, IdxDim = -2, IndexIsMajor = true]: block: [4,0,0], thread: [4,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo<T, IndexType>, TensorInfo<T, IndexType>, TensorInfo<long, IndexType>, int, int, IndexType, IndexType, long) [with T = float, IndexType = unsigned int, DstDim = 2, SrcDim = 2, IdxDim = -2, IndexIsMajor = true]: block: [4,0,0], thread: [5,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo<T, IndexType>, TensorInfo<T, IndexType>, TensorInfo<long, IndexType>, int, int, IndexType, IndexType, long) [with T = float, IndexType = unsigned int, DstDim = 2, SrcDim = 2, IdxDim = -2, IndexIsMajor = true]: block: [4,0,0], thread: [6,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo<T, IndexType>, TensorInfo<T, IndexType>, TensorInfo<long, IndexType>, int, int, IndexType, IndexType, long) [with T = float, IndexType = unsigned int, DstDim = 2, SrcDim = 2, IdxDim = -2, IndexIsMajor = true]: block: [4,0,0], thread: [7,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
terminate called after throwing an instance of 'std::runtime_error'
  what():  /pytorch/torch/csrc/jit/fuser/cuda/fused_kernel.cpp:137: device-side assert triggered
Aborted (core dumped)


maybe the wrong data type, int64 and float or something

int data[] ={1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,9,3,5};
torch::Tensor f = torch::from_blob(data, {64},torch::kInt64);

This needs to be long format. Another thing, when I display f, I don’t see {1,2,3,…} but something else like {12000,14000…}

Should data be an array of int64_t?
As is you seem to have a mismatch between the C++ array and the data type in from_blob. That would lead to funny values and might cause the device assert.

Best regards

Thomas

2 Likes
int64_t array[] = {1, 2};
torch::Tensor tensor = torch::from_blob(array, {2}, torch::kInt64);
std::cout << tensor << std::endl;

So that solves your data problem. But I still find it strange, that you try to feed a float through your net and expect it to output an int. I dont know your architecture, but it maybe worth checking that as well.

1 Like

Why int array won’t work, but int64_t works?

Int(32) works, too, if you set the dtype appropriately.

Besteht regards

Thomas

Thanks. I mean, why int type won’t work, but int_32_t or int_64_t works?

So int may vary in size depending on the architecture and so int32_t is more precise/portable. It’s more a general C/C++ thing than PyTorch specific.