o
    Di                  	   @   s   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  Zeeef ZG dd dZG d	d
 d
eZdddedeeeef  dee fddZddddee deeeef  dee fddZdS )    Nwraps)OptionalUnion   )flags)protonc                   @   s\   e Zd ZdZddedeeeef  ddfddZdd	 Z	d
d Z
dd Zdd Zd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                 C   s   || _ || _d | _d S Nr
   r   idselfr
   r    r   _/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/triton/profiler/scope.py__init__%   s   
zscope.__init__c                 C   sB   t jsd S t | _t| j| j | jrt| j| j d S d S r   )	r   profiling_on	libprotonrecord_scoper   enter_scoper
   r   add_metricsr   r   r   r   _enter_scope*   s   
zscope._enter_scopec                 C   s(   t jr| jd u r
d S t| j| j d S r   )r   r   r   r   
exit_scoper
   r   r   r   r   _exit_scope2   s   zscope._exit_scopec                 C   s   |    | S r   )r   r   r   r   r   	__enter__7   s   zscope.__enter__c                 C   s   |    d S r   )r   )r   exc_type	exc_value	tracebackr   r   r   __exit__;   s   zscope.__exit__c                    s   t   fdd}|S )Nc                     s,      z | i |W   S   w r   )r   r   )argskwargsfuncr   r   r   wrapper@   s   zscope.__call__.<locals>.wrapperr   )r   r&   r'   r   r%   r   __call__>   s   zscope.__call__r   )__name__
__module____qualname____doc__strr   dictMetricValueTyper   r   r   r   r"   r(   r   r   r   r   r	      s    $r	   c                       sT   e Zd ZdZddedeeeef  ddf fddZ fdd	Z	 fd
dZ
  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                    s0   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
ValueErrorr   	__class__r   r   r   T   s
   zcpu_timed_scope.__init__c                    s"   t jsd S t | _t   d S r   )r   r   timetime_nsr3   r2   r   r   r5   r   r   r   Z   s   
zcpu_timed_scope._enter_scopec                    sF   t jsd S t   | jd ur!t | j }t| j	d|i d S d S )Nzcpu_time (ns)(exc))
r   r   r2   r   r3   r7   r8   r   r   r   )r   r1   r5   r   r   r   `   s   

zcpu_timed_scope._exit_scoper   )r)   r*   r+   r,   r-   r   r.   floatr   r   r   __classcell__r   r   r5   r   r0   K   s
    (r0   )r   r
   r   r   c                C   sP   t jsd S t }ttdg t_tj|| f t||  |r&t	|| |S )Nscopes)
r   r   r   r   getattrthread_local_scopesr;   appendr   r   r   r   r   r   r   i   s   r   c                C   s`   t jsd S tj \}}| r| |krtd|  d| | s |} t||  |r.t|| |S )NzScope name mismatch: z != )	r   r   r=   r;   popr4   r   r   r   )r
   r   r   popped_namer   r   r   r   u   s   r   r   )	threadingr7   	functoolsr   typingr   r   r   triton._C.libprotonr   r   localr=   r9   intr/   r	   r0   r-   r.   r   r   r   r   r   r   <module>   s    =,6