Can I ask how to deal with this situation??

I construct a computational graph with tensor.
I don’t want to hold data, i.e. activation, in the intermediate nodes.
When I call backward, I want to calculate the gradient of intermediate nodes with my custom backward.

Clearly, the first and third requirements can be accomplished by using normal tensor operation and the custom autograd.Function, respectively.
The main concern is how to remove only data, not the computation node itself.
Is there any suggestion about implementing this?? I think I might to hack ctx in the autograd.Function, but there is lack of documentation… 
Some code template I want is to this.
class CustomFunction(autograd.Function):
  def forward(ctx, x):
    output = f(input)
    # TODO: remove input data or deallocate gpu memory for input data. but how?
    return output

  def backward(ctx, grad_output):
    return torch.ones_like(grad_output)