1+ /* Copyright 2025-2026 The xLLM Authors.
2+
3+ Licensed under the Apache License, Version 2.0 (the "License");
4+ you may not use this file except in compliance with the License.
5+ You may obtain a copy of the License at
6+
7+ https://github.com/jd-opensource/xllm/blob/main/LICENSE
8+
9+ Unless required by applicable law or agreed to in writing, software
10+ distributed under the License is distributed on an "AS IS" BASIS,
11+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ See the License for the specific language governing permissions and
13+ limitations under the License.
14+ ==============================================================================*/
15+
16+ // Build xlite XModelAttnMeta from xllm ModelInputParams.
17+
18+ #pragma once
19+
20+ #include < xlite/xlite.h>
21+
22+ #include < algorithm>
23+ #include < cstdint>
24+ #include < vector>
25+
26+ #include " core/framework/model/model_input_params.h"
27+ #include " core/layers/xlite/xlite_init_utils.h"
28+
29+ namespace xllm ::xlite {
30+
31+ class XliteAttnMetaBuilder {
32+ public:
33+ static void Build (const ModelInputParams& params,
34+ const torch::Tensor& positions,
35+ uint32_t block_size,
36+ XModelAttnMeta& m,
37+ int64_t pad_count = 0 ) {
38+ // version=0: xlite recomputes position from cachedLens (framework's
39+ // positions tensor is off-by-one on decode).
40+ m.version = 0 ;
41+ m.lens .clear ();
42+ m.cachedLens .clear ();
43+ m.blockTables .clear ();
44+
45+ const auto & host = params.attention .host ;
46+ int n = params.meta .num_sequences ;
47+ uint32_t bs = block_size;
48+
49+ // block_tables may be undefined in edge cases (DP empty shard).
50+ const bool has_real_seqs =
51+ n > 0 && host.block_tables .defined () && host.block_tables .dim () >= 2 ;
52+ if (has_real_seqs) {
53+ auto block_acc = host.block_tables .accessor <int32_t , 2 >();
54+ for (int s = 0 ; s < n; ++s) {
55+ int32_t q_len = host.q_seq_lens [s];
56+ int32_t kv_len = host.kv_seq_lens [s];
57+ m.lens .push_back (static_cast <uint32_t >(q_len));
58+ m.cachedLens .push_back (
59+ static_cast <uint32_t >(std::max (0 , kv_len - q_len))); // clamp >= 0
60+ int32_t nblocks =
61+ (kv_len + static_cast <int32_t >(bs) - 1 ) / static_cast <int32_t >(bs);
62+ std::vector<uint32_t > row (nblocks);
63+ for (int32_t b = 0 ; b < nblocks; ++b) {
64+ row[b] = static_cast <uint32_t >(block_acc[s][b]);
65+ }
66+ m.blockTables .push_back (std::move (row));
67+ }
68+ }
69+
70+ // DP padding: append dummy seq so sum(lens) aligns across DP groups.
71+ if (pad_count > 0 ) {
72+ m.lens .push_back (static_cast <uint32_t >(pad_count));
73+ m.cachedLens .push_back (0 );
74+ int32_t nblocks =
75+ (static_cast <int32_t >(pad_count) + static_cast <int32_t >(bs) - 1 ) /
76+ static_cast <int32_t >(bs);
77+ std::vector<uint32_t > row (nblocks, 0 );
78+ m.blockTables .push_back (std::move (row));
79+ }
80+ InitXTensor (m.vllmPosition , positions);
81+ }
82+ };
83+
84+ } // namespace xllm::xlite
0 commit comments