* 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_pdist.h"
#include "pdist.h"
#include "conversion/fill/op_api/fill.h"
#include "conversion/view_copy/op_api/view_copy.h"
#include "aclnn_kernels/contiguous.h"
#include "aclnn/aclnn_base.h"
#include "opdev/make_op_executor.h"
#include "opdev/shape_utils.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_dfx.h"
#include "opdev/op_executor.h"
#include "opdev/op_log.h"
#include "opdev/tensor_view_utils.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const std::initializer_list<op::DataType> DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT16, op::DataType::DT_FLOAT
};
inline static bool CheckNotNull(const aclTensor *self, const aclTensor *out) {
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(out, return false);
return true;
}
inline static bool CheckDtypeValid(const aclTensor *self, const aclTensor *out) {
OP_CHECK_DTYPE_NOT_SUPPORT(self, DTYPE_SUPPORT_LIST, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(out, DTYPE_SUPPORT_LIST, return false);
OP_CHECK_DTYPE_NOT_SAME(self, out, return false);
return true;
}
inline static aclnnStatus CheckParamsLogic(const aclTensor* self, float p, const aclTensor* out) {
OP_CHECK_WRONG_DIMENSION(self, 2, return ACLNN_ERR_PARAM_INVALID);
OP_CHECK_WRONG_DIMENSION(out, 1, return ACLNN_ERR_PARAM_INVALID);
int64_t N = self->GetViewShape().GetDim(0);
op::Shape expectOutShape = {N * (N - 1) / 2};
OP_CHECK_SHAPE_NOT_EQUAL_WITH_EXPECTED_SIZE(out, expectOutShape, return ACLNN_ERR_PARAM_INVALID);
if (p < 0 || std::isnan(p)) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "pdist only supports non-negative p values.");
return ACLNN_ERR_PARAM_INVALID;
}
return ACLNN_SUCCESS;
}
inline static aclnnStatus CheckParams(const aclTensor* self, float p, const aclTensor* out) {
CHECK_COND(CheckNotNull(self, out), ACLNN_ERR_PARAM_NULLPTR, "CheckNotNull failed!");
CHECK_COND(CheckDtypeValid(self, out), ACLNN_ERR_PARAM_INVALID, "CheckDtypeValid failed!");
CHECK_COND(CheckParamsLogic(self, p, out) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID, "CheckParamsLogic failed!");
return ACLNN_SUCCESS;
}
aclnnStatus aclnnPdistGetWorkspaceSize(const aclTensor* self, float p, aclTensor* out, uint64_t* workspaceSize,
aclOpExecutor** executor) {
OP_CHECK_COMM_INPUT(workspaceSize, executor);
L2_DFX_PHASE_1(aclnnPdist, DFX_IN(self, p), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, p, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
const aclTensor* PdistOutRet = nullptr;
if (self->GetViewShape().GetDim(0) <= 1) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
} else if (self->GetViewShape().GetDim(1) == 0) {
aclScalar* scalar = uniqueExecutor.get()->AllocScalar(0);
auto valueTensor = uniqueExecutor.get()->ConvertToTensor(scalar, out->GetDataType());
auto outputDims = op::ToShapeVector(out->GetViewShape());
aclIntArray* dimArray = uniqueExecutor.get()->AllocIntArray(outputDims.data(), outputDims.size());
CHECK_RET(dimArray != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto dimTensor = uniqueExecutor.get()->ConvertToTensor(dimArray, op::DataType::DT_INT64);
PdistOutRet = l0op::Fill(dimTensor, valueTensor, dimArray, uniqueExecutor.get());
} else {
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
PdistOutRet = l0op::Pdist(selfContiguous, p, uniqueExecutor.get());
}
CHECK_RET(PdistOutRet != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(PdistOutRet, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnPdist(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream) {
L2_DFX_PHASE_2(aclnnPdist);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif