Skip to content

Commit 7cb6791

Browse files
committed
Test values are gone
1 parent 0aceb97 commit 7cb6791

22 files changed

Lines changed: 34 additions & 180 deletions

docs/source/contributing/developer_guide.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,10 @@ with pm.Model() as m:
387387
print(m.basic_RVs) # ==> [z, x, y]
388388
print(m.free_RVs) # ==> [z, x]
389389

390-
type(m.logpt)
390+
type(m.logp)
391391
# pytensor.tensor.var.TensorVariable
392392

393-
m.logpt.eval({x: np.random.randn(*x.tag.test_value.shape) for x in m.free_RVs})
393+
m.logpt.eval(m.initial_point())
394394
# array(-51.25369126)
395395
```
396396

@@ -419,11 +419,9 @@ On a high level, it allows us to build conditional logp function and its gradien
419419
Here is a taste of how it works in action:
420420

421421
```python
422-
inputlist = [np.random.randn(*x.tag.test_value.shape) for x in m.free_RVs]
423-
424422
func = m.logp_dlogp_function()
425423
func.set_extra_values({})
426-
input_dict = {x.name: y for x, y in zip(m.free_RVs, inputlist)}
424+
input_dict = m.initial_point()
427425
print(input_dict)
428426
input_array = func.dict_to_array(input_dict)
429427
print(input_array)

pymc/distributions/shape_utils.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323

2424
import numpy as np
2525

26-
from pytensor import config
2726
from pytensor import tensor as pt
2827
from pytensor.graph.basic import Constant, Variable
29-
from pytensor.graph.op import Op, compute_test_value
28+
from pytensor.graph.op import Op
3029
from pytensor.raise_op import Assert
3130
from pytensor.tensor.random.op import RandomVariable
3231
from pytensor.tensor.shape import SpecifyShape
@@ -288,9 +287,6 @@ def change_dist_size(
288287
for k, v in dist.tag.__dict__.items():
289288
new_dist.tag.__dict__.setdefault(k, v)
290289

291-
if config.compute_test_value != "off":
292-
compute_test_value(new_dist)
293-
294290
return new_dist
295291

296292

pymc/model/core.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,17 +1429,11 @@ def create_value_var(
14291429
# Create value variable with the same type as the RV
14301430
value_var = rv_var.type()
14311431
value_var.name = rv_var.name
1432-
if pytensor.config.compute_test_value != "off":
1433-
value_var.tag.test_value = rv_var.tag.test_value
14341432
else:
14351433
# Create value variable with the same type as the transformed RV
14361434
value_var = transform.forward(rv_var, *rv_var.owner.inputs).type()
14371435
value_var.name = f"{rv_var.name}_{transform.name}__"
14381436
value_var.tag.transform = transform
1439-
if pytensor.config.compute_test_value != "off":
1440-
value_var.tag.test_value = transform.forward(
1441-
rv_var, *rv_var.owner.inputs
1442-
).tag.test_value
14431437

14441438
self.rvs_to_transforms[rv_var] = transform
14451439
self.rvs_to_values[rv_var] = value_var

pymc/ode/ode.py

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
import scipy
2121

2222
from pytensor.graph.basic import Apply
23-
from pytensor.graph.op import Op, get_test_value
23+
from pytensor.graph.op import Op
2424
from pytensor.tensor.type import TensorType
2525

26-
from pymc.exceptions import DtypeError, ShapeError
26+
from pymc.exceptions import ShapeError
2727
from pymc.ode import utils
2828

2929
_log = logging.getLogger(__name__)
@@ -172,46 +172,6 @@ def __call__(self, y0, theta, return_sens=False, **kwargs):
172172
# use default implementation to prepare symbolic outputs (via make_node)
173173
states, sens = super().__call__(y0, theta, **kwargs)
174174

175-
if pytensor.config.compute_test_value != "off":
176-
# compute test values from input test values
177-
test_states, test_sens = self._simulate(
178-
y0=get_test_value(y0), theta=get_test_value(theta)
179-
)
180-
181-
# check types of simulation result
182-
if not test_states.dtype == self._otypes[0].dtype:
183-
raise DtypeError(
184-
"Simulated states have the wrong type.",
185-
actual=test_states.dtype,
186-
expected=self._otypes[0].dtype,
187-
)
188-
if not test_sens.dtype == self._otypes[1].dtype:
189-
raise DtypeError(
190-
"Simulated sensitivities have the wrong type.",
191-
actual=test_sens.dtype,
192-
expected=self._otypes[1].dtype,
193-
)
194-
195-
# check shapes of simulation result
196-
expected_states_shape = (self.n_times, self.n_states)
197-
expected_sens_shape = (self.n_times, self.n_states, self.n_p)
198-
if not test_states.shape == expected_states_shape:
199-
raise ShapeError(
200-
"Simulated states have the wrong shape.",
201-
test_states.shape,
202-
expected_states_shape,
203-
)
204-
if not test_sens.shape == expected_sens_shape:
205-
raise ShapeError(
206-
"Simulated sensitivities have the wrong shape.",
207-
test_sens.shape,
208-
expected_sens_shape,
209-
)
210-
211-
# attach results as test values to the outputs
212-
states.tag.test_value = test_states
213-
sens.tag.test_value = test_sens
214-
215175
if return_sens:
216176
return states, sens
217177
return states

pymc/ode/utils.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,16 @@ def augment_system(ode_func, n_states, n_theta):
8484
"""
8585
# Present state of the system
8686
t_y = pt.vector("y", dtype="float64", shape=(n_states,))
87-
t_y.tag.test_value = np.ones((n_states,), dtype="float64")
8887
# Parameter(s). Should be vector to allow for generaliztion to multiparameter
8988
# systems of ODEs. Is m dimensional because it includes all initial conditions as well as ode parameters
9089
t_p = pt.vector("p", dtype="float64")
91-
t_p.tag.test_value = np.ones((n_states + n_theta,), dtype="float64")
9290
# Time. Allow for non-autonomous systems of ODEs to be analyzed
9391
t_t = pt.scalar("t", dtype="float64")
94-
t_t.tag.test_value = 2.459
9592

9693
# Present state of the gradients:
9794
# Will always be 0 unless the parameter is the initial condition
9895
# Entry i,j is partial of y[i] wrt to p[j]
9996
dydp_vec = pt.vector("dydp", dtype="float64")
100-
dydp_vec.tag.test_value = make_sens_ic(n_states, n_theta, "float64")
10197

10298
dydp = dydp_vec.reshape((n_states, n_states + n_theta))
10399

pymc/pytensorf.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ def grad_ii(i, f, x):
364364
)
365365

366366

367-
@pytensor.config.change_flags(compute_test_value="ignore")
368367
def hessian(f, vars=None, negate_output=True):
369368
res = jacobian(gradient(f, vars), vars)
370369
if negate_output:
@@ -378,7 +377,6 @@ def hessian(f, vars=None, negate_output=True):
378377
return res
379378

380379

381-
@pytensor.config.change_flags(compute_test_value="ignore")
382380
def hessian_diag1(f, v):
383381
g = gradient1(f, v)
384382
idx = pt.arange(g.shape[0], dtype="int32")
@@ -389,7 +387,6 @@ def hess_ii(i):
389387
return pytensor.map(hess_ii, idx, return_updates=False)
390388

391389

392-
@pytensor.config.change_flags(compute_test_value="ignore")
393390
def hessian_diag(f, vars=None, negate_output=True):
394391
if vars is None:
395392
vars = cont_inputs(f)
@@ -580,9 +577,6 @@ def join_nonshared_inputs(
580577
joined_values = np.concatenate([point[var.name].ravel() for var in inputs])
581578
joined_inputs = pytensor.shared(joined_values, "joined_inputs")
582579

583-
if pytensor.config.compute_test_value != "off":
584-
joined_inputs.tag.test_value = raveled_inputs.tag.test_value
585-
586580
replace: dict[Variable, Variable] = {}
587581
last_idx = 0
588582
for var in inputs:

pymc/variational/approximations.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ def cov(self):
6969
def std(self):
7070
return rho2sigma(self.rho)
7171

72-
@pytensor.config.change_flags(compute_test_value="off")
7372
def __init_group__(self, group):
7473
super().__init_group__(group)
7574
if not self._check_user_params():
@@ -130,7 +129,6 @@ class FullRankGroup(Group):
130129
short_name = "full_rank"
131130
alias_names = frozenset(["fr"])
132131

133-
@pytensor.config.change_flags(compute_test_value="off")
134132
def __init_group__(self, group):
135133
super().__init_group__(group)
136134
if not self._check_user_params():
@@ -201,7 +199,6 @@ class EmpiricalGroup(Group):
201199
__param_spec__ = {"histogram": ("s", "d")}
202200
short_name = "empirical"
203201

204-
@pytensor.config.change_flags(compute_test_value="off")
205202
def __init_group__(self, group):
206203
super().__init_group__(group)
207204
self._check_trace()

pymc/variational/operators.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def __init__(self, op: KSD, tf: opvi.TestFunction):
8484
raise opvi.ParametrizationError("Op should be KSD")
8585
super().__init__(op, tf)
8686

87-
@pytensor.config.change_flags(compute_test_value="off")
8887
def __call__(self, nmc, **kwargs) -> list[Variable]:
8988
op: KSD = self.op
9089
grad = op.apply(self.tf)

pymc/variational/opvi.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -149,35 +149,11 @@ def node_property(f):
149149

150150
def wrapper(fn):
151151
ff = append_name(f)(fn)
152-
f_ = pytensor.config.change_flags(compute_test_value="off")(ff)
153-
return property(locally_cachedmethod(f_))
152+
return property(locally_cachedmethod(ff))
154153

155154
return wrapper
156155
else:
157-
f_ = pytensor.config.change_flags(compute_test_value="off")(f)
158-
return property(locally_cachedmethod(f_))
159-
160-
161-
@pytensor.config.change_flags(compute_test_value="ignore")
162-
def try_to_set_test_value(node_in, node_out, s):
163-
_s = s
164-
if s is None:
165-
s = 1
166-
s = pytensor.compile.view_op(pt.as_tensor(s))
167-
if not isinstance(node_in, list | tuple):
168-
node_in = [node_in]
169-
if not isinstance(node_out, list | tuple):
170-
node_out = [node_out]
171-
for i, o in zip(node_in, node_out):
172-
if hasattr(i.tag, "test_value"):
173-
if not hasattr(s.tag, "test_value"):
174-
continue
175-
else:
176-
tv = i.tag.test_value[None, ...]
177-
tv = np.repeat(tv, s.tag.test_value, 0)
178-
if _s is None:
179-
tv = tv[0]
180-
o.tag.test_value = tv
156+
return property(locally_cachedmethod(f))
181157

182158

183159
class ObjectiveUpdates(pytensor.OrderedUpdates):
@@ -320,7 +296,6 @@ def add_obj_updates(
320296
if self.op.returns_loss:
321297
updates.loss = obj_target
322298

323-
@pytensor.config.change_flags(compute_test_value="off")
324299
def step_function(
325300
self,
326301
obj_n_mc=None,
@@ -408,7 +383,6 @@ def step_function(
408383
step_fn = compile([], [], updates=updates, random_seed=seed, **compile_kwargs)
409384
return step_fn
410385

411-
@pytensor.config.change_flags(compute_test_value="off")
412386
def score_function(
413387
self, sc_n_mc=None, more_replacements=None, compile_kwargs=None, fn_kwargs=None
414388
): # pragma: no cover
@@ -449,7 +423,6 @@ def score_function(
449423
seed = self.approx.rng.randint(2**30, dtype=np.int64)
450424
return compile([], loss, random_seed=seed, **compile_kwargs)
451425

452-
@pytensor.config.change_flags(compute_test_value="off")
453426
def __call__(self, nmc, **kwargs):
454427
if "more_tf_params" in kwargs:
455428
m = -1.0
@@ -862,7 +835,6 @@ def _input_type(self, name):
862835
"""
863836
return pt.vector(name)
864837

865-
@pytensor.config.change_flags(compute_test_value="off")
866838
def __init_group__(self, group):
867839
"""Initialize the group."""
868840
if not group:
@@ -1016,7 +988,6 @@ def set_size_and_deterministic(
1016988
self, node: list[Variable], s, d: bool, more_replacements: dict | None = None
1017989
) -> list[Variable]: ...
1018990

1019-
@pytensor.config.change_flags(compute_test_value="off")
1020991
def set_size_and_deterministic(
1021992
self, node: Variable | list[Variable], s, d: bool, more_replacements: dict | None = None
1022993
) -> Variable | list[Variable]:
@@ -1042,7 +1013,6 @@ def set_size_and_deterministic(
10421013
assert not (
10431014
set(makeiter(self.input)) & set(pytensor.graph.graph_inputs(makeiter(node_out)))
10441015
)
1045-
try_to_set_test_value(node, node_out, s)
10461016
assert self.symbolic_random not in set(pytensor.graph.graph_inputs(makeiter(node_out)))
10471017
return node_out
10481018

@@ -1410,7 +1380,6 @@ def make_size_and_deterministic_replacements(self, s, d, more_replacements=None)
14101380
flat2rand.update(more_replacements)
14111381
return flat2rand
14121382

1413-
@pytensor.config.change_flags(compute_test_value="off")
14141383
def set_size_and_deterministic(self, node, s, d, more_replacements=None):
14151384
"""*Dev* - after node is sampled via :func:`symbolic_sample_over_posterior` or :func:`symbolic_single_sample` new random generator can be allocated and applied to node.
14161385
@@ -1435,7 +1404,6 @@ def set_size_and_deterministic(self, node, s, d, more_replacements=None):
14351404
node = graph_replace(node, optimizations, strict=False)
14361405
node = graph_replace(node, flat2rand, strict=False)
14371406
assert not (set(self.symbolic_randoms) & set(pytensor.graph.graph_inputs(makeiter(node))))
1438-
try_to_set_test_value(_node, node, s)
14391407
return node
14401408

14411409
def to_flat_input(self, node, more_replacements=None):
@@ -1487,7 +1455,6 @@ def get_optimization_replacements(self, s, d):
14871455
repl[self.datalogp] = self.single_symbolic_datalogp
14881456
return repl
14891457

1490-
@pytensor.config.change_flags(compute_test_value="off")
14911458
def sample_node(self, node, size=None, deterministic=False, more_replacements=None):
14921459
"""Sample given node or nodes over shared posterior.
14931460
@@ -1519,7 +1486,6 @@ def sample_node(self, node, size=None, deterministic=False, more_replacements=No
15191486
else:
15201487
node_out = self.symbolic_sample_over_posterior(node)
15211488
node_out = self.set_size_and_deterministic(node_out, size, deterministic)
1522-
try_to_set_test_value(node_in, node_out, size)
15231489
return node_out
15241490

15251491
def rslice(self, name):

tests/distributions/test_discrete.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,12 @@ def test_categorical_logp_batch_dims(self, method):
420420
expected_p = [0.2, 0.5] if method is logp else [1.0, 1.0]
421421
np.testing.assert_allclose(expr.exp().eval(), expected_p)
422422

423-
@pytensor.config.change_flags(compute_test_value="raise")
424423
def test_categorical_bounds(self):
425424
with pm.Model():
426425
x = pm.Categorical("x", p=np.array([0.2, 0.3, 0.5]))
427426
assert np.isinf(logp(x, -1).eval())
428427
assert np.isinf(logp(x, 3).eval())
429428

430-
@pytensor.config.change_flags(compute_test_value="raise")
431429
@pytest.mark.parametrize(
432430
"p",
433431
[

0 commit comments

Comments
 (0)