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__ W/home/ubuntu/veenaModal/venv/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   