Question about c++ and libtorch error

Nilesh_Pandey1: <pre><code class="lang-auto">#include &lt;torch/script.h&gt; // One-stop header.

#include &lt;iostream&gt;
#include &lt;memory&gt;
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&lt;torch::jit::script::Module&gt; module = torch::jit::load("/home/np9207/c_pytorch/example-app/build/model.pt");


module-&gt;to(torch::kCUDA);

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


std::cout&lt;&lt;f&lt;&lt;std::endl;
    


std::cout &lt;&lt; "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&lt;T, IndexType&gt;, TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;long, IndexType&gt;, 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 &lt; srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;long, IndexType&gt;, 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 &lt; srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;long, IndexType&gt;, 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 &lt; srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;long, IndexType&gt;, 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 &lt; srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;long, IndexType&gt;, 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 &lt; srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;long, IndexType&gt;, 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 &lt; srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;long, IndexType&gt;, 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 &lt; srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:362: void indexSelectLargeIndex(TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;T, IndexType&gt;, TensorInfo&lt;long, IndexType&gt;, 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 &lt; 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)


</code></pre>

mhubii: <p>maybe the wrong data type, int64 and float or something</p>

Nilesh_Pandey1: <pre><code class="lang-auto">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);
</code></pre>
<p>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…}</p>

tom: <p>Should data be an array of <code>int64_t</code>?<br>
As is you seem to have a mismatch between the C++ array and the data type in <code>from_blob</code>. That would lead to funny values and might cause the device assert.</p>
<p>Best regards</p>
<p>Thomas</p>

mhubii: <pre><code class="lang-auto">int64_t array[] = {1, 2};
torch::Tensor tensor = torch::from_blob(array, {2}, torch::kInt64);
std::cout &lt;&lt; tensor &lt;&lt; std::endl;
</code></pre>
<p>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.</p>

marchss: <p>Why <code>int</code> array won’t work, but <code>int64_t</code> works?</p>

tom: <p>Int(32) works, too, if you set the dtype appropriately.</p>
<p>Besteht regards</p>
<p>Thomas</p>

marchss: <p>Thanks. I mean, why int type won’t work, but int_32_t or int_64_t works?</p>

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