#include "op_plugin/AclOpsInterface.h"
#include "op_plugin/OpApiInterface.h"
#include "op_plugin/utils/op_api_common.h"
#include "torch_npu/csrc/core/npu/register/OptionsManager.h"
namespace op_api {
using npu_preparation = at_npu::native::OpPreparation;
static at::Tensor self_tensor_to_device(const at::Tensor &tensor, const at::ScalarType result_type,
const c10::Device device)
{
if (npu_preparation::is_scalar_wrapped_to_tensor(tensor) ||
(tensor.dim() == 0 && !torch_npu::utils::is_npu(tensor))) {
at::Scalar scalar = tensor.item();
return npu_preparation::copy_scalar_to_device(scalar, result_type, device);
}
return tensor;
}
at::Tensor &inplace_mul_out_npu_no_check(at::Tensor &self, const at::Tensor &other)
{
if (other.dim() == 0 && !torch_npu::utils::is_npu(other)) {
c10::Scalar other_scalar = other.item();
EXEC_NPU_CMD(aclnnInplaceMuls, self, other_scalar);
} else {
EXEC_NPU_CMD(aclnnInplaceMul, self, other);
}
return self;
}
at::Tensor &mul_out_npu_no_check(const at::Tensor &self, const at::Tensor &other, at::Tensor &result)
{
if (other.dim() == 0 && !torch_npu::utils::is_npu(other)) {
c10::Scalar other_scalar = other.item();
EXEC_NPU_CMD(aclnnMuls, self, other_scalar, result);
} else {
EXEC_NPU_CMD(aclnnMul, self, other, result);
}
return result;
}
static at::Tensor mul_dest_output(const at::Tensor &self, const at::Tensor &other)
{
bool isSelfWrapped = npu_preparation::is_scalar_wrapped_to_tensor(self);
return isSelfWrapped ? other : self;
}
at::Tensor &mul_out(const at::Tensor &self, const at::Tensor &other, at::Tensor &result)
{
DO_COMPATIBILITY(aclnnMul, acl_op::mul_out(self, other, result));
DO_COMPATIBILITY(aclnnMuls, acl_op::mul_out(self, other, result));
at::Tensor output_tensor = mul_dest_output(self, other);
auto output_size = op_infer::broadcast_ops_npu_output_size(self, other);
at::ScalarType result_type = at::native::result_type(self, other);
at::Tensor self_cp = self_tensor_to_device(self, result_type, result.device());
npu_preparation::check_tensor({self}, result, result.scalar_type(), output_size);
mul_out_npu_no_check(self_cp, other, result);
return result;
}
at::Tensor mul(const at::Tensor &self, const at::Tensor &other)
{
DO_COMPATIBILITY(aclnnMul, acl_op::mul(self, other));
DO_COMPATIBILITY(aclnnMuls, acl_op::mul(self, other));
at::Tensor output_tensor = mul_dest_output(self, other);
auto output_size = op_infer::broadcast_ops_npu_output_size(self, other);
at::ScalarType result_type = at::native::result_type(self, other);
at::Tensor self_cp = self_tensor_to_device(self, result_type, output_tensor.device());
at::Tensor result =
npu_preparation::apply_tensor_without_format(output_size, output_tensor.options().dtype(result_type));
mul_out_npu_no_check(self_cp, other, result);
return result;
}
at::Tensor mul(const at::Tensor &self, const at::Scalar &other)
{
DO_COMPATIBILITY(aclnnMuls, acl_op::mul(self, other));
auto output_size = op_infer::input_same_output_size(self);
at::ScalarType result_type = at::native::result_type(self, other);
at::Tensor result = npu_preparation::apply_tensor_without_format(output_size, self.options().dtype(result_type));
EXEC_NPU_CMD(aclnnMuls, self, other, result);
return result;
}
at::Tensor &mul_(at::Tensor &self, const at::Tensor &other)
{
DO_COMPATIBILITY(aclnnInplaceMul, acl_op::mul_(self, other));
DO_COMPATIBILITY(aclnnInplaceMuls, acl_op::mul_(self, other));
TORCH_CHECK(torch_npu::utils::is_npu(self), "Inplace tensor self must be NPU-Tensor.", OPS_ERROR(ErrCode::PARAM));
npu_preparation::check_memory({self, other}, {self});
inplace_mul_out_npu_no_check(self, other);
return self;
}
at::Tensor &mul_(at::Tensor &self, const at::Scalar &other)
{
DO_COMPATIBILITY(aclnnInplaceMuls, acl_op::mul_(self, other));
TORCH_CHECK(torch_npu::utils::is_npu(self), "Inplace tensor self must be NPU-Tensor.", OPS_ERROR(ErrCode::PARAM));
EXEC_NPU_CMD(aclnnInplaceMuls, self, other);
return self;
}
}