o
    8wit                     @   s   d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZmZmZmZmZ ddlmZ eeZG d	d
 d
eZdS )z<Profiler to check if there are any bottlenecks in your code.    N)ABCabstractmethod)	Generator)contextmanager)Path)AnyCallableOptionalTextIOUnion)get_filesystemc                	   @   sH  e Zd ZdZ		d-deeeef  dee ddfddZe	deddfd	d
Z
e	deddfddZdefddZededefddZdededdfddZ	d.dee dededefddZd/ddZd/ddZdeeef defd d!Zd-d"ed#ee d$ee ddfd%d&Zd"ee ddfd'd(Zd/d)d*Zedefd+d,ZdS )0ProfilerzKIf you wish to write a custom profiler, you should inherit from this class.Ndirpathfilenamereturnc                 C   s(   || _ || _d | _d | _d | _d | _d S N)r   r   _output_file_write_stream_local_rank_stage)selfr   r    r   a/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/pytorch_lightning/profilers/profiler.py__init__    s   
zProfiler.__init__action_namec                 C      dS )z)Defines how to start recording an action.Nr   r   r   r   r   r   start-       zProfiler.startc                 C   r   )z>Defines how to record the duration once an action is complete.Nr   r   r   r   r   stop1   r   zProfiler.stopc                 C   r   )N r   r   r   r   r   summary5   s   zProfiler.summaryc              	   c   s0    z|  | |V  W | | dS | | w )aG  Yields a context manager to encapsulate the scope of a profiled action.

        Example::

            with self.profile('load training data'):
                # load training data code

        The profiler will start once you've entered the context and will automatically
        stop once you exit the code block.

        N)r   r   r   r   r   r   profile8   s
   
zProfiler.profileargskwargsc                 O   s"   | j dv rtj|i | d S d S )NNr   )r   loginfo)r   r$   r%   r   r   r   _rank_zero_infoK   s   
zProfiler._rank_zero_info.txt-	extensionsplit_tokenc                 C   sf   g }| j d ur|| j  | jr|| j | jd ur#|t| j |d ur,|| ||| S r   )r   appendr   r   strjoin)r   r   r,   r-   r$   r   r   r   _prepare_filenameO   s   


zProfiler._prepare_filenamec                 C   sp   | j d urd S | jr2| jr2tj| j|  }t|}|j| jdd |	|d}|| _
|j| _ d S | j| _ d S )NT)exist_oka)r   r   r   ospathr0   r1   r   mkdirsopenr   writer)   )r   filepathfsfiler   r   r   _prepare_streams]   s   
zProfiler._prepare_streamsc                 C   sN   |    |  }|r| jdur| | | jdur| j  | j| jd dS )z2Logs a profile report after the conclusion of run.Nstage)r<   r"   r   r   flushteardownr   )r   r"   r   r   r   describej   s   


zProfiler.describestatsc                 C   s~   | j d ur| j   dnd}|d g}| D ] \}}d| }| jd ur.|d| j 7 }|| || qtj|S )N r    zProfiler ReportzProfile stats for: z rank: )r   upperitemsr   r.   r4   linesepr0   )r   rB   r>   outputactionvalueheaderr   r   r   _stats_to_strw   s   



zProfiler._stats_to_strr>   
local_ranklog_dirc                 C   s   || _ || _| jp
|| _dS )z-Execute arbitrary pre-profiling set-up steps.N)r   r   r   )r   r>   rL   rM   r   r   r   setup   s   zProfiler.setupc                 C   s(   d| _ | jdur| j  d| _dS dS )zoExecute arbitrary post-profiling tear-down steps.

        Closes the currently open file and stream.

        N)r   r   close)r   r>   r   r   r   r@      s
   


zProfiler.teardownc                 C   s   | j | jd d S )Nr=   )r@   r   r!   r   r   r   __del__   s   zProfiler.__del__c                 C   s   | j d u rdS | j S r&   )r   r!   r   r   r   rL      s   zProfiler.local_rank)NN)Nr*   r+   )r   N)__name__
__module____qualname____doc__r	   r   r/   r   r   r   r   r   r"   r   r   r#   r   r)   r1   r<   rA   dictrK   intrN   r@   rP   propertyrL   r   r   r   r   r      sJ    



$
r   )rT   loggingr4   abcr   r   collections.abcr   
contextlibr   pathlibr   typingr   r   r	   r
   r   #lightning_fabric.utilities.cloud_ior   	getLoggerrQ   r'   r   r   r   r   r   <module>   s   
