code: def verify_backbone_fp16(
      model_cls, cfg, ctx, inputs, atol=1e-2, rtol=1e-2, check_amp=True
  ):
      """Test whether the backbone model has the comparable parameter gradient +
  
      Parameters
      ----------
      model_cls
          The modeling class
      cfg
          The configuration
      ctx
          The context
      inputs
          The input tensors of the model. We will
      atol
          The absolute tolerance
      rtol
          The relative tolerance
      check_amp
          Whether to check the AMP process. You will need to ensure that there is no
          randomness in the model when it is turned on.
  
      """
      model_fp32 = model_cls.from_cfg(cfg, dtype="float32")
      model_fp32.initialize(ctx=ctx)
      model_fp32.hybridize()
      # Check forward
      fp32_inputs = move_to_ctx(inputs, ctx=ctx)
      outputs_fp32 = model_fp32(*fp32_inputs)
      mx.npx.waitall()
      # Check forward of fp16
      model_fp16 = model_cls.from_cfg(cfg, dtype="float16")
      model_fp16.share_parameters(model_fp32.collect_params())
      model_fp16.cast("float16")
      model_fp16.hybridize()
      for param in model_fp16.collect_params().values():
          assert param.dtype == "float16"
      fp16_inputs = move_to_ctx(_cast_nested_to_fp16(inputs), ctx=ctx)
      outputs_fp16 = model_fp16(*fp16_inputs)
      mx.npx.waitall()
      _match_struct_output(
          outputs_fp16, outputs_fp32, atol=atol, rtol=rtol
      )
      if check_amp:
          from mxnet import amp
  
          amp.init()
          # Reconstruct the fp32 model
          model_fp32 = model_cls.from_cfg(cfg, dtype="float32")
          model_fp32.initialize(ctx=ctx)
          model_fp32.hybridize()
          trainer = mx.gluon.Trainer(
              model_fp32.collect_params(),
              "adam",
              {
                  "learning_rate": 1e-3,
                  "wd": 1e-4,
                  "multi_precision": True,
              },
              update_on_kvstore=False,
          )
          amp.init_trainer(trainer)
          with mx.autograd.record():
              outputs_amp = model_fp32(*fp32_inputs)
              if not isinstance(outputs_amp, (tuple, list)):
                  loss = outputs_amp.mean()
              else:
                  loss = sum([ele.mean() for ele in outputs_amp])
              with amp.scale_loss(loss, trainer) as scaled_loss:
                  mx.autograd.backward(scaled_loss)
          trainer.step(1)
          mx.npx.waitall()
  
file_path: gluon-nlp/src/gluonnlp/utils/testing.py
imports: import numpy.testing
  import numpy
  import mxnet
  from mxnet.util import use_np
  from .parameter import move_to_ctx
label_desc: Use image node_id 6 for calling a global function with example usage: verify_backbone_fp16(model_cls, cfg, ctx, inputs, atol, rtol, check_amp) without return types