1#ifndef INFINI_OPS_BASE_RESHAPE_AND_CACHE_H_
2#define INFINI_OPS_BASE_RESHAPE_AND_CACHE_H_
16 const std::string kv_cache_dtype)
41 x_{key_cache.size(4)},
43 assert(key.ndim() == 3 && value.ndim() == 3 &&
44 "`ReshapeAndCache` requires 3D `key` and `value`");
45 assert(key.shape() == value.shape() &&
46 "`ReshapeAndCache` requires `key` and `value` to have the same "
48 assert(key.dtype() == value.dtype() &&
49 "`ReshapeAndCache` requires `key` and `value` to have the same "
51 assert((
key_type_ == DataType::kFloat16 ||
54 "`ReshapeAndCache` supports float16, bfloat16, and float32 inputs");
55 assert(key_cache.ndim() == 5 && value_cache.ndim() == 4 &&
56 "`ReshapeAndCache` requires 5D `key_cache` and 4D `value_cache`");
57 assert(slot_mapping.ndim() == 1 &&
58 slot_mapping.dtype() == DataType::kInt64 &&
59 slot_mapping.stride(0) == 1 &&
60 "`ReshapeAndCache` requires contiguous int64 `slot_mapping`");
62 "`ReshapeAndCache` requires enough key/value rows for all slots");
64 "`ReshapeAndCache` requires `head_size` divisible by cache vector "
66 assert(key_cache.size(0) == value_cache.size(0) &&
72 "`ReshapeAndCache` cache shapes do not match key/value geometry");
73 assert(key.stride(2) == 1 && key.stride(1) ==
head_size_ &&
74 value.stride(2) == 1 && value.stride(1) ==
head_size_ &&
75 "`ReshapeAndCache` requires contiguous head dimensions");
76 assert(key_cache.stride(4) == 1 && key_cache.stride(3) ==
x_ &&
80 "`ReshapeAndCache` requires contiguous vLLM key-cache layout");
81 assert(value_cache.stride(3) == 1 && value_cache.stride(2) ==
block_size_ &&
84 "`ReshapeAndCache` requires contiguous vLLM value-cache layout");
87 "`ReshapeAndCache` received unsupported `kv_cache_dtype`");
93 "`ReshapeAndCache` cache storage dtype does not match "
95 assert(k_scale.numel() == 1 && v_scale.numel() == 1 &&
98 "`ReshapeAndCache` requires scalar float32 scales");
105 const std::string kv_cache_dtype)
const = 0;
Definition generated/include/operator.h:282
Definition reshape_and_cache.h:11
Tensor::Shape value_shape_
Definition reshape_and_cache.h:110
Tensor::Size x_
Definition reshape_and_cache.h:156
Tensor::Strides key_cache_strides_
Definition reshape_and_cache.h:126
Tensor::Size head_size_
Definition reshape_and_cache.h:152
Tensor::Shape key_cache_shape_
Definition reshape_and_cache.h:112
DataType value_cache_type_
Definition reshape_and_cache.h:140
DataType k_scale_type_
Definition reshape_and_cache.h:142
Tensor::Shape key_shape_
Definition reshape_and_cache.h:108
virtual void operator()(const Tensor key, const Tensor value, Tensor key_cache, Tensor value_cache, const Tensor slot_mapping, const Tensor k_scale, const Tensor v_scale, const std::string kv_cache_dtype) const =0
std::string kv_cache_dtype_
Definition reshape_and_cache.h:146
Tensor::Shape v_scale_shape_
Definition reshape_and_cache.h:120
Tensor::Size num_heads_
Definition reshape_and_cache.h:150
int device_index_
Definition reshape_and_cache.h:158
DataType v_scale_type_
Definition reshape_and_cache.h:144
Tensor::Strides k_scale_strides_
Definition reshape_and_cache.h:132
Tensor::Shape slot_mapping_shape_
Definition reshape_and_cache.h:116
Tensor::Strides slot_mapping_strides_
Definition reshape_and_cache.h:130
Tensor::Strides value_strides_
Definition reshape_and_cache.h:124
Tensor::Strides value_cache_strides_
Definition reshape_and_cache.h:128
Tensor::Size block_size_
Definition reshape_and_cache.h:154
Tensor::Shape k_scale_shape_
Definition reshape_and_cache.h:118
Tensor::Strides key_strides_
Definition reshape_and_cache.h:122
DataType key_type_
Definition reshape_and_cache.h:136
Tensor::Shape value_cache_shape_
Definition reshape_and_cache.h:114
ReshapeAndCache(const Tensor key, const Tensor value, Tensor key_cache, Tensor value_cache, const Tensor slot_mapping, const Tensor k_scale, const Tensor v_scale, const std::string kv_cache_dtype)
Definition reshape_and_cache.h:13
Tensor::Strides v_scale_strides_
Definition reshape_and_cache.h:134
Tensor::Size num_tokens_
Definition reshape_and_cache.h:148
DataType key_cache_type_
Definition reshape_and_cache.h:138
Definition generated/include/operator.h:28
infini::rt::TensorView Tensor
Definition tensor.h:8