* 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.
*/
* \file aclnn_linalg_qr.cpp
* \brief
*/
#include "aclnn_linalg_qr.h"
#include "../../../q_r/op_host/op_api/qr.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "opdev/op_dfx.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const std::initializer_list<op::DataType> DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_DOUBLE,
op::DataType::DT_COMPLEX64, op::DataType::DT_COMPLEX128};
static const int64_t REDUCE_REDUCED = 0;
static const int64_t REDUCE_COMPLETE = 1;
static const int64_t REDUCE_R = 2;
static inline bool CheckNotNull(const aclTensor *self, const aclTensor *Q, const aclTensor *R)
{
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(Q, return false);
OP_CHECK_NULL(R, return false);
return true;
}
static bool CheckDtypeValid(const aclTensor *self, const aclTensor *Q, const aclTensor *R)
{
OP_CHECK_DTYPE_NOT_SUPPORT(self, DTYPE_SUPPORT_LIST, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(Q, DTYPE_SUPPORT_LIST, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(R, DTYPE_SUPPORT_LIST, return false);
return true;
}
static bool CheckShape(const aclTensor *self, int64_t mode, const aclTensor *Q, const aclTensor *R)
{
op::Shape selfShape = self->GetViewShape();
auto dimNum = selfShape.GetDimNum();
OP_CHECK_MIN_DIM(self, 2, return false);
OP_CHECK_MAX_DIM(self, 8, return false);
auto m = selfShape.GetDim(dimNum - 2);
auto n = selfShape.GetDim(dimNum - 1);
auto k = std::min(m, n);
auto emptyShape = op::Shape{0};
auto qShape = selfShape;
auto rShape = selfShape;
if (mode == REDUCE_R) {
qShape = emptyShape;
rShape.SetDim(dimNum - 2, k);
} else if (mode == REDUCE_COMPLETE) {
qShape.SetDim(dimNum - 1, m);
} else {
qShape.SetDim(dimNum - 1, k);
rShape.SetDim(dimNum - 2, k);
}
OP_CHECK(Q->GetViewShape() == qShape,
OP_LOGE(ACLNN_ERR_PARAM_INVALID,
"The output Q should have the following shape %s, but got %s instead.",
op::ToString(qShape).GetString(), op::ToString(Q->GetViewShape()).GetString()),
return false);
OP_CHECK(R->GetViewShape() == rShape,
OP_LOGE(ACLNN_ERR_PARAM_INVALID,
"The output R should have the following shape %s, but got %s instead.",
op::ToString(rShape).GetString(), op::ToString(R->GetViewShape()).GetString()),
return false);
return true;
}
static aclnnStatus CheckParams(const aclTensor *self, int64_t mode, const aclTensor *Q, const aclTensor *R)
{
CHECK_RET(CheckNotNull(self, Q, R), ACLNN_ERR_INNER_NULLPTR);
CHECK_RET(CheckDtypeValid(self, Q, R), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, mode, Q, R), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnLinalgQrGetWorkspaceSize(const aclTensor *self, int64_t mode, aclTensor *Q, aclTensor *R,
uint64_t *workspaceSize, aclOpExecutor **executor)
{
L2_DFX_PHASE_1(aclnnLinalgQr, DFX_IN(self, mode), DFX_OUT(Q, R));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, mode, Q, R);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (self->IsEmpty() && Q->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
if (selfContiguous->GetDataType() == op::DataType::DT_FLOAT16) {
selfContiguous = l0op::Cast(selfContiguous, op::DataType::DT_FLOAT, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
auto some = (mode == REDUCE_COMPLETE) ? false : true;
auto outArray = l0op::Qr(selfContiguous, some, uniqueExecutor.get());
auto resultQ = std::get<0>(outArray);
auto resultR = std::get<1>(outArray);
CHECK_RET(resultQ != nullptr && resultR != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castOutQ = l0op::Cast(resultQ, Q->GetDataType(), uniqueExecutor.get());
CHECK_RET(castOutQ != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castOutR = l0op::Cast(resultR, R->GetDataType(), uniqueExecutor.get());
CHECK_RET(castOutR != nullptr, ACLNN_ERR_INNER_NULLPTR);
if (mode != REDUCE_R) {
auto viewCopyResultQ = l0op::ViewCopy(castOutQ, Q, uniqueExecutor.get());
CHECK_RET(viewCopyResultQ != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
auto viewCopyResultR = l0op::ViewCopy(castOutR, R, uniqueExecutor.get());
CHECK_RET(viewCopyResultR != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnLinalgQr(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnLinalgQr);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif