
      i                         d 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ZddlmZ ddlmZ  ej                   e      Zeeeeeef   Zee   Zeeeef   Ze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)Profilerc            	            e Zd ZdZ	 	 	 ddeeeef      de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eeef   fdZdefdZe
defd       Z xZS )SimpleProfilerzThis profiler simply records the duration of actions (in seconds) and reports the mean duration of each action
    and the total time spent over the entire training run.Ndirpathfilenameextendedreturnc                     t         |   ||       i | _        t        t              | _        || _        t        j                         | _	        y)a9  
        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.

            extended: If ``True``, adds extra columns representing number of calls and percentage of total time spent on
                respective action.

        Raises:
            ValueError:
                If you attempt to start an action which has already started, or
                if you attempt to stop recording an action which was never started.
        )r   r   N)
super__init__current_actionsr   listrecorded_durationsr   timeperf_counter
start_time)selfr   r   r   	__class__s       w/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pytorch_lightning/profilers/simple.pyr   zSimpleProfiler.__init__(   sC    . 	8<13(3D(9 ++-    action_namec                     || j                   v rt        d| d      t        j                         | j                   |<   y )NzAttempted to start z which has already started.)r   
ValueErrorr   r   )r   r   s     r   startzSimpleProfiler.startE   s?    $...2;-?Z[\\,0,=,=,?[)r   c                     t        j                         }|| j                  vrt        d| d      | j                  j	                  |      }||z
  }| j
                  |   j                  |       y )Nz(Attempting to stop recording an action (z) which was never started.)r   r   r   r   popr   append)r   r   end_timer   durations        r   stopzSimpleProfiler.stopK   sm    $$&d222G}Tnopp))--k:
j(,33H=r   c                    t        j                         | j                  z
  }g }| j                  j	                         D ]i  \  }}t        j                  |      }t        |      }t        j                  |      j                         }d|z  |z  }|j                  |||z  |||f       k |j                  d d       t        d |D              }	||	|fS )Ng      Y@c                     | d   S )N    xs    r   <lambda>z6SimpleProfiler._make_report_extended.<locals>.<lambda>`       !A$r   Tkeyreversec              3   &   K   | ]	  }|d      yw)   Nr)   ).0r+   s     r   	<genexpr>z7SimpleProfiler._make_report_extended.<locals>.<genexpr>a   s     /1!A$s   )r   r   r   r   itemstorchtensorlensumitemr"   sort)
r   total_durationreportadd_tensorlen_dsum_dpercentage_dtotal_callss
             r   _make_report_extendedz$SimpleProfiler._make_report_extendedT   s    **,t>++113DAq||AHFEIIh',,.E 5=>9LMM1eemUE<HI 4 	5///{N22r   c                 &   g }| j                   j                         D ]]  \  }}t        j                  |      }t        j                  |      j                         }|j                  ||t        |      z  |f       _ |j                  d d       |S )Nc                     | d   S )N   r)   r*   s    r   r,   z-SimpleProfiler._make_report.<locals>.<lambda>l   r-   r   Tr.   )	r   r5   r6   r7   r9   r:   r"   r8   r;   )r   r=   actionr?   r@   rB   s         r   _make_reportzSimpleProfiler._make_reportd   sz    00668IFA||AHIIh',,.EMM653q6>59:	 9 	5r   c                    t         j                  d}| j                   || j                  j                          dz  }|d z  }| j                  rt        | j                        dkD  rxt        d | j                  D              dt        dt        dt        d	t        d
t        dt        ffd} |ddddd      }t        |j                               } d|z   }|||z   |z   z  }| j                         \  }}}| |dd| |dd      z  }||z  }|D ]  \  }	}
}}}| ||	|
d| |d|d      z  }  ||z  }nt        d | j                  D              dt        dt        d	t        dt        ffd} |ddd      }t        |j                               } d|z   }|||z   |z   z  }| j                         }|D ]  \  }	}
}| ||	|
d|d      z  } ||z  }|z  }|S )N  zProfiler Reportr   c              3   2   K   | ]  }t        |        y wNr8   r3   ks     r   r4   z)SimpleProfiler.summary.<locals>.<genexpr>y   s     F.Ec!f.E   rI   mean	num_callstotalperr   c                 N     d| d dd|dd}|d|dd|dd|ddz  }|S )N|  <s	|  <15	|z  r)   )rI   rT   rU   rV   rW   rowmax_keyseps         r   log_row_extendedz0SimpleProfiler.summary.<locals>.log_row_extended{   sR     EVAgYa<$8d3ZsKCR	#eE#;eC9CPPCJr   ActionzMean duration (s)z	Num callszTotal time (s)zPercentage %-Totalz.5z100 %c              3   2   K   | ]  }t        |        y wrO   rP   rQ   s     r   r4   z)SimpleProfiler.summary.<locals>.<genexpr>   s     B*AQ#a&*ArS   c                 .     d| d dd|dd|ddS )NrY   rZ   r[   r\   r]   r^   r)   )rI   rT   rV   r`   ra   s      r   log_rowz'SimpleProfiler.summary.<locals>.log_row   s3    c&7)1!5U4*E%PSTWXXr   )oslinesep_stageupperr   r8   r   maxstr
expandtabsrE   rJ   )r   output_stringrb   header_stringoutput_string_len	sep_linesreport_extendedrD   r<   rI   mean_durationrU   duration_perrh   r=   r`   ra   s                  @@r   summaryzSimpleProfiler.summaryo   s   jj;;" 1 1 34A66M?3%00==4**+a/Fd.E.EFFS   TW ^a fi 
 !11;@PR`! %((@(@(B$C!"eC*;$;#<=	]!:Y!FF?C?Y?Y?[<n!1'3;a.VdegUhkr!ss*VeRFM9nl!%5(,$+)"-'+& M Wf *B$*A*ABBGY Y3 Ys Ys Y $H.ACSTM #M$<$<$> ?%&7 789IY6BBM&&(F9?5~M"3E>Z\J]!__ :@Y&Mr   )NNT)__name__
__module____qualname____doc__r   r   rn   r   boolr   r   r   r%   tuple_TABLE_DATA_EXTENDEDfloatrE   _TABLE_DATArJ   rw   __classcell__)r   s   @r   r
   r
   $   s    >
 /3"&	.%T	*+. 3-. 	.
 
.: @ @ @ @
 > > > >3u-A5%-O'P 3 	k 	 3 3 3r   r
   )r{   loggingri   r   collectionsr   pathlibr   typingr   r   r6   typing_extensionsr   $pytorch_lightning.profilers.profilerr   	getLoggerrx   logr}   rn   r   int_TABLE_ROW_EXTENDEDr   r~   
_TABLE_ROWr   r
   r)   r   r   <module>r      s    C  	  #  "  & 9g!CUE9: /0 3u$%
:X r   