I tried the piece of code posted by apaszke, but I got the following error.
RuntimeError: bitwise_xor(): functions with out=... arguments don't support automatic differentiation, but one of the arguments requires grad.

So I am confused how @big_tree and @jdhao managed to get this code to work.
I read the documentation Extending PyTorch 3, but the extending torch.nn (adding a module) section using the LinearFunction defined above, which explicitly defines both forward and backward functions, but this doesn’t seem to be the case in apaszke 's sample code above. So there is a way to make his code work, without explicitly encoding the gradient myself?