U
    ¡d¨i5  ã                   @   sº   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eeeef ZG dd„ dƒZd	ddd
œeeeeeef  eeeef  edœdd„Zdeedœdd„ZdS )é    N©Úwraps)ÚOptionalÚUnioné   )Úget_profiling_on)Úprotonc                   @   s\   e Zd ZdZdeeeeef  eeeef  ddœdd„Z	dd„ Z
ddœ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)
        ```

        decoarator:
        ```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Ú
propertiesÚreturnc                 C   s   || _ || _|| _d S ©N)Ú_nameÚ_metricsÚ_properties)Úselfr
   r   r   © r   ú9/tmp/pip-unpacked-wheel-8bvfr03u/triton/profiler/scope.pyÚ__init__%   s    zscope.__init__c                 C   sT   t ƒ s
| S t ¡ | _t | j| j¡ | jr:t | j| j¡ | jrPt 	| j| j¡ | S r   )
r   Ú	libprotonÚrecord_scopeÚ_idÚenter_scoper   r   Úadd_metricsr   Úset_properties)r   r   r   r   Ú	__enter__+   s    
zscope.__enter__)r   c                 C   s   t ƒ s
d S t | j| j¡ d S r   )r   r   Ú
exit_scoper   r   )r   Úexc_typeÚ	exc_valueÚ	tracebackr   r   r   Ú__exit__6   s    zscope.__exit__c                    s   t ˆ ƒ‡ ‡fdd„ƒ}|S )Nc                     sf   t ƒ rDt ¡ }t |ˆj¡ ˆjr0t |ˆj¡ ˆjrDt |ˆj¡ ˆ | |Ž}t ƒ rbt 	|ˆj¡ |S r   )
r   r   r   r   r   r   r   r   r   r   )ÚargsÚkwargsÚidÚret©Úfuncr   r   r   Úwrapper=   s    
zscope.__call__.<locals>.wrapperr   )r   r'   r(   r   r&   r   Ú__call__;   s    zscope.__call__)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   ÚdictÚMetricValueTypeÚPropertyValueTyper   r   r!   r)   r   r   r   r   r	      s     ÿ ÿr	   F)Ú	triton_opr   r   )r
   r2   r   r   r   c                C   st   t ƒ s
dS t ¡ }ttdƒs"g t_tj || f¡ |rDt || ¡ nt || ¡ |r`t 	||¡ |rpt 
||¡ |S )NéÿÿÿÿÚscopes)r   r   r   ÚhasattrÚ_localr4   ÚappendZenter_opr   r   r   )r
   r2   r   r   r$   r   r   r   r   N   s    
r   )r2   r   c                 C   s:   t ƒ s
dS tj ¡ \}}| r*t ||¡ nt ||¡ |S )Nr3   )r   r6   r4   Úpopr   Zexit_opr   )r2   r$   r
   r   r   r   r   a   s    r   )F)Ú	threadingÚ	functoolsr   Útypingr   r   Úflagsr   Ztriton._C.libprotonr   r   Úlocalr6   ÚfloatÚintr0   r.   r1   r	   Úboolr/   r   r   r   r   r   r   Ú<module>   s   @ÿ ÿ