code: def test_subgraph():
      # possible places to find library file
      if os.name == "posix":
          lib = "libsubgraph_lib.so"
          if os.path.exists(lib):
              # plain make build, when run in the CI
              fname = lib
          elif os.path.exists(os.path.join(base_path, "build/" + lib)):
              # plain cmake build when run in the CI
              fname = os.path.join(base_path, "build/" + lib)
          else:
              raise MXNetError(f"library {lib} not found ")
      elif os.name == "nt":
          lib = "libsubgraph_lib.dll"
          if os.path.exists("windows_package\\lib\\" + lib):
              # plain make build, when run in the CI
              fname = "windows_package\\lib\\" + lib
          else:
              # plain cmake build when run in the CI
              raise MXNetError(f"library {lib} not found ")
  
      fname = os.path.abspath(fname)
      mx.library.load(fname)
  
      # test simple graph with add, exp and log operators, library supports exp/log
      a = mx.sym.var("a")
      b = mx.sym.var("b")
      c = a + b
      d = mx.sym.exp(c)
      sym = mx.sym.log(d)
  
      args = {
          "a": mx.nd.ones((3, 2), ctx=mx.cpu()),
          "b": mx.nd.ones((3, 2), ctx=mx.cpu()),
      }
  
      # baseline - regular execution in MXNet
      exe = sym._bind(ctx=mx.cpu(), args=args)
      out = exe.forward()
  
      # without propogating shapes/types, passing a custom option to subgraph prop "myOpt"
      # should not create subgraph since subgraph prop requires type info
      mysym1 = sym.optimize_for("myProp", myOpt="yello")
      exe1 = mysym1._bind(ctx=mx.cpu(), args=args)
      out1 = exe1.forward()
      # check that result matches one executed by MXNet
      assert_almost_equal(
          out[0].asnumpy(), out1[0].asnumpy(), rtol=1e-3, atol=1e-3
      )
  
      # with propogating shapes/types, rejecting subgraph
      # this tests creating the subgraph and having the subgraph prop reject it
      mysym2 = sym.optimize_for("myProp", args, reject=True)
      exe2 = mysym2._bind(ctx=mx.cpu(), args=args)
      out2 = exe2.forward()
      # check that result matches one executed by MXNet
      assert_almost_equal(
          out[0].asnumpy(), out2[0].asnumpy(), rtol=1e-3, atol=1e-3
      )
  
      # with propogating shapes/types
      mysym3 = sym.optimize_for("myProp", args)
      exe3 = mysym3._bind(ctx=mx.cpu(), args=args)
      out3 = exe3.forward()
      # check that result matches one executed by MXNet
      assert_almost_equal(
          out[0].asnumpy(), out3[0].asnumpy(), rtol=1e-3, atol=1e-3
      )
  
      # Gluon Hybridize partitioning with shapes/types
      sym_block = nn.SymbolBlock(sym, [a, b])
      sym_block.initialize()
      sym_block.optimize_for(
          mx.nd.ones((3, 2)), mx.nd.ones((3, 2)), backend="myProp"
      )
      out4 = sym_block(mx.nd.ones((3, 2)), mx.nd.ones((3, 2)))
      # check that result matches one executed by MXNet
      assert_almost_equal(
          out[0].asnumpy(), out4[0].asnumpy(), rtol=1e-3, atol=1e-3
      )
  
      # Gluon Hybridize partitioning with sym.var
      sym_block2 = nn.SymbolBlock(sym, [a, b])
      sym_block2.initialize()
      a_var = mx.sym.var("a", shape=(3, 2))
      b_var = mx.sym.var("b", shape=(3, 2))
      sym_block2.optimize_for(a_var, b_var, backend="myProp")
  
      # Gluon Hybridize partitioning with shapes/types
      sym_block3 = nn.SymbolBlock(sym, [a, b])
      sym_block3.initialize()
      a_data = mx.nd.ones((3, 2))
      b_data = mx.nd.ones((3, 2))
      sym_block3.optimize_for(a_data, b_data, backend="myProp")
      sym_filename, params_filename = sym_block3.export("optimized")
      assert sym_filename == "optimized-symbol.json"
      assert params_filename is None
  
      # Test with additional input to subgraph op
      sym_block3.optimize_for(a_data, b_data, backend="addInputPass")
      out5 = sym_block3(a_data, b_data)
  
      # Reload exported block
      sym_block4 = nn.SymbolBlock.imports(
          sym_filename, ["a", "b"], params_filename
      )
  
      out6 = sym_block4(a_data, b_data)
      # check that result matches one executed by MXNet
      assert_almost_equal(
          out[0].asnumpy(), out6[0].asnumpy(), rtol=1e-3, atol=1e-3
      )
  
file_path: mxnet/tests/python/unittest/test_extensions.py
imports: import os
  import platform
  import mxnet
  import numpy
  from mxnet import nd
  from mxnet.gluon import nn
  from mxnet.base import MXNetError
  from mxnet.test_utils import download, is_cd_run, assert_almost_equal, default_device
  import pytest
label_desc: Use image node_id 3 for calling a global function with example usage: test_subgraph() without return types