Skip to content

Conversation

aseyboldt
Copy link
Member

Description

The cholesky blas function returns an array with values stored in the triagonal part of the array that is zero by definition.

This is the equivalent of the scipy code here: https://github.com/scipy/scipy/blob/d5e1e03ac12fb5ad7fa4949e7ffea2137e780b15/scipy/linalg/flapack_pos_def.pyf.src#L132

fixes #814

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

@aseyboldt aseyboldt requested a review from jessegrabowski June 11, 2024 14:04

@pytest.mark.parametrize("lower", [True, False], ids=["lower=True", "lower=False"])
@pytest.mark.parametrize("trans", [True, False], ids=["trans=True", "trans=False"])
def test_numba_Cholesky_scipy(lower, trans):
Copy link
Member

Choose a reason for hiding this comment

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

Any reason why not to extend/reuse the test below?

Copy link
Member Author

Choose a reason for hiding this comment

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

No good one, fixed.

@ricardoV94 ricardoV94 added bug Something isn't working numba SciPy compatibility linalg Linear algebra labels Jun 11, 2024
Copy link
Member

@jessegrabowski jessegrabowski left a comment

Choose a reason for hiding this comment

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

Nice, good catch.

#814 was about originally about gradients, so perhaps we should test them explicitly?

@ricardoV94
Copy link
Member

Nice, good catch.

#814 was about originally about gradients, so perhaps we should test them explicitly?

I would say no, as long as someone checked the motivating example and it seems to work. The cases where bugs are found can be much more complex/expensive than the final regression tests?

@aseyboldt aseyboldt force-pushed the fix-numba-cholesky branch from 028c793 to 4d9b0a2 Compare June 11, 2024 14:21
@aseyboldt
Copy link
Member Author

I checked the original example.
I think the reason the problem only showed up in the gradient is that in the forward pass the values that should be zero were not used. The triagonal solve function just ignores what's in there, but I think in the backward pass we had a matrix mult or so which does not ignore those values.

Comment on lines 128 to 129
func = pytensor.function([cov], chol, mode="NUMBA")
np.testing.assert_allclose(func(val), scipy.linalg.cholesky(val, lower=lower))
Copy link
Member

Choose a reason for hiding this comment

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

Isn't compare_numba_and_py already checking numba and python implementations match?

Copy link
Member Author

Choose a reason for hiding this comment

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

It should, but didn't find the problem because compare_numba_and_py calls the functions without fast_run rewrites. That excluded Blockwise, and because that doesn't have a numba implementation it ended up calling the python code for the numba and the python version.

I included the rewrite that remove useless Blockwise ops into the set of rewrites we do in testing.
Maybe we should actually just use the default numba mode?

Copy link
Member

Choose a reason for hiding this comment

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

Ahh... okay so hopefully just a temporary blind spot. Let's keep it like you did now

@aseyboldt aseyboldt force-pushed the fix-numba-cholesky branch from 4d9b0a2 to f57bb55 Compare June 11, 2024 16:11
@aseyboldt aseyboldt force-pushed the fix-numba-cholesky branch from f57bb55 to b766b21 Compare June 11, 2024 16:20
@ricardoV94 ricardoV94 merged commit dbe0e09 into pymc-devs:main Jun 11, 2024
@ricardoV94 ricardoV94 changed the title fix(numba): cholesky did not set off-diag entries to zero Fix numba implementation of cholesky not setting off-diag entries to zero Jun 11, 2024
Copy link

codecov bot commented Sep 9, 2024

Codecov Report

Attention: Patch coverage is 12.50000% with 7 lines in your changes missing coverage. Please review.

Project coverage is 80.86%. Comparing base (ee0075b) to head (b766b21).
Report is 168 commits behind head on main.

Files with missing lines Patch % Lines
pytensor/link/numba/dispatch/slinalg.py 12.50% 7 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #816      +/-   ##
==========================================
+ Coverage   80.84%   80.86%   +0.01%     
==========================================
  Files         163      163              
  Lines       46890    46897       +7     
  Branches    11468    11473       +5     
==========================================
+ Hits        37909    37923      +14     
+ Misses       6768     6757      -11     
- Partials     2213     2217       +4     
Files with missing lines Coverage Δ
pytensor/link/numba/dispatch/slinalg.py 52.98% <12.50%> (+5.44%) ⬆️

... and 1 file with indirect coverage changes

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 SciPy compatibility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

numba: Incorrect gradient and typing error

3 participants