* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include "aclnn_remainder.h"
#include "op_api/op_api_def.h"
#include "op_api/aclnn_check.h"
#include "conversion/broadcast_to/op_api/broadcast_to.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "floor_mod.h"
#include "conversion/squeeze/op_host/op_api/squeeze.h"
#include "conversion/unsqueeze/op_host/op_api/unsqueeze.h"
#include "aclnn_kernels/transdata.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "opdev/op_dfx.h"
#include "opdev/op_executor.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const std::initializer_list<op::DataType> ASCEND910_DTYPE_DTYPE_SUPPORT_LIST = {
op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_FLOAT16, op::DataType::DT_FLOAT,
op::DataType::DT_DOUBLE};
static const std::initializer_list<op::DataType> ASCEND910B_DTYPE_DTYPE_SUPPORT_LIST = {
op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_FLOAT16,
op::DataType::DT_FLOAT, op::DataType::DT_DOUBLE, op::DataType::DT_BF16};
static const std::initializer_list<op::DataType> ASCEND310P_DTYPE_DTYPE_SUPPORT_LIST = {
op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_FLOAT16, op::DataType::DT_FLOAT,
op::DataType::DT_DOUBLE};
static const std::initializer_list<DataType> emptyDtypes = {};
static const std::initializer_list<op::DataType> DTYPE_SUPPORT_LIST_COMPLEX = {
op::DataType::DT_COMPLEX64, op::DataType::DT_COMPLEX128};
static const std::initializer_list<DataType>& GetDtypeSupportList(NpuArch npuArch, SocVersion socVersion)
{
switch (npuArch) {
case NpuArch::DAV_2201:
case NpuArch::DAV_3510: {
return ASCEND910B_DTYPE_DTYPE_SUPPORT_LIST;
}
case NpuArch::DAV_1001: {
return ASCEND910_DTYPE_DTYPE_SUPPORT_LIST;
}
case NpuArch::DAV_2002: {
return ASCEND310P_DTYPE_DTYPE_SUPPORT_LIST;
}
default: {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "support for %s is not implemented", op::ToString(socVersion).GetString());
return emptyDtypes;
}
}
}
static op::DataType GetScalarDefaultDtype(const op::DataType input)
{
if (IsComplexType(input)) {
return op::DataType::DT_COMPLEX64;
}
return input;
}
static op::DataType InnerTypeToComplexType(const op::DataType input)
{
switch (input) {
case op::DataType::DT_BF16:
return op::DataType::DT_COMPLEX64;
case op::DataType::DT_FLOAT16:
return op::DataType::DT_COMPLEX32;
case op::DataType::DT_FLOAT:
return op::DataType::DT_COMPLEX64;
case op::DataType::DT_DOUBLE:
return op::DataType::DT_COMPLEX128;
case op::DataType::DT_COMPLEX32:
return op::DataType::DT_COMPLEX32;
case op::DataType::DT_COMPLEX64:
return op::DataType::DT_COMPLEX64;
case op::DataType::DT_COMPLEX128:
return op::DataType::DT_COMPLEX128;
default:
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Unknown Complex ScalarType for [%s]", ToString(input).GetString());
return op::DataType::DT_UNDEFINED;
}
}
static op::DataType CombineCategoriesWithComplex(const op::DataType higher, const op::DataType lower)
{
if (IsComplexType(higher)) {
return higher;
} else if (IsComplexType(lower)) {
if (IsFloatingType(higher)) {
return InnerTypeToComplexType(higher);
}
return lower;
} else if (IsFloatingType(higher)) {
return higher;
}
if (higher == op::DataType::DT_BOOL || IsFloatingType(lower)) {
return op::PromoteType(higher, lower);
}
if (higher != op::DataType::DT_UNDEFINED) {
return higher;
}
return lower;
}
static inline DataType PromoteTypeScalarV35(const op::DataType tensorDtype, const op::DataType scalarDtype)
{
auto scalarDefaultDtype = GetScalarDefaultDtype(scalarDtype);
auto promoteType = CombineCategoriesWithComplex(tensorDtype, scalarDefaultDtype);
return promoteType;
}
static inline DataType PromoteTypeScalar(const op::DataType selfDtype, const op::DataType otherDtype)
{
auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();
if (IsRegBase(npuArch)) {
return PromoteTypeScalarV35(selfDtype, otherDtype);
}
if (IsFloatingType(selfDtype)) {
return selfDtype;
} else {
if (IsFloatingType(otherDtype) || selfDtype == op::DataType::DT_BOOL) {
return op::PromoteType(selfDtype, otherDtype);
}
return selfDtype;
}
}
static inline int64_t GetTensorDimNum(const aclTensor* self)
{
return static_cast<int64_t>(self->GetViewShape().GetDimNum());
}
static aclnnStatus CheckNotNullTensorTensor(const aclTensor* self, const aclTensor* other, const aclTensor* out)
{
OP_CHECK_NULL(self, return ACLNN_ERR_INNER_NULLPTR);
OP_CHECK_NULL(other, return ACLNN_ERR_INNER_NULLPTR);
OP_CHECK_NULL(out, return ACLNN_ERR_INNER_NULLPTR);
return ACLNN_SUCCESS;
}
static aclnnStatus CheckNotNullTensorScalar(const aclTensor* self, const aclScalar* other, const aclTensor* out)
{
OP_CHECK_NULL(self, return ACLNN_ERR_INNER_NULLPTR);
OP_CHECK_NULL(other, return ACLNN_ERR_INNER_NULLPTR);
OP_CHECK_NULL(out, return ACLNN_ERR_INNER_NULLPTR);
return ACLNN_SUCCESS;
}
static bool CheckPromoteType(const op::DataType selfDtype, const op::DataType otherDtype, const op::DataType outDtype)
{
auto promoteType = op::PromoteType(selfDtype, otherDtype);
if (promoteType == DataType::DT_UNDEFINED) {
OP_LOGE(
ACLNN_ERR_PARAM_INVALID, "self dtype %s and other dtype %s can not promote dtype.",
op::ToString(selfDtype).GetString(), op::ToString(otherDtype).GetString());
return false;
}
OP_CHECK_RESULT_DTYPE_CAST_FAILED(promoteType, outDtype, return false);
auto socVersion = GetCurrentPlatformInfo().GetSocVersion();
auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();
auto DTYPE_SUPPORT_LIST = GetDtypeSupportList(npuArch, socVersion);
if (DTYPE_SUPPORT_LIST.size() == 0) {
return false;
}
if (!CheckType(promoteType, DTYPE_SUPPORT_LIST)) {
OP_LOGE(
ACLNN_ERR_PARAM_INVALID, "Promote type %s should be in dtype support list [%s].",
op::ToString(promoteType).GetString(), op::ToString(DTYPE_SUPPORT_LIST).GetString());
return false;
}
return true;
}
static bool CheckPromoteTypeTensorScalar(
const op::DataType selfDtype, const op::DataType otherDtype, const op::DataType outDtype)
{
if (CheckType(selfDtype, DTYPE_SUPPORT_LIST_COMPLEX) || CheckType(otherDtype, DTYPE_SUPPORT_LIST_COMPLEX)) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "remainder_npu not implemented for dtype complex.");
return false;
}
auto castDtype = PromoteTypeScalar(selfDtype, otherDtype);
OP_CHECK_RESULT_DTYPE_CAST_FAILED(selfDtype, castDtype, return false);
auto socVersion = GetCurrentPlatformInfo().GetSocVersion();
auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();
auto DTYPE_SUPPORT_LIST = GetDtypeSupportList(npuArch, socVersion);
if (DTYPE_SUPPORT_LIST.size() == 0) {
return false;
}
if (!CheckType(castDtype, DTYPE_SUPPORT_LIST)) {
OP_LOGE(
ACLNN_ERR_PARAM_INVALID, "expected dtype %s should be in dtype support list [%s].",
op::ToString(castDtype).GetString(), op::ToString(DTYPE_SUPPORT_LIST).GetString());
return false;
}
OP_CHECK_RESULT_DTYPE_CAST_FAILED(castDtype, outDtype, return false);
return true;
}
static bool CheckPromoteTypeScalarTensor(
const op::DataType selfDtype, const op::DataType otherDtype, const op::DataType outDtype)
{
if (CheckType(selfDtype, DTYPE_SUPPORT_LIST_COMPLEX) || CheckType(otherDtype, DTYPE_SUPPORT_LIST_COMPLEX)) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "remainder_npu not implemented for dtype complex.");
return false;
}
auto socVersion = GetCurrentPlatformInfo().GetSocVersion();
auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();
auto castDtype = IsRegBase(npuArch) ? PromoteTypeScalarV35(otherDtype, selfDtype) : otherDtype;
OP_CHECK_RESULT_DTYPE_CAST_FAILED(castDtype, outDtype, return false);
auto DTYPE_SUPPORT_LIST = GetDtypeSupportList(npuArch, socVersion);
if (DTYPE_SUPPORT_LIST.size() == 0) {
return false;
}
if (!CheckType(outDtype, DTYPE_SUPPORT_LIST)) {
OP_LOGE(
ACLNN_ERR_PARAM_INVALID, "out dtype %s should be in dtype support list [%s].",
op::ToString(outDtype).GetString(), op::ToString(DTYPE_SUPPORT_LIST).GetString());
return false;
}
return true;
}
static inline bool CheckInputOutShape(const aclTensor* input, const aclTensor* out, const std::string& printStr)
{
if (input->GetViewShape() != out->GetViewShape()) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Tensor %s and out should have same shape.", printStr.c_str());
return false;
}
return true;
}
static inline bool CheckTensorDimSize(const aclTensor* self)
{
OP_CHECK_MAX_DIM(self, MAX_SUPPORT_DIMS_NUMS, return false);
return true;
}
static inline bool CheckShapeIsSpecial(const op::Shape shape)
{
if (shape.GetDimNum() == 0) {
return true;
}
return (shape.GetDimNum() == 1 && shape.GetDim(0) == 1);
}
static bool CheckBroadcastShape(const aclTensor* self, const aclTensor* other, const aclTensor* out, bool isInplace)
{
op::Shape broadcastShape;
auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();
if (IsRegBase(npuArch)) {
OP_CHECK_BROADCAST_AND_INFER_SHAPE(self, other, broadcastShape, return false);
OP_CHECK_SHAPE_NOT_EQUAL_WITH_EXPECTED_SIZE(out, broadcastShape, return false);
return true;
}
OP_CHECK_BROADCAST(self, other, return false);
BroadcastInferShape(self->GetViewShape(), other->GetViewShape(), broadcastShape);
if (broadcastShape != out->GetViewShape()) {
if ((!isInplace) && CheckShapeIsSpecial(broadcastShape) && CheckShapeIsSpecial(out->GetViewShape())) {
return true;
}
OP_LOGE(
ACLNN_ERR_PARAM_INVALID,
"expected consistent tensor shape for the broadcast shape and out, but got %s and %s respectively.",
op::ToString(broadcastShape).GetString(), op::ToString(out->GetViewShape()).GetString());
return false;
}
return true;
}
static const aclTensor* InitializeTensor(const aclTensor* x, op::DataType dtype, aclOpExecutor* executor)
{
auto xContiguous = l0op::Contiguous(x, executor);
if (xContiguous == nullptr) {
OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "Tensor xContiguous should not be null.");
return nullptr;
}
if (GetTensorDimNum(xContiguous) == 0) {
int64_t tensorShape[1] = {};
tensorShape[0] = 1;
auto baseShape = executor->AllocIntArray(tensorShape, 1);
xContiguous = l0op::BroadcastTo(xContiguous, baseShape, executor);
if (xContiguous == nullptr) {
OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "After broadcast, tensor xContiguous should not be null.");
return nullptr;
}
}
if (x->GetDataType() != dtype) {
xContiguous = l0op::Cast(xContiguous, dtype, executor);
if (xContiguous == nullptr) {
OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "After cast, tensor xContiguous should not be null.");
return nullptr;
}
}
return xContiguous;
}
static aclIntArray* GetTensorShape(const aclTensor* self, aclOpExecutor* executor)
{
int64_t tensorSize = GetTensorDimNum(self);
if (tensorSize == 0) {
int64_t tensorShape[1] = {};
tensorShape[0] = 1;
auto baseShape = executor->AllocIntArray(tensorShape, 1);
return baseShape;
}
std::vector<int64_t> tensorShape(tensorSize);
for (int64_t i = 0; i < tensorSize; i++) {
tensorShape[i] = (self->GetViewShape())[i];
}
auto res = executor->AllocIntArray(tensorShape.data(), tensorSize);
return res;
}
static const aclTensor* BroadcastTensor(
const aclTensor* x, const aclTensor* out, const aclIntArray* broadcastShape, aclOpExecutor* executor)
{
x = l0op::ReFormat(x, op::Format::FORMAT_ND);
if (x == nullptr) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Before broadcast, Reformat result is nullptr.");
}
if (x->GetViewShape() != out->GetViewShape()) {
x = l0op::BroadcastTo(x, broadcastShape, executor);
if (x == nullptr) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Broadcast result is nullptr.");
}
}
x = l0op::ReFormat(x, op::Format::FORMAT_ND);
if (x == nullptr) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Reformat result is nullptr.");
}
return x;
}
static aclnnStatus RemainderMainProcess(
const aclTensor* selfContiguous, const aclTensor* otherContiguous, const aclTensor* out, bool needUnsqueeze,
aclOpExecutor* executor)
{
auto remainderOut = l0op::FloorMod(selfContiguous, otherContiguous, executor);
CHECK_RET(remainderOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
if (remainderOut->GetDataType() != out->GetDataType()) {
remainderOut = l0op::Cast(remainderOut, out->GetDataType(), executor);
CHECK_RET(remainderOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
if (needUnsqueeze) {
int64_t squeezeDim = 0;
remainderOut = l0op::SqueezeNd(remainderOut, squeezeDim, executor);
CHECK_RET(remainderOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
auto viewcopyResult = l0op::ViewCopy(remainderOut, out, executor);
CHECK_RET(viewcopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
return ACLNN_SUCCESS;
}
static aclnnStatus CheckParamsTensorTensor(const aclTensor* self, const aclTensor* other, const aclTensor* out)
{
CHECK_RET(CheckNotNullTensorTensor(self, other, out) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckPromoteType(self->GetDataType(), other->GetDataType(), out->GetDataType()), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckBroadcastShape(self, other, out, false), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckTensorDimSize(self), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckTensorDimSize(other), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckTensorDimSize(out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
static aclnnStatus CheckParamsInplaceTensorTensor(const aclTensor* self, const aclTensor* other, const aclTensor* out)
{
CHECK_RET(CheckNotNullTensorTensor(self, other, out) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckPromoteType(self->GetDataType(), other->GetDataType(), out->GetDataType()), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckBroadcastShape(self, other, out, true), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckTensorDimSize(self), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckTensorDimSize(other), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckTensorDimSize(out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
static aclnnStatus CheckParamsTensorScalar(const aclTensor* self, const aclScalar* other, const aclTensor* out)
{
CHECK_RET(CheckNotNullTensorScalar(self, other, out) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_NULLPTR);
OP_CHECK_SHAPE_NOT_EQUAL(self, out, return ACLNN_ERR_PARAM_INVALID);
CHECK_RET(
CheckPromoteTypeTensorScalar(self->GetDataType(), other->GetDataType(), out->GetDataType()),
ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckTensorDimSize(self), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckTensorDimSize(out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
static aclnnStatus CheckParamsInplaceTensorScalar(const aclTensor* self, const aclScalar* other, const aclTensor* out)
{
CHECK_RET(CheckNotNullTensorScalar(self, other, out) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_NULLPTR);
OP_CHECK_SHAPE_NOT_EQUAL(self, out, return ACLNN_ERR_PARAM_INVALID);
CHECK_RET(
CheckPromoteTypeTensorScalar(self->GetDataType(), other->GetDataType(), out->GetDataType()),
ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckTensorDimSize(self), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckTensorDimSize(out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
static aclnnStatus CheckParamsScalarTensor(const aclScalar* self, const aclTensor* other, const aclTensor* out)
{
OP_CHECK_NULL(self, return ACLNN_ERR_INNER_NULLPTR);
OP_CHECK_NULL(other, return ACLNN_ERR_INNER_NULLPTR);
OP_CHECK_NULL(out, return ACLNN_ERR_INNER_NULLPTR);
OP_CHECK_SHAPE_NOT_EQUAL(other, out, return ACLNN_ERR_PARAM_INVALID);
CHECK_RET(
CheckPromoteTypeScalarTensor(self->GetDataType(), other->GetDataType(), out->GetDataType()),
ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckTensorDimSize(other), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckTensorDimSize(out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
aclnnStatus ExecRemainderTensorTensorGetWorkspaceSize(
const aclTensor* self, const aclTensor* other, aclTensor* out, uint64_t* workspaceSize, aclOpExecutor** executor)
{
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
if (self->IsEmpty() || other->IsEmpty()) {
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto promoteType = op::PromoteType(self->GetDataType(), other->GetDataType());
auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();
if (IsRegBase(npuArch) && promoteType != op::DataType::DT_DOUBLE) {
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto selfCasted = l0op::Cast(selfContiguous, promoteType, uniqueExecutor.get());
CHECK_RET(selfCasted != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherContiguous = l0op::Contiguous(other, uniqueExecutor.get());
CHECK_RET(otherContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherCasted = l0op::Cast(otherContiguous, promoteType, uniqueExecutor.get());
CHECK_RET(otherCasted != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto floorModOpOut = l0op::FloorMod(selfCasted, otherCasted, uniqueExecutor.get());
CHECK_RET(floorModOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castOut = l0op::Cast(floorModOpOut, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(castOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(castOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
} else {
auto selfContiguous = InitializeTensor(self, promoteType, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherContiguous = InitializeTensor(other, promoteType, uniqueExecutor.get());
CHECK_RET(otherContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto broadcastShape = GetTensorShape(out, uniqueExecutor.get());
selfContiguous = BroadcastTensor(selfContiguous, out, broadcastShape, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
otherContiguous = BroadcastTensor(otherContiguous, out, broadcastShape, uniqueExecutor.get());
CHECK_RET(otherContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
bool needUnsqueeze = (GetTensorDimNum(out) == 0);
auto remainderRes =
RemainderMainProcess(selfContiguous, otherContiguous, out, needUnsqueeze, uniqueExecutor.get());
CHECK_RET(remainderRes == ACLNN_SUCCESS, remainderRes);
}
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus ExecRemainderTensorScalarGetWorkspaceSize(
const aclTensor* self, const aclScalar* other, aclTensor* out, uint64_t* workspaceSize, aclOpExecutor** executor)
{
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
if (self->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto castDtype = PromoteTypeScalar(self->GetDataType(), other->GetDataType());
auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();
if (IsRegBase(npuArch) && castDtype != op::DataType::DT_DOUBLE) {
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto selfCasted = l0op::Cast(selfContiguous, castDtype, uniqueExecutor.get());
CHECK_RET(selfCasted != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto floorModOpOut =
l0op::FloorMod(selfCasted, uniqueExecutor.get()->ConvertToTensor(other, castDtype), uniqueExecutor.get());
CHECK_RET(floorModOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castOut = l0op::Cast(floorModOpOut, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(castOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(castOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
} else {
auto selfContiguous = InitializeTensor(self, castDtype, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherContiguous = uniqueExecutor.get()->ConvertToTensor(other, castDtype);
auto selfShape = GetTensorShape(selfContiguous, uniqueExecutor.get());
CHECK_RET(selfShape != nullptr, ACLNN_ERR_INNER_NULLPTR);
otherContiguous = l0op::BroadcastTo(otherContiguous, selfShape, uniqueExecutor.get());
CHECK_RET(otherContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
bool needUnsqueeze = (GetTensorDimNum(out) == 0);
auto remainderRes =
RemainderMainProcess(selfContiguous, otherContiguous, out, needUnsqueeze, uniqueExecutor.get());
CHECK_RET(remainderRes == ACLNN_SUCCESS, remainderRes);
}
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnRemainderTensorTensorGetWorkspaceSize(
const aclTensor* self, const aclTensor* other, aclTensor* out, uint64_t* workspaceSize, aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnRemainderTensorTensor, DFX_IN(self, other), DFX_OUT(out));
auto ret = CheckParamsTensorTensor(self, other, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
return ExecRemainderTensorTensorGetWorkspaceSize(self, other, out, workspaceSize, executor);
}
aclnnStatus aclnnRemainderTensorScalarGetWorkspaceSize(
const aclTensor* self, const aclScalar* other, aclTensor* out, uint64_t* workspaceSize, aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnRemainderTensorScalar, DFX_IN(self, other), DFX_OUT(out));
auto ret = CheckParamsTensorScalar(self, other, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
return ExecRemainderTensorScalarGetWorkspaceSize(self, other, out, workspaceSize, executor);
}
aclnnStatus aclnnRemainderScalarTensorGetWorkspaceSize(
const aclScalar* self, const aclTensor* other, aclTensor* out, uint64_t* workspaceSize, aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnRemainderScalarTensor, DFX_IN(self, other), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParamsScalarTensor(self, other, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (other->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();
if (IsRegBase(npuArch) &&
PromoteTypeScalarV35(other->GetDataType(), self->GetDataType()) != op::DataType::DT_DOUBLE) {
auto castDtype = PromoteTypeScalarV35(other->GetDataType(), self->GetDataType());
auto otherContiguous = l0op::Contiguous(other, uniqueExecutor.get());
CHECK_RET(otherContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherCasted = l0op::Cast(otherContiguous, castDtype, uniqueExecutor.get());
CHECK_RET(otherCasted != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto floorModOpOut =
l0op::FloorMod(uniqueExecutor.get()->ConvertToTensor(self, castDtype), otherCasted, uniqueExecutor.get());
CHECK_RET(floorModOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castOut = l0op::Cast(floorModOpOut, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(castOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(castOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
} else {
auto selfContiguous = uniqueExecutor.get()->ConvertToTensor(self, out->GetDataType());
auto otherShape = GetTensorShape(other, uniqueExecutor.get());
CHECK_RET(otherShape != nullptr, ACLNN_ERR_INNER_NULLPTR);
selfContiguous = l0op::BroadcastTo(selfContiguous, otherShape, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherContiguous = InitializeTensor(other, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(otherContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
bool needUnsqueeze = (GetTensorDimNum(out) == 0);
auto remainderRes =
RemainderMainProcess(selfContiguous, otherContiguous, out, needUnsqueeze, uniqueExecutor.get());
CHECK_RET(remainderRes == ACLNN_SUCCESS, remainderRes);
}
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnInplaceRemainderTensorTensorGetWorkspaceSize(
aclTensor* selfRef, const aclTensor* other, uint64_t* workspaceSize, aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnInplaceRemainderTensorTensor, DFX_IN(selfRef, other), DFX_OUT(selfRef));
auto out = const_cast<aclTensor*>(selfRef);
auto ret = CheckParamsInplaceTensorTensor(selfRef, other, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
return ExecRemainderTensorTensorGetWorkspaceSize(selfRef, other, out, workspaceSize, executor);
}
aclnnStatus aclnnInplaceRemainderTensorScalarGetWorkspaceSize(
aclTensor* selfRef, const aclScalar* other, uint64_t* workspaceSize, aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnInplaceRemainderTensorScalar, DFX_IN(selfRef, other), DFX_OUT(selfRef));
auto out = const_cast<aclTensor*>(selfRef);
auto ret = CheckParamsInplaceTensorScalar(selfRef, other, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
return ExecRemainderTensorScalarGetWorkspaceSize(selfRef, other, out, workspaceSize, executor);
}
aclnnStatus aclnnRemainderTensorTensor(
void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnRemainderTensorTensor);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
aclnnStatus aclnnRemainderTensorScalar(
void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnRemainderTensorScalar);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
aclnnStatus aclnnRemainderScalarTensor(
void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnRemainderScalarTensor);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
aclnnStatus aclnnInplaceRemainderTensorTensor(
void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnInplaceRemainderTensorTensor);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
aclnnStatus aclnnInplaceRemainderTensorScalar(
void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnInplaceRemainderTensorScalar);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif