
    Vh                     2   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dS )!    )defaultdict)contextmanagerN)
functional)Subsetc                 R   | j         ^ }}t          j        ||z            }|dz
  |z  |z   }t          j        | d||z
  f          } t          |                                           }|d         dk    s
J d            |dd         |dgz   }|                     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           R/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/demucs/utils.pyunfoldr      s     WNUFy&))HQ,&(;6J	a!Z&()**A188::G2;!8crclfa[(G<<7%77;7AAA    tensor	referencec                     t          |t          j                  r|                    d          }n|}|                     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OOh&EqyyVeVVVWWW ?UaZ%%1**<(===>Mr   historynamec                     g }| D ]9}|}|                     d          D ]
}||         }|                    |           :|S )Nr   )splitappend)r(   r)   outmetricsmetricparts         r   pull_metricr1   8   sY    
C  JJsOO 	" 	"DD\FF

6Jr   r   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   r.   weightreturnc                     |                                  D ]7\  }}|         z  |t          |          z  z   |<   |         z  |z   |<   8fd                                 D             S )Nc                 .    i | ]\  }}|||         z  S  r8   ).0keytotfixs      r   
<dictcomp>z(EMA.<locals>._update.<locals>.<dictcomp>S   s'    BBBSS3s8^BBBr   )itemsfloat)r.   r4   r:   valuer2   r<   totals       r   _updatezEMA.<locals>._updateN   sz    !--// 	0 	0JCsd*VeEll-BBE#J3x$/CHHBBBBEKKMMBBBBr   r   )r   r?   dict)r2   rB   r<   rA   s   ` @@r   EMArE   B   sv      +511C!,U!3!3EC C Cu CT C C C C C C C C Nr   Bnumsuffixc                 `    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)rG   rH   units      r   
sizeof_fmtrU   W   sY    
 ?  s88f#tV!44444vdF+++r   Tcountc              #     K   g }	 t          |           D ]/}|                    t          j        d          j                   0|V  |r|D ]}t          j        |           d S d S # |r|D ]}t          j        |           w w xY w)NF)delete)ranger,   tempfileNamedTemporaryFiler)   osunlink)rV   rX   names_r)   s        r   temp_filenamesr`   c   s      E u 	I 	IALL4EBBBGHHHH 	     	$	  	      	     	$	  s   AA( (B*   max_samplesseedc                    |t          |           k    r| S t          j                                        |          }t          j        t          |           |          }t          | |d |                                                   S )N)	generator)lenr!   	Generatormanual_seedrandpermr   tolist)datasetrb   rc   re   perms        r   random_subsetrm   p   sr    c'll""!!--d33I>#g,,)<<<D'4-4466777r   c                   B    e Zd Z G d d          Zd	dZd Zd Zd ZdS )
DummyPoolExecutorc                       e Zd Zd Zd ZdS )DummyPoolExecutor.DummyResultc                 0    || _         || _        || _        d S Nfuncargskwargsselfru   rv   rw   s       r   __init__z&DummyPoolExecutor.DummyResult.__init__{   s    DIDI DKKKr   c                 0     | j         | j        i | j        S rs   rt   ry   s    r   resultz$DummyPoolExecutor.DummyResult.result   s    49di74;777r   N)__name__
__module____qualname__rz   r}   r8   r   r   DummyResultrq   z   s2        	! 	! 	!
	8 	8 	8 	8 	8r   r   r   c                     d S rs   r8   )ry   workerss     r   rz   zDummyPoolExecutor.__init__   s    r   c                 ,    t          j        |g|R i |S rs   )ro   r   rx   s       r   submitzDummyPoolExecutor.submit   s#     ,TCDCCCFCCCr   c                     | S rs   r8   r|   s    r   	__enter__zDummyPoolExecutor.__enter__   s    r   c                     d S rs   r8   )ry   exc_type	exc_valueexc_tbs       r   __exit__zDummyPoolExecutor.__exit__   s    r   N)r   )r~   r   r   r   rz   r   r   r   r8   r   r   ro   ro   y   s        8 8 8 8 8 8 8 8   D D D      r   ro   rC   )rF   )T)ra   )collectionsr   
contextlibr   r   r\   rZ   typingtpr!   torch.nnr   r   torch.utils.datar   r   r"   Unionintr'   ListrD   strr1   r?   rE   rU   r`   rm   ro   r8   r   r   <module>r      s   $ # # # # # % % % % % %  				       $ $ $ $ $ $ # # # # # #B B B$ %,:K1L    & c     e    *	, 	,E 	,3 	, 	, 	, 	, 	  	 # 	  	  	  	 8 8 83 8 8 8 8         r   