|
| 1 | +from typing import TYPE_CHECKING, Literal, cast |
| 2 | + |
| 3 | +from numpy import convolve as numpy_convolve |
| 4 | + |
| 5 | +from pytensor.graph import Apply, Op |
| 6 | +from pytensor.scalar.basic import upcast |
| 7 | +from pytensor.tensor.basic import as_tensor_variable, join, zeros |
| 8 | +from pytensor.tensor.blockwise import Blockwise |
| 9 | +from pytensor.tensor.math import maximum, minimum |
| 10 | +from pytensor.tensor.type import vector |
| 11 | +from pytensor.tensor.variable import TensorVariable |
| 12 | + |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from pytensor.tensor import TensorLike |
| 16 | + |
| 17 | + |
| 18 | +class Conv1d(Op): |
| 19 | + __props__ = ("mode",) |
| 20 | + gufunc_signature = "(n),(k)->(o)" |
| 21 | + |
| 22 | + def __init__(self, mode: Literal["full", "valid"] = "full"): |
| 23 | + if mode not in ("full", "valid"): |
| 24 | + raise ValueError(f"Invalid mode: {mode}") |
| 25 | + self.mode = mode |
| 26 | + |
| 27 | + def make_node(self, in1, in2): |
| 28 | + in1 = as_tensor_variable(in1) |
| 29 | + in2 = as_tensor_variable(in2) |
| 30 | + |
| 31 | + assert in1.ndim == 1 |
| 32 | + assert in2.ndim == 1 |
| 33 | + |
| 34 | + dtype = upcast(in1.dtype, in2.dtype) |
| 35 | + |
| 36 | + n = in1.type.shape[0] |
| 37 | + k = in2.type.shape[0] |
| 38 | + |
| 39 | + if n is None or k is None: |
| 40 | + out_shape = (None,) |
| 41 | + elif self.mode == "full": |
| 42 | + out_shape = (n + k - 1,) |
| 43 | + else: # mode == "valid": |
| 44 | + out_shape = (max(n, k) - min(n, k) + 1,) |
| 45 | + |
| 46 | + out = vector(dtype=dtype, shape=out_shape) |
| 47 | + return Apply(self, [in1, in2], [out]) |
| 48 | + |
| 49 | + def perform(self, node, inputs, outputs): |
| 50 | + # We use numpy_convolve as that's what scipy would use if method="direct" was passed. |
| 51 | + # And mode != "same", which this Op doesn't cover anyway. |
| 52 | + outputs[0][0] = numpy_convolve(*inputs, mode=self.mode) |
| 53 | + |
| 54 | + def infer_shape(self, fgraph, node, shapes): |
| 55 | + in1_shape, in2_shape = shapes |
| 56 | + n = in1_shape[0] |
| 57 | + k = in2_shape[0] |
| 58 | + if self.mode == "full": |
| 59 | + shape = n + k - 1 |
| 60 | + else: # mode == "valid": |
| 61 | + shape = maximum(n, k) - minimum(n, k) + 1 |
| 62 | + return [[shape]] |
| 63 | + |
| 64 | + def L_op(self, inputs, outputs, output_grads): |
| 65 | + in1, in2 = inputs |
| 66 | + [grad] = output_grads |
| 67 | + |
| 68 | + if self.mode == "full": |
| 69 | + valid_conv = type(self)(mode="valid") |
| 70 | + in1_bar = valid_conv(grad, in2[::-1]) |
| 71 | + in2_bar = valid_conv(grad, in1[::-1]) |
| 72 | + |
| 73 | + else: # mode == "valid": |
| 74 | + full_conv = type(self)(mode="full") |
| 75 | + n = in1.shape[0] |
| 76 | + k = in2.shape[0] |
| 77 | + kmn = maximum(0, k - n) |
| 78 | + nkm = maximum(0, n - k) |
| 79 | + # We need mode="full" if k >= n else "valid" for `in1_bar` (opposite for `in2_bar`), but mode is not symbolic. |
| 80 | + # Instead, we always use mode="full" and slice the result so it behaves like "valid" for the input that's shorter. |
| 81 | + in1_bar = full_conv(grad, in2[::-1]) |
| 82 | + in1_bar = in1_bar[kmn : in1_bar.shape[0] - kmn] |
| 83 | + in2_bar = full_conv(grad, in1[::-1]) |
| 84 | + in2_bar = in2_bar[nkm : in2_bar.shape[0] - nkm] |
| 85 | + |
| 86 | + return [in1_bar, in2_bar] |
| 87 | + |
| 88 | + |
| 89 | +def convolve1d( |
| 90 | + in1: "TensorLike", |
| 91 | + in2: "TensorLike", |
| 92 | + mode: Literal["full", "valid", "same"] = "full", |
| 93 | +) -> TensorVariable: |
| 94 | + """Convolve two one-dimensional arrays. |
| 95 | +
|
| 96 | + Convolve in1 and in2, with the output size determined by the mode argument. |
| 97 | +
|
| 98 | + Parameters |
| 99 | + ---------- |
| 100 | + in1 : (..., N,) tensor_like |
| 101 | + First input. |
| 102 | + in2 : (..., M,) tensor_like |
| 103 | + Second input. |
| 104 | + mode : {'full', 'valid', 'same'}, optional |
| 105 | + A string indicating the size of the output: |
| 106 | + - 'full': The output is the full discrete linear convolution of the inputs, with shape (..., N+M-1,). |
| 107 | + - 'valid': The output consists only of elements that do not rely on zero-padding, with shape (..., max(N, M) - min(N, M) + 1,). |
| 108 | + - 'same': The output is the same size as in1, centered with respect to the 'full' output. |
| 109 | +
|
| 110 | + Returns |
| 111 | + ------- |
| 112 | + out: tensor_variable |
| 113 | + The discrete linear convolution of in1 with in2. |
| 114 | +
|
| 115 | + """ |
| 116 | + in1 = as_tensor_variable(in1) |
| 117 | + in2 = as_tensor_variable(in2) |
| 118 | + |
| 119 | + if mode == "same": |
| 120 | + # We implement "same" as "valid" with padded `in1`. |
| 121 | + in1_batch_shape = tuple(in1.shape)[:-1] |
| 122 | + zeros_left = in2.shape[0] // 2 |
| 123 | + zeros_right = (in2.shape[0] - 1) // 2 |
| 124 | + in1 = join( |
| 125 | + -1, |
| 126 | + zeros((*in1_batch_shape, zeros_left), dtype=in2.dtype), |
| 127 | + in1, |
| 128 | + zeros((*in1_batch_shape, zeros_right), dtype=in2.dtype), |
| 129 | + ) |
| 130 | + mode = "valid" |
| 131 | + |
| 132 | + return cast(TensorVariable, Blockwise(Conv1d(mode=mode))(in1, in2)) |
0 commit comments