code: def test_alter_layout_pad():
      """Check NCHW, NHWC and corner case for pad layout conversion"""
  
      def alter_conv2d(attrs, inputs, tinfos, out_type):
          data, weight = inputs
          new_attrs = dict(attrs)
          new_attrs["data_layout"] = "NCHW16c"
          return relay.nn.conv2d(data, weight, **new_attrs)
  
      # Check NCHW conversion.
      def before_nchw():
          x = relay.var("x", shape=(1, 64, 56, 56))
          weight1 = relay.var("weight1")
          y = relay.nn.conv2d(
              x,
              weight1,
              channels=32,
              kernel_size=(3, 3),
              padding=(1, 1),
          )
          ret = relay.nn.pad(
              y, pad_width=((0, 0), (0, 0), (1, 1), (1, 1))
          )
          y = relay.Function(analysis.free_vars(ret), ret)
          return y
  
      def expected_nchw():
          x = relay.var("x", shape=(1, 64, 56, 56))
          weight1 = relay.var("weight1")
          y = relay.layout_transform(x, "NCHW", "NCHW16c")
          y = relay.nn.conv2d(
              y,
              weight1,
              channels=32,
              kernel_size=(3, 3),
              padding=(1, 1),
              data_layout="NCHW16c",
          )
          ret = relay.nn.pad(
              y, pad_width=((0, 0), (0, 0), (1, 1), (1, 1), (0, 0))
          )
          ret = relay.layout_transform(ret, "NCHW16c", "NCHW")
          y = relay.Function(analysis.free_vars(ret), ret)
          return y
  
      with TempOpAttr("nn.conv2d", "FTVMAlterOpLayout", alter_conv2d):
          a = before_nchw()
          a = run_opt_pass(a, transform.AlterOpLayout())
          b = run_opt_pass(expected_nchw(), transform.InferType())
  
      assert tvm.ir.structural_equal(a, b), "Actual = \n" + str(a)
  
      # Check NHWC conversion.
      def before_nhwc():
          x = relay.var("x", shape=(1, 56, 56, 64))
          weight1 = relay.var("weight1")
          y = relay.nn.conv2d(
              x,
              weight1,
              channels=32,
              kernel_size=(3, 3),
              padding=(1, 1),
              data_layout="NHWC",
          )
          ret = relay.nn.pad(
              y, pad_width=((0, 0), (1, 1), (1, 1), (0, 0))
          )
          y = relay.Function(analysis.free_vars(ret), ret)
          return y
  
      def expected_nhwc():
          x = relay.var("x", shape=(1, 56, 56, 64))
          weight1 = relay.var("weight1")
          y = relay.layout_transform(x, "NHWC", "NCHW16c")
          y = relay.nn.conv2d(
              y,
              weight1,
              channels=32,
              kernel_size=(3, 3),
              padding=(1, 1),
              data_layout="NCHW16c",
          )
          ret = relay.nn.pad(
              y, pad_width=((0, 0), (0, 0), (1, 1), (1, 1), (0, 0))
          )
          ret = relay.layout_transform(ret, "NCHW16c", "NHWC")
          y = relay.Function(analysis.free_vars(ret), ret)
          return y
  
      with TempOpAttr("nn.conv2d", "FTVMAlterOpLayout", alter_conv2d):
          a = before_nhwc()
          a = run_opt_pass(a, transform.AlterOpLayout())
          b = run_opt_pass(expected_nhwc(), transform.InferType())
  
      assert tvm.ir.structural_equal(a, b), "Actual = \n" + str(a)
  
      # Check that conversion does not happen when padding along split axis.
      def before():
          x = relay.var("x", shape=(1, 64, 56, 56))
          weight1 = relay.var("weight1")
          y = relay.nn.conv2d(
              x,
              weight1,
              channels=32,
              kernel_size=(3, 3),
              padding=(1, 1),
          )
          ret = relay.nn.pad(
              y, pad_width=((0, 0), (1, 1), (1, 1), (1, 1))
          )
          y = relay.Function(analysis.free_vars(ret), ret)
          return y
  
      def expected():
          x = relay.var("x", shape=(1, 64, 56, 56))
          weight1 = relay.var("weight1")
          y = relay.layout_transform(x, "NCHW", "NCHW16c")
          y = relay.nn.conv2d(
              y,
              weight1,
              channels=32,
              kernel_size=(3, 3),
              padding=(1, 1),
              data_layout="NCHW16c",
          )
          ret = relay.layout_transform(y, "NCHW16c", "NCHW")
          ret = relay.nn.pad(
              ret, pad_width=((0, 0), (1, 1), (1, 1), (1, 1))
          )
          y = relay.Function(analysis.free_vars(ret), ret)
          return y
  
      with TempOpAttr("nn.conv2d", "FTVMAlterOpLayout", alter_conv2d):
          a = before()
          a = run_opt_pass(a, transform.AlterOpLayout())
          b = run_opt_pass(expected(), transform.InferType())
  
      assert tvm.ir.structural_equal(a, b), "Actual = \n" + str(a)
  
imports: import pytest
  import tvm
  from tvm import relay, topi
  from tvm.relay import transform, analysis
  from tvm.relay.testing.temp_op_attr import TempOpAttr
  from tvm.relay.testing import run_infer_type
  import numpy
  import tvm.testing
  from tvm.relay import testing
label_desc: Use image node_id 20 for calling a global function with example usage: test_alter_layout_pad() and returns: relay, y, y, y, y, y, y