o
    灛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
dededeeeef  deeeef  def
ddZddedefddZdS )    Nwraps)OptionalUnion   )get_profiling_on)protonc                
   @   sb   e Zd ZdZ		ddedeeeef  deeeef  ddfddZ	d	d
 Z
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   W/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/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r(t	| 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__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                     sj   t  r"t }t|j jrt|j jr"t|j  | i |}t  r3t	|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)r   N)__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 |r"t||  nt||  |r0t	|| |r8t
|| |S )Nscopes)r   r   r   hasattr_localr4   appendenter_opr   r   r   )r
   r2   r   r   r$   r   r   r   r   N   s   
r   c                 C   s<   t  sdS tj \}}| rt|| |S t|| |S )Nr3   )r   r6   r4   popr   exit_opr   )r2   r$   r
   r   r   r   r   a   s   r   )F)	threading	functoolsr   typingr   r   flagsr   triton._C.libprotonr   r   localr6   floatintr0   r.   r1   r	   boolr/   r   r   r   r   r   r   <module>   s"    @
