code: def avgpool2d(expr, type_map):
      """Rewrite an avgpool op"""
      attrs = {**expr.attrs}
      arg = expr.args[0]
      t = type_map[arg]
      out_t = type_map[expr]
  
      # dq > nn.avg_pool2d > q
      # Use the same input quantization parameters for output if the pattern is not the above.
      # Type_map is a map of graphs and their Tensoraffinetypes
      # Find the current "nn.avg_pool2d" op after checking for the "qnn.quantize" op in the graph.
      # Structure for .. dq > op > q will be q [op [dq ..
      def check(y, expr):
          if isinstance(y, type(expr)):
              if y.op.name != "nn.avg_pool2d":
                  return True
              # check if this is the expr avg_pool
              if y.attrs != expr.attrs:
                  return True
          return False
  
      for x in type_map.items():
          if isinstance(x[0], type(expr)):
              if x[0].op.name == "qnn.quantize":
                  prev = x[0]
                  y = prev.args[0]
                  while check(y, expr):
                      prev = y
                      y = prev.args[0]
                  if (
                      isinstance(y, type(expr))
                      and y.op.name == "nn.avg_pool2d"
                      and y.attrs == expr.attrs
                  ):
                      if prev.op.name != "qnn.quantize":
                          out_t = t
                      break
  
      out = relay.qnn.op.avg_pool2d(
          arg,
          t.scale,
          t.zero_point,
          out_t.scale,
          out_t.zero_point,
          attrs["pool_size"],
          attrs["strides"],
          attrs["padding"],
          attrs["dilation"],
          attrs["ceil_mode"],
          attrs["count_include_pad"],
          attrs["layout"],
      )
  
      return [
          out,
          TensorAffineType(
              out_t.scale, out_t.zero_point, out_t.dtype, out_t.axis
          ),
      ]
  
imports: import numpy
  import tvm
  from tvm import relay
  from tvm.ir import TensorAffineType, TupleAffineType
  from tvm.relay.qnn.op import canonicalizations
  from tvm.tir import bijective_layout
  from ..op import register_fake_quantization_to_integer, register_optional_fake_quantization_to_integer
label_desc: Use image node_id 8 for calling a global function with example usage: avgpool2d(expr, type_map) and returns: list, False, True, True