已合并
[Codecheck] Fix codecheck. #2246
liu-jiaweikf创建于 2025年3月10日
[Codecheck] Fix codecheck. #2246
已合并
liu-jiaweikf创建于 2025年3月10日
refs/pull/2246/head合入到master
11 个文件变更+379-325
@@ -21,69 +21,74 @@ using npu_preparation = at_npu::native::OpPreparation;
21using npu_utils = at_npu::native::NpuUtils;21using npu_utils = at_npu::native::NpuUtils;
22 22 
23namespace {23namespace {
24-at::Tensor& fill_out_nocheck(at::Tensor& result, at::Tensor& self, const at::Tensor& other) {24+at::Tensor& fill_out_nocheck(at::Tensor& result, at::Tensor& self, const at::Tensor& value)
25- at_npu::native::OpCommand cmd;25+{
26- cmd.Name("Fill");26+ at_npu::native::OpCommand cmd;
27- if (self.dim() == 0) {27+ cmd.Name("Fill");
28- c10::SmallVector<int64_t, N> dims = {1};28+ if (self.dim() == 0) {
29- cmd.Input(dims, at::kLong);29+ c10::SmallVector<int64_t, N> dims = {1};
30- } else {30+ cmd.Input(dims, at::kLong);
31- cmd.Input(self.sizes(), at::kLong);31+ } else {
32- }32+ cmd.Input(self.sizes(), at::kLong);
33- cmd.Input(other)33+ }
34- .Output(result)34+ cmd.Input(value)
35- .Run();35+ .Output(result)
36- return result;36+ .Run();
37+ return result;
37}38}
38 39 
39-at::Tensor& fill_out_nocheck(at::Tensor& result, at::Tensor& self, at::Scalar other) {40+at::Tensor& fill_out_nocheck(at::Tensor& result, at::Tensor& self, at::Scalar value)
40- at_npu::native::OpCommand cmd;41+{
41- cmd.Name("Fill");42+ at_npu::native::OpCommand cmd;
42- if (self.dim() == 0) {43+ cmd.Name("Fill");
43- c10::SmallVector<int64_t, N> dims = {1};44+ if (self.dim() == 0) {
44- cmd.Input(dims, at::kLong);45+ c10::SmallVector<int64_t, N> dims = {1};
45- } else {46+ cmd.Input(dims, at::kLong);
46- cmd.Input(self.sizes(), at::kLong);47+ } else {
47- }48+ cmd.Input(self.sizes(), at::kLong);
48- cmd.Input(other, self.scalar_type(), at_npu::native::CompileType::MEMORY_HOST_COMPILE_INDEPENDENT)49+ }
49- .Output(result)50+ cmd.Input(value, self.scalar_type(), at_npu::native::CompileType::MEMORY_HOST_COMPILE_INDEPENDENT)
50- .Run();51+ .Output(result)
51- return result;52+ .Run();
53+ return result;
52}54}
53 55 
54-at::Tensor& fill_out_nocheck(at::Tensor& self, const at::Tensor& other) {56+at::Tensor& fill_out_nocheck(at::Tensor& self, const at::Tensor& value)
55- if (npu_preparation::IsCPUScalar(other)) {57+{
56- fill_out_nocheck(self, self, other.item());58+ if (npu_preparation::IsCPUScalar(value)) {
57- } else {59+ fill_out_nocheck(self, self, value.item());
58- fill_out_nocheck(self, self, other);60+ } else {
59- }61+ fill_out_nocheck(self, self, value);
60- return self;62+ }
63+ return self;
61}64}
62} // namespace65} // namespace
63 66 
64-at::Tensor& fill_(at::Tensor& self, const at::Tensor& other) {67+at::Tensor& fill_(at::Tensor& self, const at::Tensor& value)
65- auto other_dim = other.dim();68+{
66- TORCH_CHECK(other_dim <= 1, "fill_ only supports 0 or 1 dimension value tensor but got tensor with ",69+ auto value_dim = value.dim();
67- other_dim, " dimension." + OPS_ERROR(ErrCode::PARAM));70+ TORCH_CHECK(value_dim <= 1, "fill_ only supports 0 or 1 dimension value tensor but got tensor with ",
68- npu_preparation::CheckMemory({self, other}, {self});71+ value_dim, " dimension." + OPS_ERROR(ErrCode::PARAM));
69- if (!npu_utils::check_match(&self)) {72+ npu_preparation::CheckMemory({self, value}, {self});
70- at::Tensor contiguous_self = npu_utils::format_contiguous(self);73+ if (!npu_utils::check_match(&self)) {
71- fill_out_nocheck(contiguous_self, other);74+ at::Tensor contiguous_self = npu_utils::format_contiguous(self);
72- npu_utils::format_fresh_view(self, contiguous_self);75+ fill_out_nocheck(contiguous_self, value);
73- } else {76+ npu_utils::format_fresh_view(self, contiguous_self);
74- fill_out_nocheck(self, other);77+ } else {
75- }78+ fill_out_nocheck(self, value);
76- return self;79+ }
80+ return self;
77}81}
78 82 
79-at::Tensor& fill_(at::Tensor& self, const at::Scalar& other) {83+at::Tensor& fill_(at::Tensor& self, const at::Scalar& value)
80- if (!npu_utils::check_match(&self)) {84+{
81- at::Tensor contiguous_self = npu_utils::format_contiguous(self);85+ if (!npu_utils::check_match(&self)) {
82- fill_out_nocheck(contiguous_self, contiguous_self, other);86+ at::Tensor contiguous_self = npu_utils::format_contiguous(self);
83- npu_utils::format_fresh_view(self, contiguous_self);87+ fill_out_nocheck(contiguous_self, contiguous_self, value);
84- } else {88+ npu_utils::format_fresh_view(self, contiguous_self);
85- fill_out_nocheck(self, self, other);89+ } else {
86- }90+ fill_out_nocheck(self, self, value);
87- return self;91+ }
92+ return self;
88}93}
89} // namespace acl_op94} // namespace acl_op
@@ -20,6 +20,8 @@
20#include "torch_npu/csrc/framework/utils/UtilForOpAdapter.h"20#include "torch_npu/csrc/framework/utils/UtilForOpAdapter.h"
21 21 
22namespace acl_op {22namespace acl_op {
23+const int DIMENSION_2D = 2;
24+const int DIMENSION_3D = 3;
23using npu_preparation = at_npu::native::OpPreparation;25using npu_preparation = at_npu::native::OpPreparation;
24using format_helper = at_npu::native::FormatHelper;26using format_helper = at_npu::native::FormatHelper;
25using calcu_op_util = at_npu::native::CalcuOpUtil;27using calcu_op_util = at_npu::native::CalcuOpUtil;
@@ -39,7 +41,7 @@ Matmul: [1] 2-2-t(strict transpose); [2] 2-n-view+t(view transpose).
39*****************************************/41*****************************************/
40bool is_transpose_last_two_dims_flex(const at::Tensor &tensor)42bool is_transpose_last_two_dims_flex(const at::Tensor &tensor)
41{43{
42- if (tensor.dim() != 2) {44+ if (tensor.dim() != DIMENSION_2D) {
43 return false;45 return false;
44 }46 }
45 47 
@@ -77,7 +79,7 @@ void mm_insert_input_transpose(at::Tensor &tensor, bool &is_tensor_trans_flex, b
77 is_tensor_trans_strict = !is_tensor_trans_strict;79 is_tensor_trans_strict = !is_tensor_trans_strict;
78}80}
79 81 
80-void mm_set_format_contiguous(at::Tensor &tensor, bool &is_tensor_trans_flex, bool &is_tensor_trans_strict)82+void mm_set_format_contiguous(at::Tensor &tensor, const bool &is_tensor_trans_flex, const bool &is_tensor_trans_strict)
81{83{
82 if (is_tensor_trans_flex) {84 if (is_tensor_trans_flex) {
83 if (!is_tensor_trans_strict) {85 if (!is_tensor_trans_strict) {
@@ -95,7 +97,7 @@ void mm_set_format_contiguous(at::Tensor &tensor, bool &is_tensor_trans_flex, bo
95 97 
96bool is_mm_transpose(const at::Tensor &tensor)98bool is_mm_transpose(const at::Tensor &tensor)
97{99{
98- if (tensor.dim() < 2 || tensor.dim() > 3) {100+ if (tensor.dim() < DIMENSION_2D || tensor.dim() > DIMENSION_3D) {
99 return false;101 return false;
100 }102 }
101 int64_t dim1 = tensor.dim() - 1;103 int64_t dim1 = tensor.dim() - 1;
@@ -136,7 +138,7 @@ bool is_transpose_inner_axis(const at::Tensor &self)
136{138{
137 const static int64_t kInnerAxisMinBytes = 256;139 const static int64_t kInnerAxisMinBytes = 256;
138 const static int64_t kInnerAxisMaxLimit = 65535;140 const static int64_t kInnerAxisMaxLimit = 65535;
139- if (c10_npu::GetSocVersion() < c10_npu::SocVersion::Ascend910B1 || self.dim() < 2 ||141+ if (c10_npu::GetSocVersion() < c10_npu::SocVersion::Ascend910B1 || self.dim() < DIMENSION_2D ||
140 (self.scalar_type() != at::ScalarType::Half && self.scalar_type() != at::ScalarType::Float &&142 (self.scalar_type() != at::ScalarType::Half && self.scalar_type() != at::ScalarType::Float &&
141 self.scalar_type() != at::ScalarType::BFloat16)) {143 self.scalar_type() != at::ScalarType::BFloat16)) {
142 return false;144 return false;
@@ -160,7 +162,7 @@ bool is_transpose_inner_axis(const at::Tensor &self)
160 bool((static_cast<uint64_t>(self_inner_axis) * static_cast<uint64_t>(data_type)) & 0x1F))) &&162 bool((static_cast<uint64_t>(self_inner_axis) * static_cast<uint64_t>(data_type)) & 0x1F))) &&
161 ((self_outer_axis * data_type >= kInnerAxisMinBytes && self_outer_axis <= kInnerAxisMaxLimit) ||163 ((self_outer_axis * data_type >= kInnerAxisMinBytes && self_outer_axis <= kInnerAxisMaxLimit) ||
162 (self_outer_axis * data_type < kInnerAxisMinBytes &&164 (self_outer_axis * data_type < kInnerAxisMinBytes &&
163- !((static_cast<uint64_t>(self_outer_axis) * static_cast<uint64_t>(data_type)) & 0x1F)));165+ ((static_cast<uint64_t>(self_outer_axis) * static_cast<uint64_t>(data_type)) & 0x1F) == 0));
164}166}
165 167 
166bool is_transpose_both_inner_axis(const at::Tensor &self, const at::Tensor &mat2)168bool is_transpose_both_inner_axis(const at::Tensor &self, const at::Tensor &mat2)
@@ -247,7 +249,8 @@ at::Tensor &mm_out_npu_nocheck(at::Tensor &result, const at::Tensor &self, const
247 249 
248at::Tensor &mm_out(const at::Tensor &self, const at::Tensor &mat2, at::Tensor &result)250at::Tensor &mm_out(const at::Tensor &self, const at::Tensor &mat2, at::Tensor &result)
249{251{
250- TORCH_CHECK(self.dim() == 2 && mat2.dim() == 2, "both arguments to matmul need to be 2D, but they are ",252+ TORCH_CHECK(self.dim() == DIMENSION_2D && mat2.dim() == DIMENSION_2D,
253+ "both arguments to matmul need to be 2D, but they are ",
251 self.dim(), "D and ", mat2.dim(), "D", OPS_ERROR(ErrCode::PARAM));254 self.dim(), "D and ", mat2.dim(), "D", OPS_ERROR(ErrCode::PARAM));
252 TORCH_CHECK(self.scalar_type() != at::ScalarType::Char && mat2.scalar_type() != at::ScalarType::Char,255 TORCH_CHECK(self.scalar_type() != at::ScalarType::Char && mat2.scalar_type() != at::ScalarType::Char,
253 "mm_out is not support int8 dtype", OPS_ERROR(ErrCode::PARAM))256 "mm_out is not support int8 dtype", OPS_ERROR(ErrCode::PARAM))
@@ -263,7 +266,8 @@ at::Tensor &mm_out(const at::Tensor &self, const at::Tensor &mat2, at::Tensor &r
263 266 
264at::Tensor mm(const at::Tensor &self, const at::Tensor &mat2)267at::Tensor mm(const at::Tensor &self, const at::Tensor &mat2)
265{268{
266- TORCH_CHECK(self.dim() == 2 && mat2.dim() == 2, "both arguments to matmul need to be 2D, but they are ",269+ TORCH_CHECK(self.dim() == DIMENSION_2D && mat2.dim() == DIMENSION_2D,
270+ "both arguments to matmul need to be 2D, but they are ",
267 self.dim(), "D and ", mat2.dim(), "D", OPS_ERROR(ErrCode::PARAM));271 self.dim(), "D and ", mat2.dim(), "D", OPS_ERROR(ErrCode::PARAM));
268 272 
269 // 1.cann bmm support int8(input)->int32(out)273 // 1.cann bmm support int8(input)->int32(out)
@@ -279,15 +283,17 @@ at::Tensor mm(const at::Tensor &self, const at::Tensor &mat2)
279 // check format_out of mm is NCHW. Delate after definite NLP model.283 // check format_out of mm is NCHW. Delate after definite NLP model.
280 if ((self.scalar_type() == at::ScalarType::Half)) {284 if ((self.scalar_type() == at::ScalarType::Half)) {
281 // check is 16-algined with high-performance285 // check is 16-algined with high-performance
282- auto is_aligin = [&]() {286+ auto is_align = [&]() {
283- return (!(static_cast<uint64_t>(self.size(0)) & 0xF)) && (!(static_cast<uint64_t>(self.size(1)) & 0xF)) &&287+ return ((static_cast<uint64_t>(self.size(0)) & 0xF) == 0) &&
284- (!(static_cast<uint64_t>(mat2.size(0)) & 0xF)) && (!(static_cast<uint64_t>(mat2.size(1)) & 0xF));288+ ((static_cast<uint64_t>(self.size(1)) & 0xF) == 0) &&
289+ ((static_cast<uint64_t>(mat2.size(0)) & 0xF) == 0) &&
290+ ((static_cast<uint64_t>(mat2.size(1)) & 0xF) == 0);
285 };291 };
286 // There is a data trampling problem in non-aligned scenes. For the time292 // There is a data trampling problem in non-aligned scenes. For the time
287 // being, only aligned scenes are supported.293 // being, only aligned scenes are supported.
288 static auto mm_bmm_nd = !at_npu::native::env::CheckMmBmmNDDisable();294 static auto mm_bmm_nd = !at_npu::native::env::CheckMmBmmNDDisable();
289 if (format_helper::IsBaseFormatType(self) && format_helper::IsBaseFormatType(mat2) && mm_bmm_nd &&295 if (format_helper::IsBaseFormatType(self) && format_helper::IsBaseFormatType(mat2) && mm_bmm_nd &&
290- ((is_support_nd_out && mm_check_nd_to_nz_on_the_fly(self, mat2)) || (!is_support_nd_out && is_aligin()))) {296+ ((is_support_nd_out && mm_check_nd_to_nz_on_the_fly(self, mat2)) || (!is_support_nd_out && is_align()))) {
291 result = npu_preparation::apply_tensor_with_format(output_size, self.options(), ACL_FORMAT_ND);297 result = npu_preparation::apply_tensor_with_format(output_size, self.options(), ACL_FORMAT_ND);
292 } else {298 } else {
293 need_nd_out = mm_bmm_nd;299 need_nd_out = mm_bmm_nd;
@@ -18,72 +18,79 @@
18#include "op_plugin/utils/custom_functions/aclops/inner_compute.h"18#include "op_plugin/utils/custom_functions/aclops/inner_compute.h"
19 19 
20namespace acl_op {20namespace acl_op {
21-at::Tensor& zeros_out(at::IntArrayRef size, at::Tensor& result) {21+at::Tensor& zeros_out(at::IntArrayRef size, at::Tensor& result)
22- result.resize_(size);22+{
23- return result.zero_();23+ result.resize_(size);
24+ return result.zero_();
24}25}
25 26 
26#if VERSION_BETWEEN(V1R11, V1R11)27#if VERSION_BETWEEN(V1R11, V1R11)
27at::Tensor zeros(28at::Tensor zeros(
28 at::IntArrayRef size,29 at::IntArrayRef size,
29- c10::optional<at::ScalarType> dtype_opt,30+ c10::optional<at::ScalarType> dtype,
30- c10::optional<at::Layout> layout_opt,31+ c10::optional<at::Layout> layout,
31- c10::optional<at::Device> device_opt,32+ c10::optional<at::Device> device,
32- c10::optional<bool> pin_memory_opt) {33+ c10::optional<bool> pin_memory)
33- return zeros_common_nocheck(size, dtype_opt, layout_opt, device_opt, pin_memory_opt);34+{
35+ return zeros_common_nocheck(size, dtype, layout, device, pin_memory);
34}36}
35 37 
36at::Tensor zeros(38at::Tensor zeros(
37 at::IntArrayRef size,39 at::IntArrayRef size,
38 c10::optional<at::DimnameList> names,40 c10::optional<at::DimnameList> names,
39- c10::optional<at::ScalarType> dtype_opt,41+ c10::optional<at::ScalarType> dtype,
40- c10::optional<at::Layout> layout_opt,42+ c10::optional<at::Layout> layout,
41- c10::optional<at::Device> device_opt,43+ c10::optional<at::Device> device,
42- c10::optional<bool> pin_memory_opt) {44+ c10::optional<bool> pin_memory)
43- return zeros_common_nocheck(size, dtype_opt, layout_opt, device_opt, pin_memory_opt);45+{
46+ return zeros_common_nocheck(size, dtype, layout, device, pin_memory);
44}47}
45#endif48#endif
46 49 
47#if VERSION_BETWEEN(V2R0, V2R0)50#if VERSION_BETWEEN(V2R0, V2R0)
48at::Tensor zeros(51at::Tensor zeros(
49 at::IntArrayRef size,52 at::IntArrayRef size,
50- c10::optional<at::ScalarType> dtype_opt,53+ c10::optional<at::ScalarType> dtype,
51- c10::optional<at::Layout> layout_opt,54+ c10::optional<at::Layout> layout,
52- c10::optional<at::Device> device_opt,55+ c10::optional<at::Device> device,
53- c10::optional<bool> pin_memory_opt) {56+ c10::optional<bool> pin_memory)
54- return zeros_common_nocheck(size, dtype_opt, layout_opt, device_opt, pin_memory_opt);57+{
58+ return zeros_common_nocheck(size, dtype, layout, device, pin_memory);
55}59}
56 60 
57at::Tensor zeros(61at::Tensor zeros(
58 at::IntArrayRef size,62 at::IntArrayRef size,
59 c10::optional<at::DimnameList> names,63 c10::optional<at::DimnameList> names,
60- c10::optional<at::ScalarType> dtype_opt,64+ c10::optional<at::ScalarType> dtype,
61- c10::optional<at::Layout> layout_opt,65+ c10::optional<at::Layout> layout,
62- c10::optional<at::Device> device_opt,66+ c10::optional<at::Device> device,
63- c10::optional<bool> pin_memory_opt) {67+ c10::optional<bool> pin_memory)
64- return acl_op::zeros(size, dtype_opt, layout_opt, device_opt, pin_memory_opt);68+{
69+ return acl_op::zeros(size, dtype, layout, device, pin_memory);
65}70}
66#endif71#endif
67 72 
68#if VERSION_BETWEEN(V2R1, VERSION_NEWEST)73#if VERSION_BETWEEN(V2R1, VERSION_NEWEST)
69at::Tensor zeros_symint(74at::Tensor zeros_symint(
70 c10::SymIntArrayRef size,75 c10::SymIntArrayRef size,
71- c10::optional<at::ScalarType> dtype_opt,76+ c10::optional<at::ScalarType> dtype,
72- c10::optional<at::Layout> layout_opt,77+ c10::optional<at::Layout> layout,
73- c10::optional<at::Device> device_opt,78+ c10::optional<at::Device> device,
74- c10::optional<bool> pin_memory_opt) {79+ c10::optional<bool> pin_memory)
75- return zeros_common_nocheck(c10::asIntArrayRefUnchecked(size), dtype_opt, layout_opt, device_opt, pin_memory_opt);80+{
81+ return zeros_common_nocheck(c10::asIntArrayRefUnchecked(size), dtype, layout, device, pin_memory);
76}82}
77 83 
78 84 
79at::Tensor zeros(85at::Tensor zeros(
80 at::IntArrayRef size,86 at::IntArrayRef size,
81 c10::optional<at::DimnameList> names,87 c10::optional<at::DimnameList> names,
82- c10::optional<at::ScalarType> dtype_opt,88+ c10::optional<at::ScalarType> dtype,
83- c10::optional<at::Layout> layout_opt,89+ c10::optional<at::Layout> layout,
84- c10::optional<at::Device> device_opt,90+ c10::optional<at::Device> device,
85- c10::optional<bool> pin_memory_opt) {91+ c10::optional<bool> pin_memory)
86- return zeros_common_nocheck(size, dtype_opt, layout_opt, device_opt, pin_memory_opt);92+{
93+ return zeros_common_nocheck(size, dtype, layout, device, pin_memory);
87}94}
88#endif95#endif
89} // namespace acl_op96} // namespace acl_op
@@ -28,7 +28,9 @@ void _foreach_add_v1_(const at::TensorList self, const at::Scalar& scalar)
28 return at::native::foreach_tensor_add_scalar_kernel_slow_(self, scalar);28 return at::native::foreach_tensor_add_scalar_kernel_slow_(self, scalar);
29 }29 }
30 auto scalar_type = self[0].scalar_type();30 auto scalar_type = self[0].scalar_type();
31- if (scalar_type != at::ScalarType::Half && scalar_type != at::ScalarType::Float && scalar_type != at::ScalarType::Int) {31+ if (scalar_type != at::ScalarType::Half &&
32+ scalar_type != at::ScalarType::Float &&
33+ scalar_type != at::ScalarType::Int) {
32 TORCH_CHECK(false, "input must be half, float or int32", OPS_ERROR(ErrCode::TYPE));34 TORCH_CHECK(false, "input must be half, float or int32", OPS_ERROR(ErrCode::TYPE));
33 }35 }
34 at::Tensor scalar_tensor = npu_preparation::copy_scalar_to_device(scalar, self[0].scalar_type(), self[0].device());36 at::Tensor scalar_tensor = npu_preparation::copy_scalar_to_device(scalar, self[0].scalar_type(), self[0].device());
@@ -43,14 +45,17 @@ std::vector<at::Tensor> _foreach_add_v1(const at::TensorList self, const at::Sca
43 return at::native::foreach_tensor_add_scalar_kernel_slow(self, scalar);45 return at::native::foreach_tensor_add_scalar_kernel_slow(self, scalar);
44 }46 }
45 auto scalar_type = self[0].scalar_type();47 auto scalar_type = self[0].scalar_type();
46- if (scalar_type != at::ScalarType::Half && scalar_type != at::ScalarType::Float && scalar_type != at::ScalarType::Int) {48+ if (scalar_type != at::ScalarType::Half &&
49+ scalar_type != at::ScalarType::Float &&
50+ scalar_type != at::ScalarType::Int) {
47 TORCH_CHECK(false, "input must be half, float or int32", OPS_ERROR(ErrCode::TYPE));51 TORCH_CHECK(false, "input must be half, float or int32", OPS_ERROR(ErrCode::TYPE));
48 }52 }
49 std::vector<at::Tensor> result;53 std::vector<at::Tensor> result;
50 result.reserve(self.size());54 result.reserve(self.size());
51 for (const at::Tensor &tensor : self) {55 for (const at::Tensor &tensor : self) {
52 auto output_size = op_infer::input_same_output_size(tensor);56 auto output_size = op_infer::input_same_output_size(tensor);
53- result.push_back(npu_preparation::apply_tensor_without_format(output_size, tensor.options().dtype(scalar_type)));57+ result.push_back(npu_preparation::apply_tensor_without_format(output_size,
58+ tensor.options().dtype(scalar_type)));
54 }59 }
55 at::TensorList result_ = at::TensorList(result);60 at::TensorList result_ = at::TensorList(result);
56 at::Tensor scalar_tensor = npu_preparation::copy_scalar_to_device(scalar, self[0].scalar_type(), self[0].device());61 at::Tensor scalar_tensor = npu_preparation::copy_scalar_to_device(scalar, self[0].scalar_type(), self[0].device());
@@ -79,7 +84,7 @@ void _split_and_exec_npu_cmd_add_scalar(const at::TensorList tensors1, const at:
79 }84 }
80 85 
81 size_t remaining_count = tensor_count % max_tensor_count;86 size_t remaining_count = tensor_count % max_tensor_count;
82- if (remaining_count) {87+ if (remaining_count != 0) {
83 at::TensorList temp_tensors1(tensors1.data() + loop_time * max_tensor_count, remaining_count);88 at::TensorList temp_tensors1(tensors1.data() + loop_time * max_tensor_count, remaining_count);
84 at::TensorList temp_result(result_list.data() + loop_time * max_tensor_count, remaining_count);89 at::TensorList temp_result(result_list.data() + loop_time * max_tensor_count, remaining_count);
85 EXEC_NPU_CMD(aclnnForeachAddScalarV2, temp_tensors1, scalar_, temp_result);90 EXEC_NPU_CMD(aclnnForeachAddScalarV2, temp_tensors1, scalar_, temp_result);
@@ -105,7 +110,8 @@ std::vector<at::Tensor> _foreach_add(const at::TensorList self, const at::Scalar
105 result.reserve(self.size());110 result.reserve(self.size());
106 for (const at::Tensor &tensor : self) {111 for (const at::Tensor &tensor : self) {
107 auto output_size = op_infer::input_same_output_size(tensor);112 auto output_size = op_infer::input_same_output_size(tensor);
108- result.push_back(npu_preparation::apply_tensor_without_format(output_size, tensor.options().dtype(scalar_type)));113+ result.push_back(npu_preparation::apply_tensor_without_format(output_size,
114+ tensor.options().dtype(scalar_type)));
109 }115 }
110 at::TensorList result_ = at::TensorList(result);116 at::TensorList result_ = at::TensorList(result);
111 _split_and_exec_npu_cmd_add_scalar(self, scalar, result_, false);117 _split_and_exec_npu_cmd_add_scalar(self, scalar, result_, false);
@@ -51,7 +51,8 @@ void _split_and_exec_npu_cmd_addcmul_tensor(const at::TensorList input,
51 at::TensorList temp_tensors2(tensors2.data() + i * max_tensor_count, data_count);51 at::TensorList temp_tensors2(tensors2.data() + i * max_tensor_count, data_count);
52 at::Tensor temp_scalars = scalars.slice(0, i * max_tensor_count, data_count);52 at::Tensor temp_scalars = scalars.slice(0, i * max_tensor_count, data_count);
53 at::TensorList temp_result(result.data() + i * max_tensor_count, data_count);53 at::TensorList temp_result(result.data() + i * max_tensor_count, data_count);
54- EXEC_NPU_CMD(aclnnForeachAddcmulScalarList, temp_input, temp_tensors1, temp_tensors2, temp_scalars, temp_result);54+ EXEC_NPU_CMD(aclnnForeachAddcmulScalarList, temp_input, temp_tensors1,
55+ temp_tensors2, temp_scalars, temp_result);
55 }56 }
56}57}
57 58 
@@ -77,14 +78,16 @@ std::vector<at::Tensor> _foreach_addcmul(const at::TensorList input,
77 }78 }
78 79 
79 auto scalar_type = input[0].scalar_type();80 auto scalar_type = input[0].scalar_type();
80- if (scalar_type != at::ScalarType::Half && scalar_type != at::ScalarType::Float && scalar_type != at::ScalarType::Int && scalar_type != at::ScalarType::BFloat16) {81+ if (scalar_type != at::ScalarType::Half && scalar_type != at::ScalarType::Float &&
82+ scalar_type != at::ScalarType::Int && scalar_type != at::ScalarType::BFloat16) {
81 TORCH_CHECK(false, "input must be half, float, int32 or bfloat16" + OPS_ERROR(ErrCode::TYPE));83 TORCH_CHECK(false, "input must be half, float, int32 or bfloat16" + OPS_ERROR(ErrCode::TYPE));
82 }84 }
83 std::vector<at::Tensor> result;85 std::vector<at::Tensor> result;
84 result.reserve(input.size());86 result.reserve(input.size());
85 for (const at::Tensor &tensor : input) {87 for (const at::Tensor &tensor : input) {
86 auto output_size = op_infer::input_same_output_size(tensor);88 auto output_size = op_infer::input_same_output_size(tensor);
87- result.push_back(npu_preparation::apply_tensor_without_format(output_size, tensor.options().dtype(scalar_type)));89+ result.push_back(npu_preparation::apply_tensor_without_format(output_size,
90+ tensor.options().dtype(scalar_type)));
88 }91 }
89 at::TensorList result_ = at::TensorList(result);92 at::TensorList result_ = at::TensorList(result);
90 auto scalar_tensor = npu_preparation::copy_tensor_host_to_device(scalars);93 auto scalar_tensor = npu_preparation::copy_tensor_host_to_device(scalars);
@@ -115,7 +118,8 @@ void _foreach_addcmul_(const at::TensorList input,
115 118 
116 at::native::check_foreach_api_restrictions(input, tensors1, tensors2);119 at::native::check_foreach_api_restrictions(input, tensors1, tensors2);
117 auto scalar_type = input[0].scalar_type();120 auto scalar_type = input[0].scalar_type();
118- if (scalar_type != at::ScalarType::Half && scalar_type != at::ScalarType::Float && scalar_type != at::ScalarType::Int && scalar_type != at::ScalarType::BFloat16) {121+ if (scalar_type != at::ScalarType::Half && scalar_type != at::ScalarType::Float &&
122+ scalar_type != at::ScalarType::Int && scalar_type != at::ScalarType::BFloat16) {
119 TORCH_CHECK(false, "input must be half, float, int32, or bfloat16" + OPS_ERROR(ErrCode::TYPE));123 TORCH_CHECK(false, "input must be half, float, int32, or bfloat16" + OPS_ERROR(ErrCode::TYPE));
120 }124 }
121 auto scalar_tensor = npu_preparation::copy_tensor_host_to_device(scalars);125 auto scalar_tensor = npu_preparation::copy_tensor_host_to_device(scalars);
@@ -13,15 +13,18 @@
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 "op_plugin/AclOpsInterface.h"16+#include <ATen/native/ForeachUtils.h>
17-#include "op_plugin/OpApiInterface.h"17+ 
18#include "op_plugin/utils/op_api_common.h"18#include "op_plugin/utils/op_api_common.h"
19#include "torch_npu/csrc/framework/utils/UtilForOpAdapter.h"19#include "torch_npu/csrc/framework/utils/UtilForOpAdapter.h"
20-#include <ATen/native/ForeachUtils.h>
21#include "op_plugin/utils/custom_functions/opapi/ForeachConstants.h"20#include "op_plugin/utils/custom_functions/opapi/ForeachConstants.h"
21+#include "op_plugin/OpApiInterface.h"
22+#include "op_plugin/AclOpsInterface.h"
22 23 
23namespace op_api {24namespace op_api {
24#if VERSION_BETWEEN(V2R1, VERSION_NEWEST)25#if VERSION_BETWEEN(V2R1, VERSION_NEWEST)
26+const size_t SIZE_OF_NOT_INT = 4;
27+const size_t SIZE_OF_SHORT = 2;
25using npu_preparation = at_npu::native::OpPreparation;28using npu_preparation = at_npu::native::OpPreparation;
26using npu_calcu_util = at_npu::native::CalcuOpUtil;29using npu_calcu_util = at_npu::native::CalcuOpUtil;
27 30 
@@ -52,22 +55,24 @@ void split_and_exec_npu_cmd_copy(const at::TensorList dst, at::TensorList src, b
52 }55 }
53 56 
54 size_t remaining_count = tensor_count % max_tensor_count;57 size_t remaining_count = tensor_count % max_tensor_count;
55- if (remaining_count) {58+ if (remaining_count != 0) {
56 at::TensorList temp_src(src.data() + loop_time * max_tensor_count, remaining_count);59 at::TensorList temp_src(src.data() + loop_time * max_tensor_count, remaining_count);
57 at::TensorList temp_dst(dst.data() + loop_time * max_tensor_count, remaining_count);60 at::TensorList temp_dst(dst.data() + loop_time * max_tensor_count, remaining_count);
58 exec_npu_cmd_copy(temp_dst, temp_src, non_blocking);61 exec_npu_cmd_copy(temp_dst, temp_src, non_blocking);
59 }62 }
60}63}
61 64 
62-bool check_tensor_dtype_spport_base(const at::TensorList src)65+bool check_tensor_dtype_support_base(const at::TensorList src)
63{66{
64- if ((sizeof(src[0]) == 4 && src[0].scalar_type() != at::ScalarType::QInt32) || src[0].scalar_type() == at::ScalarType::Int) {67+ if ((sizeof(src[0]) == SIZE_OF_NOT_INT && src[0].scalar_type() != at::ScalarType::QInt32) ||
68+ src[0].scalar_type() == at::ScalarType::Int) {
65 return true;69 return true;
66 }70 }
67- if (sizeof(src[0]) == 2 || src[0].scalar_type() == at::ScalarType::Short) {71+ if (sizeof(src[0]) == SIZE_OF_SHORT || src[0].scalar_type() == at::ScalarType::Short) {
68 return true;72 return true;
69 }73 }
70- if (src[0].scalar_type() == at::ScalarType::Char || src[0].scalar_type() == at::ScalarType::Byte || src[0].scalar_type() == at::ScalarType::BFloat16 ||74+ if (src[0].scalar_type() == at::ScalarType::Char || src[0].scalar_type() == at::ScalarType::Byte ||
75+ src[0].scalar_type() == at::ScalarType::BFloat16 ||
71 src[0].scalar_type() == at::ScalarType::Float || src[0].scalar_type() == at::ScalarType::Half) {76 src[0].scalar_type() == at::ScalarType::Float || src[0].scalar_type() == at::ScalarType::Half) {
72 return true;77 return true;
73 }78 }
@@ -79,9 +84,9 @@ void _foreach_copy_(const at::TensorList self, const at::TensorList src, bool no
79 DO_COMPATIBILITY(aclnnForeachCopy, at::native::foreach_tensor_copy_list_kernel_slow_(self, src, non_blocking));84 DO_COMPATIBILITY(aclnnForeachCopy, at::native::foreach_tensor_copy_list_kernel_slow_(self, src, non_blocking));
80 at::native::check_foreach_api_restrictions(self, src);85 at::native::check_foreach_api_restrictions(self, src);
81 static const bool is_support_nd_out = (c10_npu::GetSocVersion() >= c10_npu::SocVersion::Ascend910B1 &&86 static const bool is_support_nd_out = (c10_npu::GetSocVersion() >= c10_npu::SocVersion::Ascend910B1 &&
82- c10_npu::GetSocVersion() < c10_npu::SocVersion::Ascend310B1)||87+ c10_npu::GetSocVersion() < c10_npu::SocVersion::Ascend310B1) ||
83 (c10_npu::GetSocVersion() > c10_npu::SocVersion::Ascend310B4);88 (c10_npu::GetSocVersion() > c10_npu::SocVersion::Ascend310B4);
84- if (!is_support_nd_out || !at::native::can_use_fast_route(self, src) || !check_tensor_dtype_spport_base(src)) {89+ if (!is_support_nd_out || !at::native::can_use_fast_route(self, src) || !check_tensor_dtype_support_base(src)) {
85 return at::native::foreach_tensor_copy_list_kernel_slow_(self, src, non_blocking);90 return at::native::foreach_tensor_copy_list_kernel_slow_(self, src, non_blocking);
86 }91 }
87 92 
@@ -26,15 +26,16 @@ std::tuple<at::Tensor&, at::Tensor&> kthvalue_out(
26 int64_t dim,26 int64_t dim,
27 bool keepdim,27 bool keepdim,
28 at::Tensor& values,28 at::Tensor& values,
29- at::Tensor& indices) {29+ at::Tensor& indices)
30- DO_COMPATIBILITY(aclnnKthvalue, acl_op::kthvalue_out(self, k, dim, keepdim, values, indices));30+{
31- at::IntArrayRef dims(dim);31+ DO_COMPATIBILITY(aclnnKthvalue, acl_op::kthvalue_out(self, k, dim, keepdim, values, indices));
32- auto output_size = op_infer::reduce_ops_npu_output_size(self, dims, keepdim);32+ at::IntArrayRef dims(dim);
33- npu_preparation::check_tensor({self}, values, self.scalar_type(), output_size);33+ auto output_size = op_infer::reduce_ops_npu_output_size(self, dims, keepdim);
34- npu_preparation::check_tensor({self}, indices, at::ScalarType::Long, output_size);34+ npu_preparation::check_tensor({self}, values, self.scalar_type(), output_size);
35+ npu_preparation::check_tensor({self}, indices, at::ScalarType::Long, output_size);
35 36 
36- EXEC_NPU_CMD(aclnnKthvalue, self, k, dim, keepdim, values, indices);37+ EXEC_NPU_CMD(aclnnKthvalue, self, k, dim, keepdim, values, indices);
37- return std::tuple<at::Tensor&, at::Tensor&>(values, indices);38+ return std::tuple<at::Tensor&, at::Tensor&>(values, indices);
38}39}
39 40 
40std::tuple<at::Tensor&, at::Tensor&> kthvalue_out(41std::tuple<at::Tensor&, at::Tensor&> kthvalue_out(
@@ -43,46 +44,49 @@ std::tuple<at::Tensor&, at::Tensor&> kthvalue_out(
43 at::Dimname dim,44 at::Dimname dim,
44 bool keepdim,45 bool keepdim,
45 at::Tensor& values,46 at::Tensor& values,
46- at::Tensor& indices) {47+ at::Tensor& indices)
47- DO_COMPATIBILITY(aclnnKthvalue, acl_op::kthvalue_out(self, k, dim, keepdim, values, indices));48+{
48- const int64_t real_dim = dimname_to_position(self, dim);49+ DO_COMPATIBILITY(aclnnKthvalue, acl_op::kthvalue_out(self, k, dim, keepdim, values, indices));
49- at::IntArrayRef dims(real_dim);50+ const int64_t real_dim = dimname_to_position(self, dim);
50- auto output_size = op_infer::reduce_ops_npu_output_size(self, dims, keepdim);51+ at::IntArrayRef dims(real_dim);
51- npu_preparation::check_tensor({self}, values, self.scalar_type(), output_size);52+ auto output_size = op_infer::reduce_ops_npu_output_size(self, dims, keepdim);
52- npu_preparation::check_tensor({self}, indices, at::ScalarType::Long, output_size);53+ npu_preparation::check_tensor({self}, values, self.scalar_type(), output_size);
54+ npu_preparation::check_tensor({self}, indices, at::ScalarType::Long, output_size);
53 55 
54- EXEC_NPU_CMD(aclnnKthvalue, self, k, real_dim, keepdim, values, indices);56+ EXEC_NPU_CMD(aclnnKthvalue, self, k, real_dim, keepdim, values, indices);
55- return std::tuple<at::Tensor&, at::Tensor&>(values, indices);57+ return std::tuple<at::Tensor&, at::Tensor&>(values, indices);
56}58}
57 59 
58std::tuple<at::Tensor, at::Tensor> kthvalue(60std::tuple<at::Tensor, at::Tensor> kthvalue(
59 const at::Tensor& self,61 const at::Tensor& self,
60 int64_t k,62 int64_t k,
61 int64_t dim,63 int64_t dim,
62- bool keepdim) {64+ bool keepdim)
63- DO_COMPATIBILITY(aclnnKthvalue, acl_op::kthvalue(self, k, dim, keepdim));65+{
64- at::IntArrayRef dims(dim);66+ DO_COMPATIBILITY(aclnnKthvalue, acl_op::kthvalue(self, k, dim, keepdim));
65- auto output_size = op_infer::reduce_ops_npu_output_size(self, dims, keepdim);67+ at::IntArrayRef dims(dim);
66- at::Tensor values = npu_preparation::apply_tensor_without_format(output_size, self.options());68+ auto output_size = op_infer::reduce_ops_npu_output_size(self, dims, keepdim);
67- at::Tensor indices = npu_preparation::apply_tensor_without_format(output_size, self.options().dtype(at::kLong));69+ at::Tensor values = npu_preparation::apply_tensor_without_format(output_size, self.options());
70+ at::Tensor indices = npu_preparation::apply_tensor_without_format(output_size, self.options().dtype(at::kLong));
68 71 
69- EXEC_NPU_CMD(aclnnKthvalue, self, k, dim, keepdim, values, indices);72+ EXEC_NPU_CMD(aclnnKthvalue, self, k, dim, keepdim, values, indices);
70- return std::tuple<at::Tensor, at::Tensor>(values, indices);73+ return std::tuple<at::Tensor, at::Tensor>(values, indices);
71}74}
72 75 
73std::tuple<at::Tensor, at::Tensor> kthvalue(76std::tuple<at::Tensor, at::Tensor> kthvalue(
74 const at::Tensor& self,77 const at::Tensor& self,
75 int64_t k,78 int64_t k,
76 at::Dimname dim,79 at::Dimname dim,
77- bool keepdim) {80+ bool keepdim)
78- DO_COMPATIBILITY(aclnnKthvalue, acl_op::kthvalue(self, k, dim, keepdim));81+{
79- const int64_t real_dim = dimname_to_position(self, dim);82+ DO_COMPATIBILITY(aclnnKthvalue, acl_op::kthvalue(self, k, dim, keepdim));
80- at::IntArrayRef dims(real_dim);83+ const int64_t real_dim = dimname_to_position(self, dim);
81- auto output_size = op_infer::reduce_ops_npu_output_size(self, dims, keepdim);84+ at::IntArrayRef dims(real_dim);
82- at::Tensor values = npu_preparation::apply_tensor_without_format(output_size, self.options());85+ auto output_size = op_infer::reduce_ops_npu_output_size(self, dims, keepdim);
83- at::Tensor indices = npu_preparation::apply_tensor_without_format(output_size, self.options().dtype(at::kLong));86+ at::Tensor values = npu_preparation::apply_tensor_without_format(output_size, self.options());
87+ at::Tensor indices = npu_preparation::apply_tensor_without_format(output_size, self.options().dtype(at::kLong));
84 88 
85- EXEC_NPU_CMD(aclnnKthvalue, self, k, real_dim, keepdim, values, indices);89+ EXEC_NPU_CMD(aclnnKthvalue, self, k, real_dim, keepdim, values, indices);
86- return std::tuple<at::Tensor, at::Tensor>(values, indices);90+ return std::tuple<at::Tensor, at::Tensor>(values, indices);
87}91}
88}92}
@@ -20,56 +20,58 @@
20 20 
21namespace op_api {21namespace op_api {
22 22 
23-static std::tuple<at::Tensor&, at::Tensor&> nll_loss_forward_npu_nocheck(at::Tensor& result, at::Tensor& total_weight,23+static std::tuple<at::Tensor&, at::Tensor&> nll_loss_forward_npu_nocheck(
24- const at::Tensor& self, const at::Tensor& target,24+ at::Tensor& result, at::Tensor& total_weight, const at::Tensor& self, const at::Tensor& target,
25- const at::Tensor& weight, int64_t reduction,25+ const at::Tensor& weight_value, int64_t reduction, int64_t ignore_index)
26- int64_t ignore_index) {26+{
27- at::Tensor weight_tensor = weight.defined() ? weight : at::ones(self.size(-1), self.options());27+ at::Tensor weight_tensor = weight_value.defined() ? weight_value : at::ones(self.size(-1), self.options());
28 28 
29- EXEC_NPU_CMD(aclnnNLLLoss, self, target, weight_tensor, reduction, ignore_index, result, total_weight);29+ EXEC_NPU_CMD(aclnnNLLLoss, self, target, weight_tensor, reduction, ignore_index, result, total_weight);
30- return std::tuple<at::Tensor&, at::Tensor&>(result, total_weight);30+ return std::tuple<at::Tensor&, at::Tensor&>(result, total_weight);
31}31}
32 32 
33std::tuple<at::Tensor&, at::Tensor&> nll_loss_forward_out(33std::tuple<at::Tensor&, at::Tensor&> nll_loss_forward_out(
34- const at::Tensor& self, const at::Tensor& target, const c10::optional<at::Tensor>& weight_opt, int64_t reduction,34+ const at::Tensor& self, const at::Tensor& target, const c10::optional<at::Tensor>& weight, int64_t reduction,
35- int64_t ignore_index, at::Tensor& result, at::Tensor& total_weight) {35+ int64_t ignore_index, at::Tensor& output, at::Tensor& total_weight)
36- DO_COMPATIBILITY(aclnnNLLLoss, acl_op::nll_loss_forward_out(self, target, weight_opt, reduction,36+{
37- ignore_index, result, total_weight));37+ DO_COMPATIBILITY(aclnnNLLLoss, acl_op::nll_loss_forward_out(self, target, weight, reduction,
38- at::Tensor weight = c10::value_or_else(weight_opt, [] { return at::Tensor(); });38+ ignore_index, output, total_weight));
39- if (!weight.defined()) {39+ at::Tensor weight_value = c10::value_or_else(weight, [] { return at::Tensor(); });
40- weight = at::ones(self.size(-1), self.options());40+ if (!weight_value.defined()) {
41- }41+ weight_value = at::ones(self.size(-1), self.options());
42+ }
42 43 
43- c10::SmallVector<int64_t, SIZE> output_size = {};44+ c10::SmallVector<int64_t, SIZE> output_size = {};
44- if (reduction == at::Reduction::None && self.dim() != 1) {45+ if (reduction == at::Reduction::None && self.dim() != 1) {
45- output_size = {self.size(0)};46+ output_size = {self.size(0)};
46- }47+ }
47 48 
48- at_npu::native::OpPreparation::check_tensor({self, target, weight}, result, result, output_size);49+ at_npu::native::OpPreparation::check_tensor({self, target, weight_value}, output, output, output_size);
49- at_npu::native::OpPreparation::check_tensor({self, target, weight}, total_weight, total_weight, {});50+ at_npu::native::OpPreparation::check_tensor({self, target, weight_value}, total_weight, total_weight, {});
50- nll_loss_forward_npu_nocheck(result, total_weight, self, target, weight, reduction, ignore_index);51+ nll_loss_forward_npu_nocheck(output, total_weight, self, target, weight_value, reduction, ignore_index);
51- return std::tie(result, total_weight);52+ return std::tie(output, total_weight);
52}53}
53 54 
54std::tuple<at::Tensor, at::Tensor> nll_loss_forward(const at::Tensor& self,55std::tuple<at::Tensor, at::Tensor> nll_loss_forward(const at::Tensor& self,
55 const at::Tensor& target,56 const at::Tensor& target,
56- const c10::optional<at::Tensor>& weight_opt,57+ const c10::optional<at::Tensor>& weight,
57- int64_t reduction, int64_t ignore_index) {58+ int64_t reduction, int64_t ignore_index)
58- DO_COMPATIBILITY(aclnnNLLLoss,59+{
59- acl_op::nll_loss_forward(self, target, weight_opt, reduction, ignore_index));60+ DO_COMPATIBILITY(aclnnNLLLoss,
60- c10::SmallVector<int64_t, SIZE> output_size = {};61+ acl_op::nll_loss_forward(self, target, weight, reduction, ignore_index));
61- c10::SmallVector<int64_t, SIZE> totalWeightSize = {};62+ c10::SmallVector<int64_t, SIZE> output_size = {};
62- const at::Tensor& weight = c10::value_or_else(weight_opt, [] { return at::Tensor(); });63+ c10::SmallVector<int64_t, SIZE> totalWeightSize = {};
64+ const at::Tensor& weight_value = c10::value_or_else(weight, [] { return at::Tensor(); });
63 65 
64- if (reduction == at::Reduction::None && self.dim() != 1) {66+ if (reduction == at::Reduction::None && self.dim() != 1) {
65- output_size = {self.size(0)};67+ output_size = {self.size(0)};
66- }68+ }
67 69 
68- at::Tensor result = at_npu::native::OpPreparation::apply_tensor_without_format(self, output_size);70+ at::Tensor result = at_npu::native::OpPreparation::apply_tensor_without_format(self, output_size);
69- at::Tensor total_weight = at_npu::native::OpPreparation::apply_tensor_without_format(self, totalWeightSize);71+ at::Tensor total_weight = at_npu::native::OpPreparation::apply_tensor_without_format(self, totalWeightSize);
70 72 
71- nll_loss_forward_npu_nocheck(result, total_weight, self, target, weight, reduction, ignore_index);73+ nll_loss_forward_npu_nocheck(result, total_weight, self, target, weight_value, reduction, ignore_index);
72- return std::tuple<at::Tensor, at::Tensor>(result, total_weight);74+ return std::tuple<at::Tensor, at::Tensor>(result, total_weight);
73}75}
74 76 
75-}77+} // namespace op_api
@@ -30,53 +30,59 @@ const int64_t RANDOM_BFLOAT16_MAX = 1LL << 8;
30 30 
31} // namespace31} // namespace
32static std::map<at::ScalarType, int64_t> DTYPE_MAX_VALUE_MAP = {32static std::map<at::ScalarType, int64_t> DTYPE_MAX_VALUE_MAP = {
33- {at::kHalf, RANDOM_HALF_MAX + 1},33+ {at::kHalf, RANDOM_HALF_MAX + 1},
34- {at::kFloat, RANDOM_FLOAT_MAX + 1},34+ {at::kFloat, RANDOM_FLOAT_MAX + 1},
35- {at::kDouble, RANDOM_DOUBLE_MAX + 1},35+ {at::kDouble, RANDOM_DOUBLE_MAX + 1},
36- {at::kInt, std::numeric_limits<int>::max()},36+ {at::kInt, std::numeric_limits<int>::max()},
37- {at::kShort, std::numeric_limits<int16_t>::max()},37+ {at::kShort, std::numeric_limits<int16_t>::max()},
38- {at::kChar, std::numeric_limits<int8_t>::max()},38+ {at::kChar, std::numeric_limits<int8_t>::max()},
39- {at::kByte, std::numeric_limits<uint8_t>::max()},39+ {at::kByte, std::numeric_limits<uint8_t>::max()},
40- {at::kLong, std::numeric_limits<long>::max()},40+ {at::kLong, std::numeric_limits<long>::max()},
41- {at::kBFloat16, RANDOM_BFLOAT16_MAX + 1},41+ {at::kBFloat16, RANDOM_BFLOAT16_MAX + 1},
42- {at::kBool, 1}42+ {at::kBool, 1}
43};43};
44 44 
45int64_t get_dtype_max_value(at::ScalarType dtype)45int64_t get_dtype_max_value(at::ScalarType dtype)
46{46{
47 auto iter = DTYPE_MAX_VALUE_MAP.find(dtype);47 auto iter = DTYPE_MAX_VALUE_MAP.find(dtype);
48- TORCH_CHECK(iter != DTYPE_MAX_VALUE_MAP.end(), "self scalar_type:", dtype, "is not surpported.", OPS_ERROR(ErrCode::TYPE));48+ TORCH_CHECK(iter != DTYPE_MAX_VALUE_MAP.end(),
49+ "self scalar_type:", dtype, "is not surpported.", OPS_ERROR(ErrCode::TYPE));
49 return iter->second;50 return iter->second;
50}51}
51 52 
52-at::Tensor& random_op_api_(at::Tensor& self, int64_t from, int64_t to, c10::optional<at::Generator> gen_) {53+at::Tensor& random_op_api_(at::Tensor& self, int64_t from, int64_t to, c10::optional<at::Generator> generator)
53- auto gen = at::get_generator_or_default<at_npu::NPUGeneratorImpl>(gen_, at_npu::detail::getDefaultNPUGenerator());54+{
54- auto pair = gen->philox_engine_inputs(10);55+ auto gen = at::get_generator_or_default<at_npu::NPUGeneratorImpl>(generator,
55- EXEC_NPU_CMD(aclnnInplaceRandom, self, from, to, pair.first, pair.second);56+ at_npu::detail::getDefaultNPUGenerator());
56- return self;57+ auto pair = gen->philox_engine_inputs(10);
58+ EXEC_NPU_CMD(aclnnInplaceRandom, self, from, to, pair.first, pair.second);
59+ return self;
57}60}
58 61 
59at::Tensor& random_(at::Tensor& self, int64_t from, c10::optional<int64_t> to,62at::Tensor& random_(at::Tensor& self, int64_t from, c10::optional<int64_t> to,
60- c10::optional<at::Generator> gen_) {63+ c10::optional<at::Generator> generator)
61- DO_COMPATIBILITY(aclnnInplaceRandom, acl_op::random_(self, from, to, gen_));64+{
62- int64_t to_ = to.value_or(get_dtype_max_value(self.scalar_type()));65+ DO_COMPATIBILITY(aclnnInplaceRandom, acl_op::random_(self, from, to, generator));
63- random_op_api_(self, from, to_, gen_);66+ int64_t to_ = to.value_or(get_dtype_max_value(self.scalar_type()));
64- return self;67+ random_op_api_(self, from, to_, generator);
68+ return self;
65}69}
66 70 
67-at::Tensor& random_(at::Tensor& self, int64_t to, c10::optional<at::Generator> gen_) {71+at::Tensor& random_(at::Tensor& self, int64_t to, c10::optional<at::Generator> generator)
68- DO_COMPATIBILITY(aclnnInplaceRandom, acl_op::random_(self, to, gen_));72+{
69- int64_t from = 0;73+ DO_COMPATIBILITY(aclnnInplaceRandom, acl_op::random_(self, to, generator));
70- random_op_api_(self, from, to, gen_);74+ int64_t from = 0;
71- return self;75+ random_op_api_(self, from, to, generator);
76+ return self;
72}77}
73 78 
74-at::Tensor& random_(at::Tensor& self, c10::optional<at::Generator> gen_) {79+at::Tensor& random_(at::Tensor& self, c10::optional<at::Generator> generator)
75- DO_COMPATIBILITY(aclnnInplaceRandom, acl_op::random_(self, gen_));80+{
76- int64_t from = 0;81+ DO_COMPATIBILITY(aclnnInplaceRandom, acl_op::random_(self, generator));
77- int64_t to = get_dtype_max_value(self.scalar_type());82+ int64_t from = 0;
78- random_op_api_(self, from, to, gen_);83+ int64_t to = get_dtype_max_value(self.scalar_type());
79- return self;84+ random_op_api_(self, from, to, generator);
85+ return self;
80}86}
81 87 
82}88}
@@ -14,71 +14,73 @@
14// See the License for the specific language governing permissions and14// See the License for the specific language governing permissions and
15// limitations under the License.15// limitations under the License.
16 16 
17-#include "op_plugin/AclOpsInterface.h"
18-#include "op_plugin/OpApiInterface.h"
19#include "op_plugin/utils/op_api_common.h"17#include "op_plugin/utils/op_api_common.h"
20 18 
21namespace op_api {19namespace op_api {
22#if VERSION_BETWEEN(V2R0, V2R0)20#if VERSION_BETWEEN(V2R0, V2R0)
23using npu_preparation = at_npu::native::OpPreparation;21using npu_preparation = at_npu::native::OpPreparation;
24 22 
25-static at::Tensor& div_out_npu_opapi_nocheck(const at::Tensor& self, const at::Tensor& other, at::Tensor& result) {23+static at::Tensor& div_out_npu_opapi_nocheck(const at::Tensor& self, const at::Tensor& other, at::Tensor& result)
26- // executing the NPU operator24+{
27- if (other.dim() == 0 && !torch_npu::utils::is_npu(other)) {25+ // executing the NPU operator
28- c10::Scalar others = other.item();26+ if (other.dim() == 0 && !torch_npu::utils::is_npu(other)) {
29- EXEC_NPU_CMD(aclnnDivs, self, others, result);27+ c10::Scalar others = other.item();
30- } else {28+ EXEC_NPU_CMD(aclnnDivs, self, others, result);
31- EXEC_NPU_CMD(aclnnDiv, self, other, result);29+ } else {
32- }30+ EXEC_NPU_CMD(aclnnDiv, self, other, result);
33- return result;31+ }
32+ return result;
34}33}
35 34 
36static at::Tensor self_tensor_to_device(const at::Tensor& tensor, const at::ScalarType result_type,35static at::Tensor self_tensor_to_device(const at::Tensor& tensor, const at::ScalarType result_type,
37 const c10::Device device)36 const c10::Device device)
38{37{
39- if (npu_preparation::is_scalar_wrapped_to_tensor(tensor)) {38+ if (npu_preparation::is_scalar_wrapped_to_tensor(tensor)) {
40- at::Scalar scalar = tensor.item();39+ at::Scalar scalar = tensor.item();
41- return npu_preparation::copy_scalar_to_device(scalar, result_type, device);40+ return npu_preparation::copy_scalar_to_device(scalar, result_type, device);
42- }41+ }
43- return tensor;42+ return tensor;
44}43}
45 44 
46-at::Tensor true_divide(const at::Tensor &self, const at::Tensor &other) {45+at::Tensor true_divide(const at::Tensor &self, const at::Tensor &other)
47- DO_COMPATIBILITY(aclnnDivs, acl_op::true_divide(self, other));46+{
48- DO_COMPATIBILITY(aclnnDiv, acl_op::true_divide(self, other));47+ DO_COMPATIBILITY(aclnnDivs, acl_op::true_divide(self, other));
49- // calculate the output size48+ DO_COMPATIBILITY(aclnnDiv, acl_op::true_divide(self, other));
50- bool is_self_wrapped = npu_preparation::is_scalar_wrapped_to_tensor(self);49+ // calculate the output size
51- at::Tensor output_tensor = is_self_wrapped ? other : self;50+ bool is_self_wrapped = npu_preparation::is_scalar_wrapped_to_tensor(self);
52- auto output_size = op_infer::broadcast_ops_npu_output_size(self, other);51+ at::Tensor output_tensor = is_self_wrapped ? other : self;
53- at::ScalarType high_type = at::native::result_type(self, other);52+ auto output_size = op_infer::broadcast_ops_npu_output_size(self, other);
54- at::Tensor self_cp = self_tensor_to_device(self, high_type, output_tensor.device());53+ at::ScalarType high_type = at::native::result_type(self, other);
54+ at::Tensor self_cp = self_tensor_to_device(self, high_type, output_tensor.device());
55 55 
56- if (isIntegralType(high_type, true)) {56+ if (isIntegralType(high_type, true)) {
57- high_type = at::ScalarType::Float;57+ high_type = at::ScalarType::Float;
58- }58+ }
59- // construct the output tensor of the NPU59+ // construct the output tensor of the NPU
60- at::Tensor result =60+ at::Tensor result =
61- npu_preparation::apply_tensor_without_format(output_size, output_tensor.options().dtype(high_type));61+ npu_preparation::apply_tensor_without_format(output_size, output_tensor.options().dtype(high_type));
62 62 
63- // calculate the output result of the NPU63+ // calculate the output result of the NPU
64- div_out_npu_opapi_nocheck(self_cp, other, result);64+ div_out_npu_opapi_nocheck(self_cp, other, result);
65- return result;65+ return result;
66}66}
67 67 
68-at::Tensor true_divide(const at::Tensor &self, const at::Scalar &other) {68+at::Tensor true_divide(const at::Tensor &self, const at::Scalar &other)
69- DO_COMPATIBILITY(aclnnDivs, acl_op::true_divide(self, other));69+{
70- auto output_size = op_infer::input_same_output_size(self);70+ DO_COMPATIBILITY(aclnnDivs, acl_op::true_divide(self, other));
71- at::ScalarType high_type = at::native::result_type(self, other);71+ auto output_size = op_infer::input_same_output_size(self);
72- if (isIntegralType(high_type, true)) {72+ at::ScalarType high_type = at::native::result_type(self, other);
73- high_type = at::ScalarType::Float;73+ if (isIntegralType(high_type, true)) {
74- }74+ high_type = at::ScalarType::Float;
75- at::Tensor result =75+ }
76- npu_preparation::apply_tensor_without_format(output_size, self.options().dtype(high_type));76+ at::Tensor result =
77- EXEC_NPU_CMD(aclnnDivs, self, other, result);77+ npu_preparation::apply_tensor_without_format(output_size, self.options().dtype(high_type));
78- return result;78+ EXEC_NPU_CMD(aclnnDivs, self, other, result);
79+ return result;
79}80}
80 81 
81-at::Tensor& true_divide_out(const at::Tensor& self, const at::Tensor& other, at::Tensor& result) {82+at::Tensor& true_divide_out(const at::Tensor& self, const at::Tensor& other, at::Tensor& result)
83+{
82 DO_COMPATIBILITY(aclnnDivs, acl_op::true_divide_out(self, other, result));84 DO_COMPATIBILITY(aclnnDivs, acl_op::true_divide_out(self, other, result));
83 DO_COMPATIBILITY(aclnnDiv, acl_op::true_divide_out(self, other, result));85 DO_COMPATIBILITY(aclnnDiv, acl_op::true_divide_out(self, other, result));
84 // calculate the output size86 // calculate the output size
@@ -98,23 +100,25 @@ at::Tensor& true_divide_out(const at::Tensor& self, const at::Tensor& other, at:
98 return result;100 return result;
99}101}
100 102 
101-at::Tensor& true_divide_(at::Tensor& self, const at::Tensor& other) {103+at::Tensor& true_divide_(at::Tensor& self, const at::Tensor& other)
102- DO_COMPATIBILITY(aclnnInplaceDiv, acl_op::true_divide_(self, other));104+{
103- npu_preparation::check_memory({self, other}, {self});105+ DO_COMPATIBILITY(aclnnInplaceDiv, acl_op::true_divide_(self, other));
106+ npu_preparation::check_memory({self, other}, {self});
104 107 
105- if (other.dim() == 0 && !torch_npu::utils::is_npu(other)) {108+ if (other.dim() == 0 && !torch_npu::utils::is_npu(other)) {
106- c10::Scalar other_value = other.item();109+ c10::Scalar other_value = other.item();
107- true_divide_(self, other_value);110+ true_divide_(self, other_value);
108- } else {111+ } else {
109- EXEC_NPU_CMD(aclnnInplaceDiv, self, other);112+ EXEC_NPU_CMD(aclnnInplaceDiv, self, other);
110- }113+ }
111- return self;114+ return self;
112}115}
113 116 
114-at::Tensor& true_divide_(at::Tensor& self, const at::Scalar& other) {117+at::Tensor& true_divide_(at::Tensor& self, const at::Scalar& other)
115- DO_COMPATIBILITY(aclnnInplaceDivs, acl_op::true_divide_(self, other));118+{
116- EXEC_NPU_CMD(aclnnInplaceDivs, self, other);119+ DO_COMPATIBILITY(aclnnInplaceDivs, acl_op::true_divide_(self, other));
117- return self;120+ EXEC_NPU_CMD(aclnnInplaceDivs, self, other);
121+ return self;
118}122}
119#endif123#endif
120} // namespace op_api124} // namespace op_api
@@ -20,70 +20,75 @@
20namespace op_api {20namespace op_api {
21using npu_preparation = at_npu::native::OpPreparation;21using npu_preparation = at_npu::native::OpPreparation;
22 22 
23-at::Tensor& zeros_out(at::IntArrayRef size, at::Tensor& result) {23+at::Tensor& zeros_out(at::IntArrayRef size, at::Tensor& result)
24- DO_COMPATIBILITY(aclnnInplaceZero, acl_op::zeros_out(size, result));24+{
25- result.resize_(size);25+ DO_COMPATIBILITY(aclnnInplaceZero, acl_op::zeros_out(size, result));
26- return result.zero_();26+ result.resize_(size);
27+ return result.zero_();
27}28}
28 29 
29#if VERSION_BETWEEN(V1R11, V1R11)30#if VERSION_BETWEEN(V1R11, V1R11)
30at::Tensor zeros(at::IntArrayRef size,31at::Tensor zeros(at::IntArrayRef size,
31- c10::optional<at::ScalarType> dtype_opt,32+ c10::optional<at::ScalarType> dtype,
32- c10::optional<at::Layout> layout_opt,33+ c10::optional<at::Layout> layout,
33- c10::optional<at::Device> device_opt,34+ c10::optional<at::Device> device,
34- c10::optional<bool> pin_memory_opt) {35+ c10::optional<bool> pin_memory)
35- DO_COMPATIBILITY(aclnnInplaceZero,36+{
36- acl_op::zeros(size, dtype_opt, layout_opt, device_opt, pin_memory_opt));37+ DO_COMPATIBILITY(aclnnInplaceZero,
37- at::TensorOptions option = option.dtype(dtype_opt)38+ acl_op::zeros(size, dtype, layout, device, pin_memory));
38- .layout(layout_opt)39+ at::TensorOptions option = option.dtype(dtype)
39- .device(device_opt)40+ .layout(layout)
40- .pinned_memory(pin_memory_opt);41+ .device(device)
41- at::Tensor result = npu_preparation::apply_tensor_without_format(size, option);42+ .pinned_memory(pin_memory);
42- return result.zero_();43+ at::Tensor result = npu_preparation::apply_tensor_without_format(size, option);
44+ return result.zero_();
43}45}
44 46 
45at::Tensor zeros(47at::Tensor zeros(
46 at::IntArrayRef size,48 at::IntArrayRef size,
47 c10::optional<at::DimnameList> names,49 c10::optional<at::DimnameList> names,
48- c10::optional<at::ScalarType> dtype_opt,50+ c10::optional<at::ScalarType> dtype,
49- c10::optional<at::Layout> layout_opt,51+ c10::optional<at::Layout> layout,
50- c10::optional<at::Device> device_opt,52+ c10::optional<at::Device> device,
51- c10::optional<bool> pin_memory_opt) {53+ c10::optional<bool> pin_memory)
52- DO_COMPATIBILITY(aclnnInplaceZero,54+{
53- acl_op::zeros(size, names, dtype_opt, layout_opt, device_opt, pin_memory_opt));55+ DO_COMPATIBILITY(aclnnInplaceZero,
54- return op_api::zeros(size, dtype_opt, layout_opt, device_opt, pin_memory_opt);56+ acl_op::zeros(size, names, dtype, layout, device, pin_memory));
57+ return op_api::zeros(size, dtype, layout, device, pin_memory);
55}58}
56#endif59#endif
57 60 
58#if VERSION_BETWEEN(V2R1, VERSION_NEWEST)61#if VERSION_BETWEEN(V2R1, VERSION_NEWEST)
59at::Tensor zeros_symint(62at::Tensor zeros_symint(
60 c10::SymIntArrayRef size,63 c10::SymIntArrayRef size,
61- c10::optional<at::ScalarType> dtype_opt,64+ c10::optional<at::ScalarType> dtype,
62- c10::optional<at::Layout> layout_opt,65+ c10::optional<at::Layout> layout,
63- c10::optional<at::Device> device_opt,66+ c10::optional<at::Device> device,
64- c10::optional<bool> pin_memory_opt) {67+ c10::optional<bool> pin_memory)
65- DO_COMPATIBILITY(aclnnInplaceZero, acl_op::zeros_symint(size, dtype_opt, layout_opt, device_opt, pin_memory_opt));68+{
66- at::TensorOptions option = option.dtype(dtype_opt)69+ DO_COMPATIBILITY(aclnnInplaceZero, acl_op::zeros_symint(size, dtype, layout, device, pin_memory));
67- .layout(layout_opt)70+ at::TensorOptions option = option.dtype(dtype)
68- .device(device_opt)71+ .layout(layout)
69- .pinned_memory(pin_memory_opt);72+ .device(device)
70- at::Tensor result = npu_preparation::apply_tensor_without_format(c10::asIntArrayRefUnchecked(size), option);73+ .pinned_memory(pin_memory);
71- return result.zero_();74+ at::Tensor result = npu_preparation::apply_tensor_without_format(c10::asIntArrayRefUnchecked(size), option);
75+ return result.zero_();
72}76}
73 77 
74 78 
75at::Tensor zeros(79at::Tensor zeros(
76 at::IntArrayRef size,80 at::IntArrayRef size,
77 c10::optional<at::DimnameList> names,81 c10::optional<at::DimnameList> names,
78- c10::optional<at::ScalarType> dtype_opt,82+ c10::optional<at::ScalarType> dtype,
79- c10::optional<at::Layout> layout_opt,83+ c10::optional<at::Layout> layout,
80- c10::optional<at::Device> device_opt,84+ c10::optional<at::Device> device,
81- c10::optional<bool> pin_memory_opt) {85+ c10::optional<bool> pin_memory)
82- DO_COMPATIBILITY(aclnnInplaceZero, acl_op::zeros(size, names, dtype_opt, layout_opt, device_opt, pin_memory_opt));86+{
83- at::TensorOptions option = option.dtype(dtype_opt)87+ DO_COMPATIBILITY(aclnnInplaceZero, acl_op::zeros(size, names, dtype, layout, device, pin_memory));
84- .layout(layout_opt)88+ at::TensorOptions option = option.dtype(dtype)
85- .device(device_opt)89+ .layout(layout)
86- .pinned_memory(pin_memory_opt);90+ .device(device)
91+ .pinned_memory(pin_memory);
87 at::Tensor result = npu_preparation::apply_tensor_without_format(size, option);92 at::Tensor result = npu_preparation::apply_tensor_without_format(size, option);
88 auto maybe_name = names.value_or(at::ArrayRef<at::Dimname>{});93 auto maybe_name = names.value_or(at::ArrayRef<at::Dimname>{});
89 at::namedinference::propagate_names_if_nonempty(result, maybe_name);94 at::namedinference::propagate_names_if_nonempty(result, maybe_name);