Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions xllm-kernel/csrc/ascendc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ if("ascend910b" IN_LIST ASCEND_COMPUTE_UNIT OR "ascend910_93" IN_LIST ASCEND_COM
"${mla_host}/mla_preprocess_v2_tiling.cpp"
)
endif()
if("ascend950" IN_LIST ASCEND_COMPUTE_UNIT)
set(reshape_a5_host "${CMAKE_CURRENT_SOURCE_DIR}/reshape_and_cache_a5/op_host")
list(APPEND op_definitions "${reshape_a5_host}/reshape_and_cache_a5_def.cpp")
set(reshape_a5_api "${ASCEND_AUTOGEN_PATH}/aclnn_xllm_kernel_reshape_and_cache_a5.cpp")
set(reshape_a5_tiling
"${reshape_a5_host}/reshape_and_cache_a5_proto.cpp"
"${reshape_a5_host}/reshape_and_cache_a5_tiling.cpp"
)
endif()
npu_op_code_gen(
SRC ${op_definitions}
PACKAGE ${vendor}
Expand Down Expand Up @@ -168,6 +177,7 @@ npu_op_library(cust_opapi ACLNN
"${scatter}/op_host/op_api/aclnn_scatter_nd_update_v2.cpp"
"${scatter}/op_host/op_api/scatter_nd_update_v2.cpp"
${sparse_attention_api}
${reshape_a5_api}
"${sas_metadata}/op_api/aclnn_sparse_attn_sharedkv_metadata.cpp"
"${sas_metadata}/op_api/l0_sparse_attn_sharedkv_metadata.cpp"
${a2_aclnn_sources}
Expand Down Expand Up @@ -234,6 +244,7 @@ npu_op_library(cust_optiling TILING
"${scatter}/op_host/scatter_nd_update_infershape.cpp"
"${scatter}/op_host/scatter_nd_update_v2_tiling.cpp"
${sparse_attention_tiling}
${reshape_a5_tiling}
"${CMAKE_CURRENT_SOURCE_DIR}/compressor/op_host/compressor_proto.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/sparse_attn_sharedkv/op_host/sparse_attn_sharedkv_proto.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/sparse_flash_attention_lse/op_host/sparse_flash_attention_lse_infershape.cpp"
Expand Down Expand Up @@ -402,6 +413,13 @@ foreach(op_type XllmKernelSparseAttnSharedkv XllmKernelSparseFlashAttentionLse)
"-I${CMAKE_CURRENT_SOURCE_DIR}")
endforeach()

if(reshape_a5_api)
npu_op_kernel_sources(ascendc_kernels OP_TYPE XllmKernelReshapeAndCacheA5
KERNEL_DIR reshape_and_cache_a5/op_kernel KERNEL_FILE reshape_and_cache_a5.cpp)
npu_op_kernel_options(ascendc_kernels XllmKernelReshapeAndCacheA5
OPTIONS --cce-auto-sync=off -Wno-deprecated-declarations -Werror)
endif()

npu_op_kernel_library(ascendc_kernels SRC_BASE "${CMAKE_CURRENT_SOURCE_DIR}" TILING_LIBRARY cust_optiling)

include("${CMAKE_CURRENT_SOURCE_DIR}/aicpu.cmake")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* Copyright 2026 The xLLM Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://gitcode.com/xLLM-AI/xllm_ops/blob/main/LICENSE
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "register/op_def_registry.h"

namespace ops {
class XllmKernelReshapeAndCacheA5 : public OpDef {
public:
explicit XllmKernelReshapeAndCacheA5(const char* name) : OpDef(name) {
const std::initializer_list<ge::DataType> cache_types = {ge::DT_FLOAT16,
ge::DT_BF16};
const std::initializer_list<ge::Format> cache_formats = {ge::FORMAT_ND,
ge::FORMAT_ND};

this->Input("key")
.ParamType(REQUIRED)
.DataType(cache_types)
.Format(cache_formats)
.UnknownShapeFormat(cache_formats)
.AutoContiguous();
this->Input("value")
.ParamType(REQUIRED)
.DataType(cache_types)
.Format(cache_formats)
.UnknownShapeFormat(cache_formats)
.AutoContiguous();
this->Input("keyCache")
.ParamType(REQUIRED)
.DataType(cache_types)
.Format(cache_formats)
.UnknownShapeFormat(cache_formats);
this->Input("valueCache")
.ParamType(REQUIRED)
.DataType(cache_types)
.Format(cache_formats)
.UnknownShapeFormat(cache_formats);
this->Input("slotMapping")
.ParamType(REQUIRED)
.DataType({ge::DT_INT32, ge::DT_INT32})
.Format(cache_formats)
.UnknownShapeFormat(cache_formats)
.AutoContiguous();
this->Output("keyCacheOut")
.ParamType(REQUIRED)
.DataType(cache_types)
.Format(cache_formats)
.UnknownShapeFormat(cache_formats);
this->Output("valueCacheOut")
.ParamType(REQUIRED)
.DataType(cache_types)
.Format(cache_formats)
.UnknownShapeFormat(cache_formats);

OpAICoreConfig a5_config;
a5_config.DynamicCompileStaticFlag(true)
.DynamicRankSupportFlag(true)
.DynamicShapeSupportFlag(true)
.ExtendCfgInfo("opFile.value", "reshape_and_cache_a5")
.ExtendCfgInfo("opInterface.value", "reshape_and_cache_a5");
this->AICore().AddConfig("ascend950", a5_config);
}
};

OP_ADD(XllmKernelReshapeAndCacheA5);
} // namespace ops
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright 2026 The xLLM Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://gitcode.com/xLLM-AI/xllm_ops/blob/main/LICENSE
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "register/op_def_registry.h"

namespace ge {
static graphStatus InferShape(gert::InferShapeContext* context) {
Comment thread
XuZhang99 marked this conversation as resolved.
Resolved
*context->GetOutputShape(0) = *context->GetInputShape(2);
*context->GetOutputShape(1) = *context->GetInputShape(3);
Comment thread
XuZhang99 marked this conversation as resolved.
Resolved
return GRAPH_SUCCESS;
}

static graphStatus InferDataType(gert::InferDataTypeContext* context) {
context->SetOutputDataType(0, context->GetInputDataType(2));
context->SetOutputDataType(1, context->GetInputDataType(3));
return GRAPH_SUCCESS;
}

IMPL_OP(XllmKernelReshapeAndCacheA5)
.InferShape(InferShape)
.InferDataType(InferDataType);
} // namespace ge
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/* Copyright 2026 The xLLM Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://gitcode.com/xLLM-AI/xllm_ops/blob/main/LICENSE
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "reshape_and_cache_a5_tiling.h"
Comment thread
XuZhang99 marked this conversation as resolved.
Resolved

#include <algorithm>
#include <cstdint>

#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"

namespace optiling {
namespace {
constexpr uint32_t kBlockBytes = 32;
constexpr uint32_t kReservedUbBytes = 8 * 1024;
constexpr int32_t kKeyInput = 0;
constexpr int32_t kValueInput = 1;
constexpr int32_t kKeyCacheInput = 2;
constexpr int32_t kValueCacheInput = 3;
constexpr int32_t kSlotMappingInput = 4;

bool ShapesMatch(const gert::Shape& lhs, const gert::Shape& rhs) {
Comment thread
XuZhang99 marked this conversation as resolved.
Resolved
if (lhs.GetDimNum() != rhs.GetDimNum()) {
return false;
}
for (size_t dim = 0; dim < lhs.GetDimNum(); ++dim) {
if (lhs.GetDim(dim) != rhs.GetDim(dim)) {
return false;
}
}
return true;
}

ge::graphStatus TilingFunc(gert::TilingContext* context) {
if (context == nullptr || context->GetInputShape(kKeyInput) == nullptr ||
context->GetInputShape(kValueInput) == nullptr ||
context->GetInputShape(kKeyCacheInput) == nullptr ||
context->GetInputShape(kValueCacheInput) == nullptr ||
context->GetInputShape(kSlotMappingInput) == nullptr) {
return ge::GRAPH_FAILED;
}

const gert::Shape key_shape =
context->GetInputShape(kKeyInput)->GetStorageShape();
const gert::Shape value_shape =
context->GetInputShape(kValueInput)->GetStorageShape();
const gert::Shape cache_shape =
context->GetInputShape(kKeyCacheInput)->GetStorageShape();
const gert::Shape value_cache_shape =
context->GetInputShape(kValueCacheInput)->GetStorageShape();
const gert::Shape slot_shape =
context->GetInputShape(kSlotMappingInput)->GetStorageShape();
if (key_shape.GetDimNum() != 3 || cache_shape.GetDimNum() != 4 ||
slot_shape.GetDimNum() != 1 || !ShapesMatch(key_shape, value_shape) ||
!ShapesMatch(cache_shape, value_cache_shape) ||
key_shape.GetDim(0) != slot_shape.GetDim(0) ||
key_shape.GetDim(1) != cache_shape.GetDim(2) ||
key_shape.GetDim(2) != cache_shape.GetDim(3) ||
key_shape.GetDim(1) <= 0 || key_shape.GetDim(2) <= 0 ||
cache_shape.GetDim(0) <= 0 || cache_shape.GetDim(1) <= 0) {
return ge::GRAPH_FAILED;
}

const uint32_t num_tokens = static_cast<uint32_t>(key_shape.GetDim(0));
const uint32_t row_elements =
static_cast<uint32_t>(key_shape.GetDim(1) * key_shape.GetDim(2));
const uint32_t total_slots =
static_cast<uint32_t>(cache_shape.GetDim(0) * cache_shape.GetDim(1));

platform_ascendc::PlatformAscendC platform(context->GetPlatformInfo());
uint64_t ub_bytes = 0;
platform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ub_bytes);
const uint32_t dtype_bytes = static_cast<uint32_t>(
ge::GetSizeByDataType(context->GetInputDesc(kKeyInput)->GetDataType()));
const uint64_t usable_ub_bytes =
ub_bytes > kReservedUbBytes ? ub_bytes - kReservedUbBytes : ub_bytes;
if (dtype_bytes == 0 || usable_ub_bytes < kBlockBytes) {
return ge::GRAPH_FAILED;
}
uint32_t tile_elements = static_cast<uint32_t>(usable_ub_bytes / dtype_bytes);
const uint32_t elements_per_block = kBlockBytes / dtype_bytes;
tile_elements =
std::max(elements_per_block,
tile_elements / elements_per_block * elements_per_block);
tile_elements = std::min(row_elements, tile_elements);

ReshapeAndCacheA5TilingData tiling;
tiling.set_num_tokens(num_tokens);
tiling.set_row_elements(row_elements);
tiling.set_total_slots(total_slots);
tiling.set_tile_elements(tile_elements);

const uint32_t aiv_num = platform.GetCoreNumAiv();
context->SetBlockDim(std::max(1U, std::min(num_tokens, aiv_num)));
tiling.SaveToBuffer(context->GetRawTilingData()->GetData(),
context->GetRawTilingData()->GetCapacity());
context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
context->GetWorkspaceSizes(1)[0] = platform.GetLibApiWorkSpaceSize();
return ge::GRAPH_SUCCESS;
}
} // namespace

IMPL_OP_OPTILING(XllmKernelReshapeAndCacheA5).Tiling(TilingFunc);
} // namespace optiling
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright 2026 The xLLM Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://gitcode.com/xLLM-AI/xllm_ops/blob/main/LICENSE
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#pragma once

#include "register/tilingdata_base.h"

namespace optiling {
BEGIN_TILING_DATA_DEF(ReshapeAndCacheA5TilingData)
TILING_DATA_FIELD_DEF(uint32_t, num_tokens);
TILING_DATA_FIELD_DEF(uint32_t, row_elements);
TILING_DATA_FIELD_DEF(uint32_t, total_slots);
TILING_DATA_FIELD_DEF(uint32_t, tile_elements);
END_TILING_DATA_DEF;

REGISTER_TILING_DATA_CLASS(XllmKernelReshapeAndCacheA5,
ReshapeAndCacheA5TilingData)
} // namespace optiling
Loading
Loading