CUDA with torch.random()

571 views
Skip to first unread message

abes

unread,
Mar 23, 2012, 4:22:42 PM3/23/12
to tor...@googlegroups.com
I'm trying to switch my Tensor type to CUDA with "torch.setdefaulttensortype('torch.CudaTensor')", which seems to work fine until I try to generate a random number.

For example:

require 'cutorch'
torch.setdefaulttensortype('torch.CudaTensor')

-- this works fine
a = torch.Tensor(5)

-- this causes the error below
a = torch.random()

with the error:

[string "return torch.random()"]:1: internal error: the default tensor type does not seem to be an actual tensor
stack traceback:
[C]: in function 'random'
[string "return torch.random()"]:1: in main chunk
[C]: ?


I was hoping someone might know what's going and/or where to look in the code to fix the error (I got as far as the automated wrapping of the C code, but I wasn't sure if it was an issue of the wrapping or the C code itself).

Thanks!

The Dictator

unread,
Apr 3, 2012, 1:48:45 PM4/3/12
to torch7
yes, we know about this one
it is not easy to fix

it is related to the fact that torch.random() might be used both for
numbers or tensors
and if you get a tensor, you have to dispatch to the right C function,
which handles the right type

anyways, it would be too long to explain the details. the cuda package
does not provide torch.random(), and you get this problem

in the meantime, you can do torch.CudaTensor.random =
torch.DoubleTensor.torch.random to fix the issue above

abes

unread,
Apr 3, 2012, 5:49:29 PM4/3/12
to tor...@googlegroups.com
Thanks! I tried the version you have above, and couldn't get it to work, but I changed it to:

torch.rand = torch.FloatTensor.torch.rand
torch.uniform = torch.FloatTensor.torch.uniform -- required by the Linear module

which appears to to be doing the right thing.


Unfortunately, when I run my code it now takes roughly 4 times as long. I suspect that means that not all the vectors/matrices are living on the GPU. I take the data generated by the rand() and convert it with t:cuda(), so I don't think it's that (and only do that once). Almost all my other operations are done through the modules, and I use torch.Tensor() everywhere besides the random functions, so it's a little unexpected.

I'm currently investigating possible causes. One of them might be my less-than-efficient 'join' function for concatenating vectors together.
Reply all
Reply to author
Forward
0 new messages