Skip to content

Commit 8489d8b

Browse files
committed
Rename get_scalar_constant_value to get_underlying_scalar_constant
1 parent afb109b commit 8489d8b

File tree

25 files changed

+148
-148
lines changed

25 files changed

+148
-148
lines changed

doc/library/tensor/basic.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ them perfectly, but a `dscalar` otherwise.
577577
.. method:: round(mode="half_away_from_zero")
578578
:noindex:
579579
.. method:: trace()
580-
.. method:: get_scalar_constant_value()
580+
.. method:: get_underlying_scalar_constant()
581581
.. method:: zeros_like(model, dtype=None)
582582

583583
All the above methods are equivalent to NumPy for PyTensor on the current tensor.

pytensor/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def _as_symbolic(x, **kwargs) -> Variable:
137137
# isort: on
138138

139139

140-
def get_scalar_constant_value(v):
140+
def get_underlying_scalar_constant(v):
141141
"""Return the constant scalar (i.e. 0-D) value underlying variable `v`.
142142
143143
If `v` is the output of dim-shuffles, fills, allocs, cast, etc.
@@ -153,8 +153,8 @@ def get_scalar_constant_value(v):
153153
if sparse and isinstance(v.type, sparse.SparseTensorType):
154154
if v.owner is not None and isinstance(v.owner.op, sparse.CSM):
155155
data = v.owner.inputs[0]
156-
return tensor.get_scalar_constant_value(data)
157-
return tensor.get_scalar_constant_value(v)
156+
return tensor.get_underlying_scalar_constant(data)
157+
return tensor.get_underlying_scalar_constant(v)
158158

159159

160160
# isort: off

pytensor/gradient.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ def try_to_copy_if_needed(var):
13251325
f" {i}. Since this input is only connected "
13261326
"to integer-valued outputs, it should "
13271327
"evaluate to zeros, but it evaluates to"
1328-
f"{pytensor.get_scalar_constant_value(term)}."
1328+
f"{pytensor.get_underlying_scalar_constant(term)}."
13291329
)
13301330
raise ValueError(msg)
13311331

@@ -2086,7 +2086,7 @@ def _is_zero(x):
20862086

20872087
no_constant_value = True
20882088
try:
2089-
constant_value = pytensor.get_scalar_constant_value(x)
2089+
constant_value = pytensor.get_underlying_scalar_constant(x)
20902090
no_constant_value = False
20912091
except pytensor.tensor.exceptions.NotScalarConstantError:
20922092
pass

pytensor/link/jax/dispatch/tensor_basic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
ScalarFromTensor,
1919
Split,
2020
TensorFromScalar,
21-
get_scalar_constant_value,
21+
get_underlying_scalar_constant,
2222
)
2323
from pytensor.tensor.exceptions import NotScalarConstantError
2424

@@ -106,7 +106,7 @@ def join(axis, *tensors):
106106
def jax_funcify_Split(op: Split, node, **kwargs):
107107
_, axis, splits = node.inputs
108108
try:
109-
constant_axis = get_scalar_constant_value(axis)
109+
constant_axis = get_underlying_scalar_constant(axis)
110110
except NotScalarConstantError:
111111
constant_axis = None
112112
warnings.warn(
@@ -116,7 +116,7 @@ def jax_funcify_Split(op: Split, node, **kwargs):
116116
try:
117117
constant_splits = np.array(
118118
[
119-
get_scalar_constant_value(splits[i])
119+
get_underlying_scalar_constant(splits[i])
120120
for i in range(get_vector_length(splits))
121121
]
122122
)

pytensor/scan/basic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pytensor.graph.utils import MissingInputError, TestValueError
1313
from pytensor.scan.op import Scan, ScanInfo
1414
from pytensor.scan.utils import expand_empty, safe_new, until
15-
from pytensor.tensor.basic import get_scalar_constant_value
15+
from pytensor.tensor.basic import get_underlying_scalar_constant
1616
from pytensor.tensor.exceptions import NotScalarConstantError
1717
from pytensor.tensor.math import minimum
1818
from pytensor.tensor.shape import shape_padleft, unbroadcast
@@ -147,7 +147,7 @@ def isNaN_or_Inf_or_None(x):
147147
isStr = False
148148
if not isNaN and not isInf:
149149
try:
150-
val = get_scalar_constant_value(x)
150+
val = get_underlying_scalar_constant(x)
151151
isInf = np.isinf(val)
152152
isNaN = np.isnan(val)
153153
except Exception:
@@ -476,7 +476,7 @@ def wrap_into_list(x):
476476
n_fixed_steps = int(n_steps)
477477
else:
478478
try:
479-
n_fixed_steps = at.get_scalar_constant_value(n_steps)
479+
n_fixed_steps = at.get_underlying_scalar_constant(n_steps)
480480
except NotScalarConstantError:
481481
n_fixed_steps = None
482482

pytensor/scan/rewriting.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
safe_new,
5050
scan_can_remove_outs,
5151
)
52-
from pytensor.tensor.basic import Alloc, AllocEmpty, get_scalar_constant_value
52+
from pytensor.tensor.basic import Alloc, AllocEmpty, get_underlying_scalar_constant
5353
from pytensor.tensor.elemwise import DimShuffle, Elemwise
5454
from pytensor.tensor.exceptions import NotScalarConstantError
5555
from pytensor.tensor.math import Dot, dot, maximum, minimum
@@ -1956,13 +1956,13 @@ def belongs_to_set(self, node, set_nodes):
19561956

19571957
nsteps = node.inputs[0]
19581958
try:
1959-
nsteps = int(get_scalar_constant_value(nsteps))
1959+
nsteps = int(get_underlying_scalar_constant(nsteps))
19601960
except NotScalarConstantError:
19611961
pass
19621962

19631963
rep_nsteps = rep.inputs[0]
19641964
try:
1965-
rep_nsteps = int(get_scalar_constant_value(rep_nsteps))
1965+
rep_nsteps = int(get_underlying_scalar_constant(rep_nsteps))
19661966
except NotScalarConstantError:
19671967
pass
19681968

pytensor/tensor/basic.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,10 @@ def get_scalar_constant(v, elemwise=True, only_process_constants=False, max_recu
263263
data = v.data
264264
if data.ndim != 0:
265265
raise NotScalarConstantError()
266-
return get_scalar_constant_value(v, elemwise, only_process_constants, max_recur)
266+
return get_underlying_scalar_constant(v, elemwise, only_process_constants, max_recur)
267267

268268

269-
def get_scalar_constant_value(
269+
def get_underlying_scalar_constant(
270270
orig_v, elemwise=True, only_process_constants=False, max_recur=10
271271
):
272272
"""Return the constant scalar(0-D) value underlying variable `v`.
@@ -369,7 +369,7 @@ def get_scalar_constant_value(
369369
elif isinstance(v.owner.op, CheckAndRaise):
370370
# check if all conditions are constant and true
371371
conds = [
372-
get_scalar_constant_value(c, max_recur=max_recur)
372+
get_underlying_scalar_constant(c, max_recur=max_recur)
373373
for c in v.owner.inputs[1:]
374374
]
375375
if builtins.all(0 == c.ndim and c != 0 for c in conds):
@@ -383,7 +383,7 @@ def get_scalar_constant_value(
383383
continue
384384
if isinstance(v.owner.op, _scalar_constant_value_elemwise_ops):
385385
const = [
386-
get_scalar_constant_value(i, max_recur=max_recur)
386+
get_underlying_scalar_constant(i, max_recur=max_recur)
387387
for i in v.owner.inputs
388388
]
389389
ret = [[None]]
@@ -402,7 +402,7 @@ def get_scalar_constant_value(
402402
v.owner.op.scalar_op, _scalar_constant_value_elemwise_ops
403403
):
404404
const = [
405-
get_scalar_constant_value(i, max_recur=max_recur)
405+
get_underlying_scalar_constant(i, max_recur=max_recur)
406406
for i in v.owner.inputs
407407
]
408408
ret = [[None]]
@@ -448,7 +448,7 @@ def get_scalar_constant_value(
448448
):
449449
idx = v.owner.op.idx_list[0]
450450
if isinstance(idx, Type):
451-
idx = get_scalar_constant_value(
451+
idx = get_underlying_scalar_constant(
452452
v.owner.inputs[1], max_recur=max_recur
453453
)
454454
try:
@@ -482,14 +482,14 @@ def get_scalar_constant_value(
482482
):
483483
idx = v.owner.op.idx_list[0]
484484
if isinstance(idx, Type):
485-
idx = get_scalar_constant_value(
485+
idx = get_underlying_scalar_constant(
486486
v.owner.inputs[1], max_recur=max_recur
487487
)
488488
# Python 2.4 does not support indexing with numpy.integer
489489
# So we cast it.
490490
idx = int(idx)
491491
ret = v.owner.inputs[0].owner.inputs[idx]
492-
ret = get_scalar_constant_value(ret, max_recur=max_recur)
492+
ret = get_underlying_scalar_constant(ret, max_recur=max_recur)
493493
# MakeVector can cast implicitly its input in some case.
494494
return _asarray(ret, dtype=v.type.dtype)
495495

@@ -504,7 +504,7 @@ def get_scalar_constant_value(
504504
idx_list = op.idx_list
505505
idx = idx_list[0]
506506
if isinstance(idx, Type):
507-
idx = get_scalar_constant_value(
507+
idx = get_underlying_scalar_constant(
508508
owner.inputs[1], max_recur=max_recur
509509
)
510510
grandparent = leftmost_parent.owner.inputs[0]
@@ -519,7 +519,7 @@ def get_scalar_constant_value(
519519

520520
if not (idx < ndim):
521521
msg = (
522-
"get_scalar_constant_value detected "
522+
"get_underlying_scalar_constant detected "
523523
f"deterministic IndexError: x.shape[{int(idx)}] "
524524
f"when x.ndim={int(ndim)}."
525525
)
@@ -1581,7 +1581,7 @@ def do_constant_folding(self, fgraph, node):
15811581
@_get_vector_length.register(Alloc)
15821582
def _get_vector_length_Alloc(var_inst, var):
15831583
try:
1584-
return get_scalar_constant_value(var.owner.inputs[1])
1584+
return get_underlying_scalar_constant(var.owner.inputs[1])
15851585
except NotScalarConstantError:
15861586
raise ValueError(f"Length of {var} cannot be determined")
15871587

@@ -1832,17 +1832,17 @@ def perform(self, node, inp, out_):
18321832

18331833
def extract_constant(x, elemwise=True, only_process_constants=False):
18341834
"""
1835-
This function is basically a call to tensor.get_scalar_constant_value.
1835+
This function is basically a call to tensor.get_underlying_scalar_constant.
18361836
18371837
The main difference is the behaviour in case of failure. While
1838-
get_scalar_constant_value raises an TypeError, this function returns x,
1838+
get_underlying_scalar_constant raises an TypeError, this function returns x,
18391839
as a tensor if possible. If x is a ScalarVariable from a
18401840
scalar_from_tensor, we remove the conversion. If x is just a
18411841
ScalarVariable, we convert it to a tensor with tensor_from_scalar.
18421842
18431843
"""
18441844
try:
1845-
x = get_scalar_constant_value(x, elemwise, only_process_constants)
1845+
x = get_underlying_scalar_constant(x, elemwise, only_process_constants)
18461846
except NotScalarConstantError:
18471847
pass
18481848
if isinstance(x, aes.ScalarVariable) or isinstance(
@@ -2212,7 +2212,7 @@ def make_node(self, axis, *tensors):
22122212

22132213
if not isinstance(axis, int):
22142214
try:
2215-
axis = int(get_scalar_constant_value(axis))
2215+
axis = int(get_underlying_scalar_constant(axis))
22162216
except NotScalarConstantError:
22172217
pass
22182218

@@ -2461,7 +2461,7 @@ def infer_shape(self, fgraph, node, ishapes):
24612461
def _get_vector_length_Join(op, var):
24622462
axis, *arrays = var.owner.inputs
24632463
try:
2464-
axis = get_scalar_constant_value(axis)
2464+
axis = get_underlying_scalar_constant(axis)
24652465
assert axis == 0 and builtins.all(a.ndim == 1 for a in arrays)
24662466
return builtins.sum(get_vector_length(a) for a in arrays)
24672467
except NotScalarConstantError:
@@ -2873,7 +2873,7 @@ def infer_shape(self, fgraph, node, i_shapes):
28732873

28742874
def is_constant_value(var, value):
28752875
try:
2876-
v = get_scalar_constant_value(var)
2876+
v = get_underlying_scalar_constant(var)
28772877
return np.all(v == value)
28782878
except NotScalarConstantError:
28792879
pass
@@ -3785,7 +3785,7 @@ def make_node(self, a, choices):
37853785
static_out_shape = ()
37863786
for s in out_shape:
37873787
try:
3788-
s_val = pytensor.get_scalar_constant_value(s)
3788+
s_val = pytensor.get_underlying_scalar_constant(s)
37893789
except (NotScalarConstantError, AttributeError):
37903790
s_val = None
37913791

@@ -4106,7 +4106,7 @@ def take_along_axis(arr, indices, axis=0):
41064106
"scalar_from_tensor",
41074107
"tensor_from_scalar",
41084108
"get_scalar_constant",
4109-
"get_scalar_constant_value",
4109+
"get_underlying_scalar_constant",
41104110
"constant",
41114111
"as_tensor_variable",
41124112
"as_tensor",

pytensor/tensor/blas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,7 @@ def local_gemm_to_ger(fgraph, node):
18341834
xv = x.dimshuffle(0)
18351835
yv = y.dimshuffle(1)
18361836
try:
1837-
bval = at.get_scalar_constant_value(b)
1837+
bval = at.get_underlying_scalar_constant(b)
18381838
except NotScalarConstantError:
18391839
# b isn't a constant, GEMM is doing useful pre-scaling
18401840
return

pytensor/tensor/conv/abstract_conv.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from pytensor.graph.basic import Apply, Variable
2525
from pytensor.graph.op import Op
2626
from pytensor.raise_op import Assert
27-
from pytensor.tensor.basic import as_tensor_variable, get_scalar_constant_value
27+
from pytensor.tensor.basic import as_tensor_variable, get_underlying_scalar_constant
2828
from pytensor.tensor.exceptions import NotScalarConstantError
2929
from pytensor.tensor.var import TensorConstant, TensorVariable
3030

@@ -495,8 +495,8 @@ def check_dim(given, computed):
495495
if given is None or computed is None:
496496
return True
497497
try:
498-
given = get_scalar_constant_value(given)
499-
computed = get_scalar_constant_value(computed)
498+
given = get_underlying_scalar_constant(given)
499+
computed = get_underlying_scalar_constant(computed)
500500
return int(given) == int(computed)
501501
except NotScalarConstantError:
502502
# no answer possible, accept for now
@@ -532,7 +532,7 @@ def assert_conv_shape(shape):
532532
out_shape = []
533533
for i, n in enumerate(shape):
534534
try:
535-
const_n = get_scalar_constant_value(n)
535+
const_n = get_underlying_scalar_constant(n)
536536
if i < 2:
537537
if const_n < 0:
538538
raise ValueError(
@@ -2200,7 +2200,7 @@ def __init__(
22002200
if imshp_i is not None:
22012201
# Components of imshp should be constant or ints
22022202
try:
2203-
get_scalar_constant_value(imshp_i, only_process_constants=True)
2203+
get_underlying_scalar_constant(imshp_i, only_process_constants=True)
22042204
except NotScalarConstantError:
22052205
raise ValueError(
22062206
"imshp should be None or a tuple of constant int values"
@@ -2213,7 +2213,7 @@ def __init__(
22132213
if kshp_i is not None:
22142214
# Components of kshp should be constant or ints
22152215
try:
2216-
get_scalar_constant_value(kshp_i, only_process_constants=True)
2216+
get_underlying_scalar_constant(kshp_i, only_process_constants=True)
22172217
except NotScalarConstantError:
22182218
raise ValueError(
22192219
"kshp should be None or a tuple of constant int values"

pytensor/tensor/elemwise.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ def perform(self, node, inputs, output_storage):
759759
ufunc = self.ufunc
760760
elif not hasattr(node.tag, "ufunc"):
761761
# It happen that make_thunk isn't called, like in
762-
# get_scalar_constant_value
762+
# get_underlying_scalar_constant
763763
self.prepare_node(node, None, None, "py")
764764
# prepare_node will add ufunc to self or the tag
765765
# depending if we can reuse it or not. So we need to

pytensor/tensor/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class ShapeError(Exception):
44

55
class NotScalarConstantError(Exception):
66
"""
7-
Raised by get_scalar_constant_value if called on something that is
7+
Raised by get_underlying_scalar_constant if called on something that is
88
not a scalar constant.
99
"""
1010

pytensor/tensor/extra_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ def make_node(self, x, repeats):
671671
out_shape = [None]
672672
else:
673673
try:
674-
const_reps = at.get_scalar_constant_value(repeats)
674+
const_reps = at.get_underlying_scalar_constant(repeats)
675675
except NotScalarConstantError:
676676
const_reps = None
677677
if const_reps == 1:

pytensor/tensor/random/op.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pytensor.tensor.basic import (
1313
as_tensor_variable,
1414
constant,
15-
get_scalar_constant_value,
15+
get_underlying_scalar_constant,
1616
get_vector_length,
1717
infer_static_shape,
1818
)
@@ -277,7 +277,7 @@ def infer_shape(self, fgraph, node, input_shapes):
277277
try:
278278
size_len = get_vector_length(size)
279279
except ValueError:
280-
size_len = get_scalar_constant_value(size_shape[0])
280+
size_len = get_underlying_scalar_constant(size_shape[0])
281281

282282
size = tuple(size[n] for n in range(size_len))
283283

0 commit comments

Comments
 (0)