text: # Licensed to the Apache Software Foundation (ASF) under one
  # or more contributor license agreements.  See the NOTICE file
  # distributed with this work for additional information
  # regarding copyright ownership.  The ASF licenses this file
  # to you under the Apache License, Version 2.0 (the
  # "License"); you may not use this file except in compliance
  # with the License.  You may obtain a copy of the License at
  #
  #   http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing,
  # software distributed under the License is distributed on an
  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  # KIND, either express or implied.  See the License for the
  # specific language governing permissions and limitations
  # under the License.
  
  import os
  import re
  import tvm
  import numpy as np
  from tvm import relay
  from tvm.relay import testing
  from utils.adreno_utils import gpu_preprocess, build_run_compare
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_deeplabv3_1_257_257_32x1_1_32_16():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 257, 257, 32)
      filter_shape = (1, 1, 32, 16)
      bias_shape = (filter_shape[-1],)
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          out_dtype=dtype,
          channels=filter_shape[-1],
          kernel_size=(1, 1),
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      np.random.seed(1)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      bias_data = np.zeros(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_deeplabv3_1_257_257_32x1_1_32_16_with_padding():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 257, 257, 32)
      filter_shape = (1, 1, 32, 16)
      bias_shape = (filter_shape[-1],)
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          padding=[3, 3, 3, 3],
          strides=[2, 2],
          out_dtype=dtype,
          channels=filter_shape[-1],
          kernel_size=(1, 1),
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      # mod, params = relay.testing.init.create_workload(func)
      np.random.seed(1)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      bias_data = np.zeros(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_4_35_35_32x3_3_144_16():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (4, 35, 35, 32)
      filter_shape = (3, 3, 32, 16)
      bias_shape = (filter_shape[-1],)
      kernel_size = (filter_shape[0], filter_shape[1])
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          out_dtype=dtype,
          channels=filter_shape[-1],
          kernel_size=kernel_size,
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      np.random.seed(1)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      bias_data = np.zeros(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_deeplabv3_1_513_513_3x3_3_3_32():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 513, 513, 3)
      filter_shape = (3, 3, 3, 32)
      bias_shape = (filter_shape[-1],)
      kernel_size = (filter_shape[0], filter_shape[1])
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          out_dtype=dtype,
          channels=filter_shape[-1],
          kernel_size=kernel_size,
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      np.random.seed(1)
      initializer = relay.testing.init.Xavier()
      filter_data = np.ones(filter_shape).astype(dtype)
      bias_data = np.ones(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_inceptionv3_64x35x35_96x64x3x3_nopad():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 42, 42, 32)
      filter_shape = (3, 3, 32, 96)
      bias_shape = (1, 1, 1, 96)
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      # C = relay.nn.relu(A)
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          padding=[0, 0, 0, 0],
          strides=[2, 2],
          out_dtype=dtype,
          channels=96,
          kernel_size=(3, 3),
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      np.random.seed(0)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      bias_data = np.zeros(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      build_run_compare(mod, params1, {"data": input_shape}, dtype, target, gpu_preprocess)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_inceptionv3_64x35x35_96x64x3x3_nopad_pass():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 40, 40, 32)
      filter_shape = (2, 2, 32, 96)
      bias_shape = (1, 1, 1, 96)
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      # C = relay.nn.relu(A)
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          padding=[0, 0, 0, 0],
          strides=[2, 2],
          out_dtype=dtype,
          channels=96,
          kernel_size=(2, 2),
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      np.random.seed(0)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      bias_data = np.zeros(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      build_run_compare(mod, params1, {"data": input_shape}, dtype, target, gpu_preprocess)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_inceptionv3_35_35_strides():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 35, 35, 48)
      filter_shape = (5, 5, 48, 64)
      bias_shape = (1, 1, 1, 64)
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      # C = relay.nn.relu(A)
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          padding=[2, 2, 2, 2],
          strides=[1, 1],
          out_dtype=dtype,
          channels=64,
          kernel_size=(5, 5),
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      np.random.seed(0)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      bias_data = np.zeros(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      build_run_compare(mod, params1, {"data": input_shape}, dtype, target, gpu_preprocess)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_resnet50_v2_nhwc_3c():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 224, 224, 3)
      filter_shape = (7, 7, 3, 64)
      bias_shape = (1, 1, 1, 64)
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      # C = relay.nn.relu(A)
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          padding=[3, 3, 3, 3],
          strides=[2, 2],
          out_dtype=dtype,
          channels=64,
          kernel_size=(7, 7),
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      # mod, params = relay.testing.init.create_workload(func)
      np.random.seed(1)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      bias_data = np.zeros(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_inceptionv3_nhwc_3c():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 299, 299, 3)
      filter_shape = (3, 3, 3, 64)
      bias_shape = (1, 1, 1, 64)
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      # C = relay.nn.relu(A)
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          padding=[0, 0, 0, 0],
          strides=[2, 2],
          out_dtype=dtype,
          channels=64,
          kernel_size=(3, 3),
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      np.random.seed(0)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      bias_data = np.zeros(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_1x1_16c16spatial():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 256, 256, 16)
      filter_shape = (4, 4, 16, 32)
      bias_shape = (1, 1, 1, 32)
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      # C = relay.nn.relu(A)
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          padding=[0, 0, 0, 0],
          strides=[2, 2],
          out_dtype=dtype,
          channels=32,
          kernel_size=(4, 4),
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      np.random.seed(0)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      bias_data = np.zeros(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_4x4_16c16pad():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 256, 256, 32)
      filter_shape = (4, 4, 32, 32)
      bias_shape = (1, 1, 1, 32)
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      # C = relay.nn.relu(A)
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          padding=[3, 3, 0, 0],
          strides=[2, 2],
          out_dtype=dtype,
          channels=32,
          kernel_size=(4, 4),
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      np.random.seed(0)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      bias_data = np.zeros(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_4x4x4_16c16pad():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 256, 256, 32)
      filter_shape = (4, 4, 32, 4)
      bias_shape = (1, 1, 1, 4)
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      # C = relay.nn.relu(A)
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          padding=[3, 3, 0, 0],
          strides=[2, 2],
          out_dtype=dtype,
          channels=4,
          kernel_size=(4, 4),
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      np.random.seed(0)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      bias_data = np.zeros(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_yolov3_v2_nhwc_3c():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 13, 13, 1024)
      filter_shape = (1, 1, 1024, 255)
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
  
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          padding=[0, 0, 0, 0],
          strides=[1, 1],
          out_dtype=dtype,
          channels=255,
          kernel_size=(1, 1),
      )
  
      mod = relay.Function([A, B], conv)
      # mod, params = relay.testing.init.create_workload(func)
      np.random.seed(0)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      initializer("weight", filter_data)
      params = {
          "weight": tvm.nd.array(filter_data),
      }
  
      build_run_compare(mod, params, {"data": input_shape}, dtype, target)
  
  
  @tvm.testing.requires_opencl
  def test_conv2d_vgg16_winograd_4d():
      target = "opencl --device=adreno"
      dtype = "float16"
  
      input_shape = (1, 28, 28, 512)
      filter_shape = (3, 3, 512, 512)
      bias_shape = (1, 1, 1, 512)
      A = relay.var("data", shape=input_shape, dtype=dtype)
      B = relay.var("weight", shape=filter_shape, dtype=dtype)
      bias = relay.var("bias", shape=bias_shape, dtype=dtype)
  
      conv = relay.nn.conv2d(
          A,
          B,
          data_layout="NHWC",
          kernel_layout="HWIO",
          padding=[1, 1, 1, 1],
          channels=512,
          kernel_size=[3, 3],
          out_dtype=dtype,
      )
      D = relay.op.add(conv, bias)
      D = relay.op.nn.relu(D)
  
      mod = relay.Function([A, B, bias], D)
      np.random.seed(0)
      initializer = relay.testing.init.Xavier()
      filter_data = np.zeros(filter_shape).astype(dtype)
      bias_data = np.zeros(bias_shape).astype(dtype)
      initializer("weight", filter_data)
      initializer("bias", bias_data)
      params1 = {
          "weight": tvm.nd.array(filter_data),
          "bias": tvm.nd.array(bias_data),
      }
  
      graph = build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
      matches = re.findall("winograd", graph)
      assert len(matches) > 0
  