clEsperanto / pyclesperanto

GPU-accelerated Image Processing library using OpenCL
https://clesperanto.github.io/
BSD 3-Clause "New" or "Revised" License
31 stars 6 forks source link

[Bug] Jupyter notebook kernel crashes with pyclesperanto but not pyclesperanto_prototype #219

Closed TimMonko closed 1 month ago

TimMonko commented 1 month ago

Pre-requist tasks

Describe the bug In a juptyer notebook, kernel crashes sporadically. Usually, the first time I write code with import pyclesperanto as cle, the kernel does not crash. However, upon re-running the same code block or after restarting the kernel, the kernel will always crash. Switching to import pyclesperanto_prototype as cle results in the kernel never crashing. I'm mostly posting this in case this can be a spot where this can start to be tracked down. I have had this occur in a few different projects over the past few weeks, and always find the fix is to fall back to the prototype, but I haven't kept track if it is particular functions or not.

To Reproduce I have no idea. I have tried to create a test notebook where this happens. With a random numpy array it never seems to happen. With image data that is the same size it sometimes happens. The most consistent time that it occurs, is when I have a napari viewer open in that kernel. This should be enough to crash the kernel, if it was consistent, but this does not do it regularly:

import numpy as np
import pyclesperanto as cle
# import pyclesperanto_prototype as cle

# Calculate the number of elements for a 5MB array with uint16 elements
num_elements = (5 * 1024 * 1024) // 2
# Create a 5MB, 2D array
array_5mb_2d = np.random.randint(
    0, 
    65536, 
    size=(1024, num_elements // 1024), 
    dtype=np.uint16
)

def background_subtraction(img, gb_sigma=1, th_sigma=10):
    gb = cle.gaussian_blur(img, sigma_x=gb_sigma, sigma_y=gb_sigma)
    th = cle.top_hat_box(gb, radius_x=th_sigma, radius_y=th_sigma)
    return th

img_bs = background_subtraction(array_5mb_2d, gb_sigma=2, th_sigma=30)

Screenshots If applicable, add screenshots to help explain your problem. Expected results, vs actuall results. Or the output error message if any.

Configuration details:

Additional context Add any other context about the problem here.

StRigaud commented 1 month ago

Thanks for raising the issue! I was able to reproduce on a Silicon M2 quickly.

It seems that having a napari running in the jupyter kernel while calling a pyclesperanto operation make the jupyter kernel crash. Without any napari running, I did not have any issues.

My first guess is an issue related an error in the pyclesperanto.array API in perspective of the numpy.array API. We will try to solve this quickly.

TimMonko commented 1 month ago

I'm glad my description was helpful enough to figure that out. I was quite tired last night when writing that out... I can confirm that this code minimally reproduces the crash. Commenting out instantiating the viewer prevents the crash as does switching to prototype.

import numpy as np
import napari
import pyclesperanto as cle
# import pyclesperanto_prototype as cle

viewer = napari.Viewer()
img = np.random.randint(0, 65536, size=(1024, 1024), dtype=np.uint16)
img_gb = cle.gaussian_blur(img, sigma_x=2, sigma_y=2)
StRigaud commented 1 month ago

Yes, I though at start it was due to some incompatibility between numpy memory beeing used in napari and clesperanto but simply having a napari viewer is enough for crashing the kernel, even if the data is not loaded in the viewer.

I will need to search a bit on how to solve this ... thanks for pointing this to us !

StRigaud commented 1 month ago

Reverting to 0.10.3 seems to solve the issue.

TimMonko commented 1 month ago

I just tested and 0.10.3 does not crash my kernel.

StRigaud commented 1 month ago

Great, I will work on fixing this in the coming days

StRigaud commented 1 month ago

The source of the problem was related to the introduction of the Optionnal annotation in the function definitions. I still fail to understand how it was not triggered before and how it is linked to having a napari viewer active ...

Hopefully #220 should fix this bug. I will deploy these in the next release of the package which is planned soon.

Again, thanks for pointing this !

StRigaud commented 1 month ago

@TimMonko updating to the 0.12.1 should fix the issues now.

Do no hesitate to reopen if needed!