code: def test_conv_reduce_convert_layout():
      def _test_conv_reduce_convert_layout1():
          def before():
              x = relay.var("x", shape=(1, 1, 1, 2048))
              weight = relay.var("weight", shape=(1, 1, 2048, 1000))
              y = relay.nn.conv2d(
                  x,
                  weight,
                  channels=1000,
                  kernel_size=(1, 1),
                  data_layout="NHWC",
                  kernel_layout="HWIO",
              )
              y = relay.nn.relu(y)
              y = relay.sum(y, axis=(1, 2))
              y = relay.sum(y, axis=(1,))
              y = relay.sum(y)
              y = relay.sum(y)
              return relay.Function(analysis.free_vars(y), y)
  
          def expected():
              x = relay.var("x", shape=(1, 1, 1, 2048))
              weight = relay.var("weight", shape=(1, 1, 2048, 1000))
              weight = relay.layout_transform(weight, "HWIO", "OIHW")
              x = relay.layout_transform(x, "NHWC", "NCHW")
              y = relay.nn.conv2d(
                  x, weight, channels=1000, kernel_size=(1, 1)
              )
              y = relay.nn.relu(y)
              y = relay.sum(y, axis=(2, 3))
              y = relay.sum(y, axis=(1,))
              y = relay.sum(y)
              y = relay.sum(y)
              return relay.Function(analysis.free_vars(y), y)
  
          a = before()
          a = run_opt_pass(
              a,
              transform.ConvertLayout(
                  {"nn.conv2d": ["NCHW", "default"]}
              ),
          )
          b = run_opt_pass(expected(), transform.InferType())
  
          assert tvm.ir.structural_equal(a, b), "Actual = \n" + str(a)
  
      def _test_conv_reduce_convert_layout2():
          def _set_span(y, text):
              return relay.Call(
                  y.op,
                  y.args,
                  y.attrs,
                  y.type_args,
                  relay.Span(relay.SourceName(text), 0, 0, 0, 0),
              )
  
          def before():
              x = relay.var("x", shape=(1, 38, 38, 512))
              weight = relay.var("weight", shape=(3, 3, 512, 512))
              y = relay.nn.conv2d(
                  x,
                  weight,
                  channels=512,
                  kernel_size=(3, 3),
                  data_layout="NHWC",
                  kernel_layout="HWIO",
              )
              y = _set_span(y, "SpanConv2D")
              y = relay.nn.relu(y)
              y = _set_span(y, "SpanRelu")
              y = relay.multiply(y, y)
              y = _set_span(y, "SpanMultiply")
              y = relay.sum(y, axis=(3,), keepdims=True)
              y = _set_span(y, "SpanSum")
              return relay.Function(analysis.free_vars(y), y)
  
          def expected():
              x = relay.var("x", shape=(1, 38, 38, 512))
              weight = relay.var("weight", shape=(3, 3, 512, 512))
              weight = relay.layout_transform(weight, "HWIO", "OIHW")
              x = relay.layout_transform(x, "NHWC", "NCHW")
              y = relay.nn.conv2d(
                  x, weight, channels=512, kernel_size=(3, 3)
              )
              y = relay.nn.relu(y)
              y = relay.multiply(y, y)
              y = relay.sum(y, axis=(1,), keepdims=True)
              y = relay.layout_transform(y, "NCHW", "NHWC")
              return relay.Function(analysis.free_vars(y), y)
  
          a = before()
          a = run_opt_pass(
              a,
              transform.ConvertLayout(
                  {"nn.conv2d": ["NCHW", "default"]}
              ),
          )
          assert "SpanConv2D" in a.astext()
          assert "SpanRelu" in a.astext()
          assert "SpanMultiply" in a.astext()
          assert "SpanSum" in a.astext()
          b = run_opt_pass(expected(), transform.InferType())
  
          assert tvm.ir.structural_equal(a, b), "Actual = \n" + str(a)
  
      _test_conv_reduce_convert_layout1()
      _test_conv_reduce_convert_layout2()
  
imports: import pytest
  import tvm
  from tvm import relay, te
  from tvm.relay import analysis, transform
  from tvm.relay.op import op
  from tvm.relay.op import register_alter_op_layout
  from tvm.relay.quantize._annotate import attach_simulated_quantize, QAnnotateKind
  from tvm.relay.transform.infer_layout_utils import InferCorrectLayoutOutput
label_desc: Use image node_id 39 for calling a global function with example usage: test_conv_reduce_convert_layout() and returns: relay, relay, relay, relay, relay