Error with pin_memory()

I have implemented a sequence-to-sequence model in PyTorch (v. 0.3.0post4) [Ubuntu 16.04] using Recurrent Neural Networks. I am trying to use pin_memory() to speed up the training as follows:

input_tensor = torch.LongTensor(...))
target_var = Variable(torch.LongTensor(...))

    input_var = input_tensor.pin_memory()
    target_var = target_var.cuda(async=True)

But during training, I get the following error:

  File "", line 224, in batch_pairs
    target_var = target_var.cuda(async=True)
  File "/usr/local/lib/python3.5/dist-packages/torch/autograd/", line 298, in cuda
    return CudaTransfer.apply(self, device, async)
  File "/usr/local/lib/python3.5/dist-packages/torch/autograd/_functions/", line 201, in forward
    return i.cuda(async=async)
  File "/usr/local/lib/python3.5/dist-packages/torch/", line 69, in _cuda
    return new_type(self.size()).copy_(self, async)
RuntimeError: invalid argument 3: Source tensor must be contiguous at /pytorch/torch/lib/THC/generic/THCTensorCopy.c:114

I have no idea what could be causing this. Any tips would be appreciated. Thanks!

It seems that moving target_var to cuda complains that some tensor isn’t contiguous.

Try this

target_var = target_var.contiguous().cuda(async=True)

Thanks for the suggestion. Doing that gives me the following error:

  File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/", line 325, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/", line 103, in forward
    self.scale_grad_by_freq, self.sparse
  File "/usr/local/lib/python3.5/dist-packages/torch/nn/_functions/thnn/", line 59, in forward
    output = torch.index_select(weight, 0, indices.view(-1))
TypeError: torch.index_select received an invalid combination of arguments - got (torch.cuda.FloatTensor, int, torch.LongTensor), but expected (torch.cuda.FloatTensor source, int dim, torch.cuda.LongTensor index)

It sounds like the third argument to torch.index_select() or the second argument to something.index_select() isn’t on the GPU.