Merged
fix(scatter_nd_update): 修复非连续回退路径的原始视图回写 #12073
fix(scatter_nd_update): 修复非连续回退路径的原始视图回写 #12073
Merged
陈展熹Add on Oct 16
Total: 3 files changed+163-5
@@ -237,8 +237,9 @@ static bool IsSupportNonContiguous(const aclTensor* varRef, int64_t indexAxisNum
237}237}
238 238 
239// 执行 ScatterNdUpdate 算子计算(公共实现)239// 执行 ScatterNdUpdate 算子计算(公共实现)
240-static aclnnStatus ExecuteScatterNdUpdate(const aclTensor* varRef, const aclTensor* indices, const aclTensor* updates,240+static aclnnStatus ExecuteScatterNdUpdate(const aclTensor* varRefContiguous, const aclTensor* varRef,
241- bool needViewCopy, uint64_t* workspaceSize, aclOpExecutor** executor,241+ const aclTensor* indices, const aclTensor* updates, bool needViewCopy,
242+ uint64_t* workspaceSize, aclOpExecutor** executor,
242 UniqueExecutor& uniqueExecutor)243 UniqueExecutor& uniqueExecutor)
243{244{
244 // 将 indices 转换成连续的 tensor245 // 将 indices 转换成连续的 tensor
@@ -250,11 +251,11 @@ static aclnnStatus ExecuteScatterNdUpdate(const aclTensor* varRef, const aclTens
250 CHECK_RET(updatesContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);251 CHECK_RET(updatesContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
251 252 
252 // 执行 L0 算子253 // 执行 L0 算子
253- auto scatterUpdateRes = l0op::ScatterNdUpdate(varRef, indicesContiguous, updatesContiguous, false,254+ auto scatterUpdateRes = l0op::ScatterNdUpdate(varRefContiguous, indicesContiguous, updatesContiguous, false,
254 uniqueExecutor.get());255 uniqueExecutor.get());
255 CHECK_RET(scatterUpdateRes != nullptr, ACLNN_ERR_INNER_NULLPTR);256 CHECK_RET(scatterUpdateRes != nullptr, ACLNN_ERR_INNER_NULLPTR);
256 257 
257- // 如果需要,将计算结果拷贝到输出 data 上258+ // 如果需要,按原始 varRef 的 stride 和 offset 写回连续副本的更新
258 if (needViewCopy) {259 if (needViewCopy) {
259 auto viewCopyResult = l0op::ViewCopy(scatterUpdateRes, varRef, uniqueExecutor.get());260 auto viewCopyResult = l0op::ViewCopy(scatterUpdateRes, varRef, uniqueExecutor.get());
260 CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);261 CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
@@ -303,7 +304,7 @@ static aclnnStatus ProcessContiguousCase(aclTensor* varRef, const aclTensor* ind
303 CHECK_RET(varRefContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);304 CHECK_RET(varRefContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
304 305 
305 bool needViewCopy = !IsContiguous(varRef);306 bool needViewCopy = !IsContiguous(varRef);
306- return ExecuteScatterNdUpdate(varRefContiguous, indices, updates, needViewCopy, workspaceSize, executor,307+ return ExecuteScatterNdUpdate(varRefContiguous, varRef, indices, updates, needViewCopy, workspaceSize, executor,
chenqi317
chenqi317chenqi317Oct 18

触发条件:ProcessContiguousCase 同时服务 DAV_3510 与 DAV_2201(head 中两条分支之外统一走到该函数),本次把 ViewCopy 目标改为原始 varRef 后,910B/910_93 上的非连续 fallback 也会实际执行“连续副本计算 + ViewCopy 写回非连续视图”这条新路径。PR 描述明确只做了 950PR 定向构建与 60 组回归,未执行 910B 设备测试和全量 UT。后果:arch22 上新增的这条执行路径(非连续目标拷贝、对应 tiling/拷贝 kernel)未经验证,若平台差异导致异常,回归仍会漏。建议:在 A2/A3(910B/910_93)上跑同一脚本的 fallback 用例,或补一条 910B 定向 UT/ST 后再合入。


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

likedislike
陈展熹
陈展熹
25 days ago Comments:
likedislike
307 uniqueExecutor);308 uniqueExecutor);
308}309}
309 310 
@@ -0,0 +1,156 @@
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+ 
11+#include <dlfcn.h>
12+#include <memory>
13+#include <tuple>
14+#include <vector>
15+ 
16+#include "gtest/gtest.h"
17+#include "../../../op_api/aclnn_scatter_nd_update.h"
18+#include "aclnn_kernels/contiguous.h"
19+#include "op_api_ut_common/tensor_desc.h"
20+#include "opdev/op_executor.h"
21+#include "opdev/platform.h"
22+#include "opdev/tensor_view_utils.h"
23+ 
24+namespace {
25+struct WritebackState {
26+ const aclTensor* original = nullptr;
27+ const aclTensor* contiguous = nullptr;
28+ const aclTensor* source = nullptr;
29+ const aclTensor* destination = nullptr;
30+ size_t contiguousCalls = 0;
31+ size_t viewCopyCalls = 0;
32+};
33+ 
34+thread_local WritebackState* scatterNdWritebackState = nullptr;
35+} // namespace
36+ 
37+// Intercept calls only while this fixture is active. Other tests must reach the
38+// original implementation, including libopapi_math.so when preloaded by CI.
39+namespace l0op {
40+const aclTensor* Contiguous(const aclTensor* x, aclOpExecutor* executor)
41+{
42+ auto* state = scatterNdWritebackState;
43+ if (state == nullptr) {
44+ using Function = const aclTensor* (*)(const aclTensor*, aclOpExecutor*);
45+ static const auto original = reinterpret_cast<Function>(
46+ dlsym(RTLD_NEXT, "_ZN4l0op10ContiguousEPK9aclTensorP13aclOpExecutor"));
47+ EXPECT_NE(original, nullptr);
48+ return original == nullptr ? nullptr : original(x, executor);
49+ }
50+ if (x == state->original) {
51+ ++state->contiguousCalls;
52+ if (!op::IsContiguous(x)) {
53+ state->contiguous = executor->AllocTensor(x->GetViewShape(), x->GetDataType());
54+ return state->contiguous;
55+ }
56+ }
57+ return x;
58+}
59+ 
60+const aclTensor* ViewCopy(const aclTensor* x, const aclTensor* y, aclOpExecutor* executor)
61+{
62+ auto* state = scatterNdWritebackState;
63+ if (state == nullptr) {
64+ using Function = const aclTensor* (*)(const aclTensor*, const aclTensor*, aclOpExecutor*);
65+ static const auto original = reinterpret_cast<Function>(
66+ dlsym(RTLD_NEXT, "_ZN4l0op8ViewCopyEPK9aclTensorS2_P13aclOpExecutor"));
67+ EXPECT_NE(original, nullptr);
68+ return original == nullptr ? nullptr : original(x, y, executor);
69+ }
70+ ++state->viewCopyCalls;
71+ state->source = x;
72+ state->destination = y;
73+ return x;
74+}
75+} // namespace l0op
76+ 
77+namespace {
78+struct WritebackLayout {
79+ std::vector<int64_t> strides;
80+ bool needsWriteback;
81+ bool callsContiguous;
82+};
83+ 
84+using WritebackParam = std::tuple<op::SocVersion, WritebackLayout, int64_t, aclDataType>;
85+ 
86+class ScatterNdUpdateWritebackTest : public testing::TestWithParam<WritebackParam> {
87+protected:
88+ void SetUp() override
89+ {
90+ previousState_ = scatterNdWritebackState;
91+ scatterNdWritebackState = &state_;
92+ }
93+ 
94+ void TearDown() override
95+ {
96+ executor_.reset();
97+ scatterNdWritebackState = previousState_;
98+ }
99+ 
100+ WritebackState state_;
101+ WritebackState* previousState_ = nullptr;
102+ std::unique_ptr<aclOpExecutor> executor_;
103+};
104+ 
105+TEST_P(ScatterNdUpdateWritebackTest, PreservesOriginalView)
106+{
107+ const auto& [soc, layout, offset, indexType] = GetParam();
108+ op::SocVersionManager platform(soc);
109+ const auto expectedArch = soc == op::SocVersion::ASCEND950 ? NpuArch::DAV_3510 : NpuArch::DAV_2201;
110+ ASSERT_EQ(op::GetCurrentPlatformInfo().GetCurNpuArch(), expectedArch);
111+ 
112+ auto var = TensorDesc({2, 3}, ACL_FLOAT, ACL_FORMAT_ND, layout.strides, offset, {32}).ToAclType();
113+ auto indices = TensorDesc({1, 1}, indexType, ACL_FORMAT_ND).ToAclType();
114+ auto updates = TensorDesc({1, 3}, ACL_FLOAT, ACL_FORMAT_ND).ToAclType();
115+ ASSERT_NE(var.get(), nullptr);
116+ ASSERT_NE(indices.get(), nullptr);
117+ ASSERT_NE(updates.get(), nullptr);
118+ state_.original = var.get();
119+ const auto originalShape = var->GetViewShape();
120+ const auto originalStrides = var->GetViewStrides();
121+ const auto originalStorageShape = var->GetStorageShape();
122+ 
123+ uint64_t workspaceSize = 0;
124+ aclOpExecutor* executor = nullptr;
125+ const auto status = aclnnScatterNdUpdateGetWorkspaceSize(var.get(), indices.get(), updates.get(), &workspaceSize,
126+ &executor);
127+ executor_.reset(executor);
128+ ASSERT_EQ(status, ACLNN_SUCCESS);
129+ ASSERT_NE(executor, nullptr);
130+ 
131+ EXPECT_EQ(state_.contiguousCalls, layout.callsContiguous ? 1U : 0U);
132+ if (layout.needsWriteback) {
133+ ASSERT_NE(state_.contiguous, nullptr);
134+ ASSERT_NE(state_.contiguous, var.get());
135+ EXPECT_TRUE(op::IsContiguous(state_.contiguous));
136+ EXPECT_EQ(state_.viewCopyCalls, 1U);
137+ EXPECT_EQ(state_.source, state_.contiguous);
138+ EXPECT_EQ(state_.destination, var.get());
139+ } else {
140+ EXPECT_EQ(state_.viewCopyCalls, 0U);
141+ EXPECT_EQ(state_.contiguous, nullptr);
142+ }
143+ EXPECT_EQ(var->GetViewShape(), originalShape);
144+ EXPECT_EQ(var->GetViewStrides(), originalStrides);
145+ EXPECT_EQ(var->GetStorageShape(), originalStorageShape);
146+ EXPECT_EQ(var->GetViewOffset(), offset);
147+}
148+ 
149+INSTANTIATE_TEST_SUITE_P(
150+ PlatformsAndLayouts, ScatterNdUpdateWritebackTest,
151+ testing::Combine(testing::Values(op::SocVersion::ASCEND910B, op::SocVersion::ASCEND910_93,
152+ op::SocVersion::ASCEND950),
153+ testing::Values(WritebackLayout{{6, 2}, true, true}, WritebackLayout{{1, 2}, true, true},
154+ WritebackLayout{{3, 1}, false, true}, WritebackLayout{{6, 1}, false, false}),
155+ testing::Values(int64_t{0}, int64_t{3}), testing::Values(ACL_INT32, ACL_INT64)));
156+} // namespace
@@ -74,6 +74,7 @@ if(UT_TEST_ALL OR OP_API_UT)
74 rt2_registry74 rt2_registry
75 error_manager75 error_manager
76 c_sec76 c_sec
77+ ${CMAKE_DL_LIBS}
77 )78 )
78 ## exec ophost ut exe79 ## exec ophost ut exe
79 if(ENABLE_UT_EXEC)80 if(ENABLE_UT_EXEC)