Skip to content

[minor] Record that pygranso 1.x requires numpy<2? #56

Description

@superstealthysheep

Summary

Since the last update of PyGRANSO, numpy 2 has released with breaking changes. The old requirements.txt files did not need to specify numpy<2 in the past, but now this is needed (seemingly at least for cuda functionality).

Minimal steps to reproduce:

1. repo setup

git clone https://github.com/sun-umn/PyGRANSO
cd PyGRANSO
git checkout 2a1efc2b4180e33f61c48f2fe44fa0680c569103 # specify exact commit for reproducibility
uv venv # or `python -m venv ./.venv`
source .venv/bin/activate

2a. test_cuda.py fails with requirements.txt but passes once we require numpy<2

On a cuda-enabled machine, we can try

uv pip install -r requirements.txt -f https://download.pytorch.org/whl/cu111/torch_stable.html
# ^ or `pip install -r requirements.txt -f https://download.pytorch.org/whl/cu111/torch_stable.html`
python test_cuda.py

which yields

(expand) error message
A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.2 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

Traceback (most recent call last):  File "/users/5/my_redacted_username/PyGRANSO/test_cuda.py", line 1, in <module>
    import torch
  File "/users/5/my_redacted_username/PyGRANSO/.venv/lib/python3.9/site-packages/torch/__init__.py", line 629, in <module>
    from .functional import *  # noqa: F403
  File "/users/5/my_redacted_username/PyGRANSO/.venv/lib/python3.9/site-packages/torch/functional.py", line 6, in <module>
    import torch.nn.functional as F
  File "/users/5/my_redacted_username/PyGRANSO/.venv/lib/python3.9/site-packages/torch/nn/__init__.py", line 1, in <module>
    from .modules import *  # noqa: F403
  File "/users/5/my_redacted_username/PyGRANSO/.venv/lib/python3.9/site-packages/torch/nn/modules/__init__.py", line 2, in <module>
    from .linear import Identity, Linear, Bilinear, LazyLinear
  File "/users/5/my_redacted_username/PyGRANSO/.venv/lib/python3.9/site-packages/torch/nn/modules/linear.py", line 6, in <module>
    from .. import functional as F
  File "/users/5/my_redacted_username/PyGRANSO/.venv/lib/python3.9/site-packages/torch/nn/functional.py", line 11, in <module>
    from .._jit_internal import boolean_dispatch, _overload, BroadcastingList1, BroadcastingList2, BroadcastingList3
  File "/users/5/my_redacted_username/PyGRANSO/.venv/lib/python3.9/site-packages/torch/_jit_internal.py", line 26, in <module>
    import torch.package._mangling as package_mangling
  File "/users/5/my_redacted_username/PyGRANSO/.venv/lib/python3.9/site-packages/torch/package/__init__.py", line 12, in <module>
    from .package_importer import PackageImporter
  File "/users/5/my_redacted_username/PyGRANSO/.venv/lib/python3.9/site-packages/torch/package/package_importer.py", line 26, in <module>
    from ._mock_zipreader import MockZipReader
  File "/users/5/my_redacted_username/PyGRANSO/.venv/lib/python3.9/site-packages/torch/package/_mock_zipreader.py", line 17, in <module>
    _dtype_to_storage = {data_type(0).dtype: data_type for data_type in _storages}
  File "/users/5/my_redacted_username/PyGRANSO/.venv/lib/python3.9/site-packages/torch/package/_mock_zipreader.py", line 17, in <dictcomp>
    _dtype_to_storage = {data_type(0).dtype: data_type for data_type in _storages}
/users/5/my_redacted_username/PyGRANSO/.venv/lib/python3.9/site-packages/torch/package/_mock_zipreader.py:17: UserWarning: Failed to initialize NumPy: _ARRAY_API not found (Triggered internally at  /pytorch/torch/csrc/utils/tensor_numpy.cpp:67.)
  _dtype_to_storage = {data_type(0).dtype: data_type for data_type in _storages}
Testing may take several minutes. Thank you for your patience.
Traceback (most recent call last):
  File "/users/5/my_redacted_username/PyGRANSO/pygranso/pygranso.py", line 509, in pygranso
    info = bfgssqp_obj.bfgssqp(penaltyfn_obj,bfgs_hess_inv_obj,opts,printer, torch_device)
  File "/users/5/my_redacted_username/PyGRANSO/pygranso/private/bfgssqp.py", line 249, in bfgssqp
    [stat_vec,self.stat_val,qps_solved, _, _] = self.computeApproxStationarityVector()
  File "/users/5/my_redacted_username/PyGRANSO/pygranso/private/bfgssqp.py", line 598, in computeApproxStationarityVector
    [grad_samples,dist_evals] = self.get_nearby_grads_fn()
  File "/users/5/my_redacted_username/PyGRANSO/pygranso/private/bfgssqp.py", line 247, in <lambda>
    self.get_nearby_grads_fn     = lambda : getNearbyGradients( self.penaltyfn_obj, get_nbd_grads_fn)
  File "/users/5/my_redacted_username/PyGRANSO/pygranso/private/bfgssqp.py", line 698, in getNearbyGradients
    [*_,grads_ret,dist_evals] = grad_nbd_fn(penaltyfn_obj.getX(), grads)
  File "/users/5/my_redacted_username/PyGRANSO/pygranso/private/neighborhoodCache.py", line 98, in <lambda>
    get_neighborhood_fn = lambda x,x_data: self.getCachedNeighborhoodAbout(x,x_data)
  File "/users/5/my_redacted_username/PyGRANSO/pygranso/private/neighborhoodCache.py", line 158, in getCachedNeighborhoodAbout
    indx            = (self.distances <= self.radius).cpu().numpy()
RuntimeError: Numpy is not available


 Test 1 fail, please carefully read the instructions on https://ncvx.org/ for installation

Traceback (most recent call last):
  File "/users/5/my_redacted_username/PyGRANSO/test_cuda.py", line 403, in <module>
    rosenbrock()
  File "/users/5/my_redacted_username/PyGRANSO/test_cuda.py", line 98, in rosenbrock
    soln = pygranso(var_spec= var_in, combined_fn = comb_fn, user_opts = opts)
  File "/users/5/my_redacted_username/PyGRANSO/pygranso/pygranso.py", line 523, in pygranso
    soln.termination_code       = info.termination_code
UnboundLocalError: local variable 'info' referenced before assignment

with package state

(expand) `pip freeze` result
anyio==4.10.0
argon2-cffi==25.1.0
argon2-cffi-bindings==25.1.0
arrow==1.3.0
asttokens==3.0.0
async-lru==2.0.5
attrs==25.3.0
babel==2.17.0
beautifulsoup4==4.13.4
bleach==6.2.0
certifi==2025.8.3
cffi==1.17.1
charset-normalizer==3.4.3
comm==0.2.3
debugpy==1.8.16
decorator==5.2.1
defusedxml==0.7.1
exceptiongroup==1.3.0
executing==2.2.0
fastjsonschema==2.21.1
fqdn==1.5.1
gurobipy==12.0.3
h11==0.16.0
httpcore==1.0.9
httpx==0.28.1
idna==3.10
importlib-metadata==8.7.0
ipykernel==6.30.1
ipython==8.18.1
isoduration==20.11.0
jedi==0.19.2
jinja2==3.1.6
joblib==1.5.1
json5==0.12.1
jsonpointer==3.0.0
jsonschema==4.25.0
jsonschema-specifications==2025.4.1
jupyter-client==8.6.3
jupyter-core==5.8.1
jupyter-events==0.12.0
jupyter-lsp==2.2.6
jupyter-server==2.16.0
jupyter-server-terminals==0.5.3
jupyterlab==4.4.5
jupyterlab-pygments==0.3.0
jupyterlab-server==2.27.3
lark==1.2.2
markupsafe==3.0.2
matplotlib-inline==0.1.7
mistune==3.1.3
nbclient==0.10.2
nbconvert==7.16.6
nbformat==5.10.4
nest-asyncio==1.6.0
notebook==7.4.5
notebook-shim==0.2.4
numpy==2.0.2
osqp==1.0.4
overrides==7.7.0
packaging==25.0
pandocfilters==1.5.1
parso==0.8.4
pexpect==4.9.0
pillow==11.3.0
platformdirs==4.3.8
prometheus-client==0.22.1
prompt-toolkit==3.0.51
psutil==7.0.0
ptyprocess==0.7.0
pure-eval==0.2.3
pycparser==2.22
pygments==2.19.2
python-dateutil==2.9.0.post0
python-json-logger==3.3.0
pyyaml==6.0.2
pyzmq==27.0.1
referencing==0.36.2
requests==2.32.4
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rfc3987-syntax==1.1.0
rpds-py==0.27.0
scipy==1.13.1
send2trash==1.8.3
setuptools==80.9.0
six==1.17.0
sniffio==1.3.1
soupsieve==2.7
stack-data==0.6.3
terminado==0.18.1
tinycss2==1.4.0
tomli==2.2.1
torch==1.9.0+cu111
torchaudio==0.9.0
torchvision==0.10.0+cu111
tornado==6.5.2
traitlets==5.14.3
types-python-dateutil==2.9.0.20250809
typing-extensions==4.14.1
uri-template==1.3.0
urllib3==2.5.0
wcwidth==0.2.13
webcolors==24.11.1
webencodings==0.5.1
websocket-client==1.8.0
zipp==3.23.0

Adding numpy<2 to the reqs file allows test_cuda.py to run correctly.
i.e. change line: numpy >= 1.20.3 -> numpy >= 1.20.3, < 2

% uv pip install -r requirements.txt -f https://download.pytorch.org/whl/cu111/torch_stable.html 
Resolved 106 packages in 86ms
Uninstalled 1 package in 4.57s
Installed 1 package in 2.91s
 - numpy==2.0.2
 + numpy==1.26.4

python test_cuda.py then yields

(expand) passing test output
Testing may take several minutes. Thank you for your patience.
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
test 1/7 passed (Rosenbrock)
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
test 2/7 passed (Spectral Radius Optimization)
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
test 3/7 passed (Dictionary Learning)
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
test 4/7 passed (Robust PCA)
test 5/7 passed (LASSO)
test 6/7 passed (Feasibility Problem)
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
test 7/7 passed (Sphere Manifold)
Successfully passed all tests!

2b. test_cpu.py passes with requirements_cpu.txt even for numpy==2.0.2

First, we actually need to add gurobipy to requirements_cpu.txt. Then, with a fresh venv, we can try

uv pip install -r requirements_cpu.txt
# ^ or `pip install -r requirements_cpu.txt`
python test_cpu.py

which runs and passes the tests, even with numpy>2

(expand) passing test output
Testing may take several minutes. Thank you for your patience.
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
test 1/7 passed (Rosenbrock)
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
test 2/7 passed (Spectral Radius Optimization)
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
test 3/7 passed (Dictionary Learning)
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
test 4/7 passed (Robust PCA)
test 5/7 passed (LASSO)
test 6/7 passed (Feasibility Problem)
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
Polishing not needed - no active set detected at optimal point
test 7/7 passed (Sphere Manifold)
Successfully passed all tests!

with package state

(expand) `pip freeze` result
anyio==4.10.0
argon2-cffi==25.1.0
argon2-cffi-bindings==25.1.0
arrow==1.3.0
asttokens==3.0.0
async-lru==2.0.5
attrs==25.3.0
babel==2.17.0
beautifulsoup4==4.13.4
bleach==6.2.0
certifi==2025.8.3
cffi==1.17.1
charset-normalizer==3.4.3
comm==0.2.3
debugpy==1.8.16
decorator==5.2.1
defusedxml==0.7.1
exceptiongroup==1.3.0
executing==2.2.0
fastjsonschema==2.21.1
filelock==3.18.0
fqdn==1.5.1
fsspec==2025.7.0
gurobipy==12.0.3
h11==0.16.0
httpcore==1.0.9
httpx==0.28.1
idna==3.10
importlib-metadata==8.7.0
ipykernel==6.30.1
ipython==8.18.1
isoduration==20.11.0
jedi==0.19.2
jinja2==3.1.6
joblib==1.5.1
json5==0.12.1
jsonpointer==3.0.0
jsonschema==4.25.0
jsonschema-specifications==2025.4.1
jupyter-client==8.6.3
jupyter-core==5.8.1
jupyter-events==0.12.0
jupyter-lsp==2.2.6
jupyter-server==2.16.0
jupyter-server-terminals==0.5.3
jupyterlab==4.4.5
jupyterlab-pygments==0.3.0
jupyterlab-server==2.27.3
lark==1.2.2
markupsafe==3.0.2
matplotlib-inline==0.1.7
mistune==3.1.3
mpmath==1.3.0
nbclient==0.10.2
nbconvert==7.16.6
nbformat==5.10.4
nest-asyncio==1.6.0
networkx==3.2.1
notebook==7.4.5
notebook-shim==0.2.4
numpy==2.0.2
nvidia-cublas-cu12==12.8.4.1
nvidia-cuda-cupti-cu12==12.8.90
nvidia-cuda-nvrtc-cu12==12.8.93
nvidia-cuda-runtime-cu12==12.8.90
nvidia-cudnn-cu12==9.10.2.21
nvidia-cufft-cu12==11.3.3.83
nvidia-cufile-cu12==1.13.1.3
nvidia-curand-cu12==10.3.9.90
nvidia-cusolver-cu12==11.7.3.90
nvidia-cusparse-cu12==12.5.8.93
nvidia-cusparselt-cu12==0.7.1
nvidia-nccl-cu12==2.27.3
nvidia-nvjitlink-cu12==12.8.93
nvidia-nvtx-cu12==12.8.90
osqp==1.0.4
overrides==7.7.0
packaging==25.0
pandocfilters==1.5.1
parso==0.8.4
pexpect==4.9.0
pillow==11.3.0
platformdirs==4.3.8
prometheus-client==0.22.1
prompt-toolkit==3.0.51
psutil==7.0.0
ptyprocess==0.7.0
pure-eval==0.2.3
pycparser==2.22
pygments==2.19.2
python-dateutil==2.9.0.post0
python-json-logger==3.3.0
pyyaml==6.0.2
pyzmq==27.0.1
referencing==0.36.2
requests==2.32.4
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rfc3987-syntax==1.1.0
rpds-py==0.27.0
scipy==1.13.1
send2trash==1.8.3
setuptools==80.9.0
six==1.17.0
sniffio==1.3.1
soupsieve==2.7
stack-data==0.6.3
sympy==1.14.0
terminado==0.18.1
tinycss2==1.4.0
tomli==2.2.1
torch==2.8.0
torchaudio==2.8.0
torchvision==0.23.0
tornado==6.5.2
traitlets==5.14.3
triton==3.4.0
types-python-dateutil==2.9.0.20250809
typing-extensions==4.14.1
uri-template==1.3.0
urllib3==2.5.0
wcwidth==0.2.13
webcolors==24.11.1
webencodings==0.5.1
websocket-client==1.8.0
zipp==3.23.0

I briefly attempted to test this with requirements_linux_cpu.txt, but I had issues finding the specified torch version, so I decided it wasn't worth the effort to test.

Possible action?

Would it make sense to patch the requirements.txt files to include numpy<2 or otherwise record this requirement?


Minor: additional info about the original error I experienced

In my particular (non-minimal example) case, I was actually getting a slightly different error involving ImportError: cannot import name 'Inf' from 'numpy.core.numeric' (/users/5/my_redacted_username/.conda/envs/my_redacted_env_name/lib/python3.9/site-packages/numpy/core/numeric.py), perhaps because I had mixed numpy versions? Idk, conda weirdness.

I was able to blame to find the exact numpy commit responsible for my error, which was in the numpy.core.numeric API at commit numpy/numpy@67d8d2c. In this commit, Inf was renamed to inf among other changes. I switched from conda to uv, then tried adding numpy<2 to my requirements.txt, and this fixed the error. I did not investigate any further, and there very well could be other spots broken by numpy>=2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions