Is there a bug in implementation of `torch.multinomial()`?

I was reading the documentation of torch.multinomial() method in the online documentation.

I tried this in IPython environment.

weights = torch.Tensor([0, 10, 3, 0])
print weights   # out: [torch.FloatTensor of size 4]
torch.multinomial(weights, 5)

Of course, error occurs because 5 is larger than the size of weights and I didn’t set replacement=True. The error message is like this:

RuntimeError: cannot sample n_sample > prob_dist:size(1) samples without replacement at /home/me/pytorch/pytorch/torch/lib/TH/generic/THTensorRandom.c:94

But after this, the dimensions of weights changed.

print weights  # out: [torch.FloatTensor of size 1x4]

If I run as following without runtime exception, then the dimensions of weights do not change.

weights = torch.Tensor([0, 10, 3, 0]) 
print weights # out: [torch.FloatTensor of size 4]
torch.multinomial(weights, 4)
print weights # out: [torch.FloatTensor of size 4]

Hi,

Yes it is a bug in the C implementation that is not cleaning up properly when error are raised.
Could you open an issue on github to track this and say that this operation is not cleaned up when error are raised please.

Thanks for the report !

My pleasure, and thank you for the explanation! Here is the issue: Operation is not cleaned up when exception raised in file THTensorRandom.c