code: def test_cut_subgraph_foreach():
      class TestLayer(gluon.HybridBlock):
          def __init__(self):
              super(TestLayer, self).__init__()
  
          def forward(self, inputs, states):
              def step1(data, states):
                  return data + 1, states
  
              out1, states1 = mx.npx.foreach(step1, inputs, states)
              out2, states2 = mx.npx.foreach(step1, out1, states)
  
              def step2(data, states):
                  return data + states[0], states
  
              out, states = mx.npx.foreach(step2, out2, states1)
              return out
  
      data = mx.np.random.normal(loc=0, scale=1, size=(5, 10))
      states = mx.np.random.normal(loc=0, scale=1, size=(10))
      layer = TestLayer()
      layer.initialize(device=default_device())
      res1 = layer(data, [states])
  
      with mx.autograd.record():
          res1 = layer(data, [states])
  
      layer = TestLayer()
      layer.initialize(device=default_device())
      layer.hybridize()
      res2 = layer(data, [states])
  
      with mx.autograd.record():
          res2 = layer(data, [states])
      assert_almost_equal(
          res1.asnumpy(), res2.asnumpy(), rtol=1e-3, atol=1e-3
      )
  
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 2 for calling a global function with example usage: test_cut_subgraph_foreach() and returns: out, unknown,  states, unknown,  states