已合并
Math仓算子新增Golden文件,方便算子质量看护和精度测试。 #1782
ExerrCise创建于 3月20日
Math仓算子新增Golden文件,方便算子质量看护和精度测试。 #1782
已合并
共 15 个文件变更+1004-0
| @@ -0,0 +1,44 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy as np | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "clip_by_value": "clip_by_value_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def clip_by_value_golden(x, clip_value_min, clip_value_max, **kwargs): | ||
| 23 | + ''' | ||
| 24 | + Golden function for clip_by_value. | ||
| 25 | + All the parameters (names and order) follow @clip_by_value_def.cpp without outputs. | ||
| 26 | + All the input Tensors are numpy.ndarray. | ||
| 27 | + | ||
| 28 | + Args: | ||
| 29 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 30 | + full_soc_version, short_soc_version, testcase_name | ||
| 31 | + | ||
| 32 | + Returns: | ||
| 33 | + Output tensor | ||
| 34 | + ''' | ||
| 35 | + if "bfloat16" in str(x.dtype): | ||
| 36 | + x = x.astype("float32") | ||
| 37 | + clip_value_min = clip_value_min.astype("float32") | ||
| 38 | + clip_value_max = clip_value_max.astype("float32") | ||
| 39 | + min_ = np.minimum(x, clip_value_max) | ||
| 40 | + res = np.maximum(min_, clip_value_min) | ||
| 41 | + if "bfloat16" in str(x.dtype): | ||
| 42 | + return res.astype(x.dtype, copy=False) | ||
| 43 | + return res | ||
| 44 | + | ||
| @@ -0,0 +1,43 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy as np | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "clip_by_value_v2": "clip_by_value_v2_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def clip_by_value_v2_golden(x, clip_value_min, clip_value_max, **kwargs): | ||
| 23 | + ''' | ||
| 24 | + Golden function for clip_by_value_v2. | ||
| 25 | + All the parameters (names and order) follow @clip_by_value_v2_def.cpp without outputs. | ||
| 26 | + All the input Tensors are numpy.ndarray. | ||
| 27 | + | ||
| 28 | + Args: | ||
| 29 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 30 | + full_soc_version, short_soc_version, testcase_name | ||
| 31 | + | ||
| 32 | + Returns: | ||
| 33 | + Output tensor | ||
| 34 | + ''' | ||
| 35 | + if "bfloat16" in str(x.dtype): | ||
| 36 | + x = x.astype("float32") | ||
| 37 | + clip_value_min = clip_value_min.astype("float32") | ||
| 38 | + clip_value_max = clip_value_max.astype("float32") | ||
| 39 | + max_ = np.maximum(x, clip_value_min) | ||
| 40 | + res = np.minimum(max_, clip_value_max) | ||
| 41 | + if "bfloat16" in str(x.dtype): | ||
| 42 | + return res.astype(x.dtype, copy=False) | ||
| 43 | + return res | ||
| @@ -0,0 +1,83 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "concat": "concat_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def update_axis_for_hw_inner_format(ori_shape, axis, input_format, ori_format, reduce_mode=False): | ||
| 23 | + if input_format in ("NDC1HWC0", "NC1HWC0"): | ||
| 24 | + ori_shape_len = len(ori_shape) if -2 not in ori_shape else len(ori_format) | ||
| 25 | + axis = axis % ori_shape_len | ||
| 26 | + offset_6hd = 1 if input_format == "NDC1HWC0" else 0 | ||
| 27 | + format_c_axis = 1 + offset_6hd if not reduce_mode else [1 + offset_6hd, 4 + offset_6hd] | ||
| 28 | + format_axis_map = { | ||
| 29 | + "N": 0, | ||
| 30 | + "C": format_c_axis, | ||
| 31 | + "H": 2 + offset_6hd, | ||
| 32 | + "W": 3 + offset_6hd, | ||
| 33 | + "D": 1 | ||
| 34 | + } | ||
| 35 | + concat_dim_name = ori_format[axis] | ||
| 36 | + axis = format_axis_map[concat_dim_name] | ||
| 37 | + | ||
| 38 | + if input_format in ("FRACTAL_NZ",): | ||
| 39 | + axis = axis % len(ori_shape) | ||
| 40 | + if axis == len(ori_shape) - 1: | ||
| 41 | + axis = len(ori_shape) - 2 if not reduce_mode else [len(ori_shape) - 2, len(ori_shape) + 1] | ||
| 42 | + elif axis == len(ori_shape) - 2: | ||
| 43 | + axis = len(ori_shape) - 1 if not reduce_mode else [len(ori_shape) - 1, len(ori_shape) + 0] | ||
| 44 | + | ||
| 45 | + if input_format in ("FRACTAL_Z", "FRACTAL_Z_3D"): | ||
| 46 | + axis = axis % len(ori_shape) | ||
| 47 | + offset_3d = 1 if input_format == "FRACTAL_Z_3D" else 0 | ||
| 48 | + format_c_axis = 0 + offset_3d if not reduce_mode else [0 + offset_3d, 5 + offset_3d] | ||
| 49 | + format_n_axis = 3 + offset_3d if not reduce_mode else [3 + offset_3d, 4 + offset_3d] | ||
| 50 | + format_axis_map = { | ||
| 51 | + "N": format_n_axis, | ||
| 52 | + "C": format_c_axis, | ||
| 53 | + "H": 1 + offset_3d, | ||
| 54 | + "W": 2 + offset_3d, | ||
| 55 | + "D": 0 | ||
| 56 | + } | ||
| 57 | + concat_dim_name = ori_format[axis] | ||
| 58 | + axis = format_axis_map[concat_dim_name] | ||
| 59 | + | ||
| 60 | + return axis | ||
| 61 | + | ||
| 62 | + | ||
| 63 | +def concat_golden(concat_dim, x, *, N=1, **kwargs): | ||
| 64 | + ''' | ||
| 65 | + Golden function for concat. | ||
| 66 | + All the parameters (names and order) follow @concat_def.cpp without outputs. | ||
| 67 | + All the input Tensors are numpy.ndarray. | ||
| 68 | + | ||
| 69 | + Args: | ||
| 70 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 71 | + full_soc_version, short_soc_version, testcase_name | ||
| 72 | + | ||
| 73 | + Returns: | ||
| 74 | + Output tensor | ||
| 75 | + ''' | ||
| 76 | + x_arrays = list(x) | ||
| 77 | + | ||
| 78 | + ori_shape = kwargs.get('input_ori_shapes', [x[0].shape])[0] | ||
| 79 | + input_formats = kwargs.get('input_formats', ['ND']) | ||
| 80 | + input_ori_formats = kwargs.get('input_ori_formats', ['ND']) | ||
| 81 | + | ||
| 82 | + concat_dim = update_axis_for_hw_inner_format(ori_shape, concat_dim, input_formats[0], input_ori_formats[0]) | ||
| 83 | + return numpy.concatenate(x_arrays, axis=concat_dim) | ||
| @@ -0,0 +1,83 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "concat_d": "concat_d_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def update_axis_for_hw_inner_format(ori_shape, axis, input_format, ori_format, reduce_mode=False): | ||
| 23 | + if input_format in ("NDC1HWC0", "NC1HWC0"): | ||
| 24 | + ori_shape_len = len(ori_shape) if -2 not in ori_shape else len(ori_format) | ||
| 25 | + axis = axis % ori_shape_len | ||
| 26 | + offset_6hd = 1 if input_format == "NDC1HWC0" else 0 | ||
| 27 | + format_c_axis = 1 + offset_6hd if not reduce_mode else [1 + offset_6hd, 4 + offset_6hd] | ||
| 28 | + format_axis_map = { | ||
| 29 | + "N": 0, | ||
| 30 | + "C": format_c_axis, | ||
| 31 | + "H": 2 + offset_6hd, | ||
| 32 | + "W": 3 + offset_6hd, | ||
| 33 | + "D": 1 | ||
| 34 | + } | ||
| 35 | + concat_dim_name = ori_format[axis] | ||
| 36 | + axis = format_axis_map[concat_dim_name] | ||
| 37 | + | ||
| 38 | + if input_format in ("FRACTAL_NZ",): | ||
| 39 | + axis = axis % len(ori_shape) | ||
| 40 | + if axis == len(ori_shape) - 1: | ||
| 41 | + axis = len(ori_shape) - 2 if not reduce_mode else [len(ori_shape) - 2, len(ori_shape) + 1] | ||
| 42 | + elif axis == len(ori_shape) - 2: | ||
| 43 | + axis = len(ori_shape) - 1 if not reduce_mode else [len(ori_shape) - 1, len(ori_shape) + 0] | ||
| 44 | + | ||
| 45 | + if input_format in ("FRACTAL_Z", "FRACTAL_Z_3D"): | ||
| 46 | + axis = axis % len(ori_shape) | ||
| 47 | + offset_3d = 1 if input_format == "FRACTAL_Z_3D" else 0 | ||
| 48 | + format_c_axis = 0 + offset_3d if not reduce_mode else [0 + offset_3d, 5 + offset_3d] | ||
| 49 | + format_n_axis = 3 + offset_3d if not reduce_mode else [3 + offset_3d, 4 + offset_3d] | ||
| 50 | + format_axis_map = { | ||
| 51 | + "N": format_n_axis, | ||
| 52 | + "C": format_c_axis, | ||
| 53 | + "H": 1 + offset_3d, | ||
| 54 | + "W": 2 + offset_3d, | ||
| 55 | + "D": 0 | ||
| 56 | + } | ||
| 57 | + concat_dim_name = ori_format[axis] | ||
| 58 | + axis = format_axis_map[concat_dim_name] | ||
| 59 | + | ||
| 60 | + return axis | ||
| 61 | + | ||
| 62 | + | ||
| 63 | +def concat_d_golden(x, *, concat_dim, N=1, **kwargs): | ||
| 64 | + ''' | ||
| 65 | + Golden function for concat. | ||
| 66 | + All the parameters (names and order) follow @concat_d_def.cpp without outputs. | ||
| 67 | + All the input Tensors are numpy.ndarray. | ||
D | |||
| 68 | + | ||
| 69 | + Args: | ||
| 70 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 71 | + full_soc_version, short_soc_version, testcase_name | ||
| 72 | + | ||
| 73 | + Returns: | ||
| 74 | + Output tensor | ||
| 75 | + ''' | ||
| 76 | + x_arrays = list(x) | ||
| 77 | + | ||
| 78 | + ori_shape = kwargs.get('input_ori_shapes', [x[0].shape])[0] | ||
| 79 | + input_formats = kwargs.get('input_formats', ['ND']) | ||
| 80 | + input_ori_formats = kwargs.get('input_ori_formats', ['ND']) | ||
| 81 | + | ||
| 82 | + concat_dim = update_axis_for_hw_inner_format(ori_shape, concat_dim, input_formats[0], input_ori_formats[0]) | ||
| 83 | + return numpy.concatenate(x_arrays, axis=concat_dim) | ||
| @@ -0,0 +1,83 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "concat_v2": "concat_v2_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def update_axis_for_hw_inner_format(ori_shape, axis, input_format, ori_format, reduce_mode=False): | ||
| 23 | + if input_format in ("NDC1HWC0", "NC1HWC0"): | ||
| 24 | + ori_shape_len = len(ori_shape) if -2 not in ori_shape else len(ori_format) | ||
| 25 | + axis = axis % ori_shape_len | ||
| 26 | + offset_6hd = 1 if input_format == "NDC1HWC0" else 0 | ||
| 27 | + format_c_axis = 1 + offset_6hd if not reduce_mode else [1 + offset_6hd, 4 + offset_6hd] | ||
| 28 | + format_axis_map = { | ||
| 29 | + "N": 0, | ||
| 30 | + "C": format_c_axis, | ||
| 31 | + "H": 2 + offset_6hd, | ||
| 32 | + "W": 3 + offset_6hd, | ||
| 33 | + "D": 1 | ||
| 34 | + } | ||
| 35 | + concat_dim_name = ori_format[axis] | ||
| 36 | + axis = format_axis_map[concat_dim_name] | ||
| 37 | + | ||
| 38 | + if input_format in ("FRACTAL_NZ",): | ||
| 39 | + axis = axis % len(ori_shape) | ||
| 40 | + if axis == len(ori_shape) - 1: | ||
| 41 | + axis = len(ori_shape) - 2 if not reduce_mode else [len(ori_shape) - 2, len(ori_shape) + 1] | ||
| 42 | + elif axis == len(ori_shape) - 2: | ||
| 43 | + axis = len(ori_shape) - 1 if not reduce_mode else [len(ori_shape) - 1, len(ori_shape) + 0] | ||
| 44 | + | ||
| 45 | + if input_format in ("FRACTAL_Z", "FRACTAL_Z_3D"): | ||
| 46 | + axis = axis % len(ori_shape) | ||
| 47 | + offset_3d = 1 if input_format == "FRACTAL_Z_3D" else 0 | ||
| 48 | + format_c_axis = 0 + offset_3d if not reduce_mode else [0 + offset_3d, 5 + offset_3d] | ||
| 49 | + format_n_axis = 3 + offset_3d if not reduce_mode else [3 + offset_3d, 4 + offset_3d] | ||
| 50 | + format_axis_map = { | ||
| 51 | + "N": format_n_axis, | ||
| 52 | + "C": format_c_axis, | ||
| 53 | + "H": 1 + offset_3d, | ||
| 54 | + "W": 2 + offset_3d, | ||
| 55 | + "D": 0 | ||
| 56 | + } | ||
| 57 | + concat_dim_name = ori_format[axis] | ||
| 58 | + axis = format_axis_map[concat_dim_name] | ||
| 59 | + | ||
| 60 | + return axis | ||
| 61 | + | ||
| 62 | + | ||
| 63 | +def concat_v2_golden(x, concat_dim, *, N=1, **kwargs): | ||
| 64 | + ''' | ||
| 65 | + Golden function for concat. | ||
| 66 | + All the parameters (names and order) follow @concat_def.cpp without outputs. | ||
| 67 | + All the input Tensors are numpy.ndarray. | ||
| 68 | + | ||
| 69 | + Args: | ||
| 70 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 71 | + full_soc_version, short_soc_version, testcase_name | ||
| 72 | + | ||
| 73 | + Returns: | ||
| 74 | + Output tensor | ||
| 75 | + ''' | ||
| 76 | + x_arrays = list(x) | ||
| 77 | + | ||
| 78 | + ori_shape = kwargs.get('input_ori_shapes', [x[0].shape])[0] | ||
| 79 | + input_formats = kwargs.get('input_formats', ['ND']) | ||
| 80 | + input_ori_formats = kwargs.get('input_ori_formats', ['ND']) | ||
| 81 | + | ||
| 82 | + concat_dim = update_axis_for_hw_inner_format(ori_shape, concat_dim, input_formats[0], input_ori_formats[0]) | ||
| 83 | + return numpy.concatenate(x_arrays, axis=concat_dim) | ||
| @@ -0,0 +1,57 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "depth_to_space": "depth_to_space_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def depth_to_space_golden(x, *, block_size, mode, data_format, **kwargs): | ||
| 23 | + ''' | ||
| 24 | + Golden function for depth_to_space. | ||
| 25 | + All the parameters (names and order) follow @depth_to_space_def.cpp without outputs. | ||
| 26 | + All the input Tensors are numpy.ndarray. | ||
| 27 | + | ||
| 28 | + Args: | ||
| 29 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 30 | + full_soc_version, short_soc_version, testcase_name | ||
| 31 | + | ||
| 32 | + Returns: | ||
| 33 | + Output tensor | ||
| 34 | + ''' | ||
| 35 | + shapes = x.shape | ||
| 36 | + | ||
| 37 | + if data_format == "NCHW": | ||
| 38 | + n, c, h, w = shapes | ||
| 39 | + output_shapes = [n, c // (block_size ** 2), h * block_size, w * block_size] | ||
| 40 | + if mode == "CRD": | ||
| 41 | + input_shapes = [n, c // (block_size ** 2), block_size, block_size, h, w] | ||
| 42 | + perm = [0, 1, 4, 2, 5, 3] | ||
| 43 | + else: # mode == "DCR" | ||
| 44 | + input_shapes = [n, block_size, block_size, c // (block_size ** 2), h, w] | ||
| 45 | + perm = [0, 3, 4, 1, 5, 2] | ||
| 46 | + else: # data_format == "NHWC": | ||
| 47 | + n, h, w, c = shapes | ||
| 48 | + output_shapes = [n, h * block_size, w * block_size, c // (block_size ** 2)] | ||
| 49 | + if mode == "CRD": | ||
| 50 | + input_shapes = [n, h, w, c // (block_size ** 2), block_size, block_size] | ||
| 51 | + perm = [0, 1, 4, 2, 5, 3] | ||
| 52 | + else: # mode == "DCR" | ||
| 53 | + input_shapes = [n, h, w, block_size, block_size, c // (block_size ** 2)] | ||
| 54 | + perm = [0, 1, 3, 2, 4, 5] | ||
| 55 | + tmp = x.reshape(input_shapes) | ||
| 56 | + tmp = numpy.transpose(tmp, perm) | ||
| 57 | + return tmp.reshape(output_shapes) | ||
| @@ -0,0 +1,36 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy as np | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "diag_v2": "diag_v2_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def diag_v2_golden(x, *, diagonal, **kwargs): | ||
| 23 | + ''' | ||
| 24 | + Golden function for diag_v2. | ||
| 25 | + All the parameters (names and order) follow @diag_v2_def.cpp without outputs. | ||
| 26 | + All the input Tensors are numpy.ndarray. | ||
| 27 | + | ||
| 28 | + Args: | ||
| 29 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 30 | + full_soc_version, short_soc_version, testcase_name | ||
| 31 | + | ||
| 32 | + Returns: | ||
| 33 | + Output tensor | ||
| 34 | + ''' | ||
| 35 | + | ||
| 36 | + return np.diag(x, k=diagonal) | ||
| @@ -0,0 +1,48 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy as np | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "bias_add": "bias_add_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def bias_add_golden(x, bias, *, data_format=None, **kwargs): | ||
| 23 | + ''' | ||
| 24 | + Golden function for bias_add. | ||
| 25 | + All the parameters (names and order) follow @bias_add_def.cpp without outputs. | ||
| 26 | + All the input Tensors are numpy.ndarray. | ||
| 27 | + | ||
| 28 | + Args: | ||
| 29 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 30 | + full_soc_version, short_soc_version, testcase_name | ||
| 31 | + | ||
| 32 | + Returns: | ||
| 33 | + Output tensor | ||
| 34 | + ''' | ||
| 35 | + import tensorflow as tf | ||
| 36 | + | ||
| 37 | + tf.compat.v1.disable_eager_execution() | ||
| 38 | + x_input = tf.compat.v1.placeholder(shape=x.shape, dtype=x.dtype) | ||
| 39 | + bias_input = tf.compat.v1.placeholder(shape=bias.shape, dtype=bias.dtype) | ||
| 40 | + | ||
| 41 | + out = tf.nn.bias_add(x_input, bias_input, data_format=data_format, name="biasadd") | ||
| 42 | + feed_dict = {x_input: x, bias_input: bias} | ||
| 43 | + init_op = tf.compat.v1.global_variables_initializer() | ||
| 44 | + | ||
| 45 | + with tf.compat.v1.Session() as sess: | ||
| 46 | + sess.run(init_op) | ||
| 47 | + res = sess.run(out, feed_dict=feed_dict) | ||
| 48 | + return res.astype(kwargs['output_dtypes'][0]) | ||
| @@ -0,0 +1,85 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy as np | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "bias_add_grad": "bias_add_grad_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def _infer_axes(input_data_format, data_format, shape): | ||
| 23 | + g_shape_list = [] | ||
| 24 | + if input_data_format == 'FRACTAL_NZ': | ||
| 25 | + if data_format == "NCHW": | ||
| 26 | + if len(shape) == 4: | ||
| 27 | + for i in range(-1 * len(shape), 0): | ||
| 28 | + if i not in (-1, -4): | ||
| 29 | + g_shape_list += [i + len(shape)] | ||
| 30 | + elif len(shape) == 5: | ||
| 31 | + for i in range(-1 * len(shape), 0): | ||
| 32 | + if i not in (-2, -3): | ||
| 33 | + g_shape_list += [i + len(shape)] | ||
| 34 | + else: | ||
| 35 | + g_shape_list.append(0) | ||
| 36 | + for i in range(2, len(shape)): | ||
| 37 | + g_shape_list = g_shape_list + [i] | ||
| 38 | + else: | ||
| 39 | + if len(shape) < 4: | ||
| 40 | + raise RuntimeError("cce_bias_add_grad_nz_2_nhwc only support shape larger than 4D") | ||
| 41 | + for i in range(-1 * len(shape), 0): | ||
| 42 | + if i not in (-1, -4): | ||
| 43 | + g_shape_list += [i + len(shape)] | ||
| 44 | + elif input_data_format in ("FRACTAL_Z", "FRACTAL_Z_3D", "NC1HWC0", "NDC1HWC0"): | ||
| 45 | + if input_data_format == "FRACTAL_Z": | ||
| 46 | + g_shape_list = [1, 2, 3, 4] | ||
| 47 | + elif input_data_format == "FRACTAL_Z_3D": | ||
| 48 | + g_shape_list = [0, 2, 3, 4, 5] | ||
| 49 | + elif input_data_format == "NC1HWC0": | ||
| 50 | + g_shape_list = [0, 2, 3] | ||
| 51 | + elif input_data_format == "NDC1HWC0": | ||
| 52 | + g_shape_list = [0, 1, 3, 4] | ||
| 53 | + else: | ||
| 54 | + if data_format == "NCHW": | ||
| 55 | + g_shape_list = [0] | ||
| 56 | + for i in range(2, len(shape)): | ||
| 57 | + g_shape_list += [i] | ||
| 58 | + else: | ||
| 59 | + if len(shape) < 2: | ||
| 60 | + raise RuntimeError("cce_bias_add_grad only support shape larger than 2D") | ||
| 61 | + g_shape_list = [x for x in range(len(shape) - 1)] | ||
| 62 | + return g_shape_list | ||
| 63 | + | ||
| 64 | + | ||
| 65 | +def __eliminate_duplicate_axes(axis, x): | ||
| 66 | + axis = tuple(set([_ax if _ax >= 0 else len(x.shape) + _ax for _ax in axis])) | ||
| 67 | + return axis | ||
| 68 | + | ||
| 69 | + | ||
| 70 | +def bias_add_grad_golden(x, *, data_format, **kwargs): | ||
| 71 | + ''' | ||
| 72 | + Golden function for bias_add_grad. | ||
| 73 | + All the parameters (names and order) follow @bias_add_grad_def.cpp without outputs. | ||
| 74 | + All the input Tensors are numpy.ndarray. | ||
| 75 | + | ||
| 76 | + Args: | ||
| 77 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 78 | + full_soc_version, short_soc_version, testcase_name | ||
| 79 | + | ||
| 80 | + Returns: | ||
| 81 | + Output tensor | ||
| 82 | + ''' | ||
| 83 | + actual_formats = kwargs.get('input_formats', ['ND']) | ||
| 84 | + axis = __eliminate_duplicate_axes(_infer_axes(actual_formats[0], data_format, x.shape), x) | ||
| 85 | + return np.sum(x, axis=axis, dtype="float64") | ||
| @@ -0,0 +1,74 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy as np | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "bincount": "bincount_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def numpy_to_torch_tensor(np_array): | ||
| 23 | + import torch | ||
| 24 | + if np_array is None: | ||
| 25 | + return None | ||
| 26 | + np_dtype = np_array.dtype.name | ||
| 27 | + if "bfloat16" in np_dtype: | ||
| 28 | + np_int16 = np_array.view(dtype=np.int16) | ||
| 29 | + t_int16 = torch.from_numpy(np_int16) | ||
| 30 | + return t_int16.view(torch.bfloat16) | ||
| 31 | + else: | ||
| 32 | + return torch.from_numpy(np_array) | ||
| 33 | + | ||
| 34 | + | ||
| 35 | +def torch_to_numpy_tensor(torch_tensor): | ||
| 36 | + import torch | ||
| 37 | + if torch_tensor is None: | ||
| 38 | + return None | ||
| 39 | + if not isinstance(torch_tensor, torch.Tensor): | ||
| 40 | + raise RuntimeError(f"Only support torch.Tensor. But got {type(torch_tensor)}") | ||
| 41 | + torch_dtype = torch_tensor.dtype | ||
| 42 | + if torch_dtype == torch.bfloat16: | ||
| 43 | + t_int16 = torch_tensor.view(torch.int16) | ||
| 44 | + np_int16 = t_int16.numpy() | ||
| 45 | + return np_int16.view(dtype=np.bfloat16) | ||
| 46 | + else: | ||
| 47 | + return torch_tensor.numpy() | ||
| 48 | + | ||
| 49 | + | ||
| 50 | +def bincount_golden(array, size, weight, **kwargs): | ||
| 51 | + ''' | ||
| 52 | + Golden function for bincount. | ||
| 53 | + All the parameters (names and order) follow @bincount_def.cpp without outputs. | ||
| 54 | + All the input Tensors are numpy.ndarray. | ||
| 55 | + | ||
| 56 | + Args: | ||
| 57 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 58 | + full_soc_version, short_soc_version, testcase_name | ||
| 59 | + | ||
| 60 | + Returns: | ||
| 61 | + Output tensor | ||
| 62 | + ''' | ||
| 63 | + import torch | ||
| 64 | + | ||
| 65 | + array_tensor = numpy_to_torch_tensor(array) | ||
| 66 | + weight_tensor = numpy_to_torch_tensor(weight) | ||
| 67 | + | ||
| 68 | + if weight_tensor.numel() == 0: | ||
| 69 | + res = torch.bincount(array_tensor).to(dtype=torch.int32) | ||
| 70 | + else: | ||
| 71 | + size = int(size) if size is not None else 0 | ||
| 72 | + res = torch.bincount(array_tensor, weights=weight_tensor, minlength=size) | ||
| 73 | + | ||
| 74 | + return torch_to_numpy_tensor(res) | ||
| @@ -0,0 +1,48 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy as np | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "cumsum": "cumsum_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | +def cumsum_golden(x, axis, *, exclusive, reverse, **kwargs): | ||
| 22 | + ''' | ||
| 23 | + Golden function for cumsum. | ||
| 24 | + All the parameters (names and order) follow @cumsum_def.cpp without outputs. | ||
| 25 | + All the input Tensors are numpy.ndarray. | ||
| 26 | + | ||
| 27 | + Args: | ||
| 28 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 29 | + full_soc_version, short_soc_version, testcase_name | ||
| 30 | + | ||
| 31 | + Returns: | ||
| 32 | + Output tensor | ||
| 33 | + ''' | ||
| 34 | + import tensorflow.compat.v1 as tf | ||
| 35 | + tf.disable_eager_execution() | ||
| 36 | + | ||
| 37 | + x_dtype = x.dtype | ||
| 38 | + if x_dtype.name == "bfloat16" or x_dtype.name == "float16": | ||
| 39 | + x = x.astype("float32") | ||
| 40 | + | ||
| 41 | + p0 = tf.constant(x) | ||
| 42 | + out = tf.cumsum(x=p0, axis=axis, reverse=reverse, exclusive=exclusive) | ||
| 43 | + | ||
| 44 | + with tf.Session() as sess: | ||
| 45 | + res = sess.run(out) | ||
| 46 | + if x_dtype.name == "bfloat16" or x_dtype.name == "float16": | ||
| 47 | + res = res.astype(x_dtype, copy=False) | ||
| 48 | + return res | ||
| @@ -0,0 +1,47 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy as np | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "diag_part": "diag_part_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def diag_part_golden(x, **kwargs): | ||
| 23 | + ''' | ||
| 24 | + Golden function for diag_part. | ||
| 25 | + All the parameters (names and order) follow @diag_part_def.cpp without outputs. | ||
| 26 | + All the input Tensors are numpy.ndarray. | ||
| 27 | + | ||
| 28 | + Args: | ||
| 29 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 30 | + full_soc_version, short_soc_version, testcase_name | ||
| 31 | + | ||
| 32 | + Returns: | ||
| 33 | + Output tensor | ||
| 34 | + ''' | ||
| 35 | + import tensorflow.compat.v1 as tf | ||
| 36 | + tf.disable_v2_behavior() | ||
| 37 | + | ||
| 38 | + dtype = x.dtype | ||
| 39 | + if "bfloat16" in str(dtype): | ||
| 40 | + x = x.view("float16") | ||
| 41 | + x_holder = tf.placeholder(x.dtype, shape=x.shape) | ||
| 42 | + res = tf.diag_part(x_holder) | ||
| 43 | + with tf.Session() as session: | ||
| 44 | + res = session.run(res, feed_dict={x_holder: x}) | ||
| 45 | + if "bfloat16" in str(dtype): | ||
| 46 | + res = res.view(dtype) | ||
| 47 | + return res | ||
| @@ -0,0 +1,124 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "div": "div_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def broadcast_to_maxshape(shapes: list): | ||
| 23 | + def _max(_shape): | ||
| 24 | + no_one_shape = [s for s in _shape if s != 1] | ||
| 25 | + if len(no_one_shape) == 0: | ||
| 26 | + max_value = 1 | ||
| 27 | + else: | ||
| 28 | + max_value = no_one_shape[0] | ||
| 29 | + return max_value | ||
| 30 | + max_dim_length = max(len(list(shape)) for shape in shapes) | ||
| 31 | + input_shapes = [] | ||
| 32 | + for shape in shapes: | ||
| 33 | + input_shapes.append([1 for _ in range(max_dim_length - len(shape))] + list(shape)) | ||
| 34 | + input_shapes = list(map(list, zip(*input_shapes))) | ||
| 35 | + max_shape = [_max(shape) for shape in input_shapes] | ||
| 36 | + input_shapes = list(map(list, zip(*input_shapes))) | ||
| 37 | + return (*input_shapes, max_shape) | ||
| 38 | + | ||
| 39 | + | ||
| 40 | +def div_golden(x1, x2, **kwargs): | ||
| 41 | + ''' | ||
| 42 | + Golden function for div. | ||
| 43 | + All the parameters (names and order) follow @div_def.cpp without outputs. | ||
| 44 | + All the input Tensors are numpy.ndarray. | ||
| 45 | + | ||
| 46 | + Args: | ||
| 47 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 48 | + full_soc_version, short_soc_version, testcase_name | ||
| 49 | + | ||
| 50 | + Returns: | ||
| 51 | + Output tensor | ||
| 52 | + ''' | ||
| 53 | + ori_dtype = x1.dtype | ||
| 54 | + input_dtypes = kwargs.get('input_dtypes', [ori_dtype.name, ori_dtype.name]) | ||
| 55 | + | ||
| 56 | + if "float16" in str(ori_dtype): #include bfloat16 & float16 | ||
| 57 | + x1, x2 = x1.astype("float32"), x2.astype("float32") | ||
| 58 | + | ||
| 59 | + if "complex32" in input_dtypes: | ||
| 60 | + import torch | ||
| 61 | + | ||
| 62 | + def complex32_div(reala, imaga, realb, imagb): | ||
| 63 | + abs_b1 = numpy.abs(realb) | ||
| 64 | + abs_b2 = numpy.abs(imagb) | ||
| 65 | + if abs_b1 >= abs_b2: | ||
| 66 | + if abs_b1 == 0 and abs_b2 == 0: | ||
| 67 | + real_ = reala / abs_b1 | ||
| 68 | + imag_ = imaga / abs_b2 | ||
| 69 | + else: | ||
| 70 | + temp1 = imagb / realb | ||
| 71 | + temp2 = realb + imagb * temp1 | ||
| 72 | + tensor_one = numpy.array(1.0, dtype=numpy.float16) | ||
| 73 | + cm = tensor_one / temp2 | ||
| 74 | + real_ = (reala + imaga * temp1) * cm | ||
| 75 | + imag_ = (imaga - reala * temp1) * cm | ||
| 76 | + else: | ||
| 77 | + temp1 = realb / imagb | ||
| 78 | + temp2 = imagb + realb * temp1 | ||
| 79 | + tensor_one = numpy.array(1.0, dtype=numpy.float16) | ||
| 80 | + cm = tensor_one / temp2 | ||
| 81 | + real_ = (imaga + reala * temp1) * cm | ||
| 82 | + imag_ = (imaga * temp1 - reala) * cm | ||
| 83 | + | ||
| 84 | + return real_, imag_ | ||
| 85 | + | ||
| 86 | + x1, x2 = numpy.broadcast_arrays(x1, x2) | ||
| 87 | + xreal, ximag = numpy.split(x1, 2, axis=-1) | ||
| 88 | + yreal, yimag = numpy.split(x2, 2, axis=-1) | ||
| 89 | + ori_shape = xreal.shape | ||
| 90 | + xreal = xreal.reshape(-1) | ||
| 91 | + ximag = ximag.reshape(-1) | ||
| 92 | + yreal = yreal.reshape(-1) | ||
| 93 | + yimag = yimag.reshape(-1) | ||
| 94 | + input_xr = torch.from_numpy(xreal) | ||
| 95 | + input_xi = torch.from_numpy(ximag) | ||
| 96 | + input_yr = torch.from_numpy(yreal) | ||
| 97 | + input_yi = torch.from_numpy(yimag) | ||
| 98 | + zreal = torch.zeros_like(input_xr) | ||
| 99 | + zimag = torch.zeros_like(input_xr) | ||
| 100 | + for i in range(len(input_xr)): | ||
| 101 | + zreal[i], zimag[i] = complex32_div(input_xr[i],input_xi[i], input_yr[i], input_yi[i]) | ||
| 102 | + | ||
| 103 | + zreal = zreal.numpy() | ||
| 104 | + zimag = zimag.numpy() | ||
| 105 | + zreal = zreal.reshape(ori_shape) | ||
| 106 | + zimag = zimag.reshape(ori_shape) | ||
| 107 | + res = numpy.concatenate((zreal, zimag), axis=-1) | ||
| 108 | + return res | ||
| 109 | + else: | ||
| 110 | + import tensorflow as tf | ||
| 111 | + tf.compat.v1.disable_eager_execution() | ||
| 112 | + _, _, shape_max = broadcast_to_maxshape([x1.shape, x2.shape]) | ||
| 113 | + x1 = numpy.broadcast_to(x1, shape_max) | ||
| 114 | + x2 = numpy.broadcast_to(x2, shape_max) | ||
| 115 | + x = tf.compat.v1.placeholder(shape=x1.shape, dtype=x1.dtype) | ||
| 116 | + y = tf.compat.v1.placeholder(shape=x2.shape, dtype=x2.dtype) | ||
| 117 | + out = tf.compat.v1.div(x, y) | ||
| 118 | + feed_dict = {x: x1, y: x2} | ||
| 119 | + init_op = tf.compat.v1.global_variables_initializer() | ||
| 120 | + | ||
| 121 | + with tf.compat.v1.Session() as sess: | ||
| 122 | + sess.run(init_op) | ||
| 123 | + res = sess.run(out, feed_dict=feed_dict) | ||
| 124 | + return res.astype(ori_dtype, copy=False) | ||
| @@ -0,0 +1,67 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy as np | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "div_no_nan": "div_no_nan_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def div_no_nan_golden(x1, x2, **kwargs): | ||
| 23 | + ''' | ||
| 24 | + Golden function for div_no_nan. | ||
| 25 | + All the parameters (names and order) follow @div_no_nan_def.cpp without outputs. | ||
| 26 | + All the input Tensors are numpy.ndarray. | ||
| 27 | + | ||
| 28 | + Args: | ||
| 29 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 30 | + full_soc_version, short_soc_version, testcase_name | ||
| 31 | + | ||
| 32 | + Returns: | ||
| 33 | + Output tensor | ||
| 34 | + ''' | ||
| 35 | + import tensorflow as tf | ||
| 36 | + tf.compat.v1.disable_eager_execution() | ||
| 37 | + | ||
| 38 | + x1_placeholder = tf.compat.v1.placeholder(shape=x1.shape, dtype=x1.dtype) | ||
| 39 | + x2_placeholder = tf.compat.v1.placeholder(shape=x2.shape, dtype=x2.dtype) | ||
| 40 | + | ||
| 41 | + if x1.dtype == np.int8 or x1.dtype == np.int8: | ||
| 42 | + x1_fp16 = tf.cast(x1_placeholder, tf.float16) | ||
| 43 | + x2_fp16 = tf.cast(x2_placeholder, tf.float16) | ||
| 44 | + out = tf.compat.v1.div_no_nan(x1_fp16, x2_fp16, name="divnonan") | ||
| 45 | + out = tf.cast(out, tf.int8) | ||
| 46 | + elif x1.dtype == np.uint8 or x1.dtype == np.uint8: | ||
| 47 | + x1_fp16 = tf.cast(x1_placeholder, tf.float16) | ||
| 48 | + x2_fp16 = tf.cast(x2_placeholder, tf.float16) | ||
| 49 | + out = tf.compat.v1.div_no_nan(x1_fp16, x2_fp16, name="divnonan") | ||
| 50 | + out = tf.cast(out, tf.uint8) | ||
| 51 | + elif x1.dtype == np.int32 or x1.dtype == np.int32: | ||
| 52 | + x1_fp32 = tf.cast(x1_placeholder, tf.float32) | ||
| 53 | + x2_fp32 = tf.cast(x2_placeholder, tf.float32) | ||
| 54 | + out = tf.compat.v1.div_no_nan(x1_fp32, x2_fp32, name="divnonan") | ||
| 55 | + out = tf.cast(out, tf.int32) | ||
| 56 | + else: | ||
| 57 | + out = tf.compat.v1.div_no_nan(x1_placeholder, x2_placeholder, name="divnonan") | ||
| 58 | + | ||
| 59 | + feed_dict = {x1_placeholder: x1, x2_placeholder: x2} | ||
| 60 | + init_op = tf.compat.v1.global_variables_initializer() | ||
| 61 | + | ||
| 62 | + with tf.compat.v1.Session() as sess: | ||
| 63 | + sess.run(init_op) | ||
| 64 | + res = sess.run(out, feed_dict=feed_dict) | ||
| 65 | + | ||
| 66 | + output_dtypes = kwargs.get('output_dtypes', [None]) | ||
| 67 | + return res.astype(output_dtypes[0]) if output_dtypes[0] else res | ||
| @@ -0,0 +1,82 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import numpy as np | ||
| 14 | + | ||
| 15 | +__golden__ = { | ||
| 16 | + "kernel": { | ||
| 17 | + "drop_out_do_mask": "drop_out_do_mask_golden" | ||
| 18 | + } | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def revert_bit(n): | ||
| 23 | + result = 0 | ||
| 24 | + for i in range(8): | ||
| 25 | + result <<= 1 | ||
| 26 | + result |= n & 1 | ||
| 27 | + n >>= 1 | ||
| 28 | + return result | ||
| 29 | + | ||
| 30 | + | ||
| 31 | +def revert_array_bit(arr): | ||
| 32 | + res = [] | ||
| 33 | + for item in arr.flatten(): | ||
| 34 | + res.append(revert_bit(item)) | ||
| 35 | + return np.array(res, dtype=np.uint8).reshape(arr.shape) | ||
| 36 | + | ||
| 37 | + | ||
| 38 | +def drop_out_do_mask_golden(x, mask, keep_prob, **kwargs): | ||
| 39 | + ''' | ||
| 40 | + Golden function for drop_out_do_mask. | ||
| 41 | + All the parameters (names and order) follow @drop_out_do_mask_def.cpp without outputs. | ||
| 42 | + All the input Tensors are numpy.ndarray. | ||
| 43 | + | ||
| 44 | + Args: | ||
| 45 | + **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 46 | + full_soc_version, short_soc_version, testcase_name | ||
| 47 | + | ||
| 48 | + Returns: | ||
| 49 | + Output tensor | ||
| 50 | + ''' | ||
| 51 | + short_soc_version = kwargs.get('short_soc_version', '') | ||
| 52 | + dtype = str(x.dtype) | ||
| 53 | + | ||
| 54 | + if short_soc_version in ("Ascend950",): | ||
| 55 | + if dtype in ("bfloat16", "float16"): | ||
| 56 | + x = x.astype("float32") | ||
| 57 | + keep_prob = keep_prob.astype("float32") | ||
| 58 | + if keep_prob.flat[0] == 1.0: | ||
| 59 | + x = x.astype(dtype) | ||
| 60 | + return x | ||
| 61 | + elif keep_prob.flat[0] == 0.0: | ||
| 62 | + y_out = np.zeros(x.shape, dtype=dtype) | ||
| 63 | + return y_out | ||
| 64 | + else: | ||
| 65 | + if str(x.dtype) == "bfloat16": | ||
| 66 | + x = x.astype("float32") | ||
| 67 | + keep_prob = keep_prob.astype("float32") | ||
| 68 | + | ||
| 69 | + shape_x = x.shape | ||
| 70 | + x_scale = x * (1.0 / keep_prob) | ||
| 71 | + mask = revert_array_bit(mask) | ||
| 72 | + mask_dtype = np.unpackbits(mask, axis=-1).astype(x.dtype) | ||
| 73 | + | ||
| 74 | + size_x = 1 | ||
| 75 | + x_scale = x_scale.flatten() | ||
| 76 | + for i in shape_x: | ||
| 77 | + size_x = size_x * i | ||
| 78 | + expect = x_scale | ||
| 79 | + mask_dtype = mask_dtype[:size_x] | ||
| 80 | + expect[mask_dtype == 0] = 0 | ||
| 81 | + output = expect.reshape(shape_x) | ||
| 82 | + return output.astype(dtype) | ||
@concat_d_def.cpp