2023-12-14 12:35:58 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
2024-06-09 16:23:30 -04:00
|
|
|
#include <torch/all.h>
|
2025-12-12 08:57:47 -05:00
|
|
|
#include <c10/util/Optional.h>
|
2023-02-16 07:47:03 +00:00
|
|
|
|
2023-03-10 09:58:21 -08:00
|
|
|
#include <map>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2024-05-22 03:18:41 -04:00
|
|
|
void swap_blocks(torch::Tensor& src, torch::Tensor& dst,
|
2026-01-22 14:30:04 +02:00
|
|
|
int64_t block_size_in_bytes,
|
2024-05-22 03:18:41 -04:00
|
|
|
const torch::Tensor& block_mapping);
|
2023-02-16 07:47:03 +00:00
|
|
|
|
2024-05-22 03:18:41 -04:00
|
|
|
void reshape_and_cache(torch::Tensor& key, torch::Tensor& value,
|
|
|
|
|
torch::Tensor& key_cache, torch::Tensor& value_cache,
|
|
|
|
|
torch::Tensor& slot_mapping,
|
2025-01-23 13:04:03 -05:00
|
|
|
const std::string& kv_cache_dtype,
|
|
|
|
|
torch::Tensor& k_scale, torch::Tensor& v_scale);
|
2023-02-18 19:22:57 +00:00
|
|
|
|
2024-05-22 03:18:41 -04:00
|
|
|
void reshape_and_cache_flash(torch::Tensor& key, torch::Tensor& value,
|
|
|
|
|
torch::Tensor& key_cache,
|
|
|
|
|
torch::Tensor& value_cache,
|
|
|
|
|
torch::Tensor& slot_mapping,
|
2024-07-24 11:36:52 -07:00
|
|
|
const std::string& kv_cache_dtype,
|
2025-01-23 13:04:03 -05:00
|
|
|
torch::Tensor& k_scale, torch::Tensor& v_scale);
|
2024-05-03 15:51:27 -07:00
|
|
|
|
2025-01-31 02:49:37 -05:00
|
|
|
void concat_and_cache_mla(torch::Tensor& kv_c, torch::Tensor& k_pe,
|
|
|
|
|
torch::Tensor& kv_cache, torch::Tensor& slot_mapping,
|
|
|
|
|
const std::string& kv_cache_dtype,
|
|
|
|
|
torch::Tensor& scale);
|
|
|
|
|
|
2026-01-10 04:18:37 +01:00
|
|
|
// NOTE: k_pe and kv_c order is flipped compared to concat_and_cache_mla
|
|
|
|
|
void concat_and_cache_mla_rope_fused(
|
|
|
|
|
torch::Tensor& positions, torch::Tensor& q_pe, torch::Tensor& k_pe,
|
|
|
|
|
torch::Tensor& kv_c, torch::Tensor& rope_cos_sin_cache, bool rope_is_neox,
|
|
|
|
|
torch::Tensor& kv_cache_slot_mapping, torch::Tensor& kv_cache,
|
|
|
|
|
const std::string& kv_cache_dtype, torch::Tensor& kv_cache_quant_scale);
|
|
|
|
|
|
2024-01-29 08:43:54 +08:00
|
|
|
// Just for unittest
|
2024-05-22 03:18:41 -04:00
|
|
|
void convert_fp8(torch::Tensor& dst_cache, torch::Tensor& src_cache,
|
2024-06-09 16:23:30 -04:00
|
|
|
const double scale, const std::string& kv_cache_dtype);
|
2025-02-21 18:30:12 -05:00
|
|
|
|
2025-08-21 22:26:32 -04:00
|
|
|
void gather_and_maybe_dequant_cache(
|
2025-11-25 10:05:46 +08:00
|
|
|
torch::Tensor const& src_cache, // [NUM_BLOCKS, BLOCK_SIZE, ENTRIES...]
|
|
|
|
|
torch::Tensor const& dst, // [TOT_TOKENS, ENTRIES...]
|
|
|
|
|
torch::Tensor const& block_table, // [BATCH, BLOCK_INDICES]
|
|
|
|
|
torch::Tensor const& cu_seq_lens, // [BATCH+1]
|
|
|
|
|
torch::Tensor const& token_to_seq, // [MAX_TOKEN_ACROSS_CHUNKS]
|
|
|
|
|
int64_t num_tokens, const std::string& kv_cache_dtype,
|
2025-08-21 22:26:32 -04:00
|
|
|
torch::Tensor const& scale,
|
2025-08-28 15:29:11 +08:00
|
|
|
std::optional<torch::Tensor> seq_starts = std::nullopt);
|
|
|
|
|
|
|
|
|
|
// TODO(hc): cp_gather_cache need support scaled kvcahe in the future.
|
|
|
|
|
void cp_gather_cache(
|
|
|
|
|
torch::Tensor const& src_cache, // [NUM_BLOCKS, BLOCK_SIZE, ENTRIES...]
|
|
|
|
|
torch::Tensor const& dst, // [TOT_TOKENS, ENTRIES...]
|
|
|
|
|
torch::Tensor const& block_table, // [BATCH, BLOCK_INDICES]
|
|
|
|
|
torch::Tensor const& cu_seq_lens, // [BATCH+1]
|
|
|
|
|
int64_t batch_size, std::optional<torch::Tensor> seq_starts = std::nullopt);
|
2025-09-30 05:14:41 -04:00
|
|
|
|
2025-12-12 08:57:47 -05:00
|
|
|
// Gather and upconvert FP8 KV cache to BF16 workspace
|
|
|
|
|
void cp_gather_and_upconvert_fp8_kv_cache(
|
|
|
|
|
torch::Tensor const& src_cache, // [NUM_BLOCKS, BLOCK_SIZE, 656]
|
|
|
|
|
torch::Tensor const& dst, // [TOT_TOKENS, 576]
|
|
|
|
|
torch::Tensor const& block_table, // [BATCH, BLOCK_INDICES]
|
|
|
|
|
torch::Tensor const& seq_lens, // [BATCH]
|
|
|
|
|
torch::Tensor const& workspace_starts, // [BATCH]
|
|
|
|
|
int64_t batch_size);
|
|
|
|
|
|
2025-09-30 05:14:41 -04:00
|
|
|
// Indexer K quantization and cache function
|
|
|
|
|
void indexer_k_quant_and_cache(
|
|
|
|
|
torch::Tensor& k, // [num_tokens, head_dim]
|
|
|
|
|
torch::Tensor& kv_cache, // [num_blocks, block_size, cache_stride]
|
|
|
|
|
torch::Tensor& slot_mapping, // [num_tokens]
|
|
|
|
|
int64_t quant_block_size, // quantization block size
|
|
|
|
|
const std::string& scale_fmt);
|
2025-10-08 12:58:57 +08:00
|
|
|
|
|
|
|
|
// Extract function to gather quantized K cache
|
|
|
|
|
void cp_gather_indexer_k_quant_cache(
|
|
|
|
|
const torch::Tensor& kv_cache, // [num_blocks, block_size, cache_stride]
|
|
|
|
|
torch::Tensor& dst_k, // [num_tokens, head_dim]
|
|
|
|
|
torch::Tensor& dst_scale, // [num_tokens, head_dim / quant_block_size * 4]
|
|
|
|
|
const torch::Tensor& block_table, // [batch_size, num_blocks]
|
2025-12-12 08:57:47 -05:00
|
|
|
const torch::Tensor& cu_seq_lens); // [batch_size + 1]
|