162
csrc/moe/chunk_fwd_o/op_host/op_api/aclnn_chunk_fwd_o.cpp
Normal file
162
csrc/moe/chunk_fwd_o/op_host/op_api/aclnn_chunk_fwd_o.cpp
Normal file
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Tianjin University, 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_chunk_fwd_o.h"
|
||||
#include "chunk_fwd_o.h"
|
||||
#include <dlfcn.h>
|
||||
#include <new>
|
||||
|
||||
#include "aclnn_kernels/transdata.h"
|
||||
#include "aclnn_kernels/contiguous.h"
|
||||
#include "acl/acl.h"
|
||||
#include "aclnn/aclnn_base.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/platform.h"
|
||||
#include "opdev/shape_utils.h"
|
||||
#include "opdev/tensor_view_utils.h"
|
||||
#include "opdev/make_op_executor.h"
|
||||
|
||||
|
||||
using namespace op;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct ChunkFwdOParams {
|
||||
const aclTensor *q = nullptr;
|
||||
const aclTensor *k = nullptr;
|
||||
const aclTensor *v = nullptr;
|
||||
const aclTensor *h = nullptr;
|
||||
const aclTensor *g = nullptr;
|
||||
const aclIntArray *cuSeqlensOptional = nullptr;
|
||||
const aclIntArray *chunkOffsetsOptional = nullptr;
|
||||
double scale = 1.0;
|
||||
int64_t chunkSize = 64;
|
||||
const aclTensor *oOut = nullptr;
|
||||
};
|
||||
|
||||
static aclnnStatus CheckNotNull(ChunkFwdOParams params)
|
||||
{
|
||||
CHECK_COND(params.q != nullptr, ACLNN_ERR_PARAM_NULLPTR, "q must not be nullptr.");
|
||||
CHECK_COND(params.k != nullptr, ACLNN_ERR_PARAM_NULLPTR, "k must not be nullptr.");
|
||||
CHECK_COND(params.v != nullptr, ACLNN_ERR_PARAM_NULLPTR, "v must not be nullptr.");
|
||||
CHECK_COND(params.h != nullptr, ACLNN_ERR_PARAM_NULLPTR, "h must not be nullptr.");
|
||||
CHECK_COND(params.g != nullptr, ACLNN_ERR_PARAM_NULLPTR, "g must not be nullptr.");
|
||||
|
||||
CHECK_COND(params.oOut != nullptr, ACLNN_ERR_PARAM_NULLPTR, "oOut must not be nullptr.");
|
||||
return ACLNN_SUCCESS;
|
||||
}
|
||||
|
||||
static aclnnStatus CheckFormat(ChunkFwdOParams params)
|
||||
{
|
||||
return ACLNN_SUCCESS;
|
||||
}
|
||||
|
||||
static aclnnStatus CheckShape(ChunkFwdOParams params)
|
||||
{
|
||||
return ACLNN_SUCCESS;
|
||||
}
|
||||
|
||||
static aclnnStatus DataContiguous(const aclTensor *&tensor, aclOpExecutor *executor)
|
||||
{
|
||||
tensor = l0op::Contiguous(tensor, executor);
|
||||
CHECK_RET(tensor != nullptr, ACLNN_ERR_INNER_NULLPTR);
|
||||
return ACLNN_SUCCESS;
|
||||
}
|
||||
|
||||
static aclnnStatus ParamsDataContiguous(ChunkFwdOParams ¶ms, aclOpExecutor *executorPtr)
|
||||
{
|
||||
CHECK_COND(DataContiguous(params.q, executorPtr) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID,
|
||||
"Contiguous q failed.");
|
||||
CHECK_COND(DataContiguous(params.k, executorPtr) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID,
|
||||
"Contiguous k failed.");
|
||||
CHECK_COND(DataContiguous(params.v, executorPtr) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID,
|
||||
"Contiguous v failed.");
|
||||
CHECK_COND(DataContiguous(params.h, executorPtr) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID,
|
||||
"Contiguous h failed.");
|
||||
CHECK_COND(DataContiguous(params.g, executorPtr) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID,
|
||||
"Contiguous g failed.");
|
||||
|
||||
return ACLNN_SUCCESS;
|
||||
}
|
||||
|
||||
static aclnnStatus CheckDtype(ChunkFwdOParams params)
|
||||
{
|
||||
return ACLNN_SUCCESS;
|
||||
}
|
||||
|
||||
static aclnnStatus CheckParams(ChunkFwdOParams params)
|
||||
{
|
||||
CHECK_RET(CheckNotNull(params) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID);
|
||||
CHECK_RET(CheckFormat(params) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID);
|
||||
CHECK_RET(CheckShape(params) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID);
|
||||
CHECK_RET(CheckDtype(params) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID);
|
||||
return ACLNN_SUCCESS;
|
||||
}
|
||||
|
||||
aclnnStatus aclnnChunkFwdOGetWorkspaceSize(
|
||||
const aclTensor *q,
|
||||
const aclTensor *k,
|
||||
const aclTensor *v,
|
||||
const aclTensor *h,
|
||||
const aclTensor *g,
|
||||
const aclIntArray *cuSeqlensOptional,
|
||||
const aclIntArray *chunkOffsetsOptional,
|
||||
double scale,
|
||||
int64_t chunkSize,
|
||||
const aclTensor *oOut,
|
||||
uint64_t *workspaceSize,
|
||||
aclOpExecutor **executor)
|
||||
{
|
||||
ChunkFwdOParams params{q, k, v, h, g, cuSeqlensOptional, chunkOffsetsOptional, scale, chunkSize, oOut};
|
||||
// Standard syntax, Check parameters.
|
||||
L2_DFX_PHASE_1(aclnnChunkFwdO, DFX_IN(q, k, v, h, g, cuSeqlensOptional, chunkOffsetsOptional),
|
||||
DFX_OUT(oOut));
|
||||
// 固定写法,创建OpExecutor
|
||||
auto uniqueExecutor = CREATE_EXECUTOR();
|
||||
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
|
||||
auto executorPtr = uniqueExecutor.get();
|
||||
// 固定写法,参数检查
|
||||
auto ret = CheckParams(params);
|
||||
CHECK_RET(ret == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID);
|
||||
CHECK_COND(ParamsDataContiguous(params, executorPtr) == ACLNN_SUCCESS, ACLNN_ERR_PARAM_INVALID,
|
||||
"ParamsDataContiguous failed.");
|
||||
auto result = l0op::ChunkFwdO(params.q, params.k, params.v, params.h, params.g, params.cuSeqlensOptional, params.chunkOffsetsOptional, params.scale, params.chunkSize, params.oOut, executorPtr);
|
||||
CHECK_RET(result[0] != nullptr, ACLNN_ERR_PARAM_NULLPTR);
|
||||
|
||||
// If the output tensor is non-contiguous, convert the calculated contiguous tensor to non-contiguous.
|
||||
auto viewCopyResult = l0op::ViewCopy(result[0], params.oOut, executorPtr);
|
||||
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
|
||||
|
||||
// Standard syntax, get the size of workspace needed during computation.
|
||||
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
|
||||
uniqueExecutor.ReleaseTo(executor);
|
||||
return ACLNN_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
aclnnStatus aclnnChunkFwdO(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)
|
||||
{
|
||||
L2_DFX_PHASE_2(aclnnChunkFwdO);
|
||||
CHECK_COND(CommonOpExecutorRun(workspace, workspaceSize, executor, stream) == ACLNN_SUCCESS, ACLNN_ERR_INNER,
|
||||
"This is an error in ChunkFwdO launch aicore.");
|
||||
return ACLNN_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
65
csrc/moe/chunk_fwd_o/op_host/op_api/aclnn_chunk_fwd_o.h
Normal file
65
csrc/moe/chunk_fwd_o/op_host/op_api/aclnn_chunk_fwd_o.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Tianjin University, 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.
|
||||
*/
|
||||
#ifndef OP_API_INC_ACLNN_CHUNK_FWD_O_H
|
||||
#define OP_API_INC_ACLNN_CHUNK_FWD_O_H
|
||||
#include "aclnn/aclnn_base.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* function: aclnnChunkFwdOGetWorkspaceSize
|
||||
* parameters :
|
||||
* q : required
|
||||
* k : required
|
||||
* v : required
|
||||
* h : required
|
||||
* g : required
|
||||
* cuSeqlensOptional : optional
|
||||
* chunkOffsetsOptional : optional
|
||||
* scale : required
|
||||
* chunkSize : required
|
||||
* oOut : required
|
||||
* workspaceSize : size of workspace(output).
|
||||
* executor : executor context(output).
|
||||
*/
|
||||
__attribute__((visibility("default")))
|
||||
aclnnStatus aclnnChunkFwdOGetWorkspaceSize(
|
||||
const aclTensor *q,
|
||||
const aclTensor *k,
|
||||
const aclTensor *v,
|
||||
const aclTensor *h,
|
||||
const aclTensor *g,
|
||||
const aclIntArray *cuSeqlensOptional,
|
||||
const aclIntArray *chunkOffsetsOptional,
|
||||
double scale,
|
||||
int64_t chunkSize,
|
||||
const aclTensor *oOut,
|
||||
uint64_t *workspaceSize,
|
||||
aclOpExecutor **executor);
|
||||
|
||||
/* function: aclnnChunkFwdO
|
||||
* parameters :
|
||||
* workspace : workspace memory addr(input).
|
||||
* workspaceSize : size of workspace(input).
|
||||
* executor : executor context(input).
|
||||
* stream : acl stream.
|
||||
*/
|
||||
__attribute__((visibility("default")))
|
||||
aclnnStatus aclnnChunkFwdO(
|
||||
void *workspace,
|
||||
uint64_t workspaceSize,
|
||||
aclOpExecutor *executor,
|
||||
aclrtStream stream);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
67
csrc/moe/chunk_fwd_o/op_host/op_api/chunk_fwd_o.cpp
Normal file
67
csrc/moe/chunk_fwd_o/op_host/op_api/chunk_fwd_o.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Tianjin University, 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 "opdev/op_log.h"
|
||||
#include "opdev/op_dfx.h"
|
||||
#include "opdev/make_op_executor.h"
|
||||
#include "chunk_fwd_o.h"
|
||||
|
||||
using namespace op;
|
||||
|
||||
namespace l0op {
|
||||
OP_TYPE_REGISTER(ChunkFwdO);
|
||||
|
||||
const std::array<const aclTensor *, 1> ChunkFwdO(
|
||||
const aclTensor *q,
|
||||
const aclTensor *k,
|
||||
const aclTensor *v,
|
||||
const aclTensor *h,
|
||||
const aclTensor *g,
|
||||
const aclIntArray *cuSeqlensOptional,
|
||||
const aclIntArray *chunkOffsetsOptional,
|
||||
double scale,
|
||||
int64_t chunkSize,
|
||||
const aclTensor *oOut,
|
||||
aclOpExecutor *executor)
|
||||
{
|
||||
L0_DFX(ChunkFwdO, q, k, v, h, g, cuSeqlensOptional, chunkOffsetsOptional, scale, chunkSize, oOut);
|
||||
|
||||
const aclTensor *actualCuSeqlens = nullptr;
|
||||
if (cuSeqlensOptional) {
|
||||
actualCuSeqlens = executor->ConvertToTensor(cuSeqlensOptional, DataType::DT_INT64);
|
||||
const_cast<aclTensor *>(actualCuSeqlens)->SetStorageFormat(Format::FORMAT_ND);
|
||||
const_cast<aclTensor *>(actualCuSeqlens)->SetViewFormat(Format::FORMAT_ND);
|
||||
const_cast<aclTensor *>(actualCuSeqlens)->SetOriginalFormat(Format::FORMAT_ND);
|
||||
} else {
|
||||
actualCuSeqlens = nullptr;
|
||||
}
|
||||
|
||||
const aclTensor *actualChunkOffsets = nullptr;
|
||||
if (chunkOffsetsOptional) {
|
||||
actualChunkOffsets = executor->ConvertToTensor(chunkOffsetsOptional, DataType::DT_INT64);
|
||||
const_cast<aclTensor *>(actualChunkOffsets)->SetStorageFormat(Format::FORMAT_ND);
|
||||
const_cast<aclTensor *>(actualChunkOffsets)->SetViewFormat(Format::FORMAT_ND);
|
||||
const_cast<aclTensor *>(actualChunkOffsets)->SetOriginalFormat(Format::FORMAT_ND);
|
||||
} else {
|
||||
actualChunkOffsets = nullptr;
|
||||
}
|
||||
|
||||
auto ret = ADD_TO_LAUNCHER_LIST_AICORE(ChunkFwdO,
|
||||
OP_INPUT(q, k, v, h, g, actualCuSeqlens, actualChunkOffsets),
|
||||
OP_OUTPUT(oOut),
|
||||
OP_ATTR(scale, chunkSize));
|
||||
if (ret != ACLNN_SUCCESS) {
|
||||
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "ADD_TO_LAUNCHER_LIST_AICORE failed.");
|
||||
return {nullptr};
|
||||
}
|
||||
return {oOut};
|
||||
}
|
||||
|
||||
} // namespace l0op
|
||||
30
csrc/moe/chunk_fwd_o/op_host/op_api/chunk_fwd_o.h
Normal file
30
csrc/moe/chunk_fwd_o/op_host/op_api/chunk_fwd_o.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Tianjin University, 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.
|
||||
*/
|
||||
#ifndef OP_API_INC_LEVEL0_OP_CHUNK_FWD_O_H
|
||||
#define OP_API_INC_LEVEL0_OP_CHUNK_FWD_O_H
|
||||
|
||||
#include "opdev/op_executor.h"
|
||||
|
||||
namespace l0op {
|
||||
const std::array<const aclTensor *, 1> ChunkFwdO(
|
||||
const aclTensor *q,
|
||||
const aclTensor *k,
|
||||
const aclTensor *v,
|
||||
const aclTensor *h,
|
||||
const aclTensor *g,
|
||||
const aclIntArray *cuSeqlensOptional,
|
||||
const aclIntArray *chunkOffsetsOptional,
|
||||
double scale,
|
||||
int64_t chunkSize,
|
||||
const aclTensor *oOut,
|
||||
aclOpExecutor *executor);
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user