Merged
fix(operation): Make Round Behave Consistently With AscendC. #5889
刘奕哲Add on Sep 3
fix(operation): Make Round Behave Consistently With AscendC. #5889
Merged
Merged fromCreeeeper/pyptofix-roundintocann/pyptomaster
Total: 5 files changed+17-30
| @@ -19,7 +19,7 @@ | |||
| 19 | ## 函数原型 | 19 | ## 函数原型 |
| 20 | 20 | ||
| 21 | ```python | 21 | ```python |
| 22 | -round(input: Tensor, decimals: int) -> Tensor | 22 | +round(input: Tensor, decimals: int = 0) -> Tensor |
| 23 | ``` | 23 | ``` |
| 24 | 24 | ||
| 25 | ## 参数说明 | 25 | ## 参数说明 |
| @@ -37,15 +37,16 @@ round(input: Tensor, decimals: int) -> Tensor | |||
| 37 | 37 | ||
| 38 | 1. Tensor数据类型说明: | 38 | 1. Tensor数据类型说明: |
| 39 | <!-- npu="950" id4 --> | 39 | <!-- npu="950" id4 --> |
| 40 | - - Ascend 950PR/Ascend 950DT:DT_FP32,DT_FP16,DT_BF16,DT_INT32,DT_INT16,DT_INT64。 | 40 | + - Ascend 950PR/Ascend 950DT:DT_FP32,DT_FP16,DT_BF16,DT_INT32,DT_INT64。 |
| 41 | <!-- end id4 --> | 41 | <!-- end id4 --> |
| 42 | <!-- npu="A3" id5 --> | 42 | <!-- npu="A3" id5 --> |
| 43 | - - Atlas A3 训练系列产品/Atlas A3 推理系列产品:DT_FP32,DT_FP16,DT_BF16,DT_INT32,DT_INT16。 | 43 | + - Atlas A3 训练系列产品/Atlas A3 推理系列产品:DT_FP32,DT_FP16,DT_BF16,DT_INT32。 |
| 44 | <!-- end id5 --> | 44 | <!-- end id5 --> |
| 45 | <!-- npu="910b" id6 --> | 45 | <!-- npu="910b" id6 --> |
| 46 | - - Atlas A2 训练系列产品/Atlas A2 推理系列产品:DT_FP32,DT_FP16,DT_BF16,DT_INT32,DT_INT16。 | 46 | + - Atlas A2 训练系列产品/Atlas A2 推理系列产品:DT_FP32,DT_FP16,DT_BF16,DT_INT32。 |
| 47 | <!-- end id6 --> | 47 | <!-- end id6 --> |
| 48 | 2. Tensor类型输入不支持`TileOpFormat.TILEOP_NZ`格式。 | 48 | 2. Tensor类型输入不支持`TileOpFormat.TILEOP_NZ`格式。 |
| 49 | +3. 当输入Tensor的数据类型为DT_INT32或DT_INT64时,decimals必须为0,此时返回结果与输入Tensor一致。 | ||
| 49 | 50 | ||
| 50 | ## 调用示例 | 51 | ## 调用示例 |
| 51 | 52 | ||
| @@ -260,9 +260,8 @@ Tensor Round(const Tensor& self, const int& decimals) | |||
| 260 | DECLARE_TRACER(); | 260 | DECLARE_TRACER(); |
| 261 | CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Round"); | 261 | CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Round"); |
| 262 | 262 | ||
| 263 | - static const std::unordered_set<DataType> ROUND_A2A3_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16}; | 263 | + static const std::unordered_set<DataType> ROUND_A2A3_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32}; |
| 264 | - static const std::unordered_set<DataType> ROUND_A5_TYPES = {DT_FP32, DT_FP16, DT_BF16, | 264 | + static const std::unordered_set<DataType> ROUND_A5_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT64}; |
| 265 | - DT_INT32, DT_INT16, DT_INT64}; | ||
| 266 | const auto& supportedTypes = GetSupportedDataTypesByArch(ROUND_A2A3_TYPES, ROUND_A5_TYPES); | 265 | const auto& supportedTypes = GetSupportedDataTypesByArch(ROUND_A2A3_TYPES, ROUND_A5_TYPES); |
| 267 | CheckTensorDataType(self.GetStorage(), supportedTypes, "ROUND"); | 266 | CheckTensorDataType(self.GetStorage(), supportedTypes, "ROUND"); |
| 268 | CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "ROUND"); | 267 | CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "ROUND"); |
| @@ -1535,6 +1535,13 @@ def round(input: Tensor, decimals: int = 0) -> Tensor: | |||
| 1535 | Input x: [[1.21, 2.35], [3.65, 4.76]] | 1535 | Input x: [[1.21, 2.35], [3.65, 4.76]] |
| 1536 | Output y: [[1.2, 2.4], [3.6, 4.8]] | 1536 | Output y: [[1.2, 2.4], [3.6, 4.8]] |
| 1537 | """ | 1537 | """ |
| 1538 | + if input.dtype in (DataType.DT_INT32, DataType.DT_INT64): | ||
| 1539 | + if decimals != 0: | ||
| 1540 | + raise PyptoError( | ||
| 1541 | + 0xF00002, | ||
| 1542 | + ValueError(f"round(): decimals must be 0 for integer tensor dtype {input.dtype.name}."), | ||
| 1543 | + ) | ||
| 1544 | + return add(input, 0) | ||
| 1538 | return pypto_impl.Round(input, decimals) | 1545 | return pypto_impl.Round(input, decimals) |
| 1539 | 1546 | ||
| 1540 | 1547 | ||
| @@ -33,8 +33,7 @@ def round_2d_1input_2d_output_kernel(input0: pypto.Tensor(), output0: pypto.Tens | |||
| 33 | def round_golden(input_tensor, decimals): | 33 | def round_golden(input_tensor, decimals): |
| 34 | if input_tensor.is_floating_point(): | 34 | if input_tensor.is_floating_point(): |
| 35 | return torch.round(input_tensor, decimals=decimals) | 35 | return torch.round(input_tensor, decimals=decimals) |
| 36 | - scale = 10 ** (-decimals) | 36 | + return torch.round(input_tensor) |
| 37 | - return torch.round(input_tensor.to(torch.float64) / scale).mul(scale).to(input_tensor.dtype) | ||
| 38 | 37 | ||
| 39 | 38 | ||
| 40 | KERNELS = { | 39 | KERNELS = { |
| @@ -69,33 +69,14 @@ ROUND_TESTS = [ | |||
| 69 | 'name': 'input0', | 69 | 'name': 'input0', |
| 70 | 'shape': (128, 128), | 70 | 'shape': (128, 128), |
| 71 | 'dtype': 'int32', | 71 | 'dtype': 'int32', |
| 72 | - 'data_range': {'min': 0, 'max': 1}, | 72 | + 'data_range': {'min': -2147483648, 'max': 2147483647}, |
| 73 | 'format': 'ND', | 73 | 'format': 'ND', |
| 74 | } | 74 | } |
| 75 | ], | 75 | ], |
| 76 | 'output_tensors': [{'name': 'output0', 'shape': (128, 128), 'dtype': 'int32', 'format': 'ND'}], | 76 | 'output_tensors': [{'name': 'output0', 'shape': (128, 128), 'dtype': 'int32', 'format': 'ND'}], |
| 77 | 'view_shape': (40, 40), | 77 | 'view_shape': (40, 40), |
| 78 | 'tile_shape': (32, 32), | 78 | 'tile_shape': (32, 32), |
| 79 | - 'params': {'decimals': -1}, | 79 | + 'params': {'decimals': 0}, |
| 80 | 'source_case_index': 13, | 80 | 'source_case_index': 13, |
| 81 | }, | 81 | }, |
| 82 | - { | ||
| 83 | - 'case_index': 14, | ||
| 84 | - 'case_name': 'Round_test_14', | ||
| 85 | - 'operation': 'Round', | ||
| 86 | - 'input_tensors': [ | ||
| 87 | - { | ||
| 88 | - 'name': 'input0', | ||
| 89 | - 'shape': (120, 120), | ||
| 90 | - 'dtype': 'int16', | ||
| 91 | - 'data_range': {'min': 0, 'max': 1}, | ||
| 92 | - 'format': 'ND', | ||
| 93 | - } | ||
| 94 | - ], | ||
| 95 | - 'output_tensors': [{'name': 'output0', 'shape': (120, 120), 'dtype': 'int16', 'format': 'ND'}], | ||
| 96 | - 'view_shape': (32, 32), | ||
| 97 | - 'tile_shape': (32, 32), | ||
| 98 | - 'params': {'decimals': -2}, | ||
| 99 | - 'source_case_index': 14, | ||
| 100 | - }, | ||
| 101 | ] | 82 | ] |