o
    ^i                     @  sr   U d dl mZ d dlmZmZ d dlmZmZmZ d dl	m
Z
 e
dddZded	< G d
d dZG dd dZdS )    )annotations)CallableOptional)Executoras_completedFuture)
ContextVarasync_compile_active_modeN)defaultz&ContextVar[Optional[AsyncCompileMode]]active_modec                   @  s   e Zd Zd
ddZdd Zd	S )FutureKernelfinalize_compiler   futurer   c                 C  s   || _ d | _|| _d S N)r   kernelr   )selfr   r    r   X/var/www/html/RAG/RAG_venv/lib/python3.10/site-packages/triton/runtime/_async_compile.py__init__      
zFutureKernel.__init__c                 C  s.   | j d ur| j S | j }| | || _ |S r   )r   r   resultr   )r   r   r   r   r   r      s   


zFutureKernel.resultN)r   r   r   r   )__name__
__module____qualname__r   r   r   r   r   r   r   	   s    
r   c                   @  s.   e Zd ZdddZdd Zdd Zd	d
 ZdS )AsyncCompileModeexecutorr   c                 C  s   || _ g | _i | _d S r   )r   raw_futuresfuture_kernels)r   r   r   r   r   r      r   zAsyncCompileMode.__init__c                 C  sN   | j |}|d ur|S | j|}||_| j| t||}|| j |< |S r   )r   getr   submit_keyr   appendr   )r   key
compile_fnfinalize_fnr   future_kernelr   r   r   r   !   s   

zAsyncCompileMode.submitc                 C  s"   t  d ur
tdt |  | S )Nz*Another AsyncCompileMode is already active)r   r   RuntimeErrorset)r   r   r   r   	__enter__-   s   
zAsyncCompileMode.__enter__c                 C  s.   t | jD ]
}| j|j   qtd  d S r   )r   r   r   r    r   r   r'   )r   exc_type	exc_value	tracebackr   r   r   r   __exit__3   s   zAsyncCompileMode.__exit__N)r   r   )r   r   r   r   r   r(   r,   r   r   r   r   r      s
    
r   )
__future__r   typingr   r   concurrent.futuresr   r   r   contextvarsr   r   __annotations__r   r   r   r   r   r   <module>   s    