Skip to content

Follow-up fixes for numpy2 compatibility - #3508

Merged
C-Achard merged 10 commits into
devfrom
cy/fix-numpy2-unpin-issues
Sep 14, 2026
Merged

C-Achard merged 10 commits into
devfrom
cy/fix-numpy2-unpin-issues

Conversation

@C-Achard

Copy link
Copy Markdown
Collaborator

Motivation

Preparation for #3499, which removes the numpy<2 and matplotlib<3.9 upper bounds.

Changes

  • Fixed Assembly.affinity type to be float consistently
    Without, poses[i, j, :, 4] = assembly.affinity raises TypeError: can't assign a numpy.float32 to a torch.FloatTensor under NumPy 2, on the default multi-animal inference path.
    • Added regression tests
  • Updated _add_numpy_to_torch_safe_globals
    numpy.core became numpy._core in NumPy 2, so the import failed. Now tries both module paths and logs a warning.
  • Dependencies
    • numba>=0.60 (pyproject.toml)
      numba 0.54 requires numpy<1.21, below our own numpy>=1.22.4. 0.60 is also the first release supporting NumPy 2.
    • Declare opencv-python-headless>=4.10.0.84 (pyproject.toml)
      cv2 is imported across deeplabcut/ but was not declared, it arrived only transitively via albumentations, imgaug and fmpose3d, so which build you got depended on which extras you installed.

Add NumPy 2.0 compatibility for PyTorch snapshot loading by registering the correct scalar reconstruction helper and keeping NumPy float metrics loadable without changing weights_only. If registration fails, emit a warning explaining the fallback and the impact on snapshot loading.
Add explicit float typing and return values for `Assembly.affinity`, and cast the affinity value before storing it in PyTorch pose tensors. This keeps the inference path type-consistent and avoids integer zero values when no links are present.
Expand the snapshot-loading helper docstring to explain why NumPy reconstructors must be allowlisted for legacy metrics, note the NumPy version split more clearly, and mention that newer Torch releases may make the workaround unnecessary.
- opencv was used but never declared as a dependency
- numba .054 is not compatible with our numpy lower bound now
Correct the `numba` dependency in `pyproject.toml` from `>=0.6` to `>=0.60` so the declared minimum version matches the intended release series.
@C-Achard C-Achard self-assigned this Sep 11, 2026
@C-Achard C-Achard added bug fix dependencies Pull requests that update a dependency file labels Sep 11, 2026

Copilot AI 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.

🟡 Changes recommended

Three moderate review findings remain unresolved.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR improves NumPy 2 compatibility for inference, snapshot loading, and dependencies.

Changes:

  • Normalizes assembly affinities to Python floats.
  • Adds NumPy 1.x/2.x safe-global import support.
  • Updates Numba, OpenCV, and lockfile dependencies.
  • Adds affinity regression tests.
File summaries
File Summary
uv.lock Updates locked dependency metadata.
tests/pose_estimation_pytorch/models/predictors/test_paf.py Adds affinity regression tests; moderate finding (3 votes): the test does not reproduce the production failure.
pyproject.toml Updates Numba and OpenCV requirements; moderate finding (1 vote): OpenCV providers may conflict.
deeplabcut/pose_estimation_pytorch/runners/base.py Adds NumPy import fallback; moderate finding (1 vote): missing focused regression coverage.
deeplabcut/pose_estimation_pytorch/models/predictors/paf_predictor.py Safely assigns affinity values to tensors.
deeplabcut/core/inferenceutils.py Normalizes Assembly.affinity to float.
Review details

Suppressed comments (2)

deeplabcut/pose_estimation_pytorch/runners/base.py:227

  • The new NumPy 2 import path and fallback are the compatibility fix, but the runner tests do not exercise either path or verify that a legacy snapshot containing a NumPy scalar loads with weights_only=True. Add a focused regression test for the supported NumPy paths and the safe-global registration so this import can’t silently regress.
        try:
            from numpy._core.multiarray import scalar  # NumPy >= 2.0
        except ImportError:
            from numpy.core.multiarray import scalar  # NumPy < 2.0

pyproject.toml:44

  • opencv-python-headless and opencv-python install the same cv2 package, but the tf extras pull opencv-python through imgaug and fmpose3d pulls it directly (as reflected in uv.lock). Installing deeplabcut[tf] or [fmpose3d] therefore resolves both variants, making the resulting OpenCV build depend on installation order; make the providers mutually exclusive or standardize on one variant and regenerate the lockfile.
  "opencv-python-headless>=4.10.0.84",
  • Files reviewed: 5/6 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tests/pose_estimation_pytorch/models/predictors/test_paf.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

@deruyter92 deruyter92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Minor suggestions. Thanks for the fix!!

Comment thread deeplabcut/pose_estimation_pytorch/models/predictors/paf_predictor.py Outdated
Comment thread pyproject.toml
Co-authored-by: Jaap de Ruyter van Steveninck <32810691+deruyter92@users.noreply.github.com>
@deruyter92
deruyter92 removed this pull request from stack #3515 September 14, 2026 11:38
@deruyter92
deruyter92 added this pull request to stack #3516 September 14, 2026 11:44
@C-Achard
C-Achard merged commit 96f0c44 into dev Sep 14, 2026
29 of 36 checks passed
@C-Achard
C-Achard deleted the cy/fix-numpy2-unpin-issues branch September 14, 2026 13:30
C-Achard added a commit that referenced this pull request Sep 14, 2026
* release upper bounds for numpy and matplotlib

* keep np upper bound for tensorflow extras

* add numpy-1 to canary lane

* Fix intelligent testing matrix JSON

Add the missing comma between the `matplotlib` and `numpy-1` matrix entries in `.github/workflows/intelligent-testing.yml` so the GitHub Actions matrix parses correctly.

* Expand intelligent test matrix

Broaden the intelligent-testing workflow to include strict dependency-resolution checks for Python 3.10 and 3.12, plus NumPy 1/2 and Matplotlib upgrade variants. This improves compatibility coverage and validates resolver behavior beyond the previous narrower matrix.

* Adjust matplotlib and TF numpy constraints

Restore exclusion of matplotlib 3.7 and 3.7.1 from the base dependencies, and drop the redundant numpy cap from the `tf-cu12` extra because TensorFlow 2.18 already declares the required numpy range.

* Update uv.lock

* Fix missing ceiling in tf

* Update uv.lock

* update pyproject.toml and uv.lock. Only declare np upperbound when required

* Restore numba/opencv floors from #3508 after restack

---------

Co-authored-by: C-Achard <cyril.achard@epfl.ch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.0.2 bug fix dependencies Pull requests that update a dependency file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: can't assign a numpy.float32 to a torch.FloatTensor

3 participants