* 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_quant_scatter.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "quant_update_scatter.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "aclnn/aclnn_base.h"
#include "opdev/common_types.h"
#include "opdev/data_type_utils.h"
#include "opdev/format_utils.h"
#include "opdev/op_dfx.h"
#include "opdev/op_executor.h"
#include "opdev/op_log.h"
#include "opdev/shape_utils.h"
#include "opdev/tensor_view_utils.h"
#include "opdev/platform.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const std::initializer_list<DataType> DTYPE_SUPPORT_LIST_SELFREF = {DataType::DT_INT8};
static const std::initializer_list<DataType> DTYPE_SUPPORT_LIST_INDICES = {DataType::DT_INT32, DataType::DT_INT64};
static const std::initializer_list<DataType> DTYPE_SUPPORT_LIST_UPDATES = {DataType::DT_BF16, DataType::DT_FLOAT16};
static const std::initializer_list<DataType> DTYPE_SUPPORT_LIST_QUANT_SCALES = {DataType::DT_BF16, DataType::DT_FLOAT};
static const std::initializer_list<DataType> DTYPE_SUPPORT_LIST_QUANT_ZERO_POINTS = {DataType::DT_BF16,
DataType::DT_INT32};
static inline bool CheckNotNull(const aclTensor* selfRef, const aclTensor* indices, const aclTensor* updates,
const aclTensor* quantScales) {
OP_CHECK_NULL(selfRef, return false);
OP_CHECK_NULL(indices, return false);
OP_CHECK_NULL(updates, return false);
OP_CHECK_NULL(quantScales, return false);
return true;
}
static inline bool CheckDtypeCombineValid(const aclTensor* updates, const aclTensor* quantScales,
const aclTensor* quantZeroPoints) {
DataType updatesDtype = updates->GetDataType();
DataType quantScalesDtype = quantScales->GetDataType();
if (quantZeroPoints) {
DataType quantZeroPointsDtype = quantZeroPoints->GetDataType();
if (updatesDtype == DataType::DT_FLOAT16 && quantScalesDtype == DataType::DT_FLOAT &&
quantZeroPointsDtype == DataType::DT_INT32) {
return true;
} else if (updatesDtype == DataType::DT_BF16 && quantScalesDtype == DataType::DT_BF16 &&
quantZeroPointsDtype == DataType::DT_BF16) {
return true;
} else {
OP_LOGE(ACLNN_ERR_PARAM_INVALID,
"(updates,quantScales,quantZeroPoints) Dtype should be (float16, float, int32) or (bfloat16, bfloat16, "
"bfloat16), but is (%s, %s, %s).",
op::ToString(updates->GetDataType()).GetString(), op::ToString(quantScales->GetDataType()).GetString(),
op::ToString(quantZeroPoints->GetDataType()).GetString());
return false;
}
} else {
if (updatesDtype == DataType::DT_FLOAT16 && quantScalesDtype == DataType::DT_FLOAT) {
return true;
} else if (updatesDtype == DataType::DT_BF16 && quantScalesDtype == DataType::DT_BF16) {
return true;
} else {
OP_LOGE(ACLNN_ERR_PARAM_INVALID,
"(updates,quantScales) Dtype should be (float16, float) or (bfloat16, bfloat16), but is (%s, %s).",
op::ToString(updates->GetDataType()).GetString(), op::ToString(quantScales->GetDataType()).GetString());
return false;
}
}
}
static inline bool CheckDtypeValid(const aclTensor* selfRef, const aclTensor* indices, const aclTensor* updates,
const aclTensor* quantScales, const aclTensor* quantZeroPoints) {
OP_CHECK_DTYPE_NOT_SUPPORT(selfRef, DTYPE_SUPPORT_LIST_SELFREF, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(indices, DTYPE_SUPPORT_LIST_INDICES, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(updates, DTYPE_SUPPORT_LIST_UPDATES, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(quantScales, DTYPE_SUPPORT_LIST_QUANT_SCALES, return false);
if (quantZeroPoints) {
OP_CHECK_DTYPE_NOT_SUPPORT(quantZeroPoints, DTYPE_SUPPORT_LIST_QUANT_ZERO_POINTS, return false);
}
if (!CheckDtypeCombineValid(updates, quantScales, quantZeroPoints)) {
return false;
}
return true;
}
static inline bool CheckShape(const aclTensor* selfRef, const aclTensor* updates) {
if (selfRef->GetViewShape().GetDimNum() != updates->GetViewShape().GetDimNum()) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "dimension of selfRef and updates should equal");
return false;
}
return true;
}
static void CheckFormatNZ(const aclTensor* x)
{
if (x == nullptr) { return; }
op::Format format = x->GetStorageFormat();
if (format == Format::FORMAT_FRACTAL_NZ) {
OP_LOGW("Format of input gets [%s], this format may lead to precision failure",
op::ToString(format).GetString());
}
}
static aclnnStatus CheckParams(const aclTensor* selfRef, const aclTensor* indices, const aclTensor* updates,
const aclTensor* quantScales, const aclTensor* quantZeroPoints) {
CHECK_RET(CheckNotNull(selfRef, indices, updates, quantScales), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(selfRef, indices, updates, quantScales, quantZeroPoints), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(selfRef, updates), ACLNN_ERR_PARAM_INVALID);
CheckFormatNZ(selfRef);
CheckFormatNZ(indices);
CheckFormatNZ(updates);
CheckFormatNZ(quantScales);
CheckFormatNZ(quantZeroPoints);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnInplaceQuantScatterGetWorkspaceSize(aclTensor* selfRef, const aclTensor* indices,
const aclTensor* updates, const aclTensor* quantScales,
const aclTensor* quantZeroPoints, int64_t axis, int64_t quantAxis,
int64_t reduction, uint64_t* workspaceSize,
aclOpExecutor** executor) {
L2_DFX_PHASE_1(aclnnInplaceQuantScatter,
DFX_IN(selfRef, indices, updates, quantScales, quantZeroPoints, axis, quantAxis, reduction),
DFX_OUT(selfRef));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(selfRef, indices, updates, quantScales, quantZeroPoints);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (selfRef->IsEmpty() || indices->IsEmpty() || updates->IsEmpty() || quantScales->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 indicesContiguous = l0op::Contiguous(indices, uniqueExecutor.get());
CHECK_RET(indicesContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto updatesContiguous = l0op::Contiguous(updates, uniqueExecutor.get());
CHECK_RET(updatesContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto quantScalesContiguous = l0op::Contiguous(quantScales, uniqueExecutor.get());
CHECK_RET(quantScalesContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
const aclTensor* quantZeroPointsContiguous = nullptr;
if (quantZeroPoints) {
quantZeroPointsContiguous = l0op::Contiguous(quantZeroPoints, uniqueExecutor.get());
CHECK_RET(quantZeroPointsContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
const std::string reduce = "update";
auto quantUpdateScatterResult =
l0op::QuantUpdateScatter(selfRefContiguous, indicesContiguous, updatesContiguous, quantScalesContiguous,
quantZeroPointsContiguous, reduce, axis, quantAxis, uniqueExecutor.get());
CHECK_RET(quantUpdateScatterResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(quantUpdateScatterResult, selfRef, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnInplaceQuantScatter(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor,
aclrtStream stream) {
L2_DFX_PHASE_2(aclnnInplaceQuantScatter);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif