已合并
修改aicpu算子的opgen自动生成算子工程的脚本及样例 #1098
Hou_jialin创建于 2月3日
修改aicpu算子的opgen自动生成算子工程的脚本及样例 #1098
已合并
Hou_jialin创建于 2月3日
14 个文件变更+31-82
MCMakeLists.txt+7-2
@@ -103,8 +103,13 @@ else()
103 endforeach()103 endforeach()
104endif()104endif()
105 105 
106-if("${ASCEND_OP_NAME}" STREQUAL "add_example")106+set(_orig_ops "${ASCEND_OP_NAME}")
107- add_subdirectory(examples)107+list(LENGTH _orig_ops _orig_len)
108+set(_filtered_ops "${_orig_ops}")
109+list(FILTER _filtered_ops INCLUDE REGEX "add_example")
110+list(LENGTH _filtered_ops _filtered_len)
111+if(_filtered_len EQUAL _orig_len)
112+ add_subdirectory(examples)
108endif()113endif()
109 114 
110if(ENABLE_TEST)115if(ENABLE_TEST)
Mexamples/add_example_aicpu/examples/test_aclnn_add_example.cpp+2-2
@@ -11,7 +11,7 @@
11#include <iostream>11#include <iostream>
12#include <vector>12#include <vector>
13#include "acl/acl.h"13#include "acl/acl.h"
14-#include "aclnn_add_example_aicpu.h"14+#include "aclnn_add_example.h"
15 15 
16#define CHECK_RET(cond, return_expr) \16#define CHECK_RET(cond, return_expr) \
17 do { \17 do { \
@@ -43,7 +43,7 @@ void PrintOutResult(std::vector<int64_t>& shape, void** deviceAddr)
43 ACL_MEMCPY_DEVICE_TO_HOST);43 ACL_MEMCPY_DEVICE_TO_HOST);
44 CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return);44 CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return);
45 for (int64_t i = 0; i < size; i++) {45 for (int64_t i = 0; i < size; i++) {
46- LOG_PRINT("mean result[%ld] is: %f\n", i, resultData[i]);46+ LOG_PRINT("add_example result[%ld] is: %f\n", i, resultData[i]);
47 }47 }
48}48}
49 49 
Mexamples/add_example_aicpu/examples/test_geir_add_example.cpp+2-4
@@ -26,9 +26,7 @@
26#include "array_ops.h"26#include "array_ops.h"
27#include "ge_ir_build.h"27#include "ge_ir_build.h"
28 28 
29-#include "experiment_ops.h"29+#include "../op_graph/add_example_proto.h"
30-#include "nn_other.h"
31-#include "../op_graph/add_example_aicpu_proto.h"
32 30 
33#define FAILED -131#define FAILED -1
34#define SUCCESS 032#define SUCCESS 0
@@ -179,7 +177,7 @@ int CreateOppInGraph(DataType inDtype, std::vector<ge::Tensor> &input, std::vect
179{177{
180 Status ret = SUCCESS;178 Status ret = SUCCESS;
181 // 自定义代码:添加单算子定义到图中179 // 自定义代码:添加单算子定义到图中
182- auto add1 = op::AddExample("add1");180+ auto add1 = op::AddExampleAicpu("add1");
183 std::vector<int64_t> xShape = {32,4,4,4};181 std::vector<int64_t> xShape = {32,4,4,4};
184 ADD_INPUT(1, x1, inDtype, xShape);182 ADD_INPUT(1, x1, inDtype, xShape);
185 ADD_INPUT(2, x2, inDtype, xShape);183 ADD_INPUT(2, x2, inDtype, xShape);
Mexamples/add_example_aicpu/op_graph/add_example_graph_infer.cpp+1-1
@@ -33,6 +33,6 @@ static ge::graphStatus InferDataTypeAddExample(gert::InferDataTypeContext* conte
33 return GRAPH_SUCCESS;33 return GRAPH_SUCCESS;
34}34}
35 35 
36-IMPL_OP(AddExample).InferDataType(InferDataTypeAddExample);36+IMPL_OP(AddExampleAicpu).InferDataType(InferDataTypeAddExample);
CANN-robot
CANN-robotCANN-robot2月3日

代码结构与可维护性: IMPL_OP宏调用的操作符名称从'AddExample'修改为'AddExampleAicpu',但对应的InferDataType函数名称仍为'InferDataTypeAddExample',函数命名与操作符名称不一致。虽然这不会导致编译或运行时错误,但会影响代码的可读性和维护性。当其他开发者查看代码时,可能会困惑于函数命名与操作符名称的不匹配。

问题类型: 代码结构与可维护性 文件路径: examples/add_example_aicpu/op_graph/add_example_graph_infer.cpp 行号: 36 问题代码:

IMPL_OP(AddExampleAicpu).InferDataType(InferDataTypeAddExample);

修改建议:

建议将InferDataType函数名称同步修改为'InferDataTypeAddExampleAicpu',以保持命名一致性。或者,如果'AddExampleAicpu'是'AddExample'的别名或特定实现,应在函数注释中明确说明这种关系。修改后的代码应为:IMPL_OP(AddExampleAicpu).InferDataType(InferDataTypeAddExampleAicpu); 同时需要更新函数定义处的名称。

此评论由代码审查工具自动生成

likedislike
Hou_jialin
Hou_jialin
2月3日 评论:
37 37 
38}; // namespace ops38}; // namespace ops
Mexamples/add_example_aicpu/op_graph/add_example_proto.h+2-2
@@ -32,11 +32,11 @@ namespace ge {
32*@par Third-party framework compatibility32*@par Third-party framework compatibility
33*Compatible with the TensorFlow operator Add.33*Compatible with the TensorFlow operator Add.
34*/34*/
35-REG_OP(AddExample)35+REG_OP(AddExampleAicpu)
36 .INPUT(x1, TensorType({DT_FLOAT, DT_INT32}))36 .INPUT(x1, TensorType({DT_FLOAT, DT_INT32}))
37 .INPUT(x2, TensorType({DT_FLOAT, DT_INT32}))37 .INPUT(x2, TensorType({DT_FLOAT, DT_INT32}))
38 .OUTPUT(y, TensorType({DT_FLOAT, DT_INT32}))38 .OUTPUT(y, TensorType({DT_FLOAT, DT_INT32}))
39- .OP_END_FACTORY_REG(AddExample)39+ .OP_END_FACTORY_REG(AddExampleAicpu)
40 40 
41} // namespace ge41} // namespace ge
42 42 
Dexamples/add_example_aicpu/op_host/add_example_def.cpp+0-54
@@ -1,54 +0,0 @@
1- 
2-/**
3- * Copyright (c) 2025 Huawei Technologies Co., Ltd.
4- * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5- * CANN Open Software License Agreement Version 2.0 (the "License").
6- * Please refer to the License for details. You may not use this file except in compliance with the License.
7- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8- * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9- * See LICENSE in the root of the software repository for the full text of the License.
10- */
11- 
12-/*!
13- * \file add_example_aicpu_def.cpp
14- * \brief
15- */
16-#include "register/op_def_registry.h"
17- 
18-namespace ops {
19-class AddExample : public OpDef {
20-public:
21- explicit AddExample(const char* name) : OpDef(name)
22- {
23- this->Input("x1") // 输入x1定义
24- .ParamType(REQUIRED) // 必选输入
25- .DataType({ge::DT_FLOAT, ge::DT_INT32}) // 支持数据类型
26- .Format({ge::FORMAT_ND, ge::FORMAT_ND}) // 支持format格式
27- .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND}) // 未确定大小shape对应format格式
28- .AutoContiguous(); // 内存自动连续化
29- this->Input("x2") // 输入x2定义
30- .ParamType(REQUIRED)
31- .DataType({ge::DT_FLOAT, ge::DT_INT32})
32- .Format({ge::FORMAT_ND, ge::FORMAT_ND})
33- .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND})
34- .AutoContiguous();
35- this->Output("y") // 输出y定义
36- .ParamType(REQUIRED)
37- .DataType({ge::DT_FLOAT, ge::DT_INT32})
38- .Format({ge::FORMAT_ND, ge::FORMAT_ND})
39- .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND})
40- .AutoContiguous();
41- 
42- OpAICpuConfig aicpuConfig;
43- aicpuConfig.DynamicCompileStaticFlag(true)
44- .DynamicFormatFlag(false)
45- .DynamicRankSupportFlag(true)
46- .DynamicShapeSupportFlag(true)
47- .NeedCheckSupportFlag(false)
48- .PrecisionReduceFlag(true)
49- .ExtendCfgInfo("opFile.value", "add_example_aicpu"); // 这里制定的值会对应到kernel入口文件名.cpp
50- this->AICpu().AddConfig("ascend910b", aicpuConfig); // 其他的soc版本补充部分配置项
51- }
52-};
53-OP_ADD(AddExample); // 添加算子信息库
54-} // namespace ops
Mexamples/add_example_aicpu/op_host/add_example_infershape.cpp+1-1
@@ -44,5 +44,5 @@ static ge::graphStatus InferShapeAddExample(gert::InferShapeContext* context)
44 return GRAPH_SUCCESS;44 return GRAPH_SUCCESS;
45}45}
46 46 
47-IMPL_OP_INFERSHAPE(AddExample).InferShape(InferShapeAddExample);47+IMPL_OP_INFERSHAPE(AddExampleAicpu).InferShape(InferShapeAddExample);
48} // namespace ops48} // namespace ops
Mexamples/add_example_aicpu/op_kernel_aicpu/add_example.json+1-1
@@ -1,5 +1,5 @@
1{1{
2- "AddExample":{2+ "AddExampleAicpu":{
3 "opInfo":{3 "opInfo":{
4 "computeCost":"100",4 "computeCost":"100",
5 "engine":"DNN_VM_AICPU",5 "engine":"DNN_VM_AICPU",
Mexamples/add_example_aicpu/op_kernel_aicpu/add_example_aicpu.cpp+2-2
@@ -19,10 +19,10 @@
19 19 
20#include <cmath>20#include <cmath>
21#include <string>21#include <string>
22-#include "cust_cpu_utils.h"22+#include "log.h"
23 23 
24namespace {24namespace {
25-const char* const kAddExample = "AddExample";25+const char* const kAddExample = "AddExampleAicpu";
26const uint32_t kFirstInputIndex = 0;26const uint32_t kFirstInputIndex = 0;
27const uint32_t kSecondInputIndex = 1;27const uint32_t kSecondInputIndex = 1;
28const uint32_t kFirstOutputIndex = 0;28const uint32_t kFirstOutputIndex = 0;
Mexamples/add_example_aicpu/tests/ut/op_kernel_aicpu/test_add_example.cpp+1-1
@@ -29,7 +29,7 @@ class TEST_AddExample_UT : public testing::Test {};
29 29 
30#define CREATE_NODEDEF(shapes, data_types, datas) \30#define CREATE_NODEDEF(shapes, data_types, datas) \
31 auto node_def = CpuKernelUtils::CpuKernelUtils::CreateNodeDef(); \31 auto node_def = CpuKernelUtils::CpuKernelUtils::CreateNodeDef(); \
32- NodeDefBuilder(node_def.get(), "AddExample", "AddExample") \32+ NodeDefBuilder(node_def.get(), "AddExampleAicpu", "AddExampleAicpu") \
33 .Input({"x1", data_types[0], shapes[0], datas[0]}) \33 .Input({"x1", data_types[0], shapes[0], datas[0]}) \
34 .Input({"x2", data_types[1], shapes[1], datas[1]}) \34 .Input({"x2", data_types[1], shapes[1], datas[1]}) \
35 .Output({"y", data_types[2], shapes[2], datas[2]});35 .Output({"y", data_types[2], shapes[2], datas[2]});
Mscripts/opgen/template/add_example_aicpu/op_host/add_example_infershape.cpp+10-10
@@ -10,7 +10,7 @@
10 10 
11 11 
12/*!12/*!
13- * \file add_example_infer.cpp13+ * \file add_example_infershape.cpp
14 * \brief14 * \brief
15 */15 */
16#include "register/op_impl_registry.h"16#include "register/op_impl_registry.h"
@@ -22,27 +22,27 @@ namespace ops {
22 22 
23static ge::graphStatus InferShapeAddExample(gert::InferShapeContext* context)23static ge::graphStatus InferShapeAddExample(gert::InferShapeContext* context)
24{24{
25- OP_LOGD(context->GetNodeName(), "Begin to do InferShapeAddExample");25+ OP_LOGD(context->GetNodeName(), "Begin to do InferShapeAddExample for aicpu");
26 26 
27- // get input shapes27+ // get input shapes for aicpu example
28 const gert::Shape* xShape = context->GetInputShape(IDX_0);28 const gert::Shape* xShape = context->GetInputShape(IDX_0);
29 OP_CHECK_NULL_WITH_CONTEXT(context, xShape);29 OP_CHECK_NULL_WITH_CONTEXT(context, xShape);
30 30 
31- // get output shapes31+ // get output shapes for aicpu example
32 gert::Shape* yShape = context->GetOutputShape(IDX_0);32 gert::Shape* yShape = context->GetOutputShape(IDX_0);
33 OP_CHECK_NULL_WITH_CONTEXT(context, yShape);33 OP_CHECK_NULL_WITH_CONTEXT(context, yShape);
34 34 
35- // 填充输出shape大小35+ // 填充输出shape大小(aicpu example)
36- auto xShapeSize = xShape->GetDimNum();36+ auto xAicpuShapeSize = xShape->GetDimNum();
37- yShape->SetDimNum(xShapeSize);37+ yShape->SetDimNum(xAicpuShapeSize);
38- for (size_t i = 0; i < xShapeSize; i++) {38+ for (size_t i = 0; i < xAicpuShapeSize; i++) {
39 int64_t dim = xShape->GetDim(i);39 int64_t dim = xShape->GetDim(i);
40 yShape->SetDim(i, dim);40 yShape->SetDim(i, dim);
41 }41 }
42 42 
43- OP_LOGD(context->GetNodeName(), "End to do InferShapeAddExample");43+ OP_LOGD(context->GetNodeName(), "End to do InferShapeAddExample for aicpu example");
CANN-robot
CANN-robotCANN-robot2月3日

代码结构与可维护性: 日志消息 'End to do InferShapeAddExample for aicpu example' 中的 'aicpu example' 与第26行的 'aicpu' 不一致,存在冗余和拼写不一致的问题('aicpu' vs 'aicpu example')。这种不一致的日志格式会影响日志的可读性和后续的日志分析。

问题类型: 代码结构与可维护性 文件路径: scripts/opgen/template/add_example_aicpu/op_host/add_example_infershape.cpp 行号: 43 问题代码:

OP_LOGD(context->GetNodeName(), "End to do InferShapeAddExample for aicpu example");

修改建议:

统一日志消息中的标识符。建议将第43行的日志消息修改为与第26行一致,例如:"End to do InferShapeAddExample for aicpu"。或者,如果 'aicpu example' 是特定于某个示例的标识,应考虑其必要性,可能简化为更通用的 'AddExample operator'。

此评论由代码审查工具自动生成

likedislike
Hou_jialin
Hou_jialin
2月3日 评论:
44 return GRAPH_SUCCESS;44 return GRAPH_SUCCESS;
45}45}
46 46 
47-IMPL_OP_INFERSHAPE(AddExample).InferShape(InferShapeAddExample);47+IMPL_OP_INFERSHAPE(AddExampleAicpu).InferShape(InferShapeAddExample);
48} // namespace ops48} // namespace ops
Mscripts/opgen/template/add_example_aicpu/op_kernel_aicpu/add_example.json+1-1
@@ -1,5 +1,5 @@
1{1{
2- "AddExample":{2+ "AddExampleAicpu":{
3 "opInfo":{3 "opInfo":{
4 "computeCost":"100",4 "computeCost":"100",
5 "engine":"DNN_VM_AICPU",5 "engine":"DNN_VM_AICPU",
Mscripts/opgen/template/add_example_aicpu/op_kernel_aicpu/add_example_aicpu.cpp+1-1
@@ -15,7 +15,7 @@
15#include "cust_cpu_utils.h"15#include "cust_cpu_utils.h"
16 16 
17namespace {17namespace {
18-const char* const kAddExample = "AddExample";18+const char* const kAddExample = "AddExampleAicpu";
19const uint32_t kFirstInputIndex = 0;19const uint32_t kFirstInputIndex = 0;
20const uint32_t kSecondInputIndex = 1;20const uint32_t kSecondInputIndex = 1;
21const uint32_t kSuccess = 0;21const uint32_t kSuccess = 0;
Ascripts/opgen/template/add_example_aicpu/tests/ut/.gitkeep+0-0
The file is empty