No, sorry, I’m wrong and the output won’t require gradients.
In that case, you could detach the parameters of layerA and use the functional API, which should work:
torch.manual_seed(2809)

layerA = nn.Linear(10, 10)
layerB = nn.Linear(10, 10)

x = torch.randn(1, 10)

out = layerA(x)
out = layerB(out)

out = F.linear(out, layerA.weight.detach(), layerA.bias.detach())

out.mean().backward()

print(layerA.weight.grad)