Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

distance_transform_edt not always working with cuCIM backend #7660

Closed
aaronkujawa opened this issue Apr 18, 2024 · 2 comments · Fixed by #7675
Closed

distance_transform_edt not always working with cuCIM backend #7660

aaronkujawa opened this issue Apr 18, 2024 · 2 comments · Fixed by #7675

Comments

@aaronkujawa
Copy link
Contributor

Describe the bug
The distance_transform_edt returns all zero in special cases when cuCIM is installed and used as a backend.
This happens when convert_to_cupy() decides that the data array needs to be copied here:

To Reproduce

!pip install torch
!pip install cucim
!pip install monai
!pip install numpy

from monai.transforms import distance_transform_edt
import torch
import numpy as np

# create a c-contiguous array
mask = np.zeros((1, 4, 4))

# make it f-contiguous to force convert_to_cupy to copy the data array later on
mask2 = np.asfortranarray(mask)

# convert to torch tensor to make distance_transform_edt use cuCIM backend
mask = torch.tensor(mask, device='cuda')
mask2 = torch.tensor(mask2, device='cuda')

# set half of the mask to foreground
mask[0, 0:2, :,] = 1
mask2[0, 0:2, :,] = 1

# confirm masks are the same
print(mask)
print(mask2)

# calculate distance transform
edt = distance_transform_edt(mask)
edt2 = distance_transform_edt(mask2)

# check if the results are the same
print(edt)
print(edt2)
tensor([[[1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]]], device='cuda:0', dtype=torch.float64)
tensor([[[1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]]], device='cuda:0', dtype=torch.float64)
tensor([[[2., 2., 2., 2.],
         [1., 1., 1., 1.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]]], device='cuda:0')
tensor([[[0., 0., 0., 0.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]]], device='cuda:0')

Expected behavior
Results should be the same, but the second output is all zero.

KumoLiu added a commit to KumoLiu/MONAI that referenced this issue Apr 19, 2024
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@KumoLiu
Copy link
Contributor

KumoLiu commented Apr 19, 2024

Hi @aaronkujawa, thanks for the reporting. I have created a PR to fix it.
Main reason is that indices_ may not always be a shallow copy for F-order input as it constantly converts to C-order.

indices_ = convert_to_cupy(indices)

data = cp.ascontiguousarray(data)

@aaronkujawa
Copy link
Contributor Author

Hi @KumoLiu , thanks for the quick fix!

KumoLiu added a commit that referenced this issue Apr 23, 2024
Fixes #7660

`indices_` may not always be a shallow copy for F-order input as it
constantly converts to C-order

https://github.com/Project-MONAI/MONAI/blob/ffd4454b576abd4eaae30b364f41c213e30dca4c/monai/transforms/utils.py#L2209

https://github.com/Project-MONAI/MONAI/blob/ffd4454b576abd4eaae30b364f41c213e30dca4c/monai/utils/type_conversion.py#L262

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants