MCPcopy Index your code
hub / github.com/tinygrad/tinygrad / conv2d

Method conv2d

tinygrad/tensor.py:1225–1254  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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)

Callers 15

test_conv2dFunction · 0.95
test_many_simpleMethod · 0.95
torch_progFunction · 0.45
tiny_jitFunction · 0.45
simple_conv.pyFile · 0.45
test_biased_conv2dMethod · 0.45
convolution_overrideableFunction · 0.45
patch_embedMethod · 0.45
__call__Method · 0.45
forwardMethod · 0.45

Calls 2

image_conv2dMethod · 0.95
_conv2d_winogradMethod · 0.95

Tested by 15

test_conv2dFunction · 0.76
test_many_simpleMethod · 0.76
test_biased_conv2dMethod · 0.36
test_conv2d_basicMethod · 0.36
test_conv2d_paddedMethod · 0.36
test_conv2d_groupsMethod · 0.36
test_conv2d_3dMethod · 0.36
test_profileMethod · 0.36