o
    
Û¾iR  ã                   @   sb   d dl 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„ de j
ƒZG dd	„ d	eƒZdS )
é    N)ÚABCÚabstractmethod)ÚIterable)Ú	BlockHash)ÚLoadStoreSpecc                       s>   e Zd ZdZdejfgZ‡ fdd„Zede	fdd„ƒZ
‡  ZS )ÚBlockStatusaW  
    Offloading status for a single block of KV data.
    Holds the following information:

    ref_cnt - the current number of transfers using this block as a source.
        A value of -1 indicates the block is not yet ready to be read.
    load_store_spec - backend-specific information on how to actually
        read/write the block.
    Úref_cntc                    s   t ƒ  ¡  d| _d S )Néÿÿÿÿ)ÚsuperÚ__init__r   ©Úself©Ú	__class__© úN/home/ubuntu/.local/lib/python3.10/site-packages/vllm/v1/kv_offload/backend.pyr      s   

zBlockStatus.__init__Úreturnc                 C   s
   | j dkS )z@
        Returns whether the block is ready to be read.
        r   )r   r   r   r   r   Úis_ready   s   
zBlockStatus.is_ready)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚctypesÚc_int32Ú_fields_r   ÚpropertyÚboolr   Ú__classcell__r   r   r   r   r      s    
r   c                   @   s|   e Zd ZdZdedefdd„Zedd„ ƒZede	e
 d	e	e fd
d„ƒZedefdd„ƒZdee
 dee d	efdd„ZdS )ÚBackendzi
    An abstract class for allocating and returning specs for writing
    KV blocks to some backend.
    Ú
block_sizeÚmediumc                 C   s   || _ || _d S )N)r   r    )r   r   r    r   r   r   r   +   s   
zBackend.__init__c                 C   ó   dS )zW
        Returns the number of current number of blocks that can be allocated.
        Nr   r   r   r   r   Úget_num_free_blocks/   s   zBackend.get_num_free_blocksÚblock_hashesr   c                 C   r!   )aà  
        Allocate space for writing blocks.
        This method assumes there is enough space for allocation.
        It is unsafe to use without checking get_num_free_blocks beforehand.

        Args:
            block_hashes: the hashes identifying the blocks to be written.

        Returns:
            A list of BlockStatus for the allocated blocks.
            The ref_cnt of each returned item will be -1, meaning the block
            is not yet ready to be read.
        Nr   )r   r#   r   r   r   Úallocate_blocks6   s   zBackend.allocate_blocksÚblockc                 C   r!   )zç
        Free a previously allocated block.
        You should only call this function with blocks returned by
        allocate_blocks, and only once per each block.

        Args:
            block: The block to be freed.
        Nr   )r   r%   r   r   r   ÚfreeG   s   
zBackend.freeÚblocksc                 C   s   t ‚)aB  
        Get backend-specific information on how to read/write blocks.

        Args:
            block_hashes: the list of block hashes identifying the blocks.
            blocks: the list of blocks.

        Returns:
            A LoadStoreSpec that can be used by a worker
            to read/write the blocks.
        )ÚNotImplementedError)r   r#   r'   r   r   r   Úget_load_store_specS   s   zBackend.get_load_store_specN)r   r   r   r   ÚintÚstrr   r   r"   Úlistr   r   r$   r&   r   r   r)   r   r   r   r   r   %   s     
ÿÿþr   )r   Úabcr   r   Úcollections.abcr   Úvllm.v1.core.kv_cache_utilsr   Úvllm.v1.kv_offload.abstractr   Ú	Structurer   r   r   r   r   r   Ú<module>   s   