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

Deprecate redundant utilities for extracting constants #1046

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ricardoV94
Copy link
Member

@ricardoV94 ricardoV94 commented Oct 21, 2024

Description

In PyTensor we have tho following similar utilities

  1. pytensor.get_underlying_scalar_constant
  2. pytensor.tensor.basic.get_underlying_scalar_constant_value
  3. pytensor.tensor.basic.get_scalar_constant_value
  4. pytensor.tensor.basic.extract_constant
  5. pytensor.tensor.rewriting.math.get_constant

This PR removes and deprecates all except:

  1. pytensor.tensor.basic.get_underlying_scalar_constant_value
  2. 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 as gt(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. Perhaps unique 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 return None

Related Issue

@ricardoV94 ricardoV94 changed the title Remove repeated utilities for extracting constants Deprecate redundant utilities for extracting constants Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

local_add_neg_to_sub rewrite gives wrong results with negative constants
1 participant