code: def run_test(cls, num_elem=1, aggregate=False):
      aggregate = aggregate or num_elem > 1
      rescale_factor = 10
      eta, lr, wd, epsilon = 1, 1, 0.1, 1e-8
      beta1, beta2 = 0.9, 0.999
      clip_gradient = np.random.uniform(rescale_factor, rescale_factor)
      weight, grad, m, v, etas, lrs, wds, weight_ref = (
          [],
          [],
          [],
          [],
          [],
          [],
          [],
          [],
      )
      for i in range(num_elem):
          shape = (
              np.random.randint(3, high=10),
              np.random.randint(3, high=10),
          )
          weight.append(mx.nd.random.uniform(shape=shape))
          grad.append(mx.nd.random.uniform(-1.0, 1.0, shape=shape))
          m.append(mx.nd.random.uniform(shape=shape))
          v.append(mx.nd.random.uniform(shape=shape))
          etas.append(eta - 1 / np.random.uniform(9, 10))
          lrs.append(lr - 1 / np.random.uniform(9, 10))
          wds.append(wd - 1 / np.random.uniform(95, 105))
          weight_ref.append(weight[i].copy())
  
      if aggregate:
          kwargs = {"etas": etas, "lrs": lrs, "wds": wds}
      else:
          kwargs = {"eta": etas[0], "lr": lrs[0], "wd": wds[0]}
  
      kwargs.update(
          [
              ("epsilon", epsilon),
              ("beta1", beta1),
              ("beta2", beta2),
              ("clip_gradient", clip_gradient),
          ]
      )
  
      # Test 1: Update is skipped for rescale = nan scalar
      rescale_grad = mx.nd.array([rescale_factor])
      tested_grad = [
          rescale_grad * 0,
          rescale_grad * np.nan,
          rescale_grad * np.inf,
      ]
      tested_rescaled_grad = [np.nan]
      tested_rescaled_grad.extend(tested_grad)
  
      for rescaled_grad in tested_rescaled_grad:
          if aggregate:
              cls.fn_multi_update(
                  weight,
                  grad,
                  m,
                  v,
                  rescaled_grad,
                  out=weight,
                  **kwargs,
              )
          else:
              cls.fn_update(
                  weight[0],
                  grad[0],
                  m[0],
                  v[0],
                  rescaled_grad,
                  out=weight[0],
                  **kwargs,
              )
          # weights should remain unchanged
          for j in range(num_elem):
              assert_almost_equal(weight_ref[j], weight[j])
  
      # Test 2: Same as Test 1 for multi-precision update
      weight_fp16, grad_fp16, weight_fp16_refs = [], [], []
      for i in range(num_elem):
          weight_fp16.append(weight[i].astype("float16"))
          grad_fp16.append(grad[i].astype("float16"))
          weight_fp16_refs.append(weight_fp16[i].copy())
  
      for rescaled_grad in tested_grad:
          if aggregate:
              cls.fn_multi_mp_update(
                  weight_fp16,
                  grad_fp16,
                  m,
                  v,
                  weight,
                  rescaled_grad,
                  out=weight_fp16,
                  **kwargs,
              )
          else:
              cls.fn_mp_update(
                  weight_fp16[0],
                  grad_fp16[0],
                  m[0],
                  v[0],
                  weight[0],
                  rescaled_grad,
                  out=weight_fp16[0],
                  **kwargs,
              )
          # weights should remain unchanged
          for i in range(num_elem):
              assert_almost_equal(weight_ref[i], weight[i])
              assert_almost_equal(weight_fp16_refs[i], weight_fp16[i])
  
      # Test 3: Reference normal update
      grad_rescale, weight_test, m_refs, v_refs, weight_refs = (
          [],
          [],
          [],
          [],
          [],
      )
      for i in range(num_elem):
          grad_rescale.append(rescale_grad * grad[i])
          m_ref, v_ref, weight_ref = cls.ref_impl(
              m[i],
              v[i],
              weight[i],
              grad_rescale[i],
              beta1,
              beta2,
              lrs[i],
              etas[i],
              wds[i],
              epsilon,
              clip_gradient,
          )
          m_refs.append(m_ref)
          v_refs.append(v_ref)
          weight_refs.append(weight_ref)
          weight_test.append(weight[i].copy())
      # op normal update
      if aggregate:
          cls.fn_multi_update(
              weight_test,
              grad,
              m,
              v,
              rescale_grad,
              out=weight_test,
              **kwargs,
          )
      else:
          cls.fn_update(
              weight_test[0],
              grad[0],
              m[0],
              v[0],
              rescale_grad,
              out=weight_test[0],
              **kwargs,
          )
      # Compare results
      atol = 1e-4 if aggregate else 1e-5
      rtol = 1e-4 if aggregate else None
      for i in range(num_elem):
          assert_almost_equal(
              weight_refs[i], weight_test[i], rtol=rtol, atol=atol
          )
          assert_almost_equal(m_refs[i], m[i], rtol=rtol, atol=atol)
          assert_almost_equal(v_refs[i], v[i], atol=atol)
  
      # Test 4: Reference normal multi-precision update
      grad_rescale, m_refs, v_refs, weight_refs, weight_fp16_refs = (
          [],
          [],
          [],
          [],
          [],
      )
      for i in range(num_elem):
          grad_rescale.append(
              rescale_grad * grad_fp16[i].astype("float32")
          )
          m_ref, v_ref, weight_ref = cls.ref_impl(
              m[i],
              v[i],
              weight[i],
              grad_rescale[i],
              beta1,
              beta2,
              lrs[i],
              etas[i],
              wds[i],
              epsilon,
              clip_gradient,
          )
          m_refs.append(m_ref)
          v_refs.append(v_ref)
          weight_refs.append(weight_ref)
          weight_fp16_refs.append(weight_ref.astype("float16"))
      # op normal multi-precision update
      if aggregate:
          cls.fn_multi_mp_update(
              weight_fp16,
              grad_fp16,
              m,
              v,
              weight,
              rescale_grad,
              out=weight_fp16,
              **kwargs,
          )
      else:
          cls.fn_mp_update(
              weight_fp16[0],
              grad_fp16[0],
              m[0],
              v[0],
              weight[0],
              rescale_grad,
              out=weight_fp16[0],
              **kwargs,
          )
      # Compare results
      for i in range(num_elem):
          assert_almost_equal(m_refs[i], m[i], rtol=rtol, atol=atol)
          assert_almost_equal(v_refs[i], v[i], atol=atol)
          assert_almost_equal(
              weight_refs[i], weight[i], rtol=rtol, atol=atol
          )
          assert_almost_equal(
              weight_fp16_refs[i], weight_fp16[i], rtol=1e-3, atol=atol
          )
  
file_path: mxnet/tests/python/unittest/test_contrib_optimizer.py
imports: import itertools
  import numpy
  import mxnet
  from mxnet.test_utils import *
  import pytest
  from common import xfail_when_nonstandard_decimal_separator
label_desc: Use image node_id 2 for calling the _AdamLikeTestHelper obj's underlying member method code with example usage: obj.run_test(cls, num_elem, aggregate) without return types