Skip to content
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

Buggy Accelerate Backend when using numpy 1.19 #15947

Closed
billlyzhaoyh opened this issue Apr 9, 2020 · 48 comments
Closed

Buggy Accelerate Backend when using numpy 1.19 #15947

billlyzhaoyh opened this issue Apr 9, 2020 · 48 comments

Comments

@billlyzhaoyh
Copy link

Numpy 1.19 cannot import due to run time error in MacOS 10.14.6. Polyfit sanity test emitted a warning, most likely due to using a buggy Accelerate backend. If you compiled yourself, see site.cfg.example for information. Otherwise report this to the vendor that provided NumPy.

Reproducing code example:

import numpy as np

Error message:

RuntimeError: Polyfit sanity test emitted a warning, most likely due to using a buggy Accelerate backend. If you compiled yourself, see site.cfg.example for information. Otherwise report this to the vendor that provided NumPy.
RankWarning: Polyfit may be poorly conditioned

Traceback (most recent call last):
File "/Users/billyzhaoyh/anaconda/envs/simulation/lib/python3.7/runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/Users/billyzhaoyh/anaconda/envs/simulation/lib/python3.7/runpy.py", line 109, in _get_module_details
import(pkg_name)
File "/Users/billyzhaoyh/Desktop/Simulation/reina-model/calc/init.py", line 1, in
from .utils import calcfunc
File "/Users/billyzhaoyh/Desktop/Simulation/reina-model/calc/utils.py", line 9, in
from utils.quilt import load_datasets
File "/Users/billyzhaoyh/Desktop/Simulation/reina-model/utils/quilt.py", line 4, in
import quilt
File "/Users/billyzhaoyh/anaconda/envs/simulation/lib/python3.7/site-packages/quilt/init.py", line 87, in
from .tools.command import (
File "/Users/billyzhaoyh/anaconda/envs/simulation/lib/python3.7/site-packages/quilt/tools/command.py", line 24, in
import numpy as np
File "/Users/billyzhaoyh/Desktop/Simulation/reina-model/src/numpy/numpy/init.py", line 286, in
raise RuntimeError(msg)
RuntimeError: Polyfit sanity test emitted a warning, most likely due to using a buggy Accelerate backend. If you compiled yourself, see site.cfg.example for information. Otherwise report this to the vendor that provided NumPy.
RankWarning: Polyfit may be poorly conditioned

Numpy/Python version information:

Python 3.7.6
Numpy -e git+https://github.com/numpy/numpy.git@078ac01a85c4db46e7f148829c2e0d0e0f30c36f#egg=numpy

@mattip
Copy link
Member

mattip commented Apr 9, 2020

This is by design. Did you read the error message? Is it unclear?

@billlyzhaoyh
Copy link
Author

so how can I fix this error, by updating the Accelerate backend? I had a search and don't think I can update that for my MacOS.

@vrakesh
Copy link
Member

vrakesh commented Apr 11, 2020

@billlyzhaoyh Requesting to use OpenBLAS while building on MacOS instead. Use site.cfg in the root repo directory to build using the same.

@mattip
Copy link
Member

mattip commented May 20, 2020

Closing. We strongly recommend not using Accelerate.

@xbeta
Copy link

xbeta commented Aug 14, 2020

Closing. We strongly recommend not using Accelerate.

Sure, how do we not use it? I got the same error when just run simple import numpy in my macOS

@mattip
Copy link
Member

mattip commented Aug 15, 2020

If you are building your own NumPy, get OpenBlas. You can use python3 tools/openblas_support.py to download the one used to build the official wheel. If you are using a provider's NumPy, ask them why they are using Accelerate.

@xbeta
Copy link

xbeta commented Aug 15, 2020

If you are building your own NumPy, get OpenBlas. You can use python3 tools/openblas_support.py to download the one used to build the official wheel. If you are using a provider's NumPy, ask them why they are using Accelerate.

I am definitely not building on my own. I am using macOS, and run pip3 install -U numpy , so I am not sure what "provider" do you mean by that .

@mattip
Copy link
Member

mattip commented Aug 16, 2020

Pip can build from source behind your back. If it takes more than a few seconds to install, it may be compiling. To force pip to only use binary wheels and never compile, use

pip3 install --upgrade --only-binary :all: <package>

Can you try to uninstall your numpy and install with the --only-binary :all: argument?

@slw07g
Copy link

slw07g commented Aug 29, 2020

This fixed the problem for me. (I replaced pip with pip3 ... and the numpy version as of today will be 1.19.1)
https://gist.github.com/yatsu/47bdde35e8abbe7d14bbe730342aa9e0#file-numpy-openblas-macos-pip-sh

EDIT: Pasting complete solution in case gist gets deleted :)

# Setup HomeBrew if not already installed: https://brew.sh/
brew install openblas
mkdir /tmp/numpy_local
cd /tmp/numpy_local
pip3 download --no-binary :all: --no-deps numpy
unzip numpy-*.zip  # (assuming there's only one version in this folder)
cd numpy-1.19.1 # the version may be a later version than this

cat > site.cfg <<EOF
[openblas]
libraries = openblas
library_dirs = $(brew --prefix openblas)/lib
include_dirs = $(brew --prefix openblas)/include
runtime_library_dirs = $(brew --prefix openblas)/lib
EOF

pip3 install .
# cleanup
cd /tmp
rm -rf numpy_local

@glyph
Copy link

glyph commented Sep 3, 2020

A slightly simpler solution:

$ rm -v ~/Library/Caches/pip/wheels/////numpy # clear the pip wheel cache of any built numpy wheels

Per https://twitter.com/pradyunsg/status/1317081239526936576?s=20 this can now be done with a built-in pip command:

$ pip cache remove numpy
$ brew install openblas # make sure OpenBLAS is installed
$ # activate your pypy3 virtualenv
$ OPENBLAS="$(brew --prefix openblas)" pip install numpy # let numpy's setup.py know where OpenBLAS is installed

This has the added benefit that the wheel will not need to be rebuilt, and any future pip install numpy's on pypy3 will work.

@glyph
Copy link

glyph commented Sep 3, 2020

Workarounds aside though, why is the default build behavior to produce a broken wheel that can't even import? It seems like it being "by design" to build such a broken artifact is a bug either way; a self-test at the end of the build that aborts if it finds itself in this state would be a welcome addition. (Especially if it could provide a helpful error message like "try brew install openblas" or somesuch.)

@mattip
Copy link
Member

mattip commented Sep 3, 2020

Trying to import after the build sounds like a good idea.

@xrisk
Copy link

xrisk commented Oct 7, 2020

Why not simply refuse to build on macOS if openblas is not found?

@mattip
Copy link
Member

mattip commented Oct 7, 2020

There are various valid blas backends, besides OpenBLAS. The only invalid one is Accelerate. Unfortunately, macOS does not provide a way to tell us that they are providing Accelerate, we can only detect it at runtime by triggering a known bad calculation. If you can figure out how to detect Accelerate earlier that would be great. The best we had so far is to detect a symlink, but that is very fragile.

@mikkelam
Copy link

mikkelam commented Oct 8, 2020

If any python-poetry users come across this thread, I would just like to add that @glyph's solution works as well if you just replace the last line with OPENBLAS="$(brew --prefix openblas)" poetry add numpy

@stephenyang21
Copy link

I've tried a previous version of numpy and it worked for me
pip3 install numpy==1.18.0

Hope this help

@SteveG34
Copy link

uninstall and install 1.18.0 -- worked for me! kuddos to the above ^^

@fny
Copy link

fny commented Oct 21, 2020

I'm here to report that the suggestion to use openblas doesn't work. This crashes:

OPENBLAS="$(brew --prefix openblas)" pip install numpy
python -c 'import numpy'

# =>  RuntimeError: Polyfit sanity test emitted a warning, most likely due to using a buggy Accelerate backend. If you compiled yourself, see site.cfg.example for information. Otherwise report this to the vendor that provided NumPy.

while this works without any issues:

pip install numpy==1.18.0
python -c 'import numpy'

@xrisk
Copy link

xrisk commented Oct 21, 2020

@fny check the output to see if pip is using a cached wheel. You might have to clear the cache in that case.

@glyph
Copy link

glyph commented Oct 21, 2020

@fny see #15947 (comment) for updated instructions

ax3l added a commit to ECP-WarpX/WarpX that referenced this issue Feb 8, 2021
ax3l added a commit to ECP-WarpX/WarpX that referenced this issue Feb 8, 2021
Travis-OBrien pushed a commit to Travis-OBrien/blender that referenced this issue Feb 28, 2021
Building NumPy from source with default options of builder
causes it to link against Accelerate framework which is buggy and
raises a warning mentioned in [2].
"RankWarning: Polyfit may be poorly conditioned"
Accelerate is deprecated with NumPy 1.20+.[1]

So either we build OpenBLAS in dependencies also and set appropriate
env variables suggested in [1] while building NumPy for it to find
OpenBLAS. Or download NumPy wheel from pip and never allow pip to
build NumPy from source while installing.

After this change, pip wheels are used for NumPy for macOS with x86_64.

[1] https://numpy.org/doc/stable/user/building.html#lapack
[2] numpy/numpy#15947
Reviewed By: #platform_macos, sebbas
Differential Revision: https://developer.blender.org/D10368
@poke1024
Copy link

Same here. Strange this is that this seems like a random condition. On some interpreter starts it works, on some it doesn't. :-(

macOS 10.15.7 
python 3.8.5
numpy 1.20.1

Downgrading to numpy-1.18.0 works.

aouinizied added a commit to nfstream/nfstream that referenced this issue May 17, 2021
felixdivo added a commit to felixdivo/tslearn that referenced this issue May 26, 2021
felixdivo added a commit to felixdivo/tslearn that referenced this issue May 26, 2021
ax3l added a commit to openPMD/openPMD-api that referenced this issue Jul 14, 2021
For issue:
```
RuntimeError: Polyfit sanity test emitted a warning, most likely due to using a buggy Accelerate backend. If you compiled yourself, more information is available at https://numpy.org/doc/stable/user/building.html#accelerated-blas-lapack-libraries Otherwise report this to the vendor that provided NumPy.
```
Since PyPy builds a new numpy wheel on the fly.

numpy/numpy#15947 (comment)
ax3l added a commit to openPMD/openPMD-api that referenced this issue Jul 14, 2021
For issue:
```
RuntimeError: Polyfit sanity test emitted a warning, most likely due to using a buggy Accelerate backend. If you compiled yourself, more information is available at https://numpy.org/doc/stable/user/building.html#accelerated-blas-lapack-libraries Otherwise report this to the vendor that provided NumPy.
```
Since PyPy builds a new numpy wheel on the fly.

numpy/numpy#15947 (comment)
leycec added a commit to beartype/beartype that referenced this issue Aug 5, 2021
This commit is the next in a commit chain improving our GitHub
Actions-based continuous integration (CI) configuration to install
optional test-time dependencies. Specifically, this commit attempts to
circumvent installation woes induced by Apple's patently broken
"Accelerate" BLAS replacement (as documented at numpy/numpy#15947) by
conditionally installing `pip` packages with `--force-reinstall` under
macOS. This will almost certainly fail. CI gonna CI. (*Unprecedented antecedents!*)
leycec added a commit to beartype/beartype that referenced this issue Aug 5, 2021
This commit is the next in a commit chain improving our GitHub
Actions-based continuous integration (CI) configuration to install
optional test-time dependencies. Specifically, this commit attempts to
circumvent installation woes induced by Apple's patently broken
"Accelerate" BLAS replacement (as documented at numpy/numpy#15947) by
conditionally installing `pip` packages with `--force-reinstall` under
macOS. This will almost certainly fail. CI gonna CI. (*Wrongly elongated thongs!*)
leycec added a commit to beartype/beartype that referenced this issue Aug 6, 2021
This commit is the next in a commit chain improving our GitHub
Actions-based continuous integration (CI) configuration to install
optional test-time dependencies. Specifically, this commit attempts to
circumvent installation woes induced by Apple's patently broken
"Accelerate" BLAS replacement (as documented at numpy/numpy#15947) by
conditionally avoiding installation of `pip` packages built against
"Accelerate" under PyPy. Whereas forcing `pip` to reinstall these
packages with `--force-reinstall` succeeds under CPython, the same
approach fails to improve matters under PyPy for unknown reasons. We are
short on sanity and even shorter on time, so the only remaining option
is to nuke these packages from orbit under macOS + PyPy -- which is such
an outlier edge-case to begin with that it seems doubtful anyone will
care. We care, of course. But you can only do so much when your core
platform is fundamentally broken. Thanks, Apple. (*Drip-fed metalhead!*)
rtavenar pushed a commit to tslearn-team/tslearn that referenced this issue Aug 17, 2021
* update github wheel building workflow

* update documentation building config to Python 3.9

* also test on Python 3.9 in the Azure CI environment

* revert too recent python version for readthedocs (set to 3.8)

* apply idea from numpy/numpy#15947 (comment)

* Revert "apply idea from numpy/numpy#15947 (comment)"

This reverts commit df5c44b.

* apply idea from scipy/scipy#13409 (comment)

* attempt other OPENBLAS fix

* document change
leycec added a commit to beartype/beartype that referenced this issue Aug 18, 2021
This release brings undeniable support for **typed NumPy arrays** (i.e.,
`numpy.typed.NDArray` type hints), **typing backports** (i.e., public
public attributes of the third-party `typing_extensions` package,
enabling typing_ types introduced by newer Python versions to be used
under older Python versions), and **portable beartype validators**
(i.e., `beartype.vale` type hints usable under *all* Python versions via
the `typing_extensions.Annotated` backport). This release resolves **4
issues** and merges **2 pull requests.** Changes include:

## Compatibility Improved

* **Typed NumPy arrays** (i.e., `numpy.typed.NDArray` type hints). The
  `@beartype` decorator now transparently supports the third-party
  `numpy.typing.NDArray` type hint newly introduced by NumPy >= 1.21.0,
  resolving issue #42 kindly submitted by NumPy extraordinaire @Antyos.
  Note that usage of typed NumPy arrays under Python < 3.9.0 requires
  installation of the third-party `typing-extensions` package, which
  `@beartype` will then automagically detect and leverage internally.
* **Typing backports** (i.e., `typing_extensions` type hints). The
  `@beartype` decorator now transparently supports all public type hints
  of the third-party `typing_extensions` package, resolving issue #34
  kindly submitted by Ryan Soklaski (@rsokl) of considerable *MIT Beaver
  Works Summer Institute* and *Python Like You Mean It* fame.
  Note that usage of typed NumPy arrays under Python < 3.9.0 requires
  installation of the third-party `typing-extensions` package, which
  `@beartype` will then automagically detect and leverage internally.
* **Python < 3.9.0 beartype validators.** The `@beartype` decorator now
  portably supports **beartype validators** (i.e., `beartype.vale`
  objects annotating `typing{_extensions}.Annotated` type hints) across
  all Python versions, *also* resolving issue #34 kindly submitted by
  Ryan Soklaski (@rsokl). <sup>hallowed be his username</sup> Note that
  usage of beartype validators under Python < 3.9.0 requires:
  * Installation of the third-party `typing-extensions` package.
  * Annotation of `beartype.vale` objects by
    `typing_extensions.Annotated` (rather than `typing.Annotated`).
* **Python >= 3.10.0 `typing.NewType` type hints.** This release
  resolves a minor incompatibility recently introduced by Python
  3.10.0rc1, which (*waitforit*) broke backward compatibility with prior
  implementations of the public `typing.NewType` type hint. Previously,
  that hint was implemented as a closure; Python ≥ 3.10 fundamentally
  refactored that hint to a pure-Python class instance – significantly
  complicating cross-version detection. *yuwastemytime,* CPython?
* **Binary dunder method return annotations** (i.e., dunder methods
  accepting exactly two arguments). Previously, `@beartype` adopted
  mypy's permissive approach of implicitly coercing the return
  annotations of binary dunder methods returning only booleans to
  instead return `typing.Union[bool, type(NotImplemented)]`. `beartype`
  now expands that approach to *all* binary dunder methods regardless of
  return annotation. Thanks to pull request #40 from Matt Bogosian
  (@posita), Dropbox's positive code genius!

## Compatibility Broken

* **None.** This release preserves backward compatibility with the prior
  stable release.

## Issues Resolved

* **#44,** documenting beartype's new project shield generously ginned
  up by Matt Bogosian (@posita), Dropbox's positive code genius.
* **#42,** enabling typed NumPy arrays to be used under both Python ≥
  3.9.0 natively *and* Python < 3.9.0 via the
  `typing_extensions.Annotated` backport.
* **#40,** including:
  * **Fake builtin types** (i.e., non-builtin classes erroneously
    masquerading as builtin). Previously, `@beartype` incorrectly
    generated syntactically invalid code for decorated callables
    annotated by one or more fake builtin types – which, miraculously,
    there are hardly any of. Still... *it's not a good look, beartype.*
    Thanks to Matt Bogosian (@posita), Dropbox's positive code genius,
    for first detecting this horrifying edge case in pull request #40.
* **#34,** enabling beartype validators to be used under Python < 3.9.0
  via the `typing_extensions.Annotated` backport.

## Documentation Revised

* **Badge** (i.e., shield intended for display by downstream projects).
  Our front-facing `README.rst` documentation now provides a
  post-installation advisory suggesting public declaration of @beartype
  project compatibility with reST- and Markdown-friendly text exhibiting
  a beautiful @beartype badge. Unsurprisingly, @posita made that too...
  because what doesn't @posita do? Nuthin'! I'm pretty sure that means
  @posita does everything.

## Tests Improved

* **Optional dependencies.** Our GitHub Actions-based continuous
  integration (CI) configuration now installs optional test-time
  dependencies. Although `beartype` has *no* mandatory runtime
  dependencies, fully exercising all tests necessitates installing these
  test-time dependencies:

  * mypy, enabling tests for mypy-based PEP 561 compliance.
  * NumPy, enabling tests for `numpy.typing.NDArray` type hints. This
    proved surprisingly non-trivial. Apple's patently broken
    "Accelerate" BLAS replacement (as documented at numpy/numpy#15947)
    blocks NumPy ≥ 1.18.0 installation under default CI configurations,
    necessitating that we:
    * Only conditionally install `pip` packages with `--force-reinstall`
      under macOS. CI gonna CI.
    * Avoid installing NumPy altogether under macOS + PyPy, where even
      the `--force-reinstall` option fails to improve matters. You can
      only do so much when your core platform is fundamentally broken.
      Thanks, Apple.
  * `typing_extensions`, enabling tests for `typing` attributes
    unavailable under the active Python interpreter.

Thanks yet again to Badge Connoisseur @posita for his tremendous
efforts – which we are now eternally indebted to and can only repay
across the span of several gruelling lifetimes. "It will all be worth
it," I tell my future self.

(*Clogged clogs and unlogged cogs!*)
@xkortex
Copy link

xkortex commented Oct 12, 2021

Mac Catalina 10.15.7 / Python 3.10.0 / Numpy 1.21.1 fails for me. The same works fine on 3.9.5 and lower.

Running pip install --upgrade --only-binary :all: numpy fails:

ERROR: Could not find a version that satisfies the requirement numpy (from versions: none)
ERROR: No matching distribution found for numpy

so I presume there are no prebuilt wheels. pip install --upgrade --prefer-binary numpy triggers a build, confirming my suspicion. I have openblas installed (/usr/local/opt/openblas -> ../Cellar/openblas/0.3.17) so I tried

pip uninstall numpy
export OPENBLAS=$(brew --prefix openblas)
pip install --no-cache-dir  numpy # avoid the cached .whl!

and that worked, so many thanks @fny !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests