code: def test_output_format_foreach():
      class TestLayer1(gluon.HybridBlock):
          def __init__(self, step):
              super(TestLayer1, self).__init__()
              self.step = step
  
          def forward(self, ins, states):
              out, states = mx.npx.foreach(self.step, ins, states)
              return out, states
  
      def step1(data, state):
          return data, state
  
      def step2(data, state):
          return [data], state
  
      def step3(data, state):
          if isinstance(state, list):
              return [], [state[0] + data]
          else:
              return [], state + data
  
      def step4(data, state):
          if isinstance(state, list):
              return [data, state[0]], state
          else:
              return [data, state], state
  
      steps = [step1, step2, step3, step4]
      data = mx.np.random.normal(loc=0, scale=1, size=(10, 2))
      state = mx.np.random.normal(loc=0, scale=1, size=(2))
      for step in steps:
          layer1 = TestLayer1(step)
          layer1.initialize(device=default_device())
          layer2 = TestLayer1(step)
          layer2.initialize(device=default_device())
          layer2.hybridize()
          out1, state1 = layer1(data, [state])
          out2, state2 = layer2(data, [state])
          step_out, step_state = step(data, [state])
          assert type(out1) == type(step_out)
          assert type(out2) == type(step_out)
          assert type(state1) == type(step_state)
          assert type(state2) == type(step_state)
          out1 = _as_list(out1)
          out2 = _as_list(out2)
          state1 = _as_list(state1)
          state2 = _as_list(state2)
          for i in range(len(out1)):
              assert_almost_equal(
                  out1[i].asnumpy(),
                  out2[i].asnumpy(),
                  rtol=0.001,
                  atol=0.0001,
              )
          for i in range(len(state1)):
              assert_almost_equal(
                  state1[i].asnumpy(),
                  state2[i].asnumpy(),
                  rtol=0.001,
                  atol=0.0001,
              )
  
          layer1 = TestLayer1(step)
          layer1.initialize(device=default_device())
          layer2 = TestLayer1(step)
          layer2.initialize(device=default_device())
          layer2.hybridize()
          out1, state1 = layer1(data, state)
          out2, state2 = layer2(data, state)
          step_out, step_state = step(data, state)
          assert type(out1) == type(step_out)
          assert type(out2) == type(step_out)
          assert type(state1) == type(step_state)
          assert type(state2) == type(step_state)
          out1 = _as_list(out1)
          out2 = _as_list(out2)
          state1 = _as_list(state1)
          state2 = _as_list(state2)
          for i in range(len(out1)):
              assert_almost_equal(
                  out1[i].asnumpy(),
                  out2[i].asnumpy(),
                  rtol=0.001,
                  atol=0.0001,
              )
          for i in range(len(state1)):
              assert_almost_equal(
                  state1[i].asnumpy(),
                  state2[i].asnumpy(),
                  rtol=0.001,
                  atol=0.0001,
              )
  
          if step == step3:
              continue
          layer1 = TestLayer1(step)
          layer1.initialize(device=default_device())
          layer2 = TestLayer1(step)
          layer2.initialize(device=default_device())
          layer2.hybridize()
          out1, state1 = layer1(data, [state, [state + 1]])
          out2, state2 = layer2(data, [state, [state + 1]])
          step_out, step_state = step(data, [state, [state + 1]])
          assert type(out1) == type(step_out)
          assert type(out2) == type(step_out)
          assert type(state1) == type(step_state)
          assert type(state2) == type(step_state)
          out1 = _as_list(out1)
          out2 = _as_list(out2)
          state1 = _as_list(state1)
          state2 = _as_list(state2)
          for i in range(len(out1)):
              assert_almost_equal(
                  out1[i].asnumpy(),
                  out2[i].asnumpy(),
                  rtol=0.001,
                  atol=0.0001,
              )
          for i in range(len(state1)):
              if isinstance(state1[i], list):
                  assert_almost_equal(
                      state1[i][0].asnumpy(),
                      state2[i][0].asnumpy(),
                      rtol=0.001,
                      atol=0.0001,
                  )
              else:
                  assert_almost_equal(
                      state1[i].asnumpy(),
                      state2[i].asnumpy(),
                      rtol=0.001,
                      atol=0.0001,
                  )
  
imports: import copy
  import numpy
  import mxnet
  from mxnet import gluon
  from mxnet.test_utils import *
  from mxnet.base import _as_list
  from collections import defaultdict
  from mxnet.attribute import AttrScope
label_desc: Use image node_id 6 for calling a global function with example usage: test_output_format_foreach() and returns: data,  state, list,  state, out,  states, list,  list, list,  unknown, list,  state, list,  state