Coerce dtype __props__
to string due to invalid hash of np.dtype()
objects
#1436
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.
There were some odd errors cropping up in pymc-marketing and a private project.
The issue happened when you created an Arange with
float64
and another withnp.dtype("float64")
(or any combo of dtype and string), which evaluate to the same thing, but hash differently (see below why). The PyTensor C-cache has a sanity-check mechanism to make sure the eq and hash of our Ops is correctly implemented, as the validity of the cache depends on this.PyTensor uses the properties of Op to define equality and hash automatically, and for Arange that is it's
dtype
argument. Surprisingly though,np.dtype("float64") == "float64" and hash(np.dtype("float64")) != hash("float64")
, which seems to be a fundamental bug in Numpy: numpy/numpy#17864This only became a problem once we added a C-implementation to Arange in #1392 as this is where the automatic checks for hash/eq are performed.
I tried to look for other Ops parametrized by
dtype
of some form or another, and make sure those are always converted to the equivalent strings.CC @lucianopaz and @juanitorduz
📚 Documentation preview 📚: https://pytensor--1436.org.readthedocs.build/en/1436/