Hi,
I don’t think I can thing of a “safe” way to do that. Mainly because you will have to change the state of the Module while the backward is running to avoid the infinite recursion.
So calling two backward concurrently will always do something bad.
Assuming that you never do concurrent backwards, you can set a flag on the Module to know if this is the “top” backward or not:
class MyLayer(nn.Module):
        def __init__(self, ...):
                ...  
                self.in_backward = False

	def forward(x):
		x.require_grad_()
		y = self.net(x)

		def backward_hook(grad):
			if self.in_backward:
                # If we're already in backward, do nothing
				return grad
			return autograd.grad(y, x, grad)[0]

		y.register_hook(backward_hook)
		return y