Skip to content

Fix numba dispatch not returning arrays or wrong dtypes #1406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 23, 2025

Conversation

ricardoV94
Copy link
Member

@ricardoV94 ricardoV94 commented May 20, 2025

This lead to failures in statespace models, as the Elemwise raises if the inputs are not arrays.

Closes pymc-devs/pymc-extras#476


📚 Documentation preview 📚: https://pytensor--1406.org.readthedocs.build/en/1406/

@ricardoV94 ricardoV94 added bug Something isn't working numba linalg Linear algebra labels May 20, 2025
return np.testing.assert_allclose(x, y, rtol=1e-4) and compare_shape_dtype(
x, y
)
np.testing.assert_allclose(x, y, rtol=1e-4, strict=True)
Copy link
Member Author

Choose a reason for hiding this comment

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

strict=True covers the shape/dtype mismatch

Copy link
Member Author

@ricardoV94 ricardoV94 May 20, 2025

Choose a reason for hiding this comment

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

The old logic with return x and y, didn't trigger y (the compare_shape_dtype), because the assert eithers fails or returns None which is Falsy. We don't do anything with the output of this return which again is always Falsy if it doesn't fail

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where the numba dispatch for Det and SLogDet was returning non-array outputs, which was causing failures in statespace models.

  • Consolidated tests for both Det and SLogDet with parameterized inputs and dtypes.
  • Updated the numba dispatch functions to wrap scalar outputs in NumPy arrays with type conversion.
  • Refined the assertion checks in tests to ensure both outputs are consistently NumPy arrays.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
tests/link/numba/test_nlinalg.py Consolidated test cases for Det and SLogDet with parametrize for dtype and op.
tests/link/numba/test_basic.py Modified the assertion function to ensure output array consistency using strict checks.
pytensor/link/numba/dispatch/nlinalg.py Updated Det and SLogDet dispatch functions to return NumPy arrays with proper type conversion.

@@ -52,7 +52,7 @@ def numba_funcify_Det(op, node, **kwargs):

@numba_basic.numba_njit(inline="always")
def det(x):
return numba_basic.direct_cast(np.linalg.det(inputs_cast(x)), out_dtype)
return np.array(np.linalg.det(inputs_cast(x))).astype(out_dtype)
Copy link
Preview

Copilot AI May 20, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider adding a comment explaining why wrapping the output with np.array is necessary to ensure consistency in array outputs, which aids in maintainability.

Copilot uses AI. Check for mistakes.

@ricardoV94
Copy link
Member Author

The new tests show other Ops that were failing to respect dtypes. For instance Cast is not working for downcasts

@ricardoV94 ricardoV94 force-pushed the fix_numba_slogdet_dispatch branch from 1657d4a to 594d433 Compare May 20, 2025 13:15
@ricardoV94 ricardoV94 changed the title Fix numba dispatch of Det and SlogDet returning non-arrays Fix numba dispatch not returning arrays or wrong dtypes May 20, 2025
@ricardoV94 ricardoV94 force-pushed the fix_numba_slogdet_dispatch branch from 594d433 to 614ffdd Compare May 20, 2025 13:23
Copy link

codecov bot commented May 20, 2025

Codecov Report

Attention: Patch coverage is 52.63158% with 9 lines in your changes missing coverage. Please review.

Project coverage is 82.11%. Comparing base (5ffe17a) to head (bff6d08).
Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
pytensor/link/numba/dispatch/scalar.py 46.66% 8 Missing ⚠️
...sor/link/numba/dispatch/linalg/decomposition/lu.py 0.00% 1 Missing ⚠️

❌ Your patch status has failed because the patch coverage (52.63%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##             main    #1406    +/-   ##
========================================
  Coverage   82.10%   82.11%            
========================================
  Files         208      211     +3     
  Lines       49576    49686   +110     
  Branches     8791     8813    +22     
========================================
+ Hits        40704    40798    +94     
- Misses       6699     6710    +11     
- Partials     2173     2178     +5     
Files with missing lines Coverage Δ
pytensor/link/numba/dispatch/basic.py 79.54% <100.00%> (-0.84%) ⬇️
pytensor/link/numba/dispatch/elemwise.py 94.82% <100.00%> (ø)
pytensor/link/numba/dispatch/nlinalg.py 100.00% <100.00%> (ø)
...sor/link/numba/dispatch/linalg/decomposition/lu.py 66.66% <0.00%> (ø)
pytensor/link/numba/dispatch/scalar.py 90.28% <46.66%> (-4.13%) ⬇️

... and 8 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ricardoV94
Copy link
Member Author

According to the numba devs the dtypes are correct but python converts them back to float / integer when we get out of numba. Will tweak the tests to not consider it an xfail then, although it would be better if numba returned numpy scalars

@jessegrabowski
Copy link
Member

it would be better if numba returned numpy scalars

is that a problem on our side or numba's side?

@ricardoV94
Copy link
Member Author

It's numba behavior. It doesn't return numpy scalars from jitted functions.

import numpy as np
import numba

@numba.njit
def f(x):
    return x + 1

x = np.int32(0)
y = f(x)
type(y), type(x + 1)  # (int, numpy.int32)

@jessegrabowski jessegrabowski merged commit 261aaf3 into pymc-devs:main May 23, 2025
72 of 73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working linalg Linear algebra numba
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Statespace doesn't work in Numba mode
2 participants