|
| 1 | +#ifndef INFINI_OPS_CUDA_KV_CACHING_INFINILM_KERNEL_H_ |
| 2 | +#define INFINI_OPS_CUDA_KV_CACHING_INFINILM_KERNEL_H_ |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <cstddef> |
| 6 | +#include <cstring> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#include "base/kv_caching_infinilm.h" |
| 10 | +#include "common/generic_utils.h" |
| 11 | +#include "data_type.h" |
| 12 | +#include "dispatcher.h" |
| 13 | +#include "native/cuda/ops/kv_caching_infinilm/kernel.cuh" |
| 14 | +#include "native/cuda/runtime_utils.h" |
| 15 | + |
| 16 | +namespace infini::ops { |
| 17 | + |
| 18 | +template <typename Backend> |
| 19 | +class CudaKvCachingInfinilm : public KvCachingInfinilm { |
| 20 | + public: |
| 21 | + CudaKvCachingInfinilm(const Tensor k, const Tensor v, |
| 22 | + const Tensor past_kv_lengths, Tensor k_cache, |
| 23 | + Tensor v_cache) |
| 24 | + : KvCachingInfinilm{k, v, past_kv_lengths, k_cache, v_cache} { |
| 25 | + constexpr size_t ndim = 4; |
| 26 | + size_t strides_size = ndim * sizeof(*d_k_cache_strides_); |
| 27 | + const size_t metadata_size = 4 * strides_size; |
| 28 | + std::vector<std::byte> metadata(metadata_size); |
| 29 | + |
| 30 | + Backend::Malloc((void**)&d_metadata_, metadata_size); |
| 31 | + |
| 32 | + size_t offset = 0; |
| 33 | + d_k_cache_strides_ = |
| 34 | + reinterpret_cast<Tensor::Stride*>(d_metadata_ + offset); |
| 35 | + std::memcpy(metadata.data() + offset, k_cache_strides_.data(), |
| 36 | + strides_size); |
| 37 | + offset += strides_size; |
| 38 | + |
| 39 | + d_v_cache_strides_ = |
| 40 | + reinterpret_cast<Tensor::Stride*>(d_metadata_ + offset); |
| 41 | + std::memcpy(metadata.data() + offset, v_cache_strides_.data(), |
| 42 | + strides_size); |
| 43 | + offset += strides_size; |
| 44 | + |
| 45 | + d_k_strides_ = reinterpret_cast<Tensor::Stride*>(d_metadata_ + offset); |
| 46 | + std::memcpy(metadata.data() + offset, k_strides_.data(), strides_size); |
| 47 | + offset += strides_size; |
| 48 | + |
| 49 | + d_v_strides_ = reinterpret_cast<Tensor::Stride*>(d_metadata_ + offset); |
| 50 | + std::memcpy(metadata.data() + offset, v_strides_.data(), strides_size); |
| 51 | + |
| 52 | + Backend::Memcpy(d_metadata_, metadata.data(), metadata_size, |
| 53 | + Backend::MemcpyHostToDevice); |
| 54 | + } |
| 55 | + |
| 56 | + ~CudaKvCachingInfinilm() { Backend::Free(d_metadata_); } |
| 57 | + |
| 58 | + void operator()(const Tensor k, const Tensor v, const Tensor past_kv_lengths, |
| 59 | + Tensor k_cache, Tensor v_cache) const override { |
| 60 | + auto cuda_stream = |
| 61 | + static_cast<typename Backend::Stream>(stream_ ? stream_ : 0); |
| 62 | + int block_size = std::min( |
| 63 | + RuntimeUtils<Backend::kDeviceType>::GetOptimalBlockSize(), 1024); |
| 64 | + dim3 block(std::min(static_cast<Tensor::Size>(block_size), output_size_)); |
| 65 | + dim3 grid(utils::CeilDiv(output_size_, block.x)); |
| 66 | + |
| 67 | + using IndexTypes = List<DataType::kInt32, DataType::kInt64>; |
| 68 | + DispatchFunc<AllFloatTypes, IndexTypes, List<128, 256, 512, 1024>>( |
| 69 | + {static_cast<int64_t>(data_type_), |
| 70 | + static_cast<int64_t>(past_kv_lengths_type_), block_size}, |
| 71 | + [&](auto list_tag) { |
| 72 | + using T = TypeMapType<Backend::kDeviceType, ListGet<0>(list_tag)>; |
| 73 | + using TIndex = |
| 74 | + TypeMapType<Backend::kDeviceType, ListGet<1>(list_tag)>; |
| 75 | + constexpr int kBlockSize = ListGet<2>(list_tag); |
| 76 | + |
| 77 | + KvCachingInfinilmKernel<T, TIndex, kBlockSize> |
| 78 | + <<<grid, block, 0, cuda_stream>>>( |
| 79 | + reinterpret_cast<T*>(k_cache.data()), |
| 80 | + reinterpret_cast<T*>(v_cache.data()), |
| 81 | + reinterpret_cast<const T*>(k.data()), |
| 82 | + reinterpret_cast<const T*>(v.data()), |
| 83 | + reinterpret_cast<const TIndex*>(past_kv_lengths.data()), |
| 84 | + d_k_cache_strides_, d_v_cache_strides_, d_k_strides_, |
| 85 | + d_v_strides_, output_size_, num_kv_heads_, seq_len_, |
| 86 | + hidden_size_); |
| 87 | + }, |
| 88 | + "CudaKvCachingInfinilm::operator()"); |
| 89 | + } |
| 90 | + |
| 91 | + private: |
| 92 | + std::byte* d_metadata_{nullptr}; |
| 93 | + |
| 94 | + Tensor::Stride* d_k_cache_strides_{nullptr}; |
| 95 | + |
| 96 | + Tensor::Stride* d_v_cache_strides_{nullptr}; |
| 97 | + |
| 98 | + Tensor::Stride* d_k_strides_{nullptr}; |
| 99 | + |
| 100 | + Tensor::Stride* d_v_strides_{nullptr}; |
| 101 | +}; |
| 102 | + |
| 103 | +} // namespace infini::ops |
| 104 | + |
| 105 | +#endif |
0 commit comments