Hi John,
Here,
input = mask* input

you are replacing (and hiding) the real input (leaf node) of your graph. If you want to access the gradients of the input, you should keep the original input reference and rename the masked input:
import torch

input = torch.rand((16,16),requires_grad=True)
print(input)
mask = torch.zeros_like(input)
mask[4:12,4:12]=1.0
masked_input = mask * input 
d = torch.mean(masked_input)
d.backward()
print(input.grad.data)

Hope that helps!
Additionally, if you ever want to access the masked_input gradients, which is non-leaf Tensor, you will have to to call masked_input.retain_grad() after its creation.
Please find more info about it here 3