question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cupy.random.randint is slow

See original GitHub issue

Hi all. It seems, numpy.random.randint is notably faster than cupy.random.randint. Is this expected?

I’ve also attained a dramatic speedup, sampling uniform floats with cupy and rounding them to int.

import cupy as cp
import numpy as np
import time

def runWithTime(f, iters):
    start = time.time()
    for i in range(iters):
        f()
    print(time.time() - start)

size = 100000
high = 1000000
iters = 50000

runWithTime(lambda: cp.random.uniform(low=0, high=high, size=size).astype('int', copy=False), iters)
runWithTime(lambda: np.random.randint(0, high, size=size), iters)
runWithTime(lambda: cp.random.randint(0, high, size=size), iters) 

prints

2.910490036010742
23.180258750915527
36.04059100151062

Is there a reason why we cannot rewrite cupy.random.randint, relying cupy.random.uniform?

  • Conditions
CuPy Version          : 6.0.0
CUDA Root             : /usr/local/cuda
CUDA Build Version    : 10000
CUDA Driver Version   : 10010
CUDA Runtime Version  : 10000
cuDNN Build Version   : 7301
cuDNN Version         : 7605
NCCL Build Version    : 1000
NCCL Runtime Version  : (unknown)
Linux 4.4.180-102-default 
Intel(R) Xeon(R) Gold 5118 CPU @ 2.30GHz
Tesla P100

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

0reactions
akopichcommented, Oct 13, 2020

@anaruse Could you please clarify a little further? Is your argument based on the fact, that the uniform distribution is modeled by float64-values of equal probability for intervals of equal exponent field, like [0.5, 1)? randint seems to be implemented only for int32 and it is possible to distribute 2^52 equiprobable objects into 2^32 bins almost uniformly. And in general (not on [0.5,1)), we have even more (float64-valued) objects, which are not equiprobable.

Also, I’ve noticed, cupy.random.uniform might return high.

import cupy as cp

high = int(2**32) - 1
low = int(2**32) - 3
size = 100000000
print(cp.max(cp.random.uniform(low=low, high=high, size=size)) == high)

prints True. Is this expected behavior?

Speaking of “almost”. How do you test the random generators cupy provides?

Read more comments on GitHub >

github_iconTop Results From Across the Web

cupy.random.randint — CuPy 11.4.0 documentation
Returns a scalar or an array of integer values over [low, high) . Each element of returned values are independently sampled from uniform...
Read more >
Only GPU to CPU transfer with cupy is incredible slow
Event() with record and synchronize to avoid measuring any random times, but is still the same result, the GPU->CPU transfer is incredible slow....
Read more >
Slow and fast methods for generating random integers in Python
Python's random.randint() function feels quite slow, in comparison to other randomness-generating functions. Since randint() is the ...
Read more >
CuPy Documentation - Read the Docs
CuPy is a NumPy/SciPy-compatible array library for GPU-accelerated computing with Python. CuPy acts as a drop-in.
Read more >
Random array generation : numba cuda slower than cupy?
Hi all, I am looking to optimize the random number generation in my Brownian dynamics simulation code. I quickly turned to GPU computing ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found