#include "op_plugin/AclOpsInterface.h"
#include "op_plugin/utils/OpAdapter.h"
#include "op_plugin/utils/custom_functions/aclops/inner_compute.h"
namespace acl_op {
using npu_preparation = at_npu::native::OpPreparation;
using npu_utils = at_npu::native::NpuUtils;
namespace {
at::Tensor& l1_loss_out_nocheck(
at::Tensor& result,
const at::Tensor& self,
const at::Tensor& target,
const int64_t reduction) {
std::string reduction_str = op_plugin::utils::get_reduction_str(reduction);
at_npu::native::OpCommand cmd;
cmd.Name("LpLoss")
.Input(self)
.Input(target)
.Attr("reduction", reduction_str)
.Attr("p", (int64_t)1)
.Output(result)
.Run();
return result;
}
at::Tensor& l1_loss_backward_out_nocheck(
at::Tensor& grad_input,
const at::Tensor& grad_output,
const at::Tensor& self,
const at::Tensor& target,
const int64_t reduction) {
std::string reduction_str = op_plugin::utils::get_reduction_str(reduction);
at_npu::native::OpCommand cmd;
cmd.Name("L1LossGrad")
.Input(grad_output)
.Input(self)
.Input(target)
.Attr("reduction", reduction_str)
.Output(grad_input)
.Run();
return grad_input;
}
}
at::Tensor l1_loss_backward(
const at::Tensor& grad_output,
const at::Tensor& self,
const at::Tensor& target,
int64_t reduction) {
at::Tensor grad_output_broadcast = grad_output;
at::Tensor target_broadcast = target;
if (grad_output.sizes() != self.sizes()) {
grad_output_broadcast = acl_op::npu_broadcast(grad_output, self.sizes());
}
if (target.sizes() != self.sizes()) {
target_broadcast = acl_op::npu_broadcast(target, self.sizes());
}
at::Tensor result = npu_preparation::apply_tensor(self);
l1_loss_backward_out_nocheck(result, grad_output_broadcast, self, target_broadcast, reduction);
return result;
}
at::Tensor npu_l1_loss(
const at::Tensor& self,
const at::Tensor& target,
int64_t reduction) {
at::IntArrayRef output_size;
if (reduction == at::Reduction::None) {
output_size = op_infer::input_same_output_size(self);
}
at::Tensor result = npu_preparation::apply_tensor(self, output_size);
l1_loss_out_nocheck(result, self, target, reduction);
return result;
}
at::Tensor l1_loss(
const at::Tensor& self,
const at::Tensor& target,
int64_t reduction) {
return npu_l1_loss(self, target, reduction);
}
}