Repository navigation
refactor: package Ascend950 paged KV writes. #2805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
XuZhang99
merged 4 commits into
xLLM-AI:main
from
XuZhang99:refactor/kernel-reshape-and-cache-a5
Nov 21, 2026
+536
−22
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
47ac03e
refactor: package Ascend950 paged KV writes.
XuZhang99 c197730
refactor: register paged KV backend directly.
XuZhang99 4fd8ead
docs: remove Ascend950 cache source note.
XuZhang99 386385a
refactor: rename Ascend950 cache device entry.
XuZhang99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
xllm-kernel/csrc/ascendc/reshape_and_cache_a5/op_host/reshape_and_cache_a5_def.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
34 changes: 34 additions & 0 deletions
34
xllm-kernel/csrc/ascendc/reshape_and_cache_a5/op_host/reshape_and_cache_a5_proto.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| *context->GetOutputShape(0) = *context->GetInputShape(2); | ||
| *context->GetOutputShape(1) = *context->GetInputShape(3); | ||
|
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 | ||
116 changes: 116 additions & 0 deletions
116
xllm-kernel/csrc/ascendc/reshape_and_cache_a5/op_host/reshape_and_cache_a5_tiling.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
|
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) { | ||
|
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 | ||
30 changes: 30 additions & 0 deletions
30
xllm-kernel/csrc/ascendc/reshape_and_cache_a5/op_host/reshape_and_cache_a5_tiling.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.