
    ^j              	       2   d dl Z d dlZd dlmZ d dlmZmZmZmZ ddl	m	Z	 ddl
mZmZ d dlmZ  e j                          Zeeee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AnySequence   )flags)transform_tensor_metricsset_metric_kernels)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      `/var/www/ramen.bs-engineer-server.com/venv/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                t        j                  | j                  gt        | j                          y y r   )r
   profiling_on	libprotonrecord_scoper   enter_scoper   r   r   add_metricsr   r   s    r   _enter_scopezscope._enter_scope+   si    !!((*dggtyy1<< !!$''S,DT\\,RS 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_scope4   s.    !!TWW_TWWdii0r   c                 &    | j                          | S r   )r"   r!   s    r   	__enter__zscope.__enter__9   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>.wrapperB   s:    #T,V,  "  "s   + =r   )r   r1   r2   s   `` r   __call__zscope.__call__@   s     	t	# 
	# r   r   )__name__
__module____qualname____doc__strr   dictr   r   r"   r%   r'   r,   r3    r   r   r   r      sH    ,S 8DcN+C t 
T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__V   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   rB   s    r   r"   zcpu_timed_scope._enter_scope\   s'    !!,,.r   c                     t         j                  sy | j                  Ct        j                         | j                  z
  }t        j                  | j                  d|i       t        | %          y )Nzcpu_time (ns)(exc))
r
   r   r@   rD   rE   r   r    r   r?   r%   )r   r>   rB   s     r   r%   zcpu_timed_scope._exit_scopeb   sR    !!??&||~7H!!$'',@(+KLr   r   )r4   r5   r6   r7   r8   r   r9   r   r   r"   r%   __classcell__)rB   s   @r   r<   r<   M   sB    HS H8DcN+C Ht H r   r<   )r   r   r   r   c                H   t         j                  sy t        j                         }t	        t
        dg       t
        _        t
        j                  j                  || f       t        j                  ||        |r)t                t        j                  |gt        |        |S )Nscopes)r
   r   r   r   getattrthread_local_scopesrI   appendr   r   r    r   r   s      r   r   r   k   s~    				!B!()<h!K%%r4j1"d#bE#;G#DEIr   c                $   t         j                  sy t        j                  j	                         \  }}| r| |k7  rt        d|  d|       | s|} |r)t                t        j                  |gt        |        t        j                  ||        |S )NzScope name mismatch: z != )r
   r   rK   rI   poprA   r   r   r    r   r$   )r   r   r   popped_names       r   r$   r$   x   s    )00446OB#0d;-HIIbE#;G#DET"Ir   r   )	threadingrD   	functoolsr   typingr   r   r   r   r
   metricr   r   triton._C.libprotonr   r   localrK   floatintMetricValueTyper   r<   r8   r9   r   r$   r:   r   r   <module>rY      s       1 1  @ 3%ioo' sHUOXc]BC; ;|e < CG 
c 
xS#X'? 
8TW= 
RV Xc] xS#X7O [cdg[h r   