I’m using libtorch 1.4.0 on my MSI P65 laptop (Win10, RTX 2060, NV driver 441.22, Cuda 26.21.14.4122).
When using the CPU, this code works as expected:
auto tensor1=torch::zeros({1, 1, 2, 2},torch::kCPU);
tensor1[0][0][0][0]=42;
cout << "tensor1:" << endl << tensor1 << endl << endl;

float tensor2Data[]{42,0,0,0};
auto tensor2=torch::from_blob(tensor2Data,{1, 1, 2, 2},torch::kCPU);
cout << "tensor2:" << endl << tensor2 << endl << endl;

And it shows:

tensor1:
(1,1,.,.) =
42   0
0   0
[ CPUFloatType{1,1,2,2} ]
tensor2:
(1,1,.,.) =
42   0
0   0
[ CPUFloatType{1,1,2,2} ]
Press  to close this window…

However when switching to CUDA, with this code:
auto tensor1=torch::zeros({1, 1, 2, 2},torch::kCUDA);
tensor1[0][0][0][0]=42;
cout << "tensor1:" << endl << tensor1 << endl << endl;

float tensor2Data[]{42,0,0,0};
auto tensor2=torch::from_blob(tensor2Data,{1, 1, 2, 2},torch::kCUDA);
cout << "tensor2:" << endl << tensor2 << endl << endl;

It crash at from_blob without any error message:

tensor1:
(1,1,.,.) =
42   0
0   0
[ CUDAFloatType{1,1,2,2} ]
Press  to close this window…

Running a model on CUDA works - it’s only by creating a CUDA tensor with from_blob that I get this crash, without any hint or explanation.
Any idea what’s going wrong ? Is there a workaround I could use to easily transfer blocks of data from the RAM to a CUDA tensor ?
Also, I tried creating the tensors CPU-side (first code) and then do a tensor.to(torch::kCUDA) but it didn’t transfer the tensors to CUDA.
I could also transfer values one by one to a CUDA tensor using [][][][] like with tensor1, but it takes a really long time.