
    .`i"                         d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	  G d de          Z
e G d d	                      Ze G d
 d                      Z G d de          ZdS )a:  
OffloadingManager class for managing KV data offloading in vLLM v1

This class runs in the scheduler, tracks which blocks are offloaded
and their address.

The class provides the following primitives:
    lookup() - find the length of the maximal series of blocks,
        starting from the first one, that are all offloaded.
    prepare_load() - prepare given blocks to be read.
        The given blocks will be protected from eviction.
        This function returns a LoadSpec which encapsulates
        information required for performing the load.
    touch() - marks the give blocks as recently used. Can be used
        to track block's LRU. This function is separated from the
        prepare_load function to allow setting block recency even
        for blocks which do not need reading from the cache, such as
        blocks that are cached by the GPU prefix cache.
    complete_load() - mark blocks which were previously prepared to be
        loaded as done loading. This is to re-allow their eviction.
    prepare_store() - prepare the given blocks to be written.
        Returns a StoreSpec encapsulating offloading information,
        as well as a list of blocks that were evicted as a result.
    complete_store() - marks a previous store as completed.
        Following this call, the given blocks will become loadable.
    )ABCabstractmethod)Iterable)	dataclass)	BlockHashc                   >    e Zd ZdZeedefd                        ZdS )LoadStoreSpecz
    Abstract metadata that encapsulates information allowing a worker
    to load, and optionally also to store, blocks of KV data.
    returnc                      dS )ze
        Returns a string representation of the medium type
        this store/load targets.
        N r       o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/v1/kv_offload/abstract.pymediumzLoadStoreSpec.medium+   s	     	r   N)__name__
__module____qualname____doc__staticmethodr   strr   r   r   r   r	   r	   %   sO         
 C    ^ \  r   r	   c                   F    e Zd ZU ee         ed<   eed<   ee         ed<   dS )PrepareStoreOutputblock_hashes_to_store
store_specblock_hashes_evictedN)r   r   r   listr   __annotations__r	   r   r   r   r   r   5   s>         	?***y/)))))r   r   c                   D    e Zd ZU ee         ed<   eed<   eed<   eed<   dS )OffloadingEventblock_hashes
block_sizer   removedN)	r   r   r   r   r   r   intr   boolr   r   r   r   r   <   s;         y/!!!OOOKKKMMMMMr   r   c                      e Zd Zedee         dedz  fd            Zedee         defd            Z	dee         fdZ
dee         fdZedee         dedz  fd            Zddee         d
efdZdee         fdZdS )OffloadingManagerr   r
   Nc                     dS )a  
        Finds the length of the maximal series of blocks, starting from the
        first one, that are all offloaded.

        Args:
            block_hashes: the hashes identifying the blocks to lookup.

        Returns:
            An integer representing the maximal number of blocks that
            are currently offloaded, or None if the lookup should be retried
            later. Returning None will delay the request handling by the vLLM
            scheduler.
        Nr   selfr   s     r   lookupzOffloadingManager.lookupF   	     	r   c                     dS )a  
        Prepare the given blocks to be read.
        The given blocks will be protected from eviction until
        complete_load is called.
        It assumes all given blocks are offloaded.

        Args:
            block_hashes: the hashes identifying the blocks.

        Returns:
            A LoadStoreSpec that can be used by a worker to locate and load
            the actual offloaded KV data.
        Nr   r'   s     r   prepare_loadzOffloadingManager.prepare_loadW   r*   r   c                     dS )z
        Mark the given blocks as recently used.
        This could in practice mean moving them to the end of an LRU list.

        Args:
            block_hashes: the hashes identifying the blocks.
        Nr   r'   s     r   touchzOffloadingManager.touchh   s	     	r   c                     dS )z
        Marks previous blocks that were prepared to load as done loading.

        Args:
            block_hashes: the hashes identifying the blocks.
        Nr   r'   s     r   complete_loadzOffloadingManager.complete_loadr   s	     	r   c                     dS )a  
        Prepare the given blocks to be offloaded.
        The given blocks will be protected from eviction until
        complete_store is called.

        Args:
            block_hashes: the hashes identifying the blocks.

        Returns:
            A PrepareStoreOutput indicating which blocks need storing,
            where to store them (LoadStoreSpec), and list of blocks that
            were evicted as a result.
            None is returned if the blocks cannot be stored.
        Nr   r'   s     r   prepare_storezOffloadingManager.prepare_store{   s	    $ 	r   Tsuccessc                     dS )a|  
        Marks blocks which were previously prepared to be stored, as stored.
        Following this call, the blocks become loadable.
        If if_success is False, blocks that were not marked as stored will be
        removed.

        Args:
            block_hashes: the hashes identifying the blocks.
            success: whether the blocks were stored successfully.
        Nr   )r(   r   r3   s      r   complete_storez OffloadingManager.complete_store   s	     	r   c                     dS )z
        Take the offloading events from the manager.

        Yields:
            New OffloadingEvents collected since the last call.
        r   r   )r(   s    r   take_eventszOffloadingManager.take_events   s	     rr   )T)r   r   r   r   r   r   r"   r)   r	   r,   r.   r0   r   r2   r#   r5   r   r7   r   r   r   r%   r%   E   sB       8I#6 3:    ^  ))<     ^ (9"5    (9*=     $Y/	d	"   ^& 8I+>     Xo6      r   r%   N)r   abcr   r   collections.abcr   dataclassesr   vllm.v1.core.kv_cache_utilsr   r	   r   r   r%   r   r   r   <module>r<      s/   6 $ # # # # # # # $ $ $ $ $ $ ! ! ! ! ! ! 1 1 1 1 1 1    C     * * * * * * * *        ^ ^ ^ ^ ^ ^ ^ ^ ^ ^r   