I think the answer depends on your exact problem, maybe you could give more details on your optimization procedure.

Why does your optimization procedure need to be in the forward() staticmethod of some Function anyway?
You could also precompute the Jacobian of your operation in the forward pass and use a custom Function to create an output tensor (result of your optimization) which would apply that Jacobian in its backward() staticmethod.
torch.autograd.enable_grad would be necessary within your forward() staticmethod.