You need to create instances of all your layers in constructor. Then call these instances in forward pass.
def __init__(self):
    self.conv1 = nn.Conv2d(1,32,5)

def forward(self, x):
    x = self.conv1(x)

as for the dynamic shape you can use input with largest shape possible and pad it with zeroes