Fix NUTS under NumPy >= 2.4 and relax the numpy cap to <2.5 - #773
Open
Radiationsafety wants to merge 1 commit into
Open
Radiationsafety wants to merge 1 commit into
Radiationsafety wants to merge 1 commit into
Conversation
NumPy 2.4 turns int() conversion of 1-element arrays into a TypeError
(previously only a DeprecationWarning). The NUTS slice and U-turn
indicators in cuqi/sampler/_hmc.py hit exactly this pattern whenever the
target logpdf returns shape-(1,) arrays (e.g. GMRF posteriors with
array-valued logpdfs):
TypeError: only 0-dimensional arrays can be converted to Python scalars
_hmc.py, line 375 in _BuildTree: n_prime = int(log_u <= Ham_prime)
- Wrap the three affected conversions (slice acceptance, U-turn check in
the recursive _BuildTree and in the stop criterion) with np.squeeze(),
which maps both true scalars and shape-(1,) arrays to 0-d before int().
Semantics are unchanged on older NumPy versions.
- requirements.txt: relax the upper bound 'numpy<=2.2.0' -> 'numpy<2.5'
so that NumPy 2.3/2.4 environments can resolve CUQIpy.
Validation (bssunfold BSS-unfolding integration, seeded chains): with
this patch, PCN / NUTS / hierarchical-Gibbs posterior spectra computed
under numpy 2.4.1 are bit-for-bit identical to upstream CUQIpy 1.5.1
under numpy 2.2.0 (max relative difference 0.0); without the patch NUTS
raises the TypeError above.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
NumPy 2.4 turns
int()conversion of 1-element (shape-(1,)) arrays into aTypeError(previously only aDeprecationWarning). The NUTS slice and U-turn indicators incuqi/sampler/_hmc.pyhit exactly this pattern whenever the targetlogpdfreturns shape-(1,)arrays (e.g. GMRF posteriors built fromJointDistribution):Changes
cuqi/sampler/_hmc.py— wrap the three affected conversions withnp.squeeze(), which maps both true scalars and shape-(1,)arrays to 0-d beforeint():n_prime = int(np.squeeze(log_u <= Ham_prime))_BuildTreeSemantics are unchanged on all older NumPy versions (
np.squeezeof a scalar is a 0-d array;int()on 0-d arrays is legal in every NumPy 2.x).requirements.txt— relax the upper boundnumpy<=2.2.0->numpy<2.5so NumPy 2.3/2.4 environments can resolve CUQIpy. No other runtime dependency (jax is not pulled in by the core requirements) pins numpy below 2.3.Validation
Differential test through the bssunfold BSS-unfolding integration (
solve_cuqi_bayesian, customcuqi.model.ModelforwardA @ exp(theta)with analytic gradient, GMRF/OU priors, likelihood,Posterior+PCN/NUTS/HybridGibbs):i.e. with this patch, identical seeded chains under numpy 2.4.1 reproduce the upstream-CUQIpy-on-numpy 2.2.0 results bit-for-bit; without the patch NUTS raises the
TypeErrorabove.Happy to split the cap relaxation into a separate PR if the maintainers prefer to review/verify the version bump independently of the bug fix.