Merged
[fix] index-add 确定性分支增加 #5415
[fix] index-add 确定性分支增加 #5415
Merged
L1919_snowAdd on Jul 7
Total: 1 files changed+39-4
@@ -13,6 +13,7 @@
13// See the License for the specific language governing permissions and13// See the License for the specific language governing permissions and
14// limitations under the License.14// limitations under the License.
15 15 
16+#include <ATen/ATen.h>
16#include "op_plugin/AclOpsInterface.h"17#include "op_plugin/AclOpsInterface.h"
17#include "op_plugin/OpApiInterface.h"18#include "op_plugin/OpApiInterface.h"
18#include "op_plugin/utils/op_api_common.h"19#include "op_plugin/utils/op_api_common.h"
@@ -47,6 +48,23 @@ at::Tensor& index_add_out(
47 if (!result.is_same(self)) {48 if (!result.is_same(self)) {
48 result.copy_(self);49 result.copy_(self);
49 }50 }
51+ 
52+ if (at::globalContext().deterministicAlgorithms()) {
53+ auto wrapped_dim = at::maybe_wrap_dim(dim, self.dim());
54+ c10::List<c10::optional<at::Tensor>> indices;
55+ indices.reserve(self.dim());
56+ for (const auto i : c10::irange(self.dim())) {
57+ if (i == wrapped_dim) {
58+ indices.emplace_back(index.to(at::kLong));
59+ } else {
60+ indices.emplace_back(c10::nullopt);
61+ }
62+ }
63+ 
64+ result.index_put_(indices, source * alpha, true);
65+ return result;
66+ }
67+ 
50 EXEC_NPU_CMD(aclnnIndexAdd, result, dim, index, source, alpha, result);68 EXEC_NPU_CMD(aclnnIndexAdd, result, dim, index, source, alpha, result);
51 return result;69 return result;
52}70}
@@ -72,7 +90,25 @@ at::Tensor index_add(
72 source.sizes(),90 source.sizes(),
73 OPS_ERROR(ErrCode::PARAM));91 OPS_ERROR(ErrCode::PARAM));
74 at::Tensor result = at_npu::native::OpPreparation::apply_tensor_without_format(self.sizes(), self.options());92 at::Tensor result = at_npu::native::OpPreparation::apply_tensor_without_format(self.sizes(), self.options());
75- EXEC_NPU_CMD(aclnnIndexAdd, result.copy_(self), dim, index, source, alpha, result.copy_(self));93+ 
94+ if (at::globalContext().deterministicAlgorithms()) {
95+ auto wrapped_dim = at::maybe_wrap_dim(dim, self.dim());
96+ result.copy_(self);
97+ c10::List<c10::optional<at::Tensor>> indices;
98+ indices.reserve(self.dim());
99+ for (const auto i : c10::irange(self.dim())) {
100+ if (i == wrapped_dim) {
101+ indices.emplace_back(index.to(at::kLong));
102+ } else {
103+ indices.emplace_back(c10::nullopt);
104+ }
105+ }
106+ 
107+ result.index_put_(indices, source * alpha, true);
108+ return result;
109+ }
110+ 
111+ EXEC_NPU_CMD(aclnnIndexAdd, result.copy_(self), dim, index, source, alpha, result);
76 return result;112 return result;
77}113}
78 114 
@@ -82,10 +118,9 @@ at::Tensor index_add(
82 at::Dimname dim,118 at::Dimname dim,
83 const at::Tensor& index,119 const at::Tensor& index,
84 const at::Tensor& source,120 const at::Tensor& source,
85- const at::Scalar& alpha)121+ const at::Scalar& alpha) {
86-{
87 return op_api::index_add(self, dimname_to_position(self, dim), index, source, alpha);122 return op_api::index_add(self, dimname_to_position(self, dim), index, source, alpha);
88}123}
atomgit-bot

🟠 High Priority

diff 删除了 index_add(Dimname) 重载外层的 #if !VERSION_BETWEEN(V2R13, VERSION_NEWEST) / #endif 预处理器守卫(原第79行和第89行),使该重载在所有版本下均参与编译。

但在 V2R13+ 上,dimname_to_position 函数被刻意不提供桩实现。NamedTensorCompat.h 第52-55行注释明确说明:"All Dimname / DimnameList operator overloads that need dimname_to_position / dimnames_to_positions are gated out on v2.13+ via #if !VERSION_BETWEEN(V2R13, VERSION_NEWEST), so those two helpers are not stubbed here."

代码库中所有其他使用 dimname_to_position 的 Dimname 重载(如 CatKernelNpuOpApi.cpp、SortKernelNpuOpApi.cpp、SoftmaxKernelNpuOpApi.cpp、ScatterAddKernelNpuOpApi.cpp 等)均保留该守卫。aclop 侧的 index_add(Dimname)(op_plugin/ops/aclops/IndexAddKernelNpu.cpp 第92行)也保留了该守卫。

该守卫移除后,V2R13+ 构建将因 dimname_to_position 符号未定义而失败。

建议:恢复 index_add(Dimname) 重载外层的 #if !VERSION_BETWEEN(V2R13, VERSION_NEWEST) 和 #endif 守卫,保持与代码库其余 Dimname 重载一致的版本门控策略。

likedislike
Incorrect?
89#endif124#endif
90 125 
91-}126+} // namespace op_api