Also what consider the following example where I use binary vector a to produce another variable b and compute gradients on both:
import torch

a = (torch.randn(5) > .5).float().requires_grad_(True)
print ("a", a)

b = a.abs().mean()*(torch.sign(a))
print ("b", b)

b.retain_grad()
b.sum().backward()

print ("a.grad", a.grad)
print ("b.grad", b.grad)