Assume I have a working torch::autograd::Function, and that for the forward method I actually have two ways of computing it, call them forward_1 and forward_2. They are such that if one were to do only inference, forward_1 would be faster, and if one intends to backprop then forward_2+backward is faster than forward_1+backward (basically forward_2 will pre-compute some quantities in anticipation of libtorch later calling backward which can then optimally build upon those quantities to compute the needed gradients efficiently).
So ideally, I’d love to know if there’s a way that could tell me inside torch::autograd::Function::forward whether this method is being called with autograd on or off (ie with or without a no_grad guard), so that I could do something like:
if(autograd_is_on){
  forward_2(...);
  ctx->saved_data["..."] = ...; // save the pre-computed stuff that backward will need
} else {
  forward_1(...);
}