Repository navigation
Add NumPy v1.17 new random API with Generator - #4177
Conversation
|
I tried to design some templatized mechanism for the kernels to allow any type of generator obbeying the API, The idea was that the |
|
Added an |
| }; | ||
|
|
||
| template<typename F, typename T, typename R, typename... Args> | ||
| __global__ void execute_dist(intptr_t param, void* out, ssize_t size, Args... args) { |
There was a problem hiding this comment.
since all the distributions use the same kernel, this avoids the need to write redundant kernels, however If there was an easy way to templatize the functors too, it would be awesome.
|
Btw, randint with this is super fast, around 26 us to generate 10.000 random integers compared to 500 us of current cupy implementation |
| template <typename F, typename R> | ||
| struct kernel_launcher { | ||
| template<typename T, typename... Args> | ||
| void operator()(int bpg, int tpb, cudaStream_t stream, Args&&... args) { |
There was a problem hiding this comment.
we can't have kernels as class methods, so we have to resort to dirty tricks like this ...
| template <typename F, typename... Ts> | ||
| void generator_dispatcher(int generator_id, F f, int bpg, int tpb, cudaStream_t stream, Ts&&... args) { | ||
| switch(generator_id) { | ||
| case CURAND_XOR_WOW: return f.template operator()<curand_xor_state>(bpg, tpb, stream, std::forward<Ts>(args)...); |
There was a problem hiding this comment.
Data comes from python, so we lose it in the inheritance hierarchy of BitGenerator :( and this makes imposible to directly instatiate a template. We have to use an enum like this.
| } | ||
|
|
||
|
|
||
| __device__ uint32_t rk_interval_32(rk_state* state, int mx, int mask) { |
leofang
left a comment
There was a problem hiding this comment.
This looks good! Just did a quick pass and left some 1st round comments.
| chunks = (out.size + bsize - 1) // bsize | ||
| for i in range(chunks): | ||
| y_ptr = <intptr_t>out[i*bsize:].data.ptr | ||
| func(generator, state, y_ptr, bsize, strm, *args) |
There was a problem hiding this comment.
What happens for the last chunk that might be smaller than bsize?
| void init_curand_generator(int generator, intptr_t state_ptr, uint64_t seed, ssize_t size, intptr_t stream); | ||
| void interval_32(int generator, intptr_t state, intptr_t out, ssize_t size, intptr_t stream, int mx, int mask); | ||
| void interval_64(int generator, intptr_t state, intptr_t out, ssize_t size, intptr_t stream, uint64_t mx, uint64_t mask); | ||
| void beta(int generator, intptr_t state, intptr_t out, ssize_t size, intptr_t stream, double a, double b); | ||
| void exponential(int generator, intptr_t state, intptr_t out, ssize_t size, intptr_t stream); |
There was a problem hiding this comment.
I think you don't need the tailing semicolons.
| generator = CURAND_XOR_WOW # Use The Enum | ||
|
|
||
| def _type_size(self): | ||
| return sizeof(curandState) |
There was a problem hiding this comment.
I am a bit confused: what's the difference between curandState and curandState_t?
There was a problem hiding this comment.
no one knows, they are used interchangeably in the documentation, but we should stick to only one, will fix
|
87e959a fixed the C++ error, but we still have errors due to using variadic arguments (#4177 (comment)). |
20184cb to
3e3538a
Compare
|
Would like some feedback on the new design :) |
d9a0163 to
aa149e1
Compare
| left=self.left, mode=self.mode, right=self.right, size=(3, 2)) | ||
|
|
||
|
|
||
| """ |
GeneratorGenerator
leofang
left a comment
There was a problem hiding this comment.
Very quick first-pass scan, mostly on style. Will take a deeper look later.
I don't fully understand the design decision, likely because I'm not familiar with either NumPy's or cuRAND's API, will look into them to understand things better.
I noticed some classes in .pyx are plain Python classes. Given the simplicity of data management here, I am not too worried about not turning them into cdef classes. But it'd be good to have a benchmark done. Is it possible to compare random number generation throughput before and after this PR?
Based on offline discussion with @emcastillo we think it's straightforward to add HIP support. Given the massive amount of changes in this PR I suggest we defer it (I could try to handle it in a separate PR).
| # Size is the number of threads that will be initialized | ||
| def __init__(self, seed=None, *, size=10000*256): | ||
| super().__init__(seed) | ||
| # Raw kernel has problems with integers with the 64th bit set |
There was a problem hiding this comment.
When passing a 64-bit unsigned integer as a parameter to a raw kernel, it is cast somewhere in the code to a signed one using numpy.int64, if that integer has its 64-th bit set, then an Out of range error is raised 😔
| def _check_device(self): | ||
| if cupy.cuda.Device().id != self._current_device: | ||
| raise RuntimeError( | ||
| 'This Generator state is allocated in a different device') |
There was a problem hiding this comment.
So what happens when the users switch the device?
There was a problem hiding this comment.
This should error, users shouldn't be able to use a generator with its state allocated in one device, in another device.
| state. One may also pass in a `SeedSequence` instance. | ||
| """ | ||
| def __init__(self, seed=None): | ||
| self.lock = threading.Lock() |
There was a problem hiding this comment.
What's the purpose of self.lock? I don't find it used anywhere.
There was a problem hiding this comment.
Yeah, it is actually documented in the numpy API, but I believe we don't need it, so I can get rid of it.
There was a problem hiding this comment.
Maybe self.lock should be used in random_raw of subclasses.
The kernel invocations are atomic themselves, so we can run _launch_dist in parallel.
There was a problem hiding this comment.
The user's custom generator class may require self.lock, so let's leave this interface!
|
Jenkins, test this please. |
|
Jenkins CI test (for commit dd36e4b, target branch master) failed with status FAILURE. |
|
Jenkins, test this please |
|
Jenkins CI test (for commit 7612b0c, target branch master) failed with status FAILURE. |
leofang
left a comment
There was a problem hiding this comment.
Could you add a comment to explain why this is disabled for HIP? Perhaps you can add the link to ROCm/hip#2186.
|
PTAL! |
|
Jenkins, test this please. |
|
Jenkins CI test (for commit f63babf, target branch master) failed with status FAILURE. |
|
Jenkins, test this please |
commented
Jan 6, 2021
|
Jenkins CI test (for commit 04ce86d, target branch master) succeeded! |
commented
Jan 6, 2021
|
LGTM! |
commented
Jan 6, 2021
|
Awesome work @emcastillo!!! |
This is still on-going
Due to some issues when generating the distributions in #4163, the added bit generators will use only the curand device API
I have spent a great deal of time trying to figure out how to correctly templatize the code, some ideas will be greatly appreciated. The idea is that we want multiple curand generators (or custom ones) to be supported in the kernels that implement the distributions. The generators are usually specified in the
BitGeneratorin python so we need to get that type info down to the Cython level.TODO