|
| 1 | +from typing import Callable, Optional |
| 2 | + |
| 3 | +from pytensor.graph import vectorize_graph |
| 4 | +from pytensor.tensor import TensorVariable |
| 5 | +from pytensor.tensor.utils import _parse_gufunc_signature |
| 6 | + |
| 7 | + |
| 8 | +def vectorize(func: Callable, signature: Optional[str] = None) -> Callable: |
| 9 | + """Create a vectorized version of a python function that takes TensorVariables as inputs and outputs. |
| 10 | +
|
| 11 | + Similar to numpy.vectorize. See respective docstrings for more details. |
| 12 | +
|
| 13 | + Parameters |
| 14 | + ---------- |
| 15 | + func: Callable |
| 16 | + Function that creates the desired outputs from TensorVariable inputs with the core dimensions. |
| 17 | + signature: str, optional |
| 18 | + Generalized universal function signature, e.g., (m,n),(n)->(m) for vectorized matrix-vector multiplication. |
| 19 | + If not provided, it is assumed all inputs have scalar core dimensions. Unlike numpy, the outputs |
| 20 | + can have arbitrary shapes when the signature is not provided. |
| 21 | +
|
| 22 | + Returns |
| 23 | + ------- |
| 24 | + vectorized_func: Callable |
| 25 | + Callable that takes TensorVariables with arbitrarily batched dimensions on the left |
| 26 | + and returns variables whose graphs correspond to the vectorized expressions of func. |
| 27 | +
|
| 28 | + Notes |
| 29 | + ----- |
| 30 | + Unlike numpy.vectorize, the equality of core dimensions implied by the signature is not explicitly asserted. |
| 31 | +
|
| 32 | + To vectorize an existing graph, use `pytensor.graph.replace.vectorize_graph` instead. |
| 33 | +
|
| 34 | +
|
| 35 | + Examples |
| 36 | + -------- |
| 37 | + .. code-block:: python |
| 38 | +
|
| 39 | + import pytensor |
| 40 | + import pytensor.tensor as pt |
| 41 | +
|
| 42 | + def func(x): |
| 43 | + return pt.exp(x) / pt.sum(pt.exp(x)) |
| 44 | +
|
| 45 | + vec_func = pt.vectorize(func, signature="(a)->(a)") |
| 46 | +
|
| 47 | + x = pt.matrix("x") |
| 48 | + y = vec_func(x) |
| 49 | +
|
| 50 | + fn = pytensor.function([x], y) |
| 51 | + fn([[0, 1, 2], [2, 1, 0]]) |
| 52 | + # array([[0.09003057, 0.24472847, 0.66524096], |
| 53 | + # [0.66524096, 0.24472847, 0.09003057]]) |
| 54 | +
|
| 55 | +
|
| 56 | + .. code-block:: python |
| 57 | +
|
| 58 | + import pytensor |
| 59 | + import pytensor.tensor as pt |
| 60 | +
|
| 61 | + def func(x): |
| 62 | + return x[0], x[-1] |
| 63 | +
|
| 64 | + vec_func = pt.vectorize(func, signature="(a)->(),()") |
| 65 | +
|
| 66 | + x = pt.matrix("x") |
| 67 | + y1, y2 = vec_func(x) |
| 68 | +
|
| 69 | + fn = pytensor.function([x], [y1, y2]) |
| 70 | + fn([[-10, 0, 10], [-11, 0, 11]]) |
| 71 | + # [array([-10., -11.]), array([10., 11.])] |
| 72 | +
|
| 73 | + """ |
| 74 | + |
| 75 | + def inner(*inputs): |
| 76 | + if signature is None: |
| 77 | + # Assume all inputs are scalar |
| 78 | + inputs_sig = [()] * len(inputs) |
| 79 | + else: |
| 80 | + inputs_sig, outputs_sig = _parse_gufunc_signature(signature) |
| 81 | + if len(inputs) != len(inputs_sig): |
| 82 | + raise ValueError( |
| 83 | + f"Number of inputs does not match signature: {signature}" |
| 84 | + ) |
| 85 | + |
| 86 | + # Create dummy core inputs by stripping the batched dimensions of inputs |
| 87 | + core_inputs = [] |
| 88 | + for input, input_sig in zip(inputs, inputs_sig): |
| 89 | + if not isinstance(input, TensorVariable): |
| 90 | + raise TypeError( |
| 91 | + f"Inputs to vectorize function must be TensorVariable, got {type(input)}" |
| 92 | + ) |
| 93 | + |
| 94 | + if input.ndim < len(input_sig): |
| 95 | + raise ValueError( |
| 96 | + f"Input {input} has less dimensions than signature {input_sig}" |
| 97 | + ) |
| 98 | + if len(input_sig): |
| 99 | + core_shape = input.type.shape[-len(input_sig) :] |
| 100 | + else: |
| 101 | + core_shape = () |
| 102 | + |
| 103 | + core_input = input.type.clone(shape=core_shape)(name=input.name) |
| 104 | + core_inputs.append(core_input) |
| 105 | + |
| 106 | + # Call function on dummy core inputs |
| 107 | + core_outputs = func(*core_inputs) |
| 108 | + if core_outputs is None: |
| 109 | + raise ValueError("vectorize function returned no outputs") |
| 110 | + |
| 111 | + if signature is not None: |
| 112 | + if isinstance(core_outputs, (list, tuple)): |
| 113 | + n_core_outputs = len(core_outputs) |
| 114 | + else: |
| 115 | + n_core_outputs = 1 |
| 116 | + if n_core_outputs != len(outputs_sig): |
| 117 | + raise ValueError( |
| 118 | + f"Number of outputs does not match signature: {signature}" |
| 119 | + ) |
| 120 | + |
| 121 | + # Vectorize graph by replacing dummy core inputs by original inputs |
| 122 | + outputs = vectorize_graph(core_outputs, replace=dict(zip(core_inputs, inputs))) |
| 123 | + return outputs |
| 124 | + |
| 125 | + return inner |
0 commit comments