Deprecate redundant utilities for extracting constants #1046
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.
Description
In PyTensor we have tho following similar utilities
pytensor.get_underlying_scalar_constant
pytensor.tensor.basic.get_underlying_scalar_constant_value
pytensor.tensor.basic.get_scalar_constant_value
pytensor.tensor.basic.extract_constant
pytensor.tensor.rewriting.math.get_constant
This PR removes and deprecates all except:
pytensor.tensor.basic.get_underlying_scalar_constant_value
pytensor.tensor.basic.get_scalar_constant_value
The reason for this distinction, is that the core utility,
get_underlying_scalar_constant_value
actually works for non-scalar inputs, if it can find a single scalar value underlies a potential n-dimensional tensor (say as in pt.zeros(5, 3, 2)). This is powerful, but can lead to subtle bugs when the caller forgets about it. This was the source of the bug behind #584 and was also likely present in other graphs such asgt(x, [0, 0, 0, 0])
and alike where the repeated condition broadcasts the output of the operation.The utility
get_scalar_constant_value
raises if the input is not a scalar (ndim=0) type.I don't love the
underlying
distinguishing the two. Perhapsunique
would be better.Both utilities now accept a
raise_not_constant
which when False (not-default) return the variable as is. I think I would prefer for it to returnNone
Related Issue
local_add_neg_to_sub
rewrite gives wrong results with negative constants #584