
      i2                         d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
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  ej,                  e      Z G d
 de      Zy)z<Profiler to check if there are any bottlenecks in your code.    N)defaultdict)Path)OptionalUnion)override)get_filesystem)Profiler)rank_zero_onlyc                        e Zd ZdZ	 	 	 	 ddeeeef      dee   dede	ddf
 fdZ
ed	eddfd
       Zed	eddfd       Zd	edej                  ddfdZedefd       Zedee   ddf fd       ZdefdZ xZS )AdvancedProfilerzThis profiler uses Python's cProfiler to record more detailed information about time spent in each function call
    recorded during a given action.

    The output is quite verbose and you should only use this if you want very detailed reports.

    Ndirpathfilenameline_count_restriction
dump_statsreturnc                     t         |   ||       t        t        j                        | _        || _        || _        y)a  
        Args:
            dirpath: Directory path for the ``filename``. If ``dirpath`` is ``None`` but ``filename`` is present, the
                ``trainer.log_dir`` (from :class:`~pytorch_lightning.loggers.tensorboard.TensorBoardLogger`)
                will be used.

            filename: If present, filename where the profiler results will be saved instead of printing to stdout.
                The ``.txt`` extension will be used automatically.

            line_count_restriction: this can be used to limit the number of functions
                reported for each action. either an integer (to select a count of lines),
                or a decimal fraction between 0.0 and 1.0 inclusive (to select a percentage of lines)

            dump_stats: Whether to save raw profiler results. When ``True`` then ``dirpath`` must be provided.

        Raises:
            ValueError:
                If you attempt to stop recording an action which was never started.
        )r   r   N)super__init__r   cProfileProfileprofiled_actionsr   r   )selfr   r   r   r   	__class__s        y/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pytorch_lightning/profilers/advanced.pyr   zAdvancedProfiler.__init__+   s9    4 	8<=HIYIY=Z&<#$    action_namec                     | j                   j                         D ]  }|j                           | j                   |   j                          y )N)r   valuesdisableenabler   r   prs      r   startzAdvancedProfiler.startJ   s=     ''..0BJJL 1k*113r   c                 |    | j                   j                  |      }|t        d| d      |j                          y )Nz(Attempting to stop recording an action (z) which was never started.)r   get
ValueErrorr   r!   s      r   stopzAdvancedProfiler.stopQ   s<    ""&&{3:G}Tnopp


r   profilec                    | j                   sJ t        j                  j                  | j                   | j	                  |d            }t        |      }|j                  | j                   d       t        j                  dt        t        j                        t        j                               5 }|j                  |d      5 }t        j                  j                  |d      }|j                  |       t        |      }|j                  |d	      5 }	|j                  |	j!                                d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)
Nz.prof)r   	extensionT)exist_oktest)prefixsuffixdirwbztmp.profrb)r   ospathjoin_prepare_filenamer   mkdirstempfileTemporaryDirectorystrr
   rankgetcwdopenr   writeread)
r   r   r(   dst_filepathdst_fstmp_dirdst_filesrc_filepathsrc_fssrc_files
             r   _dump_statszAdvancedProfiler._dump_statsX   s   |||ww||DLL$2H2HU`ls2H2tu-dllT2 ''vc.BUBU>V\^\e\e\ghlsKKd+x77<<<L|,#L1F\40Hx}}/ 1 , ih 10 ,+ ihs=   3E&AE E5E=E&EEE#	E&&E/c                    i }| j                   j                         D ]  \  }}| j                  r| j                  ||       t	        j
                         }t        j                  ||      j                         j                  d      }|j                  | j                         |j                         ||<    | j                  |      S )N)stream
cumulative)r   itemsr   rF   ioStringIOpstatsStats
strip_dirs
sort_statsprint_statsr   getvalue_stats_to_str)r   recorded_statsr   r"   spss         r   summaryzAdvancedProfiler.summaryh   s    #44::<OK  b1Ab+668CCLQBNN4667*+**,N;'  = !!.11r   stagec                 Z    t         |   |       | j                  j                          y )N)rX   )r   teardownr   clear)r   rX   r   s     r   rZ   zAdvancedProfiler.teardownt   s%    u%##%r   c                 d    | j                   d| j                  | j                  | j                  dfS )N )r   r   r   )r   r   r   r   )r   s    r   
__reduce__zAdvancedProfiler.__reduce__y   s1     NN$--[_[v[vw
 	
r   )NNg      ?F)__name__
__module____qualname____doc__r   r   r9   r   floatboolr   r   r#   r'   r   r   rF   rW   rZ   tupler^   __classcell__)r   s   @r   r   r   #   s    /3"&(+ %%T	*+% 3-% !&	%
 % 
%> 4 4 4 4    0s 0X5E5E 0$ 0  	2 	2 	2 &hsm & & &
E 
r   r   )rb   r   rK   loggingr2   rM   r7   collectionsr   pathlibr   typingr   r   typing_extensionsr   #lightning_fabric.utilities.cloud_ior   $pytorch_lightning.profilers.profilerr	   %pytorch_lightning.utilities.rank_zeror
   	getLoggerr_   logr   r]   r   r   <module>rq      sR    C  	  	   #  " & > 9 @g!\
x \
r   