
      i~                         d Z ddl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j*                  e      Z G d	 d
e      Zy)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            	       R   e Zd ZdZ	 	 ddeeeef      dee   ddfdZe	deddfd       Z
e	deddfd	       Zdefd
Zededefd       ZdededdfdZ	 	 ddee   dedefdZddZddZdeeef   defdZddedee   dee   ddfdZdee   ddfdZddZedefd       Zy)ProfilerzKIf you wish to write a custom profiler, you should inherit from this class.Ndirpathfilenamereturnc                 X    || _         || _        d | _        d | _        d | _        d | _        y )N)r   r   _output_file_write_stream_local_rank_stage)selfr   r   s      y/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning/pytorch/profilers/profiler.py__init__zProfiler.__init__!   s1    
  .215*.%)    action_namec                      y)z)Defines how to start recording an action.N r   r   s     r   startzProfiler.start.       r   c                      y)z>Defines how to record the duration once an action is complete.Nr   r   s     r   stopzProfiler.stop2   r!   r   c                      y)N r   r   s    r   summaryzProfiler.summary6   s    r   c              #      K   	 | j                  |       | | j                  |       y# | j                  |       w xY w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   s     r   profilezProfiler.profile9   s5     	#JJ{#IIk"DIIk"s   A+ A>Aargskwargsc                 L    | j                   dv rt        j                  |i | y y )NNr   )r   loginfo)r   r*   r+   s      r   _rank_zero_infozProfiler._rank_zero_infoL   s&    y(HHd%f% )r   	extensionc                    g }| j                   |j                  | j                          | j                  r|j                  | j                         | j                  $|j                  t	        | j                               ||j                  |       dj                  |      }t        j                  dd|      }t        j                  dd|      }|j                         }|sd}||z   S )N-z[\\/:*?\"<>|\n\r\t]_z_+r)   )	r   appendr   r   strjoinresubstrip)r   r   r1   r*   bases        r   _prepare_filenamezProfiler._prepare_filenameP   s    
 ;;"KK$==KK&'KKD,,-."KK$xx~vv,c48vveS$'zz|Dir   c                    | j                   y | j                  r| j                  rt        j                  j                  | j                  | j                               }t        |      }|j                  | j                  d       |j                  |d      }|| _
        |j                  | _         y | j                  | _         y )NT)exist_oka)r   r   r   ospathr7   r<   r   mkdirsopenr   writer0   )r   filepathfsfiles       r   _prepare_streamszProfiler._prepare_streamsg   s    )==T\\ww||DLL$2H2H2JKH)BIIdllTI2778S)D $D!%D!%!5!5Dr   c                    | j                          | j                         }|r| j                  | j                  |       | j                  | j                  j	                          | j                  | j                         y)z2Logs a profile report after the conclusion of run.Nstage)rH   r'   r   r   flushteardownr   )r   r'   s     r   describezProfiler.describet   sg    
 	,,.t))5w'(##%DKK(r   statsc                 \   | j                   | j                   j                          dnd}|dz   g}|j                         D ]J  \  }}d| }| j                  |d| j                   z  }|j	                  |       |j	                  |       L t
        j                  j                  |      S )N r%   zProfiler ReportzProfile stats for: z rank: )r   upperitemsr   r5   r@   linesepr7   )r   rO   rK   outputactionvalueheaders          r   _stats_to_strzProfiler._stats_to_str   s    -1[[-D4;;$$&'q)"++,"[[]MFE*6(3F+GD$4$4#566MM&!MM%  + zzv&&r   rK   
local_ranklog_dirc                 J    || _         || _        | j                  xs || _        y)z-Execute arbitrary pre-profiling set-up steps.N)r   r   r   )r   rK   rZ   r[   s       r   setupzProfiler.setup   s!    %||.wr   c                 n    d| _         | j                  "| j                  j                          d| _        yy)zoExecute arbitrary post-profiling tear-down steps.

        Closes the currently open file and stream.

        N)r   r   close)r   rK   s     r   rM   zProfiler.teardown   s7     "(##% $D )r   c                 <    | j                  | j                         y )NrJ   )rM   r   r&   s    r   __del__zProfiler.__del__   s    DKK(r   c                 6    | j                   dS | j                   S r-   )r   r&   s    r   rZ   zProfiler.local_rank   s    $$,qB$2B2BBr   )NN)Nz.txt)r   N)__name__
__module____qualname____doc__r
   r   r6   r   r   r   r    r#   r'   r   r   r)   r   r0   r<   rH   rN   dictrY   intr]   rM   ra   propertyrZ   r   r   r   r   r      s   U /3"&*%T	*+* 3-* 
	* 8 8 8 8 M M M M  #3 #9 # #$&S &C &D & &* c]    
	 .6)	'4S> 	'c 	'/3 /HSM /8TW= /dh /	%hsm 	% 	%) CC C Cr   r   )rf   loggingr@   r8   abcr   r   collections.abcr   
contextlibr   pathlibr   typingr   r	   r
   r   r   #lightning.fabric.utilities.cloud_ior   	getLoggerrc   r.   r   r   r   r   <module>rr      sK    C  	 	 # % %  9 9 >g!DCs DCr   