@@ -0,0 +1,143 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Huawei Technologies Co., Ltd.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* SPDX-FileCopyrightText: Copyright contributors to the vllm-ascend project
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file aclnn_fused_gdn_gating.cpp
|
||||
* \brief ACLNN C-API (GetWorkspaceSize + Execute).
|
||||
*/
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include "aclnn_fused_gdn_gating.h"
|
||||
#include "fused_gdn_gating.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "aclnn_kernels/common/op_error_check.h"
|
||||
#include "opdev/common_types.h"
|
||||
#include "opdev/op_dfx.h"
|
||||
#include "opdev/op_executor.h"
|
||||
#include "opdev/op_log.h"
|
||||
#include "opdev/platform.h"
|
||||
|
||||
#include "aclnn_kernels/contiguous.h"
|
||||
|
||||
using namespace op;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
struct FusedGdnGatingParams {
|
||||
const aclTensor *aLog{nullptr};
|
||||
const aclTensor *a{nullptr};
|
||||
const aclTensor *b{nullptr};
|
||||
const aclTensor *dtBias{nullptr};
|
||||
float beta{1.0f};
|
||||
float threshold{20.0f};
|
||||
aclTensor *g{nullptr};
|
||||
aclTensor *betaOutput{nullptr};
|
||||
};
|
||||
|
||||
static const std::initializer_list<op::DataType> AB_TYPE_SUPPORT_LIST =
|
||||
{op::DataType::DT_BF16, op::DataType::DT_FLOAT16};
|
||||
static const std::initializer_list<op::DataType> FP32_TYPE_SUPPORT_LIST =
|
||||
{op::DataType::DT_FLOAT};
|
||||
static const std::initializer_list<op::DataType> PARAM_TYPE_SUPPORT_LIST =
|
||||
{op::DataType::DT_FLOAT, op::DataType::DT_BF16, op::DataType::DT_FLOAT16};
|
||||
|
||||
static inline bool CheckNotNull(const FusedGdnGatingParams ¶ms)
|
||||
{
|
||||
OP_CHECK_NULL(params.aLog, return false);
|
||||
OP_CHECK_NULL(params.a, return false);
|
||||
OP_CHECK_NULL(params.b, return false);
|
||||
OP_CHECK_NULL(params.dtBias, return false);
|
||||
OP_CHECK_NULL(params.g, return false);
|
||||
OP_CHECK_NULL(params.betaOutput, return false);
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool CheckDtype(const FusedGdnGatingParams ¶ms)
|
||||
{
|
||||
OP_CHECK_DTYPE_NOT_SUPPORT(params.aLog, PARAM_TYPE_SUPPORT_LIST, return false);
|
||||
OP_CHECK_DTYPE_NOT_SUPPORT(params.dtBias, PARAM_TYPE_SUPPORT_LIST, return false);
|
||||
OP_CHECK_DTYPE_NOT_SUPPORT(params.a, AB_TYPE_SUPPORT_LIST, return false);
|
||||
OP_CHECK_DTYPE_NOT_SUPPORT(params.b, AB_TYPE_SUPPORT_LIST, return false);
|
||||
OP_CHECK_DTYPE_NOT_SUPPORT(params.g, FP32_TYPE_SUPPORT_LIST, return false);
|
||||
OP_CHECK_DTYPE_NOT_SUPPORT(params.betaOutput, AB_TYPE_SUPPORT_LIST, return false);
|
||||
OP_CHECK(params.a->GetDataType() == params.b->GetDataType(),
|
||||
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "a and b must have the same dtype."),
|
||||
return false);
|
||||
OP_CHECK(params.aLog->GetDataType() == params.dtBias->GetDataType(),
|
||||
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "aLog and dtBias must have the same dtype."),
|
||||
return false);
|
||||
OP_CHECK(params.betaOutput->GetDataType() == params.b->GetDataType(),
|
||||
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "betaOutput and b must have the same dtype."),
|
||||
return false);
|
||||
return true;
|
||||
}
|
||||
|
||||
static aclnnStatus CheckParams(const FusedGdnGatingParams ¶ms)
|
||||
{
|
||||
CHECK_RET(CheckNotNull(params), ACLNN_ERR_PARAM_NULLPTR);
|
||||
CHECK_RET(CheckDtype(params), ACLNN_ERR_PARAM_INVALID);
|
||||
return ACLNN_SUCCESS;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
aclnnStatus aclnnFusedGdnGatingGetWorkspaceSize(
|
||||
const aclTensor *aLog, const aclTensor *a, const aclTensor *b,
|
||||
const aclTensor *dtBias, float beta, float threshold,
|
||||
aclTensor *g, aclTensor *betaOutput,
|
||||
uint64_t *workspaceSize, aclOpExecutor **executor)
|
||||
{
|
||||
L2_DFX_PHASE_1(aclnnFusedGdnGating,
|
||||
DFX_IN(aLog, a, b, dtBias, beta, threshold),
|
||||
DFX_OUT(g, betaOutput));
|
||||
|
||||
auto uniqueExecutor = CREATE_EXECUTOR();
|
||||
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
|
||||
|
||||
FusedGdnGatingParams params{aLog, a, b, dtBias, beta, threshold, g, betaOutput};
|
||||
CHECK_RET(CheckParams(params) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID);
|
||||
|
||||
// Bring inputs to a contiguous form that the kernel expects.
|
||||
auto aLogContig = l0op::Contiguous(aLog, uniqueExecutor.get());
|
||||
auto aContig = l0op::Contiguous(a, uniqueExecutor.get());
|
||||
auto bContig = l0op::Contiguous(b, uniqueExecutor.get());
|
||||
auto dtBiasContig = l0op::Contiguous(dtBias, uniqueExecutor.get());
|
||||
CHECK_RET(aLogContig != nullptr, ACLNN_ERR_INNER_NULLPTR);
|
||||
CHECK_RET(aContig != nullptr, ACLNN_ERR_INNER_NULLPTR);
|
||||
CHECK_RET(bContig != nullptr, ACLNN_ERR_INNER_NULLPTR);
|
||||
CHECK_RET(dtBiasContig != nullptr, ACLNN_ERR_INNER_NULLPTR);
|
||||
|
||||
auto result = l0op::FusedGdnGating(aLogContig, aContig, bContig, dtBiasContig,
|
||||
beta, threshold, uniqueExecutor.get());
|
||||
CHECK_RET(result.g != nullptr && result.beta_output != nullptr,
|
||||
ACLNN_ERR_INNER_NULLPTR);
|
||||
|
||||
// Copy kernel results into the caller-provided output tensors.
|
||||
auto vcG = l0op::ViewCopy(result.g, g, uniqueExecutor.get());
|
||||
CHECK_RET(vcG != nullptr, ACLNN_ERR_INNER_NULLPTR);
|
||||
auto vcBeta = l0op::ViewCopy(result.beta_output, betaOutput, uniqueExecutor.get());
|
||||
CHECK_RET(vcBeta != nullptr, ACLNN_ERR_INNER_NULLPTR);
|
||||
|
||||
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
|
||||
uniqueExecutor.ReleaseTo(executor);
|
||||
return ACLNN_SUCCESS;
|
||||
}
|
||||
|
||||
aclnnStatus aclnnFusedGdnGating(void *workspace, uint64_t workspaceSize,
|
||||
aclOpExecutor *executor, aclrtStream stream)
|
||||
{
|
||||
L2_DFX_PHASE_2(aclnnFusedGdnGating);
|
||||
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Huawei Technologies Co., Ltd.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* SPDX-FileCopyrightText: Copyright contributors to the vllm-ascend project
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file aclnn_fused_gdn_gating.h
|
||||
* \brief ACLNN C-API for FusedGdnGating.
|
||||
*/
|
||||
|
||||
#ifndef OP_API_ACLNN_FUSED_GDN_GATING_H
|
||||
#define OP_API_ACLNN_FUSED_GDN_GATING_H
|
||||
|
||||
#include "aclnn/aclnn_base.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief FusedGdnGating phase-1: compute required workspace size.
|
||||
* @param [in] aLog : A_log, [num_heads], dtype fp32/bf16/fp16.
|
||||
* @param [in] a : a, [batch, num_heads], dtype bf16/fp16.
|
||||
* @param [in] b : b, [batch, num_heads], dtype bf16/fp16.
|
||||
* @param [in] dtBias : dt_bias, [num_heads], same dtype as aLog.
|
||||
* @param [in] beta : softplus beta (default 1.0).
|
||||
* @param [in] threshold : softplus threshold (default 20.0).
|
||||
* @param [out] g : output gate, [1, batch, num_heads], dtype fp32.
|
||||
* @param [out] betaOutput : sigmoid(b), [1, batch, num_heads], same dtype as a/b.
|
||||
* @param [out] workspaceSize: required workspace bytes on device.
|
||||
* @param [out] executor : op executor handle.
|
||||
*/
|
||||
__attribute__((visibility("default"))) aclnnStatus aclnnFusedGdnGatingGetWorkspaceSize(
|
||||
const aclTensor *aLog, const aclTensor *a, const aclTensor *b,
|
||||
const aclTensor *dtBias, float beta, float threshold,
|
||||
aclTensor *g, aclTensor *betaOutput,
|
||||
uint64_t *workspaceSize, aclOpExecutor **executor);
|
||||
|
||||
/**
|
||||
* @brief FusedGdnGating phase-2: launch the kernel.
|
||||
*/
|
||||
__attribute__((visibility("default"))) aclnnStatus aclnnFusedGdnGating(
|
||||
void *workspace, uint64_t workspaceSize,
|
||||
aclOpExecutor *executor, aclrtStream stream);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // OP_API_ACLNN_FUSED_GDN_GATING_H
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Huawei Technologies Co., Ltd.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* SPDX-FileCopyrightText: Copyright contributors to the vllm-ascend project
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file fused_gdn_gating.cpp
|
||||
* \brief L0-level API for FusedGdnGating.
|
||||
*/
|
||||
|
||||
#include "fused_gdn_gating.h"
|
||||
#include "aclnn_kernels/common/op_error_check.h"
|
||||
#include "opdev/make_op_executor.h"
|
||||
#include "opdev/op_def.h"
|
||||
#include "opdev/op_dfx.h"
|
||||
#include "opdev/op_executor.h"
|
||||
#include "opdev/op_log.h"
|
||||
#include "opdev/shape_utils.h"
|
||||
|
||||
using namespace op;
|
||||
|
||||
namespace l0op {
|
||||
|
||||
OP_TYPE_REGISTER(FusedGdnGating);
|
||||
|
||||
static constexpr FusedGdnGatingOutput kNullOutput{nullptr, nullptr};
|
||||
|
||||
FusedGdnGatingOutput FusedGdnGating(const aclTensor *aLog, const aclTensor *a,
|
||||
const aclTensor *b, const aclTensor *dtBias,
|
||||
float beta, float threshold,
|
||||
aclOpExecutor *executor)
|
||||
{
|
||||
L0_DFX(FusedGdnGating, aLog, a, b, dtBias, beta, threshold);
|
||||
|
||||
const DataType betaDtype = b->GetDataType();
|
||||
const Format format = Format::FORMAT_ND;
|
||||
|
||||
auto g = executor->AllocTensor(DataType::DT_FLOAT, format, format);
|
||||
OP_CHECK(g != nullptr, OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "g AllocTensor failed."),
|
||||
return kNullOutput);
|
||||
|
||||
auto betaOutput = executor->AllocTensor(betaDtype, format, format);
|
||||
OP_CHECK(betaOutput != nullptr,
|
||||
OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "beta_output AllocTensor failed."),
|
||||
return kNullOutput);
|
||||
|
||||
auto ret = INFER_SHAPE(FusedGdnGating,
|
||||
OP_INPUT(aLog, a, b, dtBias),
|
||||
OP_OUTPUT(g, betaOutput),
|
||||
OP_ATTR(beta, threshold));
|
||||
OP_CHECK_INFERSHAPE(ret != ACLNN_SUCCESS, return kNullOutput,
|
||||
"FusedGdnGating InferShape failed.");
|
||||
|
||||
ret = ADD_TO_LAUNCHER_LIST_AICORE(FusedGdnGating,
|
||||
OP_INPUT(aLog, a, b, dtBias),
|
||||
OP_OUTPUT(g, betaOutput),
|
||||
OP_ATTR(beta, threshold));
|
||||
OP_CHECK_ADD_TO_LAUNCHER_LIST_AICORE(ret != ACLNN_SUCCESS, return kNullOutput,
|
||||
"FusedGdnGating ADD_TO_LAUNCHER_LIST_AICORE failed.");
|
||||
|
||||
return FusedGdnGatingOutput{g, betaOutput};
|
||||
}
|
||||
|
||||
} // namespace l0op
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Huawei Technologies Co., Ltd.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* SPDX-FileCopyrightText: Copyright contributors to the vllm-ascend project
|
||||
*/
|
||||
|
||||
#ifndef PTA_NPU_OP_API_FUSED_GDN_GATING_H
|
||||
#define PTA_NPU_OP_API_FUSED_GDN_GATING_H
|
||||
|
||||
#include "opdev/op_executor.h"
|
||||
#include "opdev/make_op_executor.h"
|
||||
|
||||
namespace l0op {
|
||||
|
||||
struct FusedGdnGatingOutput {
|
||||
const aclTensor *g;
|
||||
const aclTensor *beta_output;
|
||||
};
|
||||
|
||||
FusedGdnGatingOutput FusedGdnGating(const aclTensor *aLog, const aclTensor *a,
|
||||
const aclTensor *b, const aclTensor *dtBias,
|
||||
float beta, float threshold,
|
||||
aclOpExecutor *executor);
|
||||
|
||||
} // namespace l0op
|
||||
|
||||
#endif // PTA_NPU_OP_API_FUSED_GDN_GATING_H
|
||||
Reference in New Issue
Block a user