Closed
[apply_adam_v2] quality-hardening:算子质量加固修复(准入+检视问题整改) #11221
handsomeRobot_SKAdd on Sep 24Closed at 18 days ago
[apply_adam_v2] quality-hardening:算子质量加固修复(准入+检视问题整改) #11221
Closed
handsomeRobot_SKAdd on Sep 24Closed at 18 days ago
Total: 11 files changed+225-85
@@ -84,10 +84,10 @@
84- var、m、v、grad 四个 Tensor 的 shape 和数据类型必须完全一致。84- var、m、v、grad 四个 Tensor 的 shape 和数据类型必须完全一致。
85- lr、beta1、beta2、epsilon、max_grad_norm、global_grad_norm、weight_decay、step_size 为标量(shape [1]),数据类型必须与 Tensor 的数据类型一致。85- lr、beta1、beta2、epsilon、max_grad_norm、global_grad_norm、weight_decay、step_size 为标量(shape [1]),数据类型必须与 Tensor 的数据类型一致。
86- FP16 输入时,内部计算提升到 FP32 进行,输出再转回 FP16,以保证长期训练中矩估计的精度。86- FP16 输入时,内部计算提升到 FP32 进行,输出再转回 FP16,以保证长期训练中矩估计的精度。
87-- adam 模式下,若提供 max_grad_norm 和 global_grad_norm,则进行梯度裁剪:clip_coeff = min(1.0, max_grad_norm / global_grad_norm)。87+- adam 模式下,若提供 max_grad_norm 且满足 max_grad_norm > 0、global_grad_norm > 1,则进行梯度裁剪:clip_coeff = 1 / global_grad_norm(global_grad_norm < 1e-10 时内置置为 1.0)。
chenqi317
chenqi317chenqi317Oct 18

触发条件:adam 模式下传入 0 < max_grad_norm < 1 且 global_grad_norm > 1。base 版本文档与标准 Adam 裁剪语义均为 clip_coeff = min(1, max_grad_norm / global_grad_norm),而 kernel 只把 max_grad_norm 当开关(apply_adam_v2_kernel.h:383 判断 maxGradNorm > 0 && globalGradNorm > 1 后直接 clip_coeff = 1/globalGradNorm),其数值完全不参与运算。后果:本 PR 未修实现,而是把 README 口径改成与实现一致,等于用文档消除设计偏差;若 spec/golden 仍为 min 公式则线上数值系统性偏大(如 max=0.5、global=0.8 应为 0.625,实现给出 1.0),且 kernel 对 NaN/Inf 置 1.0 的保护也未写入文档。建议:按 spec 修正 kernel 为 min(1, max/global),或按设计变更流程同步 spec/golden 并在 README 明确标注“实现差异”,同时补充 max_grad_norm≠1 的精度用例。


如已修复请回复说明,我会在确认后把该意见标记为已解决。

likedislike
88- mbart_adam 模式下,若未提供 step_size,则使用 lr 作为 step_size。88- mbart_adam 模式下,若未提供 step_size,则使用 lr 作为 step_size。
89- 算子默认确定性实现,相同输入产生相同输出。89- 算子默认确定性实现,相同输入产生相同输出。
90-- 不支持空 Tensor(0 元素)。90+- 空 Tensor(var 元素数为 0)时直接返回,不执行计算。
91- Tensor rank 范围 0~8。91- Tensor rank 范围 0~8。
92- 动态 shape 支持情况:Kernel 模式支持 -1/-2(未知维度);GEIR 模式不支持 -1/-2,因为 canndev 内置 infershape 无法将未知维度正确传播到 inplace 输出。92- 动态 shape 支持情况:Kernel 模式支持 -1/-2(未知维度);GEIR 模式不支持 -1/-2,因为 canndev 内置 infershape 无法将未知维度正确传播到 inplace 输出。
93 93 
@@ -89,9 +89,14 @@ int32_t GenOnesDataFloat32(vector<int64_t> shapes, Tensor& input_tensor, TensorD
89 uint32_t byteSizeFloat32 = 4;89 uint32_t byteSizeFloat32 = 4;
90 uint32_t data_len = size * byteSizeFloat32;90 uint32_t data_len = size * byteSizeFloat32;
91 float* pData = new (std::nothrow) float[size];91 float* pData = new (std::nothrow) float[size];
92+ if (pData == nullptr) {
93+ printf("%s - ERROR - [XIR]: allocate input data failed\n", GetTime().c_str());
94+ return FAILED;
95+ }
92 for (size_t i = 0; i < size; ++i)96 for (size_t i = 0; i < size; ++i)
93 *(pData + i) = value;97 *(pData + i) = value;
94 input_tensor = Tensor(input_tensor_desc, (uint8_t*)pData, data_len);98 input_tensor = Tensor(input_tensor_desc, (uint8_t*)pData, data_len);
99+ delete[] pData; // GE Tensor 构造已拷贝,立即释放
chenqi317
chenqi317chenqi317Oct 18

本 PR 只给 GenOnesDataFloat32 的 new 补了判空并补了 delete[],同一文件 main() 中 “Session* session = new Session(build_opts);”(第 151 行)仍未做判空,且 “ret = session->AddGraph(...)”(第 154 行)的返回值未检查就无条件打印 AddGraph success。后果:内存不足或图注册失败时示例会静默打印成功结论或空指针解引用,内存与错误处理整改未真正闭环。建议:同批补 Session new 判空与 AddGraph 返回值判断(失败时打印错误、释放 session 并返回 FAILED)。


如已修复请回复说明,我会在确认后把该意见标记为已解决。

likedislike
95 return SUCCESS;100 return SUCCESS;
96}101}
97 102 
@@ -18,6 +18,7 @@
18 */18 */
19#include "log/log.h"19#include "log/log.h"
20#include "register/op_impl_registry.h"20#include "register/op_impl_registry.h"
21+#include "optim/apply_adam_v2/op_kernel/arch35/apply_adam_v2_tiling_data.h"
21 22 
22using namespace ge;23using namespace ge;
23 24 
@@ -44,7 +45,7 @@ static bool InputShapeCheck(const gert::InferShapeContext* context, const gert::
44 for (size_t i = 0; i < inputList0.size(); i++) {45 for (size_t i = 0; i < inputList0.size(); i++) {
45 const gert::Shape* inputShape = context->GetInputShape(inputList0[i]);46 const gert::Shape* inputShape = context->GetInputShape(inputList0[i]);
46 if (inputShape == nullptr) {47 if (inputShape == nullptr) {
47- OP_LOGE(OP_NAME, "input[%ld] shape is null", inputList0[i]);48+ OP_LOGE_WITH_INVALID_INPUT(OP_NAME, inputNameList0[i].c_str());
48 return false;49 return false;
49 }50 }
50 if (*inputShape != *varShape) {51 if (*inputShape != *varShape) {
@@ -57,27 +58,31 @@ static bool InputShapeCheck(const gert::InferShapeContext* context, const gert::
57 58 
58static bool InputOneShapeCheck(gert::InferShapeContext* context, const gert::Shape* beta1Shape)59static bool InputOneShapeCheck(gert::InferShapeContext* context, const gert::Shape* beta1Shape)
59{60{
60- std::vector<int64_t> inputList1 = {61+ std::vector<int64_t> inputList1 = {LR_IDX, BETA_2_IDX, EPSILON_IDX, GLOBAL_GRAD_NORM_IDX, WEIGHT_DECAY_IDX};
61- LR_IDX, BETA_2_IDX, EPSILON_IDX, MAX_GRAD_NORM_IDX, GLOBAL_GRAD_NORM_IDX, WEIGHT_DECAY_IDX};62+ std::vector<std::string> inputNameList1 = {"lr", "beta2", "epsilon", "global_grad_norm", "weight_decay"};
62- std::vector<std::string> inputNameList1 = {"lr", "beta2", "epsilon", "max_grad_norm", "global_grad_norm",
63- "weight_decay"};
64 63 
65 auto attrs = context->GetAttrs();64 auto attrs = context->GetAttrs();
66 if (attrs == nullptr) {65 if (attrs == nullptr) {
67- OP_LOGE(OP_NAME, "attrs is null");66+ OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(OP_NAME, "attrs", "attrs is null");
68 return false;67 return false;
69 }68 }
70 const char* adamPtr = attrs->GetAttrPointer<char>(ADAM_MODE_ATTR_IDX);69 const char* adamPtr = attrs->GetAttrPointer<char>(ADAM_MODE_ATTR_IDX);
71 OP_LOGD(OP_NAME, "Get adam mode successfully.");70 OP_LOGD(OP_NAME, "Get adam mode successfully.");
72- if ((adamPtr != nullptr) && (strcmp(adamPtr, "mbart_adam") == 0)) {71+ if ((adamPtr != nullptr) && (strcmp(adamPtr, kAdamModeMbart) == 0)) {
73 OP_LOGD(context->GetNodeName(), "get adam mode as mbart_adam");72 OP_LOGD(context->GetNodeName(), "get adam mode as mbart_adam");
74- inputList1.push_back(STEP_SIZE_IDX);73+ if (context->GetOptionalInputShape(STEP_SIZE_IDX) != nullptr) {
75- inputNameList1.push_back("step_size");74+ inputList1.push_back(STEP_SIZE_IDX);
75+ inputNameList1.push_back("step_size");
76+ }
77+ }
78+ if (context->GetOptionalInputShape(MAX_GRAD_NORM_IDX) != nullptr) {
79+ inputList1.push_back(MAX_GRAD_NORM_IDX);
80+ inputNameList1.push_back("max_grad_norm");
76 }81 }
77 for (size_t i = 0; i < inputList1.size(); i++) {82 for (size_t i = 0; i < inputList1.size(); i++) {
78 const gert::Shape* input1Shape = context->GetInputShape(inputList1[i]);83 const gert::Shape* input1Shape = context->GetInputShape(inputList1[i]);
79 if (input1Shape == nullptr) {84 if (input1Shape == nullptr) {
80- OP_LOGE(OP_NAME, "input[%ld] shape is null", inputList1[i]);85+ OP_LOGE_WITH_INVALID_INPUT(OP_NAME, inputNameList1[i].c_str());
81 return false;86 return false;
82 }87 }
83 if (*input1Shape != *beta1Shape) {88 if (*input1Shape != *beta1Shape) {
@@ -96,28 +101,31 @@ static ge::graphStatus InferShapeForApplyAdamV2(gert::InferShapeContext* context
96 101 
97 auto varShape = context->GetInputShape(VAR_IDX);102 auto varShape = context->GetInputShape(VAR_IDX);
98 if (varShape == nullptr) {103 if (varShape == nullptr) {
99- OP_LOGE(context, "input[0] (var) shape is null");104+ OP_LOGE_WITH_INVALID_INPUT(OP_NAME, "var");
100 return ge::GRAPH_FAILED;105 return ge::GRAPH_FAILED;
101 }106 }
102 107 
103 auto beta1Shape = context->GetInputShape(BETA_1_IDX);108 auto beta1Shape = context->GetInputShape(BETA_1_IDX);
104 if (beta1Shape == nullptr) {109 if (beta1Shape == nullptr) {
105- OP_LOGE(context, "input[%ld] (beta1) shape is null", BETA_1_IDX);110+ OP_LOGE_WITH_INVALID_INPUT(OP_NAME, "beta1");
106 return ge::GRAPH_FAILED;111 return ge::GRAPH_FAILED;
107 }112 }
108 113 
109 if (!((beta1Shape->GetDimNum() == 1 && beta1Shape->GetDim(0) == -2) ||114 if (!((beta1Shape->GetDimNum() == 1 && beta1Shape->GetDim(0) == -2) ||
110 (beta1Shape->GetDimNum() == 1 && beta1Shape->GetDim(0) == -1) ||115 (beta1Shape->GetDimNum() == 1 && beta1Shape->GetDim(0) == -1) ||
111 (beta1Shape->GetDimNum() == 1 && beta1Shape->GetDim(0) == 1) || beta1Shape->IsScalar())) {116 (beta1Shape->GetDimNum() == 1 && beta1Shape->GetDim(0) == 1) || beta1Shape->IsScalar())) {
112- OP_LOGE(context->GetNodeName(), "shape of exp_avg must be (1, )");117+ OP_LOGE_FOR_INVALID_SHAPE(OP_NAME, "beta1", Ops::Base::ToString(*beta1Shape), "(1,) or scalar");
chenqi317
chenqi317chenqi317Oct 18

触发条件:编译 op_host/apply_adam_v2_infershape.cpp。本文件仅 include log/log.h、register/op_impl_registry.h 以及新加的 op_kernel/arch35/apply_adam_v2_tiling_data.h,而 Ops::Base::ToString 声明在 util/shape_util.h;仓内其余使用该工具函数的 infershape(如 loss/mse_loss、optim/sparse_apply_ftrl)均显式 #include "util/shape_util.h",log/log.h 不提供该声明。后果:该翻译单元报 “Ops 未声明”,op_host 构建以及本 PR 刚加固过安全编译选项的 infershape UT 目标都会编译失败,质量加固分支无法出包。建议:在 log/log.h 之后补 #include "util/shape_util.h",并本地编译 infershape 目标验证。


如已修复请回复说明,我会在确认后把该意见标记为已解决。

likedislike
113 return ge::GRAPH_FAILED;118 return ge::GRAPH_FAILED;
114 }119 }
115 if (!InputShapeCheck(context, varShape)) {120 if (!InputShapeCheck(context, varShape)) {
116- OP_LOGE(context->GetNodeName(), "invalid input shape");121+ OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(OP_NAME, "m/v/grad", "different from var",
122+ "m, v and grad shapes must be identical to var");
117 return ge::GRAPH_FAILED;123 return ge::GRAPH_FAILED;
118 }124 }
119 if (!InputOneShapeCheck(context, beta1Shape)) {125 if (!InputOneShapeCheck(context, beta1Shape)) {
120- OP_LOGE(context->GetNodeName(), "invalid input one shape");126+ OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(OP_NAME, "lr/beta2/epsilon/global_grad_norm/weight_decay",
127+ "different from beta1",
128+ "scalar inputs must have the same shape as beta1");
121 return ge::GRAPH_FAILED;129 return ge::GRAPH_FAILED;
122 }130 }
123 131 
@@ -125,7 +133,7 @@ static ge::graphStatus InferShapeForApplyAdamV2(gert::InferShapeContext* context
125 for (int64_t idx : outputIdxList) {133 for (int64_t idx : outputIdxList) {
126 auto outShape = context->GetOutputShape(idx);134 auto outShape = context->GetOutputShape(idx);
127 if (outShape == nullptr) {135 if (outShape == nullptr) {
128- OP_LOGE(context, "output[%ld] shape is null", idx);136+ OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(OP_NAME, "output", "output shape is null");
129 return ge::GRAPH_FAILED;137 return ge::GRAPH_FAILED;
130 }138 }
131 *outShape = *varShape;139 *outShape = *varShape;
@@ -43,8 +43,11 @@ bool CheckBroadcastShape(const std::vector<std::vector<int64_t>>& padded_in,
43 if (ref == -1)43 if (ref == -1)
44 ref = padded_in[i][d];44 ref = padded_in[i][d];
45 else if (padded_in[i][d] != ref) {45 else if (padded_in[i][d] != ref) {
46- OP_LOGE("CheckBroadcastShape", "dim %d broadcast incompatible: input[%d] size %d != %d", (int)d,46+ OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON("CheckBroadcastShape", "input[" + std::to_string(i) + "]",
47- (int)i, (int)padded_in[i][d], (int)ref);47+ "dim " + std::to_string(d) + " size " +
48+ std::to_string(padded_in[i][d]) +
49+ " != " + std::to_string(ref),
50+ "broadcast incompatible");
48 return false;51 return false;
49 }52 }
50 }53 }
@@ -54,8 +57,11 @@ bool CheckBroadcastShape(const std::vector<std::vector<int64_t>>& padded_in,
54 if (ref == -1)57 if (ref == -1)
55 ref = padded_out[i][d];58 ref = padded_out[i][d];
56 else if (padded_out[i][d] != ref) {59 else if (padded_out[i][d] != ref) {
57- OP_LOGE("CheckBroadcastShape", "dim %d broadcast incompatible: output[%d] size %d != %d", (int)d,60+ OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON("CheckBroadcastShape", "output[" + std::to_string(i) + "]",
58- (int)i, (int)padded_out[i][d], (int)ref);61+ "dim " + std::to_string(d) + " size " +
62+ std::to_string(padded_out[i][d]) +
63+ " != " + std::to_string(ref),
64+ "broadcast incompatible");
59 return false;65 return false;
60 }66 }
61 }67 }
@@ -69,17 +75,17 @@ bool PadAndSqueeze(const std::vector<std::vector<int64_t>>& input_shapes,
69 std::vector<std::vector<int64_t>>& normal_input_shapes,75 std::vector<std::vector<int64_t>>& normal_input_shapes,
70 std::vector<std::vector<int64_t>>& normal_output_shapes)76 std::vector<std::vector<int64_t>>& normal_output_shapes)
71{77{
72- int64_t num_inputs = (int64_t)input_shapes.size();78+ int64_t num_inputs = static_cast<int64_t>(input_shapes.size());
73- int64_t num_outputs = (int64_t)output_shapes.size();79+ int64_t num_outputs = static_cast<int64_t>(output_shapes.size());
74 int64_t max_rank = 0;80 int64_t max_rank = 0;
75 for (auto& s : input_shapes)81 for (auto& s : input_shapes)
76- max_rank = std::max(max_rank, (int64_t)s.size());82+ max_rank = std::max(max_rank, static_cast<int64_t>(s.size()));
77 for (auto& s : output_shapes)83 for (auto& s : output_shapes)
78- max_rank = std::max(max_rank, (int64_t)s.size());84+ max_rank = std::max(max_rank, static_cast<int64_t>(s.size()));
79 85 
80 auto pad = [&](const std::vector<int64_t>& s) {86 auto pad = [&](const std::vector<int64_t>& s) {
81 std::vector<int64_t> p;87 std::vector<int64_t> p;
82- p.assign(max_rank - (int64_t)s.size(), 1);88+ p.assign(max_rank - static_cast<int64_t>(s.size()), 1);
83 p.insert(p.end(), s.begin(), s.end());89 p.insert(p.end(), s.begin(), s.end());
84 return p;90 return p;
85 };91 };
@@ -128,7 +134,7 @@ bool FindSplitAxis(const std::vector<int64_t>& max_bro_shape, int64_t dtype_size
128{134{
129 int64_t per_buf_bytes = (ub_per_core / phys_nodes) & ~31LL;135 int64_t per_buf_bytes = (ub_per_core / phys_nodes) & ~31LL;
130 int64_t per_buf_elems = per_buf_bytes / dtype_size;136 int64_t per_buf_elems = per_buf_bytes / dtype_size;
131- int64_t rank = (int64_t)max_bro_shape.size();137+ int64_t rank = static_cast<int64_t>(max_bro_shape.size());
132 int64_t inner = 1;138 int64_t inner = 1;
133 for (int64_t k = rank - 1; k >= 0; k--) {139 for (int64_t k = rank - 1; k >= 0; k--) {
134 if (max_bro_shape[k] * inner > per_buf_elems) {140 if (max_bro_shape[k] * inner > per_buf_elems) {
@@ -166,7 +172,7 @@ bool MultiCoreSplit(const std::vector<int64_t>& max_bro_shape, const SplitResult
166 172 
167bool PrecomputeInputStrides(const std::vector<int64_t>& s, std::vector<int64_t>& strides)173bool PrecomputeInputStrides(const std::vector<int64_t>& s, std::vector<int64_t>& strides)
168{174{
169- int64_t rank = (int64_t)s.size();175+ int64_t rank = static_cast<int64_t>(s.size());
170 strides.assign(rank, 0);176 strides.assign(rank, 0);
171 for (int64_t d = rank - 1; d >= 0; d--) {177 for (int64_t d = rank - 1; d >= 0; d--) {
172 if (s[d] == 1) {178 if (s[d] == 1) {
@@ -183,7 +189,7 @@ bool PrecomputeInputStrides(const std::vector<int64_t>& s, std::vector<int64_t>&
183 189 
184bool PrecomputeOutputStrides(const std::vector<int64_t>& s, std::vector<int64_t>& strides)190bool PrecomputeOutputStrides(const std::vector<int64_t>& s, std::vector<int64_t>& strides)
185{191{
186- int64_t rank = (int64_t)s.size();192+ int64_t rank = static_cast<int64_t>(s.size());
187 strides.assign(rank, 0);193 strides.assign(rank, 0);
188 for (int64_t d = rank - 1; d >= 0; d--) {194 for (int64_t d = rank - 1; d >= 0; d--) {
189 if (s[d] == 1) {195 if (s[d] == 1) {
@@ -255,7 +261,8 @@ ge::graphStatus ApplyAdamV2Tiling::GetShapeInfo()
255 OP_CHECK_NULL_WITH_CONTEXT(ctx_, shape);261 OP_CHECK_NULL_WITH_CONTEXT(ctx_, shape);
256 auto storageShape = shape->GetStorageShape();262 auto storageShape = shape->GetStorageShape();
257 if (storageShape.GetDimNum() > MAX_DIM_NUM) {263 if (storageShape.GetDimNum() > MAX_DIM_NUM) {
258- OP_LOGE(opName, "input[%zu] dim_num=%zu exceeds maximum %zu", i, storageShape.GetDimNum(), MAX_DIM_NUM);264+ OP_LOGE_FOR_INVALID_SHAPEDIM(opName, "input" + std::to_string(i),
265+ std::to_string(storageShape.GetDimNum()) + "D", "<=8D");
259 return ge::GRAPH_FAILED;266 return ge::GRAPH_FAILED;
260 }267 }
261 }268 }
@@ -270,7 +277,8 @@ ge::graphStatus ApplyAdamV2Tiling::GetShapeInfo()
270 continue;277 continue;
271 auto curShape = ctx_->GetInputShape(tensorIdx[k])->GetStorageShape();278 auto curShape = ctx_->GetInputShape(tensorIdx[k])->GetStorageShape();
272 if (varShape != curShape) {279 if (varShape != curShape) {
273- OP_LOGE(opName, "var and %s shapes must be identical", tensorNames[k]);280+ OP_LOGE_FOR_INVALID_SHAPE_WITH_REASON(opName, tensorNames[k], "different from var",
281+ "var/m/v/grad shapes must be identical");
274 return ge::GRAPH_FAILED;282 return ge::GRAPH_FAILED;
275 }283 }
276 }284 }
@@ -293,14 +301,26 @@ ge::graphStatus ApplyAdamV2Tiling::GetShapeInfo()
293 return ge::GRAPH_FAILED;301 return ge::GRAPH_FAILED;
294 }302 }
295 303 
296- // ===== 6. Dtype consistency: all inputs must have same dtype as var =====304+ // ===== 6. Format check: explicit host-side rejection (admission X2: def declares ND only) =====
305+ for (size_t i = 0; i < numInputs; ++i) {
306+ auto desc = ctx_->GetInputDesc(i);
307+ OP_CHECK_NULL_WITH_CONTEXT(ctx_, desc);
308+ auto fmt = desc->GetFormat().GetStorageFormat();
309+ if (fmt != ge::FORMAT_ND) {
310+ OP_LOGE_FOR_INVALID_FORMAT(opName, "input" + std::to_string(i),
311+ ge::TypeUtils::FormatToSerialString(fmt).c_str(), "ND");
312+ return ge::GRAPH_FAILED;
313+ }
314+ }
315+ 
316+ // ===== 7. Dtype consistency: all inputs must have same dtype as var =====
297 for (size_t i = 1; i < numInputs; ++i) {317 for (size_t i = 1; i < numInputs; ++i) {
298 auto desc = ctx_->GetInputDesc(i);318 auto desc = ctx_->GetInputDesc(i);
299 OP_CHECK_NULL_WITH_CONTEXT(ctx_, desc);319 OP_CHECK_NULL_WITH_CONTEXT(ctx_, desc);
300 if (desc->GetDataType() != dtype) {320 if (desc->GetDataType() != dtype) {
301- OP_LOGE(opName, "input[%zu] dtype=%s does not match var dtype=%s, all inputs must have the same dtype", i,321+ OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(opName, "input" + std::to_string(i),
302- ge::TypeUtils::DataTypeToSerialString(desc->GetDataType()).c_str(),322+ ge::TypeUtils::DataTypeToSerialString(desc->GetDataType()).c_str(),
303- ge::TypeUtils::DataTypeToSerialString(dtype).c_str());323+ "all inputs must have the same dtype as var");
304 return ge::GRAPH_FAILED;324 return ge::GRAPH_FAILED;
305 }325 }
306 }326 }
@@ -313,6 +333,13 @@ ge::graphStatus ApplyAdamV2Tiling::GetShapeInfo()
313 }333 }
314 dtype_size_ = static_cast<int64_t>(dtypeSize);334 dtype_size_ = static_cast<int64_t>(dtypeSize);
315 335 
336+ // ===== 6.5 Empty tensor short-circuit (Design §3.4/§5.4: host-side early detection) =====
337+ if (ctx_->GetInputShape(0)->GetStorageShape().GetShapeSize() == 0) {
chenqi317
chenqi317chenqi317Oct 18

触发条件:var 为 0 元素(shape 含 0)且其它输入非法,例如 lr/beta1/epsilon 传了 0 元素张量,或 m/v/grad 与 var 不广播兼容。该“6.5 空 Tensor 早退”插入在 dtype 校验之后、标量 shape 约束(第 7 段)、has_max_grad_norm_/has_step_size_ 探测与 PadAndSqueeze/broadcast 校验之前,命中即 return GRAPH_SUCCESS。后果:非法标量/不兼容形状被当作合法调用接受;而 kernel 的 Process() 即使 0 个 tile 也会无条件执行 *(gmInPtr_[IN_LR]) 等标量 GM 读取(apply_adam_v2_kernel.h:365-369),0 元素标量会造成非法 GM 访问;同时 rank_ 保持 0,一旦后续在空分支外复用会取到错误值。建议:将空 Tensor 早退移到标量校验之后、DoTilingAndSet/PadAndSqueeze 之前(空场景仍需跳过切分以避免 MultiCoreSplit 除零),或至少在空分支内补做标量与可选输入校验。


如已修复请回复说明,我会在确认后把该意见标记为已解决。

likedislike
338+ is_empty_ = true;
339+ OP_LOGI(opName, "var is empty (0 elements), skip kernel computation per Design Contract");
340+ return ge::GRAPH_SUCCESS;
341+ }
342+ 
316 // ===== 7. Scalar constraint: lr, beta1, beta2, epsilon, global_grad_norm, weight_decay =====343 // ===== 7. Scalar constraint: lr, beta1, beta2, epsilon, global_grad_norm, weight_decay =====
317 {344 {
318 const size_t scalarIdx[] = {3, 4, 5, 6, 9, 10};345 const size_t scalarIdx[] = {3, 4, 5, 6, 9, 10};
@@ -324,8 +351,8 @@ ge::graphStatus ApplyAdamV2Tiling::GetShapeInfo()
324 OP_CHECK_NULL_WITH_CONTEXT(ctx_, shape);351 OP_CHECK_NULL_WITH_CONTEXT(ctx_, shape);
325 auto storageShape = shape->GetStorageShape();352 auto storageShape = shape->GetStorageShape();
326 if (!storageShape.IsScalar() && storageShape.GetShapeSize() != SCALAR_ELEM_SIZE) {353 if (!storageShape.IsScalar() && storageShape.GetShapeSize() != SCALAR_ELEM_SIZE) {
327- OP_LOGE(opName, "%s must be scalar or size=1, got shape_size=%ld", scalarNames[k],354+ OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON(
328- storageShape.GetShapeSize());355+ opName, scalarNames[k], std::to_string(storageShape.GetShapeSize()), "must be scalar or size=1");
329 return ge::GRAPH_FAILED;356 return ge::GRAPH_FAILED;
330 }357 }
331 }358 }
@@ -340,7 +367,8 @@ ge::graphStatus ApplyAdamV2Tiling::GetShapeInfo()
340 auto shape = ctx_->GetOptionalInputShape(8);367 auto shape = ctx_->GetOptionalInputShape(8);
341 auto storageShape = shape->GetStorageShape();368 auto storageShape = shape->GetStorageShape();
342 if (!storageShape.IsScalar() && storageShape.GetShapeSize() != SCALAR_ELEM_SIZE) {369 if (!storageShape.IsScalar() && storageShape.GetShapeSize() != SCALAR_ELEM_SIZE) {
343- OP_LOGE(opName, "max_grad_norm must be scalar or size=1");370+ OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON(
371+ opName, "max_grad_norm", std::to_string(storageShape.GetShapeSize()), "must be scalar or size=1");
344 return ge::GRAPH_FAILED;372 return ge::GRAPH_FAILED;
345 }373 }
346 }374 }
@@ -348,7 +376,8 @@ ge::graphStatus ApplyAdamV2Tiling::GetShapeInfo()
348 auto shape = ctx_->GetOptionalInputShape(11);376 auto shape = ctx_->GetOptionalInputShape(11);
349 auto storageShape = shape->GetStorageShape();377 auto storageShape = shape->GetStorageShape();
350 if (!storageShape.IsScalar() && storageShape.GetShapeSize() != SCALAR_ELEM_SIZE) {378 if (!storageShape.IsScalar() && storageShape.GetShapeSize() != SCALAR_ELEM_SIZE) {
351- OP_LOGE(opName, "step_size must be scalar or size=1");379+ OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON(opName, "step_size", std::to_string(storageShape.GetShapeSize()),
380+ "must be scalar or size=1");
352 return ge::GRAPH_FAILED;381 return ge::GRAPH_FAILED;
353 }382 }
354 }383 }
@@ -356,7 +385,7 @@ ge::graphStatus ApplyAdamV2Tiling::GetShapeInfo()
356 clip_coeff_ = 1.0f;385 clip_coeff_ = 1.0f;
357 386 
358 PadAndSqueeze(raw_input_shapes_, raw_output_shapes_, max_bro_shape_, normal_input_shapes_, normal_output_shapes_);387 PadAndSqueeze(raw_input_shapes_, raw_output_shapes_, max_bro_shape_, normal_input_shapes_, normal_output_shapes_);
359- rank_ = (int64_t)max_bro_shape_.size();388+ rank_ = static_cast<int64_t>(max_bro_shape_.size());
360 389 
361 if (rank_ > kMaxRank) {390 if (rank_ > kMaxRank) {
362 OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(opName, "rank", std::to_string(rank_).c_str(),391 OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(opName, "rank", std::to_string(rank_).c_str(),
@@ -382,19 +411,19 @@ ge::graphStatus ApplyAdamV2Tiling::DoTilingAndSet()
382 auto* tiling = ctx_->GetTilingData<ApplyAdamV2TilingData<R>>();411 auto* tiling = ctx_->GetTilingData<ApplyAdamV2TilingData<R>>();
383 OP_CHECK_NULL_WITH_CONTEXT(ctx_, tiling);412 OP_CHECK_NULL_WITH_CONTEXT(ctx_, tiling);
384 413 
385- int64_t ub_per_core = (int64_t)ub_size_;414+ int64_t ub_per_core = static_cast<int64_t>(ub_size_);
386 int64_t per_buf_bytes = (ub_per_core / kPhysNodes) & ~31LL;415 int64_t per_buf_bytes = (ub_per_core / kPhysNodes) & ~31LL;
387 416 
388 // Use max(dtype_size, 4) for UB split: FP16 path casts to FP32 in UB,417 // Use max(dtype_size, 4) for UB split: FP16 path casts to FP32 in UB,
389 // so each element needs 4 bytes of buffer space after Cast.418 // so each element needs 4 bytes of buffer space after Cast.
390 int64_t compute_dtype_size = (dtype_size_ < 4) ? 4 : dtype_size_;419 int64_t compute_dtype_size = (dtype_size_ < 4) ? 4 : dtype_size_;
391 FindSplitAxis(max_bro_shape_, compute_dtype_size, ub_per_core, kPhysNodes, tiling->split);420 FindSplitAxis(max_bro_shape_, compute_dtype_size, ub_per_core, kPhysNodes, tiling->split);
392- MultiCoreSplit(max_bro_shape_, tiling->split, (int64_t)core_num_, tiling->multicore);421+ MultiCoreSplit(max_bro_shape_, tiling->split, static_cast<int64_t>(core_num_), tiling->multicore);
393 tiling->perBufBytes = per_buf_bytes;422 tiling->perBufBytes = per_buf_bytes;
394 tiling->perBufElems = per_buf_bytes / compute_dtype_size;423 tiling->perBufElems = per_buf_bytes / compute_dtype_size;
395 424 
396- int64_t num_in = (int64_t)normal_input_shapes_.size();425+ int64_t num_in = static_cast<int64_t>(normal_input_shapes_.size());
397- int64_t num_out = (int64_t)normal_output_shapes_.size();426+ int64_t num_out = static_cast<int64_t>(normal_output_shapes_.size());
398 std::vector<std::vector<int64_t>> in_strides(num_in), out_strides(num_out);427 std::vector<std::vector<int64_t>> in_strides(num_in), out_strides(num_out);
399 for (int64_t i = 0; i < num_in; i++)428 for (int64_t i = 0; i < num_in; i++)
400 PrecomputeInputStrides(normal_input_shapes_[i], in_strides[i]);429 PrecomputeInputStrides(normal_input_shapes_[i], in_strides[i]);
@@ -454,6 +483,8 @@ ge::graphStatus ApplyAdamV2Tiling::DoTilingAndSet()
454 483 
455 // ApplyAdamV2 specific fields484 // ApplyAdamV2 specific fields
456 tiling->adamModeFlag = adam_mode_flag_;485 tiling->adamModeFlag = adam_mode_flag_;
486+ // clipCoeff/stepSizeVal/lrVal 为 ABI 预留字段(标量值位于 GM、Host 无法读取,
487+ // clip_coeff 由 kernel 运行时计算;见 tiling_data.h 设计偏差记录)
457 tiling->clipCoeff = clip_coeff_;488 tiling->clipCoeff = clip_coeff_;
458 tiling->hasMaxGradNorm = has_max_grad_norm_ ? 1 : 0;489 tiling->hasMaxGradNorm = has_max_grad_norm_ ? 1 : 0;
459 tiling->hasStepSize = has_step_size_ ? 1 : 0;490 tiling->hasStepSize = has_step_size_ ? 1 : 0;
@@ -468,8 +499,8 @@ ge::graphStatus ApplyAdamV2Tiling::DoTilingAndSet()
468 "split(axis=%ld a_i=%ld a_o=%ld a_i_tail=%ld) "499 "split(axis=%ld a_i=%ld a_o=%ld a_i_tail=%ld) "
469 "multi(cores=%ld tiles=%ld main=%ld core_tail=%ld) num_in=%ld num_out=%ld "500 "multi(cores=%ld tiles=%ld main=%ld core_tail=%ld) num_in=%ld num_out=%ld "
470 "adam_mode=%ld clip_coeff=%f",501 "adam_mode=%ld clip_coeff=%f",
471- tiling->perBufBytes, rank_, (int)R, Arr2String(tiling->maxBroShape, R).c_str(), tiling->split.axis,502+ tiling->perBufBytes, rank_, static_cast<int>(R), Arr2String(tiling->maxBroShape, R).c_str(),
472- tiling->split.aI, tiling->split.aO, tiling->split.aITail, tiling->multicore.numCores,503+ tiling->split.axis, tiling->split.aI, tiling->split.aO, tiling->split.aITail, tiling->multicore.numCores,
473 tiling->multicore.totalTiles, tiling->multicore.tilesMain, tiling->multicore.coresTail, num_in, num_out,504 tiling->multicore.totalTiles, tiling->multicore.tilesMain, tiling->multicore.coresTail, num_in, num_out,
474 tiling->adamModeFlag, tiling->clipCoeff);505 tiling->adamModeFlag, tiling->clipCoeff);
475 506 
@@ -490,15 +521,33 @@ ge::graphStatus ApplyAdamV2Tiling::RunTiling()
490 return ret;521 return ret;
491 }522 }
492 523 
493- int64_t tilingKeyValue = 0;524+ int64_t baseTilingKey = 0;
494 if (dtype_size_ == 4) {525 if (dtype_size_ == 4) {
495- tilingKeyValue = (adam_mode_flag_ == 0) ? APPLY_ADAM_V2_FP32_ADAM : APPLY_ADAM_V2_FP32_MBART;526+ baseTilingKey = (adam_mode_flag_ == 0) ? APPLY_ADAM_V2_FP32_ADAM : APPLY_ADAM_V2_FP32_MBART;
496 } else {527 } else {
497- tilingKeyValue = (adam_mode_flag_ == 0) ? APPLY_ADAM_V2_FP16_ADAM : APPLY_ADAM_V2_FP16_MBART;528+ baseTilingKey = (adam_mode_flag_ == 0) ? APPLY_ADAM_V2_FP16_ADAM : APPLY_ADAM_V2_FP16_MBART;
498 }529 }
499 530 
500- ret = DoTilingAndSet<8>();531+ // Design §3.2: rank <= 4 maps to TilingData<4>, rank > 4 maps to TilingData<8> (rank tier encoded in TilingKey)
501- ctx_->SetTilingKey(GET_TPL_TILING_KEY(tilingKeyValue));532+ if (is_empty_ || rank_ <= 4) {
533+ if (is_empty_) {
534+ // Design §3.4/§5.4: empty tensor → host-side short-circuit, kernel executes zero iterations
535+ auto* tiling = ctx_->GetTilingData<ApplyAdamV2TilingData<4>>();
536+ OP_CHECK_NULL_WITH_CONTEXT(ctx_, tiling);
537+ *tiling = {};
538+ tiling->multicore.numCores = 1;
539+ tiling->split.aO = 1;
540+ tiling->perBufBytes = (static_cast<int64_t>(ub_size_) / kPhysNodes) & ~31LL;
541+ ctx_->SetBlockDim(1);
542+ OP_LOGI(ctx_->GetNodeName(), "ApplyAdamV2 empty tensor short-circuit: blockDim=1, zero tiles");
543+ } else {
544+ ret = DoTilingAndSet<4>();
545+ }
546+ ctx_->SetTilingKey(GET_TPL_TILING_KEY(baseTilingKey));
547+ } else {
548+ ret = DoTilingAndSet<8>();
549+ ctx_->SetTilingKey(GET_TPL_TILING_KEY(baseTilingKey + APPLY_ADAM_V2_RANK8_OFFSET));
550+ }
502 551 
503 return ret;552 return ret;
504}553}
@@ -513,8 +562,10 @@ static ge::graphStatus TilingFuncApplyAdamV2(gert::TilingContext* context)
513 OP_CHECK_NULL_WITH_CONTEXT(context, platformInfo);562 OP_CHECK_NULL_WITH_CONTEXT(context, platformInfo);
514 auto ap = platform_ascendc::PlatformAscendC(platformInfo);563 auto ap = platform_ascendc::PlatformAscendC(platformInfo);
515 uint64_t coreNum = ap.GetCoreNumAiv();564 uint64_t coreNum = ap.GetCoreNumAiv();
565+ OP_CHECK_IF(coreNum == 0, OP_LOGE(context->GetNodeName(), "coreNum is 0"), return ge::GRAPH_FAILED);
516 uint64_t ubSize = 0;566 uint64_t ubSize = 0;
517 ap.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize);567 ap.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize);
568+ OP_CHECK_IF(ubSize == 0, OP_LOGE(context->GetNodeName(), "ubSize is 0"), return ge::GRAPH_FAILED);
518 569 
519 // Read adam_mode attribute (default "adam" → flag=0)570 // Read adam_mode attribute (default "adam" → flag=0)
520 int64_t adamModeFlag = 0;571 int64_t adamModeFlag = 0;
@@ -522,9 +573,9 @@ static ge::graphStatus TilingFuncApplyAdamV2(gert::TilingContext* context)
522 if (attrs != nullptr && attrs->GetAttrNum() > 0) {573 if (attrs != nullptr && attrs->GetAttrNum() > 0) {
523 const char* adamMode = attrs->GetStr(0);574 const char* adamMode = attrs->GetStr(0);
524 if (adamMode != nullptr) {575 if (adamMode != nullptr) {
525- if (strcmp(adamMode, "mbart_adam") == 0) {576+ if (strcmp(adamMode, kAdamModeMbart) == 0) {
526 adamModeFlag = 1;577 adamModeFlag = 1;
527- } else if (strcmp(adamMode, "adam") != 0) {578+ } else if (strcmp(adamMode, kAdamModeAdam) != 0) {
528 OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(context->GetNodeName(), "adam_mode", adamMode,579 OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(context->GetNodeName(), "adam_mode", adamMode,
529 "only adam or mbart_adam allowed");580 "only adam or mbart_adam allowed");
530 return ge::GRAPH_FAILED;581 return ge::GRAPH_FAILED;
@@ -48,6 +48,7 @@ private:
48 float clip_coeff_ = 1.0f; // host-precomputed gradient clipping coefficient48 float clip_coeff_ = 1.0f; // host-precomputed gradient clipping coefficient
49 bool has_max_grad_norm_ = false; // whether max_grad_norm optional input is provided49 bool has_max_grad_norm_ = false; // whether max_grad_norm optional input is provided
50 bool has_step_size_ = false; // whether step_size optional input is provided50 bool has_step_size_ = false; // whether step_size optional input is provided
51+ bool is_empty_ = false; // whether var is an empty tensor (host-side short-circuit)
51 uint64_t core_num_ = 0;52 uint64_t core_num_ = 0;
52 uint64_t ub_size_ = 0;53 uint64_t ub_size_ = 0;
53};54};
@@ -14,6 +14,7 @@
14#include "arch35/apply_adam_v2_tiling_data.h"14#include "arch35/apply_adam_v2_tiling_data.h"
15#include "arch35/apply_adam_v2_struct.h"15#include "arch35/apply_adam_v2_struct.h"
16 16 
17+using TilingData4 = ApplyAdamV2TilingData<4>;
17using TilingData8 = ApplyAdamV2TilingData<8>;18using TilingData8 = ApplyAdamV2TilingData<8>;
18 19 
19template <int KEY>20template <int KEY>
@@ -29,8 +30,15 @@ __global__ __aicore__ void apply_adam_v2(GM_ADDR var, GM_ADDR m, GM_ADDR v, GM_A
29 REGISTER_NONE_TILING;30 REGISTER_NONE_TILING;
30 KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);31 KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);
31 32 
32- GET_TILING_DATA_WITH_STRUCT(TilingData8, td, tiling);33+ if constexpr (KEY < APPLY_ADAM_V2_RANK8_OFFSET) {
33- ApplyAdamV2Kernel<DTYPE_VAR, 8> kernel;34+ GET_TILING_DATA_WITH_STRUCT(TilingData4, td, tiling);
34- kernel.Init(ins, outs, &td);35+ ApplyAdamV2Kernel<DTYPE_VAR, 4> kernel;
35- kernel.Process<KEY>();36+ kernel.Init(ins, outs, &td);
37+ kernel.Process<KEY>();
38+ } else {
39+ GET_TILING_DATA_WITH_STRUCT(TilingData8, td, tiling);
40+ ApplyAdamV2Kernel<DTYPE_VAR, 8> kernel;
41+ kernel.Init(ins, outs, &td);
42+ kernel.Process<KEY - APPLY_ADAM_V2_RANK8_OFFSET>();
43+ }
36}44}
@@ -276,9 +276,9 @@ __simd_vf__ inline void MbartUpdateVF(__ubuf__ T* uAddr, __ubuf__ T* vAddr /* un
276 276 
277template <typename T, int64_t RANK>277template <typename T, int64_t RANK>
278class ApplyAdamV2Kernel {278class ApplyAdamV2Kernel {
279- // NDDMA dimension count (max 5), RANK>5 uses outer loop for flat transport
280- static constexpr int64_t ND = (RANK <= 5) ? RANK : 5;
281 static constexpr uint32_t VL = AscendC::GetVecLen() / sizeof(T);279 static constexpr uint32_t VL = AscendC::GetVecLen() / sizeof(T);
280+ // NDDMA dimension count (max 5); dims beyond ND are covered by the outer loop in CopyInBrc
281+ static constexpr int64_t ND = (RANK <= 5) ? RANK : 5;
282 282 
283 AscendC::TPipe pipe_;283 AscendC::TPipe pipe_;
284 const ApplyAdamV2TilingData<RANK>* td_;284 const ApplyAdamV2TilingData<RANK>* td_;
@@ -308,14 +308,15 @@ public:
308 for (int i = 0; i < kPhysNodes; i++)308 for (int i = 0; i < kPhysNodes; i++)
309 pipe_.InitBuffer(buf_[i], td_->perBufBytes);309 pipe_.InitBuffer(buf_[i], td_->perBufBytes);
310 310 
311- // NDDMA parameter precomputation (following adam kernel.h:116-142)311+ // NDDMA parameter precomputation (Design Contract §8: NDDMA multi-dim CopyIn)
312 const int64_t* dstShape = td_->maxBroShape;312 const int64_t* dstShape = td_->maxBroShape;
313 int64_t k = td_->split.axis;313 int64_t k = td_->split.axis;
314 nddma_dims_ = (RANK - k <= ND) ? (RANK - k) : ND;314 nddma_dims_ = (RANK - k <= ND) ? (RANK - k) : ND;
315+ int64_t ndmaStart = RANK - nddma_dims_;
315 for (int inp = 0; inp < kMaxInputSlots; inp++) {316 for (int inp = 0; inp < kMaxInputSlots; inp++) {
316 int64_t inner = 1;317 int64_t inner = 1;
317 int64_t nd = 0;318 int64_t nd = 0;
318- for (int64_t d = RANK - 1; d >= k && nd < ND; d--) {319+ for (int64_t d = RANK - 1; d >= ndmaStart && nd < ND; d--) {
319 nddmaParams_[inp].loopInfo.loopSize[nd] = (d == k) ? 0 : dstShape[d];320 nddmaParams_[inp].loopInfo.loopSize[nd] = (d == k) ? 0 : dstShape[d];
320 nddmaParams_[inp].loopInfo.loopSrcStride[nd] = td_->inputStrides[inp][d];321 nddmaParams_[inp].loopInfo.loopSrcStride[nd] = td_->inputStrides[inp][d];
321 nddmaParams_[inp].loopInfo.loopDstStride[nd] = inner;322 nddmaParams_[inp].loopInfo.loopDstStride[nd] = inner;
@@ -332,7 +333,7 @@ public:
332 nddmaParams_[inp].loopInfo.loopRpSize[nd] = 0;333 nddmaParams_[inp].loopInfo.loopRpSize[nd] = 0;
333 }334 }
334 nddmaOuterIters_[inp] = 1;335 nddmaOuterIters_[inp] = 1;
335- for (int64_t d = k; d < RANK - nddma_dims_; d++)336+ for (int64_t d = k; d < ndmaStart; d++)
336 nddmaOuterIters_[inp] *= (d == k) ? td_->split.aI : dstShape[d];337 nddmaOuterIters_[inp] *= (d == k) ? td_->split.aI : dstShape[d];
337 }338 }
338 }339 }
@@ -349,7 +350,8 @@ public:
349 int32_t evMTE3toMTE2 = static_cast<int32_t>(GetTPipePtr()->FetchEventID(AscendC::HardEvent::MTE3_MTE2));350 int32_t evMTE3toMTE2 = static_cast<int32_t>(GetTPipePtr()->FetchEventID(AscendC::HardEvent::MTE3_MTE2));
350 int32_t evVtoMTE2 = static_cast<int32_t>(GetTPipePtr()->FetchEventID(AscendC::HardEvent::V_MTE2));351 int32_t evVtoMTE2 = static_cast<int32_t>(GetTPipePtr()->FetchEventID(AscendC::HardEvent::V_MTE2));
351 352 
352- int64_t start, end;353+ int64_t start = 0;
354+ int64_t end = 0;
353 GetCoreRange(AscendC::GetBlockIdx(), td_->multicore.tilesMain, td_->multicore.coresTail, start, end);355 GetCoreRange(AscendC::GetBlockIdx(), td_->multicore.tilesMain, td_->multicore.coresTail, start, end);
354 356 
355 constexpr int IN_LR = 3, IN_BETA1 = 4, IN_BETA2 = 5, IN_EPSILON = 6;357 constexpr int IN_LR = 3, IN_BETA1 = 4, IN_BETA2 = 5, IN_EPSILON = 6;
@@ -372,11 +374,11 @@ public:
372 float maxGradNorm = static_cast<float>(*(gmInPtr_[IN_MAX_GRAD_NORM]));374 float maxGradNorm = static_cast<float>(*(gmInPtr_[IN_MAX_GRAD_NORM]));
373 float globalGradNorm = static_cast<float>(*(gmInPtr_[IN_GLOBAL_GRAD_NORM]));375 float globalGradNorm = static_cast<float>(*(gmInPtr_[IN_GLOBAL_GRAD_NORM]));
374 bool isNaN = (globalGradNorm != globalGradNorm);376 bool isNaN = (globalGradNorm != globalGradNorm);
375- bool isInf = (globalGradNorm > 1e30f || globalGradNorm < -1e30f);377+ bool isInf = (globalGradNorm > kInfApproxThreshold || globalGradNorm < -kInfApproxThreshold);
376 if (isNaN || isInf) {378 if (isNaN || isInf) {
377 clipCoeff = 1.0f;379 clipCoeff = 1.0f;
378 } else {380 } else {
379- if (globalGradNorm < 1e-10f)381+ if (globalGradNorm < kGradNormFloor)
380 globalGradNorm = 1.0f;382 globalGradNorm = 1.0f;
381 if (maxGradNorm > 0.0f && globalGradNorm > 1.0f) {383 if (maxGradNorm > 0.0f && globalGradNorm > 1.0f) {
382 clipCoeff = 1.0f / globalGradNorm;384 clipCoeff = 1.0f / globalGradNorm;
@@ -639,17 +641,38 @@ private:
639 {641 {
640 int64_t k = td_->split.axis;642 int64_t k = td_->split.axis;
641 int64_t off = CalcOffset(coord, td_->inputStrides[inputIdx], RANK);643 int64_t off = CalcOffset(coord, td_->inputStrides[inputIdx], RANK);
644+ const int64_t* dstShape = td_->maxBroShape;
645+ int64_t ndmaStart = RANK - nddma_dims_;
642 646 
643- // Compute total contiguous element count for this tile647+ auto params = nddmaParams_[inputIdx];
644- int64_t inner_count = 1;648+ int64_t k_nd = RANK - 1 - k;
645- for (int64_t d = k + 1; d < RANK; d++)649+ int64_t inner = 1;
646- inner_count *= td_->maxBroShape[d];650+ for (int64_t nd = 0; nd < ND; nd++) {
647- int64_t count = a_i_seg * inner_count;651+ if (nd == k_nd)
652+ params.loopInfo.loopSize[nd] = a_i_seg;
653+ params.loopInfo.loopDstStride[nd] = inner;
654+ inner *= params.loopInfo.loopSize[nd];
655+ }
648 656 
649- // Simple 1D DataCopyPad (same pattern as working probe)657+ static constexpr AscendC::NdDmaConfig cfg = {false, AscendC::NdDmaConfig::unsetPad,
650- AscendC::LocalTensor<T> bufLocal = buf_[slot].template Get<T>();658+ AscendC::NdDmaConfig::unsetPad, false};
651- AscendC::DataCopyPad(bufLocal, gmIn_[inputIdx][off], {1, static_cast<uint16_t>(count * sizeof(T)), 0, 0},659+ 
652- {false, 0, 0, 0});660+ if constexpr (RANK <= 5) {
661+ AscendC::DataCopy<T, ND, cfg>(buf_[slot].template Get<T>(), gmIn_[inputIdx][off], params);
662+ } else {
663+ AscendC::LocalTensor<T> buf = buf_[slot].template Get<T>();
664+ int64_t elemBase = off;
665+ for (int64_t oi = 0; oi < nddmaOuterIters_[inputIdx]; oi++) {
666+ int64_t elemAdj = 0;
667+ int64_t tmp = oi;
668+ for (int64_t d = ndmaStart - 1; d >= k; d--) {
669+ int64_t sz = (d == k) ? a_i_seg : dstShape[d];
670+ elemAdj += (tmp % sz) * td_->inputStrides[inputIdx][d];
671+ tmp /= sz;
672+ }
673+ AscendC::DataCopy<T, ND, cfg>(buf[oi * inner], gmIn_[inputIdx][elemBase + elemAdj], params);
674+ }
675+ }
653 }676 }
654 677 
655 __aicore__ inline void CopyOutOne(const int64_t* coord, int outputIdx, int slot, int64_t a_i_seg)678 __aicore__ inline void CopyOutOne(const int64_t* coord, int outputIdx, int slot, int64_t a_i_seg)
@@ -10,7 +10,8 @@
10/* Generated By CANNBot */10/* Generated By CANNBot */
11 11 
12// ApplyAdamV2 TilingKey template parameters12// ApplyAdamV2 TilingKey template parameters
13-// Key encoding: dtype x adam_mode13+// Key encoding: rank tier x dtype x adam_mode
14+// Key 0~3 = rank <= 4 (TilingData<4>), Key 4~7 = rank > 4 (TilingData<8>)
14// Key 0 = FP32 + adam, Key 1 = FP32 + mbart_adam15// Key 0 = FP32 + adam, Key 1 = FP32 + mbart_adam
15// Key 2 = FP16 + adam, Key 3 = FP16 + mbart_adam16// Key 2 = FP16 + adam, Key 3 = FP16 + mbart_adam
16#ifndef APPLY_ADAM_V2_STRUCT_H_17#ifndef APPLY_ADAM_V2_STRUCT_H_
@@ -22,14 +23,25 @@
22#define APPLY_ADAM_V2_FP32_MBART 123#define APPLY_ADAM_V2_FP32_MBART 1
23#define APPLY_ADAM_V2_FP16_ADAM 224#define APPLY_ADAM_V2_FP16_ADAM 2
24#define APPLY_ADAM_V2_FP16_MBART 325#define APPLY_ADAM_V2_FP16_MBART 3
26+#define APPLY_ADAM_V2_RANK8_FP32_ADAM 4
27+#define APPLY_ADAM_V2_RANK8_FP32_MBART 5
28+#define APPLY_ADAM_V2_RANK8_FP16_ADAM 6
29+#define APPLY_ADAM_V2_RANK8_FP16_MBART 7
30+#define APPLY_ADAM_V2_RANK8_OFFSET 4
25 31 
26-ASCENDC_TPL_ARGS_DECL(ApplyAdamV2, ASCENDC_TPL_UINT_DECL(KEY, 8, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_FP32_ADAM,32+ASCENDC_TPL_ARGS_DECL(ApplyAdamV2,
27- APPLY_ADAM_V2_FP32_MBART, APPLY_ADAM_V2_FP16_ADAM,33+ ASCENDC_TPL_UINT_DECL(KEY, 8, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_FP32_ADAM,
28- APPLY_ADAM_V2_FP16_MBART));34+ APPLY_ADAM_V2_FP32_MBART, APPLY_ADAM_V2_FP16_ADAM, APPLY_ADAM_V2_FP16_MBART,
35+ APPLY_ADAM_V2_RANK8_FP32_ADAM, APPLY_ADAM_V2_RANK8_FP32_MBART,
36+ APPLY_ADAM_V2_RANK8_FP16_ADAM, APPLY_ADAM_V2_RANK8_FP16_MBART));
29 37 
30ASCENDC_TPL_SEL(ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(KEY, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_FP32_ADAM)),38ASCENDC_TPL_SEL(ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(KEY, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_FP32_ADAM)),
31 ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(KEY, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_FP32_MBART)),39 ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(KEY, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_FP32_MBART)),
32 ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(KEY, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_FP16_ADAM)),40 ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(KEY, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_FP16_ADAM)),
33- ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(KEY, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_FP16_MBART)));41+ ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(KEY, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_FP16_MBART)),
42+ ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(KEY, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_RANK8_FP32_ADAM)),
43+ ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(KEY, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_RANK8_FP32_MBART)),
44+ ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(KEY, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_RANK8_FP16_ADAM)),
45+ ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(KEY, ASCENDC_TPL_UI_LIST, APPLY_ADAM_V2_RANK8_FP16_MBART)));
34 46 
35#endif // APPLY_ADAM_V2_STRUCT_H_47#endif // APPLY_ADAM_V2_STRUCT_H_
@@ -19,7 +19,12 @@ constexpr int64_t
19constexpr int64_t kMaxOutputSlots = 3; // max outputs (var_out, m_out, v_out)19constexpr int64_t kMaxOutputSlots = 3; // max outputs (var_out, m_out, v_out)
20constexpr int64_t kPhysNodes = 5; // physical survival nodes P (= TBuf slots)20constexpr int64_t kPhysNodes = 5; // physical survival nodes P (= TBuf slots)
21constexpr int64_t kMaxRank = 8; // Maximum supported tensor rank21constexpr int64_t kMaxRank = 8; // Maximum supported tensor rank
22-constexpr int64_t kMaxNDDMA = 5; // Maximum NDDMA loop dimensions22+ 
23+// === Named constants (G-4.2: no magic numbers/strings) ===
24+constexpr const char* kAdamModeAdam = "adam";
25+constexpr const char* kAdamModeMbart = "mbart_adam";
26+constexpr float kInfApproxThreshold = 1e30f; // |global_grad_norm| beyond this is treated as Inf
27+constexpr float kGradNormFloor = 1e-10f; // global_grad_norm below this is clamped to 1.0
23 28 
24struct SplitResult {29struct SplitResult {
25 int64_t axis;30 int64_t axis;
@@ -49,6 +54,11 @@ struct ApplyAdamV2TilingData {
49 int64_t outputShapes[kMaxOutputSlots][kRank];54 int64_t outputShapes[kMaxOutputSlots][kRank];
50 int64_t outputStrides[kMaxOutputSlots][kRank];55 int64_t outputStrides[kMaxOutputSlots][kRank];
51 int64_t adamModeFlag;56 int64_t adamModeFlag;
57+ // === Design §3.4 S1 决策偏差记录(ABI 预留字段)===
58+ // 设计原定 Host 预计算 clip_coeff、kernel 无条件 Muls;但 max_grad_norm/global_grad_norm/lr/step_size
59+ // 均为 GM 标量张量,Host 编译期仅可见 shape/dtype,拿不到运行期数值(self-check.md 已证),
60+ // 动态 shape 场景下 Host 预计算不可行。故 clip_coeff 保留 kernel 运行时计算(含 NaN/Inf/1e-10 下限保护,
61+ // 语义与 spec/golden 一致),以下三字段仅作 ABI 占位,Host 不写入有效值:
chenqi317
chenqi317chenqi317Oct 18

注释写“以下三字段仅作 ABI 占位,Host 不写入有效值”,但紧随其后的三个字段是 clipCoeff、hasMaxGradNorm、hasStepSize:其中 hasMaxGradNorm/hasStepSize 由 host 写入有效值(apply_adam_v2_tiling_arch35.cpp:489-490)并被 kernel 用于决定是否读取 max_grad_norm/step_size 以及 mbart 模式下用 lr 兜底(apply_adam_v2_kernel.h:373、392-396),真正的三个占位字段是 clipCoeff/stepSizeVal/lrVal。后果:维护者按注释清理“占位字段”会直接破坏梯度裁剪与 mbart 步长逻辑,且该结构同时是 host/kernel ABI,删除会引入静默错位。建议:把 clipCoeff/stepSizeVal/lrVal 归并到一处并注释为占位字段,hasMaxGradNorm/hasStepSize 单独说明其真实语义。


如已修复请回复说明,我会在确认后把该意见标记为已解决。

likedislike
52 float clipCoeff;62 float clipCoeff;
53 int64_t hasMaxGradNorm;63 int64_t hasMaxGradNorm;
54 int64_t hasStepSize;64 int64_t hasStepSize;
@@ -1,8 +1,19 @@
1- 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+# Generated By CANNBot
11+#
2# InferShape UT:独立编译,不依赖 CANN faker 框架12# InferShape UT:独立编译,不依赖 CANN faker 框架
3add_executable(test_apply_adam_v2_infershape test_apply_adam_v2_infershape.cpp)13add_executable(test_apply_adam_v2_infershape test_apply_adam_v2_infershape.cpp)
chenqi317
chenqi317chenqi317Oct 21

这里用 add_executable/add_test 创建独立测试可执行文件,未接入仓内标准 UT 框架(同目录兄弟 PR 11222 用 add_modules_ut_sources 接入 nn_op_host_ut)。后果:该 infershape UT 不会被仓级 nn_op_host_ut 收集,CI 的 op_host_ut 门禁不会执行它,测试形同虚设。建议改为 add_modules_ut_sources(HOSTNAME ${OP_INFERSHAPE_MODULE_NAME} ...) 的标准写法。

likedislike
4target_compile_features(test_apply_adam_v2_infershape PRIVATE cxx_std_17)14target_compile_features(test_apply_adam_v2_infershape PRIVATE cxx_std_17)
5-target_compile_options(test_apply_adam_v2_infershape PRIVATE -Wall -Wextra)15+target_compile_options(test_apply_adam_v2_infershape PRIVATE -Wall -Wextra -fPIE -fstack-protector-all)
16+target_link_options(test_apply_adam_v2_infershape PRIVATE -pie -Wl,-z,relro,-z,now,-z,noexecstack -s)
6add_test(NAME test_apply_adam_v2_infershape COMMAND test_apply_adam_v2_infershape)17add_test(NAME test_apply_adam_v2_infershape COMMAND test_apply_adam_v2_infershape)
7 18 
8# 递归包含 arch35 子目录19# 递归包含 arch35 子目录
@@ -1,6 +1,17 @@
1- 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+# Generated By CANNBot
11+#
2# Tiling UT (arch35):独立编译,不依赖 CANN faker 框架12# Tiling UT (arch35):独立编译,不依赖 CANN faker 框架
3add_executable(test_apply_adam_v2_tiling_arch35 test_apply_adam_v2_tiling_arch35.cpp)13add_executable(test_apply_adam_v2_tiling_arch35 test_apply_adam_v2_tiling_arch35.cpp)
4target_compile_features(test_apply_adam_v2_tiling_arch35 PRIVATE cxx_std_17)14target_compile_features(test_apply_adam_v2_tiling_arch35 PRIVATE cxx_std_17)
5-target_compile_options(test_apply_adam_v2_tiling_arch35 PRIVATE -Wall -Wextra)15+target_compile_options(test_apply_adam_v2_tiling_arch35 PRIVATE -Wall -Wextra -fPIE -fstack-protector-all)
16+target_link_options(test_apply_adam_v2_tiling_arch35 PRIVATE -pie -Wl,-z,relro,-z,now,-z,noexecstack -s)
6add_test(NAME test_apply_adam_v2_tiling_arch35 COMMAND test_apply_adam_v2_tiling_arch35)17add_test(NAME test_apply_adam_v2_tiling_arch35 COMMAND test_apply_adam_v2_tiling_arch35)