Change how cupy
import fall-through is handled
#533
Merged
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.
Currently, we have many instances of this pattern:
This is done to ensure that we can write code that mixes usage of
numpy
andcupy
while still importing in an environment that doesn't havecupy
installed. Selection between the modules is done through logic within the code (and this is always necessary because even whencupy
is present it's not optimal to always replacenumpy
withcupy
).This pattern causes two problems:
import cupy as cp
. On many systems,cupy
is present but misconfigured, and the errors it emits to warn users of this are caught and dropped by this code.cp = None
makes the eventual error that appears in the former scenario extremely confusing (usually something along the lines ofAttributeError: 'NoneType' object has no attribute 'asnumpy'
. This also interferes with static analysis, and causes it to flag any use ofcp
since it (correctly) infers thatcp
can equal eithercupy
orNone
.This PR changes these two behaviors by replacing the above pattern with the following:
Issue (1) is addressed by only capturing
ModuleNotFoundError
, so that ifcupy
is present but misconfigured, its import error gets properly raised, and issue (2) is... different. In the case wherecupy
was misconfigured, we will fail much earlier and in a clearer way, so the user never reaches the call toNone.asnumpy
. In the case of the static analyzer, in an environment withoutcupy
it will flag only methods that exist incupy
but notnumpy
, reducing but not eliminating the false positives. The only oddly behaved case is if a user usesdevice='gpu'
in an environment that don't havecupy
installed at all, in which case they will eventually encounter someAttributeError
when acupy
-only function is called onnumpy
.Merging closes #527.