* 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_mul.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "math/logical_and/op_api/logical_and.h"
#include "mul.h"
#include "math/muls/op_api/muls.h"
#include "aclnn/aclnn_base.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "opdev/common_types.h"
#include "opdev/data_type_utils.h"
#include "opdev/format_utils.h"
#include "opdev/op_executor.h"
#include "opdev/op_log.h"
#include "opdev/op_dfx.h"
#include "opdev/shape_utils.h"
#include "opdev/tensor_view_utils.h"
#include "op_api/op_api_def.h"
#include "op_api/aclnn_check.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
* self other
* | |
* \ /
* Contiguous(workspace_0) Contiguous(workspace_2)
* \ /
* Cast(workspace_1) Cast(workspace_3)
* \ /
* Mul(workspace_4)
* |
* Cast(workspace_5)
* |
* ViewCopy
* |
* result
*/
static const std::initializer_list<DataType> ASCEND910_DTYPE_DTYPE_SUPPORT_LIST = {
DataType::DT_FLOAT, DataType::DT_FLOAT16, DataType::DT_INT32, DataType::DT_DOUBLE, DataType::DT_INT8,
DataType::DT_UINT8, DataType::DT_INT16, DataType::DT_INT64, DataType::DT_BOOL, DataType::DT_COMPLEX128,
DataType::DT_COMPLEX64};
static const std::initializer_list<DataType> ASCEND910B_DTYPE_DTYPE_SUPPORT_LIST = {
DataType::DT_FLOAT, DataType::DT_FLOAT16, DataType::DT_INT32, DataType::DT_DOUBLE, DataType::DT_INT8,
DataType::DT_UINT8, DataType::DT_INT16, DataType::DT_INT64, DataType::DT_BOOL, DataType::DT_COMPLEX128,
DataType::DT_COMPLEX64, DataType::DT_BF16};
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 op::DataType GetScalarDefaultDtype(const op::DataType input) {
if (IsComplexType(input)) {
return op::DataType::DT_COMPLEX64;
} else if (IsFloatingType(input)) {
return op::DataType::DT_FLOAT;
}
return input;
}
static const std::initializer_list<DataType>& GetDtypeSupportList() {
if (GetCurrentPlatformInfo().GetSocVersion() >= SocVersion::ASCEND910B &&
GetCurrentPlatformInfo().GetSocVersion() <= SocVersion::ASCEND910E) {
return ASCEND910B_DTYPE_DTYPE_SUPPORT_LIST;
} else {
return ASCEND910_DTYPE_DTYPE_SUPPORT_LIST;
}
}
inline static bool isFloatType(const DataType type) {
return type == DataType::DT_DOUBLE || type == DataType::DT_FLOAT ||
type == DataType::DT_FLOAT16 || type == DataType::DT_BF16;
}
inline static bool CheckMulsNotNull(const aclTensor *self, const aclScalar *other, const aclTensor *out) {
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(other, return false);
OP_CHECK_NULL(out, return false);
return true;
}
inline static bool CheckMulNotNull(const aclTensor *self, const aclTensor *other, const aclTensor *out) {
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(other, return false);
OP_CHECK_NULL(out, return false);
return true;
}
inline static bool CheckInplaceMulsNotNull(const aclTensor *selfRef, const aclScalar *other) {
OP_CHECK_NULL(selfRef, return false);
OP_CHECK_NULL(other, return false);
return true;
}
inline static bool CheckInplaceMulNotNull(const aclTensor *selfRef, const aclTensor *other) {
OP_CHECK_NULL(selfRef, return false);
OP_CHECK_NULL(other, return false);
return true;
}
inline static bool CheckMulsDtype(const aclTensor *self, const aclTensor *out) {
const auto& supportList = GetDtypeSupportList();
OP_CHECK_DTYPE_NOT_SUPPORT(self, supportList, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(out, supportList, return false);
return true;
}
inline static bool CheckMulDtype(const aclTensor *self, const aclTensor *other, const aclTensor *out) {
const auto& supportList = GetDtypeSupportList();
OP_CHECK_DTYPE_NOT_SUPPORT(self, supportList, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(other, supportList, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(out, supportList, return false);
return true;
}
inline static bool CheckInplaceMulDtype(const aclTensor *selfRef, const aclTensor *other) {
const auto& supportList = GetDtypeSupportList();
OP_CHECK_DTYPE_NOT_SUPPORT(selfRef, supportList, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(other, supportList, return false);
return true;
}
static inline float GetCastedFloat(const op::DataType tensorDtype, const aclScalar* scalar) {
float castedRes = 0;
switch (tensorDtype) {
case DataType::DT_FLOAT16:
castedRes = static_cast<float>(scalar->ToFp16());
break;
case DataType::DT_BF16:
castedRes = static_cast<float>(scalar->ToBf16());
break;
default:
castedRes = scalar->ToFloat();
break;
}
return castedRes;
}
static inline bool IsFloatEqual(float a, float b)
{
return std::abs(a - b) <= std::numeric_limits<float>::epsilon();
}
static DataType InferTensorScalarDtype(const aclTensor* self, const aclScalar* other, const aclTensor* out) {
if (IsRegBase()) {
auto scalarDefaultDtype = GetScalarDefaultDtype(other->GetDataType());
auto promoteType = CombineCategoriesWithComplex(self->GetDataType(), scalarDefaultDtype);
if (promoteType == DataType::DT_FLOAT16 || promoteType == DataType::DT_BF16) {
bool keepB16 = IsFloatEqual(GetCastedFloat(promoteType, other), other->ToFloat());
promoteType = keepB16 ? promoteType : DataType::DT_FLOAT;
}
if (promoteType == DataType::DT_COMPLEX32) {
promoteType = DataType::DT_COMPLEX64;
}
return promoteType;
}
if (IsComplexType(self->GetDataType()) || IsComplexType(other->GetDataType())) {
return PromoteType(self->GetDataType(), other->GetDataType());
}
if (isFloatType(self->GetDataType())) {
return self->GetDataType() != DataType::DT_BF16 ? self->GetDataType() : DataType::DT_FLOAT;
}
if ((self->GetDataType() == DataType::DT_BOOL && other->GetDataType() == DataType::DT_DOUBLE) ||
(other->GetDataType() == DataType::DT_DOUBLE && out->GetDataType() == DataType::DT_FLOAT)) {
return DataType::DT_FLOAT;
}
if (isFloatType(other->GetDataType()) || self->GetDataType() == DataType::DT_BOOL) {
return PromoteType(self->GetDataType(), other->GetDataType());
}
return self->GetDataType();
}
inline static bool CheckMulsPromoteDtype(const aclTensor* self, const aclScalar* other, const aclTensor* out) {
if (!IsRegBase()) {
return true;
}
auto inferDtype = InferTensorScalarDtype(self, other, out);
if (inferDtype == DataType::DT_UNDEFINED) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Self dtype %s and other dtype %s can not promote dtype.",
op::ToString(self->GetDataType()).GetString(), op::ToString(other->GetDataType()).GetString());
return false;
}
const auto& supportList = GetDtypeSupportList();
if (!CheckType(inferDtype, supportList)) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID,
"input dtype %s and %s after promote is %s, which should be in dtype support list %s.",
op::ToString(self->GetDataType()).GetString(), op::ToString(other->GetDataType()).GetString(),
op::ToString(inferDtype).GetString(), op::ToString(supportList).GetString());
return false;
}
OP_CHECK_RESULT_DTYPE_CAST_FAILED(self->GetDataType(), inferDtype, return false);
OP_CHECK_RESULT_DTYPE_CAST_FAILED(other->GetDataType(), inferDtype, return false);
OP_CHECK_RESULT_DTYPE_CAST_FAILED(inferDtype, out->GetDataType(), return false);
return true;
}
inline static bool CheckMulPromoteType(const aclTensor *self, const aclTensor *other, const aclTensor* out) {
DataType promoteType = PromoteType(self->GetDataType(), other->GetDataType());
if (promoteType == DataType::DT_UNDEFINED) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID,
"Expected aclnnMul self dtype [%s] and other dtype [%s] to be promotable but check failed.",
ToString(self->GetDataType()).GetString(), ToString(other->GetDataType()).GetString());
return false;
}
if (!IsRegBase()) {
OP_CHECK_RESULT_DTYPE_CAST_FAILED(promoteType, out->GetDataType(), return false);
}
return true;
}
inline static bool CheckInplaceMulPromoteType(const aclTensor *selfRef, const aclTensor *other) {
DataType promoteType = PromoteType(selfRef->GetDataType(), other->GetDataType());
if (promoteType == DataType::DT_UNDEFINED) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID,
"Expected aclnnInplaceMul selfRef dtype [%s] and other dtype [%s] to be promotable but check failed.",
ToString(selfRef->GetDataType()).GetString(), ToString(other->GetDataType()).GetString());
return false;
}
if (!IsRegBase()) {
OP_CHECK_RESULT_DTYPE_CAST_FAILED(promoteType, selfRef->GetDataType(), return false);
}
return true;
}
inline static bool CheckMulShape(const aclTensor *self, const aclTensor *other, const aclTensor *out) {
Shape dstShape;
OP_CHECK_MAX_DIM(self, MAX_SUPPORT_DIMS_NUMS, return false);
OP_CHECK_MAX_DIM(other, MAX_SUPPORT_DIMS_NUMS, return false);
OP_CHECK_BROADCAST_AND_INFER_SHAPE(self, other, dstShape, return false);
OP_CHECK_SHAPE_NOT_EQUAL_WITH_EXPECTED_SIZE(out, dstShape, return false);
return true;
}
inline static bool CheckInplaceMulShape(const aclTensor *selfRef, const aclTensor *other) {
Shape dstShape;
OP_CHECK_BROADCAST_AND_INFER_SHAPE(selfRef, other, dstShape, return false);
OP_CHECK_SHAPE_NOT_EQUAL_WITH_EXPECTED_SIZE(selfRef, dstShape, return false);
return true;
}
inline static aclnnStatus CheckMulsParams(const aclTensor *self, const aclScalar *other, const aclTensor *out) {
CHECK_RET(CheckMulsNotNull(self, other, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckMulsDtype(self, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckMulsPromoteDtype(self, other, out), ACLNN_ERR_PARAM_INVALID);
OP_CHECK_SHAPE_NOT_EQUAL(self, out, return ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
inline static aclnnStatus CheckMulParams(const aclTensor *self, const aclTensor *other, const aclTensor *out) {
CHECK_RET(CheckMulNotNull(self, other, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckMulDtype(self, other, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckMulPromoteType(self, other, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckMulShape(self, other, out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
inline static aclnnStatus CheckInplaceMulsParams(const aclTensor *selfRef, const aclScalar *other) {
CHECK_RET(CheckInplaceMulsNotNull(selfRef, other), ACLNN_ERR_PARAM_NULLPTR);
const auto& supportList = GetDtypeSupportList();
OP_CHECK_DTYPE_NOT_SUPPORT(selfRef, supportList, return ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckMulsPromoteDtype(selfRef, other, selfRef), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
inline static aclnnStatus CheckInplaceMulParams(const aclTensor *selfRef, const aclTensor *other) {
CHECK_RET(CheckInplaceMulNotNull(selfRef, other), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckInplaceMulDtype(selfRef, other), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckInplaceMulPromoteType(selfRef, other), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckInplaceMulShape(selfRef, other), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
inline static bool IsMulMixDtypeSupport(const aclTensor *self, const aclTensor *other) {
return (self->GetDataType() == DataType::DT_FLOAT16 && other->GetDataType() == DataType::DT_FLOAT) ||
(self->GetDataType() == DataType::DT_FLOAT && other->GetDataType() == DataType::DT_FLOAT16) ||
(self->GetDataType() == DataType::DT_BF16 && other->GetDataType() == DataType::DT_FLOAT) ||
(self->GetDataType() == DataType::DT_FLOAT && other->GetDataType() == DataType::DT_BF16);
}
static void MulsCheckFormat(const aclTensor* self){
ge::Format selfStorageFormat = self->GetStorageFormat();
if (selfStorageFormat != ge::Format::FORMAT_ND){
OP_LOGW("aclnnMuls only support format ND.");
}
}
aclnnStatus aclnnMulsGetWorkspaceSize(const aclTensor *self, const aclScalar *other, aclTensor *out,
uint64_t *workspaceSize, aclOpExecutor **executor) {
L2_DFX_PHASE_1(aclnnMuls, DFX_IN(self, other), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckMulsParams(self, other, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
MulsCheckFormat(self);
if (self->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto inferDtype = InferTensorScalarDtype(self, other, out);
auto selfWithStride = uniqueExecutor.get()->CreateView(
self, self->GetViewShape(), self->GetStorageShape(), self->GetViewStrides(), self->GetViewOffset());
CHECK_RET(selfWithStride != nullptr, ACLNN_ERR_INNER_NULLPTR);
const aclTensor* resTensor = nullptr;
bool canUseMuls = IsRegBase() &&
(self->GetDataType() == DataType::DT_BF16 ||
self->GetDataType() == DataType::DT_FLOAT16) &&
GetScalarDefaultDtype(other->GetDataType()) == DataType::DT_FLOAT;
canUseMuls = canUseMuls || (!IsRegBase() &&
self->GetDataType() == DataType::DT_BF16 &&
other->GetDataType() == DataType::DT_DOUBLE);
if (canUseMuls) {
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
resTensor = l0op::Muls(selfContiguous, other->ToFloat(), uniqueExecutor.get());
} else {
auto otherTensor = uniqueExecutor.get()->ConvertToTensor(other, inferDtype);
if (self->GetDataType() == inferDtype && l0op::IsMulSupportNonContiguous(self, otherTensor)) {
resTensor = l0op::Mul(selfWithStride, otherTensor, uniqueExecutor.get());
} else {
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto selfCast = l0op::Cast(selfContiguous, inferDtype, uniqueExecutor.get());
CHECK_RET(selfCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
resTensor = l0op::Mul(selfCast, otherTensor, uniqueExecutor.get());
}
}
CHECK_RET(resTensor != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castOut = l0op::Cast(resTensor, 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);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
static void MulCheckFormat(const aclTensor* self, const aclTensor* other){
ge::Format selfStorageFormat = self->GetStorageFormat();
ge::Format otherStorageFormat = other->GetStorageFormat();
if (selfStorageFormat != ge::Format::FORMAT_ND || otherStorageFormat != ge::Format::FORMAT_ND){
OP_LOGW("aclnnMul only support format ND.");
}
}
aclnnStatus aclnnMulGetWorkspaceSize(const aclTensor *self, const aclTensor *other, aclTensor *out,
uint64_t *workspaceSize, aclOpExecutor **executor) {
L2_DFX_PHASE_1(aclnnMul, DFX_IN(self, other), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckMulParams(self, other, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
MulCheckFormat(self, other);
if (self->IsEmpty() || other->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
bool isMixDataType = IsMulMixDtypeSupport(self, other);
bool isSupportNonContiguous = IsRegBase();
auto selfWithStride = uniqueExecutor.get()->CreateView(
self, self->GetViewShape(), self->GetStorageShape(), self->GetViewStrides(), self->GetViewOffset());
CHECK_RET(selfWithStride != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherWithStride = uniqueExecutor.get()->CreateView(
other, other->GetViewShape(), other->GetStorageShape(), other->GetViewStrides(), other->GetViewOffset());
CHECK_RET(otherWithStride != nullptr, ACLNN_ERR_INNER_NULLPTR);
const aclTensor* resTensor = nullptr;
if (isMixDataType) {
if (isSupportNonContiguous) {
resTensor = l0op::Mul(selfWithStride, otherWithStride, uniqueExecutor.get());
} else {
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherContiguous = l0op::Contiguous(other, uniqueExecutor.get());
CHECK_RET(otherContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
resTensor = l0op::Mul(selfContiguous, otherContiguous, uniqueExecutor.get());
}
} else {
auto promoteType = op::PromoteType(self->GetDataType(), other->GetDataType());
if (self->GetDataType() == promoteType && other->GetDataType() == promoteType && l0op::IsMulSupportNonContiguous(self, other)) {
resTensor = l0op::Mul(selfWithStride, otherWithStride, uniqueExecutor.get());
} else {
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherContiguous = l0op::Contiguous(other, uniqueExecutor.get());
CHECK_RET(otherContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto selfCast = l0op::Cast(selfContiguous, promoteType, uniqueExecutor.get());
CHECK_RET(selfCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherCast = l0op::Cast(otherContiguous, promoteType, uniqueExecutor.get());
CHECK_RET(otherCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
resTensor = l0op::Mul(selfCast, otherCast, uniqueExecutor.get());
}
}
CHECK_RET(resTensor != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castOut = l0op::Cast(resTensor, 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);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnInplaceMulsGetWorkspaceSize(aclTensor *selfRef, const aclScalar *other, uint64_t *workspaceSize,
aclOpExecutor **executor) {
L2_DFX_PHASE_1(aclnnInplaceMuls, DFX_IN(selfRef, other), DFX_OUT(selfRef));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckInplaceMulsParams(selfRef, other);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (selfRef->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto inferDtype = InferTensorScalarDtype(selfRef, other, selfRef);
auto selfRefContiguous = l0op::Contiguous(selfRef, uniqueExecutor.get());
CHECK_RET(selfRefContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
const aclTensor* resTensor = nullptr;
bool canUseMuls = IsRegBase() &&
(selfRefContiguous->GetDataType() == DataType::DT_BF16 ||
selfRefContiguous->GetDataType() == DataType::DT_FLOAT16) &&
GetScalarDefaultDtype(other->GetDataType()) == DataType::DT_FLOAT;
canUseMuls = canUseMuls || (!IsRegBase() &&
selfRefContiguous->GetDataType() == DataType::DT_BF16 &&
other->GetDataType() == DataType::DT_DOUBLE);
if (canUseMuls) {
resTensor = l0op::Muls(selfRefContiguous, other->ToFloat(), uniqueExecutor.get());
} else {
auto selfRefCast = l0op::Cast(selfRefContiguous, inferDtype, uniqueExecutor.get());
CHECK_RET(selfRefCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherTensor = uniqueExecutor.get()->ConvertToTensor(other, inferDtype);
resTensor = l0op::Mul(selfRefCast, otherTensor, uniqueExecutor.get());
}
CHECK_RET(resTensor != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castOut = l0op::Cast(resTensor, selfRef->GetDataType(), uniqueExecutor.get());
CHECK_RET(castOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(castOut, selfRef, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnInplaceMulGetWorkspaceSize(aclTensor *selfRef, const aclTensor *other, uint64_t *workspaceSize,
aclOpExecutor **executor) {
L2_DFX_PHASE_1(aclnnInplaceMul, DFX_IN(selfRef, other), DFX_OUT(selfRef));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckInplaceMulParams(selfRef, other);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
MulCheckFormat(selfRef, other);
if (selfRef->IsEmpty() || other->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto selfRefContiguous = l0op::Contiguous(selfRef, uniqueExecutor.get());
CHECK_RET(selfRefContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherContiguous = l0op::Contiguous(other, uniqueExecutor.get());
CHECK_RET(otherContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
bool isMixDataType = IsMulMixDtypeSupport(selfRefContiguous, otherContiguous);
const aclTensor* resTensor = nullptr;
if (IsRegBase() && isMixDataType) {
resTensor = l0op::Mul(selfRefContiguous, otherContiguous, uniqueExecutor.get());
} else {
auto promoteType = PromoteType(selfRef->GetDataType(), other->GetDataType());
auto selfRefCast = l0op::Cast(selfRefContiguous, promoteType, uniqueExecutor.get());
CHECK_RET(selfRefCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherCast = l0op::Cast(otherContiguous, promoteType, uniqueExecutor.get());
CHECK_RET(otherCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
resTensor = l0op::Mul(selfRefCast, otherCast, uniqueExecutor.get());
}
CHECK_RET(resTensor != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castOut = l0op::Cast(resTensor, selfRef->GetDataType(), uniqueExecutor.get());
CHECK_RET(castOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(castOut, selfRef, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnMuls(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream) {
L2_DFX_PHASE_2(aclnnMuls);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
aclnnStatus aclnnMul(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream) {
L2_DFX_PHASE_2(aclnnMul);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
aclnnStatus aclnnInplaceMuls(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream) {
L2_DFX_PHASE_2(aclnnInplaceMuls);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
aclnnStatus aclnnInplaceMul(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream) {
L2_DFX_PHASE_2(aclnnInplaceMul);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif