
    wia                        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eeeef         Z G 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ZddedefdZdS )    Nwraps)OptionalUnion   )get_profiling_on)protonc            
       |    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Z	d Z
d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
propertiesreturnc                 0    || _         || _        || _        d S N)r   r   r   )selfr   r   r   s       e/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/triton/profiler/scope.py__init__zscope.__init__%   s    	$    c                 ,   t                      s| S t          j                    | _        t          j        | j        | j                   | j        rt          j        | j        | j                   | j        rt          j	        | j        | j                   | S r   )
r   	libprotonrecord_scopeidenter_scoper   r   add_metricsr   set_properties)r   s    r   	__enter__zscope.__enter__+   s    !! 	K(**dgty111< 	9!$'4<888? 	?$TWdo>>>r   c                 d    t                      sd S t          j        | j        | j                   d S r   )r   r   
exit_scoper   r   )r   exc_type	exc_value	tracebacks       r   __exit__zscope.__exit__6   s2    !! 	FTWdi00000r   c                 @     t                     fd            }|S )Nc                  b   t                      rot          j                    }t          j        |j                   j        rt          j        |j                   j        rt          j        |j                    | i |}t                      rt          j	        |j                   |S r   )
r   r   r   r   r   r   r   r   r   r   )argskwargsr   retfuncr   s       r   wrapperzscope.__call__.<locals>.wrapper=   s    !! B+--%b$)444< <)"dl;;;? B,RAAA$'''C!! 4$R333Jr   r   )r   r)   r*   s   `` r   __call__zscope.__call__;   s9    	t	 	 	 	 	 
	 r   )NN)r   N)__name__
__module____qualname____doc__strr   dictMetricValueTypePropertyValueTyper   r   r#   r+    r   r   r   r      s         , SWFJ% %S %8Do9M4N+O %%d30A+A&BC%OS% % % %	 	 	1 1 1 1
    r   r   F)	triton_opr   r   r   r5   r   r   r   c                   t                      sdS t          j                    }t          t          d          sg t          _        t          j                            || f           |rt          j        ||            nt          j        ||            |rt          j	        ||           |rt          j
        ||           |S )Nscopes)r   r   r   hasattrthread_local_scopesr8   appendenter_opr   r   r   )r   r5   r   r   r   s        r   r   r   N   s     r			!	!B&11 (%'"%%r4j111 (2t$$$$b$''' +b'*** 1 Z000Ir   c                     t                      sdS t          j                                        \  }}| rt	          j        ||           nt	          j        ||           |S )Nr7   )r   r:   r8   popr   exit_opr   )r5   r   r   s      r   r   r   a   sc     r")--//HB '"d####R&&&Ir   )F)	threading	functoolsr   typingr   r   flagsr   triton._C.libprotonr	   r   localr:   floatintr2   r0   r3   r   boolr1   r   r   r4   r   r   <module>rI      sV             " " " " " " " " # # # # # # 3 3 3 3 3 3%io'' s
#%c/* = = = = = = = =@ 16gkEI  c  cSbNbIc@d $T#/@*@%ABNQ   & $ 3      r   