
    XhM              
           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 j                    Zeeef         Z G d d          Z G d	 d
e          Zddddededeeeef                  defdZddedeeeef                  defdZdS )    Nwraps)OptionalUnion   )get_profiling_on)protonc                   b    e Zd ZdZddedeeeef                  ddfdZd Z	d Z
d	 Zd
 Z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                 0    || _         || _        d | _        d S N)r   r   id)selfr   r   s      g/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/triton/profiler/scope.py__init__zscope.__init__%   s    	    c                     t                      sd S t          j                    | _        t          j        | j        | j                   | j        r!t          j        | j        | j                   d S d S r   )r   	libprotonrecord_scoper   enter_scoper   r   add_metricsr   s    r   _enter_scopezscope._enter_scope*   sm    !! 	F(**dgty111< 	9!$'4<88888	9 	9r   c                 r    t                      r| j        d S t          j        | j        | j                   d S r   )r   r   r   
exit_scoper   r   s    r   _exit_scopezscope._exit_scope2   s8    !! 	TW_FTWdi00000r   c                 .    |                                   | S r   )r   r   s    r   	__enter__zscope.__enter__7   s    r   c                 .    |                                   d S r   )r   )r   exc_type	exc_value	tracebacks       r   __exit__zscope.__exit__;   s    r   c                 @     t                     fd            }|S )Nc                                                        	  | i |                                 S #                                  w xY wr   )r   r   )argskwargsfuncr   s     r   wrapperzscope.__call__.<locals>.wrapper@   sY    #tT,V,,  """"  """"s	   3 A	r   )r   r+   r,   s   `` r   __call__zscope.__call__>   s9    	t	# 	# 	# 	# 	# 
	# r   r   )__name__
__module____qualname____doc__strr   dictMetricValueTyper   r   r   r!   r&   r-    r   r   r   r      s         , S 8Do9M4N+O [_    
9 9 91 1 1
    
 
 
 
 
r   r   c                   b     e Zd ZdZd	dedeeeef                  ddf fdZ fdZ	 fdZ
 x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                     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
ValueError)r   r   r   	__class__s      r   r   zcpu_timed_scope.__init__T   sX    w''' 	HzW,,FGGG	H 	H,,r   c                     t                      sd S t          j                    | _        t	                                                       d S r   )r   timetime_nsr;   r:   r   )r   r=   s    r   r   zcpu_timed_scope._enter_scopeZ   s?    !! 	F,..r   c                     t                      sd S t                                                       | j        9t	          j                    | j        z
  }t          j        | j        d|i           d S d S )Nzcpu_time (ns)(exc))	r   r:   r   r;   r?   r@   r   r   r   )r   r9   r=   s     r   r   zcpu_timed_scope._exit_scope`   so    !! 	F?&|~~7H!$',@(+KLLLLL '&r   r   )r.   r/   r0   r1   r2   r   r3   floatr   r   r   __classcell__)r=   s   @r   r7   r7   K   s         H HS H8De4D+E HQU H H H H H H    M M M M M M M M Mr   r7   F)	triton_opr   r   rD   r   r   c                V   t                      sdS t          j                    }t          t          dg           t          _        t          j                            || f           |rt          j        ||            nt          j        ||            |rt          j	        ||           |S )Nscopes)
r   r   r   getattrthread_local_scopesrG   appendenter_opr   r   )r   rD   r   r   s       r   r   r   i   s     r			!	!B!()<h!K!K%%r4j111 (2t$$$$b$''' +b'***Ir   c                     t                      sdS t          j                                        \  }}| rt	          j        ||           nt	          j        ||           |rt	          j        ||           |S )NrF   )r   rI   rG   popr   exit_opr   r   )rD   r   r   r   s       r   r   r   x   s~     r")--//HB '"d####R&&& +b'***Ir   )FN)	threadingr?   	functoolsr   typingr   r   flagsr   triton._C.libprotonr	   r   localrI   rB   intr4   r   r7   r2   boolr3   r   r   r5   r   r   <module>rW      s|              " " " " " " " " # # # # # # 3 3 3 3 3 3%io'' s
#: : : : : : : :zM M M M Me M M M< 16gk   c  cSbNbIc@d ps    
 
$ 
$sO?S:T1U 
ad 
 
 
 
 
 
r   