
    mi                     @   d dl mZ d dlm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 Zde	j                   dej"                  e	j                   ef   fd	Zd
ej(                  e   defdZddefdZddedefdZeddefd       ZddedefdZ G d d      Zy)    )defaultdict)contextmanagerN)
functional)Subsetc                 0   | j                   ^ }}t        j                  ||z        }|dz
  |z  |z   }t        j                  | d||z
  f      } t        | j                               }|d   dk(  sJ d       |dd |dgz   }| j                  g ||||      S )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           F/var/www/stems/demucs_env/lib/python3.12/site-packages/demucs/utils.pyunfoldr      s     WWNUFyy&)HQ,&(;6J	a!Z&()*A188:G2;!888crlfa[(G<<7%77;7AA    tensor	referencec                     t        |t        j                        r|j                  d      }n|}| j                  d      |z
  }|dk  rt	        d| d      |r| d|dz  ||dz  z
   f   } | S )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                 t    g }| D ]0  }|}|j                  d      D ]  }||   }	 |j                  |       2 |S )Nr   )splitappend)r(   r)   outmetricsmetricparts         r   pull_metricr1   8   sN    
C JJsO 	"DD\F	"

6	
 Jr   betac                 ~     t        t              t        t              ddt        dt        dt        f fd}|S )a  
    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                     | j                         D ]-  \  }}|   z  |t        |      z  z   |<   |   z  |z   |<   / j                         D ci c]  \  }}|||   z   c}}S c c}}w N)itemsfloat)r.   r4   keyvaluetotr2   fixtotals        r   _updatezEMA.<locals>._updateN   s    !--/ 	0JCsd*VeEl-BBE#J3x$/CH	0 5:KKMBSS3s8^#BBBs   A,r   )r   r9   dict)r2   r?   r=   r>   s   ` @@r   EMArB   B   s>      +51C!,U!3EC Cu CT C Nr   numsuffixc                 Z    dD ]  }t        |       dk  r
d| ||fz  c S | dz  } ! d| d|fz  S )zk
    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)rC   rD   units      r   
sizeof_fmtrQ   W   sN    
 ? s8f#tV!444v dF+++r   countc              #     K   g }	 t        |       D ]1  }|j                  t        j                  d      j                         3 | |r|D ]  }t        j                  |        y y # |r|D ]  }t        j                  |        w w xY ww)NF)delete)ranger,   tempfileNamedTemporaryFiler)   osunlink)rR   rT   names_r)   s        r   temp_filenamesr\   c   s     E u 	IALL44EBGGH	I  		$  6  		$  s   BAA) 	 B)!B

Bmax_samplesseedc                     |t        |       k\  r| S t        j                         j                  |      }t        j                  t        |       |      }t        | |d | j                               S )N)	generator)lenr!   	Generatormanual_seedrandpermr   tolist)datasetr]   r^   r`   perms        r   random_subsetrh   p   s[    c'l"!--d3I>>#g,)<D'4-44677r   c                   :    e Zd Z G d d      ZddZd Zd Zd Zy)	DummyPoolExecutorc                       e Zd Zd Zd Zy)DummyPoolExecutor.DummyResultc                 .    || _         || _        || _        y r7   funcargskwargsselfro   rp   rq   s       r   __init__z&DummyPoolExecutor.DummyResult.__init__{   s    DIDI DKr   c                 N     | j                   | j                  i | j                  S r7   rn   rs   s    r   resultz$DummyPoolExecutor.DummyResult.result   s     499dii74;;77r   N)__name__
__module____qualname__rt   rw    r   r   DummyResultrl   z   s    	!
	8r   r|   c                      y r7   r{   )rs   workerss     r   rt   zDummyPoolExecutor.__init__   s    r   c                 4    t        j                  |g|i |S r7   )rj   r|   rr   s       r   submitzDummyPoolExecutor.submit   s     ,,TCDCFCCr   c                     | S r7   r{   rv   s    r   	__enter__zDummyPoolExecutor.__enter__   s    r   c                      y r7   r{   )rs   exc_type	exc_valueexc_tbs       r   __exit__zDummyPoolExecutor.__exit__   s    r   N)r   )rx   ry   rz   r|   rt   r   r   r   r{   r   r   rj   rj   y   s!    8 8Dr   rj   r@   )B)T)*   )collectionsr   
contextlibr   r   rX   rV   typingtpr!   torch.nnr   r   torch.utils.datar   r   r"   Unionintr'   ListrA   strr1   r9   rB   rQ   r\   rh   rj   r{   r   r   <module>r      s    $ %  	    $ #B$ %,,:K1L & c e *	,E 	,3 	, 	 # 	  	 8 83 8 r   