
    xi                    h   S r SSKJr  SSKJr  SSKJrJrJr  SSKJ	r	J
r
JrJrJrJrJrJrJrJr  SSKrSSKrSS jrSqSS	 jr\" 5         SS
 jrSS jrSS jr\	SS.SS jjr\	\
\\\\\\\\S.
rS S!S jjrSS.S"S jjrS#S jrS$S jr SS.S"S jjr!S#S jr"S$S jr#\	SS.S%S jjr$\	SS.S&S jjr%g)'z
Debug helpers (global).
    )annotations)datetime)OptionalCallableUnion)
TENSOR_DEBUG_MODEDEBUG_API_SERVERDEBUG_INFERENCEDEBUG_TRAININGDEBUG_DATASETDEBUG_AUDIO	DEBUG_LLMDEBUG_UIDEBUG_MODEL_LOADING	DEBUG_GPUNc                 ^   [         R                  " 5       =(       d    Sn U S:  a  U S-
  OU n[        US5      n [        R                  " U5         [        R                  " U5        g! [
         a  n[        SU SU 35      UeSnAff = f! [
         a  n[        SU SU 35      UeSnAff = f)u0  Set torch's intra-op and inter-op thread counts based on available CPUs.

This function configures PyTorch to use most available CPU cores while
leaving a couple free for the OS and other processes. The logic is:
  * If the system has ≤ 2 logical CPUs, use all of them.
  * Otherwise, use (cpu_count - 2) threads.

This mirrors the common "all-but-two" heuristic while guaranteeing at
least one thread.

Raises:
    RuntimeError: If torch.set_num_threads or torch.set_num_interop_threads
        fails (e.g., if called after threads have already been used).
      z(Failed to set torch intra-op threads to z: Nz(Failed to set torch inter-op threads to )os	cpu_countmaxtorchset_num_threadsRuntimeErrorset_num_interop_threads)cpu_cntthreadsexcs      J/mnt/workspace/acestep.cpp/tests/../../ACE-Step-1.5/acestep/debug_utils.py_configure_cpu_threadsr    $   s     lln!G$q[gkgG'1oGg&%%g.  6wir#G
	  6wir#G
	s/   A' B '
B1BB
B,B''B,Fc                     [         (       a  g[        R                  R                  SS5      R	                  5       R                  5       n U S;  a  g[        5         Sq g)av  Configure CPU threads if enabled via environment variable.

This function provides an opt-in mechanism for configuring PyTorch's
thread counts. It only takes effect if the environment variable
``ACESTEP_CONFIGURE_THREADS`` is set to a truthy value (e.g., "1", "true", "yes").

The configuration is applied at most once per process; subsequent calls
are no-ops.

Returns:
    True if configuration was applied, False if skipped (either because
    the environment variable is not set or configuration was already done).

Raises:
    RuntimeError: If thread configuration fails (propagated from
        ``_configure_cpu_threads``).
FACESTEP_CONFIGURE_THREADS )1trueyesonT)_cpu_threads_configuredr   environgetstriplowerr    )	env_values    r   configure_cpu_threads_if_neededr.   J   sO    ( 

:B?EEGMMOI22"    c                P    U =(       d    SR                  5       R                  5       $ )Nr#   )r+   uppermodes    r   _normalize_moder4   n   s    JB%%''r/   c                    [        U 5      S:g  $ )NOFFr4   r2   s    r   is_debug_enabledr8   r   s    4 E))r/   c                    [        U 5      S:H  $ )NVERBOSEr7   r2   s    r   is_debug_verboser;   v   s    4 I--r/   debugr3   prefixc                   [        U5      (       d  g[        U 5      (       a  U " 5       n [        R                  " 5       R	                  S5      SS n[        SU SU SU  3SS9  g)	z9Emit a timestamped debug log line if the mode is enabled.Nz%Y-%m-%d %H:%M:%S.%f[z]  T)flush)r8   callabler   nowstrftimeprint)messager3   r>   tss       r   	debug_logrJ   z   s]    D!!)		 	 !7	8"	=B	AfXRt1WI
&d3r/   )
tensor
api_server	inferencetrainingdatasetaudiollmuimodel_loadinggpuc                x    [         R                  U =(       d    SR                  5       R                  5       U5      $ )z'Fetch a placeholder debug mode by name.r#   )DEBUG_SWITCHESr*   r+   r,   )namedefaults     r   get_debug_moderY      s+    tzr00288:GDDr/   )r>   c               @    [        U 5      n[        XU=(       d    U S9  g)z3Emit a timestamped debug log for a named subsystem.r=   N)rY   rJ   rW   rH   r>   r3   s       r   debug_log_forr\      s    $Dg48r/   c                ,    [        U 5      n[        XU S9$ )z#Start timing for a named subsystem.r=   )rY   debug_startrW   labelr3   s      r   debug_start_forra      s    $Du55r/   c                .    [        U 5      n[        XX0S9  g)z!End timing for a named subsystem.r=   N)rY   	debug_endrW   r`   start_tsr3   s       r   debug_end_forrf      s    $DeD6r/   c               b    [        U 5      n[        U5      (       d  g[        XU=(       d    U S9  g)zHEmit a timestamped debug log only in VERBOSE mode for a named subsystem.Nr=   )rY   r;   rJ   r[   s       r   debug_log_verbose_forrh      s(    $DD!!g48r/   c                N    [        U 5      n[        U5      (       d  g[        XU S9$ )z8Start timing only in VERBOSE mode for a named subsystem.Nr=   )rY   r;   r^   r_   s      r   debug_start_verbose_forrj      s'    $DD!!u55r/   c                P    [        U 5      n[        U5      (       d  g[        XX0S9  g)z6End timing only in VERBOSE mode for a named subsystem.Nr=   )rY   r;   rc   rd   s       r   debug_end_verbose_forrl      s$    $DD!!eD6r/   c               X    [        U5      (       d  g[        SU  3XS9  SSKJn  U" 5       $ )zCReturn a start timestamp (perf counter) if enabled, otherwise None.NzSTART r=   r   perf_counter)r8   rJ   timero   )rW   r3   r>   ro   s       r   r^   r^      s+    D!!tfoD8!>r/   c               v    Ub  [        U5      (       d  gSSKJn  U" 5       U-
  S-  n[        SU  SUS S3X#S	9  g)
zCEmit an END log with elapsed ms if enabled and start_ts is present.Nr   rn   g     @@zEND z (z.1fz ms)r=   )r8   rp   ro   rJ   )rW   re   r3   r>   ro   
elapsed_mss         r   rc   rc      sF    /55!.8+v5JTF"Z,D1Lr/   )returnNone)rs   bool)r3   strrs   rv   )r3   rv   rs   ru   )rH   Union[str, Callable[[], str]]r3   rv   r>   rv   rs   rt   )r6   )rW   rv   rX   rv   rs   rv   )rW   rv   rH   rw   r>   z
str | Noners   rt   )rW   rv   r`   rv   rs   Optional[float])rW   rv   r`   rv   re   rx   rs   rt   )rW   rv   r3   rv   r>   rv   rs   rx   )
rW   rv   re   rx   r3   rv   r>   rv   rs   rt   )&__doc__
__future__r   r   typingr   r   r   acestep.constantsr   r	   r
   r   r   r   r   r   r   r   r   r   r    r(   r.   r4   r8   r;   rJ   rV   rY   r\   ra   rf   rh   rj   rl   r^   rc    r/   r   <module>r~      s    #  , ,  0 
  F   B   !(*. FWfm 4  " 
(E
 ^b 967 fj 967 +<7  DUdk M Mr/   