implemented backward for Pad2D & test
MODIFY
test/test_ops.py
test/test_ops.py
@@ -64,7 +64,7 @@ class TestOps(unittest.TestCase):
helper_test_op([(45,65)], lambda x: torch.nn.LogSoftmax(dim=1)(x), Tensor.logsoftmax, atol=1e-5, gpu=self.gpu)
def test_pad2d(self):
- helper_test_op([(3,3,3,3)], lambda x: torch.nn.functional.pad(x, (1,1,1,1)), lambda x: x.pad2d(padding=(1,1,1,1)), gpu=self.gpu, forward_only=True)
+ helper_test_op([(3,3,3,3)], lambda x: torch.nn.functional.pad(x, (1,1,1,1)), lambda x: x.pad2d(padding=(1,1,1,1)), gpu=self.gpu)
def test_conv2d(self):
for bs in [1,8]:

MODIFY
tinygrad/ops.py
tinygrad/ops.py
@@ -97,13 +97,15 @@ register('matmul', Dot)
class Pad2D(Function):
@staticmethod
def forward(ctx, x, padding=None):
+ ctx.save_for_backward(padding)
return np.pad(x,
((0,0), (0,0),
(padding[0], padding[1]), (padding[2], padding[3])))
@staticmethod
def backward(ctx, grad_output):
- raise Exception("write this")
+ padding, = ctx.saved_tensors
+ return grad_output[..., padding[0]:-padding[1], padding[2]:-padding[3]]
register('pad2d', Pad2D)
class Reshape(Function):

implemented backward for Pad2D & test (#73)