|
| 1 | +#ifndef KV_CACHING_H |
| 2 | +#define KV_CACHING_H |
| 3 | + |
| 4 | +#include "../../../handle.h" |
| 5 | +#include "../../../operator.h" |
| 6 | +#include "../../../tensor.h" |
| 7 | + |
| 8 | +#include "../../../../../build/ninetoothed/kv_caching.h" |
| 9 | +#include "../../../ninetoothed/utils.h" |
| 10 | + |
| 11 | +namespace op::kv_caching::ninetoothed { |
| 12 | +class Descriptor final : public InfiniopDescriptor { |
| 13 | + |
| 14 | +public: |
| 15 | + Descriptor( |
| 16 | + infiniopHandle_t handle, |
| 17 | + infiniopTensorDescriptor_t k_cache_desc, |
| 18 | + infiniopTensorDescriptor_t v_cache_desc, |
| 19 | + infiniopTensorDescriptor_t k_desc, |
| 20 | + infiniopTensorDescriptor_t v_desc, |
| 21 | + infiniopTensorDescriptor_t past_kv_lengths_desc) : InfiniopDescriptor{handle->device, handle->device_id}, |
| 22 | + k_cache_shape_{k_cache_desc->shape()}, |
| 23 | + k_cache_strides_{k_cache_desc->strides()}, |
| 24 | + v_cache_shape_{v_cache_desc->shape()}, |
| 25 | + v_cache_strides_{v_cache_desc->strides()}, |
| 26 | + k_shape_{k_desc->shape()}, |
| 27 | + k_strides_{k_desc->strides()}, |
| 28 | + v_shape_{v_desc->shape()}, |
| 29 | + v_strides_{v_desc->strides()}, |
| 30 | + past_kv_lengths_shape_{past_kv_lengths_desc->shape()}, |
| 31 | + past_kv_lengths_strides_{past_kv_lengths_desc->strides()}, |
| 32 | + dtype_{k_desc->dtype()} {} |
| 33 | + |
| 34 | + ~Descriptor() = default; |
| 35 | + |
| 36 | + size_t get_workspace_size() const { return 0; }; |
| 37 | + |
| 38 | + static infiniStatus_t create( |
| 39 | + infiniopHandle_t handle, |
| 40 | + Descriptor **desc_ptr, |
| 41 | + infiniopTensorDescriptor_t k_cache, |
| 42 | + infiniopTensorDescriptor_t v_cache, |
| 43 | + infiniopTensorDescriptor_t k, |
| 44 | + infiniopTensorDescriptor_t v, |
| 45 | + infiniopTensorDescriptor_t past_kv_lengths) { |
| 46 | + *desc_ptr = new Descriptor{handle, k_cache, v_cache, k, v, past_kv_lengths}; |
| 47 | + return INFINI_STATUS_SUCCESS; |
| 48 | + } |
| 49 | + |
| 50 | + infiniStatus_t calculate( |
| 51 | + void *workspace, size_t workspace_size, |
| 52 | + void *k_cache, |
| 53 | + void *v_cache, |
| 54 | + const void *k, |
| 55 | + const void *v, |
| 56 | + const void *past_kv_lengths, |
| 57 | + void *stream) const { |
| 58 | + auto k_cache_nt{::ninetoothed::Tensor{k_cache, k_cache_shape_, k_cache_strides_}}; |
| 59 | + auto v_cache_nt{::ninetoothed::Tensor{v_cache, v_cache_shape_, v_cache_strides_}}; |
| 60 | + auto k_nt{::ninetoothed::Tensor{k, k_shape_, k_strides_}}; |
| 61 | + auto v_nt{::ninetoothed::Tensor{v, v_shape_, v_strides_}}; |
| 62 | + auto past_kv_lengths_nt{::ninetoothed::Tensor{past_kv_lengths, past_kv_lengths_shape_, past_kv_lengths_strides_}}; |
| 63 | + |
| 64 | + if (launch_kv_caching(stream, |
| 65 | + k_cache_nt, |
| 66 | + v_cache_nt, |
| 67 | + k_nt, |
| 68 | + v_nt, |
| 69 | + past_kv_lengths_nt, |
| 70 | + k_shape_[3], |
| 71 | + dtype_, |
| 72 | + 64, 64)) { |
| 73 | + return INFINI_STATUS_NOT_IMPLEMENTED; |
| 74 | + } |
| 75 | + |
| 76 | + return INFINI_STATUS_SUCCESS; |
| 77 | + } |
| 78 | + |
| 79 | +private: |
| 80 | + using Size = ::ninetoothed::Tensor<>::Size; |
| 81 | + using Stride = ::ninetoothed::Tensor<>::Stride; |
| 82 | + |
| 83 | + std::vector<Size> k_cache_shape_; |
| 84 | + std::vector<Stride> k_cache_strides_; |
| 85 | + |
| 86 | + std::vector<Size> v_cache_shape_; |
| 87 | + std::vector<Stride> v_cache_strides_; |
| 88 | + |
| 89 | + std::vector<Size> k_shape_; |
| 90 | + std::vector<Stride> k_strides_; |
| 91 | + std::vector<Size> v_shape_; |
| 92 | + std::vector<Stride> v_strides_; |
| 93 | + |
| 94 | + std::vector<Size> past_kv_lengths_shape_; |
| 95 | + std::vector<Stride> past_kv_lengths_strides_; |
| 96 | + |
| 97 | + infiniDtype_t dtype_; |
| 98 | +}; |
| 99 | +} // namespace op::kv_caching::ninetoothed |
| 100 | + |
| 101 | +#endif // KV_CACHING_H |
0 commit comments