|
| 1 | +/* Copyright 2026 The xLLM Authors. All Rights Reserved. |
| 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 | +#include "framework/kv_cache/indexed_kv_cache_impl.h" |
| 17 | + |
| 18 | +#include "util/tensor_helper.h" |
| 19 | + |
| 20 | +namespace xllm { |
| 21 | + |
| 22 | +IndexedKVCacheImpl::IndexedKVCacheImpl(const IndexedKVCacheTensors& tensors) |
| 23 | + : KVCacheImpl(tensors.kv_cache_tensors), |
| 24 | + index_cache_(tensors.index_cache) {} |
| 25 | + |
| 26 | +IndexedKVCacheImpl::IndexedKVCacheImpl( |
| 27 | + const std::vector<std::vector<int64_t>>& kv_cache_shape, |
| 28 | + const KVCacheCreateOptions& create_options) |
| 29 | + : IndexedKVCacheImpl( |
| 30 | + create_indexed_kv_cache_tensors(kv_cache_shape, create_options)) {} |
| 31 | + |
| 32 | +torch::Tensor IndexedKVCacheImpl::get_index_cache() const { |
| 33 | + return index_cache_; |
| 34 | +} |
| 35 | + |
| 36 | +bool IndexedKVCacheImpl::empty() const { |
| 37 | + return !key_cache_.defined() || !value_cache_.defined() || |
| 38 | + !index_cache_.defined(); |
| 39 | +} |
| 40 | + |
| 41 | +std::vector<std::vector<int64_t>> IndexedKVCacheImpl::get_shapes() const { |
| 42 | + std::vector<std::vector<int64_t>> tensor_shapes(3); |
| 43 | + tensor_shapes[0] = get_tensor_shape(key_cache_); |
| 44 | + tensor_shapes[1] = get_tensor_shape(value_cache_); |
| 45 | + tensor_shapes[2] = get_tensor_shape(index_cache_); |
| 46 | + return tensor_shapes; |
| 47 | +} |
| 48 | + |
| 49 | +} // namespace xllm |
0 commit comments