code: def _recompute_grad(fn, args, use_entire_scope, train_vars=None):
      """See recompute_grad."""
  
      cached_vs = []
  
      def grad_fn(inputs, variables, outputs, output_grads):
          """Recompute outputs for gradient computation."""
          del outputs
          variables = [underlying_variable_ref(v) for v in variables]
          # Recompute outputs
          with tf.control_dependencies(
              [o for o in output_grads if o is not None]
          ):
              with tf.compat.v1.variable_scope(
                  cached_vs[0], reuse=True
              ):
                  outputs = fn(*inputs)
  
          if not isinstance(outputs, (list, tuple)):
              outputs = [outputs]
          outputs = [o for o in outputs if isinstance(o, tf.Tensor)]
          input_vars = [
              v for v in inputs + variables if is_numerical_tensor
          ]
  
          grads = tf.gradients(
              ys=outputs, xs=input_vars, grad_ys=output_grads
          )
  
          grad_inputs = grads[: len(inputs)]
          grad_vars = grads[len(inputs) :]
          return grad_inputs, grad_vars
  
      @fn_with_custom_grad(
          grad_fn,
          use_entire_scope=use_entire_scope,
          train_vars=train_vars,
      )
      def fn_with_recompute(*args):
          cached_vs.append(tf.compat.v1.get_variable_scope())
          return fn(*args)
  
      return fn_with_recompute(*args)
  
file_path: finetune/finetune/optimizers/recompute_grads.py
imports: import functools
  import tensorflow
  from tensorflow.python.framework import function, ops
  from finetune.util.optimize_loss import is_numerical_tensor
label_desc: Use image node_id 5 for calling a global function with example usage: _recompute_grad(fn, args, use_entire_scope, train_vars) and returns: fn_with_recompute, grad_inputs,  grad_vars, fn