Applies a convolution over a tensor with a given `weight` and optional `bias`. This function supports three different types of `padding` 1. `int` (single value): Applies the same padding value uniformly to all spatial dimensions. 2. `tuple[int, ...]` (length = number of spa
(self, weight:Tensor, bias:Tensor|None=None, groups=1, stride=1, dilation=1, padding:int|Sequence[int]=0,
dtype:DTypeLike|None=None)
| 1223 | return (ret if bias is None else ret.add(bias.reshape(1, -1, *[1 for _ in range(len(HW))]))).contiguous().contiguous_backward() |
| 1224 | |
| 1225 | def conv2d(self, weight:Tensor, bias:Tensor|None=None, groups=1, stride=1, dilation=1, padding:int|Sequence[int]=0, |
| 1226 | dtype:DTypeLike|None=None) -> Tensor: |
| 1227 | """ |
| 1228 | Applies a convolution over a tensor with a given `weight` and optional `bias`. |
| 1229 | |
| 1230 | This function supports three different types of `padding` |
| 1231 | |
| 1232 | 1. `int` (single value): |
| 1233 | Applies the same padding value uniformly to all spatial dimensions. |
| 1234 | |
| 1235 | 2. `tuple[int, ...]` (length = number of spatial dimensions): |
| 1236 | Specifies a distinct padding value for each spatial dimension in the form `(padding_height, padding_width, ...)`. |
| 1237 | |
| 1238 | 3. `tuple[int, ...]` (length = 2 * number of spatial dimensions): |
| 1239 | Specifies explicit padding for each side of each spatial dimension in the form |
| 1240 | `(padding_left, padding_right, padding_top, padding_bottom, ...)`. |
| 1241 | |
| 1242 | NOTE: unlike PyTorch, this implementation is not limited to only 2d convolutions and instead works for any number of dimensions. |
| 1243 | |
| 1244 | See: https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html |
| 1245 | |
| 1246 | ```python exec="true" source="above" session="tensor" result="python" |
| 1247 | t = Tensor.arange(9).reshape(1, 1, 3, 3) |
| 1248 | w = Tensor.ones(1, 1, 2, 2) |
| 1249 | print(t.conv2d(w).numpy()) |
| 1250 | ``` |
| 1251 | """ |
| 1252 | if IMAGE: return self.image_conv2d(weight, bias, groups, stride, dilation, padding, dtype) |
| 1253 | if WINO and all(x == 3 for x in weight.shape[2:]) and stride == dilation == 1: return self._conv2d_winograd(weight, bias, groups, padding, dtype) |
| 1254 | return super().conv2d(weight, bias, groups, stride, dilation, padding, dtype) |
| 1255 | |
| 1256 | def dot(self, w:Tensor, dtype:DTypeLike|None=None) -> Tensor: |
| 1257 | if IMAGE: return self.image_dot(w, dtype) |