code: # Copyright (c) 2020, Huawei Technologies.All rights reserved.
  #
  # Licensed under the BSD 3-Clause License  (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  # https://opensource.org/licenses/BSD-3-Clause
  #
  # 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 torch
  import numpy as np
  import math
  import random
  from torch._six import nan
  from common_utils import TestCase, run_tests
  from common_device_type import dtypes, instantiate_device_type_tests
  
  
  class TestNormExceptDim(TestCase):
      def generate_data(self, min, max, shape, dtype):
          input1 = np.random.uniform(min, max, shape).astype(dtype)
          input2 = np.random.uniform(min, max, shape).astype(dtype)
          npu_input1 = torch.from_numpy(input1)
          npu_input2 = torch.from_numpy(input2)
  
          return npu_input1, npu_input2
  
      def generate_single_data(self, min, max, shape, dtype):
          input = np.random.uniform(min, max, shape).astype(dtype)
          npu_input = torch.from_numpy(input)
          return npu_input
  
      def generate_int_dim(self, max):
          dim = np.random.randint(0, max)
          return dim
  
      def generate_bool_keepdim(self):
          keepdim = random.choice([True, False])
          return keepdim
  
      def test_norm_except_dim_type(self, device):
          def cpu_op_exec(input1, pow):
              output = torch.norm_except_dim(input1, pow=pow, dim=0)
              output = output.numpy()
              return output
  
          def npu_op_exec(input1, pow):
              print(input1.shape)
              input1 = input1.to("npu")
              output = torch.norm_except_dim(input1, pow=pow, dim=0)
              output = output.to("cpu")
              output = output.numpy()
              print(output.shape)
              return output
  
          def test_norm_except_dim_exec(input_type):
              input1 = self.generate_single_data(0, 100, (5, 3), input_type)
              pow = self.generate_int_dim(10)
              cpu_output = cpu_op_exec(input1, pow)
              npu_output = npu_op_exec(input1, pow)
              return cpu_output, npu_output
  
          for dtype in [np.float32]:
              cpu_output, npu_output = test_norm_except_dim_exec(dtype)
              self.assertRtolEqual(cpu_output, npu_output)
  
      
  instantiate_device_type_tests(TestNormExceptDim, globals(), except_for="cpu")
  
  if __name__ == "__main__":
      run_tests() # Copyright (c) 2020 Huawei Technologies Co., Ltd
  # Copyright (c) 2019, Facebook CORPORATION. 
  # All rights reserved.
  #
  # Licensed under the BSD 3-Clause License  (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  # https://opensource.org/licenses/BSD-3-Clause
  #
  # 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 torch
  import numpy as np
  import sys
  import copy
  from common_utils import TestCase, run_tests
  from common_device_type import dtypes, instantiate_device_type_tests
  from util_test import create_common_tensor
  
  class TestUpsampleBicubic2dBackward(TestCase):
  
      def cpu_op_exec(self, input1, output_size, align_corners, scale_h, scale_w):
          input1.requires_grad = True
          output = torch._C._nn.upsample_bicubic2d(input1, output_size, align_corners, scale_h, scale_w)
          output.backward(torch.ones_like(output))
          output_grad = input1.grad
          output_grad = output_grad.detach().numpy()
          return output_grad
  
      def npu_op_exec(self, input1, output_size, align_corners, scale_h, scale_w):
          input1.requires_grad = True
          output = torch._C._nn.upsample_bicubic2d(input1, output_size, align_corners, scale_h, scale_w)
          output.backward(torch.ones_like(output))
          output_grad = input1.grad
          output_grad = output_grad.to("cpu").detach().numpy()
          return output_grad
  
  
      def test_upsample_bicubic2d_common_shape_format(self, device):
          shape_format = [
                          [[np.float32, -1, (1, 1, 1, 1)], (1, 1), True, 0, 0, 0, 255],
                          [[np.float32, -1, (2, 65535, 2, 2)], (2, 2), True, 0, 0, 0, 255],
                          [[np.float32, -1, (10, 10, 786432, 8)], (786432, 8), False, 0, 0, 0, 255],
                          [[np.float32, -1, (1, 1, 1, 1)], (2, 2), True, 0, 0, 0, 255],
                          [[np.float32, -1, (1, 1, 2, 2)], (4, 4), True, 0, 0, 0, 255],
                          [[np.float32, -1, (1, 1, 1, 1)], (2, 2), False, 0.5, 0.5, 0, 255],
                          [[np.float32, -1, (1, 1, 2, 2)], (4, 4), False, 0.5, 0.5, 0, 255],
                          [[np.float32, -1, (32, 32, 32, 32)], (64, 64), False, 0.5, 0.5, 0, 3402823500.0]
                         ]
          for item in shape_format:
              cpu_input1, npu_input1 = create_common_tensor(item[0], item[5], item[6])
              cpu_output = self.cpu_op_exec(cpu_input1, item[1], item[2], item[3], item[4])
              npu_output = self.npu_op_exec(npu_input1, item[1], item[2], item[3], item[4])
              self.assertRtolEqual(cpu_output, npu_output)
  
  
      def test_upsample_bicubic2d_float16_shape_format(self, device):
          def cpu_op_exec_fp16(input1, output_size, align_corners, scale_h, scale_w):
              input1 = input1.to(torch.float32)
              input1.requires_grad = True
              output = torch._C._nn.upsample_bicubic2d(input1, output_size, align_corners, scale_h, scale_w)
              output.backward(torch.ones_like(output))
              output_grad = input1.grad
              output_grad = output_grad.detach().numpy()
              output_grad = output_grad.astype(np.float16)
              return output_grad
      
          shape_format = [
                          [[np.float16, -1, (1, 1, 1, 1)], (1, 1), True, 0, 0, 0, 255],
                          [[np.float16, -1, (2, 65535, 2, 2)], (2, 2), True, 0, 0, 0, 255],
                          [[np.float16, -1, (32, 32, 32, 32)], (32, 32), False, 0, 0, 0, 6550.0],
                          [[np.float16, -1, (1, 1, 1, 1)], (2, 2), True, 0, 0, 0, 255],
                          [[np.float16, -1, (1, 1, 1, 1)], (2, 2), False, 0.5, 0.5, 0, 255],
                          [[np.float16, -1, (1, 1, 2, 2)], (4, 4), False, 0.5, 0.5, 0, 255],
                          [[np.float16, -1, (32, 32, 32, 32)], (64, 64), False, 0.5, 0.5, 0, 6550.0]
                         ]
          
          for item in shape_format:
              cpu_input1, npu_input1 = create_common_tensor(item[0], item[5], item[6])
              cpu_output = cpu_op_exec_fp16(cpu_input1, item[1], item[2], item[3], item[4])
              npu_output = self.npu_op_exec(npu_input1, item[1], item[2], item[3], item[4])
              self.assertRtolEqual(cpu_output, npu_output)
  
  instantiate_device_type_tests(TestUpsampleBicubic2dBackward, globals(), except_for='cpu')
  if __name__ == "__main__":
      run_tests()
   # Copyright (c) 2020, Huawei Technologies.All rights reserved.
  #
  # Licensed under the BSD 3-Clause License  (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  # https://opensource.org/licenses/BSD-3-Clause
  #
  # 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 torch
  import numpy as np
  import copy
  from common_utils import TestCase, run_tests
  from common_device_type import dtypes, instantiate_device_type_tests
  from util_test import create_common_tensor
  
  class TestConv1d(TestCase):
      def op_exec_cpu(self, input, weight, in_channels, out_channels, kernel_size, padding=0, stride=1, dilation=1, bias=True):
          m = torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride, padding, dilation, bias=bias, groups=1)
          m.weight.data = weight
          output = m(input)
          output = output.detach().numpy()
          return output
  
      def op_exec_npu(self, input, weight, in_channels, out_channels, kernel_size, padding=0, stride=1, dilation=1, bias=True):
          m = torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride, padding, dilation, bias=bias, groups=1)
          m.weight.data = weight
          m = m.to("npu")
          output = m(input)
          output = output.to("cpu")
          output = output.detach().numpy()
          return output
  
      def test_conv1d_shape_format(self, device):
          shape_format = [  
              [[np.float32, 3, (256, 32, 1, 1)], [np.float32, 3, (8, 32, 1, 1)], 0, (1, 1), (1, 1), (8)],
              [[np.float32, 3, [256, 32, 112, 112]], [np.float32, 0, [16, 32, 1, 1]], 0, 1, 1, None],
              [[np.float32, 0, [256, 3, 224, 224]], [np.float32, 0, [32, 3, 3, 3]], 0, [2, 2], 1, None],
              [[np.float32, 3, (2, 3, 3, 3)], [np.float32, 0, (3, 1, 3, 3)], 3, 1, 1, 1],
              [[np.float32, 3, [1024, 232, 7, 7]], [np.float32, 4, [232, 232, 1, 1]], 0, 1, 1, True],
          ]
  
          for item in shape_format:
              input_cpu, input_npu = create_common_tensor(item[0], -2, 2)
              weight_cpu, weight_npu = create_common_tensor(item[1], -2, 2)
              kernel_size = (item[1][2][2], item[1][2][3])
              cpu_output = self.op_exec_cpu(input_cpu, weight_cpu, item[0][2][1], item[1][2][0], kernel_size=kernel_size,
                                            padding=item[2], stride=item[3], dilation=item[4], bias=item[5])
              weight_npu = weight_npu.to("cpu")
              npu_output = self.op_exec_npu(input_npu, weight_npu, item[0][2][1], item[1][2][0], kernel_size=kernel_size,
                                            padding=item[2], stride=item[3], dilation=item[4], bias=item[5])            
              self.assertRtolEqual(cpu_output, npu_output)
              
      def test_conv1d_shape_format_float16(self, device):
          def cpu_op_exec_fp16(input, weight, in_channels, out_channels, kernel_size, padding=0, stride=1, dilation=1, bias=True):
              input = input.to(torch.float32)
              weight = weight.to(torch.float32)
              m = torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride, padding, dilation, bias=bias, groups=1)
              m.weight.data = weight
              output = m(input)
              output = output.detach().numpy()
              output = output.astype(np.float16)
              return output
  
          shape_format = [  
              [[np.float16, 3, (256, 32, 1, 1)], [np.float16, 3, (8, 32, 1, 1)], 0, (1, 1), (1, 1), (8)],
              [[np.float16, 3, [256, 32, 112, 112]], [np.float16, 0, [16, 32, 1, 1]], 0, 1, 1, None],
              [[np.float16, 0, [256, 3, 224, 224]], [np.float16, 0, [32, 3, 3, 3]], 0, [2, 2], 1, None],
              [[np.float16, 3, (2, 3, 3, 3)], [np.float16, 0, (3, 1, 3, 3)], 3, 1, 1, 1],
              [[np.float16, 3, [1024, 232, 7, 7]], [np.float16, 4, [232, 232, 1, 1]], 0, 1, 1, True],
          ]
  
          for item in shape_format:
              input_cpu, input_npu = create_common_tensor(item[0], -2, 2)
              weight_cpu, weight_npu = create_common_tensor(item[1], -2, 2)
              kernel_size = (item[1][2][2], item[1][2][3])
              cpu_output = cpu_op_exec_fp16(input_cpu, weight_cpu, item[0][2][1], item[1][2][0], kernel_size=kernel_size,
                                            padding=item[2], stride=item[3], dilation=item[4], bias=item[5])
              weight_npu = weight_npu.to("cpu")
              npu_output = self.op_exec_npu(input_npu, weight_npu, item[0][2][1], item[1][2][0], kernel_size=kernel_size,
                                            padding=item[2], stride=item[3], dilation=item[4], bias=item[5])            
              self.assertRtolEqual(cpu_output, npu_output)          
  
  instantiate_device_type_tests(TestConv1d, globals(), except_for='cpu')
  if __name__ == "__main__":
      torch.npu.set_device("npu:6")
      run_tests()
apis: numpy.random.uniform torch.from_numpy torch.norm_except_dim numpy.random.randint
apis: torch.ones_like torch._C._nn.upsample_bicubic2d
apis: torch.nn.Conv1d torch.npu.set_device