a
    dx                     @   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                   @   s0  e Zd ZdZd)eeeef  ee ddddZe	eddddZ
e	eddd	d
ZedddZeeedddZeeddddZd*ee eeedddZddddZddddZeeef edddZd+eee ee ddd d!Zee dd"d#d$Zddd%d&Zeedd'd(ZdS ),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   m/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/profilers/profiler.py__init__   s    zProfiler.__init__)action_namer   c                 C   s   dS )z)Defines how to start recording an action.Nr   r   r   r   r   r   start+   s    zProfiler.startc                 C   s   dS )z>Defines how to record the duration once an action is complete.Nr   r   r   r   r   stop/   s    zProfiler.stop)r   c                 C   s   dS )N r   r   r   r   r   summary3   s    zProfiler.summaryc              	   c   s0   z|  | |V  W | | n| | 0 dS )aF  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   profile6   s    
zProfiler.profile)argskwargsr   c                 O   s   | j dv rtj|i | d S )NNr   )r   loginfo)r   r#   r$   r   r   r   _rank_zero_infoH   s    
zProfiler._rank_zero_info.txt-)r   	extensionsplit_tokenr   c                 C   sj   g }| j d ur|| j  | jr,|| j | jd urF|t| j |d urX|| ||| }|S r   )r   appendr   r   strjoin)r   r   r+   r,   r#   r   r   r   r   _prepare_filenameL   s    


zProfiler._prepare_filenamec                 C   sn   | j d urd S | jrb| jrbtj| j|  }t|}|j| jdd |	|d}|| _
|j| _ n| j| _ d S )NT)exist_oka)r   r   r   ospathr/   r0   r   Z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   describeh   s    


zProfiler.describe)statsr   c                 C   s~   | j d ur| j   dnd}|d g}| D ]@\}}d| }| jd ur\|d| j 7 }|| || q0tj|S )N r   zProfiler ReportzProfile stats for: z rank: )r   upperitemsr   r-   r3   linesepr/   )r   r@   r<   outputactionvalueheaderr   r   r   _stats_to_stru   s    



zProfiler._stats_to_str)r<   
local_ranklog_dirr   c                 C   s   || _ || _| jp|| _dS )z-Execute arbitrary pre-profiling set-up steps.N)r   r   r   )r   r<   rJ   rK   r   r   r   setup   s    zProfiler.setup)r<   r   c                 C   s$   d| _ | jdur | j  d| _dS )znExecute 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   rJ      s    zProfiler.local_rank)NN)Nr)   r*   )NN)__name__
__module____qualname____doc__r
   r   r.   r   r   r   r   r   r!   r   r	   r"   r   r(   r0   r:   r?   r   rI   intrL   r>   rN   propertyrJ   r   r   r   r   r      s:      

r   )rR   loggingr3   abcr   r   
contextlibr   pathlibr   typingr   r   r   r	   r
   r   r   Z#lightning_fabric.utilities.cloud_ior   	getLoggerrO   r&   r   r   r   r   r   <module>   s   $
