
    xi                    >   d Z ddlmZ ddlmZ ddlmZmZmZ ddlm	Z	m
Z
mZmZmZmZmZmZmZmZ ddlZddlZddZdadd	Z e        dd
ZddZddZe	ddddZe	e
eeeeeeeed
Zd d!dZddd"dZd#dZd$dZ ddd"dZ!d#dZ"d$dZ#e	ddd%dZ$e	ddd&dZ%y)'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                 H   t        j                         xs d} | dkD  r| dz
  n| }t        |d      }	 t        j                  |       	 t        j                  |       y# t
        $ r}t        d| d|       |d}~ww xY w# t
        $ r}t        d| d|       |d}~ww xY w)uX  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      2/mnt/workspace/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 	A>'A99A>	B!
BB!Fc                     t         ryt        j                  j                  dd      j	                         j                         } | dvryt                da y)a  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   sL    ( 

:B?EEGMMOI22"    c                F    | xs dj                         j                         S )Nr#   )r+   uppermodes    r   _normalize_moder4   n   s    JB%%''r/   c                    t        |       dk7  S )NOFFr4   r2   s    r   is_debug_enabledr8   r   s    4 E))r/   c                    t        |       dk(  S )NVERBOSEr7   r2   s    r   is_debug_verboser;   v   s    4 I--r/   debugr3   prefixc                   t        |      syt        |       r |        } t        j                         j	                  d      dd }t        d| d| d|  d       y)	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   sW    D!)		 	 !7	8"	=B	AfXRt1WI
&d3r/   )
tensor
api_server	inferencetrainingdatasetaudiollmuimodel_loadinggpuc                n    t         j                  | xs dj                         j                         |      S )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               >    t        |       }t        |||xs |        y)z3Emit a timestamped debug log for a named subsystem.r=   N)rY   rJ   rW   rH   r>   r3   s       r   debug_log_forr\      s    $DgD48r/   c                4    t        |       }t        |||       S )z#Start timing for a named subsystem.r=   )rY   debug_startrW   labelr3   s      r   debug_start_forra      s    $Du455r/   c                8    t        |       }t        ||||        y)z!End timing for a named subsystem.r=   N)rY   	debug_endrW   r`   start_tsr3   s       r   debug_end_forrf      s    $DeXD6r/   c               V    t        |       }t        |      syt        |||xs |        y)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!gD48r/   c                L    t        |       }t        |      syt        |||       S )z8Start timing only in VERBOSE mode for a named subsystem.Nr=   )rY   r;   r^   r_   s      r   debug_start_verbose_forrj      s&    $DD!u455r/   c                P    t        |       }t        |      syt        ||||        y)z6End timing only in VERBOSE mode for a named subsystem.Nr=   )rY   r;   rc   rd   s       r   debug_end_verbose_forrl      s%    $DD!eXD6r/   c               V    t        |      syt        d|  ||       ddlm}  |       S )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               r    |t        |      syddlm}  |       |z
  dz  }t        d|  d|dd||	       y)
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      sE    /5!.8+v5JTF"Z,D1V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r/   