o
    )i                     @   s  U d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	m
Z
 d dlZd dlmZ d dlmZ d dlmZ d dlmZmZ d d	lmZmZ eeZd
ede	e fddZde	e fddZdae	e ed< de	e ddfddZde	e fddZ eddG dd dZ!ddde
ee"e f de#dej$de%de!f
ddZ&	 	 	 d.de#dej$d!e	e d"e#d#e%d$e%d%e%de"e fd&d'Z'e	 	 	 d.de#dej$d!e	e d"e#d#e%d(e%d$e%d%e%de"e fd)d*Z(ededed+ fd,d-Z)dS )/    N)contextmanager)	dataclass)cache)	GeneratorOptionalUnion)AttentionBackend)init_logger)_Backendcurrent_platform)STR_BACKEND_ENV_VARresolve_obj_by_qualnamebackend_namereturnc                 C   s"   | dusJ | t jv rt |  S dS )z
    Convert a string backend name to a _Backend enum value.

    Returns:
    * _Backend: enum value if backend_name is a valid in-tree type
    * None: otherwise it's an invalid in-tree type or an out-of-tree platform is
            loaded.
    N)r
   __members__r    r   c/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/vllm/attention/selector.pybackend_name_to_enum   s   	r   c                  C   s    t jt} | du rdS t| S )z
    Get the backend override specified by the vLLM attention
    backend environment variable, if one is specified.

    Returns:

    * _Backend enum value if an override is specified
    * None otherwise
    N)osenvirongetr   r   r   r   r   r   get_env_variable_attn_backend#   s
   
r   forced_attn_backendattn_backendc                 C   s   | a dS )z
    Force all attention operations to use a specified backend.

    Passing `None` for the argument re-enables automatic
    backend selection.,

    Arguments:

    * attn_backend: backend selection (None to revert to auto)
    Nr   )r   r   r   r   global_force_attn_backend<   s   r   c                   C   s   t S )zs
    Get the currently-forced choice of attention backend,
    or None if auto-selection is currently enabled.
    r   r   r   r   r   get_global_forced_attn_backendK   s   r   T)frozenc                   @   s4   e Zd ZU eed< eed< eed< defddZdS )_IsSupported
can_import	head_sizedtyper   c                 C   s   | j o| jo| jS )Nr    r!   r"   )selfr   r   r   __bool__Y   s   z_IsSupported.__bool__N)__name__
__module____qualname__bool__annotations__r%   r   r   r   r   r   S   s
   
 r   )allow_import_errorr!   r"   r+   c          	      C   s   t | trzt| } W n ty   |s tdddd Y S w t | ts&J t| dd  }r4|| v }n$t| dd  }rPz|| d}W n tyO   d}Y n	w t| j	 dt| dd  }rf|| v }nt| j	 dtd||dS )	NFr#   get_supported_head_sizesvalidate_head_sizeTz& does not support head size validationget_supported_dtypesz" does not support dtype validation)

isinstancestrr   ImportErrorr   typegetattr	ExceptionNotImplementedErrorr&   )	r   r!   r"   r+   r,   Zis_head_size_supportedr-   r.   Zis_dtype_supportedr   r   r   is_attn_backend_supported]   sB   
r6   Fkv_cache_dtype
block_sizeis_attention_freeuse_mlahas_sinkc              
   C   s   t | ||||tj||dS )z=Selects which attention backend to use and lazily imports it.)r!   r"   r7   r8   r9   use_v1r:   r;   )_cached_get_attn_backendenvsZVLLM_USE_V1)r!   r"   r7   r8   r9   r:   r;   r   r   r   get_attn_backend   s   r?   r<   c              
   C   s   |r
ddl m} |S d }	t }
|
d ur|
}	ntj}|d ur4t|}	|	d u r4td| dttj	
  t|	| ||||||}|sJtdtj t|S )Nr   )PlaceholderAttentionBackendzInvalid attention backend: 'z'. Valid backends are: zInvalid attention backend for )Z(vllm.attention.backends.placeholder_attnr@   r   r>   ZVLLM_ATTENTION_BACKENDr   
ValueErrorlistr
   r   keysr   Zget_attn_backend_clsZdevice_namer   )r!   r"   r7   r8   r9   r<   r:   r;   r@   Zselected_backendZbackend_by_global_settingZbackend_by_env_varZattention_clsr   r   r   r=      s4   
r=   )NNNc                 c   s0    t  }t|  z
dV  W t| dS t| w )a$  
    Globally force a vLLM attention backend override within a
    context manager, reverting the global attention backend
    override to its prior state upon exiting the context
    manager.

    Arguments:

    * attn_backend: attention backend to force

    Returns:

    * Generator
    N)r   r   )r   original_valuer   r   r   )global_force_attn_backend_context_manager   s   rE   )FFF)*r   
contextlibr   dataclassesr   	functoolsr   typingr   r   r   ZtorchZ	vllm.envsr>   Z vllm.attention.backends.abstractr   Zvllm.loggerr	   Zvllm.platformsr
   r   Z
vllm.utilsr   r   r&   loggerr0   r   r   r   r*   r   r   r   r2   intr"   r)   r6   r?   r=   rE   r   r   r   r   <module>   s   

4
	/