All Problems Description Template Solution

Conv2d

Convolution, unfold, stride/padding

Medium Fundamentals

Problem Description

Implement 2D convolution from scratch.

Signature

def my_conv2d(x, weight, bias=None, stride=1, padding=0): # x: (B, C_in, H, W), weight: (C_out, C_in, kH, kW) # Returns: (B, C_out, H_out, W_out)

Rules

• Do NOT use F.conv2d or nn.Conv2d

• Support stride and padding parameters

F.pad for zero-padding is allowed

Template

Implement the function below. Use only basic PyTorch operations.

# ✏️ YOUR IMPLEMENTATION HERE def my_conv2d(x, weight, bias=None, stride=1, padding=0): pass # extract patches, apply kernel, handle stride/padding

Test Your Implementation

Use this code to debug before submitting.

# 🧪 Debug x = torch.randn(1, 3, 8, 8) w = torch.randn(16, 3, 3, 3) print('Output:', my_conv2d(x, w).shape) print('Match:', torch.allclose(my_conv2d(x, w), F.conv2d(x, w), atol=1e-4))

Reference Solution

Try solving it yourself first! Click below to reveal the solution.

# ✅ SOLUTION def my_conv2d(x, weight, bias=None, stride=1, padding=0): if padding > 0: x = F.pad(x, [padding] * 4) B, C_in, H, W = x.shape C_out, _, kH, kW = weight.shape H_out = (H - kH) // stride + 1 W_out = (W - kW) // stride + 1 patches = x.unfold(2, kH, stride).unfold(3, kW, stride) out = torch.einsum('bihwjk,oijk->bohw', patches, weight) if bias is not None: out = out + bias.view(1, -1, 1, 1) return out

Tips

Run Locally

For interactive practice with auto-grading, run TorchCode locally:
pip install torch-judge then use check("conv2d")

Key Concepts

Convolution, unfold, stride/padding

Conv2d

Description Template Test Solution Tips