From a5b0737d29350ab78fa2351544ee3ae0a87de711 Mon Sep 17 00:00:00 2001 From: wangchao Date: Sat, 5 Aug 2023 15:23:25 +0800 Subject: [PATCH 1/4] run_ut support params convert --- .../hook_module/wrap_functional.py | 5 +++-- .../hook_module/wrap_tensor.py | 5 +++-- .../api_accuracy_checker/hook_module/wrap_torch.py | 9 +++++---- .../api_accuracy_checker/run_ut/data_generate.py | 5 +++-- .../api_accuracy_checker/run_ut/run_ut.py | 14 +++++++++++--- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/debug/accuracy_tools/api_accuracy_checker/hook_module/wrap_functional.py b/debug/accuracy_tools/api_accuracy_checker/hook_module/wrap_functional.py index 0429a6a4e7..dbe27a134c 100644 --- a/debug/accuracy_tools/api_accuracy_checker/hook_module/wrap_functional.py +++ b/debug/accuracy_tools/api_accuracy_checker/hook_module/wrap_functional.py @@ -43,10 +43,11 @@ class HOOKFunctionalOP(object): class FunctionalOPTemplate(HOOKModule): - def __init__(self, op_name, hook): + def __init__(self, op_name, hook, need_hook=True): self.op_name_ = op_name self.prefix_op_name_ = "Functional*" + str(op_name) + "*" - super().__init__(hook) + if need_hook: + super().__init__(hook) @torch_device_guard def forward(self, *args, **kwargs): diff --git a/debug/accuracy_tools/api_accuracy_checker/hook_module/wrap_tensor.py b/debug/accuracy_tools/api_accuracy_checker/hook_module/wrap_tensor.py index 012c659bd6..93d92923c6 100644 --- a/debug/accuracy_tools/api_accuracy_checker/hook_module/wrap_tensor.py +++ b/debug/accuracy_tools/api_accuracy_checker/hook_module/wrap_tensor.py @@ -41,10 +41,11 @@ class HOOKTensor(object): class TensorOPTemplate(HOOKModule): - def __init__(self, op_name, hook): + def __init__(self, op_name, hook, need_hook=True): self.op_name_ = op_name self.prefix_op_name_ = "Tensor*" + str(op_name) + "*" - super().__init__(hook) + if need_hook: + super().__init__(hook) @torch_device_guard def forward(self, *args, **kwargs): diff --git a/debug/accuracy_tools/api_accuracy_checker/hook_module/wrap_torch.py b/debug/accuracy_tools/api_accuracy_checker/hook_module/wrap_torch.py index 4903e56385..07a037b779 100644 --- a/debug/accuracy_tools/api_accuracy_checker/hook_module/wrap_torch.py +++ b/debug/accuracy_tools/api_accuracy_checker/hook_module/wrap_torch.py @@ -41,10 +41,11 @@ class HOOKTorchOP(object): class TorchOPTemplate(HOOKModule): - def __init__(self, op_name, hook): + def __init__(self, op_name, hook, need_hook=True): self.op_name_ = op_name self.prefix_op_name_ = "Torch*" + str(op_name) + "*" - super().__init__(hook) + if need_hook: + super().__init__(hook) def input_param_need_adapt(self): special_op_list = ["broadcast_tensors"] @@ -52,7 +53,7 @@ class TorchOPTemplate(HOOKModule): if item in self.op_name_: return True return False - + def einsum_adapt(self, *args): if len(args) < 2: raise ValueError('einsum(): must specify the equation string and at least one operand, ' @@ -69,7 +70,7 @@ class TorchOPTemplate(HOOKModule): return chr(ord('a') + n - 26) raise ValueError('einsum(): subscript in subscript list is not within the valid range [0, 52]') equation = ','.join(''.join(parse_subscript(s) for s in l) for l in args[1::2]) - + if len(args) % 2 == 1: equation += '->' + ''.join(parse_subscript(s) for s in args[-1]) operands = args[:-1:2] diff --git a/debug/accuracy_tools/api_accuracy_checker/run_ut/data_generate.py b/debug/accuracy_tools/api_accuracy_checker/run_ut/data_generate.py index 92c85a16a8..14c26331ec 100644 --- a/debug/accuracy_tools/api_accuracy_checker/run_ut/data_generate.py +++ b/debug/accuracy_tools/api_accuracy_checker/run_ut/data_generate.py @@ -22,6 +22,7 @@ import numpy as np from api_accuracy_checker.common.utils import Const, check_file_or_directory_path, check_object_type, print_warn_log, print_error_log, \ CompareException +TORCH_TYPE = ["torch.device", "torch.dtype"] TENSOR_DATA_LIST = ["torch.Tensor", "torch.nn.parameter.Parameter"] FLOAT_TYPE = ['torch.float32', 'torch.float', 'torch.float64', 'torch.double', 'torch.float16', 'torch.half', 'torch.bfloat16'] @@ -187,8 +188,8 @@ def gen_kwargs(api_info, convert_type=None): kwargs_params[key] = gen_list_kwargs(value, convert_type) elif value.get('type') in TENSOR_DATA_LIST: kwargs_params[key] = gen_data(value, False, convert_type) - elif value.get('type') == "torch.device": - kwargs_params[key] = torch.device(value.get('value')) + elif value.get('type') in TORCH_TYPE: + kwargs_params[key] = eval(value.get('type'))(value.get('value')) else: kwargs_params[key] = value.get('value') return kwargs_params diff --git a/debug/accuracy_tools/api_accuracy_checker/run_ut/run_ut.py b/debug/accuracy_tools/api_accuracy_checker/run_ut/run_ut.py index 350f436b38..1fe42015a0 100644 --- a/debug/accuracy_tools/api_accuracy_checker/run_ut/run_ut.py +++ b/debug/accuracy_tools/api_accuracy_checker/run_ut/run_ut.py @@ -7,6 +7,9 @@ import torch from api_accuracy_checker.run_ut.data_generate import gen_api_params, gen_args from api_accuracy_checker.common.utils import print_info_log, print_warn_log, get_json_contents, check_need_convert from api_accuracy_checker.compare.compare import Comparator +from api_accuracy_checker.hook_module.wrap_tensor import TensorOPTemplate +from api_accuracy_checker.hook_module.wrap_functional import FunctionalOPTemplate +from api_accuracy_checker.hook_module.wrap_torch import TorchOPTemplate NO_GRAD_APIS = ["hardtanh"] @@ -22,11 +25,14 @@ for f in dir(torch.nn.functional): def exec_api(api_type, api_name, args, kwargs): if api_type == "Functional": - out = eval(api_name)(*args, **kwargs) + functional_api = FunctionalOPTemplate(api_name, str, False) + out = functional_api.forward(*args, **kwargs) if api_type == "Tensor": - out = getattr(torch._C._TensorBase, str(api_name))(*args, **kwargs) + tensor_api = TensorOPTemplate(api_name, str, False) + out = tensor_api.forward(*args, **kwargs) if api_type == "Torch": - out = getattr(torch._C._VariableFunctionsClass, str(api_name))(*args, **kwargs) + torch_api = TorchOPTemplate(api_name, str, False) + out = torch_api.forward(*args, **kwargs) return out @@ -76,6 +82,8 @@ def run_torch_api(api_full_name, api_setting_dict, backward_content, value): print_warn_log("%s involves in-place operations, skip backward" % api_full_name) npu_args, npu_kwargs = generate_npu_params(args, kwargs, need_backward) grad_out, npu_grad_out = None, None + if kwargs.get("device"): + del kwargs["device"] out = exec_api(api_type, api_name, args, kwargs) npu_out = exec_api(api_type, api_name, npu_args, npu_kwargs) grad_input_index = api_setting_dict.get(api_name) -- Gitee From 337f7f347741ac38360280b803d858052667cc07 Mon Sep 17 00:00:00 2001 From: wangchao Date: Sat, 5 Aug 2023 15:40:30 +0800 Subject: [PATCH 2/4] run_ut support params convert --- .../api_accuracy_checker/run_ut/data_generate.py | 9 ++++++++- .../accuracy_tools/api_accuracy_checker/run_ut/run_ut.py | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/debug/accuracy_tools/api_accuracy_checker/run_ut/data_generate.py b/debug/accuracy_tools/api_accuracy_checker/run_ut/data_generate.py index 14c26331ec..4200b3b3b1 100644 --- a/debug/accuracy_tools/api_accuracy_checker/run_ut/data_generate.py +++ b/debug/accuracy_tools/api_accuracy_checker/run_ut/data_generate.py @@ -189,12 +189,19 @@ def gen_kwargs(api_info, convert_type=None): elif value.get('type') in TENSOR_DATA_LIST: kwargs_params[key] = gen_data(value, False, convert_type) elif value.get('type') in TORCH_TYPE: - kwargs_params[key] = eval(value.get('type'))(value.get('value')) + gen_torch_kwargs(kwargs_params, key, value) else: kwargs_params[key] = value.get('value') return kwargs_params +def gen_torch_kwargs(kwargs_params, key, value): + if value.get('type') == "torch.device": + kwargs_params[key] = eval(value.get('type'))(value.get('value')) + else: + kwargs_params[key] = eval(value.get('value')) + + def gen_list_kwargs(kwargs_item_value, convert_type): """ Function Description: diff --git a/debug/accuracy_tools/api_accuracy_checker/run_ut/run_ut.py b/debug/accuracy_tools/api_accuracy_checker/run_ut/run_ut.py index 1fe42015a0..b326d225d8 100644 --- a/debug/accuracy_tools/api_accuracy_checker/run_ut/run_ut.py +++ b/debug/accuracy_tools/api_accuracy_checker/run_ut/run_ut.py @@ -145,6 +145,7 @@ def _run_ut(): args = parser.parse_args(sys.argv[1:]) if not args.jit_compile: torch.npu.set_compile_mode(jit_compile=False) + torch.npu.set_device("npu:0") forward_file = os.path.realpath(args.forward_input_file) backward_file = os.path.realpath(args.backward_input_file) if not forward_file.endswith(".json") or not backward_file.endswith(".json"): -- Gitee From d818e5150738db65ea1e4e0026cf500b67a354f8 Mon Sep 17 00:00:00 2001 From: wangchao Date: Sat, 5 Aug 2023 15:52:43 +0800 Subject: [PATCH 3/4] run_ut support params convert --- .../api_accuracy_checker/run_ut/run_ut.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/debug/accuracy_tools/api_accuracy_checker/run_ut/run_ut.py b/debug/accuracy_tools/api_accuracy_checker/run_ut/run_ut.py index b326d225d8..a5c96b207e 100644 --- a/debug/accuracy_tools/api_accuracy_checker/run_ut/run_ut.py +++ b/debug/accuracy_tools/api_accuracy_checker/run_ut/run_ut.py @@ -5,7 +5,8 @@ import torch_npu import yaml import torch from api_accuracy_checker.run_ut.data_generate import gen_api_params, gen_args -from api_accuracy_checker.common.utils import print_info_log, print_warn_log, get_json_contents, check_need_convert +from api_accuracy_checker.common.utils import print_info_log, print_warn_log, get_json_contents, check_need_convert, \ + print_error_log from api_accuracy_checker.compare.compare import Comparator from api_accuracy_checker.hook_module.wrap_tensor import TensorOPTemplate from api_accuracy_checker.hook_module.wrap_functional import FunctionalOPTemplate @@ -137,6 +138,8 @@ def _run_ut_parser(parser): help=" Save compare failed api output.", required=False) parser.add_argument("-c", "--jit_compile", dest="jit_compile", help=" whether to turn on jit compile", default=True, required=False) + parser.add_argument("-d", "--device", dest="device_id", type=int, help=" set NPU device id to run ut", + default=0, required=False) def _run_ut(): @@ -145,7 +148,12 @@ def _run_ut(): args = parser.parse_args(sys.argv[1:]) if not args.jit_compile: torch.npu.set_compile_mode(jit_compile=False) - torch.npu.set_device("npu:0") + npu_device = "npu:" + str(args.device_id) + try: + torch.npu.set_device(npu_device) + except Exception: + print_error_log(f"Set NPU device id failed. device id is: {args.device_id}") + raise NotImplementedError forward_file = os.path.realpath(args.forward_input_file) backward_file = os.path.realpath(args.backward_input_file) if not forward_file.endswith(".json") or not backward_file.endswith(".json"): -- Gitee From 8c0c36b1c6dbfe7009ca933e017afb00b9c0e796 Mon Sep 17 00:00:00 2001 From: wangchao Date: Mon, 7 Aug 2023 09:13:40 +0800 Subject: [PATCH 4/4] add group_norm support half --- debug/accuracy_tools/api_accuracy_checker/common/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debug/accuracy_tools/api_accuracy_checker/common/utils.py b/debug/accuracy_tools/api_accuracy_checker/common/utils.py index 8ca6381ce9..988bcaa8f0 100644 --- a/debug/accuracy_tools/api_accuracy_checker/common/utils.py +++ b/debug/accuracy_tools/api_accuracy_checker/common/utils.py @@ -77,13 +77,13 @@ class Const: API_PATTERN = r"^[A-Za-z0-9]+[_]+([A-Za-z0-9]+[_]*[A-Za-z0-9]+)[_]+[0-9]+[_]+[A-Za-z0-9]+" WRITE_FLAGS = os.O_WRONLY | os.O_CREAT WRITE_MODES = stat.S_IWUSR | stat.S_IRUSR - + CONVERT = { "fp16_to_fp32": ["torch.float16", "torch.float32"] } CONVERT_API = { - "fp16_to_fp32": ["conv2d", "batch_norm", "relu", "max_pool2d", "interpolate"] + "fp16_to_fp32": ["conv2d", "batch_norm", "relu", "max_pool2d", "interpolate", "group_norm"] } class CompareConst: -- Gitee