Merged
fix(optim): InplaceApplyCenteredRMSProp/InplaceApplyPowerSign 合规质量整改 #14077
tan_xinAdd on 12 days ago
fix(optim): InplaceApplyCenteredRMSProp/InplaceApplyPowerSign 合规质量整改 #14077
Merged
tan_xinAdd on 12 days ago
Total: 10 files changed+267-124
@@ -108,13 +108,6 @@
108 <td>FLOAT、FLOAT16</td>108 <td>FLOAT、FLOAT16</td>
109 <td>ND</td>109 <td>ND</td>
110 </tr>110 </tr>
111- <tr>
112- <td>use_locking</td>
113- <td>属性</td>
114- <td>可选兼容属性,默认值为false,取值不影响计算结果。</td>
115- <td>BOOL</td>
116- <td>-</td>
117- </tr>
118 <tr>111 <tr>
119 <td>var</td>112 <td>var</td>
120 <td>输出</td>113 <td>输出</td>
@@ -143,6 +136,13 @@
143 <td>FLOAT、FLOAT16</td>136 <td>FLOAT、FLOAT16</td>
144 <td>ND</td>137 <td>ND</td>
145 </tr>138 </tr>
139+ <tr>
140+ <td>use_locking</td>
141+ <td>属性</td>
142+ <td>可选兼容属性,默认值为false,取值不影响计算结果。</td>
143+ <td>BOOL</td>
144+ <td>-</td>
145+ </tr>
146</tbody></table>146</tbody></table>
147 147 
148## 约束说明148## 约束说明
@@ -160,6 +160,6 @@
160 160 
161## 调用说明161## 调用说明
162 162 
163-| 调用方式 | 调用样例 | 说明 |163+| 调用方式 | 样例代码 | 说明 |
164|:---------|:---------|:-----|164|:---------|:---------|:-----|
165-| 图模式 | [test_geir_inplace_apply_centered_rms_prop](./examples/arch35/test_geir_inplace_apply_centered_rms_prop.cpp) | 通过[算子IR](./op_graph/inplace_apply_centered_rms_prop_proto.h)构图方式调用InplaceApplyCenteredRMSProp算子。 |165+| GE图模式 | [test_geir_inplace_apply_centered_rms_prop](./examples/arch35/test_geir_inplace_apply_centered_rms_prop.cpp) | 通过[算子IR](./op_graph/inplace_apply_centered_rms_prop_proto.h)构图方式调用InplaceApplyCenteredRMSProp算子。 |
@@ -162,6 +162,6 @@ English | [简体中文](./README.md)
162 162 
163## Invocation Description163## Invocation Description
164 164 
165-| Invocation method | Invocation sample | Description |165+| Invocation method | Sample code | Description |
166|:---------|:---------|:-----|166|:---------|:---------|:-----|
167-| Graph mode | [test_geir_inplace_apply_centered_rms_prop](./examples/arch35/test_geir_inplace_apply_centered_rms_prop.cpp) | Calls the InplaceApplyCenteredRMSProp operator through graph construction with the [operator IR](./op_graph/inplace_apply_centered_rms_prop_proto.h). |167+| GE graph mode | [test_geir_inplace_apply_centered_rms_prop](./examples/arch35/test_geir_inplace_apply_centered_rms_prop.cpp) | Calls the InplaceApplyCenteredRMSProp operator through graph construction with the [operator IR](./op_graph/inplace_apply_centered_rms_prop_proto.h). |
@@ -193,6 +193,35 @@ static ge::graphStatus GetShapeAttrsInfo(gert::TilingContext* context, int64_t*
193 return ge::GRAPH_FAILED);193 return ge::GRAPH_FAILED);
194 }194 }
195 195 
196+ // 标量输入校验(lr/rho/momentum/epsilon,索引 4-7):
197+ // README 约束:"必须为标量或仅含一个元素的一维张量"且 dtype 与 var 一致。
198+ // CODE.04/GEIR.02/03/07/ROUTE.05:此前无任何校验入口,多元素标量静默取首元素、
199+ // 混合 dtype 在 kernel GM 回退路径按 var dtype 误读,静默交付错误结果。
200+ const int32_t scalarInputIndices[] = {4, 5, 6, 7}; // lr, rho, momentum, epsilon
201+ const char* scalarInputNames[] = {"lr", "rho", "momentum", "epsilon"};
202+ for (size_t i = 0; i < sizeof(scalarInputIndices) / sizeof(scalarInputIndices[0]); i++) {
203+ auto scalarShape = context->GetInputShape(scalarInputIndices[i]);
204+ OP_CHECK_NULL_WITH_CONTEXT(context, scalarShape);
205+ // rank 必须 ≤ 1(标量或仅含一个元素的一维张量)
206+ OP_CHECK_IF(scalarShape->GetStorageShape().GetDimNum() > 1,
207+ OP_LOGE(context, "InplaceApplyCenteredRMSProp: %s rank=%zu must be scalar or 1-D",
208+ scalarInputNames[i], scalarShape->GetStorageShape().GetDimNum()),
209+ return ge::GRAPH_FAILED);
210+ // 元素数必须为 1
211+ OP_CHECK_IF(
212+ EnsureNotScalar(scalarShape->GetStorageShape()).GetShapeSize() != 1,
213+ OP_LOGE(context, "InplaceApplyCenteredRMSProp: %s must have exactly 1 element", scalarInputNames[i]),
214+ return ge::GRAPH_FAILED);
215+ // dtype 必须与 var 一致(FP16/FP32)
216+ auto scalarDesc = context->GetInputDesc(scalarInputIndices[i]);
217+ OP_CHECK_NULL_WITH_CONTEXT(context, scalarDesc);
218+ ge::DataType scalarDtype = scalarDesc->GetDataType();
219+ OP_CHECK_IF(scalarDtype != *dataType,
220+ OP_LOGE(context, "InplaceApplyCenteredRMSProp: %s dtype=%d mismatch var=%d", scalarInputNames[i],
221+ static_cast<int>(scalarDtype), static_cast<int>(*dataType)),
222+ return ge::GRAPH_FAILED);
223+ }
224+ 
196 return ge::GRAPH_SUCCESS;225 return ge::GRAPH_SUCCESS;
197}226}
198 227 
@@ -44,7 +44,10 @@ namespace NsInplaceApplyCenteredRmsProp {
44 44 
45using namespace AscendC;45using namespace AscendC;
46 46 
47-constexpr int64_t BUFFER_NUM = 2; // 双缓冲(DoubleBuffer)47+// TQUE.01:同一 TPosition(VECIN)5 个双缓冲队列峰值 5×2=10 块 Buffer,超过 A5 已知上限 8。
48+// 修复:VECIN 5 个输入队列改为单缓冲(5 块 ≤ 8),VECOUT 4 个输出队列保持双缓冲(4×2=8 恰达上限)。
49+constexpr int64_t INPUT_BUFFER_NUM = 1; // VECIN 单缓冲,满足同位置 ≤ 8 块约束
50+constexpr int64_t BUFFER_NUM = 2; // VECOUT 双缓冲(DoubleBuffer)
48 51 
49// ============================================================================52// ============================================================================
50// VF function 前向声明53// VF function 前向声明
@@ -263,12 +266,12 @@ __aicore__ inline void InplaceApplyCenteredRmsProp<T>::Init(GM_ADDR var, GM_ADDR
263 msOutGm_.SetGlobalBuffer((__gm__ T*)msOut + offset);266 msOutGm_.SetGlobalBuffer((__gm__ T*)msOut + offset);
264 momOutGm_.SetGlobalBuffer((__gm__ T*)momOut + offset);267 momOutGm_.SetGlobalBuffer((__gm__ T*)momOut + offset);
265 268 
266- // 5. InitBuffer:5 输入 TQue + 4 输出 TQue(双缓冲)269+ // 5. InitBuffer:5 输入 TQue 单缓冲(VECIN 峰值 5 块 ≤ A5 上限 8)+ 4 输出 TQue 双缓冲(8 块)
267- pipe.InitBuffer(inQueueVar_, BUFFER_NUM, ubFormer_ * sizeof(T));270+ pipe.InitBuffer(inQueueVar_, INPUT_BUFFER_NUM, ubFormer_ * sizeof(T));
268- pipe.InitBuffer(inQueueMg_, BUFFER_NUM, ubFormer_ * sizeof(T));271+ pipe.InitBuffer(inQueueMg_, INPUT_BUFFER_NUM, ubFormer_ * sizeof(T));
269- pipe.InitBuffer(inQueueMs_, BUFFER_NUM, ubFormer_ * sizeof(T));272+ pipe.InitBuffer(inQueueMs_, INPUT_BUFFER_NUM, ubFormer_ * sizeof(T));
270- pipe.InitBuffer(inQueueMom_, BUFFER_NUM, ubFormer_ * sizeof(T));273+ pipe.InitBuffer(inQueueMom_, INPUT_BUFFER_NUM, ubFormer_ * sizeof(T));
271- pipe.InitBuffer(inQueueGrad_, BUFFER_NUM, ubFormer_ * sizeof(T));274+ pipe.InitBuffer(inQueueGrad_, INPUT_BUFFER_NUM, ubFormer_ * sizeof(T));
272 pipe.InitBuffer(outQueueVar_, BUFFER_NUM, ubFormer_ * sizeof(T));275 pipe.InitBuffer(outQueueVar_, BUFFER_NUM, ubFormer_ * sizeof(T));
273 pipe.InitBuffer(outQueueMg_, BUFFER_NUM, ubFormer_ * sizeof(T));276 pipe.InitBuffer(outQueueMg_, BUFFER_NUM, ubFormer_ * sizeof(T));
274 pipe.InitBuffer(outQueueMs_, BUFFER_NUM, ubFormer_ * sizeof(T));277 pipe.InitBuffer(outQueueMs_, BUFFER_NUM, ubFormer_ * sizeof(T));
@@ -10,8 +10,36 @@
10# ----------------------------------------------------------------------------10# ----------------------------------------------------------------------------
11 11 
12# Generated By CANNBot12# Generated By CANNBot
13+"""
14+inplace_apply_centered_rms_prop 在 Kernel/GEIR 测试路径下的 golden。
15+ 
16+整改依据(人工处理意见:接受):
17+ - GOLDEN.01: 显式登记 __spec__(Kernel/GEIR 通路)。
18+ - GOLDEN.05: 存在语义适用直接竞品(TF ResourceApplyCenteredRMSProp),golden 参考数学
19+ 改由 Torch 基础算子组合承担,禁止 NumPy 承担参考算术/归约/语义类型转换
20+ (NumPy 仅做数据语义桥接)。
21+ - GOLDEN.07: float32 分支不再与 kernel 同精度镜像算术;改在 float64 高精度域独立计算后
22+ round 到输入 dtype,避免 |grad| > sqrt(FLT_MAX)≈1.8446743e19 时 grad^2 上溢
23+ 与 kernel 同错而被比较掩盖。
24+ - GOLDEN.13: 交付 third_party(tf.raw_ops.ResourceApplyCenteredRMSProp,直接竞品 oracle)。
25+ - tolerance: cross_check 三方交叉校验(NPU / golden / third_party),level=L2 最严档,
26+ 触发 need_3party 运行 GPU 三方比对。
27+ 
28+数学(epsilon 加在 sqrt 内部,对标 TF):
29+ mg_out = rho * mg + (1 - rho) * grad
30+ ms_out = rho * ms + (1 - rho) * grad^2
31+ mom_out = momentum * mom + lr * grad / sqrt(ms_out - mg_out^2 + epsilon)
32+ var_out = var - mom_out
33+ 
34+Import discipline: torch / tensorflow 仅在函数体内 lazy import(TTK 在 aclnn 进程内加载
35+本文件时 aclrt 已初始化,模块级竞品框架 import 有 SIGSEGV 风险)。
36+"""
37+ 
13import numpy as np38import numpy as np
14 39 
40+__spec__ = {
41+ "inplace_apply_centered_rms_prop": "InplaceApplyCenteredRMSPropKernelSpec",
42+}
15 43 
16__golden__ = {44__golden__ = {
17 "kernel": {45 "kernel": {
@@ -19,83 +47,149 @@ __golden__ = {
19 }47 }
20}48}
21 49 
50+# tolerance: cross_check 三方交叉校验(NPU / golden / third_party),触发 need_3party
51+# 运行 GPU 三方比对;level=L2(最严档)。不使用 mix_tolerance(单标杆两方,不触发三方)。
52+_TOLERANCE = {
53+ "float16": {"standard": "cross_check", "level": "L2"},
54+ "float32": {"standard": "cross_check", "level": "L2"},
55+}
56+ 
57+_TORCH_DTYPE = {
58+ np.dtype(np.float16): "float16",
59+ np.dtype(np.float32): "float32",
60+ np.dtype(np.float64): "float64",
61+}
62+ 
63+ 
64+def _to_numpy(arr):
65+ if isinstance(arr, np.ndarray):
66+ return arr
67+ if hasattr(arr, "detach"): # torch.Tensor
68+ return arr.detach().cpu().numpy()
69+ return np.asarray(arr)
70+ 
71+ 
72+def _scalar_to_float(val):
73+ """标量参数统一 FP32 域读取(spec 强制:禁止 FP16 标量参与计算)。"""
74+ if hasattr(val, "item") and not isinstance(val, np.ndarray):
75+ val = val.item()
76+ if isinstance(val, (int, float, np.floating)):
77+ return float(val)
78+ arr = _to_numpy(val)
79+ if arr.dtype == np.float16:
80+ arr = arr.astype(np.float32)
81+ return float(arr.flat[0])
82+ 
83+ 
84+def _compute_fp64(var, mg, ms, mom, lr, rho, momentum, epsilon, grad):
85+ """Torch 基础算子组合 + float64 高精度独立参考,round 到输入 dtype 返回。
86+ 
87+ - GOLDEN.05: 参考数学由 Torch 基础算子承担(非 NumPy)。
88+ - GOLDEN.07: float64 域独立计算后舍入,不与 kernel 同精度镜像其算术。
89+ """
90+ import torch
91+ 
92+ var_np = _to_numpy(var)
93+ orig_dtype = var_np.dtype
94+ 
95+ if var_np.size == 0:
96+ return (
97+ var_np.astype(orig_dtype),
98+ _to_numpy(mg).astype(orig_dtype),
99+ _to_numpy(ms).astype(orig_dtype),
100+ _to_numpy(mom).astype(orig_dtype),
101+ )
102+ 
103+ v = torch.from_numpy(var_np.astype(np.float64))
104+ mg_t = torch.from_numpy(_to_numpy(mg).astype(np.float64))
105+ ms_t = torch.from_numpy(_to_numpy(ms).astype(np.float64))
106+ mom_t = torch.from_numpy(_to_numpy(mom).astype(np.float64))
107+ grad_t = torch.from_numpy(_to_numpy(grad).astype(np.float64))
108+ 
109+ lr_f = torch.tensor(_scalar_to_float(lr), dtype=torch.float64)
110+ rho_f = torch.tensor(_scalar_to_float(rho), dtype=torch.float64)
111+ momentum_f = torch.tensor(_scalar_to_float(momentum), dtype=torch.float64)
112+ epsilon_f = torch.tensor(_scalar_to_float(epsilon), dtype=torch.float64)
113+ one_minus_rho = 1.0 - rho_f
114+ 
115+ mg_out = rho_f * mg_t + one_minus_rho * grad_t
116+ ms_out = rho_f * ms_t + one_minus_rho * grad_t * grad_t
117+ denom = ms_out - mg_out * mg_out + epsilon_f
118+ mom_out = momentum_f * mom_t + lr_f * grad_t / torch.sqrt(denom)
119+ var_out = v - mom_out
120+ 
121+ torch_dtype = getattr(torch, _TORCH_DTYPE[np.dtype(orig_dtype)])
122+ outputs = []
123+ for t in (var_out, mg_out, ms_out, mom_out):
124+ # 到目标 dtype 的转换使用 round-to-nearest-even(对齐 kernel CAST_RINT),
125+ # 类型转换由 torch .to 完成,NumPy 仅做数据桥接(GOLDEN.05)
126+ outputs.append(t.to(torch_dtype).cpu().numpy())
127+ return tuple(outputs)
128+ 
22 129 
23def inplace_apply_centered_rms_prop_golden(130def inplace_apply_centered_rms_prop_golden(
24 var, mg, ms, mom, lr, rho, momentum, epsilon, grad, **kwargs131 var, mg, ms, mom, lr, rho, momentum, epsilon, grad, **kwargs
25):132):
26 """133 """
27- Golden function for inplace_apply_centered_rms_prop.134+ Golden for inplace_apply_centered_rms_prop.
28 135 
29- Inputs follow the operator definition order:136+ 输入顺序: var, mg, ms, mom, lr, rho, momentum, epsilon, grad
30- var, mg, ms, mom, lr, rho, momentum, epsilon, grad137+ 返回: [var_out, mg_out, ms_out, mom_out]
31- 138+ 精度: torch float64 高精度独立参考 → round 到输入 dtype(round-to-nearest-even)
32- Formula (epsilon inside sqrt, TF-aligned):
33- mg_out = rho * mg + (1 - rho) * grad
34- ms_out = rho * ms + (1 - rho) * grad^2
35- mom_out = momentum * mom + lr * grad / sqrt(ms_out - mg_out^2 + epsilon)
36- var_out = var - mom_out
37- 
38- Computation precision (aligned with kernel):
39- - float32: native float32 computation
40- - float16: cast to float32 (CAST_NONE), compute in float32, cast back (CAST_RINT)
41- 
42- Returns:
43- [var_out, mg_out, ms_out, mom_out]
44 """139 """
45- input_dtype = var.dtype140+ del kwargs
141+ return list(_compute_fp64(var, mg, ms, mom, lr, rho, momentum, epsilon, grad))
46 142 
47- lr = float(np.asarray(lr).flat[0])
48- rho = float(np.asarray(rho).flat[0])
49- momentum = float(np.asarray(momentum).flat[0])
50- epsilon = float(np.asarray(epsilon).flat[0])
51 143 
52- if var.size == 0:144+class _TensorFlowResourceApplyCenteredRMSProp:
53- return [145+ """真实 TF 竞品接口(直接竞品优先 oracle,GOLDEN.05)。
54- var.astype(input_dtype),
55- mg.astype(input_dtype),
56- ms.astype(input_dtype),
57- mom.astype(input_dtype),
58- ]
59 146 
60- if input_dtype == np.float32:147+ 调用 tf.raw_ops.ResourceApplyCenteredRMSProp,epsilon 在 sqrt 内部,与算子语义逐项一致。
61- var_f = var.astype(np.float32)148+ """
62- mg_f = mg.astype(np.float32)
63- ms_f = ms.astype(np.float32)
64- mom_f = mom.astype(np.float32)
65- grad_f = grad.astype(np.float32)
66 149 
67- lr_f = np.float32(lr)150+ def __call__(self, **kwargs):
68- rho_f = np.float32(rho)151+ import tensorflow as tf
69- momentum_f = np.float32(momentum)
70- epsilon_f = np.float32(epsilon)
71- one_minus_rho_f = np.float32(1.0) - rho_f
72 152 
73- mg_out = (rho_f * mg_f + one_minus_rho_f * grad_f).astype(np.float32)153+ var_np = _to_numpy(kwargs.get("var"))
74- ms_out = (rho_f * ms_f + one_minus_rho_f * grad_f * grad_f).astype(np.float32)154+ orig_dtype = var_np.dtype
75- denom = (ms_out - mg_out * mg_out + epsilon_f).astype(np.float32)155+ 
76- mom_out = (momentum_f * mom_f + lr_f * grad_f / np.sqrt(denom)).astype(156+ var_v = tf.Variable(var_np.astype(np.float32))
77- np.float32157+ mg_v = tf.Variable(_to_numpy(kwargs.get("mg")).astype(np.float32))
158+ ms_v = tf.Variable(_to_numpy(kwargs.get("ms")).astype(np.float32))
159+ mom_v = tf.Variable(_to_numpy(kwargs.get("mom")).astype(np.float32))
160+ grad_t = tf.constant(_to_numpy(kwargs.get("grad")).astype(np.float32))
161+ 
162+ tf.raw_ops.ResourceApplyCenteredRMSProp(
163+ var=var_v.handle,
164+ mg=mg_v.handle,
165+ ms=ms_v.handle,
166+ mom=mom_v.handle,
167+ lr=tf.constant(_scalar_to_float(kwargs.get("lr")), dtype=tf.float32),
168+ rho=tf.constant(_scalar_to_float(kwargs.get("rho")), dtype=tf.float32),
169+ momentum=tf.constant(
170+ _scalar_to_float(kwargs.get("momentum")), dtype=tf.float32
171+ ),
172+ epsilon=tf.constant(
173+ _scalar_to_float(kwargs.get("epsilon")), dtype=tf.float32
174+ ),
175+ grad=grad_t,
78 )176 )
79- var_out = (var_f - mom_out).astype(np.float32)
80- 
81- return [var_out, mg_out, ms_out, mom_out]
82- else:
83- var = var.astype(np.float64)
84- mg = mg.astype(np.float64)
85- ms = ms.astype(np.float64)
86- mom = mom.astype(np.float64)
87- grad = grad.astype(np.float64)
88- 
89- one_minus_rho = 1.0 - rho
90- mg_out = rho * mg + one_minus_rho * grad
91- ms_out = rho * ms + one_minus_rho * grad * grad
92- denom = ms_out - mg_out * mg_out + epsilon
93- mom_out = momentum * mom + lr * grad / np.sqrt(denom)
94- var_out = var - mom_out
95 177 
96 return [178 return [
97- var_out.astype(input_dtype),179+ var_v.numpy().astype(orig_dtype),
98- mg_out.astype(input_dtype),180+ mg_v.numpy().astype(orig_dtype),
99- ms_out.astype(input_dtype),181+ ms_v.numpy().astype(orig_dtype),
100- mom_out.astype(input_dtype),182+ mom_v.numpy().astype(orig_dtype),
101 ]183 ]
184+ 
185+ 
186+class InplaceApplyCenteredRMSPropKernelSpec:
187+ golden = staticmethod(inplace_apply_centered_rms_prop_golden)
188+ # GOLDEN.13: third_party 独立参考(直接竞品 API,跨设备数值对标)
189+ third_party = {
190+ "tf": _TensorFlowResourceApplyCenteredRMSProp,
191+ }
192+ tolerance = _TOLERANCE
193+ 
194+ 
195+# 本算子仅提供 Kernel/GEIR 通路,无 aclnn / torch e2e 通路,故不注册对应 TestSpec。
@@ -1,15 +1,14 @@
1- # -----------------------------------------------------------------------------1+# ----------------------------------------------------------------------------
2- # This program is free software, you can redistribute it and/or modify.2+# Copyright (c) 2026 Huawei Technologies Co., Ltd.
3- # 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- # This file is a part of the CANN Open Software.4+# CANN Open Software License Agreement Version 2.0 (the "License").
5- # Licensed under 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- # 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- # 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- # 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- # See LICENSE in the root of the software repository for the full text of the License.9+#
10- # -----------------------------------------------------------------------------10+# Generated By CANNBot
11- 11+# ----------------------------------------------------------------------------
12- # Generated By CANNBot
13if(UT_TEST_ALL OR OP_HOST_UT)12if(UT_TEST_ALL OR OP_HOST_UT)
14 add_modules_ut_sources(HOSTNAME ${OP_TILING_MODULE_NAME} MODE PRIVATE DIR ${CMAKE_CURRENT_SOURCE_DIR})13 add_modules_ut_sources(HOSTNAME ${OP_TILING_MODULE_NAME} MODE PRIVATE DIR ${CMAKE_CURRENT_SOURCE_DIR})
15 add_modules_ut_sources(HOSTNAME ${OP_INFERSHAPE_MODULE_NAME} MODE PRIVATE DIR ${CMAKE_CURRENT_SOURCE_DIR})14 add_modules_ut_sources(HOSTNAME ${OP_INFERSHAPE_MODULE_NAME} MODE PRIVATE DIR ${CMAKE_CURRENT_SOURCE_DIR})
@@ -62,28 +62,28 @@
62 <tr>62 <tr>
63 <td>lr</td>63 <td>lr</td>
64 <td>输入</td>64 <td>输入</td>
65- <td>学习率,对应公式中的lr。shape必须为 [1] 的scalar Tensor。</td>65+ <td>学习率,对应公式中的lr。shape必须为 [1] 或 0-D([])的scalar Tensor。</td>
66 <td>BFLOAT16、FLOAT16、FLOAT</td>66 <td>BFLOAT16、FLOAT16、FLOAT</td>
67 <td>ND</td>67 <td>ND</td>
68 </tr>68 </tr>
69 <tr>69 <tr>
70 <td>logbase</td>70 <td>logbase</td>
71 <td>输入</td>71 <td>输入</td>
72- <td>对数基底,对应公式中的logbase。shape必须为 [1] 的scalar Tensor。</td>72+ <td>对数基底,对应公式中的logbase。shape必须为 [1] 或 0-D([])的scalar Tensor。</td>
73 <td>BFLOAT16、FLOAT16、FLOAT</td>73 <td>BFLOAT16、FLOAT16、FLOAT</td>
74 <td>ND</td>74 <td>ND</td>
75 </tr>75 </tr>
76 <tr>76 <tr>
77 <td>sign_decay</td>77 <td>sign_decay</td>
78 <td>输入</td>78 <td>输入</td>
79- <td>符号衰减因子,对应公式中的sign_decay。shape必须为 [1] 的scalar Tensor。</td>79+ <td>符号衰减因子,对应公式中的sign_decay。shape必须为 [1] 或 0-D([])的scalar Tensor。</td>
80 <td>BFLOAT16、FLOAT16、FLOAT</td>80 <td>BFLOAT16、FLOAT16、FLOAT</td>
81 <td>ND</td>81 <td>ND</td>
82 </tr>82 </tr>
83 <tr>83 <tr>
84 <td>beta</td>84 <td>beta</td>
85 <td>输入</td>85 <td>输入</td>
86- <td>一阶矩衰减率,对应公式中的β,取值范围 [0, 1)。shape必须为 [1] 的scalar Tensor。</td>86+ <td>一阶矩衰减率,对应公式中的β,取值范围 [0, 1)。shape必须为 [1] 或 0-D([])的scalar Tensor。</td>
87 <td>BFLOAT16、FLOAT16、FLOAT</td>87 <td>BFLOAT16、FLOAT16、FLOAT</td>
88 <td>ND</td>88 <td>ND</td>
89 </tr>89 </tr>
@@ -94,13 +94,6 @@
94 <td>BFLOAT16、FLOAT16、FLOAT</td>94 <td>BFLOAT16、FLOAT16、FLOAT</td>
95 <td>ND</td>95 <td>ND</td>
96 </tr>96 </tr>
97- <tr>
98- <td>use_locking</td>
99- <td>属性</td>
100- <td>是否在更新时加锁。默认false。当前实现不强制互斥锁,仅作语义占位。</td>
101- <td>BOOL</td>
102- <td>-</td>
103- </tr>
104 <tr>97 <tr>
105 <td>var</td>98 <td>var</td>
106 <td>输出</td>99 <td>输出</td>
@@ -115,17 +108,25 @@
115 <td>BFLOAT16、FLOAT16、FLOAT</td>108 <td>BFLOAT16、FLOAT16、FLOAT</td>
116 <td>ND</td>109 <td>ND</td>
117 </tr>110 </tr>
111+ <tr>
112+ <td>use_locking</td>
113+ <td>属性</td>
114+ <td>是否在更新时加锁。默认false。当前实现不强制互斥锁,仅作语义占位。</td>
115+ <td>BOOL</td>
116+ <td>-</td>
117+ </tr>
118</tbody></table>118</tbody></table>
119 119 
120## 约束说明120## 约束说明
121 121 
122- var、m、grad三个张量的shape必须一致。122- var、m、grad三个张量的shape必须一致。
123-- lr、logbase、sign_decay、beta必须为shape=[1]的scalar Tensor。123+- lr、logbase、sign_decay、beta必须为shape=[1]或0-D([])的scalar Tensor。
124- 所有输入输出的DataType必须一致。124- 所有输入输出的DataType必须一致。
125- FP16/BF16输入内部提升至FP32计算后cast回原类型。125- FP16/BF16输入内部提升至FP32计算后cast回原类型。
126+- 支持动态Shape与动态Rank(GE图模式):输出shape与对应输入shape一致,rank不超过8;动态场景下各输入需在构图时满足上述shape一致性约束。
126 127 
127## 调用说明128## 调用说明
128 129 
129-| 调用方式 | 调用样例 | 说明 |130+| 调用方式 | 样例代码 | 说明 |
130|--------------|------------------------------------------------------------------------|--------------------------------------------------------------|131|--------------|------------------------------------------------------------------------|--------------------------------------------------------------|
131-| 图模式 | [test_geir_inplace_apply_power_sign](./examples/arch35/test_geir_inplace_apply_power_sign.cpp) | 通过[算子IR](./op_graph/inplace_apply_power_sign_proto.h)构图方式调用InplaceApplyPowerSign算子。 |132+| GE图模式 | [test_geir_inplace_apply_power_sign](./examples/arch35/test_geir_inplace_apply_power_sign.cpp) | 通过[算子IR](./op_graph/inplace_apply_power_sign_proto.h)构图方式调用InplaceApplyPowerSign算子。 |
@@ -64,28 +64,28 @@ English | [简体中文](./README.md)
64 <tr>64 <tr>
65 <td>lr</td>65 <td>lr</td>
66 <td>Input</td>66 <td>Input</td>
67- <td>Learning rate, corresponding to lr in the formula. The shape must be a scalar tensor of [1].</td>67+ <td>Learning rate, corresponding to lr in the formula. The shape must be a scalar tensor of [1] or 0-D ([]).</td>
68 <td>BFLOAT16, FLOAT16, FLOAT</td>68 <td>BFLOAT16, FLOAT16, FLOAT</td>
69 <td>ND</td>69 <td>ND</td>
70 </tr>70 </tr>
71 <tr>71 <tr>
72 <td>logbase</td>72 <td>logbase</td>
73 <td>Input</td>73 <td>Input</td>
74- <td>Logarithm base, corresponding to logbase in the formula. The shape must be a scalar tensor of [1].</td>74+ <td>Logarithm base, corresponding to logbase in the formula. The shape must be a scalar tensor of [1] or 0-D ([]).</td>
75 <td>BFLOAT16, FLOAT16, FLOAT</td>75 <td>BFLOAT16, FLOAT16, FLOAT</td>
76 <td>ND</td>76 <td>ND</td>
77 </tr>77 </tr>
78 <tr>78 <tr>
79 <td>sign_decay</td>79 <td>sign_decay</td>
80 <td>Input</td>80 <td>Input</td>
81- <td>Sign decay factor, corresponding to sign_decay in the formula. The shape must be a scalar tensor of [1].</td>81+ <td>Sign decay factor, corresponding to sign_decay in the formula. The shape must be a scalar tensor of [1] or 0-D ([]).</td>
82 <td>BFLOAT16, FLOAT16, FLOAT</td>82 <td>BFLOAT16, FLOAT16, FLOAT</td>
83 <td>ND</td>83 <td>ND</td>
84 </tr>84 </tr>
85 <tr>85 <tr>
86 <td>beta</td>86 <td>beta</td>
87 <td>Input</td>87 <td>Input</td>
88- <td>First moment decay rate, corresponding to β in the formula, with the value range [0, 1). The shape must be a scalar tensor of [1].</td>88+ <td>First moment decay rate, corresponding to β in the formula, with the value range [0, 1). The shape must be a scalar tensor of [1] or 0-D ([]).</td>
89 <td>BFLOAT16, FLOAT16, FLOAT</td>89 <td>BFLOAT16, FLOAT16, FLOAT</td>
90 <td>ND</td>90 <td>ND</td>
91 </tr>91 </tr>
@@ -96,13 +96,6 @@ English | [简体中文](./README.md)
96 <td>BFLOAT16, FLOAT16, FLOAT</td>96 <td>BFLOAT16, FLOAT16, FLOAT</td>
97 <td>ND</td>97 <td>ND</td>
98 </tr>98 </tr>
99- <tr>
100- <td>use_locking</td>
101- <td>Attribute</td>
102- <td>Whether to lock during the update. The default value is false. The current implementation does not enforce a mutex lock; it serves only as a semantic placeholder.</td>
103- <td>BOOL</td>
104- <td>-</td>
105- </tr>
106 <tr>99 <tr>
107 <td>var</td>100 <td>var</td>
108 <td>Output</td>101 <td>Output</td>
@@ -117,17 +110,25 @@ English | [简体中文](./README.md)
117 <td>BFLOAT16, FLOAT16, FLOAT</td>110 <td>BFLOAT16, FLOAT16, FLOAT</td>
118 <td>ND</td>111 <td>ND</td>
119 </tr>112 </tr>
113+ <tr>
114+ <td>use_locking</td>
115+ <td>Attribute</td>
116+ <td>Whether to lock during the update. The default value is false. The current implementation does not enforce a mutex lock; it serves only as a semantic placeholder.</td>
117+ <td>BOOL</td>
118+ <td>-</td>
119+ </tr>
120</tbody></table>120</tbody></table>
121 121 
122## Constraints122## Constraints
123 123 
124- The shapes of the three tensors var, m, and grad must be consistent.124- The shapes of the three tensors var, m, and grad must be consistent.
125-- lr, logbase, sign_decay, and beta must be scalar tensors with shape=[1].125+- lr, logbase, sign_decay, and beta must be scalar tensors with shape=[1] or 0-D ([]).
126- The DataTypes of all inputs and outputs must be consistent.126- The DataTypes of all inputs and outputs must be consistent.
127- FP16/BF16 inputs are internally promoted to FP32 for computation and cast back to the original type.127- FP16/BF16 inputs are internally promoted to FP32 for computation and cast back to the original type.
128+- Dynamic shape and dynamic rank are supported (GE graph mode): the output shape is the same as that of the corresponding input, with rank not exceeding 8; in dynamic scenarios, each input must still satisfy the shape-consistency constraints above at graph construction.
128 129 
129## Invocation Description130## Invocation Description
130 131 
131-| Invocation method | Invocation sample | Description |132+| Invocation method | Sample code | Description |
132|--------------|------------------------------------------------------------------------|--------------------------------------------------------------|133|--------------|------------------------------------------------------------------------|--------------------------------------------------------------|
133-| Graph-mode invocation | [test_geir_inplace_apply_power_sign](./examples/arch35/test_geir_inplace_apply_power_sign.cpp) | Invokes the InplaceApplyPowerSign operator through graph construction with the [operator IR](./op_graph/inplace_apply_power_sign_proto.h). |134+| GE graph mode | [test_geir_inplace_apply_power_sign](./examples/arch35/test_geir_inplace_apply_power_sign.cpp) | Invokes the InplaceApplyPowerSign operator through graph construction with the [operator IR](./op_graph/inplace_apply_power_sign_proto.h). |
@@ -154,10 +154,15 @@ int32_t GenOnesDataFloat32(vector<int64_t> shapes, Tensor& input_tensor, TensorD
154 }154 }
155 uint32_t data_len = size * 4;155 uint32_t data_len = size * 4;
156 float* pData = new (std::nothrow) float[size];156 float* pData = new (std::nothrow) float[size];
157+ if (pData == nullptr) {
158+ printf("GenOnesDataFloat32: allocation failed for size=%zu\n", size);
159+ return FAILED;
160+ }
157 for (size_t i = 0; i < size; ++i) {161 for (size_t i = 0; i < size; ++i) {
158 *(pData + i) = value;162 *(pData + i) = value;
159 }163 }
160 input_tensor = Tensor(input_tensor_desc, (uint8_t*)pData, data_len);164 input_tensor = Tensor(input_tensor_desc, (uint8_t*)pData, data_len);
165+ delete[] pData;
161 return SUCCESS;166 return SUCCESS;
162}167}
163 168 
@@ -171,10 +176,15 @@ int32_t GenOnesData(vector<int64_t> shapes, Tensor& input_tensor, TensorDesc& in
171 }176 }
172 uint32_t data_len = size * GetDataTypeSize(data_type);177 uint32_t data_len = size * GetDataTypeSize(data_type);
173 int32_t* pData = new (std::nothrow) int32_t[data_len];178 int32_t* pData = new (std::nothrow) int32_t[data_len];
179+ if (pData == nullptr) {
180+ printf("GenOnesData: allocation failed for data_len=%u\n", data_len);
181+ return FAILED;
182+ }
174 for (size_t i = 0; i < size; ++i) {183 for (size_t i = 0; i < size; ++i) {
175 *(pData + i) = value;184 *(pData + i) = value;
176 }185 }
177 input_tensor = Tensor(input_tensor_desc, reinterpret_cast<uint8_t*>(pData), data_len);186 input_tensor = Tensor(input_tensor_desc, reinterpret_cast<uint8_t*>(pData), data_len);
187+ delete[] pData;
178 return SUCCESS;188 return SUCCESS;
179}189}
180 190 
@@ -258,7 +268,12 @@ int main(int argc, char* argv[])
258 std::map<AscendString, AscendString> graph_options = {};268 std::map<AscendString, AscendString> graph_options = {};
259 uint32_t graph_id = 0;269 uint32_t graph_id = 0;
260 ret = session->AddGraph(graph_id, graph, graph_options);270 ret = session->AddGraph(graph_id, graph, graph_options);
261- 271+ if (ret != SUCCESS) {
272+ printf("%s - ERROR - [XIR]: Session add ir compute graph to ir session failed\n", GetTime().c_str());
273+ delete session;
274+ GEFinalize();
275+ return FAILED;
276+ }
262 printf("%s - INFO - [XIR]: Session add ir compute graph to ir session success\n", GetTime().c_str());277 printf("%s - INFO - [XIR]: Session add ir compute graph to ir session success\n", GetTime().c_str());
263 printf("%s - INFO - [XIR]: dump graph to txt\n", GetTime().c_str());278 printf("%s - INFO - [XIR]: dump graph to txt\n", GetTime().c_str());
264 std::string file_path = "./dump";279 std::string file_path = "./dump";
@@ -307,6 +322,7 @@ int main(int argc, char* argv[])
307 std::string warning_str(warning_msg.GetString());322 std::string warning_str(warning_msg.GetString());
308 std::cout << "Warning message: " << warning_str << std::endl;323 std::cout << "Warning message: " << warning_str << std::endl;
309 printf("%s - INFO - [XIR]: Start to finalize ir graph session\n", GetTime().c_str());324 printf("%s - INFO - [XIR]: Start to finalize ir graph session\n", GetTime().c_str());
325+ delete session;
310 ret = ge::GEFinalize();326 ret = ge::GEFinalize();
311 if (ret != SUCCESS) {327 if (ret != SUCCESS) {
312 printf("%s - INFO - [XIR]: Finalize ir graph session failed\n", GetTime().c_str());328 printf("%s - INFO - [XIR]: Finalize ir graph session failed\n", GetTime().c_str());
@@ -3,7 +3,7 @@
3# This program is free software, you can redistribute it and/or modify it under the terms and conditions of3# 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").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.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 OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,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.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.8# See LICENSE in the root of the software repository for the full text of the License.
9# ----------------------------------------------------------------------------9# ----------------------------------------------------------------------------