o
    oÇhM  ã                
   @   sÌ   d dl Z d dlZd dlmZ d dlmZmZ ddlmZ d dl	m
Z e  ¡ Zeeef ZG dd„ dƒZG d	d
„ d
eƒZdddœdededeeeef  defdd„Zddedeeeef  defdd„ZdS )é    N©Úwraps)ÚOptionalÚUnioné   )Úget_profiling_on)Úprotonc                   @   s\   e Zd ZdZddedeeeef  ddfdd„Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zdd„ ZdS )Úscopea  
    A context manager and decorator for entering and exiting a scope.

    Usage:
        context manager:
        ```python
        with proton.scope("test0", {metric_name: metric_value}):
            foo[1,](x, y)
        ```

        decorator:
        ```python
        @proton.scope("test0", {metric_name: metric_value})
        def foo(x, y):
            ...
        ```

    Args:
        name (str): The name of the scope.
        metrics (dict[str, float], optional): The metrics of the scope. Default is None.
    NÚnameÚmetricsÚreturnc                 C   s   || _ || _d | _d S ©N)r
   r   Úid©Úselfr
   r   © r   úi/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/triton/profiler/scope.pyÚ__init__%   s   
zscope.__init__c                 C   sB   t ƒ sd S t ¡ | _t | j| j¡ | jrt | j| j¡ d S d S r   )r   Ú	libprotonÚrecord_scoper   Úenter_scoper
   r   Úadd_metrics©r   r   r   r   Ú_enter_scope*   s   
ÿzscope._enter_scopec                 C   s(   t ƒ r| jd u r
d S t | j| j¡ d S r   )r   r   r   Ú
exit_scoper
   r   r   r   r   Ú_exit_scope2   s   zscope._exit_scopec                 C   s   |   ¡  | S r   )r   r   r   r   r   Ú	__enter__7   s   zscope.__enter__c                 C   s   |   ¡  d S r   )r   )r   Úexc_typeÚ	exc_valueÚ	tracebackr   r   r   Ú__exit__;   s   zscope.__exit__c                    s   t ˆ ƒ‡ ‡fdd„ƒ}|S )Nc                     s,   ˆ  ¡  zˆ | i |¤ŽW ˆ ¡  S ˆ ¡  w r   )r   r   )ÚargsÚkwargs©Úfuncr   r   r   Úwrapper@   s   zscope.__call__.<locals>.wrapperr   )r   r$   r%   r   r#   r   Ú__call__>   s   zscope.__call__r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   ÚdictÚMetricValueTyper   r   r   r   r    r&   r   r   r   r   r	      s    $r	   c                       sT   e Zd ZdZddedeeeef  ddf‡ fdd„Z‡ fdd	„Z	‡ fd
d„Z
‡  ZS )Úcpu_timed_scopezÈ
    A scope that measures elapsed time (cpu_time).

    Args:
        name (str): The name of the scope.
        metrics (dict[str, float], optional): Additional metrics to add. Default is None.
    Nr
   r   r   c                    s0   t ƒ  ||¡ d | _|rd|v rtdƒ‚d S d S )NÚcpu_timez'The metric name 'cpu_time' is reserved.)Úsuperr   Ú
start_timeÚ
ValueErrorr   ©Ú	__class__r   r   r   T   s
   ÿzcpu_timed_scope.__init__c                    s"   t ƒ sd S t ¡ | _tƒ  ¡  d S r   )r   ÚtimeÚtime_nsr1   r0   r   r   r3   r   r   r   Z   s   
zcpu_timed_scope._enter_scopec                    sF   t ƒ sd S tƒ  ¡  | jd ur!t ¡ | j }t | jd|i¡ d S d S )Nzcpu_time (ns)(exc))	r   r0   r   r1   r5   r6   r   r   r   )r   r/   r3   r   r   r   `   s   

þzcpu_timed_scope._exit_scoper   )r'   r(   r)   r*   r+   r   r,   Úfloatr   r   r   Ú__classcell__r   r   r3   r   r.   K   s
    (r.   F)Ú	triton_opr   r
   r9   r   r   c                C   sb   t ƒ sdS t ¡ }ttdg ƒt_tj || f¡ |r!t || ¡ nt || ¡ |r/t 	||¡ |S )NéÿÿÿÿÚscopes)
r   r   r   ÚgetattrÚthread_local_scopesr;   ÚappendÚenter_opr   r   )r
   r9   r   r   r   r   r   r   i   s   r   c                 C   sJ   t ƒ sdS tj ¡ \}}| rt ||¡ nt ||¡ |r#t ||¡ |S )Nr:   )r   r=   r;   Úpopr   Úexit_opr   r   )r9   r   r   r
   r   r   r   r   x   s   r   )FN)Ú	threadingr5   Ú	functoolsr   Útypingr   r   Úflagsr   Útriton._C.libprotonr   r   Úlocalr=   r7   Úintr-   r	   r.   r+   Úboolr,   r   r   r   r   r   r   Ú<module>   s    =.(