code: def _verify_cond(
      cond_func,
      then_func,
      else_func,
      input_var_shapes,
      free_var_shapes,
      is_train,
  ):
      def _create_symbol(prefix, i):
          return mx.sym.var(prefix + str(i))
  
      def _create_array(shape):
          return mx.nd.random.uniform(-1.0, 1.0, shape=shape)
  
      def _to_numpy_list(arrays):
          return [x.asnumpy() if x is not None else x for x in arrays]
  
      def _merge_dict(*dicts):
          result = {}
          for item in dicts:
              result.update(item)
          return result
  
      _input_syms = [
          _create_symbol("InputVar", i)
          for i, _ in enumerate(input_var_shapes)
      ]
      _free_syms = [
          _create_symbol("FreeVar", i)
          for i, _ in enumerate(free_var_shapes)
      ]
      _input_vars = [_create_array(x) for x in input_var_shapes]
      _free_vars = [_create_array(x) for x in free_var_shapes]
      _args_dict = _merge_dict(
          {"InputVar" + str(i): x for i, x in enumerate(_input_vars)},
          {"FreeVar" + str(i): x for i, x in enumerate(_free_vars)},
      )
  
      def _get_imperative_result():
          free_vars = [x.copy() for x in _free_vars]
          input_vars = [x.copy() for x in _input_vars]
          out_grads = []
          if is_train:
              for var in free_vars + input_vars:
                  var.attach_grad()
          with mx.autograd.record(train_mode=is_train):
              outputs = mx.nd.contrib.cond(
                  pred=cond_func(input_vars, free_vars),
                  then_func=lambda: then_func(input_vars, free_vars),
                  else_func=lambda: else_func(input_vars, free_vars),
              )
              outputs = _as_list(outputs)
              outputs = [x * 2 for x in outputs]
              grads = []
              if is_train:
                  out_grads = [_create_array(x.shape) for x in outputs]
                  cat_out = mx.nd.concat(
                      *[x.reshape(-1) for x in outputs], dim=0
                  )
                  cat_out.backward(
                      out_grad=mx.nd.concat(
                          *[x.reshape(-1) for x in out_grads], dim=0
                      )
                  )
                  grads = [
                      free_vars[i].grad
                      for i, _ in enumerate(free_var_shapes)
                  ] + [
                      input_vars[i].grad
                      for i, _ in enumerate(input_var_shapes)
                  ]
              return (
                  _to_numpy_list(outputs),
                  _to_numpy_list(grads),
                  out_grads,
              )
  
      def _get_symbolic_result(out_grads):
          outputs_sym = mx.sym.contrib.cond(
              pred=cond_func(_input_syms, _free_syms),
              then_func=lambda: then_func(_input_syms, _free_syms),
              else_func=lambda: else_func(_input_syms, _free_syms),
          )
          outputs_sym = _as_list(outputs_sym)
          outputs_sym = [x * 2 for x in outputs_sym]
          outputs_sym = mx.sym.Group(outputs_sym)
          executor = outputs_sym._bind(
              ctx=default_device(),
              args={
                  name: _args_dict[name].copy()
                  for name in outputs_sym.list_inputs()
              },
              args_grad=None
              if not is_train
              else _merge_dict(
                  {
                      "InputVar" + str(i): mx.nd.zeros(s)
                      for i, s in enumerate(input_var_shapes)
                  },
                  {
                      "FreeVar" + str(i): mx.nd.zeros(s)
                      for i, s in enumerate(free_var_shapes)
                  },
              ),
          )
          outputs = executor.forward(is_train=is_train)
          grads = []
          if is_train:
              executor.backward(out_grads=out_grads)
              grads = [
                  executor.grad_dict.get("FreeVar" + str(i), None)
                  for i, _ in enumerate(free_var_shapes)
              ] + [
                  executor.grad_dict.get("InputVar" + str(i), None)
                  for i, _ in enumerate(input_var_shapes)
              ]
          return _to_numpy_list(outputs), _to_numpy_list(grads)
  
      imp_outs, imp_grads, out_grads = _get_imperative_result()
      sym_outs, sym_grads = _get_symbolic_result(out_grads)
      for imp_out, sym_out in zip(imp_outs, sym_outs):
          if imp_out is None or sym_out is None:
              continue
          assert_almost_equal(imp_out, sym_out, rtol=1e-3, atol=1e-3)
      for imp_grad, sym_grad in zip(imp_grads, sym_grads):
          if imp_grad is None or sym_grad is None:
              continue
          assert_almost_equal(imp_grad, sym_grad, rtol=1e-3, atol=1e-3)
  
file_path: mxnet/tests/python/unittest/test_contrib_control_flow.py
imports: import pytest
  import mxnet
  from numpy.testing import assert_allclose
  from mxnet.test_utils import *
  from mxnet.base import _as_list
label_desc: Use image node_id 4 for calling a global function with example usage: _verify_cond(cond_func, then_func, else_func, input_var_shapes, free_var_shapes, is_train) and returns: mx, mx, unknown, result, _to_numpy_list,  _to_numpy_list, _to_numpy_list,  _to_numpy_list,  out_grads