Skip to content

feat(segmentation): GPU-resident Cellpose-SAM eval + v4 API migration - #46

Merged
alxndrkalinin merged 10 commits into
mainfrom
feat/gpu-resident-cpsam
Jun 1, 2026
Merged

alxndrkalinin merged 10 commits into
mainfrom
feat/gpu-resident-cpsam

Conversation

@alxndrkalinin

@alxndrkalinin alxndrkalinin commented May 29, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds a GPU-resident Cellpose-SAM inference path to cubic — cubic.segmentation.segment_cpsam — so that, on a CUDA box, the input image is uploaded to the GPU exactly once and only the final integer mask returns to the host. Today stock CellposeModel.eval round-trips the image across PCIe several times (preprocessing on CPU, per-batch upload/download in the network forward, re-upload for dynamics, re-upload for flow QC). The new path keeps the flow field on the device end-to-end and is intended as cubic's default Cellpose-SAM segmentation method.

cellpose itself is not modified: its network and torch dynamics kernels are reused as-is; only the numpy/scipy/scikit-image/cv2/fastremap "glue" is replaced with cubic's device-agnostic wrappers (cubic.scipy → cupyx, cubic.skimage → cuCIM), bridging cupy↔torch zero-copy via the CUDA Array Interface.

Changes

v4 (SAM) dependency + wrapper migration

  • Bump cellpose>=4.0 (+ torch) in the cellpose extra; regenerate uv.lock (cellpose 4.1.1 + torch/torchvision).
  • Migrate cellpose_eval/cellpose_segment off the removed v3 API (Cellpose, model_type/omni/channels) to CellposeModel(pretrained_model="cpsam") + channel_axis.

GPU-resident eval

  • cuda.py: to_torch (cupy↔torch zero-copy) and ascupy_f32 (guards the silent bf16→|V2 corruption — cupy has no bfloat16); both lazy-import torch so base cubic still imports without it.
  • cellpose_sam_gpu.py: device-aware tiling/normalization/resize + forward, and the segment_cpsam orchestrator — GPU-only by contract (clear precondition). download=False (default) keeps dP/cellprob as CuPy; download=True returns the stock-eval tuple. Lists / 5D inputs are processed per image.
  • cellpose_dynamics.py: dynamics, flow QC, hole-fill/size-filter, and 3D stitching, all on the GPU. The flow-QC step falls back to cellpose's CPU path for very large masks (masks.size > 1e8 and insufficient free VRAM) to avoid OOM.

Validation

  • Parity vs stock CellposeModel.eval: average precision = 1.0 @ IoU 0.5/0.7/0.9 for 2D, 3D, and stitch on synthetic data (100% foreground agreement).
  • Residency: a spy on host transfers confirms exactly one device→host transfer (the mask) with download=False; dP/cellprob stay CuPy.
  • Full suite: 306 passed; 11 SAM tests (API/version guard, import-safety without optional deps, GPU-only precondition, CPU building-blocks, 2D/3D/stitch/list parity, residency). ruff + ruff-format + mypy clean.

Notes / limitations

  • The orchestrator is GPU-only by design; the device-agnostic building blocks still run on NumPy and are tested on CPU.
  • tile_norm/sharpen normalization sub-paths raise NotImplementedError for now (non-default; standard percentile normalization is supported).
  • Numerical parity is "close enough", not bit-exact (cuCIM order=0 vs cv2 INTER_NEAREST differ at half-pixel ties; validated by IoU/AP).
  • The uv.lock diff also folds in pre-existing examples-extra (jupyter) drift that was already uncommitted on main.

🤖 Generated with Claude Code

alxndrkalinin and others added 2 commits May 29, 2026 09:12
…l API

Cellpose 4 removed the `Cellpose` class and the `model_type`/`omni`/
`channels` arguments. Rewrite `cellpose_eval`/`cellpose_segment` to use
`CellposeModel(gpu=True, pretrained_model="cpsam")` with `channel_axis`,
and bump the cellpose extra to `>=4.0` (plus `torch`, needed by the SAM
stack). Regenerate uv.lock accordingly (cellpose 4.1.1 + torch/torchvision).

The wrapper's import-error test is made deterministic by monkeypatching
`_CELLPOSE_AVAILABLE`, so it exercises the guard regardless of whether
cellpose is installed in the running environment.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…_resident)

Add a GPU-resident inference path for Cellpose-SAM that uploads the input
image to the GPU exactly once and returns only the final integer mask to
the host. The cellpose network and its torch dynamics kernels are reused
unchanged; the numpy/scipy/scikit-image/cv2/fastremap glue is replaced
with cubic's device-agnostic wrappers (cubic.scipy -> cupyx, cubic.skimage
-> cuCIM), so the flow field never leaves the device.

- cuda.py: `to_torch` (cupy<->torch zero-copy via the CUDA Array Interface)
  and `ascupy_f32` (guards the silent bfloat16 -> |V2 corruption, since
  cupy has no bf16); both lazy-import torch so base cubic still imports.
- cellpose_sam_gpu.py: device-aware tiling/normalization/resize + forward
  (run_net/run_3D), and the `segment_cpsam_resident` orchestrator. GPU-only
  by contract (clear precondition); `download=False` keeps dP/cellprob as
  CuPy, `download=True` returns the stock-eval tuple. Lists/5D inputs are
  processed per image.
- cellpose_dynamics.py: dynamics, flow QC, hole-fill/size-filter and 3D
  stitching, all on the GPU.

Validated against stock CellposeModel.eval: average precision = 1.0 at
IoU 0.5/0.7/0.9 for 2D, 3D and stitch on synthetic data; residency test
confirms a single device->host transfer. tile-norm/sharpen normalization
sub-paths raise NotImplementedError for now.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds a GPU-resident Cellpose-SAM segmentation path that keeps all intermediate tensors on the GPU, introduces CuPy↔torch zero-copy bridge helpers, and migrates existing Cellpose wrappers/tests and dependencies to the v4 SAM API while preserving a CPU-safe fallback path.

Sequence diagram for GPU-resident Cellpose-SAM segmentation path

sequenceDiagram
    actor User
    participant segment_cpsam_resident
    participant CUDAManager
    participant CellposeModel
    participant _run_net_gpu
    participant compute_masks_resident

    User->>segment_cpsam_resident: call with model, x (image)
    segment_cpsam_resident->>CUDAManager: get_cp
    CUDAManager-->>segment_cpsam_resident: CuPy handle
    segment_cpsam_resident->>CellposeModel: access net, backbone, device
    segment_cpsam_resident->>segment_cpsam_resident: convert_image
    segment_cpsam_resident->>segment_cpsam_resident: normalize_img_gpu
    segment_cpsam_resident->>segment_cpsam_resident: ascupy (host→device upload once)

    segment_cpsam_resident->>_run_net_gpu: call with model.net, x
    _run_net_gpu->>_run_net_gpu: run_net_gpu / run_3D_gpu
    _run_net_gpu-->>segment_cpsam_resident: dP (CuPy), cellprob (CuPy), styles (CuPy)

    segment_cpsam_resident->>compute_masks_resident: call with dP, cellprob
    compute_masks_resident-->>segment_cpsam_resident: masks (CuPy)

    alt download=False
        segment_cpsam_resident->>segment_cpsam_resident: asnumpy(masks) (device→host once)
        segment_cpsam_resident-->>User: masks (NumPy), [None, dP, cellprob], styles (CuPy)
    else download=True
        segment_cpsam_resident->>segment_cpsam_resident: asnumpy(dP, cellprob, styles)
        segment_cpsam_resident-->>User: masks, [dx_to_circ(dP), dP, cellprob], styles (NumPy)
    end
Loading

Flow diagram for new cellpose_eval / cellpose_segment v4 SAM API

flowchart LR
    A[cellpose_segment] --> B[downscale_and_filter]
    B --> C[cellpose_eval]
    C --> D["CellposeModel(gpu=True, pretrained_model='cpsam')"]
    D --> E["model.eval(image, channel_axis, diameter, do_3D, batch_size, flow_threshold, cellprob_threshold)"]
    E --> F[masks]
    F --> G[remove_touching_objects]
    G --> H[clear_xy_borders]
    H --> I[final masks]
Loading

File-Level Changes

Change Details Files
Introduce CuPy↔torch bridge utilities to support GPU-resident flows and guard bfloat16 conversions.
  • Generalize ascupy to accept Any input and document dtype-agnostic behavior at the bridge.
  • Add to_torch to create torch tensors from CuPy/NumPy/tensors with lazy torch import and zero-copy when possible.
  • Add ascupy_f32 to safely convert torch tensors (including bfloat16) to CuPy by casting to float32 on-device before ascupy.
cubic/cuda.py
Migrate the public Cellpose wrapper to Cellpose v4 SAM API and expose a deterministic import-error path.
  • Update cellpose_eval/cellpose_segment to use CellposeModel with pretrained_model='cpsam', channel_axis, and new eval thresholds/batch arguments.
  • Adjust wrapper docstrings and parameter types from v3 (model_type/omni/channels) to v4 semantics.
  • Make the cellpose import-error test deterministic by monkeypatching _CELLPOSE_AVAILABLE instead of relying on environment state.
cubic/segmentation/cellpose.py
tests/segmentation/test_cellpose.py
Expose the new GPU-resident Cellpose-SAM orchestrator in the segmentation API and bump cellpose/torch optional deps.
  • Export segment_cpsam_resident and cellpose_eval from cubic.segmentation.init for public use.
  • Update the cellpose extra in pyproject.toml to require cellpose>=4.0 and torch, and regenerate uv.lock accordingly.
cubic/segmentation/__init__.py
pyproject.toml
uv.lock
Implement GPU-resident Cellpose-SAM network path that keeps all preprocessing, tiling, and forward passes on the GPU.
  • Reimplement resize/tiling/unaugment/averaging logic with device-aware wrappers (cubic.skimage, CuPy) to avoid cv2/NumPy-only ops.
  • Add run_net_gpu/run_3D_gpu/_run_net_gpu that mirror cellpose.core.run_net/run_3D/_run_net but operate on CuPy arrays, using to_torch/ascupy_f32 for zero-copy bridging and dtype-correct outputs.
  • Implement normalize_img_gpu to reuse cellpose transforms normalization on GPU and enforce NotImplemented for tile_norm/sharpen branches.
  • Add segment_cpsam_resident orchestrator that enforces a GPU-only precondition, handles list/5D inputs, normalization, SAM backbone-specific tiling, and returns either GPU-resident flows or a stock-eval-compatible tuple when download=True.
cubic/segmentation/cellpose_sam_gpu.py
Implement GPU-resident Cellpose dynamics, flow QC, size filtering, and stitching while keeping masks on the GPU.
  • Port steps_interp/follow_flows/get_masks_torch to use torch on GPU with CuPy inputs, replacing fastremap and CPU-only ops with CuPy/cucim equivalents.
  • Implement _masks_to_flows_gpu, flow_error, and remove_bad_flow_masks fully on GPU, including center finding and flow comparison.
  • Replace fill_holes/size-filter utilities with CuPy + cupyx.scipy.ndimage versions and implement 3D stitching via GPU IoU and relabeling.
  • Provide compute_masks_resident/resize_and_compute_masks orchestration that mirrors CellposeModel._compute_masks and returns a CuPy label array sized/rescaled to the original image shape.
cubic/segmentation/cellpose_dynamics.py
Add tests validating SAM v4 API exposure, CPU-safe imports, GPU-only preconditions, numeric parity with stock eval, and residency guarantees.
  • Ensure importing cubic.segmentation works without cellpose/cupy/torch and that GPU orchestrator raises clear ImportError/RuntimeError when prerequisites are missing.
  • Validate that the SAM model exposes v4-only attributes (backbone, net.dtype, CUDA device) required by the GPU-resident path.
  • Test that building-block functions (tiling, normalization) work on NumPy CPU arrays independently of the GPU orchestrator.
  • Compare resident vs stock CellposeModel.eval masks on synthetic data (single and list inputs) using AP@0.5, asserting close agreement.
  • Add a residency test that spies on asnumpy to confirm exactly one device→host transfer when download=False and that flows/styles remain on GPU.
tests/segmentation/test_cellpose_sam_gpu.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues, and left some high level feedback:

  • In segment_cpsam_resident, the list/5D-image handling recursively calls itself for each element while passing through many parameters; consider extracting a small internal helper for the per-image path to reduce duplication and risk of future parameter drift.
  • The GPU precondition checks are split between _require_cellpose, _check_gpu_precondition, and _cp; consolidating or clearly documenting their distinct responsibilities would make the error paths and invariants easier to reason about.
  • Some of the longer functions in cellpose_sam_gpu.py and cellpose_dynamics.py (e.g. _run_net_gpu, _resize_and_compute_masks) are quite dense; factoring out small, named helpers for repeated patterns (e.g. 2D vs 3D resize branches, normalization/parameter resolution) would improve readability and maintainability.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `segment_cpsam_resident`, the list/5D-image handling recursively calls itself for each element while passing through many parameters; consider extracting a small internal helper for the per-image path to reduce duplication and risk of future parameter drift.
- The GPU precondition checks are split between `_require_cellpose`, `_check_gpu_precondition`, and `_cp`; consolidating or clearly documenting their distinct responsibilities would make the error paths and invariants easier to reason about.
- Some of the longer functions in `cellpose_sam_gpu.py` and `cellpose_dynamics.py` (e.g. `_run_net_gpu`, `_resize_and_compute_masks`) are quite dense; factoring out small, named helpers for repeated patterns (e.g. 2D vs 3D resize branches, normalization/parameter resolution) would improve readability and maintainability.

## Individual Comments

### Comment 1
<location path="cubic/cuda.py" line_range="173-182" />
<code_context>
+def to_torch(array: Any, device: Any = None, dtype: Any = None) -> Any:
</code_context>
<issue_to_address>
**issue:** Avoid defaulting CuPy inputs to hard-coded 'cuda' device in to_torch.

In the CuPy→torch path, `device` currently falls back to the hard-coded string `'cuda'` when `None`. This can break when users run on non-default CUDA devices (e.g. `cuda:1`) or alternative backends (MPS/ROCm), and may diverge from `model.device`. Since `_forward_gpu` passes `device=net.device` but other callers might not, this can cause subtle cross-device issues. Prefer leaving `device=None` and letting `torch.as_tensor` infer, or derive the default from `array.device`/`model.device` instead of hard-coding `'cuda'`.
</issue_to_address>

### Comment 2
<location path="cubic/cuda.py" line_range="218" />
<code_context>
+    return tensor.to(dtype) if dtype is not None else tensor
+
+
+def ascupy_f32(array: Any) -> object:
+    """Move (or keep) an array on the GPU as a CuPy array, fp32-safe at the bridge.
+
+    Identical to :func:`ascupy` except that torch tensors are first cast to
+    ``float32`` **on-device**. This guards the silent-corruption failure mode
+    where ``cupy.asarray`` on a torch ``bfloat16`` tensor yields an opaque
+    ``|V2`` array (CuPy has no ``bfloat16``). Use at the network-output
+    boundary, where activations may be ``bfloat16``.
+
+    Non-torch inputs are forwarded to :func:`ascupy` unchanged.
+    """
+    if _is_torch_tensor(array):
+        import torch  # lazy
+
+        array = array.to(torch.float32)
+    return ascupy(array)
</code_context>
<issue_to_address>
**suggestion:** Consider matching the return type annotation of ascupy for ascupy_f32.

`ascupy_f32` currently returns `object`, while `ascupy` returns `Any`. Since this function is just a fp32-enforcing wrapper around `ascupy`, using `Any` here too would keep the signatures aligned and prevent downstream code from inferring an unnecessarily generic `object` type.

```suggestion
def ascupy_f32(array: Any) -> Any:
```
</issue_to_address>

### Comment 3
<location path="cubic/segmentation/cellpose_sam_gpu.py" line_range="353-362" />
<code_context>
+def run_3D_gpu(
</code_context>
<issue_to_address>
**suggestion:** Check that 3D style aggregation matches Cellpose expectations.

The 3D path currently initializes `style = None` and then replaces it with the `style` from only the last orthogonal sweep. In stock Cellpose, styles from multiple views/volumes are aggregated (e.g., averaged per-image). If callers expect `styles` to reflect all sweeps/planes, this change may subtly alter behavior. Please consider aggregating styles across the three permutations (e.g., taking the mean) instead of just using the final sweep’s style.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread cubic/cuda.py Resolved
Comment thread cubic/cuda.py Resolved
Comment thread cubic/segmentation/cellpose_sam_gpu.py Resolved
alxndrkalinin and others added 2 commits May 29, 2026 09:48
`_remove_bad_flow_masks` recomputes flows from the predicted masks on the
GPU. For very large masks that recomputation can exceed free VRAM, so
mirror cellpose's `dynamics.remove_bad_flow_masks` guard: when
`masks.size > 1e8` and the estimate (size*32) exceeds free GPU memory,
fall back to cellpose's CPU flow-QC (warn, route through asnumpy) instead
of OOM-ing. This is the one place a derived array may leave the GPU, and
only to avoid an out-of-memory error on a huge image.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codify the 3D (do_3D=True) and stitch (stitch_threshold>0) parity that was
previously only checked with throwaway scripts, so regressions in
run_3D_gpu / _stitch3D / the 3D mask path are caught. Both assert
average precision >= 0.95 @ IoU 0.5 vs stock CellposeModel.eval on small
synthetic fixtures and run in a few seconds on a GPU.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a GPU-resident Cellpose-SAM (cellpose v4) evaluation/segmentation path to reduce CPU↔GPU transfers, while migrating existing Cellpose integration from the removed v3 API to the v4 CellposeModel API and expanding test coverage around optional dependencies and GPU-only contracts.

Changes:

  • Migrate cubic.segmentation.cellpose wrappers to Cellpose v4 (CellposeModel, pretrained_model, channel_axis, updated thresholds/batch args).
  • Introduce GPU-resident inference + GPU-resident dynamics/postprocessing modules (cellpose_sam_gpu.py, cellpose_dynamics.py) and export the new orchestrator.
  • Add/adjust tests to be deterministic in optional-dep environments and to validate GPU residency and parity vs stock CellposeModel.eval; update cellpose optional deps.

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/segmentation/test_cellpose.py Makes import-error test deterministic via monkeypatch.
tests/segmentation/test_cellpose_sam_gpu.py Adds GPU-resident Cellpose-SAM API/behavior/residency/parity tests with clean skips when deps/GPU are missing.
pyproject.toml Updates cellpose extra to require cellpose v4 and torch.
cubic/segmentation/cellpose.py Migrates wrapper functions to v4 CellposeModel API and new eval parameters.
cubic/segmentation/cellpose_sam_gpu.py Adds GPU-resident tiling/normalize/resize + forward path and the segment_cpsam_resident orchestrator.
cubic/segmentation/cellpose_dynamics.py Adds GPU-resident dynamics, flow QC, hole fill/size filtering, and stitching.
cubic/segmentation/init.py Exposes cellpose_eval and segment_cpsam_resident from the public segmentation package.
cubic/cuda.py Adds CuPy↔torch zero-copy bridge (to_torch) and bf16-safe CuPy casting helper (ascupy_f32).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cubic/segmentation/cellpose.py Resolved
Comment thread cubic/segmentation/cellpose_sam_gpu.py Resolved
Comment thread cubic/segmentation/cellpose_dynamics.py Resolved
Comment thread cubic/segmentation/cellpose_dynamics.py Resolved
alxndrkalinin and others added 6 commits May 29, 2026 09:50
Drop the `_resident` suffix from the public entry point (and the internal
`compute_masks_resident` -> `compute_masks`); the docstrings already explain
the GPU-resident behavior. `segment_cpsam` is cubic's recommended default
Cellpose-SAM segmentation method going forward.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…32 as Any

- to_torch: when no device is given for a CuPy input, derive it from the
  array's own device (`cuda:{array.device.id}`) instead of a hard-coded
  "cuda", so arrays on cuda:1 (etc.) are not silently moved to cuda:0.
- ascupy_f32: return `Any` to match `ascupy` and avoid an unnecessarily
  generic `object` inference downstream.

Addresses Sourcery review on PR #46.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
`cellpose_eval` is exported from `cubic.segmentation` but, unlike
`cellpose_segment`, did not check `_CELLPOSE_AVAILABLE`. In a no-cellpose
environment it raised a `NameError` for `models` instead of a clear
`ImportError`. Add the same guard for a deterministic error message.

Addresses Copilot review on PR #46.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- _fill_holes_and_size_filter: assign through the slice view with
  `masks[slc] = np.where(filled, i+1, masks[slc])` instead of chained
  indexing `masks[slc][filled] = ...`, which is fragile; add a regression
  test that an interior hole is actually filled.
- _get_masks: cast `pt` to int64 for `torch.sparse_coo_tensor`, which
  requires LongTensor indices (`pt` arrives as int32 from `.int()`).

Addresses Copilot review on PR #46.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…n_net_gpu

CPSAM emits zero style vectors and stock `CellposeModel.eval` likewise
returns zeros (its style aggregation is disabled), so the per-tile
`stylea` buffer was computed and never used. Drop it; `styles` stays the
allocated zeros, matching stock behavior.

Addresses Copilot review on PR #46.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…PU path

The GPU-resident Cellpose-SAM path previously raised NotImplementedError
for the non-default normalizations (tile_norm_blocksize>0 and
sharpen_radius/smooth_radius>0), since they rely on cv2.resize,
scipy.ndimage, torch.from_numpy and np.array(list-of-arrays) on the host.

Port both leaves device-agnostically:
- _smooth_sharpen_img_gpu mirrors transforms.smooth_sharpen_img but bridges
  to torch via to_torch (zero-copy from CuPy), reuses cellpose's pure-torch
  gaussian_kernel, keeps the FFT body on torch, and writes into a fresh
  tensor so the input buffer is never mutated.
- _normalize99_tile_gpu mirrors transforms.normalize99_tile with xp.zeros
  buffers, xp.stack for the per-z lists (np.array does not dispatch on a
  list of CuPy arrays), cubic.scipy.ndimage.gaussian_filter1d, and resize_gpu
  for the percentile maps (channel-preserving, so the cv2 singleton re-add is
  dropped). Fixes the upstream norm3D=False/Lz>1 typo (x99 assigned from
  x01_rsz) that forces x99==x01 and always raises ZeroDivisionError.

normalize_img_gpu is restructured to apply sharpen first, then tile-norm or
the standard percentile path (still delegated to cellpose.transforms.normalize_img,
which dispatches on cupy unchanged); channel-last (axis=-1) is assumed. The
orchestrator passes device=model.device through to both normalize calls.

Add GPU parity tests vs stock CellposeModel.eval (tile-norm 2D, tile-norm 3D
with norm3D, sharpen 2D; all AP>=0.95 @ IoU 0.5) and extend the CPU
building-block test to exercise both leaves on NumPy.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@alxndrkalinin
alxndrkalinin merged commit 256cc9d into main Jun 1, 2026
9 checks passed
@alxndrkalinin
alxndrkalinin deleted the feat/gpu-resident-cpsam branch June 1, 2026 20:56
alxndrkalinin added a commit that referenced this pull request Jun 1, 2026
Resets the version after v0.7.0a8/a9/a10 tags were deleted before
publish. Covers PRs #42, #43, #44, #45, and #46 on top of v0.7.0a7.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
alxndrkalinin added a commit that referenced this pull request Jun 1, 2026
PyPI already holds cubic-0.7.0a8 from an earlier release and rejects
file-name reuse, so skip ahead to a9. Covers PRs #42-#46 on top of
v0.7.0a7.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.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 this pull request may close these issues.

2 participants