code: def _reference_grad(
      self, x, grad_y, scale, mean, var, epsilon, data_format
  ):
      # Use the following formulas to calculate gradients:
      # grad_scale =
      #   sum(grad_y * (x - mean)) * rsqrt(var + epsilon)
      #
      # grad_offset = sum(output_y)
      #
      # grad_x =
      #   1/N * scale * rsqrt(var + epsilon) * (N * grad_y - sum(grad_y) -
      #   (x - mean) * sum(grad_y * (x - mean)) / (var + epsilon))
      if data_format != "NHWC":
          raise ValueError(
              "data_format must be NHWC, got %s." % data_format
          )
      grad_x = (
          scale
          * (
              grad_y
              - np.mean(grad_y, axis=(0, 1, 2))
              - (x - mean)
              * np.mean(grad_y * (x - mean), axis=(0, 1, 2))
              / (var + epsilon)
          )
          / np.sqrt(var + epsilon)
      )
      grad_scale = np.sum(
          grad_y * (x - mean) / np.sqrt(var + epsilon), axis=(0, 1, 2)
      )
      grad_offset = np.sum(grad_y, axis=(0, 1, 2))
      return grad_x, grad_scale, grad_offset
  
imports: from absl.testing import parameterized
  import numpy
  from tensorflow.compiler.tests import test_utils
  from tensorflow.compiler.tests import xla_test
  from tensorflow.python.framework import test_util
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import gen_nn_ops
  from tensorflow.python.ops import gradient_checker
  from tensorflow.python.ops import nn
  from tensorflow.python.platform import test
label_desc: Use image node_id 2 for calling the FusedBatchNormTest obj's underlying member method code with example usage: obj._reference_grad(x, grad_y, scale, mean, var, epsilon, data_format) and returns: grad_x,  grad_scale,  grad_offset