diff --git a/debug/accuracy_tools/api_accuracy_checker/dump/api_info.py b/debug/accuracy_tools/api_accuracy_checker/dump/api_info.py index adb0c4b0f34215b93c2bcbcce1d8bbd4bdac877b..12d59820c6fb61821a05fc644577bce0dd839a35 100644 --- a/debug/accuracy_tools/api_accuracy_checker/dump/api_info.py +++ b/debug/accuracy_tools/api_accuracy_checker/dump/api_info.py @@ -2,6 +2,7 @@ import os import inspect import torch +import numpy as np from api_accuracy_checker.common.config import msCheckerConfig from api_accuracy_checker.common.utils import print_error_log, write_pt, create_directory, DumpException from ptdbg_ascend.src.python.ptdbg_ascend.common.utils import check_path_before_create @@ -92,6 +93,10 @@ class APIInfo: else: out_dict[key] = self.analyze_element(value) return out_dict + + converted_numpy, numpy_type = self._convert_numpy_to_builtin(element) + if converted_numpy is not element: + return self._analyze_numpy(converted_numpy, numpy_type) if isinstance(element, torch.Tensor): return self._analyze_tensor(element) @@ -135,6 +140,29 @@ class APIInfo: single_arg.update({'type': get_type_name(str(type(arg)))}) single_arg.update({'value': arg}) return single_arg + + def _analyze_numpy(self, value, numpy_type): + single_arg = {} + if self.is_save_data: + self.args_num += 1 + single_arg.update({'type': numpy_type}) + single_arg.update({'value': value}) + return single_arg + + def _convert_numpy_to_builtin(self, arg): + type_mapping = { + np.integer: int, + np.floating: float, + np.bool_: bool, + np.complexfloating: complex, + np.str_: str, + np.bytes_: bytes, + np.unicode_: str + } + for numpy_type, builtin_type in type_mapping.items(): + if isinstance(arg, numpy_type): + return builtin_type(arg), get_type_name(str(type(arg))) + return arg, '' class ForwardAPIInfo(APIInfo): 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 5765f980d2850e9caee6147230e8fbd48aec46be..21bc23cfb86d79d99e9b42fb3d35e7a173e8de4a 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 @@ -17,7 +17,7 @@ import os import torch -import numpy as np +import numpy from api_accuracy_checker.common.utils import Const, check_file_or_directory_path, check_object_type, print_warn_log, print_error_log, \ CompareException @@ -26,6 +26,9 @@ 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'] +NUMPY_TYPE = ["numpy.int8", "numpy.int16", "numpy.int32", "numpy.int64", "numpy.uint8", "numpy.uint16", "numpy.uint32", + "numpy.uint64", "numpy.float16", "numpy.float32", "numpy.float64", "numpy.float128", "numpy.complex64", + "numpy.complex128", "numpy.complex256", "numpy.bool_", "numpy.string_", "numpy.bytes_", "numpy.unicode_"] def gen_data(info, need_grad, convert_type): @@ -50,6 +53,14 @@ def gen_data(info, need_grad, convert_type): temp_data = data * 1 data = temp_data.type_as(data) data.retain_grad() + elif data_type.startswith("numpy"): + if data_type not in NUMPY_TYPE: + raise Exception("{} is not supported now".format(data_type)) + data = info.get("value") + try: + data = eval(data_type)(data) + except Exception as err: + print_error_log("Failed to convert the type to numpy: %s" % str(err)) else: data = info.get('value') if info.get("type") == "slice": @@ -73,7 +84,7 @@ def gen_real_tensor(data_path, convert_type): if data_path.endswith('.pt'): data = torch.load(data_path) else: - data_np = np.load(data_path) + data_np = numpy.load(data_path) data = torch.from_numpy(data_np) if convert_type: ori_dtype = Const.CONVERT.get(convert_type)[0] @@ -193,7 +204,7 @@ def gen_kwargs(api_info, convert_type=None): for key, value in kwargs_params.items(): if isinstance(value, (list, tuple)): kwargs_params[key] = gen_list_kwargs(value, convert_type) - elif value.get('type') in TENSOR_DATA_LIST: + elif value.get('type') in TENSOR_DATA_LIST or value.get('type').startswith("numpy"): kwargs_params[key] = gen_data(value, False, convert_type) elif value.get('type') in TORCH_TYPE: gen_torch_kwargs(kwargs_params, key, value)