o
    )iF                     @   s   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	 d dl
mZ d dlmZ d dlmZ eeZe jG dd dZG d	d
 d
ZdS )    N)AnyCallable)VllmBackend)end_monitoring_torch_compile)
VllmConfig)init_loggerc                   @   s.   e Zd ZU eed< dZeed< dZeed< dS )ConcreteSizeEntryruntime_shapeFcompiledNrunnable)	__name__
__module____qualname__int__annotations__r
   boolr   r    r   r   s/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/vllm/compilation/cuda_piecewise_backend.pyr      s   
 r   c                   @   sN   e Zd Zdejdedededee dede	fdd	Z
d
d ZdefddZdS )PiecewiseBackendgraphvllm_configpiecewise_compile_indextotal_piecewise_compilessym_shape_indices compiled_graph_for_general_shapevllm_backendc           	      C   s   || _ || _|j| _|| _|| _|| _|dk| _||d k| _|dk| _t	| jj
| _
d| _|| _|| _tjdk| _i | _| j
 | _| j
D ]}t|| jd| j|< qFdS )aM  
        The backend for piecewise compilation.
        It mainly handles the compilation of static shapes and 
        dispatching based on runtime shape.

        We will compile `self.graph` once for the general shape,
        and then compile for different shapes specified in
        `compilation_config.compile_sizes`.
        r      FDEBUG)r	   r   N)r   r   compilation_configr   r   r   Zis_first_graphis_last_graphZis_full_graphsetZcompile_sizesfirst_run_finishedr   r   envsZVLLM_LOGGING_LEVELZis_debugging_modeconcrete_size_entriescopyto_be_compiled_sizesr   )	selfr   r   r   r   r   r   r   shaper   r   r   __init__   s2   



zPiecewiseBackend.__init__c                 C   s.   | j r| js| jj  t| j d S d S d S )N)r   r%   r   compiler_managerZsave_to_filer   r   )r&   r   r   r   check_for_ending_compilationO   s   z-PiecewiseBackend.check_for_ending_compilationreturnc              	   G   s   | j sd| _ |   | j| S || jd  }|| jvr | j| S | j| }|jsPd|_| j| | jj	j
| j|| jj| j| j| j|d|_| jrP| jsP|   |j| S )NTr   )Zgraph_indexZ
num_graphsr	   )r!   r*   r   r   r#   r
   r%   remover   r)   compiler   r   Zinductor_compile_configr   r   r   r   )r&   argsr	   entryr   r   r   __call__V   s.   





zPiecewiseBackend.__call__N)r   r   r   fxZGraphModuler   r   listr   r   r(   r*   r   r0   r   r   r   r   r      s    
4r   )dataclassestypingr   r   Ztorch.fxr1   Z	vllm.envsr"   Zvllm.compilation.backendsr   Zvllm.compilation.monitorr   Zvllm.configr   Zvllm.loggerr   r   logger	dataclassr   r   r   r   r   r   <module>   s   