y.requires_grad_() would work on tensors (though the need of a gradient of “external” values is a bit suspicious…).
I normally do:
if x.requires_grad: x.register_hook(…)
to avoid failures during validation / evaluation.