|
4 | 4 | import pytensor.tensor as pt
|
5 | 5 | from pytensor import config, function
|
6 | 6 | from pytensor.gradient import NullTypeGradError, grad
|
7 |
| -from pytensor.graph.replace import vectorize_node |
| 7 | +from pytensor.graph.replace import vectorize_graph |
8 | 8 | from pytensor.raise_op import Assert
|
9 | 9 | from pytensor.tensor.math import eq
|
10 | 10 | from pytensor.tensor.random import normal
|
@@ -241,62 +241,59 @@ def test_multivariate_rv_infer_static_shape():
|
241 | 241 | assert mv_op(param1, param2, size=(10, 2)).type.shape == (10, 2, 3)
|
242 | 242 |
|
243 | 243 |
|
244 |
| -def test_vectorize_node(): |
| 244 | +def test_vectorize(): |
245 | 245 | vec = tensor(shape=(None,))
|
246 | 246 | mat = tensor(shape=(None, None))
|
247 | 247 |
|
248 | 248 | # Test without size
|
249 |
| - node = normal(vec).owner |
250 |
| - new_inputs = node.inputs.copy() |
251 |
| - new_inputs[3] = mat # mu |
252 |
| - vect_node = vectorize_node(node, *new_inputs) |
| 249 | + out = normal(vec) |
| 250 | + vect_node = vectorize_graph(out, {vec: mat}).owner |
253 | 251 | assert vect_node.op is normal
|
254 | 252 | assert vect_node.inputs[3] is mat
|
255 | 253 |
|
256 | 254 | # Test with size, new size provided
|
257 |
| - node = normal(vec, size=(3,)).owner |
258 |
| - new_inputs = node.inputs.copy() |
259 |
| - new_inputs[1] = (2, 3) # size |
260 |
| - new_inputs[3] = mat # mu |
261 |
| - vect_node = vectorize_node(node, *new_inputs) |
| 255 | + size = pt.as_tensor(np.array((3,), dtype="int64")) |
| 256 | + out = normal(vec, size=size) |
| 257 | + vect_node = vectorize_graph(out, {vec: mat, size: (2, 3)}).owner |
262 | 258 | assert vect_node.op is normal
|
263 | 259 | assert tuple(vect_node.inputs[1].eval()) == (2, 3)
|
264 | 260 | assert vect_node.inputs[3] is mat
|
265 | 261 |
|
266 | 262 | # Test with size, new size not provided
|
267 |
| - node = normal(vec, size=(3,)).owner |
268 |
| - new_inputs = node.inputs.copy() |
269 |
| - new_inputs[3] = mat # mu |
270 |
| - vect_node = vectorize_node(node, *new_inputs) |
| 263 | + out = normal(vec, size=(3,)) |
| 264 | + vect_node = vectorize_graph(out, {vec: mat}).owner |
271 | 265 | assert vect_node.op is normal
|
272 | 266 | assert vect_node.inputs[3] is mat
|
273 | 267 | assert tuple(
|
274 | 268 | vect_node.inputs[1].eval({mat: np.zeros((2, 3), dtype=config.floatX)})
|
275 | 269 | ) == (2, 3)
|
276 | 270 |
|
277 | 271 | # Test parameter broadcasting
|
278 |
| - node = normal(vec).owner |
279 |
| - new_inputs = node.inputs.copy() |
280 |
| - new_inputs[3] = tensor("mu", shape=(10, 5)) # mu |
281 |
| - new_inputs[4] = tensor("sigma", shape=(10,)) # sigma |
282 |
| - vect_node = vectorize_node(node, *new_inputs) |
| 272 | + mu = vec |
| 273 | + sigma = pt.as_tensor(np.array(1.0)) |
| 274 | + out = normal(mu, sigma) |
| 275 | + new_mu = tensor("mu", shape=(10, 5)) |
| 276 | + new_sigma = tensor("sigma", shape=(10,)) |
| 277 | + vect_node = vectorize_graph(out, {mu: new_mu, sigma: new_sigma}).owner |
283 | 278 | assert vect_node.op is normal
|
284 | 279 | assert vect_node.default_output().type.shape == (10, 5)
|
285 | 280 |
|
286 | 281 | # Test parameter broadcasting with non-expanding size
|
287 |
| - node = normal(vec, size=(5,)).owner |
288 |
| - new_inputs = node.inputs.copy() |
289 |
| - new_inputs[3] = tensor("mu", shape=(10, 5)) # mu |
290 |
| - new_inputs[4] = tensor("sigma", shape=(10,)) # sigma |
291 |
| - vect_node = vectorize_node(node, *new_inputs) |
| 282 | + mu = vec |
| 283 | + sigma = pt.as_tensor(np.array(1.0)) |
| 284 | + out = normal(mu, sigma, size=(5,)) |
| 285 | + new_mu = tensor("mu", shape=(10, 5)) |
| 286 | + new_sigma = tensor("sigma", shape=(10,)) |
| 287 | + vect_node = vectorize_graph(out, {mu: new_mu, sigma: new_sigma}).owner |
292 | 288 | assert vect_node.op is normal
|
293 | 289 | assert vect_node.default_output().type.shape == (10, 5)
|
294 | 290 |
|
295 | 291 | # Test parameter broadcasting with expanding size
|
296 |
| - node = normal(vec, size=(2, 5)).owner |
297 |
| - new_inputs = node.inputs.copy() |
298 |
| - new_inputs[3] = tensor("mu", shape=(10, 5)) # mu |
299 |
| - new_inputs[4] = tensor("sigma", shape=(10,)) # sigma |
300 |
| - vect_node = vectorize_node(node, *new_inputs) |
| 292 | + mu = vec |
| 293 | + sigma = pt.as_tensor(np.array(1.0)) |
| 294 | + out = normal(mu, sigma, size=(2, 5)) |
| 295 | + new_mu = tensor("mu", shape=(1, 5)) |
| 296 | + new_sigma = tensor("sigma", shape=(10,)) |
| 297 | + vect_node = vectorize_graph(out, {mu: new_mu, sigma: new_sigma}).owner |
301 | 298 | assert vect_node.op is normal
|
302 | 299 | assert vect_node.default_output().type.shape == (10, 2, 5)
|
0 commit comments