#include "op_plugin/AclOpsInterface.h"
#include "op_plugin/utils/OpAdapter.h"
namespace acl_op {
using npu_preparation = at_npu::native::OpPreparation;
using npu_utils = at_npu::native::NpuUtils;
namespace {
static void round_decimals_check(const at::Tensor& self, int64_t decimals) {
TORCH_CHECK(isFloatingType(self.scalar_type()) ||
self.scalar_type() == at::ScalarType::Int ||
self.scalar_type() == at::ScalarType::Long,
"\"round_npu\" not implemented for '", toString(self.scalar_type()), "'",
OPS_ERROR(ErrCode::TYPE));
}
at::Tensor& round_out_npu_nocheck(at::Tensor& result, const at::Tensor& self, int64_t decimals) {
at_npu::native::OpCommand cmd;
cmd.Name("Round")
.Input(self)
.Output(result)
.Attr("decimals", decimals)
.Run();
return result;
}
}
at::Tensor& round_out(const at::Tensor& self, int64_t decimals, at::Tensor& result) {
round_decimals_check(self, decimals);
npu_preparation::CheckOut({self}, result, self);
if (!npu_utils::check_match(&result)) {
at::Tensor contiguous_result = npu_utils::format_contiguous(result);
round_out_npu_nocheck(contiguous_result, self, decimals);
npu_utils::format_fresh_view(result, contiguous_result);
} else {
round_out_npu_nocheck(result, self, decimals);
}
return result;
}
at::Tensor round(const at::Tensor& self, int64_t decimals) {
round_decimals_check(self, decimals);
at::Tensor result = npu_preparation::ApplyTensor(self);
round_out_npu_nocheck(result, self, decimals);
return result;
}
at::Tensor& round_(at::Tensor& self, int64_t decimals) {
round_decimals_check(self, decimals);
acl_op::round_out(self, decimals, self);
return self;
}
}