cuslide2: make `pip install cucim-cu13` work and add tests for this #1219
Should we note this is true for Conda as well?
[P2-1] Locate the installed plugin before importing cuCIM
AI-assisted review comment (finding 1/2).
With a pip or conda install and no source-built plugin, the documented python scripts/test_aperio_svs.py --download command reaches this new import before setup_environment() writes CUCIM_CONFIG_PATH. Loading cucim.clara loads the native library, whose static CuImage::config_ reads the configuration immediately. The later environment update cannot select cuslide2, so this script can exercise the legacy plugin while reporting a cuslide2 test. Please locate the installed package without importing cuCIM, then complete plugin configuration before the first import.
Validation: The exact helper confirmed the import/configuration order with an import recorder. Native initialization order was verified in source. The proposed patch passed git apply --check, Python syntax checks, and isolated default/enable-flag selection probes. Full cuCIM and GPU execution remain unrun.
<details>
<summary>Detailed reasoning, proposed patch, and regression coverage</summary>
Evidence
The new installed-package branch is scripts/test_common.py:52-60. The configuration file and environment variable are created later at scripts/test_common.py:84-98. The default download command is documented at README.md:145-149.
The Python package imports its native extension at python/cucim/src/cucim/clara/__init__.py:9-10. The load-time configuration object is initialized at cpp/src/cuimage.cpp:147-151, and its constructor reads the file and environment at cpp/src/config/config.cpp:24-43. Image initialization later uses that stored plugin list at cpp/src/cuimage.cpp:1068-1071.
The exact Python helper was executed with a fake installed plugin and a recorder at the cucim.clara import boundary. Without the enable flag, all three selection variables were absent at import. Only after the import did the helper set CUCIM_CONFIG_PATH. Setting ENABLE_CUSLIDE2=1 before starting Python avoids this particular default-command failure. The source-build path does not introduce this early import.
This is a new installed-package-path problem. The baseline helper did not import cuCIM while discovering the plugin directory.
Original hunk
if plugin_lib is None:
from cucim.clara import _get_plugin_root
plugin_lib = Path(_get_plugin_root())
if not list(plugin_lib.glob("cucim.kit.cuslide2@*.so")):
raise RuntimeError(
f"No cuslide2 plugin (cucim.kit.cuslide2@*.so) found in the "
f"installed cuCIM package ({plugin_lib}) or in a source build."
)
Suggested fix
Resolve the top-level package location with find_spec("cucim"), which does not import this top-level package, then search its clara directory. Avoid find_spec("cucim.clara"), which would import the parent package. The proposal retains source-build precedence and the existing configuration logic. It assumes the package has not already been imported before this setup helper runs.
--- a/scripts/test_common.py
+++ b/scripts/test_common.py
@@ -10,6 +10,7 @@
import tempfile
import time
from importlib import metadata as importlib_metadata
+from importlib.util import find_spec
from pathlib import Path
import numpy as np
@@ -50,13 +51,19 @@
plugin_lib = candidate
break
if plugin_lib is None:
- from cucim.clara import _get_plugin_root
-
- plugin_lib = Path(_get_plugin_root())
- if not list(plugin_lib.glob("cucim.kit.cuslide2@*.so")):
+ # Locate the package without importing cuCIM: its library reads the
+ # plugin configuration at load time, before setup below is complete.
+ spec = find_spec("cucim")
+ package_dirs = [] if spec is None else spec.submodule_search_locations or []
+ for package_dir in package_dirs:
+ candidate = Path(package_dir) / "clara"
+ if list(candidate.glob("cucim.kit.cuslide2@*.so")):
+ plugin_lib = candidate
+ break
+ if plugin_lib is None:
raise RuntimeError(
- f"No cuslide2 plugin (cucim.kit.cuslide2@*.so) found in the "
- f"installed cuCIM package ({plugin_lib}) or in a source build."
+ "No cuslide2 plugin (cucim.kit.cuslide2@*.so) found in the "
+ "installed cuCIM package or in a source build."
)
dist_version = None
Regression coverage
Run the script in a fresh interpreter with an installed plugin, no source build, and no selection environment variables. Verify that cuslide2 is selected before opening an image. Cover the default download command, an explicit enable flag, source-build discovery, and a missing installed plugin.
</details>
[P2-2] Pin the subprocess to the cuslide2 plugin
AI-assisted review comment (finding 2/2).
If the caller exports CUCIM_PLUGINS=cucim.kit.cuslide@<version>.so, this subprocess inherits it. Config::override_from_envs() applies that explicit list after ENABLE_CUSLIDE2, so the test runs the legacy plugin even though cuslide2 is installed. Its metadata and pixel assertions do not identify the decoder. This can hide a broken cuslide2 installation. Please set CUCIM_PLUGINS to the discovered cuslide2 filename in the child environment.
Validation: The actual C++ environment-selection method was compiled with minimal enclosing storage, and the exact Python launcher was checked with an intercepted subprocess. The proposed patch passed git apply --check, Python syntax checks, and an inherited-legacy-override probe. No GPU decoder was executed.
<details>
<summary>Detailed reasoning, proposed patch, and regression coverage</summary>
Evidence
The child environment is assembled at python/cucim/tests/unit/clara/test_cuslide2_plugin.py:109-116. The module's skip check only establishes that a cuslide2 file exists at python/cucim/tests/unit/clara/test_cuslide2_plugin.py:34-44. The assertions at python/cucim/tests/unit/clara/test_cuslide2_plugin.py:138-154 check output properties without proving which plugin produced them.
The configuration implementation first translates the default plugin list for ENABLE_CUSLIDE2, then replaces that list from CUCIM_PLUGINS: cpp/src/config/config.cpp:173-220. A host executable compiled from that exact selection method chose cucim.kit.cuslide2@26.12.00.so with only the enable flag, and cucim.kit.cuslide@26.12.00.so when the legacy override was also present. The exact _run_cuslide2() function, with subprocess execution intercepted, forwarded both conflicting variables unchanged.
The selection precedence is existing behavior. The new regression test fails to isolate itself from that supported override. This does not establish that current CI exports the conflicting variable, or that all four cases passed through the legacy decoder in this review.
Original hunk
def _run_cuslide2(path):
env = dict(os.environ, ENABLE_CUSLIDE2="1")
proc = subprocess.run(
[sys.executable, "-c", _CHECK, str(path)],
env=env,
capture_output=True,
text=True,
timeout=600,
)
Suggested fix
Select the actual installed cuslide2 filename explicitly in the child environment. The proposal rejects ambiguous installations containing multiple cuslide2 versions so the test cannot silently choose an arbitrary library.
--- a/python/cucim/tests/unit/clara/test_cuslide2_plugin.py
+++ b/python/cucim/tests/unit/clara/test_cuslide2_plugin.py
@@ -107,7 +107,15 @@
def _run_cuslide2(path):
- env = dict(os.environ, ENABLE_CUSLIDE2="1")
+ from cucim.clara import _get_plugin_root
+
+ plugins = glob.glob(os.path.join(_get_plugin_root(), "cucim.kit.cuslide2@*.so"))
+ assert len(plugins) == 1, f"Expected one cuslide2 plugin, found {plugins}"
+ env = dict(
+ os.environ,
+ ENABLE_CUSLIDE2="1",
+ CUCIM_PLUGINS=os.path.basename(plugins[0]),
+ )
proc = subprocess.run(
[sys.executable, "-c", _CHECK, str(path)],
env=env,
Regression coverage
Run with an inherited legacy CUCIM_PLUGINS value and verify that the child still selects cuslide2. Remove a cuslide2 codec dependency and confirm the new tests fail instead of falling back to the legacy plugin. Keep the normal clean-environment case.
</details>
AI-assisted review comments for findings P2-1 and P2-2.
Hi, @gforsyth. Could you please review conda-package-related change here?
Thanks!
Approved these changes.
Addressed in df1d5840. The helper now uses find_spec("cucim") to locate the installed package. It checks both the wheel and Conda plugin locations and sets up plugin selection before the first cuCIM import, so the default download command selects cuslide2 when the native library loads.
Addressed in df1d5840. The test launcher now sets CUCIM_PLUGINS to the absolute path of the discovered cuslide2 library in the child environment. This replaces any inherited legacy plugin selection. Discovery covers both wheel and Conda installs.
Thank you @grlee77 for the change!
It looks good to me!
/merge
gforsyth
grlee77
Summary
closes #1211
With cuCIM installed from PyPI, the cuslide2 plugin (
ENABLE_CUSLIDE2=1) does not work as theREADME describes:
1. libnvimgcodec is not found.
CuImage(path)raisesdlopen libnvimgcodec.so failed!forevery TIFF, although
nvidia-nvimgcodec-cu13is installed. That wheel putslibnvimgcodec.so.0in
site-packages/nvidia/nvimgcodec/. The plugin only callsdlopenwith bare library names,and its RPATH (rewritten by
auditwheeltocucim_cu13.libs) does not include that directory.2. nvJPEG2000 and nvTIFF are not installed. cuCIM requires the nvImageCodec wheel without its
optional extras. When nvImageCodec cannot load nvTIFF and nvJPEG2000 ("Failed to load nvtiff
library!"), cuslide2 reads the wrong pyramid for every TIFF (extra levels for the macro,
thumbnail and label images; tile sizes of
(0, 0)), returns all-zero regions without anerror for Aperio JPEG 2000 (compression 33003) and standard JPEG 2000 (34712) images, and
raises an error for Aperio RGB JPEG 2000 (33005).
3. The README and the test scripts do not work with pip installs, and the scripts report success
on zero-filled images.
Tested with
cucim-cu13==26.10.0from PyPI in a fresh virtual environment (Python 3.13,nvidia-nvimgcodec-cu13==0.9.0.20), and with the nightly26.12.0a34wheel, which behaves the sameway. With
nvidia-nvimgcodec-cu13[nvjpeg2k,nvtiff]and the library directory onLD_LIBRARY_PATH,the same files are read correctly (the same pyramids, tile sizes and region means as the legacy
plugin).
Changes
1. Depend on the nvImageCodec extras (
dependencies.yaml, regeneratedpyproject.toml):nvidia-nvimgcodec-cu12/-cu13[nvjpeg,nvjpeg2k,nvtiff]>=0.9.0,<0.10.0. Constraints files cannotcontain extras, so they keep the plain requirement.
2. Find libnvimgcodec in the pip package (
nvimgcodec_wrap.cc). The default search (system,conda,
LD_LIBRARY_PATH) is tried first, as before. If it fails, the plugin also tries<directory of the plugin>/../../nvidia/nvimgcodec/, which is where the pip package is relativeto
site-packages/cucim/clara/. The error message now lists every path that was tried and namesthe pip and conda packages. The warning about missing TIFF tags now mentions the nvTIFF and
nvJPEG2000 libraries.
3. Tests on synthetic images (
python/cucim/tests/unit/clara/test_cuslide2_plugin.py).Pyramidal, tiled RGB TIFF files are written with tifffile (Deflate, LZW, JPEG, JPEG 2000), so no
sample data is downloaded. In a new process with
ENABLE_CUSLIDE2=1, each test checks the levelcount, level dimensions and tile sizes, and that regions read on the CPU and on the GPU match the
source image (exactly for the lossless codecs, within a small mean error for the lossy ones) and
are not all zero. The tests are skipped only if the package has no cuslide2 plugin. If it has
one, a missing nvImageCodec, nvTIFF or nvJPEG2000 library is a test failure.
4. README and scripts. The README lists the requirements for cuslide2 with pip.
scripts/test_common.pyused only the plugin directory of a source build (so the scripts failedwith pip and conda installs); it now falls back to the installed package. The scripts now fail on
tile sizes of
(0, 0)(before: "skipping caching assertions") and on all-zero decoded tiles(before, GPU and CPU both returned zeros and matched).
Testing
nvJPEG libraries. With nvJPEG2000 and nvTIFF removed from the library path, they fail: no TIFF
tags are returned, and
read_regionraisesFailed to decode IFD[0] with nvImageCodec.nvimgcodec_wrap.ccwas compiled with-Wall -Wextra -Werrorinto a shared object placed in a layout likesite-packages/cucim/clara/next to
site-packages/nvidia/nvimgcodec/(from thenvidia-nvimgcodec-cu13wheel), with noLD_LIBRARY_PATH. It loads the library through the new fallback, and with thenvidiadirectoryremoved it fails with the message that lists the paths.
Not in this PR
nvImageCodec reports success after the per-tile failures. The extras remove the cause for pip
installs; checking that a decoder is registered before decoding is left for a later change.