
    nVhM              
           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y)    Nwraps)OptionalUnion   )get_profiling_on)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      E/home/dcms/DCMS/lib/python3.12/site-packages/triton/profiler/scope.py__init__zscope.__init__%   s    	    c                    t               sy t        j                         | _        t        j                  | j                  | j
                         | j                  r+t        j                  | j                  | j                         y y r   )r   	libprotonrecord_scoper   enter_scoper   r   add_metricsr   s    r   _enter_scopezscope._enter_scope*   sW    !((*dggtyy1<<!!$''4<<8 r   c                     t               r| j                  y t        j                  | j                  | j                         y 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                 j    t               sy t        j                         | _        t        |           y r   )r   timetime_nsr;   r:   r   )r   r=   s    r   r   zcpu_timed_scope._enter_scopeZ   s$    !,,.r   c                     t               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?   r@   r   r   r   )r   r9   r=   s     r   r   zcpu_timed_scope._exit_scope`   sT    !??&||~7H!!$'',@(+KL 'r   r   )r.   r/   r0   r1   r2   r   r3   floatr   r   r   __classcell__)r=   s   @r   r7   r7   K   sF    HS H8De4D+E HQU HM Mr   r7   F)	triton_opr   r   rD   r   r   c                H   t               syt        j                         }t        t        dg       t        _        t        j
                  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    				!B!()<h!K%%r4j12t$b$'b'*Ir   c                     t               syt        j                  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^    "))--/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%ioo' s
#: :zMe M< 16gk c  cSbNbIc@d ps 
$ 
$sO?S:T1U 
ad 
r   