
    -i              	       
   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edeeeef      dee   fdZddddee   deeeef      dee   fdZy)    Nwraps)OptionalUnion   )flags)protonc                   T    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y)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                 .    || _         || _        d | _        y Nr   r   id)selfr   r   s      _/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/triton/profiler/scope.py__init__zscope.__init__%   s    	    c                    t         j                  sy t        j                         | _        t        j
                  | j                  | j                         | j                  r+t        j                  | j                  | j                         y y r   )	r   profiling_on	libprotonrecord_scoper   enter_scoper   r   add_metricsr   s    r   _enter_scopezscope._enter_scope*   sZ    !!((*dggtyy1<<!!$''4<<8 r   c                     t         j                  r| j                  y t        j                  | j                  | j
                         y r   )r   r   r   r   
exit_scoper   r   s    r   _exit_scopezscope._exit_scope2   s.    !!TWW_TWWdii0r   c                 &    | j                          | S r   )r   r   s    r   	__enter__zscope.__enter__7   s    r   c                 $    | j                          y r   )r!   )r   exc_type	exc_value	tracebacks       r   __exit__zscope.__exit__;   s    r   c                 2     t               fd       }|S )Nc                      j                          	  | i |j                          S # j                          w xY wr   )r   r!   )argskwargsfuncr   s     r   wrapperzscope.__call__.<locals>.wrapper@   s:    #T,V,  "  "s   + =r   )r   r-   r.   s   `` r   __call__zscope.__call__>   s     	t	# 
	# r   r   )__name__
__module____qualname____doc__strr   dictMetricValueTyper   r   r!   r#   r(   r/    r   r   r   r      sJ    ,S 8Do9M4N+O [_ 
91

r   r   c                   V     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                 X    t         |   ||       d | _        |rd|v rt        d      y y )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   s6    w'zW,FGG -7r   c                 v    t         j                  sy t        j                         | _        t
        |           y r   )r   r   timetime_nsr=   r<   r   )r   r?   s    r   r   zcpu_timed_scope._enter_scopeZ   s'    !!,,.r   c                     t         j                  sy t        |           | j                  Dt        j                         | j                  z
  }t        j                  | j                  d|i       y y )Nzcpu_time (ns)(exc))
r   r   r<   r!   r=   rA   rB   r   r   r   )r   r;   r?   s     r   r!   zcpu_timed_scope._exit_scope`   sW    !!??&||~7H!!$'',@(+KL 'r   r   )r0   r1   r2   r3   r4   r   r5   floatr   r   r!   __classcell__)r?   s   @r   r9   r9   K   sF    HS H8De4D+E HQU HM Mr   r9   )r   r   r   r   c                "   t         j                  sy t        j                         }t	        t
        dg       t
        _        t
        j                  j                  || f       t        j                  ||        |rt        j                  ||       |S )Nscopes)
r   r   r   r   getattrthread_local_scopesrG   appendr   r   r   s      r   r   r   i   so    				!B!()<h!K%%r4j1"d#b'*Ir   c                    t         j                  sy t        j                  j	                         \  }}| r| |k7  rt        d|  d|       | s|} t        j                  ||        |rt        j                  ||       |S )NzScope name mismatch: z != )	r   r   rI   rG   popr>   r   r    r   )r   r   r   popped_names       r   r    r    u   sy    )00446OB#0d;-HIIT"b'*Ir   r   )	threadingrA   	functoolsr   typingr   r   r   triton._C.libprotonr	   r   localrI   rD   intr6   r   r9   r4   r5   r   r    r7   r   r   <module>rT      s       "  3%ioo' s
#: :zMe M< OS 	c 	xS/5I0J'K 	W_`cWd 	^b Xc] xS/EY@Z7[ gopsgt r   