已合并
cleancode for ops. #2247
weixin_40805272创建于 2025年3月10日
cleancode for ops. #2247
已合并
weixin_40805272创建于 2025年3月10日
refs/pull/2247/head合入到master
38 个文件变更+282-253
@@ -19,13 +19,14 @@
19namespace acl_op {19namespace acl_op {
20using npu_preparation = at_npu::native::OpPreparation;20using npu_preparation = at_npu::native::OpPreparation;
21 21 
22-std::tuple<at::Tensor, at::Tensor, at::Tensor, at::Tensor>npu_add_layer_norm_backward(const c10::optional<at::Tensor> &dy_opt,22+std::tuple<at::Tensor, at::Tensor, at::Tensor, at::Tensor>npu_add_layer_norm_backward(
23- const at::Tensor &x1,23+ const c10::optional<at::Tensor> &dy_opt,
24- const at::Tensor &x2,24+ const at::Tensor &x1,
25- const at::Tensor &rstd,25+ const at::Tensor &x2,
26- const at::Tensor &mean,26+ const at::Tensor &rstd,
27- const at::Tensor &gamma,27+ const at::Tensor &mean,
28- const c10::optional<at::Tensor> &dsum_opt)28+ const at::Tensor &gamma,
29+ const c10::optional<at::Tensor> &dsum_opt)
29{30{
30 at::SmallVector<int64_t, SIZE> shape;31 at::SmallVector<int64_t, SIZE> shape;
31 for (int64_t index = 0; index < gamma.dim(); index++) {32 for (int64_t index = 0; index < gamma.dim(); index++) {
@@ -49,7 +49,7 @@ at::Tensor& addbmm_out(
49 const at::Tensor& batch2,49 const at::Tensor& batch2,
50 const at::Scalar& beta,50 const at::Scalar& beta,
51 const at::Scalar& alpha,51 const at::Scalar& alpha,
52- at::Tensor& result)52+ at::Tensor& out)
53{53{
54 TORCH_CHECK(batch1.dim() >= 2 && batch2.dim() >= 3,54 TORCH_CHECK(batch1.dim() >= 2 && batch2.dim() >= 3,
55 "batch1 is expected to be at least 2D and batch2 is expected to be at least 3D, but got batch1: ",55 "batch1 is expected to be at least 2D and batch2 is expected to be at least 3D, but got batch1: ",
@@ -68,15 +68,15 @@ at::Tensor& addbmm_out(
68 at::Tensor bias = at::mul(self, beta);68 at::Tensor bias = at::mul(self, beta);
69 acl_op::addbmm_out_npu_nocheck(biasbmm_result, bias, mul_result, batch2);69 acl_op::addbmm_out_npu_nocheck(biasbmm_result, bias, mul_result, batch2);
70 }70 }
71- result = at::sum_to(biasbmm_result, dims);71+ out = at::sum_to(biasbmm_result, dims);
72 } else {72 } else {
73 at::Tensor mul_result = at::mul(batch1, alpha);73 at::Tensor mul_result = at::mul(batch1, alpha);
74 at::Tensor bmm_result = at::bmm(mul_result, batch2);74 at::Tensor bmm_result = at::bmm(mul_result, batch2);
75 at::Tensor sum_result = at::sum_to(bmm_result, dims);75 at::Tensor sum_result = at::sum_to(bmm_result, dims);
76 // sum_result + self*beta76 // sum_result + self*beta
77- at::add_out(result, sum_result, self, beta);77+ at::add_out(out, sum_result, self, beta);
78 }78 }
79- return result;79+ return out;
80}80}
81 81 
82at::Tensor addbmm(82at::Tensor addbmm(
@@ -26,7 +26,8 @@ at::Tensor& addcdiv_npu_nocheck(
26 const at::Tensor& self,26 const at::Tensor& self,
27 const at::Tensor& tensor1,27 const at::Tensor& tensor1,
28 const at::Tensor& tensor2,28 const at::Tensor& tensor2,
29- at::Scalar value) {29+ at::Scalar value)
30+{
30 at_npu::native::OpCommand cmd;31 at_npu::native::OpCommand cmd;
31 cmd.Name("Addcdiv")32 cmd.Name("Addcdiv")
32 .Input(self)33 .Input(self)
@@ -44,29 +45,31 @@ at::Tensor& addcdiv_out(
44 const at::Tensor& tensor1,45 const at::Tensor& tensor1,
45 const at::Tensor& tensor2,46 const at::Tensor& tensor2,
46 const at::Scalar& value,47 const at::Scalar& value,
47- at::Tensor& result) {48+ at::Tensor& out)
49+{
48 auto input_size = op_infer::broadcast_ops_npu_output_size(self, tensor1);50 auto input_size = op_infer::broadcast_ops_npu_output_size(self, tensor1);
49 auto output_size = op_infer::broadcast_ops_npu_output_size(input_size, tensor2.sizes());51 auto output_size = op_infer::broadcast_ops_npu_output_size(input_size, tensor2.sizes());
50 npu_preparation::CheckOut(52 npu_preparation::CheckOut(
51 {self, tensor1, tensor2},53 {self, tensor1, tensor2},
52- result,54+ out,
53 self,55 self,
54 output_size);56 output_size);
55- if (!npu_utils::check_match(&result)) {57+ if (!npu_utils::check_match(&out)) {
56- at::Tensor contiguous_result = npu_utils::format_contiguous(result);58+ at::Tensor contiguous_result = npu_utils::format_contiguous(out);
57 addcdiv_npu_nocheck(contiguous_result, self, tensor1, tensor2, value);59 addcdiv_npu_nocheck(contiguous_result, self, tensor1, tensor2, value);
58- npu_utils::format_fresh_view(result, contiguous_result);60+ npu_utils::format_fresh_view(out, contiguous_result);
59 } else {61 } else {
60- addcdiv_npu_nocheck(result, self, tensor1, tensor2, value);62+ addcdiv_npu_nocheck(out, self, tensor1, tensor2, value);
61 }63 }
62- return result;64+ return out;
63}65}
64 66 
65at::Tensor addcdiv(67at::Tensor addcdiv(
66 const at::Tensor& self,68 const at::Tensor& self,
67 const at::Tensor& tensor1,69 const at::Tensor& tensor1,
68 const at::Tensor& tensor2,70 const at::Tensor& tensor2,
69- const at::Scalar& value) {71+ const at::Scalar& value)
72+{
70 auto input_size = op_infer::broadcast_ops_npu_output_size(self, tensor1);73 auto input_size = op_infer::broadcast_ops_npu_output_size(self, tensor1);
71 auto output_size = op_infer::broadcast_ops_npu_output_size(input_size, tensor2.sizes());74 auto output_size = op_infer::broadcast_ops_npu_output_size(input_size, tensor2.sizes());
72 at::Tensor result = npu_preparation::apply_tensor(self, output_size);75 at::Tensor result = npu_preparation::apply_tensor(self, output_size);
@@ -102,23 +102,23 @@ tensor_list1 batch_norm_gather_stats_with_counts_npu_impl(at::Tensor &mean_all,
102}102}
103} // namespace103} // namespace
104 104 
105-tensor_list2 batch_norm_gather_stats_with_counts(const at::Tensor &self, const at::Tensor &mean,105+tensor_list2 batch_norm_gather_stats_with_counts(const at::Tensor &input, const at::Tensor &mean,
106 const at::Tensor &invstd,106 const at::Tensor &invstd,
107- const c10::optional<at::Tensor> &running_mean_opt,107+ const c10::optional<at::Tensor> &running_mean,
108- const c10::optional<at::Tensor> &running_var_opt, double momentum,108+ const c10::optional<at::Tensor> &running_var, double momentum,
109 double eps, const at::Tensor &counts)109 double eps, const at::Tensor &counts)
110{110{
111- const at::Tensor &running_mean = c10::value_or_else(running_mean_opt, [] { return at::Tensor(); });111+ const at::Tensor &running_mean_opt = c10::value_or_else(running_mean, [] { return at::Tensor(); });
112- const at::Tensor &running_var = c10::value_or_else(running_var_opt, [] { return at::Tensor(); });112+ const at::Tensor &running_var_opt = c10::value_or_else(running_var, [] { return at::Tensor(); });
113 bool is_fully_fp16 = false;113 bool is_fully_fp16 = false;
114- if (self.scalar_type() == mean.scalar_type() && self.scalar_type() == at::kHalf) {114+ if (input.scalar_type() == mean.scalar_type() && input.scalar_type() == at::kHalf) {
115 is_fully_fp16 = true;115 is_fully_fp16 = true;
116 }116 }
117 117 
118- at::Tensor mean_all = npu_preparation::apply_tensor({1, self.size(1)}, self.options().dtype(at::kFloat), self);118+ at::Tensor mean_all = npu_preparation::apply_tensor({1, input.size(1)}, input.options().dtype(at::kFloat), input);
119- at::Tensor invstd_all = npu_preparation::apply_tensor({1, self.size(1)}, self.options().dtype(at::kFloat), self);119+ at::Tensor invstd_all = npu_preparation::apply_tensor({1, input.size(1)}, input.options().dtype(at::kFloat), input);
120 120 
121- batch_norm_gather_stats_with_counts_npu_impl(mean_all, invstd_all, self, mean, invstd, running_mean, running_var,121+ batch_norm_gather_stats_with_counts_npu_impl(mean_all, invstd_all, input, mean, invstd, running_mean_opt, running_var_opt,
122 momentum, eps, counts);122 momentum, eps, counts);
123 123 
124 if (is_fully_fp16) {124 if (is_fully_fp16) {
@@ -19,14 +19,15 @@
19namespace acl_op {19namespace acl_op {
20using npu_preparation = at_npu::native::OpPreparation;20using npu_preparation = at_npu::native::OpPreparation;
21 21 
22-at::Tensor channel_shuffle(const at::Tensor& self, int64_t groups) {22+at::Tensor channel_shuffle(const at::Tensor& self, int64_t groups)
23- at::Tensor result = npu_preparation::apply_tensor(self);23+{
24- at_npu::native::OpCommand cmd;24+ at::Tensor result = npu_preparation::apply_tensor(self);
25- cmd.Name("ShuffleChannel")25+ at_npu::native::OpCommand cmd;
26- .Input(self)26+ cmd.Name("ShuffleChannel")
27- .Output(result)27+ .Input(self)
28- .Attr("group", groups)28+ .Output(result)
29- .Run();29+ .Attr("group", groups)
30- return result;30+ .Run();
31+ return result;
31}32}
32} // namespace acl_op33} // namespace acl_op
@@ -23,20 +23,21 @@ void _cummax_helper(
23 const at::Tensor& self,23 const at::Tensor& self,
24 at::Tensor& values,24 at::Tensor& values,
25 at::Tensor& indices,25 at::Tensor& indices,
26- int64_t dim) {26+ int64_t dim)
27- at::Tensor values_temp = npu_preparation::apply_tensor(self);27+{
28- at::Tensor indices_temp = npu_preparation::apply_tensor_with_format(self.sizes(), self.options().dtype(at::kLong),28+ at::Tensor values_temp = npu_preparation::apply_tensor(self);
29- ACL_FORMAT_ND);29+ at::Tensor indices_temp = npu_preparation::apply_tensor_with_format(self.sizes(), self.options().dtype(at::kLong),
30+ ACL_FORMAT_ND);
30 31 
31- at_npu::native::OpCommand cmd;32+ at_npu::native::OpCommand cmd;
32- cmd.Name("Cummax")33+ cmd.Name("Cummax")
33- .Input(self)34+ .Input(self)
34- .Output(values_temp)35+ .Output(values_temp)
35- .Output(indices_temp)36+ .Output(indices_temp)
36- .Attr("dim", dim)37+ .Attr("dim", dim)
37- .Run();38+ .Run();
38 39 
39- values.copy_(values_temp);40+ values.copy_(values_temp);
40- indices.copy_(indices_temp);41+ indices.copy_(indices_temp);
41}42}
42} // namespace acl_op43} // namespace acl_op
@@ -21,7 +21,8 @@ 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& dot_out_npu_nocheck(at::Tensor& result, const at::Tensor& self, const at::Tensor& tensor) {24+at::Tensor& dot_out_npu_nocheck(at::Tensor& result, const at::Tensor& self, const at::Tensor& tensor)
25+{
25 at_npu::native::OpCommand cmd;26 at_npu::native::OpCommand cmd;
26 cmd.Name("Dot")27 cmd.Name("Dot")
27 .Input(self)28 .Input(self)
@@ -33,7 +34,8 @@ at::Tensor& dot_out_npu_nocheck(at::Tensor& result, const at::Tensor& self, cons
33}34}
34} // namespace35} // namespace
35 36 
36-at::Tensor& dot_out(const at::Tensor& self, const at::Tensor& tensor, at::Tensor& result) {37+at::Tensor& dot_out(const at::Tensor& self, const at::Tensor& tensor, at::Tensor& result)
38+{
37 auto self_dtype = self.scalar_type();39 auto self_dtype = self.scalar_type();
38 TORCH_CHECK(self_dtype != at::kInt && self_dtype != at::kByte && self_dtype != at::kChar,40 TORCH_CHECK(self_dtype != at::kInt && self_dtype != at::kByte && self_dtype != at::kChar,
39 "'dot_npu' not implemented for 'Int'" + OPS_ERROR(ErrCode::TYPE));41 "'dot_npu' not implemented for 'Int'" + OPS_ERROR(ErrCode::TYPE));
@@ -55,7 +57,8 @@ at::Tensor& dot_out(const at::Tensor& self, const at::Tensor& tensor, at::Tensor
55 return result;57 return result;
56}58}
57 59 
58-at::Tensor dot(const at::Tensor& self, const at::Tensor& tensor) {60+at::Tensor dot(const at::Tensor& self, const at::Tensor& tensor)
61+{
59 auto self_dtype = self.scalar_type();62 auto self_dtype = self.scalar_type();
60 TORCH_CHECK(self_dtype != at::kInt && self_dtype != at::kByte && self_dtype != at::kChar,63 TORCH_CHECK(self_dtype != at::kInt && self_dtype != at::kByte && self_dtype != at::kChar,
61 "'dot_npu' not implemented for 'Int'" + OPS_ERROR(ErrCode::TYPE));64 "'dot_npu' not implemented for 'Int'" + OPS_ERROR(ErrCode::TYPE));
@@ -77,34 +77,35 @@ std::tuple<at::Tensor, at::Tensor, at::Tensor> npu_dropout_with_add_softmax_forw
77}77}
78} // namespace78} // namespace
79 79 
80-std::tuple<at::Tensor, at::Tensor> npu_dropout_with_add_softmax_backward(const at::Tensor &grad_out,80+std::tuple<at::Tensor, at::Tensor> npu_dropout_with_add_softmax_backward(const at::Tensor &grad,
81 const at::Tensor &mask,81 const at::Tensor &mask,
82 const at::Tensor &softmax_out,82 const at::Tensor &softmax_out,
83- const at::Scalar &alpha, double p, int64_t dim)83+ const at::Scalar &alpha, double prob, int64_t dim)
84{84{
85 at::Tensor result = npu_preparation::apply_tensor(softmax_out);85 at::Tensor result = npu_preparation::apply_tensor(softmax_out);
86 c10::SmallVector<int64_t, N> dimList = {dim};86 c10::SmallVector<int64_t, N> dimList = {dim};
87- double retain = 1. - p;87+ double retain = 1. - prob;
88- at::Scalar prob = at::Scalar(retain);88+ at::Scalar p = at::Scalar(retain);
89 89 
90 at_npu::native::OpCommand cmd;90 at_npu::native::OpCommand cmd;
91 cmd.Name("DropoutWithMulsAndSoftmaxGrad")91 cmd.Name("DropoutWithMulsAndSoftmaxGrad")
92- .Input(grad_out)92+ .Input(grad)
93 .Input(mask)93 .Input(mask)
94 .Input(softmax_out)94 .Input(softmax_out)
95 .Output(result)95 .Output(result)
96 .Attr("alpha", alpha)96 .Attr("alpha", alpha)
97- .Attr("input_keep_prob", prob)97+ .Attr("input_keep_prob", p)
98 .Attr("axes", dimList)98 .Attr("axes", dimList)
99 .Run();99 .Run();
100- return std::tie(result, grad_out);100+ return std::tie(result, grad);
101}101}
102 102 
103std::tuple<at::Tensor, at::Tensor, at::Tensor> npu_dropout_with_add_softmax(const at::Tensor &self,103std::tuple<at::Tensor, at::Tensor, at::Tensor> npu_dropout_with_add_softmax(const at::Tensor &self,
104 const at::Tensor &x1,104 const at::Tensor &x1,
105- const at::Scalar &alpha, double p,105+ const at::Scalar &alpha,
106+ double prob,
106 int64_t dim)107 int64_t dim)
107{108{
108- return npu_dropout_with_add_softmax_forward(self, x1, alpha, p, dim);109+ return npu_dropout_with_add_softmax_forward(self, x1, alpha, prob, dim);
109}110}
110} // namespace acl_op111} // namespace acl_op
@@ -18,14 +18,16 @@
18#include "op_plugin/utils/OpAdapter.h"18#include "op_plugin/utils/OpAdapter.h"
19 19 
20namespace acl_op {20namespace acl_op {
21-at::Tensor& gcd_out(const at::Tensor &self, const at::Tensor &other, at::Tensor &out) {21+at::Tensor& gcd_out(const at::Tensor &self, const at::Tensor &other, at::Tensor &out)
22- // convert args to cpu in order to use at::native kernel22+{
23- TORCH_NPU_WARN_ONCE("Warning: kernel [gcd.out] is not supported by NPU currently. Now this kernel is running on CPU.");23+ // convert args to cpu in order to use at::native kernel
24- const auto self_cpu = self.cpu();24+ TORCH_NPU_WARN_ONCE("Warning: kernel [gcd.out] is not supported by NPU currently."
25- const auto other_cpu = other.cpu();25+ "Now this kernel is running on CPU.");
26- auto out_cpu = out.cpu();26+ const auto self_cpu = self.cpu();
27- out_cpu = at::gcd_out(out_cpu, self_cpu, other_cpu);27+ const auto other_cpu = other.cpu();
28- out.copy_(out_cpu);28+ auto out_cpu = out.cpu();
29- return out;29+ out_cpu = at::gcd_out(out_cpu, self_cpu, other_cpu);
30+ out.copy_(out_cpu);
31+ return out;
30}32}
31} // acl_op33} // acl_op
@@ -19,14 +19,16 @@
19 19 
20namespace acl_op {20namespace acl_op {
21#if VERSION_BETWEEN(V1R11, V1R11)21#if VERSION_BETWEEN(V1R11, V1R11)
22-at::Tensor gelu_backward(const at::Tensor& grad, const at::Tensor& self) {22+at::Tensor gelu_backward(const at::Tensor& grad_output, const at::Tensor& self)
23- return gelu_backward_common_nocheck(grad, self);23+{
24+ return gelu_backward_common_nocheck(grad_output, self);
24}25}
25#endif26#endif
26 27 
27#if VERSION_BETWEEN(V2R0, VERSION_NEWEST)28#if VERSION_BETWEEN(V2R0, VERSION_NEWEST)
28-at::Tensor gelu_backward(const at::Tensor& grad, const at::Tensor& self, c10::string_view approximate) {29+at::Tensor gelu_backward(const at::Tensor& grad_output, const at::Tensor& self, c10::string_view approximate)
29- return gelu_backward_common_nocheck(grad, self);30+{
31+ return gelu_backward_common_nocheck(grad_output, self);
30}32}
31#endif33#endif
32} // namespace acl_op34} // namespace acl_op
@@ -21,13 +21,14 @@ namespace acl_op {
21using npu_preparation = at_npu::native::OpPreparation;21using npu_preparation = at_npu::native::OpPreparation;
22 22 
23at::Tensor& hardshrink_backward_out(23at::Tensor& hardshrink_backward_out(
24- const at::Tensor& grad_output,24+ const at::Tensor& grad_out,
25 const at::Tensor& self,25 const at::Tensor& self,
26 const at::Scalar& lambd,26 const at::Scalar& lambd,
27- at::Tensor& grad_input) {27+ at::Tensor& grad_input)
28+{
28 at_npu::native::OpCommand cmd;29 at_npu::native::OpCommand cmd;
29 cmd.Name("HardShrinkGrad")30 cmd.Name("HardShrinkGrad")
30- .Input(grad_output)31+ .Input(grad_out)
31 .Input(self)32 .Input(self)
32 .Attr("lambd", lambd)33 .Attr("lambd", lambd)
33 .Output(grad_input)34 .Output(grad_input)
@@ -36,11 +37,12 @@ at::Tensor& hardshrink_backward_out(
36}37}
37 38 
38at::Tensor hardshrink_backward(39at::Tensor hardshrink_backward(
39- const at::Tensor& grad_output,40+ const at::Tensor& grad_out,
40 const at::Tensor& self,41 const at::Tensor& self,
41- const at::Scalar& lambd) {42+ const at::Scalar& lambd)
43+{
42 at::Tensor grad_input = npu_preparation::apply_tensor(self);44 at::Tensor grad_input = npu_preparation::apply_tensor(self);
43- hardshrink_backward_out(grad_output, self, lambd, grad_input);45+ hardshrink_backward_out(grad_out, self, lambd, grad_input);
44 return grad_input;46 return grad_input;
45}47}
46} // namespace acl_op48} // namespace acl_op
@@ -21,11 +21,11 @@ namespace acl_op {
21using npu_preparation = at_npu::native::OpPreparation;21using npu_preparation = at_npu::native::OpPreparation;
22 22 
23at::Tensor npu_layer_norm_eval(const at::Tensor &input, at::IntArrayRef normalized_shape,23at::Tensor npu_layer_norm_eval(const at::Tensor &input, at::IntArrayRef normalized_shape,
24- const c10::optional<at::Tensor> &weight_opt, const c10::optional<at::Tensor> &bias_opt,24+ const c10::optional<at::Tensor> &weight, const c10::optional<at::Tensor> &bias,
25 double eps)25 double eps)
26{26{
27- const at::Tensor &weight = c10::value_or_else(weight_opt, [] { return at::Tensor(); });27+ const at::Tensor &weight_opt = c10::value_or_else(weight, [] { return at::Tensor(); });
28- const at::Tensor &bias = c10::value_or_else(bias_opt, [] { return at::Tensor(); });28+ const at::Tensor &bias_opt = c10::value_or_else(bias, [] { return at::Tensor(); });
29 const int normalized_ndim = static_cast<int>(normalized_shape.size());29 const int normalized_ndim = static_cast<int>(normalized_shape.size());
30 const auto input_shape = input.sizes();30 const auto input_shape = input.sizes();
31 const auto input_ndim = input.dim();31 const auto input_ndim = input.dim();
@@ -46,8 +46,8 @@ at::Tensor npu_layer_norm_eval(const at::Tensor &input, at::IntArrayRef normaliz
46 }46 }
47 47 
48 std::reverse(tmp_size.begin(), tmp_size.end());48 std::reverse(tmp_size.begin(), tmp_size.end());
49- at::Tensor resize_weight = weight.defined() ? weight.detach().clone() : at::Tensor();49+ at::Tensor resize_weight = weight_opt.defined() ? weight_opt.detach().clone() : at::Tensor();
50- at::Tensor resize_bias = bias.defined() ? bias.detach().clone() : at::Tensor();50+ at::Tensor resize_bias = bias_opt.defined() ? bias_opt.detach().clone() : at::Tensor();
51 if (!resize_weight.defined()) {51 if (!resize_weight.defined()) {
52 resize_weight = at::ones(tmp_size, input.options());52 resize_weight = at::ones(tmp_size, input.options());
53 } else if (!resize_weight.sizes().equals(tmp_size)) {53 } else if (!resize_weight.sizes().equals(tmp_size)) {
@@ -38,21 +38,21 @@ at::Tensor &linalg_cross_out_nocheck(at::Tensor &result, const at::Tensor &self,
38}38}
39} // namespace39} // namespace
40 40 
41-at::Tensor &linalg_cross_out(const at::Tensor &self, const at::Tensor &other, const int64_t dim, at::Tensor &result)41+at::Tensor &linalg_cross_out(const at::Tensor &self, const at::Tensor &other, const int64_t dim, at::Tensor &out)
42{42{
43 auto output_size = op_infer::broadcast_ops_npu_output_size(self, other);43 auto output_size = op_infer::broadcast_ops_npu_output_size(self, other);
44 at::Tensor output_tensor = linalg_cross_dest_output(self, other);44 at::Tensor output_tensor = linalg_cross_dest_output(self, other);
45- npu_preparation::CheckOut({self}, result, npu_preparation::get_tensor_npu_format(output_tensor), self.scalar_type(),45+ npu_preparation::CheckOut({self}, out, npu_preparation::get_tensor_npu_format(output_tensor), self.scalar_type(),
46 output_size);46 output_size);
47 47 
48- if (!npu_utils::check_match(&result)) {48+ if (!npu_utils::check_match(&out)) {
49- at::Tensor contiguous_result = npu_utils::format_contiguous(result);49+ at::Tensor contiguous_result = npu_utils::format_contiguous(out);
50 linalg_cross_out_nocheck(contiguous_result, self, other, dim);50 linalg_cross_out_nocheck(contiguous_result, self, other, dim);
51- npu_utils::format_fresh_view(result, contiguous_result);51+ npu_utils::format_fresh_view(out, contiguous_result);
52 } else {52 } else {
53- linalg_cross_out_nocheck(result, self, other, dim);53+ linalg_cross_out_nocheck(out, self, other, dim);
54 }54 }
55- return result;55+ return out;
56}56}
57 57 
58at::Tensor linalg_cross(const at::Tensor &self, const at::Tensor &other, const int64_t dim)58at::Tensor linalg_cross(const at::Tensor &self, const at::Tensor &other, const int64_t dim)
@@ -28,27 +28,27 @@ at::Tensor &log_out_npu_nocheck(at::Tensor &result, const at::Tensor &self)
28 cmd.Name("Log")28 cmd.Name("Log")
29 .Input(self)29 .Input(self)
30 .Output(result)30 .Output(result)
31- .Attr("base", (float)-1)31+ .Attr("base", static_cast<float>(-1))
32- .Attr("scale", (float)1)32+ .Attr("scale", static_cast<float>(1))
33- .Attr("shift", (float)0)33+ .Attr("shift", static_cast<float>(0))
34 .Run();34 .Run();
35 35 
36 return result;36 return result;
37}37}
38} // namespace38} // namespace
39 39 
40-at::Tensor &log_out(const at::Tensor &self, at::Tensor &result)40+at::Tensor &log_out(const at::Tensor &self, at::Tensor &out)
41{41{
42- npu_preparation::CheckOut({self}, result, ACL_FORMAT_ND, self.scalar_type(), self.sizes());42+ npu_preparation::CheckOut({self}, out, ACL_FORMAT_ND, self.scalar_type(), self.sizes());
43 43 
44- if (!npu_utils::check_match(&result)) {44+ if (!npu_utils::check_match(&out)) {
45- at::Tensor contiguous_result = npu_utils::format_contiguous(result);45+ at::Tensor contiguous_result = npu_utils::format_contiguous(out);
46 log_out_npu_nocheck(contiguous_result, self);46 log_out_npu_nocheck(contiguous_result, self);
47- npu_utils::format_fresh_view(result, contiguous_result);47+ npu_utils::format_fresh_view(out, contiguous_result);
48 } else {48 } else {
49- log_out_npu_nocheck(result, self);49+ log_out_npu_nocheck(out, self);
50 }50 }
51- return result;51+ return out;
52}52}
53 53 
54at::Tensor log(const at::Tensor &self)54at::Tensor log(const at::Tensor &self)
@@ -20,14 +20,15 @@
20namespace acl_op {20namespace acl_op {
21using npu_preparation = at_npu::native::OpPreparation;21using npu_preparation = at_npu::native::OpPreparation;
22 22 
23-at::Tensor mish_backward(const at::Tensor& grad_output, const at::Tensor& self) {23+at::Tensor mish_backward(const at::Tensor& grad_output, const at::Tensor& self)
24- at::Tensor grad_input = npu_preparation::apply_tensor(self);24+{
25- at_npu::native::OpCommand cmd;25+ at::Tensor grad_input = npu_preparation::apply_tensor(self);
26- cmd.Name("MishGrad")26+ at_npu::native::OpCommand cmd;
27- .Input(grad_output)27+ cmd.Name("MishGrad")
28- .Input(self)28+ .Input(grad_output)
29- .Output(grad_input)29+ .Input(self)
30- .Run();30+ .Output(grad_input)
31- return grad_input;31+ .Run();
32+ return grad_input;
32}33}
33} // namespace acl_op34} // namespace acl_op
@@ -24,11 +24,12 @@ using npu_preparation = at_npu::native::OpPreparation;
24std::tuple<at::Tensor, at::Tensor> prelu_backward(24std::tuple<at::Tensor, at::Tensor> prelu_backward(
25 const at::Tensor& grad_output,25 const at::Tensor& grad_output,
26 const at::Tensor& self,26 const at::Tensor& self,
27- const at::Tensor& weight) {27+ const at::Tensor& weight)
28- at::Tensor grad_input = npu_preparation::apply_tensor(self);28+{
29- at::Tensor grad_weight = npu_preparation::apply_tensor(weight);29+ at::Tensor grad_input = npu_preparation::apply_tensor(self);
30- prelu_backward_commom_nocheck(grad_input, grad_weight, grad_output, self, weight);30+ at::Tensor grad_weight = npu_preparation::apply_tensor(weight);
31- return std::tie<at::Tensor, at::Tensor>(grad_input, grad_weight);31+ prelu_backward_commom_nocheck(grad_input, grad_weight, grad_output, self, weight);
32+ return std::tie<at::Tensor, at::Tensor>(grad_input, grad_weight);
32}33}
33#endif34#endif
34 35 
@@ -36,17 +37,18 @@ std::tuple<at::Tensor, at::Tensor> prelu_backward(
36std::tuple<at::Tensor, at::Tensor> _prelu_kernel_backward(37std::tuple<at::Tensor, at::Tensor> _prelu_kernel_backward(
37 const at::Tensor& grad_output,38 const at::Tensor& grad_output,
38 const at::Tensor& self,39 const at::Tensor& self,
39- const at::Tensor& weight) {40+ const at::Tensor& weight)
40- c10::SmallVector<int64_t, N> weight_shape = op_infer::array_to_small_vector(weight.sizes());41+{
41- at::Tensor reshape_weight = weight.reshape({-1});42+ c10::SmallVector<int64_t, N> weight_shape = op_infer::array_to_small_vector(weight.sizes());
43+ at::Tensor reshape_weight = weight.reshape({-1});
42 44 
43- at::Tensor grad_input = npu_preparation::apply_tensor(self);45+ at::Tensor grad_input = npu_preparation::apply_tensor(self);
44- at::Tensor grad_weight = npu_preparation::apply_tensor(reshape_weight);46+ at::Tensor grad_weight = npu_preparation::apply_tensor(reshape_weight);
45 47 
46- prelu_backward_commom_nocheck(grad_input, grad_weight, grad_output, self, reshape_weight);48+ prelu_backward_commom_nocheck(grad_input, grad_weight, grad_output, self, reshape_weight);
47- grad_weight = grad_weight.reshape(weight_shape);49+ grad_weight = grad_weight.reshape(weight_shape);
48 50 
49- return std::tie<at::Tensor, at::Tensor>(grad_input, grad_weight);51+ return std::tie<at::Tensor, at::Tensor>(grad_input, grad_weight);
50}52}
51#endif53#endif
52} // namespace acl_op54} // namespace acl_op
@@ -21,25 +21,26 @@ namespace acl_op {
21using npu_preparation = at_npu::native::OpPreparation;21using npu_preparation = at_npu::native::OpPreparation;
22 22 
23std::tuple<at::Tensor, at::Tensor> npu_random_choice_with_mask(23std::tuple<at::Tensor, at::Tensor> npu_random_choice_with_mask(
24- const at::Tensor& self,24+ const at::Tensor& x,
25 int64_t count,25 int64_t count,
26 int64_t seed,26 int64_t seed,
27- int64_t seed2) {27+ int64_t seed2)
28+{
28 TORCH_CHECK(29 TORCH_CHECK(
29- self.scalar_type() == at::ScalarType::Bool,30+ x.scalar_type() == at::ScalarType::Bool,
30 "The input.dtype should be bool, but get",31 "The input.dtype should be bool, but get",
31- self.scalar_type(), OPS_ERROR(ErrCode::TYPE));32+ x.scalar_type(), OPS_ERROR(ErrCode::TYPE));
32 TORCH_CHECK(33 TORCH_CHECK(
33- self.dim() <= 5 && self.dim() >= 1,34+ x.dim() <= 5 && x.dim() >= 1,
34 "The input.dim should be in [1, 5], but get",35 "The input.dim should be in [1, 5], but get",
35- self.dim(), OPS_ERROR(ErrCode::PARAM));36+ x.dim(), OPS_ERROR(ErrCode::PARAM));
36 TORCH_CHECK(count > 0, "The count must greater than 0, but get", count, OPS_ERROR(ErrCode::VALUE));37 TORCH_CHECK(count > 0, "The count must greater than 0, but get", count, OPS_ERROR(ErrCode::VALUE));
37 38 
38- at::Tensor result = npu_preparation::apply_tensor({count, self.dim()}, self.options().dtype(at::kInt), self);39+ at::Tensor result = npu_preparation::apply_tensor({count, x.dim()}, x.options().dtype(at::kInt), x);
39- at::Tensor mask = npu_preparation::apply_tensor(self, {count});40+ at::Tensor mask = npu_preparation::apply_tensor(x, {count});
40 at_npu::native::OpCommand cmd;41 at_npu::native::OpCommand cmd;
41 cmd.Name("RandomChoiceWithMask")42 cmd.Name("RandomChoiceWithMask")
42- .Input(self)43+ .Input(x)
43 .Output(result)44 .Output(result)
44 .Output(mask)45 .Output(mask)
45 .Attr("count", count)46 .Attr("count", count)
@@ -38,7 +38,7 @@ at::Tensor &roi_align_npu_nocheck(at::Tensor &result, const at::Tensor &self, co
38 .Input(self, "features")38 .Input(self, "features")
39 .Input(rois)39 .Input(rois)
40 .Output(result, "y")40 .Output(result, "y")
41- .Attr("spatial_scale", (float)spatial_scale)41+ .Attr("spatial_scale", static_cast<float>(spatial_scale))
42 .Attr("pooled_height", pooled_height)42 .Attr("pooled_height", pooled_height)
43 .Attr("pooled_width", pooled_width)43 .Attr("pooled_width", pooled_width)
44 .Attr("sample_num", sample_num)44 .Attr("sample_num", sample_num)
@@ -21,21 +21,22 @@ using npu_preparation = at_npu::native::OpPreparation;
21 21 
22at::Tensor npu_rotated_box_encode(22at::Tensor npu_rotated_box_encode(
23 const at::Tensor& self,23 const at::Tensor& self,
24- const at::Tensor& gtBox,24+ const at::Tensor& gt_bboxes,
25- const at::Tensor& weight) {25+ const at::Tensor& weight)
26- at::Tensor result = npu_preparation::apply_tensor(self);26+{
27- at::Tensor weight_cpu = weight.to(at::Device(at::kCPU), at::kFloat);27+ at::Tensor result = npu_preparation::apply_tensor(self);
28- auto weight_ptr = weight_cpu.data_ptr<float>();28+ at::Tensor weight_cpu = weight.to(at::Device(at::kCPU), at::kFloat);
29- TORCH_CHECK(weight_ptr != nullptr, "weight_cpu is null" + OPS_ERROR(ErrCode::VALUE))29+ auto weight_ptr = weight_cpu.data_ptr<float>();
30- at::ArrayRef<float> weight_list(weight_ptr, weight_cpu.numel());30+ TORCH_CHECK(weight_ptr != nullptr, "weight_cpu is null" + OPS_ERROR(ErrCode::VALUE))
31+ at::ArrayRef<float> weight_list(weight_ptr, weight_cpu.numel());
31 32 
32- at_npu::native::OpCommand cmd;33+ at_npu::native::OpCommand cmd;
33- cmd.Name("RotatedBoxEncode")34+ cmd.Name("RotatedBoxEncode")
34- .Input(self)35+ .Input(self)
35- .Input(gtBox)36+ .Input(gt_bboxes)
36- .Output(result)37+ .Output(result)
37- .Attr("weight", weight_list)38+ .Attr("weight", weight_list)
38- .Run();39+ .Run();
39- return result;40+ return result;
40}41}
41} // namespace acl_op42} // namespace acl_op
@@ -19,18 +19,19 @@
19namespace acl_op {19namespace acl_op {
20using npu_preparation = at_npu::native::OpPreparation;20using npu_preparation = at_npu::native::OpPreparation;
21 21 
22-at::Tensor npu_scatter(const at::Tensor& self, const at::Tensor& indices, const at::Tensor& updates, int64_t dim) {22+at::Tensor npu_scatter(const at::Tensor& self, const at::Tensor& indices, const at::Tensor& updates, int64_t dim)
23- at::Tensor outputs = npu_preparation::apply_tensor(self);23+{
24- at_npu::native::OpCommand cmd;24+ at::Tensor outputs = npu_preparation::apply_tensor(self);
25- cmd.Name("ArgMaxGrad")25+ at_npu::native::OpCommand cmd;
26- .Input(self)26+ cmd.Name("ArgMaxGrad")
27- .Input(indices)27+ .Input(self)
28- .Input(updates)28+ .Input(indices)
29- .Output(outputs)29+ .Input(updates)
30- .Attr("dimension", dim)30+ .Output(outputs)
31- .Run();31+ .Attr("dimension", dim)
32+ .Run();
32 33 
33- return outputs;34+ return outputs;
34}35}
35 36 
36} // namespace acl_op37} // namespace acl_op
@@ -48,12 +48,13 @@ at::Tensor silent_check_nocheck(at::Tensor &input_grad, const at::Tensor &val, a
48}48}
49} // namespace49} // namespace
50 50 
51-at::Tensor _npu_silent_check(at::Tensor &input_grad, const at::Tensor &val, at::Tensor &pre_val, at::Tensor &min_val,51+at::Tensor _npu_silent_check(at::Tensor &input_grad, const at::Tensor &val, at::Tensor &pre_val,
52- at::Tensor &max_val, const at::Tensor &val_counter, int64_t c_min_steps, double c_thresh_l1,52+ at::Tensor &min_val, at::Tensor &max_val, const at::Tensor &val_counter,
53- double c_coeff_l1, double c_thresh_l2, double c_coeff_l2)53+ int64_t c_min_steps, double c_thresh_l1, double c_coeff_l1, double c_thresh_l2,
54+ double c_coeff_l2)
54{55{
55 at::Tensor result = npu_preparation::apply_tensor(val_counter);56 at::Tensor result = npu_preparation::apply_tensor(val_counter);
56 return silent_check_nocheck(input_grad, val, pre_val, min_val, max_val, val_counter, c_min_steps, c_thresh_l1,57 return silent_check_nocheck(input_grad, val, pre_val, min_val, max_val, val_counter, c_min_steps, c_thresh_l1,
57 c_coeff_l1, c_thresh_l2, c_coeff_l2, result);58 c_coeff_l1, c_thresh_l2, c_coeff_l2, result);
58}59}
59-} // namespace acl_op60+} // namespace acl_op
@@ -13,15 +13,15 @@
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"
17#include "op_plugin/utils/OpAdapter.h"16#include "op_plugin/utils/OpAdapter.h"
18 17 
19namespace acl_op {18namespace acl_op {
20 19 
21#if VERSION_BETWEEN(V1R11, V1R11)20#if VERSION_BETWEEN(V1R11, V1R11)
22-at::Tensor& square_out(const at::Tensor& self, at::Tensor& result) {21+at::Tensor& square_out(const at::Tensor& self, at::Tensor& result)
23- at::Scalar exp = 2;22+{
24- return acl_op::pow_out(self, exp, result);23+ at::Scalar exp = 2;
24+ return acl_op::pow_out(self, exp, result);
25}25}
26#endif26#endif
27 27 
@@ -60,26 +60,26 @@ at::Tensor &stack_out_nocheck(at::Tensor &result, at::TensorList tensors, int64_
60 string input_name = "x" + std::to_string(i);60 string input_name = "x" + std::to_string(i);
61 cmd.Input(input_tensors[i], input_name);61 cmd.Input(input_tensors[i], input_name);
62 }62 }
63- cmd.Output(result).Attr("N", (int64_t)tensors.size()).Attr("axis", dim).Run();63+ cmd.Output(result).Attr("N", static_cast<int64_t>(tensors.size())).Attr("axis", dim).Run();
64 64 
65 return result;65 return result;
66}66}
67} // namespace67} // namespace
68 68 
69-at::Tensor &stack_out(at::TensorList tensors, int64_t dim, at::Tensor &result)69+at::Tensor &stack_out(at::TensorList tensors, int64_t dim, at::Tensor &out)
70{70{
71 auto output_size = stack_npu_output_size(tensors, dim);71 auto output_size = stack_npu_output_size(tensors, dim);
72 72 
73- npu_preparation::CheckOut({tensors[0]}, result, ACL_FORMAT_ND, tensors[0].scalar_type(), output_size);73+ npu_preparation::CheckOut({tensors[0]}, out, ACL_FORMAT_ND, tensors[0].scalar_type(), output_size);
74- if (!npu_utils::check_match(&result)) {74+ if (!npu_utils::check_match(&out)) {
75- at::Tensor contiguous_result = npu_utils::format_contiguous(result);75+ at::Tensor contiguous_result = npu_utils::format_contiguous(out);
76 stack_out_nocheck(contiguous_result, tensors, dim);76 stack_out_nocheck(contiguous_result, tensors, dim);
77- npu_utils::format_fresh_view(result, contiguous_result);77+ npu_utils::format_fresh_view(out, contiguous_result);
78 } else {78 } else {
79- stack_out_nocheck(result, tensors, dim);79+ stack_out_nocheck(out, tensors, dim);
80 }80 }
81 81 
82- return result;82+ return out;
83}83}
84 84 
85at::Tensor stack(at::TensorList tensors, int64_t dim)85at::Tensor stack(at::TensorList tensors, int64_t dim)
@@ -22,15 +22,16 @@ using npu_preparation = at_npu::native::OpPreparation;
22at::Tensor npu_sub_sample(22at::Tensor npu_sub_sample(
23 const at::Tensor& self,23 const at::Tensor& self,
24 int64_t per_images,24 int64_t per_images,
25- double positive_fraction) {25+ double positive_fraction)
26- at::Tensor result = npu_preparation::apply_tensor(self);26+{
27- at_npu::native::OpCommand cmd;27+ at::Tensor result = npu_preparation::apply_tensor(self);
28- cmd.Name("SubSample")28+ at_npu::native::OpCommand cmd;
29- .Input(self)29+ cmd.Name("SubSample")
30- .Output(result)30+ .Input(self)
31- .Attr("batch_size_per_images", per_images)31+ .Output(result)
32- .Attr("positive_fraction", (float)positive_fraction)32+ .Attr("batch_size_per_images", per_images)
33- .Run();33+ .Attr("positive_fraction", (float)positive_fraction)
34- return result;34+ .Run();
35+ return result;
35}36}
36} // namespace acl_op37} // namespace acl_op
@@ -22,7 +22,8 @@ using npu_preparation = at_npu::native::OpPreparation;
22#if VERSION_BETWEEN(V1R11, V1R11) || VERSION_BETWEEN(V2R1, VERSION_NEWEST)22#if VERSION_BETWEEN(V1R11, V1R11) || VERSION_BETWEEN(V2R1, VERSION_NEWEST)
23at::Tensor trace(const at::Tensor &self)23at::Tensor trace(const at::Tensor &self)
24{24{
25- TORCH_CHECK(self.dim() == 2, "trace: expected a matrix, but got tensor with dim ", self.dim(), OPS_ERROR(ErrCode::PARAM));25+ TORCH_CHECK(self.dim() == 2, "trace: expected a matrix, but got tensor with dim ",
26+ self.dim(), OPS_ERROR(ErrCode::PARAM));
26 c10::SmallVector<int64_t, N> outputSize = {};27 c10::SmallVector<int64_t, N> outputSize = {};
27 auto outDtype = (isIntegralType(self.scalar_type(), true)) ? at::kLong : self.scalar_type();28 auto outDtype = (isIntegralType(self.scalar_type(), true)) ? at::kLong : self.scalar_type();
28 at::Tensor result = npu_preparation::apply_tensor(outputSize, self.options().dtype(outDtype), self);29 at::Tensor result = npu_preparation::apply_tensor(outputSize, self.options().dtype(outDtype), self);
@@ -21,15 +21,15 @@
21 21 
22namespace acl_op {22namespace acl_op {
23std::tuple<at::Tensor &, at::Tensor &> triangular_solve_out(const at::Tensor &self, const at::Tensor &A, bool upper,23std::tuple<at::Tensor &, at::Tensor &> triangular_solve_out(const at::Tensor &self, const at::Tensor &A, bool upper,
24- bool transpose, bool unitriangular, at::Tensor &result,24+ bool transpose, bool unitriangular, at::Tensor &X,
25- at::Tensor &clone_a)25+ at::Tensor &M)
26{26{
27 at::Tensor result_tmp;27 at::Tensor result_tmp;
28 at::Tensor clone_a_tmp;28 at::Tensor clone_a_tmp;
29 std::tie(result_tmp, clone_a_tmp) = triangular_solve_out_common_nocheck(self, A, upper, transpose, unitriangular);29 std::tie(result_tmp, clone_a_tmp) = triangular_solve_out_common_nocheck(self, A, upper, transpose, unitriangular);
30- result.resize_as_(result_tmp).copy_(result_tmp);30+ X.resize_as_(result_tmp).copy_(result_tmp);
31- clone_a.resize_as_(clone_a_tmp).copy_(clone_a_tmp);31+ M.resize_as_(clone_a_tmp).copy_(clone_a_tmp);
32- return std::tie(result, clone_a);32+ return std::tie(X, M);
33}33}
34 34 
35#if VERSION_BETWEEN(V2R0, V2R0)35#if VERSION_BETWEEN(V2R0, V2R0)
@@ -77,16 +77,16 @@ bool AicoreValid(at::Tensor &self, const at::Tensor &src)
77}77}
78} // namespace78} // namespace
79 79 
80-at::Tensor &npu_view_copy(at::Tensor &self, const at::Tensor &src, bool non_blocking)80+at::Tensor &npu_view_copy(at::Tensor &self, const at::Tensor &other, bool non_blocking)
81{81{
82 auto self_size = self.sizes();82 auto self_size = self.sizes();
83 auto self_stride = self.strides();83 auto self_stride = self.strides();
84- auto src_size = src.sizes();84+ auto src_size = other.sizes();
85- auto src_stride = src.strides();85+ auto src_stride = other.strides();
86 86 
87 at_npu::native::OpCommand cmd;87 at_npu::native::OpCommand cmd;
88- if (AicoreValid(self, src)) {88+ if (AicoreValid(self, other)) {
89- at::Tensor contiguous_src(src);89+ at::Tensor contiguous_src(other);
90 if (!npu_utils::check_match(&contiguous_src)) {90 if (!npu_utils::check_match(&contiguous_src)) {
91 contiguous_src = npu_utils::format_contiguous(contiguous_src);91 contiguous_src = npu_utils::format_contiguous(contiguous_src);
92 }92 }
@@ -109,12 +109,12 @@ at::Tensor &npu_view_copy(at::Tensor &self, const at::Tensor &src, bool non_bloc
109 .Input(self_size, at::kLong, npu_compile_type::MEMORY_HOST_COMPILE_INDEPENDENT)109 .Input(self_size, at::kLong, npu_compile_type::MEMORY_HOST_COMPILE_INDEPENDENT)
110 .Input(self_stride, at::kLong, npu_compile_type::MEMORY_HOST_COMPILE_INDEPENDENT)110 .Input(self_stride, at::kLong, npu_compile_type::MEMORY_HOST_COMPILE_INDEPENDENT)
111 .Input(at::Scalar(0), at::kLong)111 .Input(at::Scalar(0), at::kLong)
112- .InputWithoutContiguous(src)112+ .InputWithoutContiguous(other)
113 .Input(src_size, at::kLong, npu_compile_type::MEMORY_HOST_COMPILE_INDEPENDENT)113 .Input(src_size, at::kLong, npu_compile_type::MEMORY_HOST_COMPILE_INDEPENDENT)
114 .Input(src_stride, at::kLong, npu_compile_type::MEMORY_HOST_COMPILE_INDEPENDENT)114 .Input(src_stride, at::kLong, npu_compile_type::MEMORY_HOST_COMPILE_INDEPENDENT)
115 .Input(at::Scalar(0), at::kLong)115 .Input(at::Scalar(0), at::kLong)
116 .Output(self)116 .Output(self)
117- .Attr("_exclude_engines", (string) "AiCore")117+ .Attr("_exclude_engines", static_cast<string>("AiCore"))
118 .Run();118 .Run();
119 }119 }
120 120 
@@ -43,48 +43,48 @@ at::Tensor &xlogy_out_npu_nocheck(at::Tensor &result, const at::Scalar &self, co
43}43}
44} // namespace44} // namespace
45 45 
46-at::Tensor &xlogy_out(const at::Tensor &self, const at::Tensor &other, at::Tensor &result)46+at::Tensor &xlogy_out(const at::Tensor &self, const at::Tensor &other, at::Tensor &out)
47{47{
48 at::Tensor format_cast_of_self = npu_preparation::CastBackToOriFormat(self);48 at::Tensor format_cast_of_self = npu_preparation::CastBackToOriFormat(self);
49 at::Tensor format_cast_of_other = npu_preparation::CastBackToOriFormat(other);49 at::Tensor format_cast_of_other = npu_preparation::CastBackToOriFormat(other);
50 auto output_size = op_infer::broadcast_ops_npu_output_size(self, other);50 auto output_size = op_infer::broadcast_ops_npu_output_size(self, other);
51- npu_preparation::CheckOut({self, other}, result, npu_preparation::get_tensor_npu_format(format_cast_of_self),51+ npu_preparation::CheckOut({self, other}, out, npu_preparation::get_tensor_npu_format(format_cast_of_self),
52- result.scalar_type(), output_size);52+ out.scalar_type(), output_size);
53- if (!npu_utils::check_match(&result)) {53+ if (!npu_utils::check_match(&out)) {
54- at::Tensor contiguous_result = npu_utils::format_contiguous(result);54+ at::Tensor contiguous_result = npu_utils::format_contiguous(out);
55 xlogy_out_npu_nocheck(contiguous_result, format_cast_of_self, format_cast_of_other);55 xlogy_out_npu_nocheck(contiguous_result, format_cast_of_self, format_cast_of_other);
56- npu_utils::format_fresh_view(result, contiguous_result);56+ npu_utils::format_fresh_view(out, contiguous_result);
57 } else {57 } else {
58- xlogy_out_npu_nocheck(result, format_cast_of_self, format_cast_of_other);58+ xlogy_out_npu_nocheck(out, format_cast_of_self, format_cast_of_other);
59 }59 }
60- return result;60+ return out;
61}61}
62 62 
63-at::Tensor &xlogy_out(const at::Tensor &self, const at::Scalar &other, at::Tensor &result)63+at::Tensor &xlogy_out(const at::Tensor &self, const at::Scalar &other, at::Tensor &out)
64{64{
65- npu_preparation::CheckOut({self}, result, self);65+ npu_preparation::CheckOut({self}, out, self);
66- if (!npu_utils::check_match(&result)) {66+ if (!npu_utils::check_match(&out)) {
67- at::Tensor contiguous_result = npu_utils::format_contiguous(result);67+ at::Tensor contiguous_result = npu_utils::format_contiguous(out);
68 xlogy_out_npu_nocheck(contiguous_result, self, other);68 xlogy_out_npu_nocheck(contiguous_result, self, other);
69- npu_utils::format_fresh_view(result, contiguous_result);69+ npu_utils::format_fresh_view(out, contiguous_result);
70 } else {70 } else {
71- xlogy_out_npu_nocheck(result, self, other);71+ xlogy_out_npu_nocheck(out, self, other);
72 }72 }
73- return result;73+ return out;
74}74}
75 75 
76-at::Tensor &xlogy_out(const at::Scalar &self, const at::Tensor &other, at::Tensor &result)76+at::Tensor &xlogy_out(const at::Scalar &self, const at::Tensor &other, at::Tensor &out)
77{77{
78- npu_preparation::CheckOut({other}, result, npu_preparation::get_tensor_npu_format(other), other.scalar_type(),78+ npu_preparation::CheckOut({other}, out, npu_preparation::get_tensor_npu_format(other), other.scalar_type(),
79 other.sizes());79 other.sizes());
80- if (!npu_utils::check_match(&result)) {80+ if (!npu_utils::check_match(&out)) {
81- at::Tensor contiguous_result = npu_utils::format_contiguous(result);81+ at::Tensor contiguous_result = npu_utils::format_contiguous(out);
82 xlogy_out_npu_nocheck(contiguous_result, self, other);82 xlogy_out_npu_nocheck(contiguous_result, self, other);
83- npu_utils::format_fresh_view(result, contiguous_result);83+ npu_utils::format_fresh_view(out, contiguous_result);
84 } else {84 } else {
85- xlogy_out_npu_nocheck(result, self, other);85+ xlogy_out_npu_nocheck(out, self, other);
86 }86 }
87- return result;87+ return out;
88}88}
89 89 
90at::Tensor xlogy(const at::Tensor &self, const at::Tensor &other)90at::Tensor xlogy(const at::Tensor &self, const at::Tensor &other)
@@ -13,9 +13,9 @@
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/native/ForeachUtils.h>
16#include "op_plugin/OpApiInterface.h"17#include "op_plugin/OpApiInterface.h"
17#include "op_plugin/utils/op_api_common.h"18#include "op_plugin/utils/op_api_common.h"
18-#include <ATen/native/ForeachUtils.h>
19 19 
20namespace op_api {20namespace op_api {
21using npu_preparation = at_npu::native::OpPreparation;21using npu_preparation = at_npu::native::OpPreparation;
@@ -26,11 +26,11 @@ at::Tensor bucketize(const at::Tensor& self, const at::Tensor& boundaries, bool
26 return op_api::searchsorted(boundaries, self, out_int32, right, c10::nullopt, c10::nullopt);26 return op_api::searchsorted(boundaries, self, out_int32, right, c10::nullopt, c10::nullopt);
27}27}
28 28 
29-at::Tensor bucketize(const at::Scalar& scalar, const at::Tensor& boundaries, bool out_int32, bool right)29+at::Tensor bucketize(const at::Scalar& self, const at::Tensor& boundaries, bool out_int32, bool right)
30{30{
31 TORCH_CHECK(boundaries.dim() == 1, "boundaries tensor must be 1 dimension, but got dim(", boundaries.dim(),31 TORCH_CHECK(boundaries.dim() == 1, "boundaries tensor must be 1 dimension, but got dim(", boundaries.dim(),
32 ")" + OPS_ERROR(ErrCode::PARAM));32 ")" + OPS_ERROR(ErrCode::PARAM));
33- return op_api::searchsorted(boundaries, scalar, out_int32, right, c10::nullopt, c10::nullopt);33+ return op_api::searchsorted(boundaries, self, out_int32, right, c10::nullopt, c10::nullopt);
34}34}
35 35 
36at::Tensor &bucketize_out(36at::Tensor &bucketize_out(
@@ -38,10 +38,10 @@ at::Tensor &bucketize_out(
38 const at::Tensor& boundaries,38 const at::Tensor& boundaries,
39 bool out_int32,39 bool out_int32,
40 bool right,40 bool right,
41- at::Tensor& result)41+ at::Tensor& out)
42{42{
43 TORCH_CHECK(boundaries.dim() == 1, "boundaries tensor must be 1 dimension, but got dim(", boundaries.dim(),43 TORCH_CHECK(boundaries.dim() == 1, "boundaries tensor must be 1 dimension, but got dim(", boundaries.dim(),
44 ")" + OPS_ERROR(ErrCode::PARAM));44 ")" + OPS_ERROR(ErrCode::PARAM));
45- return op_api::searchsorted_out(boundaries, self, out_int32, right, c10::nullopt, c10::nullopt, result);45+ return op_api::searchsorted_out(boundaries, self, out_int32, right, c10::nullopt, c10::nullopt, out);
46}46}
47}47}
@@ -13,14 +13,13 @@
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"
17-#include "op_plugin/OpApiInterface.h"
18#include "op_plugin/utils/op_api_common.h"16#include "op_plugin/utils/op_api_common.h"
19 17 
20namespace op_api {18namespace op_api {
21#if VERSION_BETWEEN(V1R11, V1R11)19#if VERSION_BETWEEN(V1R11, V1R11)
22-at::Tensor dequantize(const at::Tensor& self) {20+at::Tensor dequantize(const at::Tensor& self)
23- return self.to(at::kFloat);21+{
22+ return self.to(at::kFloat);
24}23}
25#endif24#endif
26}25}
@@ -49,7 +49,8 @@ at::Tensor embedding_symint(
49 bool scale_grad_by_freq,49 bool scale_grad_by_freq,
50 bool sparse)50 bool sparse)
51{51{
52- DO_COMPATIBILITY(aclnnEmbedding, acl_op::embedding_symint(weight, indices, padding_idx, scale_grad_by_freq, sparse));52+ DO_COMPATIBILITY(aclnnEmbedding, acl_op::embedding_symint(weight, indices, padding_idx,
53+ scale_grad_by_freq, sparse));
53 TORCH_CHECK(weight.device() == indices.device(),54 TORCH_CHECK(weight.device() == indices.device(),
54 "Expected all tensors to be on the same device, but "55 "Expected all tensors to be on the same device, but "
55 "found at least two devices, ", weight.device(), " and ", indices.device(), "! "56 "found at least two devices, ", weight.device(), " and ", indices.device(), "! "
@@ -20,10 +20,11 @@
20 20 
21namespace op_api {21namespace op_api {
22 22 
23-at::Tensor& exponential_(at::Tensor& self, double lambda, c10::optional<at::Generator> generator)23+at::Tensor& exponential_(at::Tensor& self, double lambd, c10::optional<at::Generator> generator)
24{24{
25- TORCH_CHECK(lambda > 0.0, "exponential_ expects lambda > 0.0, but found lambda=", lambda, OPS_ERROR(ErrCode::PARAM));25+ TORCH_CHECK(lambd > 0.0, "exponential_ expects lambd > 0.0, but found lambd=",
26- if (std::isinf(lambda)) {26+ lambd, OPS_ERROR(ErrCode::PARAM));
27+ if (std::isinf(lambd)) {
27 self.zero_();28 self.zero_();
28 return self;29 return self;
29 }30 }
@@ -34,7 +35,7 @@ at::Tensor& exponential_(at::Tensor& self, double lambda, c10::optional<at::Gene
34 self = op_api::sub_(self, at::Scalar(1.0), at::Scalar(1.0));35 self = op_api::sub_(self, at::Scalar(1.0), at::Scalar(1.0));
35 self = op_api::mul_(self, at::Scalar(-1.0));36 self = op_api::mul_(self, at::Scalar(-1.0));
36 self = op_api::log_(self);37 self = op_api::log_(self);
37- self = op_api::div_(self, at::Scalar(-lambda));38+ self = op_api::div_(self, at::Scalar(-lambd));
38 39 
39 auto eps = std::numeric_limits<double>::min();40 auto eps = std::numeric_limits<double>::min();
40 self = self.add(eps);41 self = self.add(eps);
@@ -44,14 +45,15 @@ at::Tensor& exponential_(at::Tensor& self, double lambda, c10::optional<at::Gene
44 self.neg_();45 self.neg_();
45 self.add_(1.0);46 self.add_(1.0);
46 47 
47- AT_DISPATCH_FLOATING_TYPES_AND2(at::ScalarType::Half, at::ScalarType::BFloat16, self.scalar_type(), "exponential_", [&]() {48+ AT_DISPATCH_FLOATING_TYPES_AND2(at::ScalarType::Half, at::ScalarType::BFloat16,
49+ self.scalar_type(), "exponential_", [&]() {
48 auto eps = std::numeric_limits<scalar_t>::epsilon() / 2;50 auto eps = std::numeric_limits<scalar_t>::epsilon() / 2;
49 auto mask = self >= (1.0 - eps);51 auto mask = self >= (1.0 - eps);
50 self.masked_fill_(mask, 1.0 - eps);52 self.masked_fill_(mask, 1.0 - eps);
51 });53 });
52 54 
53 self.log_();55 self.log_();
54- self.mul_(-1.0 / lambda);56+ self.mul_(-1.0 / lambd);
55 return self;57 return self;
56}58}
57} // namespace op_api59} // namespace op_api
@@ -13,8 +13,6 @@
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"
17-#include "op_plugin/OpApiInterface.h"
18#include "op_plugin/utils/op_api_common.h"16#include "op_plugin/utils/op_api_common.h"
19 17 
20namespace op_api {18namespace op_api {
@@ -57,7 +57,7 @@ at::Tensor nansum(const at::Tensor& self, c10::optional<c10::ScalarType> dtype)
57 57 
58#if VERSION_BETWEEN(V2R0, VERSION_NEWEST)58#if VERSION_BETWEEN(V2R0, VERSION_NEWEST)
59at::Tensor& nansum_out(const at::Tensor& self, at::OptionalIntArrayRef dim, bool keepdim,59at::Tensor& nansum_out(const at::Tensor& self, at::OptionalIntArrayRef dim, bool keepdim,
60- c10::optional<c10::ScalarType> dtype, at::Tensor& result)60+ c10::optional<c10::ScalarType> dtype, at::Tensor& out)
61{61{
62 at::IntArrayRef dimArray;62 at::IntArrayRef dimArray;
63 c10::SmallVector<int64_t, N> dimlist;63 c10::SmallVector<int64_t, N> dimlist;
@@ -71,17 +71,17 @@ at::Tensor& nansum_out(const at::Tensor& self, at::OptionalIntArrayRef dim, bool
71 c10::ScalarType dstType;71 c10::ScalarType dstType;
72 if (dtype.has_value()) {72 if (dtype.has_value()) {
73 dstType = dtype.value();73 dstType = dtype.value();
74- } else if (result.defined()) {74+ } else if (out.defined()) {
75- dstType = result.scalar_type();75+ dstType = out.scalar_type();
76 } else {76 } else {
77 dstType = self.scalar_type();77 dstType = self.scalar_type();
78 }78 }
79 // infer reduecshape79 // infer reduecshape
80 auto output_size = op_infer::reduce_ops_npu_output_size(self, dimArray, keepdim);80 auto output_size = op_infer::reduce_ops_npu_output_size(self, dimArray, keepdim);
81- at_npu::native::OpPreparation::check_tensor({self}, result, result.scalar_type(), output_size);81+ at_npu::native::OpPreparation::check_tensor({self}, out, out.scalar_type(), output_size);
82 82 
83- EXEC_NPU_CMD(aclnnReduceNansum, self, dimArray, keepdim, dstType, result);83+ EXEC_NPU_CMD(aclnnReduceNansum, self, dimArray, keepdim, dstType, out);
84- return result;84+ return out;
85}85}
86 86 
87at::Tensor nansum(const at::Tensor& self, at::OptionalIntArrayRef dim, bool keepdim,87at::Tensor nansum(const at::Tensor& self, at::OptionalIntArrayRef dim, bool keepdim,
@@ -30,7 +30,8 @@ at::Tensor _pdist_forward(const at::Tensor& self, double p)
30 if (std::isinf(p)) {30 if (std::isinf(p)) {
31 p_float = std::numeric_limits<float>::infinity();31 p_float = std::numeric_limits<float>::infinity();
32 } else {32 } else {
33- TORCH_CHECK(p <= std::numeric_limits<float>::max(), "p dose not support float64 currently.", OPS_ERROR(ErrCode::TYPE));33+ TORCH_CHECK(p <= std::numeric_limits<float>::max(), "p dose not support float64 currently.",
34+ OPS_ERROR(ErrCode::TYPE));
34 p_float = static_cast<float>(p);35 p_float = static_cast<float>(p);
35 }36 }
36 auto output_size = op_infer::pdist_npu_output_size(self);37 auto output_size = op_infer::pdist_npu_output_size(self);
@@ -29,7 +29,9 @@ std::tuple<at::Tensor, at::Tensor> npu_rms_norm_backward(
29 DO_COMPATIBILITY(aclnnRmsNormGrad, acl_op::npu_rms_norm_backward(dy, self, rstd, gamma));29 DO_COMPATIBILITY(aclnnRmsNormGrad, acl_op::npu_rms_norm_backward(dy, self, rstd, gamma));
30 auto output_size = op_infer::rms_norm_grad_npu_output_size(self, gamma);30 auto output_size = op_infer::rms_norm_grad_npu_output_size(self, gamma);
31 at::Tensor dx = npu_preparation::apply_tensor_with_format(output_size[0], self.options(), ACL_FORMAT_ND);31 at::Tensor dx = npu_preparation::apply_tensor_with_format(output_size[0], self.options(), ACL_FORMAT_ND);
32- at::Tensor dgamma = npu_preparation::apply_tensor_with_format(output_size[1], self.options().dtype(at::kFloat), ACL_FORMAT_ND);32+ at::Tensor dgamma = npu_preparation::apply_tensor_with_format(output_size[1],
33+ self.options().dtype(at::kFloat),
34+ ACL_FORMAT_ND);
33 EXEC_NPU_CMD(aclnnRmsNormGrad, dy, self, rstd, gamma, dx, dgamma);35 EXEC_NPU_CMD(aclnnRmsNormGrad, dy, self, rstd, gamma, dx, dgamma);
34 return std::tuple<at::Tensor, at::Tensor>(dx, dgamma);36 return std::tuple<at::Tensor, at::Tensor>(dx, dgamma);
35}37}
@@ -28,7 +28,9 @@ std::tuple<at::Tensor, at::Tensor> npu_rms_norm(
28 DO_COMPATIBILITY(aclnnRmsNorm, acl_op::npu_rms_norm(self, gamma, epsilon));28 DO_COMPATIBILITY(aclnnRmsNorm, acl_op::npu_rms_norm(self, gamma, epsilon));
29 auto output_size = op_infer::rms_norm_npu_output_size(self, gamma);29 auto output_size = op_infer::rms_norm_npu_output_size(self, gamma);
30 at::Tensor y = npu_preparation::apply_tensor_with_format(output_size[0], self.options(), ACL_FORMAT_ND);30 at::Tensor y = npu_preparation::apply_tensor_with_format(output_size[0], self.options(), ACL_FORMAT_ND);
31- at::Tensor rstd = npu_preparation::apply_tensor_with_format(output_size[1], self.options().dtype(at::kFloat), ACL_FORMAT_ND);31+ at::Tensor rstd = npu_preparation::apply_tensor_with_format(output_size[1],
32+ self.options().dtype(at::kFloat),
33+ ACL_FORMAT_ND);
32 EXEC_NPU_CMD(aclnnRmsNorm, self, gamma, epsilon, y, rstd);34 EXEC_NPU_CMD(aclnnRmsNorm, self, gamma, epsilon, y, rstd);
33 return std::tuple<at::Tensor, at::Tensor>(y, rstd);35 return std::tuple<at::Tensor, at::Tensor>(y, rstd);
34}36}
@@ -13,15 +13,14 @@
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"
17-#include "op_plugin/OpApiInterface.h"
18#include "op_plugin/utils/op_api_common.h"16#include "op_plugin/utils/op_api_common.h"
19 17 
20namespace op_api {18namespace op_api {
21using npu_preparation = at_npu::native::OpPreparation;19using npu_preparation = at_npu::native::OpPreparation;
22 20 
23#if VERSION_BETWEEN(V1R11, V1R11)21#if VERSION_BETWEEN(V1R11, V1R11)
24-at::Tensor &square_out(const at::Tensor &self, at::Tensor &out) {22+at::Tensor &square_out(const at::Tensor &self, at::Tensor &out)
23+{
25 return op_api::mul_out(self, self, out);24 return op_api::mul_out(self, self, out);
26}25}
27#endif26#endif