Hi,
The way to do this is to implement a new custom Function (instructions here https://pytorch.org/docs/stable/notes/extending.html 1) that will encapsulate both these ops.
You will then have the control over what is saved for the backward and you will need to implement the backward yourself to match this new computation that you want to do.