-
Notifications
You must be signed in to change notification settings - Fork 6.8k
mxnet.nd.random.multinomial is very slow on CPU and stucked on GPU #15231
Comments
Hey, this is the MXNet Label Bot. |
@zixuanweeei could you take a look for the CPU performance? |
While |
You can get nearly the same result as mx.nd.random.multinomial, but very swift. sample_list = []
p = [1 / 300000.] * 300000
probs = np.array(p)
sample_times = np.random.multinomial(len(probs), np.array(probs))
for i, t in enumerate(sample_times):
if t > 0:
sample_list.extend([i]*t)
random.shuffle(sample_list) |
pyt*ch version t0 = time.time()
p = [1/300000.] * 300000
probs = torch.as_tensor(p)
indices = torch.multinomial(probs, len(probs), True)
print("Pytorch output:\t\t", indices.tolist())
t1 = time.time()
print(t1 - t0) |
@chinakook Thanks for your examples. It's informative. Pytorch uses a binary search for the slot in which the prob falls, while it uses linear search in MXNet. Pytorch uses the alias method as well. But I am not sure whether it works for drawing samples just once. We will optimize it using binary search at this moment. |
We used binary search to optimize Thanks for @chinakook 's examples. The optimization requires further verification. And the PR is on the way:tada:. |
@chinakook Thanks for the issue. You can close it if there are no problems. BTW, considering that
from this, I think Any insights of this? |
No problem. |
MXNet version is mxnet-cu100mkl 1.5.0b20190605(also tested on mxnet-cu100 1.4.1)
The result is
The text was updated successfully, but these errors were encountered: