code: def test_legalize_dense():
      def _test_legalize_dense(
          data_shape,
          kernel_shape,
          pad_shape,
          dtype,
          do_pad=True,
          units=None,
      ):
          """test legalize dense to enable tensorcore"""
          M, K = data_shape
          N, _ = kernel_shape
          out_shape = (M, N)
          dm, dk, dn = pad_shape
  
          def before():
              x = relay.var("x", shape=data_shape, dtype=dtype)
              weight = relay.var(
                  "weight", shape=kernel_shape, dtype=dtype
              )
              y = relay.nn.dense(x, weight, units)
              y = relay.Function([x, weight], y)
              return y
  
          def legalize_dense(attrs, inputs, types):
              with tvm.target.Target("cuda"):
                  return topi.nn.dense_legalize(attrs, inputs, types)
  
          def expected():
              if not do_pad:
                  return before()
              x = relay.var("x", shape=data_shape, dtype=dtype)
              if dm or dk:
                  x_pad = relay.nn.pad(x, pad_width=((0, dm), (0, dk)))
              else:
                  x_pad = x
              weight = relay.var(
                  "weight", shape=(kernel_shape), dtype=dtype
              )
              if dn or dk:
                  weight_pad = relay.nn.pad(
                      weight, pad_width=((0, dn), (0, dk))
                  )
              else:
                  weight_pad = weight
              y_pad = relay.nn.dense(
                  x_pad, weight_pad, units=N + dn if units else None
              )
              if dm or dn:
                  y = relay.strided_slice(
                      y_pad, begin=[0, 0], end=out_shape
                  )
              else:
                  y = y_pad
              y = relay.Function([x, weight], y)
              return y
  
          with TempOpAttr("nn.dense", "FTVMLegalize", legalize_dense):
              a = before()
              a = run_opt_pass(a, transform.Legalize())
              b = run_opt_pass(expected(), transform.InferType())
  
          assert tvm.ir.structural_equal(a, b), (
              "Actual = \n" + str(a) + "Expected = \n" + str(b)
          )
  
      # dense
      for dtype in ["float16", "int8"]:
          _test_legalize_dense(
              (8, 16), (32, 16), (0, 0, 0), dtype, False
          )
          _test_legalize_dense((7, 16), (32, 16), (1, 0, 0), dtype)
          _test_legalize_dense((8, 15), (32, 15), (0, 1, 0), dtype)
          _test_legalize_dense((8, 16), (31, 16), (0, 0, 1), dtype)
          _test_legalize_dense((7, 15), (31, 15), (1, 1, 1), dtype)
          _test_legalize_dense((3, 16), (32, 16), (5, 0, 0), dtype)
          _test_legalize_dense(
              (1, 16), (32, 16), (0, 0, 0), dtype, False
          )
  
      # Test if units parameter is correctly updated
      _test_legalize_dense(
          (8, 16), (30, 16), (0, 0, 2), "float16", units=30
      )
  
      _test_legalize_dense((8, 32), (32, 32), (0, 0, 0), "int4", False)
      _test_legalize_dense((7, 32), (32, 32), (1, 0, 0), "int4")
      _test_legalize_dense((8, 31), (32, 31), (0, 1, 0), "int4")
      _test_legalize_dense((8, 32), (31, 32), (0, 0, 1), "int4")
      _test_legalize_dense((7, 31), (31, 31), (1, 1, 1), "int4")
      _test_legalize_dense((3, 32), (32, 32), (5, 0, 0), "int4")
      _test_legalize_dense((8, 16), (32, 16), (0, 16, 0), "int4")
      _test_legalize_dense((2, 16), (32, 16), (0, 0, 0), "int4", False)
  
file_path: tvm/tests/python/relay/test_pass_legalize_tensorcore.py
imports: import numpy
  import tvm
  from tvm import te
  from tvm import topi
  from tvm import relay
  from tvm.contrib import graph_executor
  from tvm.relay import transform, analysis
  from tvm.relay.testing.temp_op_attr import TempOpAttr
label_desc: Use image node_id 4 for calling a global function with example usage: test_legalize_dense() and returns: y, y, topi, before