
    ]i                     T   S SK Jr  S SKJr  S SKrS SKrS SKrS SKrS SK	r	S SK
Jr  S SKJr  S rS\	R                   S\R"                  \	R                   \4   4S	 jrS
\R(                  \   S\4S jrSS\4S jjrSS\S\4S jjr\SS\4S jj5       rSS\S\4S jjr " S S5      rg)    )defaultdict)contextmanagerN)
functional)Subsetc                 0   U R                   Gt p4[        R                  " XB-  5      nUS-
  U-  U-   n[        R                  " U SXd-
  45      n [        U R                  5       5      nUS   S:X  d   S5       eUSS US/-   nU R                  / UQUPUPU5      $ )zGiven input of size [*OT, T], output Tensor of size [*OT, F, K]
with K the kernel size, by extracting frames with the given stride.

This will pad the input so that `F = ceil(T / K)`.

see https://github.com/pytorch/pytorch/issues/60466
   r   zdata should be contiguousN)shapemathceilFpadliststride
as_strided)akernel_sizer   r
   lengthn_frames
tgt_lengthstridess           I/mnt/rpi/tmp/demucs-venv-sys/lib/python3.13/site-packages/demucs/utils.pyunfoldr      s     WWNUyy)HQ,&(;6J	a!Z()*A188:G2;!888crlfa[(G<<7%77;7AA    tensor	referencec                     [        U[        R                  5      (       a  UR                  S5      nOUnU R                  S5      U-
  nUS:  a  [	        SU S35      eU(       a  U SUS-  X3S-  -
  * 24   n U $ )z
Center trim `tensor` with respect to `reference`, along the last dimension.
`reference` can also be a number, representing the length to trim to.
If the size difference != 0 mod 2, the extra sample is removed on the right side.
r	   r   z/tensor must be larger than reference. Delta is ..   )
isinstancetorchTensorsize
ValueError)r   r   ref_sizedeltas       r   center_trimr'   %   s     )U\\**>>"%KKOh&EqyMeWTUVWWUaZ%1**<(===>Mr   historynamec                 z    / nU  H2  nUnUR                  S5       H  nXE   nM	     UR                  U5        M4     U$ )Nr   )splitappend)r(   r)   outmetricsmetricparts         r   pull_metricr1   8   sB    
CJJsOD\F $

6	 
 Jr   betac                    ^ ^^ [        [        5      m[        [        5      mSS[        S[        S[        4U UU4S jjjnU$ )z
Exponential Moving Average callback.
Returns a single function that can be called to repeatidly update the EMA
with a dict of metrics. The callback will return
the new averaged dict of metrics.

Note that for `beta=1`, this is just plain averaging.
r.   weightreturnc                    > U R                  5        H-  u  p#TU   T-  U[        U5      -  -   TU'   TU   T-  U-   TU'   M/     TR                  5        VVs0 s H  u  p$X$TU   -  _M     snn$ s  snnf N)itemsfloat)r.   r4   keyvaluetotr2   fixtotals        r   _updateEMA.<locals>._updateN   sx    !--/JCsd*VeEl-BBE#J3x$/CH * 5:KKMBM3s8^#MBBBs   A.r   )r   r9   dict)r2   r?   r=   r>   s   ` @@r   EMArC   B   sD      +51C!,U!3EC Cu CT C C Nr   numsuffixc                 ^    S H   n[        U 5      S:  a	  SXU4-  s  $ U S-  n M"     SU SU4-  $ )z_
Given `num` bytes, return human readable size.
Taken from https://stackoverflow.com/a/1094933
) KiMiGiTiPiEiZig      @z	%3.1f%s%sz%.1f%s%sYi)abs)rD   rE   units      r   
sizeof_fmtrR   W   sG    
 ?s8f#V!444v ? dF+++r   countc              #   >  #    / n [        U 5       H0  nUR                  [        R                  " SS9R                  5        M2     Uv   U(       a   U H  n[
        R                  " U5        M     g g ! U(       a   U H  n[
        R                  " U5        M     f f = f7f)NF)delete)ranger,   tempfileNamedTemporaryFiler)   osunlink)rS   rU   names_r)   s        r   temp_filenamesr]   c   sy     E uALL44EBGGH 		$  6		$  s   BAA1 	(B1)BBmax_samplesseedc                     U[        U 5      :  a  U $ [        R                  " 5       R                  U5      n[        R                  " [        U 5      US9n[        XS U R                  5       5      $ )N)	generator)lenr!   	Generatormanual_seedrandpermr   tolist)datasetr^   r_   ra   perms        r   random_subsetri   p   sY    c'l"!--d3I>>#g,)<D'-44677r   c                   D    \ rS rSr " S S5      rS
S jrS rS rS rSr	g	)DummyPoolExecutory   c                        \ rS rSrS rS rSrg)DummyPoolExecutor.DummyResultz   c                 (    Xl         X l        X0l        g r7   funcargskwargsselfrr   rs   rt   s       r   __init__&DummyPoolExecutor.DummyResult.__init__{   s    II Kr   c                 N    U R                   " U R                  0 U R                  D6$ r7   rq   rv   s    r   result$DummyPoolExecutor.DummyResult.result   s    99dii74;;77r   )rs   rr   rt   N)__name__
__module____qualname____firstlineno__rw   r{   __static_attributes__ r   r   DummyResultrn   z   s    	!
	8r   r   c                     g r7   r   )rv   workerss     r   rw   DummyPoolExecutor.__init__   s    r   c                 6    [         R                  " U/UQ70 UD6$ r7   )rk   r   ru   s       r   submitDummyPoolExecutor.submit   s     ,,TCDCFCCr   c                     U $ r7   r   rz   s    r   	__enter__DummyPoolExecutor.__enter__   s    r   c                     g r7   r   )rv   exc_type	exc_valueexc_tbs       r   __exit__DummyPoolExecutor.__exit__   s    r   r   N)r   )
r}   r~   r   r   r   rw   r   r   r   r   r   r   r   rk   rk   y   s!    8 8Dr   rk   rA   )B)T)*   )collectionsr   
contextlibr   r   rY   rW   typingtpr!   torch.nnr   r   torch.utils.datar   r   r"   Unionintr'   ListrB   strr1   r9   rC   rR   r]   ri   rk   r   r   r   <module>r      s    $ %  	    $ #B$ %,,:K1L & c e *	,E 	,3 	, 	 # 	  	 8 83 8 r   