o
    ߗi                     @   sv   U d dl Z d dlmZmZmZmZmZ d dlmZ ee	e	ee
ef eee  ee gdf Zeed< G dd dZdS )    N)AnyCallableDictOptionalType)	TypeAlias
OnExitTypec                   @   s   e Zd ZdefddZdddZdeee  dee d	e	dd
fddZ
defddZdededd
fddZdede	dd
fddZdedede	dd
fddZdeee	f dd
fddZdeee	f dd
fddZdede	dd
fddZd
S ) MetricsContexton_exitc                 C   s   || _ i | _d| _d| _dS )aQ  
        Use this class as a contextmanager to create a context under which to accumulate
        a set of metrics, e.g., metrics gathered during a compilation. On exit of the
        contextmanager, call the provided 'on_exit' function and pass a dictionary of
        all metrics set during the lifetime of the contextmanager.
        r   N)_on_exit_metrics_start_time_ns_level)selfr
    r   [/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/_dynamo/metrics_context.py__init__   s   
zMetricsContext.__init__returnc                 C   s,   | j dkri | _t | _|  j d7  _ | S )z/
        Initialize metrics recording.
        r      )r   r   timetime_nsr   r   r   r   r   	__enter__   s
   

zMetricsContext.__enter__exc_type	exc_value
_tracebackNc                 C   sL   |  j d8  _ | j dksJ | j dkr$t }| | j|| j|| dS dS )z>
        At exit, call the provided on_exit function.
        r   r   N)r   r   r   r   r   r   )r   r   r   r   end_time_nsr   r   r   __exit__%   s   	
zMetricsContext.__exit__c                 C   s
   | j dkS )z4
        True if we've entered the context.
        r   )r   r   r   r   r   in_progress6   s   
zMetricsContext.in_progressmetricvaluec                 C   sD   | j dkrtd| d|| jvrd| j|< | j|  |7  < dS )z7
        Increment a metric by a given amount.
        r   zCannot increment  outside of a MetricsContextNr   RuntimeErrorr   r   r   r    r   r   r   	increment<   s
   


zMetricsContext.incrementc                 C   sB   | j dkrtd| d|| jv rtd| d|| j|< dS )z
        Set a metric to a given value. Raises if the metric has been assigned previously
        in the current context.
        r   Cannot set r!   zMetric 'z-' has already been set in the current contextNr"   r$   r   r   r   setF   s   


zMetricsContext.setkeyc                 C   s@   | j dkrtd| d|| jvri | j|< || j| |< dS )a=  
        Treats a give metric as a dictionary and set the k and value within it.
        Note that the metric must be a dictionary or not present.

        We allow this to be called multiple times (i.e. for features, it's not uncommon
        for them to be used multiple times within a single compilation).
        r   r&   r!   Nr"   )r   r   r(   r    r   r   r   set_key_valueS   s
   


zMetricsContext.set_key_valuevaluesc                 C   sH   | j dkr	td| j | @ }|rtd| d| j| dS )z
        Set multiple metrics directly. This method does NOT increment. Raises if any
        metric has been assigned previously in the current context.
        r   1Cannot update metrics outside of a MetricsContextz
Metric(s) z- have already been set in the current contextN)r   r#   r   keysupdate)r   r*   existingr   r   r   r-   a   s   

zMetricsContext.updatec                 C   s.   | j dkr	td| j dkr| | dS dS )zA
        Update, but only when at the outermost context.
        r   r+   r   N)r   r#   r-   )r   r*   r   r   r   update_outero   s
   

zMetricsContext.update_outerc                 C   sD   | j dkrtd| d|| jvrt | j|< | j| | dS )z8
        Records a metric as a set() of values.
        r   zCannot add r!   N)r   r#   r   r'   addr$   r   r   r   
add_to_setx   s
   

zMetricsContext.add_to_set)r   r	   )__name__
__module____qualname__r   r   r   r   r   BaseExceptionr   r   boolr   strintr%   r'   r)   r   r-   r/   r1   r   r   r   r   r	      s&    



	r	   )r   typingr   r   r   r   r   typing_extensionsr   r8   r7   r5   r   __annotations__r	   r   r   r   r   <module>   s   
  