diff --git a/test/_inductor/__init__.py b/test/_inductor/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..ac0a39a91feeaed1fe9b67cc972855a8927b99c0 --- /dev/null +++ b/test/_inductor/__init__.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved. \ No newline at end of file diff --git a/test/_inductor/test_add.py b/test/_inductor/test_add.py new file mode 100644 index 0000000000000000000000000000000000000000..f952b20031df6d1d0d8463050a02626be4be5c02 --- /dev/null +++ b/test/_inductor/test_add.py @@ -0,0 +1,30 @@ +import torch +from torch.testing._internal.common_utils import run_tests, parametrize, instantiate_parametrized_tests +from testutils import TestUtils +import torch_npu +import torch_npu._inductor + + +class TestAdd(TestUtils): + def op_calc(self, first_element, second_element): + result = first_element + second_element + return result + + @parametrize('shape', TestUtils._pointwise_demo_shapes) + @parametrize('dtype', ['float32', 'int64']) + def test_pointwise_cases(self, shape, dtype): + first_element = self._generate_tensor(shape, dtype) + second_element = self._generate_tensor(shape, dtype) + + std_sum = self.op_calc(first_element, second_element) + + compiled_op_calc = torch.compile(self.op_calc, backend="inductor") + inductor_sum = compiled_op_calc(first_element, second_element) + + self.assertEqual(std_sum, inductor_sum) + + +instantiate_parametrized_tests(TestAdd) + +if __name__ == "__main__": + run_tests() diff --git a/test/_inductor/testutils.py b/test/_inductor/testutils.py new file mode 100644 index 0000000000000000000000000000000000000000..47863aaf3483ea0923564d3dff71e3363b31647f --- /dev/null +++ b/test/_inductor/testutils.py @@ -0,0 +1,36 @@ +from collections.abc import Sequence +import os +import time +import numpy as np +import torch +from torch.testing._internal.common_utils import TestCase +import torch_npu + + +class TestUtils(TestCase): + _pointwise_test_shape2d = [(4096, 256), (1024, 32), (8, 2048), (8, 4096)] # (8, 4), (8, 8), not supported + _pointwise_test_shape3d = [(8, 8, 4), (8, 8, 8), (8, 8, 2048), (8, 8, 4096)] + _pointwise_test_shape4d = [(128, 128, 4096, 4), (128, 128, 4096, 8), + (32, 32, 1024, 1024)] # 128*128*4096*2048 is too big(512G) + _pointwise_test_shapes = _pointwise_test_shape2d + _pointwise_test_shape3d + _pointwise_test_shape4d + + _pointwise_demo_shapes = [(1024, 32), (8, 16, 256, 32)] + _reduction_extest_shape4d = [(8, 8, 8, 16384), (8, 8, 16384, 8), (8, 16384, 8, 8), (16384, 8, 8, 8)] + _reduction_extest_dim4d = [-1, -2, 1, 0] + _reduction_extest_SDbinding = list(zip(_reduction_extest_shape4d, _reduction_extest_dim4d)) + + _test_dtypes = ['float32', 'int32', 'float16', 'bfloat16', 'int64'] + + @staticmethod + def _generate_tensor(shape, dtype, floatPOSIFLAG=0): + if dtype == 'float32' or dtype == 'float16' or dtype == 'bfloat16': + if floatPOSIFLAG: + return 1000 * torch.rand(size=shape, dtype=eval('torch.' + dtype), device=torch.device("npu")) + else: + return torch.randn(size=shape, dtype=eval('torch.' + dtype), device=torch.device("npu")) * 2000 + elif dtype == 'int32' or dtype == 'int64': + return torch.randint(low=0, high=2000, size=shape, dtype=eval('torch.' + dtype), device=torch.device("npu")) + elif dtype == 'bool': + return torch.randint(low=0, high=2, size=shape, device=torch.device("npu")).bool() + else: + raise ValueError('Invalid parameter \"dtype\" is found : {}'.format(dtype)) diff --git a/torch_npu/_inductor/__init__.py b/torch_npu/_inductor/__init__.py index 04ef05321d0362f1aa3f5396b49fb6311cafaf81..61a50b38f68b3d7a7d428a08cedc61ea4e213d6a 100644 --- a/torch_npu/_inductor/__init__.py +++ b/torch_npu/_inductor/__init__.py @@ -78,6 +78,7 @@ if npu_config.dump_fx_graph: else: from .lowering import _register_npu_inductor_fallbacks + _register_npu_inductor_fallbacks() _register_npu_inductor_decompositons()