已合并
fix: 修复 div/mod 类算子多项代码缺陷 #2111
zhang-song-rui创建于 4月7日
fix: 修复 div/mod 类算子多项代码缺陷 #2111
已合并
zhang-song-rui创建于 4月7日
9 个文件变更+92-11
@@ -144,13 +144,12 @@ ge::graphStatus AddcdivTiling::GetPlatformInfo()
144 144 
145ge::graphStatus TilingForAddcdiv(gert::TilingContext* context)145ge::graphStatus TilingForAddcdiv(gert::TilingContext* context)
146{146{
147- auto nodeName = context->GetNodeName();147+ OP_LOGD("AddcdivTiling", "Enter TilingForAddcdiv");
CANN-robot
CANN-robotCANN-robot4月7日
空指针检查: 在检查context指针为空之前,调用了context->GetNodeName()方法获取nodeName变量。如果context为nullptr,解引用空指针会导致未定义行为(通常是程序崩溃)。虽然第148行添加了空指针检查,但第147行已经提前使用了context指针。
问题类型: 空指针检查
文件路径: math/addcdiv/op_host/arch35/addcdiv_tiling_arch35.cpp
行号: 147
问题代码:
    OP_LOGD("AddcdivTiling", "Enter TilingForAddcdiv");
    if (context == nullptr) {
        OP_LOGE("AddcdivTiling", "Tiling context is nullptr");
        return ge::GRAPH_FAILED;
    }
    auto nodeName = context->GetNodeName();
修改建议:
将获取nodeName的代码移到空指针检查之后,确保在安全访问context指针后才调用其成员函数。修改后的代码顺序应为:先记录日志(使用固定字符串),然后检查context是否为空,如果非空再获取nodeName。
---
此评论由代码审查工具自动生成
likedislike
zhang-song-rui
zhang-song-rui
4月8日 评论:
148- OP_LOGD(nodeName, "Enter TilingForAddcdiv");
149 if (context == nullptr) {148 if (context == nullptr) {
150- OP_LOGE(nodeName, "Tiling context is nullptr");149+ OP_LOGE("AddcdivTiling", "Tiling context is nullptr");
151 return ge::GRAPH_FAILED;150 return ge::GRAPH_FAILED;
152 }151 }
153- 152+ auto nodeName = context->GetNodeName();
154 auto compileInfo = reinterpret_cast<const AddcdivCompileInfo*>(context->GetCompileInfo());153 auto compileInfo = reinterpret_cast<const AddcdivCompileInfo*>(context->GetCompileInfo());
155 OP_CHECK_NULL_WITH_CONTEXT(context, compileInfo);154 OP_CHECK_NULL_WITH_CONTEXT(context, compileInfo);
156 OP_LOGD(nodeName, "Enter ascendc AddcdivTiling");155 OP_LOGD(nodeName, "Enter ascendc AddcdivTiling");
@@ -458,7 +458,7 @@ static aclnnStatus HandleNotMixDataTypeDiv(
458 458 
459 // 处理other输入459 // 处理other输入
460 const aclTensor* otherProcessed = nullptr;460 const aclTensor* otherProcessed = nullptr;
461- if (other->GetDataType() == promoteType && l0op::IsRealDivSupportNonContiguous(self)) {461+ if (other->GetDataType() == promoteType && l0op::IsRealDivSupportNonContiguous(other)) {
S
Ssun-yibo14月9日

确定要改成other?

likedislike
zhang-song-rui
zhang-song-rui
4月9日 评论:
462 otherProcessed = executor->CreateView(462 otherProcessed = executor->CreateView(
463 other, other->GetViewShape(), other->GetStorageShape(), other->GetViewStrides(), other->GetViewOffset());463 other, other->GetViewShape(), other->GetStorageShape(), other->GetViewStrides(), other->GetViewOffset());
464 } else {464 } else {
@@ -97,10 +97,8 @@ const aclTensor* Div(const aclTensor* self, const aclTensor* other, aclOpExecuto
97 auto divOut = executor->AllocTensor(broadcastShape, self->GetDataType());97 auto divOut = executor->AllocTensor(broadcastShape, self->GetDataType());
98 if (IsAiCoreSupport(self)) {98 if (IsAiCoreSupport(self)) {
99 return DivAiCore(self, other, divOut, executor);99 return DivAiCore(self, other, divOut, executor);
100- } else {
101- return DivAiCpu(self, other, divOut, executor);
102 }100 }
103- return divOut;101+ return DivAiCpu(self, other, divOut, executor);
Z
Zzhangzijie4月9日
已过期

一个函数必须要有return

likedislike
zhang-song-rui
zhang-song-rui
4月9日 评论:
104}102}
105 103 
106} // namespace l0op104} // namespace l0op
@@ -191,6 +191,30 @@ TEST_F(l2_div_test, case_NonContiguous)
191 EXPECT_EQ(aclRet, ACL_SUCCESS);191 EXPECT_EQ(aclRet, ACL_SUCCESS);
192}192}
193 193 
194+TEST_F(l2_div_test, case_NonContiguous_other_only)
195+{
196+ auto self_tensor_desc = TensorDesc({5, 4}, ACL_FLOAT, ACL_FORMAT_ND).ValueRange(-2, 2);
197+ auto other_tensor_desc = TensorDesc({5, 4}, ACL_FLOAT, ACL_FORMAT_ND, {1, 5}, 0, {4, 5}).ValueRange(-2, 2);
198+ auto out_tensor_desc = TensorDesc({5, 4}, ACL_FLOAT, ACL_FORMAT_ND).Precision(0.0001, 0.0001);
199+ 
200+ auto ut = OP_API_UT(aclnnDiv, INPUT(self_tensor_desc, other_tensor_desc), OUTPUT(out_tensor_desc));
201+ uint64_t workspace_size = 0;
202+ aclnnStatus aclRet = ut.TestGetWorkspaceSize(&workspace_size);
203+ EXPECT_EQ(aclRet, ACL_SUCCESS);
204+}
205+ 
206+TEST_F(l2_div_test, case_NonContiguous_self_only)
207+{
208+ auto self_tensor_desc = TensorDesc({5, 4}, ACL_FLOAT, ACL_FORMAT_ND, {1, 5}, 0, {4, 5}).ValueRange(-2, 2);
209+ auto other_tensor_desc = TensorDesc({5, 4}, ACL_FLOAT, ACL_FORMAT_ND).ValueRange(-2, 2);
210+ auto out_tensor_desc = TensorDesc({5, 4}, ACL_FLOAT, ACL_FORMAT_ND).Precision(0.0001, 0.0001);
211+ 
212+ auto ut = OP_API_UT(aclnnDiv, INPUT(self_tensor_desc, other_tensor_desc), OUTPUT(out_tensor_desc));
213+ uint64_t workspace_size = 0;
214+ aclnnStatus aclRet = ut.TestGetWorkspaceSize(&workspace_size);
215+ EXPECT_EQ(aclRet, ACL_SUCCESS);
216+}
217+ 
194// 测试不支持类型CheckDtypeValid218// 测试不支持类型CheckDtypeValid
195TEST_F(l2_div_test, case_CheckDtypeValid)219TEST_F(l2_div_test, case_CheckDtypeValid)
196{220{
@@ -73,7 +73,8 @@ ge::graphStatus FloorModTiling::DoOpTiling()
73 // 获取fmod额外空间和存活节点73 // 获取fmod额外空间和存活节点
74 uint32_t maxLiveNodeCnt = 0;74 uint32_t maxLiveNodeCnt = 0;
75 uint32_t extraBuf = 0;75 uint32_t extraBuf = 0;
76- AscendC::GetFmodTmpBufferFactorSize(sizeof(input0Dtype), maxLiveNodeCnt, extraBuf);76+ // kernel中所有输入类型均cast到float32后进入高阶API计算,因此临时buffer按float大小计算
77+ AscendC::GetFmodTmpBufferFactorSize(sizeof(float), maxLiveNodeCnt, extraBuf);
77 78 
78 ge::graphStatus ret = ge::GRAPH_SUCCESS;79 ge::graphStatus ret = ge::GRAPH_SUCCESS;
79 if (input0Dtype == ge::DT_FLOAT16 || input0Dtype == ge::DT_BF16) {80 if (input0Dtype == ge::DT_FLOAT16 || input0Dtype == ge::DT_BF16) {
@@ -54,3 +54,39 @@ TEST_F(FloorModTiling, floor_mod_test_0)
54 std::vector<size_t> expectWorkspaces = {16777216};54 std::vector<size_t> expectWorkspaces = {16777216};
55 ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData, expectWorkspaces);55 ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData, expectWorkspaces);
56}56}
57+ 
58+TEST_F(FloorModTiling, floor_mod_test_float16)
59+{
60+ optiling::FloorModCompileInfo compileInfo = {64, 245760};
61+ gert::TilingContextPara tilingContextPara(
62+ "FloorMod",
63+ {
64+ {{{1, 32, 4, 32}, {1, 32, 4, 32}}, ge::DT_FLOAT16, ge::FORMAT_ND},
65+ {{{1, 32, 4, 32}, {1, 32, 4, 32}}, ge::DT_FLOAT16, ge::FORMAT_ND},
66+ },
67+ {
68+ {{{1, 32, 4, 32}, {1, 32, 4, 32}}, ge::DT_FLOAT16, ge::FORMAT_ND},
69+ },
70+ &compileInfo);
71+ TilingInfo tilingInfo;
72+ bool success = ExecuteTiling(tilingContextPara, tilingInfo);
73+ EXPECT_TRUE(success);
74+}
75+ 
76+TEST_F(FloorModTiling, floor_mod_test_int64)
77+{
78+ optiling::FloorModCompileInfo compileInfo = {64, 245760};
79+ gert::TilingContextPara tilingContextPara(
80+ "FloorMod",
81+ {
82+ {{{1, 64, 2, 64}, {1, 64, 2, 64}}, ge::DT_INT64, ge::FORMAT_ND},
83+ {{{1, 64, 2, 64}, {1, 64, 2, 64}}, ge::DT_INT64, ge::FORMAT_ND},
84+ },
85+ {
86+ {{{1, 64, 2, 64}, {1, 64, 2, 64}}, ge::DT_INT64, ge::FORMAT_ND},
87+ },
88+ &compileInfo);
89+ TilingInfo tilingInfo;
90+ bool success = ExecuteTiling(tilingContextPara, tilingInfo);
91+ EXPECT_TRUE(success);
92+}
@@ -77,7 +77,8 @@ ge::graphStatus ModTiling::DoOpTiling()
77 // 获取fmod额外空间和存活节点77 // 获取fmod额外空间和存活节点
78 uint32_t maxLiveNodeCnt = 0;78 uint32_t maxLiveNodeCnt = 0;
79 uint32_t extraBuf = 0;79 uint32_t extraBuf = 0;
80- AscendC::GetFmodTmpBufferFactorSize(sizeof(input0Dtype), maxLiveNodeCnt, extraBuf);80+ // kernel中所有输入类型均cast到float32后进入高阶API计算,因此临时buffer按float大小计算
81+ AscendC::GetFmodTmpBufferFactorSize(sizeof(float), maxLiveNodeCnt, extraBuf);
81 82 
82 ge::graphStatus ret = ge::GRAPH_SUCCESS;83 ge::graphStatus ret = ge::GRAPH_SUCCESS;
83 if (input0Dtype == ge::DT_FLOAT16 || input0Dtype == ge::DT_BF16) {84 if (input0Dtype == ge::DT_FLOAT16 || input0Dtype == ge::DT_BF16) {
@@ -260,4 +260,23 @@ TEST_F(ModTilingTest, test_tiling_int64)
260 string expectTilingData = "8192 137438953600 ";260 string expectTilingData = "8192 137438953600 ";
261 std::vector<size_t> expectWorkspaces = {16777216};261 std::vector<size_t> expectWorkspaces = {16777216};
262 ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData, expectWorkspaces);262 ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData, expectWorkspaces);
263+}
264+ 
265+TEST_F(ModTilingTest, test_tiling_bf16)
266+{
267+ BroadcastCompileInfo compileInfo{};
268+ gert::TilingContextPara tilingContextPara(
269+ "Mod",
270+ {
271+ {{{1, 32, 4, 32}, {1, 32, 4, 32}}, ge::DT_BF16, ge::FORMAT_ND},
272+ {{{1, 32, 4, 32}, {1, 32, 4, 32}}, ge::DT_BF16, ge::FORMAT_ND},
273+ },
274+ {
275+ {{{1, 32, 4, 32}, {1, 32, 4, 32}}, ge::DT_BF16, ge::FORMAT_ND},
276+ },
277+ &compileInfo);
278+ 
279+ TilingInfo tilingInfo;
280+ bool success = ExecuteTiling(tilingContextPara, tilingInfo);
281+ EXPECT_TRUE(success);
263}282}
@@ -53,9 +53,12 @@ static const aclTensor *PowAiCore(const aclTensor *self, const aclTensor *expone
53 aclTensor *powOut, aclOpExecutor *executor) {53 aclTensor *powOut, aclOpExecutor *executor) {
54 L0_DFX(PowAiCore, self, exponent, powOut);54 L0_DFX(PowAiCore, self, exponent, powOut);
55 55 
56- ADD_TO_LAUNCHER_LIST_AICORE(Pow,56+ auto ret = ADD_TO_LAUNCHER_LIST_AICORE(Pow,
57 OP_INPUT(self, exponent),57 OP_INPUT(self, exponent),
58 OP_OUTPUT(powOut));58 OP_OUTPUT(powOut));
59+ OP_CHECK(
60+ ret == ACL_SUCCESS, OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "PowAiCore ADD_TO_LAUNCHER_LIST_AICORE failed."),
61+ return nullptr);
59 return powOut;62 return powOut;
60}63}
61 64