Merged
fix(foreach_acos): update doc constraints, harden tiling checks, and fix UT precision #12980
wuyanAdd on 24 days ago
fix(foreach_acos): update doc constraints, harden tiling checks, and fix UT precision #12980
Merged
wuyanAdd on 24 days ago
Total: 11 files changed+210-83
@@ -55,7 +55,7 @@
55 <tr>55 <tr>
56 <td>y</td>56 <td>y</td>
57 <td>输出</td>57 <td>输出</td>
58- <td>表示进行反余弦运算的输出张量列表,对应公式中的`y`。数据类型和数据格式与入参`x`的数据类型和数据格式一致,shape size大于等于入参`x`的shape size。该参数中所有Tensor的数据类型保持一致。</td>58+ <td>表示进行反余弦运算的输出张量列表,对应公式中的`y`。数据类型和数据格式与入参`x`的数据类型和数据格式一致,shape(维度及每一维长度)与入参`x`对应位置的Tensor一致。该参数中所有Tensor的数据类型保持一致。</td>
59 <td>FLOAT32、FLOAT16、BFLOAT16</td>59 <td>FLOAT32、FLOAT16、BFLOAT16</td>
60 <td>ND</td>60 <td>ND</td>
61 </tr>61 </tr>
@@ -63,10 +63,6 @@
63 63 
64- <term>Kirin X90处理器系列产品</term>、<term>Kirin 9030处理器系列产品</term>:不支持BFLOAT16。64- <term>Kirin X90处理器系列产品</term>、<term>Kirin 9030处理器系列产品</term>:不支持BFLOAT16。
65 65 
66-## 约束说明
67- 
68-- 输出不支持非连续Tensor。
69- 
70## 调用说明66## 调用说明
71 67 
72| 调用方式 | 样例代码 | 说明 |68| 调用方式 | 样例代码 | 说明 |
@@ -42,7 +42,7 @@
42```Cpp42```Cpp
43aclnnStatus aclnnForeachAcosGetWorkspaceSize(43aclnnStatus aclnnForeachAcosGetWorkspaceSize(
44 const aclTensorList *x,44 const aclTensorList *x,
45- const aclTensorList *out,45+ aclTensorList *out,
46 uint64_t *workspaceSize,46 uint64_t *workspaceSize,
47 aclOpExecutor **executor)47 aclOpExecutor **executor)
48```48```
@@ -95,7 +95,7 @@ aclnnStatus aclnnForeachAcos(
95 <td>out(aclTensorList*)</td>95 <td>out(aclTensorList*)</td>
96 <td>输出</td>96 <td>输出</td>
97 <td>表示进行反余弦运算的输出张量列表,对应公式中的`y`。</td>97 <td>表示进行反余弦运算的输出张量列表,对应公式中的`y`。</td>
98- <td><ul><li>支持空Tensor。</li><li>该参数中所有Tensor的数据类型保持一致。</li><li>数据类型和数据格式与入参`x`的数据类型和数据格式一致,shape size大于等于入参`x`的shape size。</li></ul></td>98+ <td><ul><li>支持空Tensor。</li><li>该参数中所有Tensor的数据类型保持一致。</li><li>数据类型和数据格式与入参`x`的数据类型和数据格式一致,shape(维度及每一维长度)与入参`x`对应位置的Tensor一致。</li></ul></td>
99 <td>BFLOAT16、FLOAT16、FLOAT32</td>99 <td>BFLOAT16、FLOAT16、FLOAT32</td>
100 <td>ND</td>100 <td>ND</td>
101 <td>0-8</td>101 <td>0-8</td>
@@ -149,24 +149,28 @@ aclnnStatus aclnnForeachAcos(
149 <td>传入的x或out是空指针。</td>149 <td>传入的x或out是空指针。</td>
150 </tr>150 </tr>
151 <tr>151 <tr>
152- <td rowspan="2">ACLNN_ERR_PARAM_INVALID</td>152+ <td rowspan="7">ACLNN_ERR_PARAM_INVALID</td>
153- <td rowspan="2">161002</td>153+ <td rowspan="7">161002</td>
154 <td>x或out的数据类型不在支持的范围之内。</td>154 <td>x或out的数据类型不在支持的范围之内。</td>
155 </tr>155 </tr>
156 <tr>156 <tr>
157 <td>x和out的数据类型不一致。</td>157 <td>x和out的数据类型不一致。</td>
158 </tr>158 </tr>
159- <tr>
160- <td rowspan="3">ACLNN_ERR_INNER_TILING_ERROR</td>
161- <td rowspan="3">561002</td>
162- <td>x与out的shape不满足约束。</td>
163- </tr>
164 <tr>159 <tr>
165 <td>x或out中的Tensor的数据类型不一致。</td>160 <td>x或out中的Tensor的数据类型不一致。</td>
166 </tr>161 </tr>
162+ <tr>
163+ <td>x与out的Tensor数量不一致。</td>
164+ </tr>
167 <tr>165 <tr>
168 <td>x或out中的Tensor维度超过8维。</td>166 <td>x或out中的Tensor维度超过8维。</td>
169 </tr>167 </tr>
168+ <tr>
169+ <td>x与out中对应位置的Tensor的shape不一致。</td>
170+ </tr>
171+ <tr>
172+ <td>x或out的数据格式不在支持的范围之内。</td>
173+ </tr>
170 </tbody></table>174 </tbody></table>
171 175 
172## aclnnForeachAcos176## aclnnForeachAcos
@@ -95,7 +95,7 @@ static ge::graphStatus ForeachAcosTilingFunc(gert::TilingContext* context)
95 OP_CHECK_NULL_WITH_CONTEXT(context, idxInstanceInfoPtr);95 OP_CHECK_NULL_WITH_CONTEXT(context, idxInstanceInfoPtr);
96 uint64_t tensorNum = idxInstanceInfoPtr->GetInstanceNum();96 uint64_t tensorNum = idxInstanceInfoPtr->GetInstanceNum();
97 97 
98- OP_CHECK_IF((static_cast<int32_t>(tensorNum) > MAX_TENSOR_NUM_FOREACH_ACOS),98+ OP_CHECK_IF((tensorNum > static_cast<uint64_t>(MAX_TENSOR_NUM_FOREACH_ACOS)),
99 OP_LOGE(context, "tensorNum %lu exceeds MAX_TENSOR_NUM_FOREACH_ACOS %d", tensorNum,99 OP_LOGE(context, "tensorNum %lu exceeds MAX_TENSOR_NUM_FOREACH_ACOS %d", tensorNum,
100 MAX_TENSOR_NUM_FOREACH_ACOS),100 MAX_TENSOR_NUM_FOREACH_ACOS),
101 return ge::GRAPH_FAILED);101 return ge::GRAPH_FAILED);
@@ -111,17 +111,30 @@ static ge::graphStatus ForeachAcosTilingFunc(gert::TilingContext* context)
111 OP_CHECK_NULL_WITH_CONTEXT(context, idxTensorShapePtr);111 OP_CHECK_NULL_WITH_CONTEXT(context, idxTensorShapePtr);
112 auto idxTensorShape = idxTensorShapePtr->GetStorageShape();112 auto idxTensorShape = idxTensorShapePtr->GetStorageShape();
113 int64_t numel = idxTensorShape.GetShapeSize();113 int64_t numel = idxTensorShape.GetShapeSize();
114+ OP_CHECK_IF((numel < 0 || totalElements > INT64_MAX - numel),
115+ OP_LOGE(context, "totalElements of %luth tensor is overflow when accumulating ", i),
116+ return ge::GRAPH_FAILED);
114 totalElements += numel;117 totalElements += numel;
115 tilingData->prefixSums[i + 1] = totalElements;118 tilingData->prefixSums[i + 1] = totalElements;
116 119 
117- // Get dtype from first tensor (all tensors must have same dtype)120+ // All tensors must have the same dtype; validate cross-instance consistency.
121+ auto idxTensorDtypePtr = context->GetDynamicInputDesc(INPUT_IDX, i);
122+ OP_CHECK_NULL_WITH_CONTEXT(context, idxTensorDtypePtr);
123+ ge::DataType curDtype = idxTensorDtypePtr->GetDataType();
118 if (i == 0) {124 if (i == 0) {
119- auto idxTensorDtypePtr = context->GetDynamicInputDesc(INPUT_IDX, i);125+ dataType = curDtype;
120- OP_CHECK_NULL_WITH_CONTEXT(context, idxTensorDtypePtr);126+ } else if (curDtype != dataType) {
121- dataType = idxTensorDtypePtr->GetDataType();127+ OP_LOGE(context, "dtype of %luth tensor is inconsistent with the first tensor's dtype", i);
128+ return ge::GRAPH_FAILED;
122 }129 }
123 }130 }
124 131 
132+ // Guard the rounding arithmetic below: totalElements + MIN_PER_CORE_ELEMENTS - 1 and
133+ // totalElements + needCoreNum - 1 (needCoreNum <= maxCoreNum < MIN_PER_CORE_ELEMENTS)
134+ // must not overflow int64.
135+ OP_CHECK_IF((totalElements > INT64_MAX - (MIN_PER_CORE_ELEMENTS - 1)),
136+ OP_LOGE(context, "totalElements %ld exceeds safe upper bound", totalElements), return ge::GRAPH_FAILED);
137+ 
125 // 4. Compute needCoreNum first (MDE Section 2.1):138 // 4. Compute needCoreNum first (MDE Section 2.1):
126 // needCoreNum = min(maxCoreNum, max(1, ceil(totalElements / MIN_PER_CORE_ELEMENTS)))139 // needCoreNum = min(maxCoreNum, max(1, ceil(totalElements / MIN_PER_CORE_ELEMENTS)))
127 int32_t needCoreNum = (totalElements == 0) ?140 int32_t needCoreNum = (totalElements == 0) ?
@@ -13,16 +13,18 @@
13/*!13/*!
14 * \file foreach_acos_simt.h14 * \file foreach_acos_simt.h
15 * \brief SIMT kernel implementation for foreach_acos operator.15 * \brief SIMT kernel implementation for foreach_acos operator.
16- * Uses per-core continuous allocation, prefix-sum binary search for tensor mapping,16+ * Follows the unified foreach tiling framework: Process partitions the
17- * and type promotion (half/bf16 -> float -> acosf -> half/bf16) for precision.17+ * flattened element range per core (GetBlockIdx() * perCoreElements), and
18+ * each tensor's intersected segment is computed by a SIMT VF within the
19+ * per-core chunk, with type promotion (half/bf16 -> float -> acosf -> back).
18 */20 */
19 21 
20#ifndef FOREACH_ACOS_SIMT_H22#ifndef FOREACH_ACOS_SIMT_H
21#define FOREACH_ACOS_SIMT_H23#define FOREACH_ACOS_SIMT_H
22 24 
23#include "kernel_operator.h"25#include "kernel_operator.h"
26+#include "kernel_operator_list_tensor_intf.h"
24#include "kernel_tiling/kernel_tiling.h"27#include "kernel_tiling/kernel_tiling.h"
25-#include "simt_api/asc_simt.h"
26#include "simt_api/math_functions.h"28#include "simt_api/math_functions.h"
27#include "simt_api/asc_fp16.h"29#include "simt_api/asc_fp16.h"
28#include "simt_api/asc_bf16.h"30#include "simt_api/asc_bf16.h"
@@ -33,7 +35,7 @@ namespace NsForeachAcos {
33 35 
34using namespace AscendC;36using namespace AscendC;
35 37 
36-// Fixed thread count per MDE Section 2.2: 512 for balanced register pressure and parallelism38+// Fixed thread count per core
37static constexpr uint32_t THREAD_NUM = 512;39static constexpr uint32_t THREAD_NUM = 512;
38 40 
39// ========== Type-specialized acos computation with type promotion ==========41// ========== Type-specialized acos computation with type promotion ==========
@@ -44,7 +46,7 @@ struct AcosCompute {
44 __simt_callee__ static inline T Compute(T val) { return acosf(val); }46 __simt_callee__ static inline T Compute(T val) { return acosf(val); }
45};47};
46 48 
47-// float16 version: half -> float -> acosf -> float -> half (MDE Section 5.5)49+// float16 version: half -> float -> acosf -> float -> half
48template <>50template <>
49struct AcosCompute<half> {51struct AcosCompute<half> {
50 __simt_callee__ static inline half Compute(half val)52 __simt_callee__ static inline half Compute(half val)
@@ -55,7 +57,7 @@ struct AcosCompute<half> {
55 }57 }
56};58};
57 59 
58-// bfloat16 version: bf16 -> float -> acosf -> float -> bf16 (MDE Section 5.5)60+// bfloat16 version: bf16 -> float -> acosf -> float -> bf16
59template <>61template <>
60struct AcosCompute<bfloat16_t> {62struct AcosCompute<bfloat16_t> {
61 __simt_callee__ static inline bfloat16_t Compute(bfloat16_t val)63 __simt_callee__ static inline bfloat16_t Compute(bfloat16_t val)
@@ -66,64 +68,62 @@ struct AcosCompute<bfloat16_t> {
66 }68 }
67};69};
68 70 
69-// ========== Helper: get tensor address from ListTensorDesc ==========71+// ========== SIMT VF kernel: acos on a contiguous segment of one tensor ==========
70 72 
71template <typename T>73template <typename T>
72-__simt_callee__ inline __gm__ T* SimtGetTensorAddr(GM_ADDR tensorListPtr, int64_t idx)74+__simt_vf__ __aicore__ LAUNCH_BOUND(THREAD_NUM) inline void ForeachAcosSimtKernel(int64_t localStart, int64_t localEnd,
75+ __gm__ T* inputPtr,
76+ __gm__ T* outputPtr)
73{77{
74- __gm__ uint64_t* dataAddr = reinterpret_cast<__gm__ uint64_t*>(tensorListPtr);78+ for (int64_t localIdx = localStart + static_cast<int64_t>(Simt::GetThreadIdx()); localIdx < localEnd;
75- uint64_t tensorPtrOffset = *dataAddr;79+ localIdx += static_cast<int64_t>(Simt::GetThreadNum())) {
76- __gm__ uint64_t* tensorPtr = dataAddr + (tensorPtrOffset >> 3);
77- return reinterpret_cast<__gm__ T*>(*(tensorPtr + idx));
78-}
79- 
80-// ========== Prefix-sum binary search O(log N) (MDE Section 5.4) ==========
81- 
82-__simt_callee__ inline int32_t PrefixSumUpperBound(__gm__ const int64_t* prefixSums, int32_t n, int64_t target)
83-{
84- int32_t lo = 0;
85- int32_t hi = n;
86- while (lo < hi) {
87- int32_t mid = lo + (hi - lo) / 2;
88- if (prefixSums[mid] <= target) {
89- lo = mid + 1;
90- } else {
91- hi = mid;
92- }
93- }
94- // prefixSums[lo - 1] <= target < prefixSums[lo]
95- return lo - 1;
96-}
97- 
98-// ========== SIMT VF kernel: per-core continuous acos on tensor list (MDE Section 5.2) ==========
99- 
100-template <typename T>
101-__simt_vf__ __aicore__ LAUNCH_BOUND(THREAD_NUM) inline void ForeachAcosSimtKernel(int64_t elementCount,
102- int32_t tensorIdx, GM_ADDR xList,
103- GM_ADDR yList)
104-{
105- __gm__ T* inputPtr = SimtGetTensorAddr<T>(xList, tensorIdx);
106- __gm__ T* outputPtr = SimtGetTensorAddr<T>(yList, tensorIdx);
107- for (int64_t localIdx = static_cast<int64_t>(blockIdx.x * blockDim.x + threadIdx.x); localIdx < elementCount;
108- localIdx += static_cast<int64_t>(blockDim.x * gridDim.x)) {
109 T val = inputPtr[localIdx];80 T val = inputPtr[localIdx];
110 T result = AcosCompute<T>::Compute(val);81 T result = AcosCompute<T>::Compute(val);
111 outputPtr[localIdx] = result;82 outputPtr[localIdx] = result;
112 }83 }
113}84}
114 85 
115-// ========== Process entry function (MDE Section 5.6) ==========86+// ========== Process entry function ==========
116 87 
117template <typename T>88template <typename T>
118__aicore__ inline void Process(GM_ADDR workspace, const ForeachAcosTilingData* tilingData, GM_ADDR inputList,89__aicore__ inline void Process(GM_ADDR workspace, const ForeachAcosTilingData* tilingData, GM_ADDR inputList,
119 GM_ADDR outputList)90 GM_ADDR outputList)
120{91{
92+ // Per-core contiguous chunk over the flattened element range:
93+ // [coreStart, coreEnd) = [blockIdx * perCoreElements, min(totalElements, ...))
94+ int64_t blockIdx = static_cast<int64_t>(GetBlockIdx());
95+ int64_t coreStart = blockIdx * tilingData->perCoreElements;
96+ int64_t coreEnd = coreStart + tilingData->perCoreElements;
97+ if (coreEnd > tilingData->totalElements) {
98+ coreEnd = tilingData->totalElements;
99+ }
100+ // Empty core: nothing to compute
101+ if (coreStart >= tilingData->totalElements) {
102+ return;
103+ }
104+ 
105+ ListTensorDesc inputListDesc(reinterpret_cast<__gm__ void*>(inputList));
106+ ListTensorDesc outputListDesc(reinterpret_cast<__gm__ void*>(outputList));
107+ 
121 for (int32_t tensorIdx = 0; tensorIdx < tilingData->tensorNum; tensorIdx++) {108 for (int32_t tensorIdx = 0; tensorIdx < tilingData->tensorNum; tensorIdx++) {
122- int64_t elementCount = tilingData->prefixSums[tensorIdx + 1] - tilingData->prefixSums[tensorIdx];109+ int64_t tensorStart = tilingData->prefixSums[tensorIdx];
123- if (elementCount <= 0) {110+ int64_t tensorEnd = tilingData->prefixSums[tensorIdx + 1];
111+ 
112+ // Intersection of [coreStart, coreEnd) and [tensorStart, tensorEnd)
113+ int64_t segStart = (coreStart > tensorStart) ? coreStart : tensorStart;
114+ int64_t segEnd = (coreEnd < tensorEnd) ? coreEnd : tensorEnd;
115+ if (segStart >= segEnd) {
124 continue;116 continue;
125 }117 }
126- asc_vf_call<ForeachAcosSimtKernel<T>>(dim3(THREAD_NUM), elementCount, tensorIdx, inputList, outputList);118+ 
119+ // Convert flat segment bounds to tensor-local indices
120+ int64_t localStart = segStart - tensorStart;
121+ int64_t localEnd = segEnd - tensorStart;
122+ 
123+ __gm__ T* inputPtr = inputListDesc.GetDataPtr<T>(tensorIdx);
124+ __gm__ T* outputPtr = outputListDesc.GetDataPtr<T>(tensorIdx);
125+ 
126+ Simt::VF_CALL<ForeachAcosSimtKernel<T>>(Simt::Dim3(THREAD_NUM), localStart, localEnd, inputPtr, outputPtr);
127 }127 }
128}128}
129 129 
@@ -0,0 +1,80 @@
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+"""foreach_acos 三方 Golden — 反余弦。"""
13+ 
14+__spec__ = {
15+ "foreach_acos": "ForeachAcosKernelSpec",
16+ "aclnnForeachAcos": "AclnnForeachAcosSpec",
17+ "torch._foreach_acos": "TorchForeachAcosSpec",
18+}
19+ 
20+import numpy as np
21+import torch
22+ 
23+_TOLERANCE = {
24+ "float16": {"standard": "cross_check", "level": "L1"},
25+ "float32": {"standard": "cross_check", "level": "L1"},
26+ "bfloat16": {"standard": "cross_check", "level": "L1"},
27+}
28+ 
29+ 
30+def _remote_third_party(x, **kwargs):
31+ return list(torch._foreach_acos(x))
32+ 
33+ 
34+class _TorchForeachAcosE2EImpl:
35+ def __call__(self, *args, **kwargs):
36+ return list(torch._foreach_acos(args[0]))
37+ 
38+ 
39+_THIRD_PARTY = {"torch": _remote_third_party}
40+_E2E_THIRD_PARTY = {"torch": _TorchForeachAcosE2EImpl}
41+ 
42+ 
43+class ForeachAcosKernelSpec:
44+ """Kernel 路径 — list[np.ndarray] -> list[np.ndarray]。"""
45+ 
46+ def golden(x, **kwargs):
47+ x_list = [torch.from_numpy(np.ascontiguousarray(t)) for t in x]
48+ result = torch._foreach_acos(x_list)
49+ return [t.numpy() for t in result]
50+ 
51+ third_party = _THIRD_PARTY
52+ tolerance = _TOLERANCE
53+ 
54+ 
55+class AclnnForeachAcosSpec:
56+ """ACLNN 路径 — list[torch.Tensor] -> list[torch.Tensor]。
57+ 
58+ aclnn 签名: AclnnForeachAcosGetWorkspaceSize(x, out, workspaceSize, executor)
59+ """
60+ 
61+ def golden(x, out=None, **kwargs):
62+ result = torch._foreach_acos(x)
63+ if out is not None:
64+ for o, r in zip(out, result):
65+ o.copy_(r)
66+ return list(out)
67+ return list(result)
68+ 
69+ third_party = _THIRD_PARTY
70+ tolerance = _TOLERANCE
71+ 
72+ 
73+class TorchForeachAcosSpec:
74+ """E2E 路径 — list[torch.Tensor] -> list[torch.Tensor]。"""
75+ 
76+ def golden(x, **kwargs):
77+ return list(torch._foreach_acos(x))
78+ 
79+ third_party = _E2E_THIRD_PARTY
80+ tolerance = _TOLERANCE
@@ -0,0 +1,6 @@
1+testcase_name,api_name,tensor_view_shapes,tensor_dtypes,tensor_formats,scalar_dtypes,attributes,output_tensor_indexes,precision_tolerances,absolute_precision,input_data_ranges,scalar_data_ranges,remark
2+aclnnForeachAcos_L0_001,aclnnForeachAcos,"(((6,4,3,2,7,5,6,),(1,4,1,2,1,1,6,),(1,1,1,1,1,5,1,),(1,4,3,2,7,5,6,),(6,1,1,2,1,5,1,),(6,4,3,1,7,1,1,),(6,1,1,1,7,1,1,),(6,1,3,1,7,1,6,),(6,1,1,1,1,5,1,),(1,1,1,2,1,1,1,),(1,1,1,1,7,5,6,),),((6,4,3,2,7,5,6,),(1,4,1,2,1,1,6,),(1,1,1,1,1,5,1,),(1,4,3,2,7,5,6,),(6,1,1,2,1,5,1,),(6,4,3,1,7,1,1,),(6,1,1,1,7,1,1,),(6,1,3,1,7,1,6,),(6,1,1,1,1,5,1,),(1,1,1,2,1,1,1,),(1,1,1,1,7,5,6,),),)","(('float16','float16','float16','float16','float16','float16','float16','float16','float16','float16','float16',),('float16','float16','float16','float16','float16','float16','float16','float16','float16','float16','float16',),)","(('ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND',),('ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND',),)",,,"(1,)",,,"(((-0.0,-0.0,),(-0.0,-0.0,),(-6.103515625e-05,-6.103515625e-05,),(0,0.001,),(-65504.0,65504.0,),(-1000,-10,),(2,10,),(0.01,1,),(0.0,0.0,),(-inf,-inf,),(-0.001,0,),),)",,
3+aclnnForeachAcos_L0_002,aclnnForeachAcos,"(((5,2,5,6,3,6,3,),(5,1,1,6,1,6,3,),(5,1,1,6,1,6,3,),(5,2,5,6,3,6,3,),(5,2,1,6,3,1,1,),(1,1,1,1,1,1,1,),(5,1,5,1,3,6,3,),(5,1,1,6,3,1,1,),(1,2,5,1,1,1,3,),(1,1,1,1,3,6,3,),(1,1,1,1,1,1,1,),(5,2,1,1,1,1,1,),(5,1,1,6,1,1,1,),(5,2,1,6,1,1,3,),(1,1,1,1,1,1,3,),(1,1,5,1,3,6,1,),(5,2,5,6,3,1,3,),(1,2,1,1,1,1,1,),(1,2,5,1,3,1,1,),(5,1,1,1,1,6,3,),),((5,2,5,6,3,6,3,),(5,1,1,6,1,6,3,),(5,1,1,6,1,6,3,),(5,2,5,6,3,6,3,),(5,2,1,6,3,1,1,),(1,1,1,1,1,1,1,),(5,1,5,1,3,6,3,),(5,1,1,6,3,1,1,),(1,2,5,1,1,1,3,),(1,1,1,1,3,6,3,),(1,1,1,1,1,1,1,),(5,2,1,1,1,1,1,),(5,1,1,6,1,1,1,),(5,2,1,6,1,1,3,),(1,1,1,1,1,1,3,),(1,1,5,1,3,6,1,),(5,2,5,6,3,1,3,),(1,2,1,1,1,1,1,),(1,2,5,1,3,1,1,),(5,1,1,1,1,6,3,),),)","(('float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32',),('float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32','float32',),)","(('ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND',),('ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND','ND',),)",,,"(1,)",,,"(((0.001,0.01,),(-10,-2,),(-3.4028235e+38,-3.4028235e+38,),(-0.01,-0.001,),(2,10,),(-2,-1,),(-1.1754943508e-38,-1.1754943508e-38,),(10,1000,),(-0.001,0,),(-3.0517578125e-05,3.0517578125e-05,),(0.01,1,),(inf,inf,),(-2,-1,),(-1,1,),(-2,-1,),(3.4028235e+38,3.4028235e+38,),(0.001,0.01,),(-2,-1,),(-inf,-inf,),(-1000,-10,),),)",,
4+aclnnForeachAcos_L0_003,aclnnForeachAcos,"(((1,4,2,),(1,4,1,),),((1,4,2,),(1,4,1,),),)","(('bfloat16','bfloat16',),('bfloat16','bfloat16',),)","(('ND','ND',),('ND','ND',),)",,,"(1,)",,,"(((-0.01,0.01,),(-1.1754943508e-38,-1.1754943508e-38,),),)",,
5+aclnnForeachAcos_L0_004,aclnnForeachAcos,"(((),(),(),(),(),),((),(),(),(),(),),)","(('bfloat16','bfloat16','bfloat16','bfloat16','bfloat16',),('bfloat16','bfloat16','bfloat16','bfloat16','bfloat16',),)","(('ND','ND','ND','ND','ND',),('ND','ND','ND','ND','ND',),)",,,"(1,)",,,"(((-10,-2,),(0.0,0.0,),(-0.01,-0.001,),(0,0.001,),(-100,100,),),)",,
6+aclnnForeachAcos_L0_005,aclnnForeachAcos,"(((5,),(5,),(1,),(1,),(5,),),((5,),(5,),(1,),(1,),(5,),),)","(('float16','float16','float16','float16','float16',),('float16','float16','float16','float16','float16',),)","(('ND','ND','ND','ND','ND',),('ND','ND','ND','ND','ND',),)",,,"(1,)",,,"(((-0.01,0.01,),(-10,-2,),(inf,inf,),(-100,100,),(0,0.001,),),)",,
@@ -0,0 +1,6 @@
1+testcase_name,network_name,op_name,input_shapes,input_dtypes,input_formats,output_shapes,output_dtypes,output_formats,input_ori_shapes,input_ori_formats,output_ori_shapes,output_ori_formats,attributes,input_data_ranges,precision_tolerances,absolute_precision,output_inplace_indexes,output_shape_unknown_indexes,is_enabled,remark,soc_series,priority,dump_file_prefix,manual_input_binaries,manual_golden_binaries
2+foreach_acos_L0_001,UNKNOWN,foreach_acos,"(((5,3,4,8,3,6,),(8,4,3,1,7,6,),(5,6,2,6,2,3,),(4,1,5,8,6,3,),(7,1,8,),(2,5,7,3,5,2,),(2,6,6,2,7,1,),),)","(('float16','float16','float16','float16','float16','float16','float16',),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((5,3,4,8,3,6,),(8,4,3,1,7,6,),(5,6,2,6,2,3,),(4,1,5,8,6,3,),(7,1,8,),(2,5,7,3,5,2,),(2,6,6,2,7,1,),),)","(('float16','float16','float16','float16','float16','float16','float16',),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((5,3,4,8,3,6,),(8,4,3,1,7,6,),(5,6,2,6,2,3,),(4,1,5,8,6,3,),(7,1,8,),(2,5,7,3,5,2,),(2,6,6,2,7,1,),),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((5,3,4,8,3,6,),(8,4,3,1,7,6,),(5,6,2,6,2,3,),(4,1,5,8,6,3,),(7,1,8,),(2,5,7,3,5,2,),(2,6,6,2,7,1,),),)","(('ND','ND','ND','ND','ND','ND','ND',),)",{},"(((-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),),)",,1e-08,(),(),True,,,0,,(),()
3+foreach_acos_L0_002,UNKNOWN,foreach_acos,"(((8,3,6,2,8,7,),(7,1,2,6,7,1,),(7,3,2,4,6,2,),(1,1,5,1,4,1,),(1,4,5,4,7,6,),(5,7,7,2,4,1,),(7,2,6,1,5,2,),),)","(('float32','float32','float32','float32','float32','float32','float32',),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((8,3,6,2,8,7,),(7,1,2,6,7,1,),(7,3,2,4,6,2,),(1,1,5,1,4,1,),(1,4,5,4,7,6,),(5,7,7,2,4,1,),(7,2,6,1,5,2,),),)","(('float32','float32','float32','float32','float32','float32','float32',),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((8,3,6,2,8,7,),(7,1,2,6,7,1,),(7,3,2,4,6,2,),(1,1,5,1,4,1,),(1,4,5,4,7,6,),(5,7,7,2,4,1,),(7,2,6,1,5,2,),),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((8,3,6,2,8,7,),(7,1,2,6,7,1,),(7,3,2,4,6,2,),(1,1,5,1,4,1,),(1,4,5,4,7,6,),(5,7,7,2,4,1,),(7,2,6,1,5,2,),),)","(('ND','ND','ND','ND','ND','ND','ND',),)",{},"(((-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),),)",,1e-08,(),(),True,,,0,,(),()
4+foreach_acos_L0_003,UNKNOWN,foreach_acos,"(((8,2,5,6,),(4,1,6,1,),(3,),(4,5,2,5,),(5,7,8,7,),(4,7,1,7,),(3,5,2,5,),),)","(('bfloat16','bfloat16','bfloat16','bfloat16','bfloat16','bfloat16','bfloat16',),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((8,2,5,6,),(4,1,6,1,),(3,),(4,5,2,5,),(5,7,8,7,),(4,7,1,7,),(3,5,2,5,),),)","(('bfloat16','bfloat16','bfloat16','bfloat16','bfloat16','bfloat16','bfloat16',),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((8,2,5,6,),(4,1,6,1,),(3,),(4,5,2,5,),(5,7,8,7,),(4,7,1,7,),(3,5,2,5,),),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((8,2,5,6,),(4,1,6,1,),(3,),(4,5,2,5,),(5,7,8,7,),(4,7,1,7,),(3,5,2,5,),),)","(('ND','ND','ND','ND','ND','ND','ND',),)",{},"(((-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),),)",,1e-08,(),(),True,,,0,,(),()
5+foreach_acos_L0_004,UNKNOWN,foreach_acos,"(((),(),(),(),(),(),(),),)","(('float32','float32','float32','float32','float32','float32','float32',),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((),(),(),(),(),(),(),),)","(('float32','float32','float32','float32','float32','float32','float32',),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((),(),(),(),(),(),(),),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((),(),(),(),(),(),(),),)","(('ND','ND','ND','ND','ND','ND','ND',),)",{},"(((-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),),)",,1e-08,(),(),True,,,0,,(),()
6+foreach_acos_L0_005,UNKNOWN,foreach_acos,"(((2,),(5,),(3,),(3,),(6,),(2,),(5,),),)","(('float16','float16','float16','float16','float16','float16','float16',),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((2,),(5,),(3,),(3,),(6,),(2,),(5,),),)","(('float16','float16','float16','float16','float16','float16','float16',),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((2,),(5,),(3,),(3,),(6,),(2,),(5,),),)","(('ND','ND','ND','ND','ND','ND','ND',),)","(((2,),(5,),(3,),(3,),(6,),(2,),(5,),),)","(('ND','ND','ND','ND','ND','ND','ND',),)",{},"(((-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),(-1.0,1.0,),),)",,1e-08,(),(),True,,,0,,(),()
@@ -2,10 +2,10 @@
2# -*- coding: utf-8 -*-2# -*- coding: utf-8 -*-
3# ----------------------------------------------------------------------------3# ----------------------------------------------------------------------------
4# Copyright (c) 2025 Huawei Technologies Co., Ltd.4# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 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").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.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, 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.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.10# See LICENSE in the root of the software repository for the full text of the License.
11# ----------------------------------------------------------------------------11# ----------------------------------------------------------------------------
@@ -17,22 +17,29 @@ import os
17 17 
18curr_dir = os.path.dirname(os.path.realpath(__file__))18curr_dir = os.path.dirname(os.path.realpath(__file__))
19 19 
20+ 
20def compare_data(golden_file_lists, output_file_lists, d_type):21def compare_data(golden_file_lists, output_file_lists, d_type):
21 np_dtype = np.float3222 np_dtype = np.float32
22 if d_type == "float16":23 if d_type == "float16":
23 np_dtype = np.float1624 np_dtype = np.float16
24- precision = 1/100025+ precision = 1 / 1000
25 elif d_type == "float32":26 elif d_type == "float32":
26- precision = 1/1000027+ precision = 1 / 10000
28+ elif d_type == "bfloat16_t":
29+ np_dtype = np.uint16
30+ precision = 1 / 10
27 else:31 else:
28- precision = 1/1032+ precision = 1 / 10
29- 33+ 
30 data_same = True34 data_same = True
31 for gold, out in zip(golden_file_lists, output_file_lists):35 for gold, out in zip(golden_file_lists, output_file_lists):
32 tmp_out = np.fromfile(out, np_dtype)36 tmp_out = np.fromfile(out, np_dtype)
33 tmp_gold = np.fromfile(gold, np_dtype)37 tmp_gold = np.fromfile(gold, np_dtype)
38+ if d_type == "bfloat16_t":
39+ tmp_out = (tmp_out.astype(np.uint32) << 16).view(np.float32)
40+ tmp_gold = (tmp_gold.astype(np.uint32) << 16).view(np.float32)
34 diff_res = np.isclose(tmp_out, tmp_gold, precision, 0, True)41 diff_res = np.isclose(tmp_out, tmp_gold, precision, 0, True)
35- diff_idx = np.where(diff_res != True)[0]42+ diff_idx = np.where(~diff_res)[0]
36 if len(diff_idx) == 0:43 if len(diff_idx) == 0:
37 print("PASSED!")44 print("PASSED!")
38 else:45 else:
@@ -42,14 +49,18 @@ def compare_data(golden_file_lists, output_file_lists, d_type):
42 data_same = False49 data_same = False
43 return data_same50 return data_same
44 51 
52+ 
45def get_file_lists(dtype):53def get_file_lists(dtype):
46 golden_file_lists = sorted(glob.glob(curr_dir + "/*golden*.bin"))54 golden_file_lists = sorted(glob.glob(curr_dir + "/*golden*.bin"))
47 output_file_lists = sorted(glob.glob(curr_dir + "/*output*.bin"))55 output_file_lists = sorted(glob.glob(curr_dir + "/*output*.bin"))
48 return golden_file_lists, output_file_lists56 return golden_file_lists, output_file_lists
49 57 
58+ 
50def process(d_type):59def process(d_type):
51 golden_file_lists, output_file_lists = get_file_lists(d_type)60 golden_file_lists, output_file_lists = get_file_lists(d_type)
52 result = compare_data(golden_file_lists, output_file_lists, d_type)61 result = compare_data(golden_file_lists, output_file_lists, d_type)
62+ sys.exit(0 if result else 1)
53 63 
54-if __name__ == '__main__':64+ 
55- process(sys.argv[1])65+if __name__ == "__main__":
66+ process(sys.argv[1])
@@ -2,10 +2,10 @@
2# -*- coding: utf-8 -*-2# -*- coding: utf-8 -*-
3# ----------------------------------------------------------------------------3# ----------------------------------------------------------------------------
4# Copyright (c) 2025 Huawei Technologies Co., Ltd.4# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 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").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.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, 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.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.10# See LICENSE in the root of the software repository for the full text of the License.
11# ----------------------------------------------------------------------------11# ----------------------------------------------------------------------------
@@ -15,6 +15,7 @@ import os
15import numpy as np15import numpy as np
16import re16import re
17import tensorflow as tf17import tensorflow as tf
18+import torch
18 19 
19 20 
20def parse_str_to_shape_list(shape_str):21def parse_str_to_shape_list(shape_str):
@@ -30,15 +31,24 @@ def gen_data_and_golden(shape_str, d_type="float32"):
30 d_type_dict = {31 d_type_dict = {
31 "float32": np.float32,32 "float32": np.float32,
32 "float16": np.float16,33 "float16": np.float16,
33- "bfloat16_t": tf.bfloat16.as_numpy_dtype34+ "bfloat16_t": tf.bfloat16.as_numpy_dtype,
34 }35 }
35 np_type = d_type_dict[d_type]36 np_type = d_type_dict[d_type]
36 shape_list = parse_str_to_shape_list(shape_str)37 shape_list = parse_str_to_shape_list(shape_str)
37 for index, shape in enumerate(shape_list):38 for index, shape in enumerate(shape_list):
38 tmp_input = np.random.uniform(-0.5, 0.5, size=shape).astype(np_type)39 tmp_input = np.random.uniform(-0.5, 0.5, size=shape).astype(np_type)
39- tmp_golden = np.arccos(tmp_input.astype(np_type))
40 40 
41 tmp_input.astype(np_type).tofile(f"{d_type}_input_t_foreach_acos_{index}.bin")41 tmp_input.astype(np_type).tofile(f"{d_type}_input_t_foreach_acos_{index}.bin")
42+ 
43+ # 参考数学由直接竞品 torch._foreach_acos 承担(GOLDEN.05 直接竞品优先、禁 NumPy 计算)
44+ if d_type == "bfloat16_t":
45+ x_torch = torch.from_numpy(tmp_input.view(np.uint16)).view(torch.bfloat16)
46+ golden_torch = torch._foreach_acos([x_torch])[0]
47+ tmp_golden = golden_torch.view(torch.uint16).numpy().view(np_type)
48+ else:
49+ x_torch = torch.from_numpy(tmp_input)
50+ tmp_golden = torch._foreach_acos([x_torch])[0].numpy().astype(np_type)
51+ 
42 tmp_golden.astype(np_type).tofile(f"{d_type}_golden_t_foreach_acos_{index}.bin")52 tmp_golden.astype(np_type).tofile(f"{d_type}_golden_t_foreach_acos_{index}.bin")
43 53 
44 54 
@@ -17,6 +17,7 @@
17#include <vector>17#include <vector>
18#include <iostream>18#include <iostream>
19#include <string>19#include <string>
20+#include <cstdlib>
20#include <cstdint>21#include <cstdint>
21#include "gtest/gtest.h"22#include "gtest/gtest.h"
22#include "tikicpulib.h"23#include "tikicpulib.h"
@@ -61,7 +62,7 @@ TEST_F(foreach_acos_test, test_case_float_1)
61 AscendC::GmFree((void*)workspace);62 AscendC::GmFree((void*)workspace);
62 AscendC::GmFree((void*)tiling);63 AscendC::GmFree((void*)tiling);
63 64 
64- system("cd ./acos_data/ && python3 compare_data.py 'float32'");65+ ASSERT_EQ(std::system("cd ./acos_data/ && python3 compare_data.py 'float32'"), 0);
65}66}
66 67 
67TEST_F(foreach_acos_test, test_case_float16_2)68TEST_F(foreach_acos_test, test_case_float16_2)
@@ -94,7 +95,7 @@ TEST_F(foreach_acos_test, test_case_float16_2)
94 AscendC::GmFree((void*)workspace);95 AscendC::GmFree((void*)workspace);
95 AscendC::GmFree((void*)tiling);96 AscendC::GmFree((void*)tiling);
96 97 
97- system("cd ./acos_data/ && python3 compare_data.py 'float16'");98+ ASSERT_EQ(std::system("cd ./acos_data/ && python3 compare_data.py 'float16'"), 0);
98}99}
99 100 
100TEST_F(foreach_acos_test, test_case_bfloat16_3)101TEST_F(foreach_acos_test, test_case_bfloat16_3)
@@ -127,5 +128,5 @@ TEST_F(foreach_acos_test, test_case_bfloat16_3)
127 AscendC::GmFree((void*)workspace);128 AscendC::GmFree((void*)workspace);
128 AscendC::GmFree((void*)tiling);129 AscendC::GmFree((void*)tiling);
129 130 
130- system("cd ./acos_data/ && python3 compare_data.py 'bfloat16_t'");131+ ASSERT_EQ(std::system("cd ./acos_data/ && python3 compare_data.py 'bfloat16_t'"), 0);
131}132}