code: def _constrain_L_inf_grad(precondition=True):
      def grad_f(op, grad):
          """Gradient for constrained preconditioned optimization on an L_inf unit
          ball.
  
          This function projects the gradient onto the ball if you are on the
          boundary (or outside!). It always preconditions the gradient so it is the
          direction of steepest descent under L_inf.
  
          Args:
            op: the tensorflow op we're computing the gradient for.
            grad: gradient we need to backprop
  
          Returns:
            (projected if necessary) preconditioned gradient.
          """
          inp = op.inputs[0]
          dim_at_edge = tf.greater_equal(tf.abs(inp), 1.0)
          dim_outward = tf.greater(inp * grad, 0.0)
          if precondition:
              grad = tf.sign(grad)
  
          return tf.where(
              tf.logical_and(dim_at_edge, dim_outward),
              tf.zeros(grad.shape),
              grad,
          )
  
      return grad_f
  
file_path: lucid/lucid/optvis/param/unit_balls.py
imports: import tensorflow
  from lucid.misc.gradient_override import use_gradient
label_desc: Use image node_id 5 for calling a global function with example usage: _constrain_L_inf_grad(precondition) and returns: grad_f, tf