* 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_logsoftmax_backward.h"
#include "logsoftmax_grad.h"
#include "aclnn/aclnn_base.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "opdev/common_types.h"
#include "opdev/data_type_utils.h"
#include "opdev/shape_utils.h"
#include "opdev/format_utils.h"
#include "opdev/tensor_view_utils.h"
#include "opdev/platform.h"
#include "opdev/op_dfx.h"
#include "opdev/op_executor.h"
#include "opdev/op_log.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static bool CheckNotNull(const aclTensor* gradOutput, const aclTensor* output, const aclTensor* out)
{
OP_CHECK_NULL(output, return false);
OP_CHECK_NULL(gradOutput, return false);
OP_CHECK_NULL(out, return false);
return true;
}
static const std::initializer_list<op::DataType> dtype_support_list = {op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16,
op::DataType::DT_BF16};
static const size_t AXIS_LIMIT = 8;
static inline bool CheckSocVersionIsSupportBf16(void)
{
return GetCurrentPlatformInfo().GetSocVersion() >= SocVersion::ASCEND910B &&
GetCurrentPlatformInfo().GetSocVersion() <= SocVersion::ASCEND910E;
}
static bool CheckDtypeValid(const aclTensor* gradOutput, const aclTensor* output)
{
if (!CheckSocVersionIsSupportBf16() &&
((gradOutput->GetDataType() == op::DataType::DT_BF16) || (output->GetDataType() == op::DataType::DT_BF16))) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "aclnnLogSoftmaxBackward is not support bfloat16 in current socversion.");
return false;
}
OP_CHECK_DTYPE_NOT_SUPPORT(output, dtype_support_list, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(gradOutput, dtype_support_list, return false);
OP_CHECK_DTYPE_NOT_SAME(gradOutput, output, return false);
return true;
}
static bool CheckDim(const aclTensor* self, int64_t dim)
{
auto selfViewShape = self->GetViewShape();
auto selfDimNumber = static_cast<int64_t>(selfViewShape.GetDimNum());
if (selfDimNumber == 0) {
selfDimNumber++;
}
if (dim >= selfDimNumber || dim < -selfDimNumber) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "provided dim %ld not in the range of input size %ld.", dim, selfDimNumber);
return false;
}
return true;
}
static bool CheckShape(const aclTensor* gradOutput, const aclTensor* output, aclTensor* out)
{
if (gradOutput->GetViewShape().GetDimNum() > AXIS_LIMIT || output->GetViewShape().GetDimNum() > AXIS_LIMIT) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Dim of input tensor can't be greater than 8.");
return false;
}
OP_CHECK_MAX_DIM(gradOutput, AXIS_LIMIT, return false);
OP_CHECK_SHAPE_NOT_EQUAL(gradOutput, output, return false);
OP_CHECK_SHAPE_NOT_EQUAL(gradOutput, out, return false);
return true;
}
static aclnnStatus CheckParams(const aclTensor* gradOutput, const aclTensor* output, int64_t dim, aclTensor* out)
{
CHECK_RET(CheckNotNull(gradOutput, output, out), ACLNN_ERR_PARAM_NULLPTR);
if ((gradOutput->IsEmpty()) || (output->IsEmpty())) {
return ACLNN_SUCCESS;
}
CHECK_RET(CheckDtypeValid(gradOutput, output), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(gradOutput, output, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckDim(gradOutput, dim), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnLogSoftmaxBackwardGetWorkspaceSize(const aclTensor* gradOutput, const aclTensor* output, int64_t dim,
aclTensor* out, uint64_t* workspaceSize, aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnLogSoftmaxBackward, DFX_IN(gradOutput, output, dim), DFX_OUT(out));
auto ret = CheckParams(gradOutput, output, dim, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
if ((gradOutput->IsEmpty()) || (output->IsEmpty())) {
*workspaceSize = static_cast<uint64_t>(0);
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto gradOutput_contiguous = l0op::Contiguous(gradOutput, uniqueExecutor.get());
CHECK_RET(gradOutput_contiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto output_contiguous = l0op::Contiguous(output, uniqueExecutor.get());
CHECK_RET(output_contiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto op_out = l0op::LogSoftmaxGrad(gradOutput_contiguous, output_contiguous, dim, uniqueExecutor.get());
CHECK_RET(op_out != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto cast_out = l0op::Cast(op_out, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(cast_out != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto view_copy_result = l0op::ViewCopy(cast_out, out, uniqueExecutor.get());
CHECK_RET(view_copy_result != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnLogSoftmaxBackward(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor,
aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnLogSoftmaxBackward);
OP_LOGD("Entering aclnnLogSoftmaxBackward");
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif