code: def test_eliminate_common_expr():
      # helper function to test a single model
      def check_cse_on_symbol(
          sym, expected_savings, check_data, **kwargs
      ):
          inputs = sym.list_inputs()
          shapes = {inp: kwargs[inp].shape for inp in inputs}
          rtol = {
              "float16": 1e-2,
              "float32": 1.5e-6,
              "float64": 1.5e-6,
          }
          atol = {
              "float16": 1e-3,
              "float32": 1e-7,
              "float64": 1e-7,
          }
          for dtype in ["float16", "float32", "float64"]:
              data = {inp: kwargs[inp].astype(dtype) for inp in inputs}
              for grad_req in ["write", "add"]:
                  type_dict = {inp: dtype for inp in inputs}
                  with environment(
                      {"MXNET_ELIMINATE_COMMON_EXPR": "0"}
                  ):
                      orig_exec = sym._simple_bind(
                          ctx=mx.cpu(0),
                          grad_req=grad_req,
                          type_dict=type_dict,
                          **shapes,
                      )
                  with environment(
                      {"MXNET_ELIMINATE_COMMON_EXPR": "1"}
                  ):
                      cse_exec = sym._simple_bind(
                          ctx=mx.cpu(0),
                          grad_req=grad_req,
                          type_dict=type_dict,
                          **shapes,
                      )
                  fwd_orig = orig_exec.forward(is_train=True, **data)
                  out_grads = [mx.nd.ones_like(arr) for arr in fwd_orig]
                  orig_exec.backward(out_grads=out_grads)
                  fwd_cse = cse_exec.forward(is_train=True, **data)
                  cse_exec.backward(out_grads=out_grads)
                  if check_data:
                      for orig, cse in zip(fwd_orig, fwd_cse):
                          np.testing.assert_allclose(
                              orig.asnumpy(),
                              cse.asnumpy(),
                              rtol=rtol[dtype],
                              atol=atol[dtype],
                          )
                      for orig, cse in zip(
                          orig_exec.grad_arrays, cse_exec.grad_arrays
                      ):
                          if orig is None and cse is None:
                              continue
                          assert orig is not None
                          assert cse is not None
                          np.testing.assert_allclose(
                              orig.asnumpy(),
                              cse.asnumpy(),
                              rtol=rtol[dtype],
                              atol=atol[dtype],
                          )
                  orig_sym_internals = (
                      orig_exec.get_optimized_symbol().get_internals()
                  )
                  cse_sym_internals = (
                      cse_exec.get_optimized_symbol().get_internals()
                  )
                  # test that the graph has been simplified as expected
                  assert (
                      len(cse_sym_internals) + expected_savings
                  ) == len(orig_sym_internals)
  
      a = mx.sym.Variable("a")
      b = mx.sym.Variable("b")
      c = mx.sym.Variable("c")
      shape = rand_shape_nd(2)
      arr1 = mx.random.uniform(shape=shape)
      arr2 = mx.random.uniform(shape=shape)
      arr3 = mx.random.uniform(shape=shape)
  
      check_cse_on_symbol(
          (a + 1) + (a + 2),
          expected_savings=0,
          check_data=True,
          a=arr1,
          b=arr2,
      )
      check_cse_on_symbol(
          (a + b) + (a + b),
          expected_savings=1,
          check_data=True,
          a=arr1,
          b=arr2,
      )
      check_cse_on_symbol(
          ((a + b) + c) + ((a + b) + c),
          expected_savings=2,
          check_data=True,
          a=arr1,
          b=arr2,
          c=arr3,
      )
      d = a + 1
  
      # a*d node gets eliminated, but then a copy is inserted to isolate the outputs, so no net gain.
      check_cse_on_symbol(
          mx.sym.Group([a * d, a * d]),
          expected_savings=0,
          check_data=True,
          a=arr1,
      )
  
      # a*d node gets eliminated, then the duplicated add-of-b, but then a copy is added for net of 1.
      check_cse_on_symbol(
          mx.sym.Group([a * d + b, a * d + b]),
          expected_savings=1,
          check_data=True,
          a=arr1,
          b=arr2,
      )
  
      # dropout uses a resource that precludes any optimization
      check_cse_on_symbol(
          mx.sym.Dropout(a) + mx.sym.Dropout(a),
          expected_savings=0,
          check_data=False,
          a=arr1,
      )
  
imports: import copy
  import sys
  import os
  import logging
  import re
  import json
  import mxnet
  import numpy
  from common import assertRaises, models, TemporaryDirectory
  from mxnet.base import NotImplementedForSymbol
  from mxnet.test_utils import discard_stderr, rand_shape_nd, use_np, environment
  from mxnet.util import np_shape
  import pickle
label_desc: Use image node_id 21 for calling a global function with example usage: test_eliminate_common_expr() without return types