
    Vhv              	       z   d Z ddlmZ ddlZddlZddlmZ ddlZddlm	Z	 ddl
mZ ddlmZmZ dd	lmZ dd
lmZmZ ddej        dej        eef         dedefdZ G d de	j                  Z G d de	j                  Z G d de	j                  Z G d de	j                  Z G d de	j                  Z dS )zB
This code contains the spectrogram and Hybrid version of Demucs.
    )deepcopyN)wiener)nn)
functional   )DConvrescale_module)capture_init)spectroispectroconstant        xpaddingsmodevaluec                    | }| j         d         }|\  }}|dk    rTt          ||          }||k    r>||z
  dz   }	t          ||	          }
|	|
z
  }||z
  ||
z
  f}t          j        | ||
f          } t          j        | |||          }|j         d         ||z   |z   k    sJ |d|||z   f         |k                                    sJ |S )zTiny wrapper around F.pad, just to allow for reflect padding on small input.
    If this is the case, we insert extra 0 padding to the right before the reflection happen.reflectr   .)shapemaxminFpadall)r   r   r   r   x0lengthpadding_leftpadding_rightmax_pad	extra_padextra_pad_rightextra_pad_leftouts                T/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/demucs/hdemucs.pypad1dr&      s    
BWR[F"*L-ylM22W&(1,I!-;;O&8N$~5}7VWHa./:;;A
%8T5
)
)C9R=F\1MAAAAA\<&#8889R?DDFFFFFJ    c                   P     e Zd ZdZ	 d
dededef fdZed             Zd	 Z	 xZ
S )ScaledEmbeddingzt
    Boost learning rate for embeddings (with `scale`).
    Also, can make embeddings continuous with `smooth`.
          $@Fnum_embeddingsembedding_dimscalec                    t                                                       t          j        ||          | _        |rt          j        | j        j        j        d          }|t          j	        d|dz             
                    |                                          d d d f         z  }|| j        j        j        d d <   | j        j        xj        |z  c_        || _        d S )Nr   dimr   )super__init__r   	Embedding	embeddingtorchcumsumweightdataarangetosqrtr-   )selfr+   r,   r-   smoothr7   	__class__s         r%   r2   zScaledEmbedding.__init__0   s    nmDD 	3\$."7"<!DDDFel1nq.@AADDVLLQQSSTUTUTUW[T[\\F,2DN!&qqq)""e+""


r'   c                 *    | j         j        | j        z  S N)r4   r7   r-   )r<   s    r%   r7   zScaledEmbedding.weight<   s    ~$tz11r'   c                 @    |                      |          | j        z  }|S r@   )r4   r-   )r<   r   r$   s      r%   forwardzScaledEmbedding.forward@   s    nnQ$*,
r'   )r*   F)__name__
__module____qualname____doc__intfloatr2   propertyr7   rB   __classcell__r>   s   @r%   r)   r)   +   s         
 -2
 
s 
3 

 
 
 
 
 
 2 2 X2      r'   r)   c                   >     e Zd Zddddddddi ddf fd	Zd
d	Z xZS )	HEncLayer      r   FTr   c                    t                                                       d }|	rfd}|r|dz  }nd}t          j        }|| _        || _        || _        || _        |	| _        || _	        |r|dg}|dg}|dg}t          j
        } ||||||          | _        | j        rdS  ||          | _        d| _        |r0 ||d|z  dd|
z  z   d|
          | _         |d|z            | _        d| _        |rt!          |fi || _        dS dS )a#  Encoder layer. This used both by the time and the frequency branch.

        Args:
            chin: number of input channels.
            chout: number of output channels.
            norm_groups: number of groups for group norm.
            empty: used to make a layer with just the first conv. this is used
                before merging the time and freq. branches.
            freq: this is acting on frequencies.
            dconv: insert DConv residual branches.
            norm: use GroupNorm.
            context: context size for the 1x1 conv.
            dconv_kw: list of kwargs for the DConv class.
            pad: pad the input. Padding is done so that the output size is
                always the input size / stride.
            rewrite: add 1x1 conv at the end of the layer.
        c                 (    t          j                    S r@   r   Identityds    r%   <lambda>z$HEncLayer.__init__.<locals>.<lambda>[       BKMM r'   c                 .    t          j        |           S r@   r   	GroupNormrU   norm_groupss    r%   rV   z$HEncLayer.__init__.<locals>.<lambda>]       [! < < r'   rO   r   r   N   )r1   r2   r   Conv1dfreqkernel_sizestrideemptynormr   Conv2dconvnorm1rewritenorm2dconvr   )r<   chinchoutra   rb   r\   rc   r`   rj   rd   contextdconv_kwr   rh   norm_fnklassr>   s        `          r%   r2   zHEncLayer.__init__F   sd   ( 	)) 	=<<<<G 	"CCC		&
	 	&*Ka[F(CIEE${FC@@	: 	FWU^^
 	, 5E	1q7{?AwOODL U++DJ
 	2u1111DJJJ	2 	2r'   Nc                 H   | j         s;|                                dk    r#|j        \  }}}}|                    |d|          }| j         sB|j        d         }|| j        z  dk    s't          j        |d| j        || j        z  z
  f          }|                     |          }| j        r|S |v|j        d         |j        d         k    sJ |j        |j        f            |                                dk    r'|                                dk    r|dddddf         }||z   }t          j	        | 
                    |                    }| j        r| j         r9|j        \  }}}}|                    dddd                              d||          }|                     |          }| j         r.|                    ||||                              dddd          }| j        r?|                     |                     |                    }	t          j        |	d          }	n|}	|	S )	z
        `inject` is used to inject the result from the time branch into the frequency branch,
        when both have the same stride.
        rO   r   r   N   r^   r   r/   )r`   r0   r   viewrb   r   r   rf   rc   gelurg   rj   permutereshaperh   ri   glu)
r<   r   injectBCFrTleyzs
             r%   rB   zHEncLayer.forward{   s   
 y 	!QUUWW\\'KAq"aq"a  Ay 	DB#q((E!aT[0@!ABCCIIaLL: 	H<#qwr{222V\174K222zz||q  QUUWW\\111d
+F
AF4::a==!!: 	<y <g1b!IIaAq))11"a;;

1Ay <FF1b!Q''//1a;;< 	

4<<??++AaQAAAr'   r@   rC   rD   rE   r2   rB   rJ   rK   s   @r%   rM   rM   E   sl        01!RW$T1rt32 32 32 32 32 32j" " " " " " " "r'   rM   c                   *     e Zd ZdZ fdZddZ xZS )	MultiWrapa  
    Takes one layer and replicate it N times. each replica will act
    on a frequency band. All is done so that if the N replica have the same weights,
    then this is exactly equivalent to applying the original module on all frequencies.

    This is a bit over-engineered to avoid edge artifacts when splitting
    the frequency bands, but it is possible the naive implementation would work as well...
    c                 R   t                                                       || _        t          j                    | _        t          |t                    | _        |j	        rJ |j
        sJ |j        sJ | j        s	|j        rJ t          t          |          dz             D ]}t          |          }| j        rd|j        _        nd|_        |                                D ]&}t%          |d          r|                                 '| j                            |           dS )z
        Args:
            layer: module to clone, must be either HEncLayer or HDecLayer.
            split_ratios: list of float indicating which ratio to keep for each band.
        r   )r   r   Freset_parametersN)r1   r2   split_ratiosr   
ModuleListlayers
isinstancerM   rf   rd   r`   r   context_freqrangelenr   paddingmoduleshasattrr   append)r<   layerr   klaymr>   s         r%   r2   zMultiWrap.__init__   s.    	(mooui00	:~zzyyy 	*))))s<((1,-- 		$ 		$A5//Cy  #)  [[]] ) )1011 )&&(((Ks####		$ 		$r'   Nc                    |j         \  }}}}t          | j                  dgz   }d}	g }
t          || j                  D ]\  }}| j        r4|j        dz  }|dk    r|}d}ntt          t          ||z                      }||	z
  }|	dk    r||z  }t          ||j        z
  |j	        z  dz             }|	|dz
  |j	        z  z   |j        z   }|	dk    r||z  }||	z
  dk    sJ ||	f            ||k    sJ ||f            |d d d d |	|d d f         }|	dk    rt          j        |dd|df          }|dk    rt          j        |ddd|f          }|
                     ||                     ||j        z
  |j	        z   }	B|dk    r|}nt          t          ||z                      }|j        }d|_        |d d d d |	|f         }|d d d d |	|f         } |||d           \  }}|
rr|
d         d d d d |j	         d fxx         |d d d d d |j	        f         |j        j                            dddd          z
  z  cc<   |d d d d |j	        d f         }|dk    r|d d d d d |j	         dz  d d f         }|	dk    r|d d d d |j	        dz  d d d f         }|
                    |           ||_        |}	t#          j        |
d          }| j        s|st          j        |          }| j        r|S |d fS )Nr   r   rO   r   Tr^   r/   )r   listr   zipr   rf   ra   rG   roundrb   r   r   r   lastconv_trbiasrs   r5   catrt   )r<   r   skipr   ry   rz   r{   r|   ratiosstartoutsratior   r   limitframesr}   r~   r   sr$   _s                         r%   rB   zMultiWrap.forward   s   g1b!d'((A3.44 -	 -	LE5y ,'1,A::EFFb5j 1 122EBzzc	"B):$:el#JQ#NOOF!VaZ5<$??%BSSEzzu}q(((5%.((({{{UBK{{{aaaE%K*+A::a!QQ00AA::a!Q300AEE!HH%%% 11EL@A::EEb5j 1 122Ez!
aaaE%K'(AAAuU{*+q!T**Q 3HQQQEL=>>1222AAAqqq-5<-/05=3E3J3J1bRSUV3W3WWY222aaaELMM12CA::aaa$7el]a%7$7:;CA::aaaELA$5$6$69:CC   !
i!$$$y 	 	&++C9 	J9r'   )NN)rC   rD   rE   rF   r2   rB   rJ   rK   s   @r%   r   r      sV         $ $ $ $ $4: : : : : : : :r'   r   c                   @     e Zd Zdddddddddi dddf fd	Zd Z xZS )	HDecLayerFrN   rO   r   Tc                    t                                                       d }|
rfd}|r|dz  }nd}|| _        || _        || _        || _        || _        || _        || _        |
| _	        || _
        t          j        }t          j        }|r |dg}|dg}t          j        }t          j        } |||||          | _         ||          | _        | j        rdS d| _        |rT|r ||d|z  dd|z  z   d|          | _        n! ||d|z  ddd|z  z   gdd|g          | _         |d|z            | _        d| _        |	rt+          |fi || _        dS dS )zW
        Same as HEncLayer but for decoder. See `HEncLayer` for documentation.
        c                 (    t          j                    S r@   rR   rT   s    r%   rV   z$HDecLayer.__init__.<locals>.<lambda>  rW   r'   c                 .    t          j        |           S r@   rY   r[   s    r%   rV   z$HDecLayer.__init__.<locals>.<lambda>
  r]   r'   rO   r   r   Nr^   )r1   r2   r   r   r`   rk   rc   rb   ra   rd   r   r   r_   ConvTranspose1dre   ConvTranspose2dr   ri   rh   rg   rj   r   )r<   rk   rl   r   ra   rb   r\   rc   r`   rj   rd   rm   rn   r   r   rh   ro   rp   klass_trr>   s         `            r%   r2   zHDecLayer.__init__  s    	)) 	=<<<<G 	"CCC			
&	(	% 	*&*Ka[FIE)Hxe[&AAWU^^
: 	F 	+ 3$uT1t8QW_aQQ$uT1t8aQ[5I1&'\ 3  3 T**DJ
 	1t00x00DJJJ	1 	1r'   c                    | j         r@|                                dk    r(|j        \  }}}|                    || j        d|          }| j        s||z   }| j        r=t          j        | 	                    |                     |                    d          }n|}| j
        r| j         r9|j        \  }}}}|                    dddd                              d||          }| 
                    |          }| j         r.|                    ||||                              dddd          }n|}|J |                     |                     |                    }	| j         r"| j        r|	d| j        | j         d d f         }	nA|	d| j        | j        |z   f         }	|	j        d         |k    sJ |	j        d         |f            | j        st          j        |	          }	|	|fS )Nrr   r   r   r/   r   r^   .)r`   r0   r   rs   rk   rc   rh   r   rw   rg   rj   ru   rv   ri   r   r   r   rt   )
r<   r   r   r   ry   rz   r|   r~   r{   r   s
             r%   rB   zHDecLayer.forward0  s   9 	,AgGAq!q$)R++Az 	 DA| E$**T\\!__551===z @9 @"#'KAq"a		!Q1--55b!Q??AJJqMM9 @q"a++33Aq!Q??AA<<<JJt||A''9 	@x 2c48TXI-qqq01#tx6 1112A72;&(((172;*?(((y 	q		A!tr'   r   rK   s   @r%   r   r      sl        ).AaUV^c$T1rt"D-1 -1 -1 -1 -1 -1^      r'   r   c                        e Zd ZdZe	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	            Zd ZddZd Zd Z	d Z
d Z xZS )HDemucsaV  
    Spectrogram and hybrid Demucs model.
    The spectrogram model has the same structure as Demucs, except the first few layers are over the
    frequency axis, until there is only 1 frequency, and then it moves to time convolutions.
    Frequency layers can still access information across time steps thanks to the DConv residual.

    Hybrid model have a parallel time branch. At some layer, the time branch has the same stride
    as the frequency branch and then the two are combined. The opposite happens in the decoder.

    Models can either use naive iSTFT from masking, Wiener filtering ([Ulhih et al. 2017]),
    or complex as channels (CaC) [Choi et al. 2020]. Wiener filtering is based on
    Open Unmix implementation [Stoter et al. 2019].

    The loss is always on the temporal domain, by backpropagating through the above
    output methods and iSTFT. This allows to define hybrid models nicely. However, this breaks
    a bit Wiener filtering, as doing more iteration at test time will change the spectrogram
    contribution, without changing the one from the waveform, which will lead to worse performance.
    I tried using the residual option in OpenUnmix Wiener implementation, but it didn't improve.
    CaC on the other hand provides similar performance for hybrid, and works naturally with
    hybrid models.

    This model also uses frequency embeddings are used to improve efficiency on convolutions
    over the freq. axis, following [Isik et al. 2020] (https://arxiv.org/pdf/2008.04470.pdf).

    Unlike classic Demucs, there is no resampling here, and normalization is always applied.
    r^   0   N   r   FT   皙?
   rN   rO   r   -C6?皙?D  (   c$                    t                                                       |
| _        |	| _        || _        || _        || _        || _        || _        || _	        || _
        |"| _        |#| _        || _        |dz  | _        || _        || _        d| _        || _        || _        |r|s
J d            |r||k    sJ t)          j                    | _        t)          j                    | _        |r0t)          j                    | _        t)          j                    | _        |}$|$}%| j        r|%dz  }%|p|}&|}'|dz  }(t5          |          D ],})|)|k    }*|)|k    }+|)|k    },|(dk    }-|}.|}/|-s|(dk    sJ |dz  }/|}.d}0d}1|-r|(|k    r|(}/d}0d}1|/|.|-|0|,|||*|+||| ddd	}2t7          |2          }3d
|3d<   ||3d<   ||3d<   d|3d<   t7          |2          }4d}5|r|)|k     rd}5d|4d<   |1rt9          |&|'          }'|'}&t;          |%|'f|dz  |d|2}6|r1|-r/t;          |$|&f|dz  ||1d|3}7| j                            |7           |5rt?          |6|          }6| j                            |6           |)d
k    r*| j        tA          | j                  z  }$|$}%| j        r|%dz  }%tC          |'|%f|dz  |)d
k    |d|4}8|5rt?          |8|          }8|r7|-r5tC          |&|$f|dz  |1|)d
k    |d|3}9| j        "                    d
|9           | j        "                    d
|8           |&}$|'}%tG          ||&z            }&tG          ||'z            }'|-r|(|k    rd}(n|(|z  }(|)d
k    r!|rtI          |(|%||          | _        || _%        .|!rtM          | |!           dS dS )aa  
        Args:
            sources (list[str]): list of source names.
            audio_channels (int): input/output audio channels.
            channels (int): initial number of hidden channels.
            channels_time: if not None, use a different `channels` value for the time branch.
            growth: increase the number of hidden channels by this factor at each layer.
            nfft: number of fft bins. Note that changing this require careful computation of
                various shape parameters and will not work out of the box for hybrid models.
            wiener_iters: when using Wiener filtering, number of iterations at test time.
            end_iters: same but at train time. For a hybrid model, must be equal to `wiener_iters`.
            wiener_residual: add residual source before wiener filtering.
            cac: uses complex as channels, i.e. complex numbers are 2 channels each
                in input and output. no further processing is done before ISTFT.
            depth (int): number of layers in the encoder and in the decoder.
            rewrite (bool): add 1x1 convolution to each layer.
            hybrid (bool): make a hybrid time/frequency domain, otherwise frequency only.
            hybrid_old: some models trained for MDX had a padding bug. This replicates
                this bug to avoid retraining them.
            multi_freqs: list of frequency ratios for splitting frequency bands with `MultiWrap`.
            multi_freqs_depth: how many layers to wrap with `MultiWrap`. Only the outermost
                layers will be wrapped.
            freq_emb: add frequency embedding after the first frequency layer if > 0,
                the actual value controls the weight of the embedding.
            emb_scale: equivalent to scaling the embedding learning rate
            emb_smooth: initialize the embedding with a smooth one (with respect to frequencies).
            kernel_size: kernel_size for encoder and decoder layers.
            stride: stride for encoder and decoder layers.
            time_stride: stride for the final time layer, after the merge.
            context: context for 1x1 conv in the decoder.
            context_enc: context for 1x1 conv in the encoder.
            norm_starts: layer at which group norm starts being used.
                decoder layers are numbered in reverse order.
            norm_groups: number of groups for group norm.
            dconv_mode: if 1: dconv in encoder only, 2: decoder only, 3: both.
            dconv_depth: depth of residual DConv branch.
            dconv_comp: compression of DConv branch.
            dconv_attn: adds attention layers in DConv branch starting at this layer.
            dconv_lstm: adds a LSTM layer in DConv branch starting at this layer.
            dconv_init: initial scale for the DConv branch LayerScale.
            rescale: weight recaling trick

        rO   Nz%hybrid_old must come with hybrid=Truer^   r   TF)lstmattndepthcompressinitrt   )ra   rb   r`   r   rd   rh   r\   rn   r   r`   ra   rb   r   r   )rj   rm   )rj   rm   rc   )rj   r   rm   )rj   rc   r   rm   )r=   r-   )	reference)'r1   r2   cacwiener_residualaudio_channelssourcesra   rm   rb   r   channels
sampleratesegmentnfft
hop_lengthwiener_iters	end_itersfreq_embhybrid
hybrid_oldr   r   encoderdecodertencodertdecoderr   dictr   rM   r   r   r   r   insertrG   r)   freq_emb_scaler	   );r<   r   r   r   channels_timegrowthr   r   r   r   r   r   rh   r   r   multi_freqsmulti_freqs_depthr   	emb_scale
emb_smoothra   time_striderb   rm   context_encnorm_startsr\   
dconv_modedconv_depth
dconv_comp
dconv_attn
dconv_lstm
dconv_initrescaler   r   rk   chin_zrl   chout_zfreqsindexr   r   rd   r`   strikerr   	last_freqkwkwtkw_decmultienctencdectdecr>   s;                                                             r%   r2   zHDemucs.__init__m  s	   r 	.,&
 $	!)("$ 	CBBBBB6 	-9,,,,}} 	,MOODMMOODM8 	aKF)	5\\ X	/ X	/EJ&DJ&DK'D19DDC #zzzz!Ao"CI !,, 	  #"*  ( *&   B" r((CCK!,C"CMCJ"XXFE /u'888).~&  eW--FG M",q.+M MIKM MC +$ + u 9JNK'09 9479 9$$T*** 2[11L$$$zz*S->->>8  aKFGV H:>!&!WH H@FH HC 2[11 .$ .  JJN)&+qj'J JEHJ J$$Q---L3'''DF''E&7*++G %K''EEf$Ezzhz /6*I!G !G !G&.# 	447333333	4 	4r'   c                 "   | j         }| j        }|}| j        r||dz  k    sJ t          t	          j        |j        d         |z                      }|dz  dz  }| j        s)t          |||||z  z   |j        d         z
  fd          }n&t          |||||z  z   |j        d         z
  f          }t          |||          dd dd d f         }| j        r:|j        d         |dz   k    sJ |j        |j        |f            |ddd|z   f         }|S )NrO   r   r^   rr   r   )r   .)
r   r   r   rG   mathceilr   r   r&   r   )r<   r   hlr   r   r}   r   r   s           r%   _speczHDemucs._specK  s8   _y; 	A ????TYqwr{R/0011B'A+C? A!c3b=172;#>?iPPP!c3b=172;#>?@@AtR  crc111-; 	72;"q&(((17AGR*@(((#q2v+Ar'   c                    | j         d|z  z  }t          j        |d          }| j        rt          j        |d          }|dz  dz  }| j        s.|t          t          j        ||z                      z  d|z  z   }n'|t          t          j        ||z                      z  }t          |||          }| j        s|d|||z   f         }n|dd |f         }nt          |||          }|S )NrO   )r   r   r   r   )r^   r^   r^   rr   )r   .)	r   r   r   r   r   rG   r   r   r   )r<   r   r   r-   r   r   r}   r   s           r%   _ispeczHDemucs._ispecf  s   _e,E!\""; 	(a  A'A+C? 6#di44555C?#di44555Br***A? $c3sV|++,c7F7lOB''Ar'   c                     | j         rS|j        \  }}}}t          j        |                              ddddd          }|                    ||dz  ||          }n|                                }|S )Nr   r   rO   r^   rr   )r   r   r5   view_as_realru   rv   abs)r<   r   ry   rz   r{   r|   r   s          r%   
_magnitudezHDemucs._magnitudey  sr     8 	'KAq"a"1%%--aAq!<<A		!QUB**AAAr'   c                    | j         }| j        rg|j        \  }}}}}|                    ||dd||                              dddddd          }	t          j        |	                                          }	|	S | j        r| j	        }|dk     r)|d d d f         }|d|
                                z   z  |z  S |                     |||          S )	Nr   r^   r   r   rO      rr   g:0yE>)r   r   r   rs   ru   r5   view_as_complex
contiguoustrainingr   r   _wiener)
r<   r   r   nitersry   Srz   r{   r|   r$   s
             r%   _maskzHDemucs._mask  s     "8 	WNAq!R&&Ar1b!,,44Q1aAFFC'(8(899CJ= 	$^FA::!!!T'
Aquuww'!++<<1f---r'   c                 V   |j         }d}| j        }|j        \  }}}	}
}|                    ddddd          }t	          j        |                    dddd                    }g }t          |          D ]}d}g }t          d||          D ]a}t          |||z             }t          |||f         |||f         ||          }|	                    |
                    dd	                     b|	                    t	          j        |d
                     t	          j        t	          j        |d                    }|                    ddddd                                          }|r|d d d df         }t          |j                  |||	|
|gk    sJ |                    |          S )Ni,  r   rO   rr   r^   r   )residualr   r/   )dtyper   r   ru   r5   r   r   slicer   r   	transposer   r  stackr  r   r:   )r<   mag_outmix_stftr  r   wiener_win_lenr	  ry   r  rz   Fqr|   r   sampleposr$   framez_outs                     r%   r  zHDemucs._wiener  s   ~' 1aQ//!Q1a00%h&6&6q!Q&B&BCCAhh 		/ 		/FCCQ>22 4 4c3#788FEM*HVU],CV%' ' ' 

5??2r223333KK	#1---....#EKa$8$899kk!Q1a((3355 	aaa"f+CCI1aB"22222vvd||r'   c                 j	   |}|j         d         }|                     |          }|                     |                              |j                  }|}|j         \  }}}}	|                    dd          }
|                    dd          }||
z
  d|z   z  }| j        r;|}|                    dd          }|                    dd          }||z
  d|z   z  }g }g }g }g }t          | j	                  D ]G\  }}|
                    |j         d                    d }| j        ro|t          | j                  k     rW|
                    |j         d                    | j        |         } ||          }|j        s|
                    |           n|} |||          }|dk    r| j        {t          j        |j         d         |j        	          }|                     |                                          d d d d d d f                             |          }|| j        |z  z   }|
                    |           It          j        |          }| j        rt          j        |          }t          | j                  D ]\  }}|                    d          } ||||                    d                    \  }}| j        r| j        t          | j                  z
  }| j        r||k    r| j        ||z
           }|                    d          }|j        r@|j         d
         dk    sJ |j                     |d d d d df         } ||d |          \  }}|                    d          } ||||          \  }}t          |          dk    sJ t          |          dk    sJ t          |          dk    sJ t          | j                  } |                    || d||	          }||d d d f         z  |
d d d f         z   }|j        j        dk    }!|!r|                                }|                     ||          }"|                     |"|          }|!r|                    d          }| j        r9|                    || d|          }||d d d f         z  |d d d f         z   }||z   }|S )Nr   )r   r^   rr   T)r0   keepdimgh㈵>)r   r^   r   r
  )devicer^   r   mps)r   r   r   r:   r  meanstdr   	enumerater   r   r   r   rc   r   r5   r9   t	expand_asr   
zeros_liker   popr   r   r   rs   typecpur  r   )#r<   mixr   r   r   magry   rz   r  r|   r  r  xtmeantstdtsavedsaved_tlengths	lengths_tidxencoderx   r   frsembdecoder   preoffsetr   length_tr   r  x_is_mpszouts#                                      r%   rB   zHDemucs.forward  s   JJsOOooa  ##CJ//g1b! vv)Tv22ee	4e00X$*% ; 	.BGGG55E66fd633Du*-B 	$T\22 	 	KCNN172;'''F{  sS%7%777  "...}S)T"XXz  NN2&&&&  Fq&!!AaxxDM5 l172;qx@@@mmC((**,,T111aaa-=>HHKK+c11LLOOOOQ; 	%!!$$B %T\22 	5 	5KC99R==DVAtW[[__55FAs { 9c$-&8&88{ 	5sf}}}S6\2$==,,: 59Q<1,,,ci,,,aaaAg,C DdH55EB";;r??D DT844EB 5zzQ9~~""""7||q    FF1aR##AAAtGtAAAtG}, 8=E) 	Azz!QKKf%%  	UA; 	Ar6**Bd111d7m#eAAAtGn4BQAr'   )"r^   r   Nr^   r   r   r   FTr   TTFNr^   r   r   TrN   r^   rO   r   r   rO   rO   r   r^   rO   rO   rO   r   r   r   r   )Nr   )rC   rD   rE   rF   r
   r2   r   r   r   r  r  rB   rJ   rK   s   @r%   r   r   R  s)        4  !"#!&!!#$  !Y[4 [4 [4 [4 [4 \[4z  6   &	 	 	. . ."  8i i i i i i ir'   r   )r   r   )!rF   copyr   r   typingtpopenunmix.filteringr   r5   r   torch.nnr   r   demucsr   r	   statesr
   specr   r   TensorTuplerG   strrH   r&   Moduler)   rM   r   r   r    r'   r%   <module>rD     s               & & & & & &        $ $ $ $ $ $ ) ) ) ) ) ) ) )             # # # # # # # # U\ RXc3h%7 s X]    (    bi   4X X X X X	 X X Xv] ] ] ] ]	 ] ] ]@O O O O O	 O O OdH H H H Hbi H H H H Hr'   