The gradients will be zero at the masked points:
x = torch.randn(1, 10)
weight = nn.Parameter(torch.randn(5, 10))
adj = torch.randint(0, 2, (5, 10)).float()

out = torch.matmul(x, (adj * weight).t())
out.mean().backward()

print(weight.grad)
> tensor([[ 0.1776, -0.1401,  0.0000, -0.0000,  0.0000,  0.0000,  0.0000, -0.0000,
         -0.0000, -0.1063],
        [ 0.1776, -0.0000,  0.0133, -0.0294,  0.0000,  0.3073,  0.1285, -0.0000,
         -0.0000, -0.0000],
        [ 0.1776, -0.0000,  0.0000, -0.0000,  0.0000,  0.3073,  0.1285, -0.0000,
         -0.0000, -0.0000],
        [ 0.1776, -0.1401,  0.0000, -0.0000,  0.0944,  0.3073,  0.1285, -0.1295,
         -0.0000, -0.1063],
        [ 0.1776, -0.1401,  0.0133, -0.0000,  0.0944,  0.0000,  0.0000, -0.1295,
         -0.1472, -0.1063]])
print(adj)
> tensor([[1., 1., 0., 0., 0., 0., 0., 0., 0., 1.],
        [1., 0., 1., 1., 0., 1., 1., 0., 0., 0.],
        [1., 0., 0., 0., 0., 1., 1., 0., 0., 0.],
        [1., 1., 0., 0., 1., 1., 1., 1., 0., 1.],
        [1., 1., 1., 0., 1., 0., 0., 1., 1., 1.]])

and won’t add any information to the gradient which flows to the preceding layer, if that’s what you are looking for.