kv_cache.cpp 8.54 KB
Newer Older
PanZezhong's avatar
PanZezhong committed
1
2
3
#include "kv_cache.hpp"

#include "../utils.hpp"
4
#include "infinicore/ops.hpp"
PanZezhong's avatar
PanZezhong committed
5
6
7
#include <stdexcept>

namespace infinilm::cache {
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// ==========================
// StaticKVCacheConfig
// ==========================

StaticKVCacheConfig::StaticKVCacheConfig(
    infinicore::Size _max_batch_size,
    infinicore::Size _max_cache_len)
    : max_batch_size_(_max_batch_size),
      max_cache_len_(_max_cache_len) {
}

std::unique_ptr<CacheConfig>
StaticKVCacheConfig::unique_copy() const {
    return std::make_unique<StaticKVCacheConfig>(*this);
}

infinicore::Size
StaticKVCacheConfig::max_batch_size() const {
    return max_batch_size_;
}

infinicore::Size
StaticKVCacheConfig::max_cache_len() const {
    return max_cache_len_;
}
PanZezhong's avatar
PanZezhong committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

// ==========================
// StaticKVCache
// ==========================

StaticKVCache::StaticKVCache(
    infinicore::Size k_dim,
    infinicore::Size v_dim,
    infinicore::Size num_k_heads,
    infinicore::Size num_v_heads,
    infinicore::Size num_layers,
    infinicore::Size max_positional_embedding,
    infinicore::DataType dtype,
    const StaticKVCacheConfig &config,
    const engine::distributed::RankInfo &rank_info)
    : Cache(),
      k_dim_(k_dim),
      v_dim_(v_dim),
      num_rank_k_heads_(num_k_heads / rank_info.tp_size),
      num_rank_v_heads_(num_v_heads / rank_info.tp_size),
      rank_batch_size_(config.max_batch_size()),
54
      cache_len_(config.max_cache_len() == std::numeric_limits<infinicore::Size>::max() || config.max_cache_len() == 0 ? max_positional_embedding : config.max_cache_len()),
PanZezhong's avatar
PanZezhong committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
      rank_num_layers_(num_layers),
      dtype_(dtype) {

    // Allocate K cache
    k_caches_ = infinicore::Tensor::empty(
        {rank_num_layers_,
         rank_batch_size_,
         num_rank_k_heads_,
         cache_len_,
         k_dim_},
        dtype_,
        rank_info.device);

    // Allocate V cache
    v_caches_ = infinicore::Tensor::empty(
        {rank_num_layers_,
         rank_batch_size_,
         num_rank_v_heads_,
         cache_len_,
         v_dim_},
        dtype_,
        rank_info.device);
}

std::tuple<infinicore::Tensor, infinicore::Tensor>
StaticKVCache::update(size_t layer_idx,
                      const infinicore::Tensor &k,
                      const infinicore::Tensor &v,
83
                      const infinicore::Tensor &past_sequence_lengths) {
PanZezhong's avatar
PanZezhong committed
84
85
86
87
88
89
90
91
92
93
    ASSERT(layer_idx < rank_num_layers_);

    auto batch_size = k->size(0);
    auto update_len = k->size(2);

    ASSERT_EQ(batch_size, rank_batch_size_);

    auto k_cache_layer = k_caches_->narrow({{0, layer_idx, 1}})->squeeze(0);
    auto v_cache_layer = v_caches_->narrow({{0, layer_idx, 1}})->squeeze(0);

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    auto device = k_cache_layer->device();

    if (device.getType() == infinicore::Device::Type::NVIDIA
        || device.getType() == infinicore::Device::Type::ILUVATAR
        || device.getType() == infinicore::Device::Type::METAX
        || device.getType() == infinicore::Device::Type::MOORE
        || device.getType() == infinicore::Device::Type::CAMBRICON) {
        infinicore::op::kv_caching_(
            k_cache_layer,
            v_cache_layer,
            k,
            v,
            past_sequence_lengths);
    } else {
        size_t cache_pos = reinterpret_cast<int64_t *>(past_sequence_lengths->to(infinicore::Device::cpu())->data())[0];
        auto result_len = cache_pos + update_len;
        ASSERT(result_len <= cache_len_);

        auto k_cache_update = k_cache_layer->narrow({{2, cache_pos, update_len}});
        auto v_cache_update = v_cache_layer->narrow({{2, cache_pos, update_len}});

        k_cache_update->copy_from(k);
        v_cache_update->copy_from(v);
    }

    return {k_cache_layer, v_cache_layer};
PanZezhong's avatar
PanZezhong committed
120
121
122
}

// ==========================
123
// PagedKVCacheConfig
PanZezhong's avatar
PanZezhong committed
124
// ==========================
125
PagedKVCacheConfig::PagedKVCacheConfig(
126
    size_t num_blocks,
127
    size_t block_size)
128
    : num_blocks_(num_blocks),
129
      block_size_(block_size) {
PanZezhong's avatar
PanZezhong committed
130
131
132
}

std::unique_ptr<CacheConfig>
133
134
PagedKVCacheConfig::unique_copy() const {
    return std::make_unique<PagedKVCacheConfig>(*this);
PanZezhong's avatar
PanZezhong committed
135
136
}

137
size_t
138
139
PagedKVCacheConfig::num_blocks() const {
    return num_blocks_;
PanZezhong's avatar
PanZezhong committed
140
141
}

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
size_t
PagedKVCacheConfig::block_size() const {
    return block_size_;
}

// ==========================
// PagedKVCache
// ==========================
PagedKVCache::PagedKVCache(
    infinicore::Size k_dim,
    infinicore::Size v_dim,
    infinicore::Size num_k_heads,
    infinicore::Size num_v_heads,
    infinicore::Size num_layers,
    infinicore::DataType dtype,
    const PagedKVCacheConfig &config,
    const engine::distributed::RankInfo &rank_info)
    : Cache(),
      k_dim_(k_dim),
      v_dim_(v_dim),
      num_rank_k_heads_(num_k_heads / rank_info.tp_size),
      num_rank_v_heads_(num_v_heads / rank_info.tp_size),
      rank_num_layers_(num_layers),
      dtype_(dtype),
166
      num_blocks_per_layer_(config.num_blocks()),
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
      block_size_(config.block_size()) {
    // [num_layers, num_blocks, num_rank_k_heads, block_size, k_dim]
    k_caches_ = infinicore::Tensor::empty(
        {rank_num_layers_,
         num_blocks_per_layer_,
         num_rank_k_heads_,
         block_size_,
         k_dim_},
        dtype_,
        rank_info.device);

    // [num_layers, num_blocks, num_rank_v_heads, block_size, v_dim]
    v_caches_ = infinicore::Tensor::empty(
        {rank_num_layers_,
         num_blocks_per_layer_,
         num_rank_v_heads_,
         block_size_,
         v_dim_},
        dtype_,
        rank_info.device);
PanZezhong's avatar
PanZezhong committed
187
188
}

189
190
191
192
193
194
std::tuple<infinicore::Tensor, infinicore::Tensor> PagedKVCache::update(
    size_t layer_idx,
    const infinicore::Tensor &k,
    const infinicore::Tensor &v,
    const infinicore::Tensor &slot_mapping) {

195
    auto &&[k_cache_layer, v_cache_layer] = get_paged_kv(layer_idx);
196

197
198
199
200
201
202
    infinicore::op::paged_caching_(
        k_cache_layer,
        v_cache_layer,
        k,
        v,
        slot_mapping);
203
204
    return {k_cache_layer, v_cache_layer};
}
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269

std::tuple<infinicore::Tensor, infinicore::Tensor>
PagedKVCache::get_paged_kv(size_t layer_idx) {
    auto k_cache_layer = k_caches_->narrow({{0, layer_idx, 1}})->squeeze(0);
    auto v_cache_layer = v_caches_->narrow({{0, layer_idx, 1}})->squeeze(0);
    return {k_cache_layer, v_cache_layer};
}

std::tuple<infinicore::Tensor, infinicore::Tensor>
PagedKVCache::get_contiguous_kv(
    size_t layer_idx,
    const infinicore::Tensor block_tables,
    const infinicore::Tensor cache_lens,
    const infinicore::Tensor input_offsets,
    size_t request_id) {
    ASSERT_EQ(block_tables->dtype(), infinicore::DataType::I64);
    ASSERT_EQ(cache_lens->dtype(), infinicore::DataType::I64);
    ASSERT_EQ(input_offsets->dtype(), infinicore::DataType::I64);

    auto nreq = block_tables->size(0);
    auto block_tables_cpu = block_tables->to(infinicore::Device::cpu());
    auto cache_lens_cpu = cache_lens->to(infinicore::Device::cpu());
    auto input_offsets_cpu = input_offsets->to(infinicore::Device::cpu());
    infinicore::context::syncDevice();

    // [num_blocks, num_rank_v_heads, block_size, v_dim]
    auto &&[k_cache_layer, v_cache_layer] = get_paged_kv(layer_idx);

    auto req = request_id;
    auto cache_lens_ptr = reinterpret_cast<const int64_t *>(cache_lens_cpu->data());
    auto input_offsets_ptr = reinterpret_cast<const int64_t *>(input_offsets_cpu->data());
    int64_t total_len = cache_lens_ptr[req] + (input_offsets_ptr[req + 1] - input_offsets_ptr[req]);

    auto full_k = infinicore::Tensor::empty(
        {num_rank_k_heads_, (size_t)total_len, k_dim_},
        k_cache_layer->dtype(), k_cache_layer->device());

    auto full_v = infinicore::Tensor::empty(
        {num_rank_v_heads_, (size_t)total_len, v_dim_},
        v_cache_layer->dtype(), v_cache_layer->device());

    size_t nblocks = total_len / block_size_;
    size_t r = total_len % block_size_;

    for (size_t b = 0; b < nblocks; b++) {
        size_t bid = *((int64_t *)(block_tables_cpu->narrow({{0, req, 1}, {1, b, 1}})->data()));

        full_k->narrow({{1, b * block_size_, block_size_}})
            ->copy_from(k_cache_layer->narrow({{0, bid, 1}})->squeeze(0));
        full_v->narrow({{1, b * block_size_, block_size_}})
            ->copy_from(v_cache_layer->narrow({{0, bid, 1}})->squeeze(0));
    }

    if (r > 0) {
        size_t bid = *((int64_t *)(block_tables_cpu->narrow({{0, req, 1}, {1, nblocks, 1}})->data()));

        full_k->narrow({{1, nblocks * block_size_, r}})
            ->copy_from(k_cache_layer->narrow({{0, bid, 1}})->squeeze(0)->narrow({{1, 0, r}}));
        full_v->narrow({{1, nblocks * block_size_, r}})
            ->copy_from(v_cache_layer->narrow({{0, bid, 1}})->squeeze(0)->narrow({{1, 0, r}}));
    }

    return {full_k, full_v};
}

PanZezhong's avatar
PanZezhong committed
270
} // namespace infinilm::cache