EF.Identity is a custom layer that simply copies the input to the output in the forward pass. I use it to register a hook in the backward pass where I overwrite gradInput. I do this to check (by print) that gradInput of ‘ef1’ is equal to gradOutput of ‘relu1’.
But I notice that in the backward pass, the hook on ‘relu1’ is run before the hook on ‘ef1’. Is this because of some optimization going on that is changing the op graph? If so, is there a way to deactivate this behavior? It’s really important that the backward operations are run following the order for my method.
The reason why this happens is because of the way backward hooks are implemented for nn.Module.
Basically, the hook is done by attaching a hook on the output Variable of the module after the forward pass.
Since in your case the forward pass does not do anything, both hooks will be added to the same Variable during the forward pass. And so the first one added will be called first: the one corresponding to relu1 was added first and so is called first, the one corresponding to ef1 was added after and thus is called after.
Not sure if this is a bug or not…
cc: @apaszke do we want to fix this behaviour? Can we actually do it?