o
    oi_                     @   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mZ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)contextmanager)Path)AnyCallableDict	Generator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   X/home/ubuntu/.local/lib/python3.10/site-packages/lightning/pytorch/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   stop0   r   zProfiler.stopc                 C   r   )N r   r   r   r   r   summary4   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   profile7   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_infoJ   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_filenameN   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pathr1   r2   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   describei   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/   r5   linesepr1   )r   rC   r?   outputactionvalueheaderr   r   r   _stats_to_strv   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?   rM   rN   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   rA      s
   


zProfiler.teardownc                 C   s   | j | jd d S )Nr>   )rA   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   rM      s   zProfiler.local_rank)NN)Nr+   r,   )r   N)__name__
__module____qualname____doc__r
   r   r0   r   r   r   r   r    r#   r   r	   r$   r   r*   r2   r=   rB   r   rL   intrO   rA   rQ   propertyrM   r   r   r   r   r      sJ    



$
r   )rU   loggingr5   abcr   r   
contextlibr   pathlibr   typingr   r   r   r	   r
   r   r   #lightning.fabric.utilities.cloud_ior   	getLoggerrR   r(   r   r   r   r   r   <module>   s   $
