@@ -47,26 +47,6 @@ def _dequantize_fp8(values: torch.Tensor, scales: torch.Tensor) -> torch.Tensor:
4747 return values .to (torch .bfloat16 ) * scales .to (torch .bfloat16 ).unsqueeze (- 1 )
4848
4949
50- def _gather_paged_fp8_cache (
51- kv_cache : torch .Tensor ,
52- block_table : torch .Tensor ,
53- context_len : int ,
54- head_dim : int ,
55- ) -> torch .Tensor :
56- num_blocks , block_size , _ = kv_cache .shape
57- cache = kv_cache .view (num_blocks , - 1 )
58- values = cache [:, : block_size * head_dim ].view (
59- current_platform .fp8_dtype ()
60- )
61- values = values .view (num_blocks , block_size , head_dim )
62- scales = cache [:, block_size * head_dim :].view (torch .float32 )
63- scales = scales .view (num_blocks , block_size )
64- block_ids = block_table [: (context_len + block_size - 1 ) // block_size ]
65- values = values [block_ids ].reshape (- 1 , head_dim )[:context_len ]
66- scales = scales [block_ids ].reshape (- 1 )[:context_len ]
67- return _dequantize_fp8 (values , scales )
68-
69-
7050def _gather_workspace_shapes (
7151 total_seq_lens : int ,
7252 head_dim : int ,
@@ -338,7 +318,37 @@ def sparse_attn_indexer_fl(
338318 if current_platform .vendor_name == "metax" and not use_fp4_cache :
339319 from vllm_metax .utils .deep_gemm import bf16_mqa_logits
340320
341- max_context_len = int (seq_lens .max ().item ())
321+ context_lens_per_batch = seq_lens .amax (dim = 1 )
322+ cu_seq_lens = torch .zeros (
323+ batch_size + 1 ,
324+ dtype = torch .int32 ,
325+ device = seq_lens .device ,
326+ )
327+ torch .cumsum (context_lens_per_batch , dim = 0 , out = cu_seq_lens [1 :])
328+ seq_offsets = cu_seq_lens .tolist ()
329+
330+ workspace_manager = current_workspace_manager ()
331+ values_spec , scales_spec = _gather_workspace_shapes (
332+ total_seq_lens , head_dim , fp8_dtype , False
333+ )
334+ k_quant_full , k_scale_full = workspace_manager .get_simultaneous (
335+ values_spec ,
336+ scales_spec ,
337+ )
338+ total_context_len = seq_offsets [- 1 ]
339+ k_quant = k_quant_full [:total_context_len ]
340+ k_scale = k_scale_full [:total_context_len ]
341+ _cp_gather_indexer_k_quant_cache (
342+ kv_cache ,
343+ k_quant ,
344+ k_scale ,
345+ decode_metadata .block_table ,
346+ cu_seq_lens ,
347+ )
348+
349+ max_context_len = max (
350+ seq_offsets [i + 1 ] - seq_offsets [i ] for i in range (batch_size )
351+ )
342352 logits = torch .full (
343353 (num_padded_tokens , max_context_len ),
344354 - float ("inf" ),
@@ -347,17 +357,16 @@ def sparse_attn_indexer_fl(
347357 )
348358 for batch_idx in range (batch_size ):
349359 context_lens = seq_lens [batch_idx ].reshape (- 1 )
350- context_len = int (context_lens .max ().item ())
351- k_bf16 = _gather_paged_fp8_cache (
352- kv_cache .squeeze (- 2 ),
353- decode_metadata .block_table [batch_idx ],
354- context_len ,
355- head_dim ,
360+ seq_start = seq_offsets [batch_idx ]
361+ seq_end = seq_offsets [batch_idx + 1 ]
362+ k_bf16 = _dequantize_fp8 (
363+ k_quant [seq_start :seq_end ],
364+ k_scale [seq_start :seq_end ].view (torch .float32 ).squeeze (- 1 ),
356365 )
357366 q_bf16 = padded_q_quant_cast [batch_idx ].to (torch .bfloat16 )
358367 starts = torch .zeros_like (context_lens )
359368 row_start = batch_idx * next_n
360- logits [row_start : row_start + next_n , :context_len ] = (
369+ logits [row_start : row_start + next_n , : seq_end - seq_start ] = (
361370 bf16_mqa_logits (
362371 q_bf16 ,
363372 k_bf16 ,
0 commit comments