Operator Verification on PyTorch Network (Training)
Overview
After operator adaptation is complete, you can run the PyTorch operator adapted to Ascend AI Processor to verify the operator running result.
Operator verification involves all deliverables generated during operator development, including the implementation files, operator prototype definitions, operator information library, and operator plugins. This section describes only the verification method.
Test Cases and Tools
Use the PyTorch frontend to construct the custom operator function and run the function to verify the custom operator functions.
The reference directories of test cases and tools are listed as follows:
- 1.11.0 or later: /test
Implementation
- Set environment variables.
# Set environment variables. (The following uses the installation to the default path by the root user as an example.) source /usr/local/Ascend/ascend-toolkit/set_env.sh
- Compile a test script in the test case and tool directory. The following uses the add operator as an example. The file name is test_add.py.
# Import the dependency library. import torch import torch_npu import numpy as np from torch_npu.testing.testcase import TestCase, run_tests from torch_npu.testing.common_utils import create_common_tensor # Define the add test case class. class TestAdd(TestCase): # Define the functions to execute the add operator on the CPU and NPU. def cpu_op_exec(self, input1, input2): output = torch.add(input1, input2, alpha = 1) output = output.numpy() return output def npu_op_exec_new(self, input1, input2): output = torch.add(input1, input2, alpha = 1) output = output.to("cpu") output = output.numpy() return output # Define a general function for the add scenario. This function is used to input data and compare the compute results of the CPU and NPU. def add_result(self, shape_format): for item in shape_format: cpu_input1, npu_input1 = create_common_tensor(item, 0, 100) cpu_input2, npu_input2 = create_common_tensor(item, 0, 100) if cpu_input1.dtype == torch.float16: cpu_input1 = cpu_input1.to(torch.float32) cpu_input2 = cpu_input2.to(torch.float32) cpu_output = self.cpu_op_exec(cpu_input1, cpu_input2) npu_output = self.npu_op_exec_new(npu_input1, npu_input2) cpu_output = cpu_output.astype(npu_output.dtype) self.assertRtolEqual(cpu_output, npu_output) # Define a test case for a specific add scenario. The test case function must start with test_. def test_add_shape_format_fp32_2d(self): format_list = [0, 3, 29] shape_format = [ [np.float32, i, [5, 256]] for i in format_list ] self.add_result(shape_format) if __name__ == "__main__": run_tests() - Execute the test case script.
python3 test_add.py
Parent topic: Operator Verification on Network