Repository: rapidsai/cucim (language: python)

Issue:
Add support for Python 3.12
## Description

Contributes to https://github.com/rapidsai/build-planning/issues/40

This PR adds support for Python 3.12.

## Notes for Reviewers

This is part of ongoing work to add Python 3.12 support across RAPIDS.
It temporarily introduces a build/test matrix including Python 3.12, from https://github.com/rapidsai/shared-workflows/pull/213.

A follow-up PR will revert back to pointing at the `branch-24.10` branch of `shared-workflows` once all
RAPIDS repos have added Python 3.12 support.

### This will fail until all dependencies have been updates to Python 3.12

CI here is expected to fail until all of this project's upstream dependencies support Python 3.12.

This can be merged whenever all CI jobs are passing.

add test-time dependency on setuptools
## Summary

Contributes to #775, follow-up to #773.

Some nightly Python 3.12 tests are failing in this repo, as a result of the following:

* some tests here rely on `GPUtil`
* `GPUtil` was last updated 5+ years ago, and unconditionally imports `distutils` ([code link](https://github.com/anderskm/gputil/blob/42ef071dfcb6469b7ab5ab824bde6ca9f6d0ab8a/GPUtil/GPUtil.py#L34))
* `distutils` was removed from the Python standard library in Python 3.12

This proposes a **temporary fix**... adding a test-time dependency on `setuptools`. `import distutils` will work in an environment where `setuptools` is installed.

## Benefits of these changes

Fixes nightly tests, without imposing any new requirements of users of `cucim`.

## Notes for Reviewers

At some point, `setuptools` will likely remove `distutils` or the parts of it that `GPUtil` needs, and these tests will break in this way again. I've listed out some alternatives in #775, those should be explored at some point.

## How I tested this

On a machine with CUDA 12.2 and V100s, I can reproduce the test failures like this:

```shell
docker run \
    --rm \
    --gpus 1 \
    -v $(pwd):/opt/work \
    -w /opt/work \
    -it rapidsai/citestwheel:cuda12.5.1-ubuntu22.04-py3.12 \
    bash

python -m pip install \
    'cucim-cu12[test]==24.10.*,>=0.0.0a0'

python -m pytest \
    --numprocesses=8 \
    --dist=worksteal \
    ./python/cucim/tests/performance/clara
```

After running `pip install setuptools`, those tests pass 😁 


Discussion / hints:
/merge
We may be able to get rid of `GPUtil`. It's very outdated and we have other ways to get the information it provides.

It is only used in one test: https://github.com/rapidsai/cucim/blob/1584885a0e6447ded28f78a59e3f303a25c7c0f6/python/cucim/tests/performance/clara/test_read_region_memory_usage.py#L24

We can replace `GPUtil.get_GPUs()[0].memoryUsed` with this code from rmm:

```python
import rmm

def get_used_memory():
    free, total = rmm.mr.available_device_memory()
    memory_used = (total - free) / (2 ** 20)
    return memory_used
```

Or we can use cuda-python: https://nvidia.github.io/cuda-python/module/cudart.html#cuda.cudart.cudaMemGetInfo
```python
import cuda.cudart

def get_used_memory():
    status, free, total = cuda.cudart.cudaMemGetInfo()
    if status != cuda.cudart.cudaError_t.cudaSuccess:
        raise RuntimeError("Failed to get GPU memory info.")
    memory_used = (total - free) / (2 ** 20)
    return memory_used
```

Neither rmm or cuda-python are test dependencies at present, but I think it would be reasonable to use one of these stable, actively maintained packages instead of `GPUtil`. I lean towards the `cuda-python` solution because it's more minimal.

Fix patch:
diff --git a/conda/environments/all_cuda-118_arch-x86_64.yaml b/conda/environments/all_cuda-118_arch-x86_64.yaml
index 1b657fcd0..d657617b4 100644
--- a/conda/environments/all_cuda-118_arch-x86_64.yaml
+++ b/conda/environments/all_cuda-118_arch-x86_64.yaml
@@ -6,7 +6,6 @@ channels:
 - conda-forge
 - nvidia
 dependencies:
-- GPUtil>=1.4.0
 - c-compiler
 - click
 - cmake>=3.26.4,!=3.30.0
diff --git a/conda/environments/all_cuda-125_arch-x86_64.yaml b/conda/environments/all_cuda-125_arch-x86_64.yaml
index 2f5f50192..a193f5cdd 100644
--- a/conda/environments/all_cuda-125_arch-x86_64.yaml
+++ b/conda/environments/all_cuda-125_arch-x86_64.yaml
@@ -6,7 +6,6 @@ channels:
 - conda-forge
 - nvidia
 dependencies:
-- GPUtil>=1.4.0
 - c-compiler
 - click
 - cmake>=3.26.4,!=3.30.0
diff --git a/dependencies.yaml b/dependencies.yaml
index e8d5f8aaf..62e2ddefe 100644
--- a/dependencies.yaml
+++ b/dependencies.yaml
@@ -295,7 +295,6 @@ dependencies:
     common:
       - output_types: [conda, requirements, pyproject]
         packages:
-          - GPUtil>=1.4.0
           - psutil>=5.8.0
           - pytest>=6.2.4,<8.0.0a0
           - pytest-cov>=2.12.1
diff --git a/python/cucim/pyproject.toml b/python/cucim/pyproject.toml
index 4537f3df6..25248b3b2 100644
--- a/python/cucim/pyproject.toml
+++ b/python/cucim/pyproject.toml
@@ -57,7 +57,6 @@ Tracker = "https://github.com/rapidsai/cucim/issues"
 
 [project.optional-dependencies]
 test = [
-    "GPUtil>=1.4.0",
     "imagecodecs>=2021.6.8; platform_machine=='x86_64'",
     "matplotlib",
     "numpydoc>=1.5",


Test patch:
diff --git a/python/cucim/tests/performance/clara/test_read_region_memory_usage.py b/python/cucim/tests/performance/clara/test_read_region_memory_usage.py
index 8f259b910..9b17b1549 100644
--- a/python/cucim/tests/performance/clara/test_read_region_memory_usage.py
+++ b/python/cucim/tests/performance/clara/test_read_region_memory_usage.py
@@ -13,6 +13,7 @@
 # limitations under the License.
 #
 
+import cupy as cp
 import pytest
 
 from ...util.io import open_image_cucim
@@ -22,23 +23,24 @@
 
 
 def test_read_region_cuda_memleak(testimg_tiff_stripe_4096x4096_256_jpeg):
-    import GPUtil
-
-    gpus = GPUtil.getGPUs()
-
-    if len(gpus) == 0:
+    def get_used_gpu_memory_mib():
+        """Get the used GPU memory in MiB."""
+        dev = cp.cuda.Device()
+        free, total = dev.mem_info
+        memory_used = (total - free) / (2**20)
+        return memory_used
+
+    num_gpus = cp.cuda.runtime.getDeviceCount()
+    if num_gpus == 0:
         pytest.skip("No gpu available")
 
     img = open_image_cucim(testimg_tiff_stripe_4096x4096_256_jpeg)
 
-    gpu = gpus[0]
-    mem_usage_history = [gpu.memoryUsed]
+    mem_usage_history = [get_used_gpu_memory_mib()]
 
     for i in range(10):
         _ = img.read_region(device="cuda")
-        gpus = GPUtil.getGPUs()
-        gpu = gpus[0]
-        mem_usage_history.append(gpu.memoryUsed)
+        mem_usage_history.append(get_used_gpu_memory_mib())
 
     print(mem_usage_history)