Repository navigation
Add cupyx.scipy.ndimage sum, mean,standard deviation and variance - #3259
Conversation
|
I did some performance testing and got the following results The large usage of atomicAdd is making this computation extremely slow, I am trying to improve this looking for other reduction methods that could be applied here, as well as moving the label and index lookup to simple hashed map in shared memory( Some guidance here would be helpfull) to reduce the lookup time. |
|
Can you post an example of your reduction kernels so we can see why they didn't work? |
|
@emcastillo I couldnt write one because the strategy used in cupy Reduction Kernel relies on interleaved addressing strategies to reduce, but ndimage.sum and its variants, the values are to be selectively summed by a label, which cant summed using the reduction kernel, I dont think we can add values to certain location using our current reduction strategy.( Please correct me If i am wrong anywhere) I tried to split the difference where in the inital step I create multiple boxes of index shape and fill them according to a split in the labels and then sum them, as a way to reduce the serialization of atomicAdd. A implementation would go like this However even this method was too slow compared to ndimage.sum using numpy. |
|
@emcastillo I added tests and tried to improve the kernel but I think this task is largely serial and I wasn't able to improve it significantly. |
|
As for reduction algorithm, the following code does computation equivalent to import numpy
import cupy
image = cupy.arange(100, dtype=numpy.float32)
labels = cupy.random.choice(3, 100) + 1
index = cupy.array([1, 2, 3])
cupy.where(labels == index[..., None], image, 0).sum(axis=(1,))Additionally, using |
|
@takagi I tested the following n-d generalization: def sum_v2(image, labels, index):
matched = labels == index.reshape((-1,) + (1,) * image.ndim)
sum_axes = tuple(range(1, 1 + image.ndim))
return cupy.where(matched, image, 0).sum(axis=sum_axes, dtype=cupy.intp)For shape (256, 256, 256), it was indeed faster for 2, 4 or 8 labels, but became substantially slower for larger numbers of labels.
I didn't try it, but I suppose one could run |
|
@grlee77 Thanks for pointing it out and it makes sense. I tried to implement the same thing with BTW, I made a similar performance test as @grlee77 's including
|
|
Thank you so much @takagi and @grlee77 for your help and insight. I ran the tests myself using sum_v2 and ran out of memory as predicted. I then implemented the batched version as suggested by @grlee77 . def sum_v3(image, label, index, batch_size = 4):
out = cupy.empty_like(index, dtype=image.dtype)
for i in range(0, index.size, batch_size):
matched = label == index[i:i + batch_size].reshape((-1,) + (1,) * image.ndim)
sum_axes = tuple(range(1, 1 + image.ndim))
out[i:i + batch_size] = cupy.where(matched, image, 0).sum(axis=sum_axes)
return out (256, 256, 256), batch_size = 4
(For reference my GPU is a 970MX) Since I cannot compute the values for sum_v2 for larger label sizes, someone else could perhaps try and compare the performance difference that arises due to batching it. |
|
|
|
We talked about this PR and it would be reasonable to use the both algorithms, elementwise one and reduction, switching them by the number of labels empirically given. How do you think? |
|
I think that would be the best solution too, Since my GPU isn't very good with atomicAdd, I started testing my work on Google Colab.
size : (32, 32, 32)
size : (64, 64, 64)
size : (128, 128, 128)
size : (256, 256, 256)
In my opinium sum_v2 requires a lot of memory, so we should consider sum_v3 for the reduction method, sum_v4 which is a binned method that I proposed earlier doesent seem to outperform the current method. |
|
Thanks for the detail investigation on colab, and switching the algorithms with the label size of 4 sounds good to me! The best candidate of the label size would be 4 or 8, which may vary on environments. I respect your opinion with Tesla P100. Then, would you apply this result to the code of this PR? |
niteya-shah
left a comment
There was a problem hiding this comment.
I have updated the colab notebook above to reflect the tests for mean. Currently variance is blocked out to due it not working.
|
@takagi any update on this? |
|
Thanks, let me see the changes shortly! |
takagi
left a comment
There was a problem hiding this comment.
Would you check some comments?
takagi
left a comment
There was a problem hiding this comment.
Would you check some comments?
takagi
left a comment
There was a problem hiding this comment.
Would you check the comments?
|
Would you parameterize the ndimage operations using |
Co-authored-by: Masayuki Takagi <[email protected]>
|
@takagi I have added the float16 tests, with the reduced thresholds. |
|
pfnCI, test this please. |
|
Successfully created a job for commit 675aa47: |
|
Jenkins CI test (for commit 675aa47, target branch master) succeeded! |
|
Let me merge this PR to the master. Thanks for your effort! I'll make some cosmetic changes later, which are not essential for your contribution. |
This PR adds the following routines for cupyx.scipy.ndimage
Currently this method uses Elementwise Kernels,I tried to use Reduction Kernel but couldnt get it to work, if possible can someone recommend a way to use Reduction Kernels.
This PR is still a work in progress.
Todo list