Implement ATen Distributions.cu + Poisson#58
Conversation
|
Ah, not quite ready for review yet, by the way - I still need to debug this CUDA issue and write tests. I tried refactoring to use ATen instead (which is much cleaner), but I can't figure out how to sample a uniform given a |
fritzo
left a comment
There was a problem hiding this comment.
Note you should register this in the EXAMPLES list in test_distributions.py. When you do so, tests will fail because you do not implement .entropy(). If you don't want to implement .entropy() (which as I understand has no closed form for Poisson), you should allow NotImplementedError in TestDistributionShapes.test_entropy_shape():
- actual_shape = dist.entropy().size()
+ try:
+ actual_shape = dist.entropy().size()
+ except NotImplementedError:
+ continueThere was a problem hiding this comment.
nit: Consider reverting unnecessary whitespace changes to ease review by PyTorch folks.
There was a problem hiding this comment.
Thanks! I usually squash before review but I'll be more on top of it.
|
On your question, I don't think we should add calls to get random numbers directly to the generator; the normal pattern is to pass in the generator to a function that generates the random numbers, right? |
|
Yeah, that sounds right. I'm not sure about casting the result of EDIT: taken care of, I didn't realize how |
| //g if (!getApplyGrid(totalElements, grid)) { | ||
| //g return false; | ||
| //g } | ||
| //g grid = dim3(1); |
There was a problem hiding this comment.
This is temporary - looking for advice on how to best limit the blockDim to 256 when calling CUDA_tensor_apply2
|
Ok, I think it's probably ready for review. @fritzo, given the C++/CUDA changes, it's probably best to ask someone from PyTorch to review as well? I don't know what exactly the plans are for CUDAGenerator, etc. |
fritzo
left a comment
There was a problem hiding this comment.
Python code looks good. just one comment on parameter name. I have not reviewed CUDA code; let's do that review on pytorch/pytorch.
There was a problem hiding this comment.
nit: preceding underscore usually denotes a private variable. It's probably safer to call this lam or lambda_. Alternatively it would be nice to call it rate which is both semantically meaningful and improves compatibility with tensorflow distributions.
There was a problem hiding this comment.
For example self._lambda would be the obvious member name but that looks private. Instead self.lambda_ or self.rate are clearly public.
There was a problem hiding this comment.
Both sound good to me. Let's use rate since that's more evocative? I.e. like mean/std in Normal already, and matches up with Exponential, which is the right idea.
|
@apaszke How should we test the CUDA sampler? Do we use the same tests in |
|
I think |
There was a problem hiding this comment.
It's better to use tensor instead of zeros if you don't depend on the initial values of the tensor (it will be uninitialized)
There was a problem hiding this comment.
Is this unstable in fp32? fp64 math is extremely slow on non-Tesla GPUs, and we should avoid it
There was a problem hiding this comment.
Is this the right place for this copyright notice?
There was a problem hiding this comment.
Let's avoid adding more of the sampling methods to the global namespace. torch.distributions is the way to expose those samplers
|
Ok, think I addressed comments (thanks for the review!), and am removing |
8bd52eb to
3d39052
Compare
There was a problem hiding this comment.
if you are having trouble with half, try DISPATCH_ALL_FLOATING_TYPES; dispatch_all uses Half and DISPATCH_ALL_FLOATING_TYPES uses half; we should resolve this, probably always using Half (and providing device conversion functions?). CC @colesbury
There was a problem hiding this comment.
Ideally we would always use half for device functions and at::Half for host functions.
|
After doing some checks, it looks like I think there's 3 options - (1) to merge as-is (by raising the threshold on the CUDA version), but to warn that for low values of |
|
I'm fine with all options. I'll leave the decision to @fritzo |
|
Option 1 sounds very reasonable. You could also file an issue at probtorch/pytorch noting that we should revisit this. |
|
OK, fixed the issues and added a Philox-based RNG. I don't think the sampling algorithm is the most accurate, but we can work on it in the future I think. Also, I think there's a bug in the handling of Let me know if this is good, I'll squash the commits to be 1 (or whatever you think is semantic), and then update the pytorch PR. |
|
(discussing in pytorch#4556) |
I got a bit into the weeds and the scope expanded, but I think this is the right way to do it. This PR (will) implement a pointwise Poisson sampling method for CPU/CUDA, and should be followed up quickly with ports of the Gamma / Dirichlet samplers, and other things we might want to use from
distributions.c.So far:
GeneratorinsideTHCRandomTensor.hto beTHCGenerator, and fix CUDAGenerator instantiationtorch.poisson(Variable only for now, but we can wrap it).