Skip to content

Commit 796af66

Browse files
author
zhuyue
committed
fix: support ascend paged graph runtime
Synchronize RoPE cache copies before the cache is consumed by Ascend kernels. Rename paged attention block_idx locals to page_block_idx to avoid AscendC kernel symbol conflicts while preserving the same block table address calculation. Detect common Ascend driver library layouts before linking libascend_hal.so instead of relying on one hard-coded driver path.
1 parent 4e5b2e6 commit 796af66

4 files changed

Lines changed: 27 additions & 9 deletions

File tree

src/infinicore/nn/rope.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "infinicore/nn/rope.hpp"
22
#include "../../utils.h"
33
#include "../utils.hpp"
4+
#include "infinicore/context/context.hpp"
45
#include "infinicore/ops.hpp"
56
#include <algorithm>
67
#include <cassert>
@@ -11,6 +12,13 @@
1112
#include <vector>
1213

1314
namespace infinicore::nn {
15+
namespace {
16+
17+
void sync_cache_copy() {
18+
infinicore::context::syncStream();
19+
}
20+
21+
} // namespace
1422

1523
RoPE::RoPE(size_t head_dim,
1624
size_t rotary_dim,
@@ -82,6 +90,7 @@ void RoPE::initialize_cache() {
8290
auto cos_f32_cpu = Tensor::from_blob(cos_data.data(), {max_seq_len_, cache_dim}, DataType::F32, cpu_device);
8391
sin_cache_->copy_from(sin_f32_cpu);
8492
cos_cache_->copy_from(cos_f32_cpu);
93+
sync_cache_copy();
8594
} else if (dtype_ == DataType::BF16) {
8695
// Convert F32 to BF16 using the same conversion as Python's ml_dtypes.bfloat16
8796
// This uses round-to-nearest-even (matching _f32_to_bf16 implementation)
@@ -99,6 +108,7 @@ void RoPE::initialize_cache() {
99108
// copy_from handles cross-device copying to target device
100109
sin_cache_->copy_from(sin_bf16_cpu);
101110
cos_cache_->copy_from(cos_bf16_cpu);
111+
sync_cache_copy();
102112
} else if (dtype_ == DataType::F16) {
103113
// Convert F32 to F16
104114
std::vector<fp16_t> sin_f16_data(max_seq_len_ * cache_dim);
@@ -114,6 +124,7 @@ void RoPE::initialize_cache() {
114124

115125
sin_cache_->copy_from(sin_f16_cpu);
116126
cos_cache_->copy_from(cos_f16_cpu);
127+
sync_cache_copy();
117128
} else {
118129
throw std::runtime_error(
119130
"RoPE cache dtype conversion not yet supported for dtype: "

src/infiniop/ops/paged_attention/ascend/paged_attention_ascend_kernel.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,22 @@ class PagedAttentionKernel {
164164

165165
private:
166166
__aicore__ inline ptrdiff_t keyBase(size_t seq_idx, size_t kv_head_idx, size_t token_idx) {
167-
const size_t block_idx = token_idx / _page_block_size;
167+
const size_t page_block_idx = token_idx / _page_block_size;
168168
const size_t token_offset = token_idx % _page_block_size;
169169
const int64_t physical_block = loadIndex(
170170
_block_tables_gm,
171-
static_cast<ptrdiff_t>(seq_idx) * _block_table_batch_stride + static_cast<ptrdiff_t>(block_idx));
171+
static_cast<ptrdiff_t>(seq_idx) * _block_table_batch_stride + static_cast<ptrdiff_t>(page_block_idx));
172172
return static_cast<ptrdiff_t>(physical_block) * _k_batch_stride
173173
+ static_cast<ptrdiff_t>(kv_head_idx) * _k_head_stride
174174
+ static_cast<ptrdiff_t>(token_offset) * _k_row_stride;
175175
}
176176

177177
__aicore__ inline ptrdiff_t valueBase(size_t seq_idx, size_t kv_head_idx, size_t token_idx) {
178-
const size_t block_idx = token_idx / _page_block_size;
178+
const size_t page_block_idx = token_idx / _page_block_size;
179179
const size_t token_offset = token_idx % _page_block_size;
180180
const int64_t physical_block = loadIndex(
181181
_block_tables_gm,
182-
static_cast<ptrdiff_t>(seq_idx) * _block_table_batch_stride + static_cast<ptrdiff_t>(block_idx));
182+
static_cast<ptrdiff_t>(seq_idx) * _block_table_batch_stride + static_cast<ptrdiff_t>(page_block_idx));
183183
return static_cast<ptrdiff_t>(physical_block) * _v_batch_stride
184184
+ static_cast<ptrdiff_t>(kv_head_idx) * _v_head_stride
185185
+ static_cast<ptrdiff_t>(token_offset) * _v_row_stride;

src/infiniop/ops/paged_attention_prefill/ascend/paged_attention_prefill_ascend_kernel.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,22 @@ class PagedAttentionPrefillKernel {
197197
}
198198

199199
__aicore__ inline ptrdiff_t keyBase(size_t seq_idx, size_t kv_head_idx, size_t token_idx) {
200-
const size_t block_idx = token_idx / _page_block_size;
200+
const size_t page_block_idx = token_idx / _page_block_size;
201201
const size_t token_offset = token_idx % _page_block_size;
202202
const int64_t physical_block = loadPrefillIndex(
203203
_block_tables_gm,
204-
static_cast<ptrdiff_t>(seq_idx) * _block_table_batch_stride + static_cast<ptrdiff_t>(block_idx));
204+
static_cast<ptrdiff_t>(seq_idx) * _block_table_batch_stride + static_cast<ptrdiff_t>(page_block_idx));
205205
return static_cast<ptrdiff_t>(physical_block) * _k_batch_stride
206206
+ static_cast<ptrdiff_t>(kv_head_idx) * _k_head_stride
207207
+ static_cast<ptrdiff_t>(token_offset) * _k_row_stride;
208208
}
209209

210210
__aicore__ inline ptrdiff_t valueBase(size_t seq_idx, size_t kv_head_idx, size_t token_idx) {
211-
const size_t block_idx = token_idx / _page_block_size;
211+
const size_t page_block_idx = token_idx / _page_block_size;
212212
const size_t token_offset = token_idx % _page_block_size;
213213
const int64_t physical_block = loadPrefillIndex(
214214
_block_tables_gm,
215-
static_cast<ptrdiff_t>(seq_idx) * _block_table_batch_stride + static_cast<ptrdiff_t>(block_idx));
215+
static_cast<ptrdiff_t>(seq_idx) * _block_table_batch_stride + static_cast<ptrdiff_t>(page_block_idx));
216216
return static_cast<ptrdiff_t>(physical_block) * _v_batch_stride
217217
+ static_cast<ptrdiff_t>(kv_head_idx) * _v_head_stride
218218
+ static_cast<ptrdiff_t>(token_offset) * _v_row_stride;

xmake/ascend.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ add_links("libascendcl.so")
1010
add_links("libnnopbase.so")
1111
add_links("libopapi.so")
1212
add_links("libruntime.so")
13-
add_linkdirs(ASCEND_HOME .. "/../../driver/lib64/driver")
13+
for _, driver_dir in ipairs({
14+
path.join(ASCEND_HOME, "../driver/lib64/driver"),
15+
path.join(ASCEND_HOME, "../../driver/lib64/driver"),
16+
}) do
17+
if os.isdir(driver_dir) then
18+
add_linkdirs(driver_dir)
19+
end
20+
end
1421
add_links("libascend_hal.so")
1522
local builddir = string.format(
1623
"%s/build/%s/%s/%s",

0 commit comments

Comments
 (0)