You could set the requires_grad attribute of the layer before the second forward pass to False and reset it to True afterwards or alternatively could use the functional API with detached parameters:
lin = nn.Linear(1, 1, bias=False)
x = torch.randn(1, 1)

lin.zero_grad()
out = lin(x)
for param in lin.parameters():
    param.requires_grad = False
out = lin(out)
for param in lin.parameters():
    param.requires_grad = True
out.backward()
print(lin.weight.grad)
> tensor([[0.2955]])

lin.zero_grad()
out = lin(x)
out = F.linear(out, lin.weight.detach())
out.backward()
print(lin.weight.grad)
> tensor([[0.2955]])