Merged
fix(math): handle atan2 zero boundaries in experimental kernel #6218
fix(math): handle atan2 zero boundaries in experimental kernel #6218
Merged
puppyAdd on Oct 11
Total: 8 files changed+420-271
@@ -8,7 +8,7 @@
8 8 
9## 功能说明9## 功能说明
10 10 
11-- 算子功能:计算两个输入张量 x1(分子,对应数学中的 y)和 x2(分母,对应数学中的 x)的反正切值,返回值的角度范围为 (−π, π],可正确区分所有象限。11+- 算子功能:计算两个输入张量 x1(分子,对应数学中的 y)和 x2(分母,对应数学中的 x)的反正切值,返回值的角度范围为 [−π, π],可正确区分所有象限。
12 12 
13- 计算公式:13- 计算公式:
14 14 
@@ -29,6 +29,8 @@ $$
29\end{cases}29\end{cases}
30$$30$$
31 31 
32+零值边界:当 x < 0 时,y 为 +0 返回 +π,y 为 −0 返回 −π;当 x > 0 时保留 y 的零值符号。当 x 和 y 都为零时,按上述定义返回 +0,包括带符号零的组合。
33+ 
32## 参数说明34## 参数说明
33 35 
34<table style="table-layout: fixed; width: 980px"><colgroup>36<table style="table-layout: fixed; width: 980px"><colgroup>
@@ -64,7 +66,7 @@ $$
64 <tr>66 <tr>
65 <td>y</td>67 <td>y</td>
66 <td>输出</td>68 <td>输出</td>
67- <td>计算结果张量,与输入 shape 相同,值域为 (−π, π]。</td>69+ <td>计算结果张量,与输入 shape 相同,值域为 [−π, π]。</td>
68 <td>fp16、fp32、bf16</td>70 <td>fp16、fp32、bf16</td>
69 <td>ND</td>71 <td>ND</td>
70 </tr>72 </tr>
@@ -10,190 +10,69 @@
10 10 
11/*!11/*!
12 * \file atan2_tiling.cpp12 * \file atan2_tiling.cpp
13- * \brief Tiling for the Atan2 binary elementwise operator.13+ * \brief Adapt the Atan2 buffer-aware schedule to the graph tiling context.
14- *
15- * Strategy: identical to celu_v2 big/small core split.
16- * UB budget accounts for:
17- * - 2 VECIN queues (x1, x2): BUFFER_NUM × tileDataNum × sizeof(T) each
18- * - 1 VECOUT queue (y): BUFFER_NUM × tileDataNum × sizeof(T)
19- * - 5 float32 VECCALC scratch: 5 × tileDataNum × sizeof(float)
20- * - 1 uint8_t VECCALC scratch for Atan: atanTmpSize bytes
21- *
22- * atanTmpSize is obtained via GetAtanMaxMinTmpSize (ascendc host-side API).
23- * If unavailable, we conservatively allocate 8× the tile float32 size.
24- *
25- * Supported dtypes: float32, float16, bfloat16.
26 */14 */
27 15 
16+#include "atan2_tiling_schedule.h"
28#include "log/log.h"17#include "log/log.h"
29-#include "util/math_util.h"
30#include "register/op_impl_registry.h"18#include "register/op_impl_registry.h"
31-#include <graph/utils/type_utils.h>
32#include "tiling/platform/platform_ascendc.h"19#include "tiling/platform/platform_ascendc.h"
33-#include "../op_kernel/atan2_tiling_data.h"
34#include "../op_kernel/atan2_tiling_key.h"20#include "../op_kernel/atan2_tiling_key.h"
35 21 
36namespace optiling {22namespace optiling {
37 23 
38-constexpr uint32_t BLOCK_SIZE = 32;
39-constexpr uint32_t BUFFER_NUM = 2;
40-constexpr uint32_t WS_SYS_SIZE = 512U;
41- 
42struct Atan2CompileInfo {};24struct Atan2CompileInfo {};
43 25 
44-static ge::graphStatus GetPlatformInfo(gert::TilingContext* context, uint64_t& ubSize, int64_t& coreNum)
45-{
46- fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo();
47- OP_CHECK_NULL_WITH_CONTEXT(context, platformInfoPtr);
48- auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr);
49- coreNum = ascendcPlatform.GetCoreNumAiv();
50- OP_CHECK_IF(coreNum == 0, OP_LOGE(context, "coreNum is 0"), return ge::GRAPH_FAILED);
51- ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize);
52- OP_CHECK_IF(ubSize == 0, OP_LOGE(context, "ubSize is 0"), return ge::GRAPH_FAILED);
53- return ge::GRAPH_SUCCESS;
54-}
55- 
56-static ge::graphStatus GetShapeInfo(gert::TilingContext* context, int64_t& totalIdx, ge::DataType& dataType)
57-{
58- auto inputX1 = context->GetInputShape(0);
59- OP_CHECK_NULL_WITH_CONTEXT(context, inputX1);
60- totalIdx = inputX1->GetStorageShape().GetShapeSize();
61- 
62- const std::set<ge::DataType> supportedDtype = {ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_BF16};
63- auto inputDesc = context->GetInputDesc(0);
64- OP_CHECK_NULL_WITH_CONTEXT(context, inputDesc);
65- dataType = inputDesc->GetDataType();
66- if (supportedDtype.count(dataType) == 0) {
67- OP_LOGE(context, "invalid dtype");
68- return ge::GRAPH_FAILED;
69- }
70- return ge::GRAPH_SUCCESS;
71-}
72- 
73-static ge::graphStatus GetWorkspaceSize(gert::TilingContext* context)
74-{
75- auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
76- uint32_t sysWorkspaceSize = ascendcPlatform.GetLibApiWorkSpaceSize();
77- size_t* currentWorkspace = context->GetWorkspaceSizes(1);
78- OP_CHECK_NULL_WITH_CONTEXT(context, currentWorkspace);
79- currentWorkspace[0] = WS_SYS_SIZE + sysWorkspaceSize;
80- return ge::GRAPH_SUCCESS;
81-}
82- 
83static ge::graphStatus Atan2TilingFunc(gert::TilingContext* context)26static ge::graphStatus Atan2TilingFunc(gert::TilingContext* context)
84{27{
85- // 1. Platform info28+ auto* input = context->GetInputShape(0);
86- uint64_t ubSize = 0;29+ auto* descriptor = context->GetInputDesc(0);
87- int64_t coreNum = 0;30+ auto* platformInfo = context->GetPlatformInfo();
88- OP_CHECK_IF(31+ auto* data = context->GetTilingData<Atan2TilingData>();
89- GetPlatformInfo(context, ubSize, coreNum) != ge::GRAPH_SUCCESS, OP_LOGE(context, "GetPlatformInfo error"),32+ OP_CHECK_NULL_WITH_CONTEXT(context, input);
90- return ge::GRAPH_FAILED);33+ OP_CHECK_NULL_WITH_CONTEXT(context, descriptor);
34+ OP_CHECK_NULL_WITH_CONTEXT(context, platformInfo);
35+ OP_CHECK_NULL_WITH_CONTEXT(context, data);
91 36 
92- // 2. Shape / dtype37+ uint32_t elementBytes = 0;
93- int64_t totalIdx = 0;38+ switch (descriptor->GetDataType()) {
94- ge::DataType dataType = ge::DataType::DT_UNDEFINED;39+ case ge::DT_FLOAT:
95- OP_CHECK_IF(40+ elementBytes = sizeof(float);
96- GetShapeInfo(context, totalIdx, dataType) != ge::GRAPH_SUCCESS, OP_LOGE(context, "GetShapeInfo error"),41+ break;
97- return ge::GRAPH_FAILED);42+ case ge::DT_FLOAT16:
98- 43+ case ge::DT_BF16:
99- // Handle empty input44+ elementBytes = sizeof(uint16_t);
100- if (totalIdx <= 0) {45+ break;
101- Atan2TilingData* tiling = context->GetTilingData<Atan2TilingData>();46+ default:
102- OP_CHECK_NULL_WITH_CONTEXT(context, tiling);47+ OP_LOGE(context, "Atan2 requires FLOAT, FLOAT16 or BF16 input");
103- memset_s(tiling, sizeof(Atan2TilingData), 0, sizeof(Atan2TilingData));48+ return ge::GRAPH_FAILED;
104- context->SetBlockDim(1);
105- context->SetTilingKey(GET_TPL_TILING_KEY(ELEMENTWISE_TPL_SCH_MODE_0));
106- return ge::GRAPH_SUCCESS;
107 }49 }
108 50 
109- // 3. Workspace51+ // One platform object supplies all resources, including library workspace.
110- OP_CHECK_IF(52+ platform_ascendc::PlatformAscendC platform(platformInfo);
111- GetWorkspaceSize(context) != ge::GRAPH_SUCCESS, OP_LOGE(context, "GetWorkspaceSize error"),53+ uint64_t ubBytes = 0;
112- return ge::GRAPH_FAILED);54+ platform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubBytes);
113- 55+ const uint32_t availableCores = platform.GetCoreNumAiv();
114- // 4. Compute tiling parameters56+ if (availableCores == 0 || ubBytes == 0) {
115- Atan2TilingData* tiling = context->GetTilingData<Atan2TilingData>();57+ OP_LOGE(context, "Atan2 requires nonzero AIV cores and UB capacity");
116- OP_CHECK_NULL_WITH_CONTEXT(context, tiling);
117- OP_CHECK_IF(
118- memset_s(tiling, sizeof(Atan2TilingData), 0, sizeof(Atan2TilingData)) != EOK,
119- OP_LOGE(context, "set tiling data error"), return ge::GRAPH_FAILED);
120- 
121- uint32_t typeLength = 0;
122- ge::TypeUtils::GetDataTypeLength(dataType, typeLength);
123- if (typeLength == 0) {
124- OP_LOGE(context, "typeLength is 0");
125 return ge::GRAPH_FAILED;58 return ge::GRAPH_FAILED;
126 }59 }
127- uint64_t inputBytes = static_cast<uint64_t>(typeLength);
128- uint64_t inputLengthBytes = static_cast<uint64_t>(totalIdx) * inputBytes;
129 60 
130- // elements per 32-byte block61+ *data = Atan2TilingData{};
131- uint32_t dtypeAlign = static_cast<uint32_t>(BLOCK_SIZE / inputBytes);62+ const int64_t elements = input->GetStorageShape().GetShapeSize();
132- 63+ uint32_t activeCores = 1;
133- uint32_t ubTotalFactor = 19U;64+ if (elements > 0) {
134- if (dataType == ge::DT_FLOAT16 || dataType == ge::DT_BF16) {65+ if (!atan2::BuildSchedule(static_cast<uint64_t>(elements), elementBytes, ubBytes, availableCores, *data,
135- ubTotalFactor = 16U;66+ activeCores)) {
136- } else if (dataType == ge::DT_FLOAT) {67+ OP_LOGE(context, "Atan2 shape or UB capacity cannot fit the kernel schedule");
137- ubTotalFactor = 19U;68+ return ge::GRAPH_FAILED;
69+ }
70+ auto* workspace = context->GetWorkspaceSizes(1);
71+ OP_CHECK_NULL_WITH_CONTEXT(context, workspace);
72+ workspace[0] = static_cast<size_t>(platform.GetLibApiWorkSpaceSize()) + 512U;
138 }73 }
139- 74+ context->SetBlockDim(activeCores);
140- uint64_t tileDataNum64 = (ubSize / sizeof(float)) / ubTotalFactor;
141- 
142- // Round down to nearest dtypeAlign multiple to keep DMA transfers aligned
143- tileDataNum64 = (tileDataNum64 / dtypeAlign) * dtypeAlign;
144- uint32_t tileDataNum = (tileDataNum64 == 0ULL) ? dtypeAlign : static_cast<uint32_t>(tileDataNum64);
145- 
146- // tileBlockNum: number of 32-byte BLOCK_SIZE units in one tile (input dtype)
147- uint32_t tileBlockNum =
148- static_cast<uint32_t>((static_cast<uint64_t>(tileDataNum) * inputBytes + BLOCK_SIZE - 1U) / BLOCK_SIZE);
149- 
150- uint32_t atanTmpSize = 8U * tileDataNum * sizeof(float);
151- tiling->atanTmpSize = atanTmpSize;
152- 
153- uint64_t blocksTotal = (inputLengthBytes + BLOCK_SIZE - 1ULL) / BLOCK_SIZE;
154- uint64_t coreNum64 = static_cast<uint64_t>(coreNum);
155- if (coreNum64 > blocksTotal) {
156- coreNum64 = blocksTotal;
157- }
158- if (coreNum64 == 0ULL) {
159- coreNum64 = 1ULL;
160- }
161- uint32_t finalCoreNum = static_cast<uint32_t>(coreNum64);
162- 
163- uint64_t everyCoreInputBlockNum = blocksTotal / coreNum64;
164- uint32_t tailBlockNum = static_cast<uint32_t>(blocksTotal % coreNum64);
165- 
166- // small-core
167- uint64_t smallCoreDataNum_u = everyCoreInputBlockNum * BLOCK_SIZE / inputBytes;
168- uint32_t smallCoreDataNum = static_cast<uint32_t>(smallCoreDataNum_u);
169- uint32_t smallTileNum = static_cast<uint32_t>(everyCoreInputBlockNum / static_cast<uint64_t>(tileBlockNum));
170- uint32_t finalSmallTileNum = ((everyCoreInputBlockNum % tileBlockNum) == 0) ? smallTileNum : (smallTileNum + 1);
171- int64_t smallTailDataNum_i =
172- static_cast<int64_t>(smallCoreDataNum) - static_cast<int64_t>(tileDataNum) * static_cast<int64_t>(smallTileNum);
173- uint32_t smallTailDataNum = (smallTailDataNum_i <= 0) ? tileDataNum : static_cast<uint32_t>(smallTailDataNum_i);
174- 
175- // big-core
176- uint64_t bigEveryCoreBlockNum = everyCoreInputBlockNum + 1ULL;
177- uint64_t bigCoreDataNum_u = bigEveryCoreBlockNum * BLOCK_SIZE / inputBytes;
178- uint32_t bigCoreDataNum = static_cast<uint32_t>(bigCoreDataNum_u);
179- uint32_t bigTileNum = static_cast<uint32_t>(bigEveryCoreBlockNum / static_cast<uint64_t>(tileBlockNum));
180- uint32_t finalBigTileNum = ((bigEveryCoreBlockNum % tileBlockNum) == 0) ? bigTileNum : (bigTileNum + 1);
181- int64_t bigTailDataNum_i =
182- static_cast<int64_t>(bigCoreDataNum) - static_cast<int64_t>(tileDataNum) * static_cast<int64_t>(bigTileNum);
183- uint32_t bigTailDataNum = (bigTailDataNum_i <= 0) ? tileDataNum : static_cast<uint32_t>(bigTailDataNum_i);
184- 
185- tiling->smallCoreDataNum = static_cast<int64_t>(smallCoreDataNum);
186- tiling->bigCoreDataNum = static_cast<int64_t>(bigCoreDataNum);
187- tiling->tileDataNum = static_cast<int64_t>(tileDataNum);
188- tiling->smallTailDataNum = static_cast<int64_t>(smallTailDataNum);
189- tiling->bigTailDataNum = static_cast<int64_t>(bigTailDataNum);
190- tiling->finalSmallTileNum = static_cast<int64_t>(finalSmallTileNum);
191- tiling->finalBigTileNum = static_cast<int64_t>(finalBigTileNum);
192- tiling->tailBlockNum = static_cast<int64_t>(tailBlockNum);
193- 
194- context->SetBlockDim(finalCoreNum);
195 context->SetTilingKey(GET_TPL_TILING_KEY(ELEMENTWISE_TPL_SCH_MODE_0));75 context->SetTilingKey(GET_TPL_TILING_KEY(ELEMENTWISE_TPL_SCH_MODE_0));
196- 
197 return ge::GRAPH_SUCCESS;76 return ge::GRAPH_SUCCESS;
198}77}
199 78 
@@ -203,4 +82,4 @@ static ge::graphStatus TilingParseForAtan2([[maybe_unused]] gert::TilingParseCon
203}82}
204 83 
205IMPL_OP_OPTILING(Atan2).Tiling(Atan2TilingFunc).TilingParse<Atan2CompileInfo>(TilingParseForAtan2);84IMPL_OP_OPTILING(Atan2).Tiling(Atan2TilingFunc).TilingParse<Atan2CompileInfo>(TilingParseForAtan2);
206-} // namespace optiling85+} // namespace optiling
@@ -0,0 +1,82 @@
1+/**
2+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8+ * See LICENSE in the root of the software repository for the full text of the License.
9+ */
10+ 
11+#ifndef ATAN2_TILING_SCHEDULE_H
12+#define ATAN2_TILING_SCHEDULE_H
13+ 
14+#include <algorithm>
15+#include <cstdint>
16+#include <limits>
17+#include "../op_kernel/atan2_tiling_data.h"
18+ 
19+namespace optiling {
20+namespace atan2 {
21+ 
22+constexpr uint64_t DMA_BYTES = 32;
23+constexpr uint64_t COMPARE_ELEMENTS = 256 / sizeof(float);
24+constexpr uint64_t SELECT_BYTES = 8192;
25+constexpr uint64_t ATAN_FLOAT_BUFFERS = 8;
26+ 
27+struct CoreSlice {
28+ int64_t elements;
29+ int64_t tiles;
30+ int64_t lastTile;
31+};
32+ 
33+inline CoreSlice DescribeCore(uint64_t blocks, uint64_t elementsPerBlock, uint64_t tile)
34+{
35+ const uint64_t elements = blocks * elementsPerBlock;
36+ return {static_cast<int64_t>(elements), static_cast<int64_t>(elements / tile + (elements % tile != 0)),
37+ static_cast<int64_t>((elements - 1) % tile + 1)};
38+}
39+ 
40+// Pure scheduling arithmetic, shared with the host and device regression
41+// probes. The buffer layout is three double-buffered queues, five float
42+// temporaries, eight float-sized Atan buffers, and Select's fixed scratch.
43+inline bool BuildSchedule(uint64_t elements, uint32_t elementBytes, uint64_t ubBytes, uint32_t availableCores,
44+ Atan2TilingData& data, uint32_t& blocks)
45+{
46+ if (elements == 0 || availableCores == 0 || (elementBytes != 2 && elementBytes != 4)) {
47+ return false;
48+ }
49+ const uint64_t bytesPerElement = 3 * 2 * elementBytes + (5 + ATAN_FLOAT_BUFFERS) * sizeof(float);
50+ if (ubBytes < SELECT_BYTES + COMPARE_ELEMENTS * bytesPerElement) {
51+ return false;
52+ }
53+ const uint64_t tile = (ubBytes - SELECT_BYTES) / bytesPerElement / COMPARE_ELEMENTS * COMPARE_ELEMENTS;
54+ if (tile > std::numeric_limits<uint32_t>::max() / (ATAN_FLOAT_BUFFERS * sizeof(float))) {
55+ return false;
56+ }
57+ const uint64_t elementsPerBlock = DMA_BYTES / elementBytes;
58+ const uint64_t dmaBlocks = elements / elementsPerBlock + (elements % elementsPerBlock != 0);
59+ // Kernel offsets and lengths are uint32_t, including padded DMA elements.
60+ if (dmaBlocks > std::numeric_limits<uint32_t>::max() / elementsPerBlock) {
61+ return false;
62+ }
63+ const uint32_t activeCores = static_cast<uint32_t>(std::min<uint64_t>(availableCores, dmaBlocks));
64+ const uint64_t regularBlocks = dmaBlocks / activeCores;
65+ const CoreSlice regular = DescribeCore(regularBlocks, elementsPerBlock, tile);
66+ const CoreSlice extra = DescribeCore(regularBlocks + 1, elementsPerBlock, tile);
67+ data.smallCoreDataNum = regular.elements;
68+ data.bigCoreDataNum = extra.elements;
69+ data.finalBigTileNum = extra.tiles;
70+ data.finalSmallTileNum = regular.tiles;
71+ data.tileDataNum = static_cast<int64_t>(tile);
72+ data.smallTailDataNum = regular.lastTile;
73+ data.bigTailDataNum = extra.lastTile;
74+ data.tailBlockNum = static_cast<int64_t>(dmaBlocks % activeCores);
75+ data.atanTmpSize = static_cast<uint32_t>(ATAN_FLOAT_BUFFERS * tile * sizeof(float));
76+ blocks = activeCores;
77+ return true;
78+}
79+ 
80+} // namespace atan2
81+} // namespace optiling
82+#endif
@@ -27,8 +27,7 @@ using namespace AscendC;
27constexpr int32_t BUFFER_NUM = 2;27constexpr int32_t BUFFER_NUM = 2;
28 28 
29constexpr float CONST_PI = 3.14159265358979323846f;29constexpr float CONST_PI = 3.14159265358979323846f;
30-constexpr float CONST_PI_BY_TWO = 1.57079632679489661923f;30+constexpr uint32_t COMPARE_ALIGN_ELEMS = 256 / sizeof(float);
31-constexpr float EPSILON = 1e-37f;
32 31 
33template <typename T>32template <typename T>
34class Atan2 {33class Atan2 {
@@ -127,13 +126,8 @@ __aicore__ inline void Atan2<T>::CopyOut(int32_t progress)
127 126 
128// ── Compute ────────────────────────────────────────────────────────────────127// ── Compute ────────────────────────────────────────────────────────────────
129//128//
130-// Scratch assignment during Compute (all float32 except atanTmp):129+// Reuse float scratch as comparison masks after computing the base angle;
131-// tmp0: step1→yF, step6→sign_x_soft→unsigned_out130+// no additional UB allocation is required for the zero boundary handling.
132-// tmp1: step1→xF, step5→blend_xne0
133-// tmp2: step2→ind_xlt0, step6→blend*(base_atan−π/2)
134-// tmp3: step2→scratch for ind denominator, step3→sign_y
135-// tmp4: step4→base_atan = Atan(|yF/xF|) [dst≠src per Atan constraint]
136-// atanTmpBuf: uint8_t scratch for Atan
137 131 
138template <typename T>132template <typename T>
139__aicore__ inline void Atan2<T>::Compute(int32_t progress)133__aicore__ inline void Atan2<T>::Compute(int32_t progress)
@@ -150,69 +144,73 @@ __aicore__ inline void Atan2<T>::Compute(int32_t progress)
150 LocalTensor<uint8_t> atanTmp = atanTmpBuf.Get<uint8_t>();144 LocalTensor<uint8_t> atanTmp = atanTmpBuf.Get<uint8_t>();
151 145 
152 uint32_t n = this->processDataNum;146 uint32_t n = this->processDataNum;
147+ uint32_t compareNum = (n + COMPARE_ALIGN_ELEMS - 1U) / COMPARE_ALIGN_ELEMS * COMPARE_ALIGN_ELEMS;
148+ if (compareNum > n) {
149+ // CompareScalar requires a complete 256-byte input repeat. Initialize
150+ // that final repeat before copying/casting the valid input elements.
151+ uint32_t lastRepeat = compareNum - COMPARE_ALIGN_ELEMS;
152+ AscendC::Duplicate(tmp0[lastRepeat], 0.0f, COMPARE_ALIGN_ELEMS);
153+ AscendC::Duplicate(tmp1[lastRepeat], 0.0f, COMPARE_ALIGN_ELEMS);
154+ if constexpr (AscendC::Std::is_same<T, bfloat16_t>::value) {
155+ AscendC::Duplicate(tmp4[lastRepeat], 0.0f, COMPARE_ALIGN_ELEMS);
156+ }
157+ }
153 158 
154 // ── Step 1: cast inputs to float32 → tmp0(yF), tmp1(xF) ──────────────159 // ── Step 1: cast inputs to float32 → tmp0(yF), tmp1(xF) ──────────────
155 if constexpr (AscendC::Std::is_same<T, half>::value || AscendC::Std::is_same<T, bfloat16_t>::value) {160 if constexpr (AscendC::Std::is_same<T, half>::value || AscendC::Std::is_same<T, bfloat16_t>::value) {
156 AscendC::Cast(tmp0, yLocal, RoundMode::CAST_NONE, n);161 AscendC::Cast(tmp0, yLocal, RoundMode::CAST_NONE, n);
157 AscendC::Cast(tmp1, xLocal, RoundMode::CAST_NONE, n);162 AscendC::Cast(tmp1, xLocal, RoundMode::CAST_NONE, n);
158 } else {163 } else {
159- // T == float: copy into float scratch164+ // Copy the bits so that the sign of a negative zero is preserved.
160- AscendC::Adds(tmp0, yLocal.template ReinterpretCast<float>(), 0.0f, n);165+ AscendC::DataCopy(tmp0, yLocal.template ReinterpretCast<float>(), n);
161- AscendC::Adds(tmp1, xLocal.template ReinterpretCast<float>(), 0.0f, n);166+ AscendC::DataCopy(tmp1, xLocal.template ReinterpretCast<float>(), n);
162 }167 }
163- // tmp0 = yF, tmp1 = xF
164 168 
165- // ── Step 2: ind_xlt0 = (x<0)?1:0 → tmp2 ─────────────────────────────169+ // Atan requires distinct source and destination tensors.
166- // neg_x = min(xF, 0) → 0 when x≥0, negative when x<0170+ AscendC::Div(tmp2, tmp0, tmp1, n);
167- // tmp2 = |neg_x| / (|neg_x| + ε) → 1 when x<0, 0 when x≥0171+ AscendC::Abs(tmp2, tmp2, n);
168- AscendC::Mins(tmp2, tmp1, 0.0f, n); // tmp2 = min(xF, 0)172+ AscendC::Atan(tmp4, tmp2, atanTmp, n);
169- AscendC::Abs(tmp2, tmp2, n); // tmp2 = |min(xF,0)|
170- AscendC::Adds(tmp3, tmp2, EPSILON, n); // tmp3 = |min(xF,0)| + ε (borrow tmp3 momentarily)
171- AscendC::Div(tmp2, tmp2, tmp3, n); // tmp2 = ind_xlt0 ∈ [0,1]
172 173 
173- // ── Step 3: sign_y = yF/(|yF|+ε) → tmp3 ─────────────────────────────174+ LocalTensor<uint8_t> mask = tmp2.ReinterpretCast<uint8_t>();
174- AscendC::Abs(tmp3, tmp0, n); // tmp3 = |yF|175+ LocalTensor<uint8_t> xZeroMask = tmp3.ReinterpretCast<uint8_t>();
175- AscendC::Adds(tmp3, tmp3, EPSILON, n); // tmp3 = |yF| + ε176+ AscendC::CompareScalar(mask, tmp1, 0.0f, CMPMODE::LT, compareNum);
176- AscendC::Div(tmp3, tmp0, tmp3, n); // tmp3 = sign_y ∈ (-1,+1]177+ AscendC::Muls(tmp3, tmp4, -1.0f, n);
178+ AscendC::Adds(tmp3, tmp3, CONST_PI, n);
179+ AscendC::Select(tmp4, mask, tmp3, tmp4, SELMODE::VSEL_TENSOR_TENSOR_MODE, n);
177 180 
178- // ── Step 4: base_atan = Atan(|yF/xF|) → tmp4 ─────────────────────────181+ // Save the origin mask before reusing xF to form an exact +/-1 sign.
179- // Use tmp0 as scratch for |yF/xF| (yF is no longer needed after step 3)182+ AscendC::CompareScalar(mask, tmp0, 0.0f, CMPMODE::EQ, compareNum);
180- AscendC::Div(tmp0, tmp0, tmp1, n); // tmp0 = yF/xF (±Inf when xF=0; handled by blend)183+ AscendC::CompareScalar(xZeroMask, tmp1, 0.0f, CMPMODE::EQ, compareNum);
181- AscendC::Abs(tmp0, tmp0, n); // tmp0 = |yF/xF| ≥ 0184+ AscendC::And(mask.ReinterpretCast<uint16_t>(), mask.ReinterpretCast<uint16_t>(),
182- // Atan: dst(tmp4) ≠ src(tmp0), sharedTmpBuffer = atanTmp185+ xZeroMask.ReinterpretCast<uint16_t>(), (n + 15U) / 16U);
183- AscendC::Atan(tmp4, tmp0, atanTmp, n); // tmp4 = Atan(|yF/xF|) ∈ [0, π/2]186+ AscendC::ShiftRight(tmp1.ReinterpretCast<uint32_t>(), tmp0.ReinterpretCast<uint32_t>(), static_cast<uint32_t>(31),
187+ n);
188+ AscendC::Cast(tmp1, tmp1.ReinterpretCast<int32_t>(), RoundMode::CAST_NONE, n);
189+ AscendC::Muls(tmp1, tmp1, -2.0f, n);
190+ AscendC::Adds(tmp1, tmp1, 1.0f, n);
191+ AscendC::Mul(tmp4, tmp4, tmp1, n);
184 192 
185- // ── Step 5: blend_xne0 = |xF|/(|xF|+ε) → tmp1 ───────────────────────193+ // Select the documented origin result: multiplying NaN by zero would
186- // xF is in tmp1; compute |xF| in-place194+ // leave the 0/0 from the division above as NaN.
187- AscendC::Abs(tmp1, tmp1, n); // tmp1 = |xF|195+ AscendC::Duplicate(tmp0, 0.0f, n);
188- AscendC::Adds(tmp0, tmp1, EPSILON, n); // tmp0 = |xF| + ε196+ AscendC::Select(tmp4, mask, tmp0, tmp4, SELMODE::VSEL_TENSOR_TENSOR_MODE, n);
189- AscendC::Div(tmp1, tmp1, tmp0, n); // tmp1 = blend_xne0
190 197 
191- // ── Step 6: unsigned_out accumulation → tmp0 ──────────────────────────198+ // Cast the float32 result back to the input dtype.
192- // Correct formula: unsigned_out = sign_x_soft * blend*(base_atan − π/2) + π/2
193- // where sign_x_soft = 1 − 2*ind_xlt0 (+1 when x≥0, −1 when x<0)
194- //
195- // Derivation check:
196- // x>0 (ind=0): blend*(base−π/2)+π/2 → base when blend=1 ∈[0,π/2] ✓
197- // x<0 (ind=1): −blend*(base−π/2)+π/2 → π−base when blend=1 ✓
198- // x=0 (blend=0): π/2 ✓
199- //
200- // tmp1=blend_xne0, tmp2=ind_xlt0, tmp4=base_atan
201- AscendC::Muls(tmp0, tmp2, -2.0f, n); // tmp0 = −2*ind_xlt0
202- AscendC::Adds(tmp0, tmp0, 1.0f, n); // tmp0 = sign_x_soft = 1−2*ind_xlt0
203- AscendC::Adds(tmp2, tmp4, -CONST_PI_BY_TWO, n); // tmp2 = base_atan − π/2
204- AscendC::Mul(tmp2, tmp1, tmp2, n); // tmp2 = blend*(base_atan−π/2)
205- AscendC::Mul(tmp0, tmp0, tmp2, n); // tmp0 = sign_x_soft*blend*(base_atan−π/2)
206- AscendC::Adds(tmp0, tmp0, CONST_PI_BY_TWO, n); // tmp0 = unsigned_out
207- 
208- // ── Step 7: out = sign_y * unsigned_out → tmp0 ────────────────────────
209- AscendC::Mul(tmp0, tmp3, tmp0, n);
210- 
211- // ── Step 8: cast float32 result back to T → outLocal ─────────────────
212 if constexpr (AscendC::Std::is_same<T, half>::value || AscendC::Std::is_same<T, bfloat16_t>::value) {199 if constexpr (AscendC::Std::is_same<T, half>::value || AscendC::Std::is_same<T, bfloat16_t>::value) {
213- AscendC::Cast(outLocal, tmp0, RoundMode::CAST_ROUND, n);200+ AscendC::Cast(outLocal, tmp4, RoundMode::CAST_ROUND, n);
214 } else {201 } else {
215- AscendC::Adds(outLocal.template ReinterpretCast<float>(), tmp0, 0.0f, n);202+ AscendC::DataCopy(outLocal.template ReinterpretCast<float>(), tmp4, n);
203+ }
204+ 
205+ if constexpr (AscendC::Std::is_same<T, bfloat16_t>::value) {
206+ // Preserve the NaN result independently of float-to-BF16 conversion.
207+ // Select only copies bits: treating BF16 storage as half here does
208+ // not convert the quiet-NaN encoding or the rounded finite values.
209+ AscendC::Compare(mask, tmp4, tmp4, CMPMODE::EQ, compareNum);
210+ AscendC::Not(mask.ReinterpretCast<uint16_t>(), mask.ReinterpretCast<uint16_t>(), (compareNum + 15U) / 16U);
211+ AscendC::Duplicate(tmp0.ReinterpretCast<uint16_t>(), static_cast<uint16_t>(0x7fc0), n);
212+ AscendC::Select(outLocal.template ReinterpretCast<half>(), mask, tmp0.ReinterpretCast<half>(),
213+ outLocal.template ReinterpretCast<half>(), SELMODE::VSEL_TENSOR_TENSOR_MODE, n);
216 }214 }
217 215 
218 outputQueueY.EnQue<T>(outLocal);216 outputQueueY.EnQue<T>(outLocal);
@@ -12,6 +12,7 @@
12#include <gtest/gtest.h>12#include <gtest/gtest.h>
13#include "../../../op_kernel/atan2_tiling_data.h"13#include "../../../op_kernel/atan2_tiling_data.h"
14#include "../../../op_kernel/atan2_tiling_key.h"14#include "../../../op_kernel/atan2_tiling_key.h"
15+#include "../../../op_host/atan2_tiling_schedule.h"
15#include "tiling_context_faker.h"16#include "tiling_context_faker.h"
16#include "tiling_case_executor.h"17#include "tiling_case_executor.h"
17 18 
@@ -32,30 +33,92 @@ protected:
32 static void TearDownTestCase() { cout << "Atan2Tiling TearDown " << endl; }33 static void TearDownTestCase() { cout << "Atan2Tiling TearDown " << endl; }
33};34};
34 35 
35-TEST_F(Atan2Tiling, ascend910b_test_tiling_fp16_001)36+static void VerifyTiling(uint64_t ubSize, const string& expectTilingData, ge::DataType dtype = ge::DT_FLOAT16)
36{37{
37 optiling::Atan2CompileInfo compileInfo;38 optiling::Atan2CompileInfo compileInfo;
38 39 
39 // atan2 是双输入 (x1, x2),单输出 (y)40 // atan2 是双输入 (x1, x2),单输出 (y)
40- gert::TilingContextPara tilingContextPara(41+ gert::TilingContextPara tilingContextPara("Atan2",
41- "Atan2",42+ {
42- {43+ // Input 0: x1
43- // Input 0: x144+ {{{1, 64, 2, 64}, {1, 64, 2, 64}}, dtype, ge::FORMAT_ND},
44- {{{1, 64, 2, 64}, {1, 64, 2, 64}}, ge::DT_FLOAT16, ge::FORMAT_ND},45+ // Input 1: x2
45- // Input 1: x246+ {{{1, 64, 2, 64}, {1, 64, 2, 64}}, dtype, ge::FORMAT_ND},
46- {{{1, 64, 2, 64}, {1, 64, 2, 64}}, ge::DT_FLOAT16, ge::FORMAT_ND},47+ },
47- },48+ {
48- {49+ // Output 0: y
49- // Output 0: y50+ {{{1, 64, 2, 64}, {1, 64, 2, 64}}, dtype, ge::FORMAT_ND},
50- {{{1, 64, 2, 64}, {1, 64, 2, 64}}, ge::DT_FLOAT16, ge::FORMAT_ND},51+ },
51- },52+ &compileInfo, 64, ubSize);
52- &compileInfo);
53 53 
54 uint64_t expectTilingKey = 0; // ELEMENTWISE_TPL_SCH_MODE_054 uint64_t expectTilingKey = 0; // ELEMENTWISE_TPL_SCH_MODE_0
55 55 
56- string expectTilingData = "128 144 1 1 4080 128 144 0 130560 ";
57- 
58 std::vector<size_t> expectWorkspaces = {16777728};56 std::vector<size_t> expectWorkspaces = {16777728};
59 57 
60 ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData, expectWorkspaces);58 ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData, expectWorkspaces);
61-}59+}
60+ 
61+// FP16 buffers use 64 bytes per element plus 8192 bytes for Select scratch.
62+// Keep both budgets inside a tile interval so SDK-reserved bytes do not make
63+// the expected 64-element alignment depend on an exact capacity boundary.
64+TEST_F(Atan2Tiling, ascend910b_test_tiling_fp16_001)
65+{
66+ VerifyTiling(254U * 1024U, "128 144 1 1 3904 128 144 0 124928 ");
67+}
68+ 
69+TEST_F(Atan2Tiling, ascend910b_test_tiling_fp16_smaller_ub)
70+{
71+ VerifyTiling(126U * 1024U, "128 144 1 1 1856 128 144 0 59392 ");
72+}
73+ 
74+TEST_F(Atan2Tiling, ascend910b_test_tiling_fp32)
75+{
76+ // 1 * 64 * 2 * 64 = 8192 elements, split across 64 vector cores.
77+ VerifyTiling(254U * 1024U, "128 136 1 1 3264 128 136 0 104448 ", ge::DT_FLOAT);
78+}
79+ 
80+TEST_F(Atan2Tiling, ascend910b_test_tiling_bf16)
81+{
82+ VerifyTiling(254U * 1024U, "128 144 1 1 3904 128 144 0 124928 ", ge::DT_BF16);
83+}
84+ 
85+TEST_F(Atan2Tiling, schedule_balances_dma_blocks_and_keeps_tile_budget)
86+{
87+ for (uint32_t bytes : {2U, 4U}) {
88+ for (uint64_t elements : {1U, 17U, 160U, 16384U, 65569U}) {
89+ for (uint32_t cores : {1U, 8U, 64U}) {
90+ Atan2TilingData data{};
91+ uint32_t blocks = 0;
92+ constexpr uint64_t ub = 254U * 1024U;
93+ ASSERT_TRUE(optiling::atan2::BuildSchedule(elements, bytes, ub, cores, data, blocks));
94+ const uint64_t paddedElements = ((elements * bytes + 31) / 32) * 32 / bytes;
95+ const uint64_t covered = data.tailBlockNum * data.bigCoreDataNum +
96+ (blocks - data.tailBlockNum) * data.smallCoreDataNum;
97+ EXPECT_EQ(covered, paddedElements);
98+ EXPECT_LE(blocks, cores);
99+ EXPECT_EQ(data.bigCoreDataNum - data.smallCoreDataNum, 32 / bytes);
100+ EXPECT_EQ((data.finalSmallTileNum - 1) * data.tileDataNum + data.smallTailDataNum,
101+ data.smallCoreDataNum);
102+ EXPECT_EQ((data.finalBigTileNum - 1) * data.tileDataNum + data.bigTailDataNum, data.bigCoreDataNum);
103+ EXPECT_EQ(data.tileDataNum % 64, 0);
104+ const uint64_t usedBytes = data.tileDataNum * (6 * bytes + 13 * sizeof(float)) + 8192;
105+ EXPECT_LE(usedBytes, ub);
106+ EXPECT_GT(usedBytes + 64 * (6 * bytes + 13 * sizeof(float)), ub);
107+ }
108+ }
109+ }
110+}
111+ 
112+TEST_F(Atan2Tiling, schedule_rejects_insufficient_resources_and_kernel_offset_overflow)
113+{
114+ Atan2TilingData data{};
115+ uint32_t blocks = 0;
116+ EXPECT_FALSE(optiling::atan2::BuildSchedule(64, 4, 8192 + 64 * 76 - 1, 64, data, blocks));
117+ ASSERT_TRUE(optiling::atan2::BuildSchedule(64, 4, 8192 + 64 * 76, 64, data, blocks));
118+ EXPECT_EQ(data.tileDataNum, 64);
119+ EXPECT_EQ(data.atanTmpSize, 2048U);
120+ EXPECT_FALSE(optiling::atan2::BuildSchedule(64, 4, 256 * 1024, 0, data, blocks));
121+ EXPECT_FALSE(optiling::atan2::BuildSchedule(64, 1, 256 * 1024, 64, data, blocks));
122+ EXPECT_FALSE(optiling::atan2::BuildSchedule(0, 4, 256 * 1024, 64, data, blocks));
123+ EXPECT_FALSE(optiling::atan2::BuildSchedule(1ULL << 32, 4, 256 * 1024, 64, data, blocks));
124+}
@@ -8,9 +8,12 @@
8# See LICENSE in the root of the software repository for the full text of the License.8# See LICENSE in the root of the software repository for the full text of the License.
9# ----------------------------------------------------------------------------9# ----------------------------------------------------------------------------
10 10 
11+set_property(SOURCE "${CMAKE_CURRENT_LIST_DIR}/test_atan2.cpp" APPEND PROPERTY
12+ COMPILE_DEFINITIONS "ATAN2_TEST_DATA_DIR=\"${CMAKE_CURRENT_LIST_DIR}/atan2_data\"")
13+ 
11if (UT_TEST_ALL OR OP_KERNEL_UT)14if (UT_TEST_ALL OR OP_KERNEL_UT)
12 set(atan2_tiling_files15 set(atan2_tiling_files
13 ${CMAKE_CURRENT_SOURCE_DIR}/../../../op_host/atan2_tiling.cpp16 ${CMAKE_CURRENT_SOURCE_DIR}/../../../op_host/atan2_tiling.cpp
14 )17 )
15 AddOpTestCase(atan2 "ascend910b" "-DDTYPE_X1=half -DDTYPE_X2=half -DDTYPE_Y=half" "${atan2_tiling_files}")18 AddOpTestCase(atan2 "ascend910b" "-DDTYPE_X1=half -DDTYPE_X2=half -DDTYPE_Y=half" "${atan2_tiling_files}")
16-endif()19+endif()
@@ -13,24 +13,23 @@
13import sys13import sys
14import os14import os
15import numpy as np15import numpy as np
16-import tensorflow as tf
17 16 
18 17 
19def parse_str_to_shape_list(shape_str):18def parse_str_to_shape_list(shape_str):
20- shape_str = shape_str.strip('(').strip(')')19+ shape_str = shape_str.strip("(").strip(")")
21 shape_list = [int(x) for x in shape_str.split(",")]20 shape_list = [int(x) for x in shape_str.split(",")]
22 return np.array(shape_list)21 return np.array(shape_list)
23 22 
24 23 
25def gen_data_and_golden(shape_str, d_type="float32"):24def gen_data_and_golden(shape_str, d_type="float32"):
26- d_type_dict = {25+ d_type_dict = {"float32": np.float32, "float16": np.float16}
27- "float32": np.float32,26+ if d_type == "bfloat16":
28- "float16": np.float16,27+ import tensorflow as tf
29- "bfloat16": tf.bfloat16.as_numpy_dtype28+ 
30- }29+ d_type_dict["bfloat16"] = tf.bfloat16.as_numpy_dtype
31 np_type = d_type_dict[d_type]30 np_type = d_type_dict[d_type]
32 shape = parse_str_to_shape_list(shape_str)31 shape = parse_str_to_shape_list(shape_str)
33- 32+ 
34 # 为了覆盖 atan2 的四个象限,数据范围取 [-100.0, 100.0]33 # 为了覆盖 atan2 的四个象限,数据范围取 [-100.0, 100.0]
35 low, high = -100.0, 100.034 low, high = -100.0, 100.0
36 35 
@@ -38,7 +37,7 @@ def gen_data_and_golden(shape_str, d_type="float32"):
38 # 先用 float32 生成再转 bfloat1637 # 先用 float32 生成再转 bfloat16
39 input_x1_f32 = np.random.uniform(low, high, size=shape).astype(np.float32)38 input_x1_f32 = np.random.uniform(low, high, size=shape).astype(np.float32)
40 input_x2_f32 = np.random.uniform(low, high, size=shape).astype(np.float32)39 input_x2_f32 = np.random.uniform(low, high, size=shape).astype(np.float32)
41- 40+ 
42 input_x1 = input_x1_f32.astype(np_type)41 input_x1 = input_x1_f32.astype(np_type)
43 input_x2 = input_x2_f32.astype(np_type)42 input_x2 = input_x2_f32.astype(np_type)
44 golden = np.arctan2(input_x1_f32, input_x2_f32).astype(np_type)43 golden = np.arctan2(input_x1_f32, input_x2_f32).astype(np_type)
@@ -46,7 +45,9 @@ def gen_data_and_golden(shape_str, d_type="float32"):
46 # 先生成 float16 数据,用 float32 计算 atan2 保持精度,再转回 float1645 # 先生成 float16 数据,用 float32 计算 atan2 保持精度,再转回 float16
47 input_x1 = np.random.uniform(low, high, size=shape).astype(np.float16)46 input_x1 = np.random.uniform(low, high, size=shape).astype(np.float16)
48 input_x2 = np.random.uniform(low, high, size=shape).astype(np.float16)47 input_x2 = np.random.uniform(low, high, size=shape).astype(np.float16)
49- golden = np.arctan2(input_x1.astype(np.float32), input_x2.astype(np.float32)).astype(np.float16)48+ golden = np.arctan2(
49+ input_x1.astype(np.float32), input_x2.astype(np.float32)
50+ ).astype(np.float16)
50 else: # float3251 else: # float32
51 input_x1 = np.random.uniform(low, high, size=shape).astype(np.float32)52 input_x1 = np.random.uniform(low, high, size=shape).astype(np.float32)
52 input_x2 = np.random.uniform(low, high, size=shape).astype(np.float32)53 input_x2 = np.random.uniform(low, high, size=shape).astype(np.float32)
@@ -64,4 +65,4 @@ if __name__ == "__main__":
64 exit(1)65 exit(1)
65 # 清理bin文件66 # 清理bin文件
66 os.system("rm -rf *.bin")67 os.system("rm -rf *.bin")
67- gen_data_and_golden(sys.argv[1], sys.argv[2])68+ gen_data_and_golden(sys.argv[1], sys.argv[2])
@@ -13,6 +13,10 @@
13#include <iostream>13#include <iostream>
14#include <string>14#include <string>
15#include <cstdint>15#include <cstdint>
16+#include <cmath>
17+#include <limits>
18+#include <filesystem>
19+#include <type_traits>
16#include "gtest/gtest.h"20#include "gtest/gtest.h"
17#include "tikicpulib.h"21#include "tikicpulib.h"
18#include "data_utils.h"22#include "data_utils.h"
@@ -21,15 +25,126 @@
21 25 
22using namespace std;26using namespace std;
23 27 
28+template <typename T>
29+__global__ __aicore__ void Atan2BoundaryKernel(GM_ADDR x1, GM_ADDR x2, GM_ADDR y, GM_ADDR tiling)
30+{
31+ REGISTER_TILING_DEFAULT(Atan2TilingData);
32+ GET_TILING_DATA_WITH_STRUCT(Atan2TilingData, tilingData, tiling);
33+ NsAtan2::Atan2<T> op;
34+ op.Init(x1, x2, y, &tilingData);
35+ op.Process();
36+}
37+ 
38+template <typename T>
39+void CheckZeroBoundaries(float tolerance, uint32_t dataCount = 160, uint32_t inputOffset = 0, uint32_t tileSize = 64)
40+{
41+ const float nan = std::numeric_limits<float>::quiet_NaN();
42+ const std::array<std::array<float, 2>, 36> inputs = {
43+ {{0.0f, -1.0f}, {-0.0f, -1.0f}, {0.0f, 1.0f}, {-0.0f, 1.0f}, {1.0f, 0.0f}, {-1.0f, 0.0f},
44+ {1.0f, -0.0f}, {-1.0f, -0.0f}, {0.0f, 0.0f}, {-0.0f, 0.0f}, {0.0f, -0.0f}, {-0.0f, -0.0f},
45+ {1.0f, 1.0f}, {-1.0f, 1.0f}, {1.0f, -1.0f}, {-1.0f, -1.0f}, {2.0f, 3.0f}, {-2.0f, 3.0f},
46+ {2.0f, -3.0f}, {-2.0f, -3.0f}, {1e-36f, 2e-36f}, {-1e-36f, 2e-36f}, {1e-36f, -2e-36f}, {-1e-36f, -2e-36f},
47+ {nan, 0.0f}, {0.0f, nan}, {nan, 1.0f}, {1.0f, nan}, {-nan, 0.0f}, {0.0f, -nan},
48+ {-nan, 1.0f}, {1.0f, -nan}, {nan, nan}, {-nan, nan}, {nan, -nan}, {-nan, -nan}}};
49+ const size_t bytes = dataCount * sizeof(T);
50+ T* x1 = reinterpret_cast<T*>(AscendC::GmAlloc(bytes));
51+ T* x2 = reinterpret_cast<T*>(AscendC::GmAlloc(bytes));
52+ T* output = reinterpret_cast<T*>(AscendC::GmAlloc(bytes));
53+ ASSERT_NE(x1, nullptr);
54+ ASSERT_NE(x2, nullptr);
55+ ASSERT_NE(output, nullptr);
56+ Atan2TilingData tiling{};
57+ tiling.smallCoreDataNum = dataCount;
58+ tiling.bigCoreDataNum = dataCount;
59+ tiling.finalBigTileNum = (dataCount + tileSize - 1) / tileSize;
60+ tiling.finalSmallTileNum = (dataCount + tileSize - 1) / tileSize;
61+ tiling.tileDataNum = tileSize;
62+ tiling.smallTailDataNum = dataCount % tileSize;
63+ tiling.bigTailDataNum = dataCount % tileSize;
64+ tiling.tailBlockNum = 1;
65+ tiling.atanTmpSize = 8 * tileSize * sizeof(float);
66+ auto* tilingBuffer = reinterpret_cast<Atan2TilingData*>(AscendC::GmAlloc(sizeof(tiling)));
67+ ASSERT_NE(tilingBuffer, nullptr);
68+ *tilingBuffer = tiling;
69+ for (uint32_t i = 0; i < dataCount; ++i) {
70+ x1[i] = static_cast<T>(inputs[(i + inputOffset) % inputs.size()][0]);
71+ x2[i] = static_cast<T>(inputs[(i + inputOffset) % inputs.size()][1]);
72+ output[i] = static_cast<T>(-123.0f);
73+ }
74+ 
75+ AscendC::SetKernelMode(KernelMode::AIV_MODE);
76+ auto func = Atan2BoundaryKernel<T>;
77+ ICPU_RUN_KF(func, 1, reinterpret_cast<uint8_t*>(x1), reinterpret_cast<uint8_t*>(x2),
78+ reinterpret_cast<uint8_t*>(output), reinterpret_cast<uint8_t*>(tilingBuffer));
79+ for (uint32_t i = 0; i < dataCount; ++i) {
80+ const float numerator = static_cast<float>(x1[i]);
81+ const float denominator = static_cast<float>(x2[i]);
82+ // The operator documents a canonical zero for all origins.
83+ const float expected = (numerator == 0.0f && denominator == 0.0f) ? 0.0f : std::atan2(numerator, denominator);
84+ const float actual = static_cast<float>(output[i]);
85+ SCOPED_TRACE(i);
86+ if (std::isnan(expected)) {
87+ EXPECT_TRUE(std::isnan(actual));
88+ } else {
89+ EXPECT_NEAR(actual, expected, tolerance);
90+ if (expected == 0.0f) {
91+ EXPECT_EQ(std::signbit(actual), std::signbit(expected));
92+ }
93+ }
94+ }
95+ AscendC::GmFree(x1);
96+ AscendC::GmFree(x2);
97+ AscendC::GmFree(output);
98+ AscendC::GmFree(tilingBuffer);
99+}
100+ 
101+template <typename T>
102+void CheckDefaultUbTile(float tolerance)
103+{
104+ // Match the host budget against the active CPU model, including the
105+ // Select API reservation, then exercise a full tile and a short tail.
106+ constexpr uint32_t ubFactor = std::is_same<T, float>::value ? 19 : 16;
107+ constexpr uint32_t tileSize = ((AscendC::TOTAL_UB_SIZE - 8 * 1024) / sizeof(float) / ubFactor / 64) * 64;
108+ CheckZeroBoundaries<T>(tolerance, tileSize + 32, 0, tileSize);
109+}
110+ 
111+TEST(Atan2ZeroBoundaryTest, float32)
112+{
113+ CheckZeroBoundaries<float>(1e-4f);
114+ CheckZeroBoundaries<float>(1e-4f, 8);
115+ CheckZeroBoundaries<float>(1e-4f, 8, 24);
116+ CheckDefaultUbTile<float>(1e-4f);
117+}
118+ 
119+TEST(Atan2ZeroBoundaryTest, float16)
120+{
121+ CheckZeroBoundaries<half>(5e-3f);
122+ CheckZeroBoundaries<half>(5e-3f, 16);
123+ CheckZeroBoundaries<half>(5e-3f, 16, 24);
124+ CheckDefaultUbTile<half>(5e-3f);
125+}
126+ 
127+TEST(Atan2ZeroBoundaryTest, bfloat16)
128+{
129+ CheckZeroBoundaries<bfloat16_t>(2e-2f);
130+ CheckZeroBoundaries<bfloat16_t>(2e-2f, 16);
131+ CheckZeroBoundaries<bfloat16_t>(2e-2f, 16, 24);
132+ CheckDefaultUbTile<bfloat16_t>(2e-2f);
133+}
134+ 
24class Atan2Test : public testing::Test {135class Atan2Test : public testing::Test {
25protected:136protected:
26 static void SetUpTestCase()137 static void SetUpTestCase()
27 {138 {
28 std::cout << "atan2_test SetUp" << std::endl;139 std::cout << "atan2_test SetUp" << std::endl;
29- // 修正路径:从 build 目录直接访问 experimental 目录140+ // The build may shorten __FILE__. CMake supplies the fixture path
30- const string cmd = "cp -rf ../experimental/math/atan2/tests/ut/op_kernel/atan2_data ./";141+ // independently of both compiler path rewriting and the working directory.
31- system(cmd.c_str());142+ const std::filesystem::path sourceData(ATAN2_TEST_DATA_DIR);
32- system("chmod -R 755 ./atan2_data/");143+ std::error_code error;
144+ std::filesystem::copy(
145+ sourceData, "atan2_data",
146+ std::filesystem::copy_options::recursive | std::filesystem::copy_options::overwrite_existing, error);
147+ ASSERT_FALSE(error) << "Cannot copy Atan2 fixtures from " << sourceData << ": " << error.message();
33 }148 }
34 static void TearDownTestCase() { std::cout << "atan2_test TearDown" << std::endl; }149 static void TearDownTestCase() { std::cout << "atan2_test TearDown" << std::endl; }
35};150};
@@ -44,44 +159,50 @@ TEST_F(Atan2Test, test_case_float16_1)
44{159{
45 uint32_t blockDim = 40;160 uint32_t blockDim = 40;
46 // 调用 python 生成测试数据161 // 调用 python 生成测试数据
47- system("cd ./atan2_data/ && python3 gen_data.py '(128, 64)' 'float16'");162+ ASSERT_EQ(system("cd ./atan2_data/ && python3 gen_data.py '(128, 64)' 'float16'"), 0);
48 uint32_t dataCount = 128 * 64;163 uint32_t dataCount = 128 * 64;
49 size_t inputByteSize = dataCount * sizeof(half);164 size_t inputByteSize = dataCount * sizeof(half);
50 165 
51 std::string x1_fileName = "./atan2_data/float16_input_x1_t_atan2.bin";166 std::string x1_fileName = "./atan2_data/float16_input_x1_t_atan2.bin";
52 uint8_t* x1 = (uint8_t*)AscendC::GmAlloc(CeilAlign(inputByteSize, 256));167 uint8_t* x1 = (uint8_t*)AscendC::GmAlloc(CeilAlign(inputByteSize, 256));
53- ReadFile(x1_fileName, inputByteSize, x1, inputByteSize);168+ ASSERT_NE(x1, nullptr);
169+ ASSERT_TRUE(ReadFile(x1_fileName, inputByteSize, x1, inputByteSize));
54 170 
55 std::string x2_fileName = "./atan2_data/float16_input_x2_t_atan2.bin";171 std::string x2_fileName = "./atan2_data/float16_input_x2_t_atan2.bin";
56 uint8_t* x2 = (uint8_t*)AscendC::GmAlloc(CeilAlign(inputByteSize, 256));172 uint8_t* x2 = (uint8_t*)AscendC::GmAlloc(CeilAlign(inputByteSize, 256));
57- ReadFile(x2_fileName, inputByteSize, x2, inputByteSize);173+ ASSERT_NE(x2, nullptr);
174+ ASSERT_TRUE(ReadFile(x2_fileName, inputByteSize, x2, inputByteSize));
58 175 
59 size_t outputByteSize = dataCount * sizeof(half);176 size_t outputByteSize = dataCount * sizeof(half);
60 uint8_t* y = (uint8_t*)AscendC::GmAlloc(CeilAlign(outputByteSize, 256));177 uint8_t* y = (uint8_t*)AscendC::GmAlloc(CeilAlign(outputByteSize, 256));
178+ ASSERT_NE(y, nullptr);
61 179 
62 size_t workspaceSize = 16 * 1024 * 1024;180 size_t workspaceSize = 16 * 1024 * 1024;
63 uint8_t* workspace = (uint8_t*)AscendC::GmAlloc(workspaceSize);181 uint8_t* workspace = (uint8_t*)AscendC::GmAlloc(workspaceSize);
64 uint8_t* tiling = (uint8_t*)AscendC::GmAlloc(sizeof(Atan2TilingData));182 uint8_t* tiling = (uint8_t*)AscendC::GmAlloc(sizeof(Atan2TilingData));
183+ ASSERT_NE(workspace, nullptr);
184+ ASSERT_NE(tiling, nullptr);
65 185 
66 Atan2TilingData* tilingData = reinterpret_cast<Atan2TilingData*>(tiling);186 Atan2TilingData* tilingData = reinterpret_cast<Atan2TilingData*>(tiling);
67 187 
68- // 将 Tiling 里的块大小强行缩减,避免把 192KB 的 UB 内存撑爆188+ // Split all 8192 elements into 32 big cores and 8 small cores, using
69- tilingData->smallCoreDataNum = 128;189+ // a small tile to stay within the CPU model's UB capacity.
70- tilingData->bigCoreDataNum = 144;190+ tilingData->smallCoreDataNum = 192;
191+ tilingData->bigCoreDataNum = 208;
71 tilingData->finalBigTileNum = 1;192 tilingData->finalBigTileNum = 1;
72 tilingData->finalSmallTileNum = 1;193 tilingData->finalSmallTileNum = 1;
73- tilingData->tileDataNum = 256; // <-- 缩小:原为 4080194+ tilingData->tileDataNum = 256;
74- tilingData->smallTailDataNum = 128;195+ tilingData->smallTailDataNum = 192;
75- tilingData->bigTailDataNum = 144;196+ tilingData->bigTailDataNum = 208;
76- tilingData->tailBlockNum = 0;197+ tilingData->tailBlockNum = 32;
77- tilingData->atanTmpSize = 8192; // <-- 缩小:原为 130560198+ tilingData->atanTmpSize = 8192;
78 199 
79 AscendC::SetKernelMode(KernelMode::AIV_MODE);200 AscendC::SetKernelMode(KernelMode::AIV_MODE);
80 auto func = atan2<ELEMENTWISE_TPL_SCH_MODE_0>;201 auto func = atan2<ELEMENTWISE_TPL_SCH_MODE_0>;
81 ICPU_RUN_KF(func, blockDim, x1, x2, y, workspace, (uint8_t*)(tilingData));202 ICPU_RUN_KF(func, blockDim, x1, x2, y, workspace, (uint8_t*)(tilingData));
82 203 
83 std::string fileName = "./atan2_data/float16_output_t_atan2.bin";204 std::string fileName = "./atan2_data/float16_output_t_atan2.bin";
84- WriteFile(fileName, y, outputByteSize);205+ ASSERT_TRUE(WriteFile(fileName, y, outputByteSize));
85 206 
86 AscendC::GmFree((void*)(x1));207 AscendC::GmFree((void*)(x1));
87 AscendC::GmFree((void*)(x2));208 AscendC::GmFree((void*)(x2));
@@ -90,5 +211,5 @@ TEST_F(Atan2Test, test_case_float16_1)
90 AscendC::GmFree((void*)tiling);211 AscendC::GmFree((void*)tiling);
91 212 
92 // 调用 python 进行精度比对213 // 调用 python 进行精度比对
93- system("cd ./atan2_data/ && python3 compare_data.py 'float16'");214+ ASSERT_EQ(system("cd ./atan2_data/ && python3 compare_data.py 'float16'"), 0);
94-}215+}