@@ -497,9 +497,11 @@ class PagedKVCacheAuxDataManager {
497497 /* ! \brief Copy the indptr array of append lengths after coalescing. (see GetChunkedBlockIds) */
498498 virtual ffi::Tensor CopyQOIndptrOnDepthAsync (HostMemoryVector* data, int depth) = 0;
499499 /* ! \brief Copy the indptr array of page table. */
500- virtual ffi::Tensor CopyPageIndptrOnDepthAsync (HostMemoryVector* data, int depth) = 0;
500+ virtual ffi::Tensor CopyPageIndptrOnDepthAsync (HostMemoryVector* data, int depth,
501+ bool layer_sliding_window = false ) = 0;
501502 /* ! \brief Copy the indices array of page table. */
502- virtual ffi::Tensor CopyPageIndicesOnDepthAsync (HostMemoryVector* data, int depth) = 0;
503+ virtual ffi::Tensor CopyPageIndicesOnDepthAsync (HostMemoryVector* data, int depth,
504+ bool layer_sliding_window = false ) = 0;
503505 /* ! \brief Copy the array of KV slot number used in the last page of the seq. */
504506 virtual ffi::Tensor CopyLastPageLenOnDepthAsync (HostMemoryVector* data, int depth) = 0;
505507 /* !
@@ -514,7 +516,8 @@ class PagedKVCacheAuxDataManager {
514516 */
515517 virtual ffi::Tensor CopyLengthInfoOnDepthAsync (HostMemoryVector* last_page_len,
516518 HostMemoryVector* sliding_window_offset,
517- HostMemoryVector* sink_size, int depth) = 0;
519+ HostMemoryVector* sink_size, int depth,
520+ bool layer_sliding_window = false ) = 0;
518521 /* ! \brief Copy the k position offset of applying RoPE for each sequence. */
519522 virtual ffi::Tensor CopyKRoPEPosOffsetOnDepthAsync (HostMemoryVector* data, int depth) = 0;
520523 /* !
@@ -580,7 +583,8 @@ class PlainPagedKVCacheAuxDataManager : public PagedKVCacheAuxDataManager {
580583 explicit PlainPagedKVCacheAuxDataManager (int64_t reserved_num_seqs, int64_t num_total_pages,
581584 int64_t prefill_chunk_size, DLDataType dtype_aux,
582585 Device device, Device preferred_host_device,
583- TVMStreamHandle copy_stream)
586+ TVMStreamHandle copy_stream,
587+ bool layer_sliding_window = false )
584588 : PagedKVCacheAuxDataManager(dtype_aux, device, preferred_host_device, copy_stream),
585589 cur_append_length_indptr_device_(
586590 tvm::runtime::Empty ({reserved_num_seqs + 1 }, dtype_aux_, device)),
@@ -611,6 +615,14 @@ class PlainPagedKVCacheAuxDataManager : public PagedKVCacheAuxDataManager {
611615 tvm::runtime::Empty ({num_total_pages}, dtype_aux_, device));
612616 length_info_on_depths_device_.push_back (
613617 tvm::runtime::Empty ({3 , reserved_num_seqs}, dtype_aux_, device));
618+ if (layer_sliding_window) {
619+ page_indptr_sliding_on_depths_device_.push_back (
620+ tvm::runtime::Empty ({reserved_num_seqs + 1 }, dtype_aux_, device));
621+ page_indices_sliding_on_depths_device_.push_back (
622+ tvm::runtime::Empty ({num_total_pages}, dtype_aux_, device));
623+ length_info_sliding_on_depths_device_.push_back (
624+ tvm::runtime::Empty ({3 , reserved_num_seqs}, dtype_aux_, device));
625+ }
614626 k_rope_pos_offset_on_depths_device_.push_back (
615627 tvm::runtime::Empty ({reserved_num_seqs}, dtype_aux_, device));
616628 tree_attn_mask_device_.push_back (tvm::runtime::Empty (
@@ -628,15 +640,21 @@ class PlainPagedKVCacheAuxDataManager : public PagedKVCacheAuxDataManager {
628640 CopyVecDataToArray (view, data->data ());
629641 return view;
630642 }
631- ffi::Tensor CopyPageIndptrOnDepthAsync (HostMemoryVector* data, int depth) final {
632- ffi::Tensor view = tvm::runtime::CreateView (page_indptr_on_depths_device_[depth],
633- {static_cast <int64_t >(data->size ())}, dtype_aux_);
643+ ffi::Tensor CopyPageIndptrOnDepthAsync (HostMemoryVector* data, int depth,
644+ bool layer_sliding_window = false ) final {
645+ const auto & storage = layer_sliding_window ? page_indptr_sliding_on_depths_device_
646+ : page_indptr_on_depths_device_;
647+ ffi::Tensor view =
648+ tvm::runtime::CreateView (storage[depth], {static_cast <int64_t >(data->size ())}, dtype_aux_);
634649 CopyVecDataToArray (view, data->data ());
635650 return view;
636651 }
637- ffi::Tensor CopyPageIndicesOnDepthAsync (HostMemoryVector* data, int depth) final {
638- ffi::Tensor view = tvm::runtime::CreateView (page_indices_on_depths_device_[depth],
639- {static_cast <int64_t >(data->size ())}, dtype_aux_);
652+ ffi::Tensor CopyPageIndicesOnDepthAsync (HostMemoryVector* data, int depth,
653+ bool layer_sliding_window = false ) final {
654+ const auto & storage = layer_sliding_window ? page_indices_sliding_on_depths_device_
655+ : page_indices_on_depths_device_;
656+ ffi::Tensor view =
657+ tvm::runtime::CreateView (storage[depth], {static_cast <int64_t >(data->size ())}, dtype_aux_);
640658 CopyVecDataToArray (view, data->data ());
641659 return view;
642660 }
@@ -722,11 +740,13 @@ class PlainPagedKVCacheAuxDataManager : public PagedKVCacheAuxDataManager {
722740
723741 ffi::Tensor CopyLengthInfoOnDepthAsync (HostMemoryVector* last_page_len,
724742 HostMemoryVector* sliding_window_offset,
725- HostMemoryVector* sink_size, int depth) final {
743+ HostMemoryVector* sink_size, int depth,
744+ bool layer_sliding_window = false ) final {
726745 int n_elem = last_page_len->size ();
727746 TVM_FFI_ICHECK_GT (n_elem, 0 );
728- ffi::Tensor view =
729- tvm::runtime::CreateView (length_info_on_depths_device_[depth], {3 , n_elem}, dtype_aux_);
747+ const auto & storage = layer_sliding_window ? length_info_sliding_on_depths_device_
748+ : length_info_on_depths_device_;
749+ ffi::Tensor view = tvm::runtime::CreateView (storage[depth], {3 , n_elem}, dtype_aux_);
730750 ffi::Shape copy_shape{n_elem};
731751 CopyVecDataToArray (view, last_page_len->data (), copy_shape);
732752 CopyVecDataToArray (view, sliding_window_offset->data (), copy_shape,
@@ -804,6 +824,10 @@ class PlainPagedKVCacheAuxDataManager : public PagedKVCacheAuxDataManager {
804824 tvm::runtime::CopyFromTo (©_src, ©_dst, copy_stream_);
805825 }
806826
827+ // Both page tables stay live until all global and sliding layers finish.
828+ std::vector<ffi::Tensor> page_indptr_sliding_on_depths_device_;
829+ std::vector<ffi::Tensor> page_indices_sliding_on_depths_device_;
830+ std::vector<ffi::Tensor> length_info_sliding_on_depths_device_;
807831 std::vector<ffi::Tensor> qo_indptr_on_depths_device_;
808832 std::vector<ffi::Tensor> page_indptr_on_depths_device_;
809833 std::vector<ffi::Tensor> page_indices_on_depths_device_;
@@ -865,10 +889,12 @@ class CachedPagedKVCacheAuxDataManager : public PagedKVCacheAuxDataManager {
865889 ffi::Tensor CopyQOIndptrOnDepthAsync (HostMemoryVector* data, int depth) final {
866890 return CopyAttnAuxVecToCache (data);
867891 }
868- ffi::Tensor CopyPageIndptrOnDepthAsync (HostMemoryVector* data, int depth) final {
892+ ffi::Tensor CopyPageIndptrOnDepthAsync (HostMemoryVector* data, int depth,
893+ bool layer_sliding_window = false ) final {
869894 return CopyAttnAuxVecToCache (data);
870895 }
871- ffi::Tensor CopyPageIndicesOnDepthAsync (HostMemoryVector* data, int depth) final {
896+ ffi::Tensor CopyPageIndicesOnDepthAsync (HostMemoryVector* data, int depth,
897+ bool layer_sliding_window = false ) final {
872898 return CopyAttnAuxVecToCache (data);
873899 }
874900 ffi::Tensor CopyLastPageLenOnDepthAsync (HostMemoryVector* data, int depth) final {
@@ -914,7 +940,8 @@ class CachedPagedKVCacheAuxDataManager : public PagedKVCacheAuxDataManager {
914940 }
915941 ffi::Tensor CopyLengthInfoOnDepthAsync (HostMemoryVector* last_page_len,
916942 HostMemoryVector* sliding_window_offset,
917- HostMemoryVector* sink_size, int depth) final {
943+ HostMemoryVector* sink_size, int depth,
944+ bool layer_sliding_window = false ) final {
918945 int64_t n_elem = last_page_len->size ();
919946 std::memcpy (merged_attn_aux_data_host_.data () + attn_aux_data_copy_offset_,
920947 last_page_len->data (), n_elem * elem_byte_size_);
0 commit comments