@@ -494,6 +494,19 @@ def cache_finished_req(
494494 )
495495 return
496496
497+ if not is_insert :
498+ # Frees committed slots that no token id names, which the insert
499+ # path below cannot reach; the protected prefix stays with the cache.
500+ kv_indices = self .req_to_token_pool .req_to_token [
501+ req .kv .req_pool_idx , req .kv .cache_protected_len : kv_len_to_handle
502+ ]
503+ self .token_to_kv_pool_allocator .free_segment (
504+ kv_indices , start_pos = req .kv .cache_protected_len
505+ )
506+ if req .last_node is not None :
507+ self .dec_lock_ref (req .last_node )
508+ return
509+
497510 token_ids = (req .origin_input_ids + req .output_ids )[:kv_len_to_handle ]
498511 kv_indices = self .req_to_token_pool .req_to_token [
499512 req .kv .req_pool_idx , : len (token_ids )
@@ -509,39 +522,36 @@ def cache_finished_req(
509522 values = kv_indices [:key_len ].to (dtype = torch .int64 , copy = True )
510523
511524 # Radix Cache takes one ref in memory pool
512- if is_insert :
513- priority = getattr (req , "priority" , 0 ) or 0
514- result = self .insert (
515- InsertParams (key = radix_key , value = values , priority = priority )
516- )
517- # A request that was never cached while unfinished can add its
518- # whole prompt and generated output as one leaf. Split that leaf at
519- # the prompt boundary so LRU eviction can discard output KV without
520- # also losing the reusable prompt KV. Reinserting a prefix only
521- # changes radix topology; it reuses the indices inserted above.
522- prompt_key = RadixKey (
523- token_ids [: len (req .origin_input_ids )],
524- req .extra_key ,
525- is_bigram = self .is_eagle ,
526- cache_salt = req .cache_salt ,
527- ).page_aligned (self .page_size )
528- if 0 < len (prompt_key ) < key_len :
529- self .insert (
530- InsertParams (
531- key = prompt_key ,
532- value = values [: len (prompt_key )],
533- priority = priority + 1 ,
534- # Topology-only re-insert: this request created these
535- # nodes moments ago, so counting it as a hit is the
536- # same self-referencing inflation `chunked` exists to
537- # suppress. hit_count drives eviction order, so an
538- # extra bump would silently promote every prompt node.
539- chunked = True ,
540- )
525+ priority = getattr (req , "priority" , 0 ) or 0
526+ result = self .insert (
527+ InsertParams (key = radix_key , value = values , priority = priority )
528+ )
529+ # A request that was never cached while unfinished can add its
530+ # whole prompt and generated output as one leaf. Split that leaf at
531+ # the prompt boundary so LRU eviction can discard output KV without
532+ # also losing the reusable prompt KV. Reinserting a prefix only
533+ # changes radix topology; it reuses the indices inserted above.
534+ prompt_key = RadixKey (
535+ token_ids [: len (req .origin_input_ids )],
536+ req .extra_key ,
537+ is_bigram = self .is_eagle ,
538+ cache_salt = req .cache_salt ,
539+ ).page_aligned (self .page_size )
540+ if 0 < len (prompt_key ) < key_len :
541+ self .insert (
542+ InsertParams (
543+ key = prompt_key ,
544+ value = values [: len (prompt_key )],
545+ priority = priority + 1 ,
546+ # Topology-only re-insert: this request created these
547+ # nodes moments ago, so counting it as a hit is the
548+ # same self-referencing inflation `chunked` exists to
549+ # suppress. hit_count drives eviction order, so an
550+ # extra bump would silently promote every prompt node.
551+ chunked = True ,
541552 )
542- freed_end = result .prefix_len
543- else :
544- freed_end = key_len
553+ )
554+ freed_end = result .prefix_len
545555
546556 # duplicates / uninserted range, then the unaligned tail
547557 self .token_to_kv_pool_allocator .free_segments (
0 commit comments