
    ^jc                        d Z ddlZddlmZ ddlmZ ddlZddlmZ ddl	mc m
Z ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZmZ ddlmZ ddlmZ e G d de             Ze G d de             Ze G d de             Z  G d dejB                        Z" G d dejB                        Z# G d dejB                        Z$ G d dejB                        Z% G d dejB                        Z& G d d ejB                        Z' G d! d"ejB                        Z( G d# d$ejB                        Z)e G d% d&e             Z* ed'(       G d) d*e*             Z+d*d&gZ,y)+zTransformers Xcodec model.    N)	dataclass)	lru_cache   )initialization)conv1d_output_length)PreTrainedAudioTokenizerBase)Unpack)ModelOutputTransformersKwargsauto_docstringcan_return_tuple   )	AutoModel   )XcodecConfigc                   b    e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   y)XcodecOutputao  
    Args:
        audio_codes (`torch.LongTensor`  of shape `(batch_size, num_quantizers, codes_length)`, *optional*):
            Discrete code indices computed using `model.encode`.
        audio_values (`torch.FloatTensor` of shape `(batch_size, channels, num_samples)`, *optional*)
            Decoded audio values obtained using the decoder part of Xcodec.
    Naudio_codesaudio_values)
__name__
__module____qualname____doc__r   torch
LongTensor__annotations__r   FloatTensor     u/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/xcodec/modeling_xcodec.pyr   r   &   s3     ,0K!!D(/-1L%##d*1r   r   c                   :    e Zd ZU dZdZej                  dz  ed<   y)XcodecEncoderOutputz
    Args:
        audio_codes (`torch.LongTensor`  of shape `(batch_size, num_quantizers, codes_length)`, *optional*):
            Discrete code indices computed using `model.encode`.
    Nr   )r   r   r   r   r   r   r   r   r   r   r    r"   r"   4   s     ,0K!!D(/r   r"   c                   :    e Zd ZU dZdZej                  dz  ed<   y)XcodecDecoderOutputz
    Args:
        audio_values (`torch.FloatTensor`  of shape `(batch_size, channels, num_samples)`, *optional*):
            Decoded audio values obtained using the decoder part of Xcodec.
    Nr   )r   r   r   r   r   r   r   r   r   r   r    r$   r$   ?   s     .2L%##d*1r   r$   c                   l     e Zd ZdZdedededef fdZdej                  dej                  fd	Z	 xZ
S )
XcodecResidualUnitzFResidual block for SemanticEncoder and SemanticDecoder used in Xcodec.configin_channelsout_channelsdilationc           
         t         |           t        j                         | _        |j
                  dz
  dz  |z  }t        j                  |||j
                  d||dd      | _        t        j                  ||dd      | _        y )Nr   r   F)stridepaddingr*   groupsbias)r(   r)   kernel_sizer/   )	super__init__nnELU
activationunit_kernel_sizeConv1dconv1conv2)selfr'   r(   r)   r*   r-   	__class__s         r    r2   zXcodecResidualUnit.__init__M   s}    &&(++a/A5AYY##	

 YY<l`ahmn
r   hidden_statereturnc                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }||z   S N)r5   r8   r9   )r:   r<   output_tensors      r    forwardzXcodecResidualUnit.forward]   sE    5

=16

=1m++r   )r   r   r   r   r   intr2   r   TensorrA   __classcell__r;   s   @r    r&   r&   J   sH    Po| o# oS o\_ o ,ELL ,U\\ ,r   r&   c                   h     e Zd Zdedededef fdZdej                  dej                  fdZ xZ	S )	XcodecSemanticEncoderBlockr'   r(   r)   r,   c                    t         |           t        j                  |j                  D cg c]  }t        ||||       c}      | _        |dk(  rdnd|z  }|dz
  dz  }t        j                  |||||d      | _        y c c}w )Nr   r   r   Tr0   r,   r-   r/   )	r1   r2   r3   
ModuleListblock_dilationsr&   	res_unitsr7   conv)	r:   r'   r(   r)   r,   r*   kernelr-   r;   s	           r    r2   z#XcodecSemanticEncoderBlock.__init__f   s    \b\r\rsPX[(Ks

 kF
A:!#IIk<VTZdkrvw	 ts   Br<   r=   c                 Z    | j                   D ]
  } ||      } | j                  |      }|S r?   )rL   rM   r:   r<   units      r    rA   z"XcodecSemanticEncoderBlock.forwardq   s3    NN 	.D-L	.yy.r   
r   r   r   r   rB   r2   r   rC   rA   rD   rE   s   @r    rG   rG   e   sE    	x| 	x# 	xS 	xZ] 	xELL U\\ r   rG   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )SemanticEncoderc                     t         |           t        |j                        t        |j                        k7  rt        d      t        j                  |j                  |j                  |j                  d|j                  dz  d      | _
        |j                  }g }t        |j                        D ]>  \  }}t        |j                  |j                  |   z        }|t        ||||      gz  }|}@ t        j                  |      | _        y )Nz:Number of strides must match the number of channel_ratios.r   r   Fr/   )r1   r2   lenstrideschannel_ratios
ValueErrorr3   r7   semantic_hidden_sizer0   rM   	enumeraterB   rG   rJ   conv_blocks)r:   r'   r(   r]   ir,   r)   r;   s          r    r2   zSemanticEncoder.__init__y   s    v~~#f&;&;"<<YZZII''''!#
	 11"6>>2 	'IAvv::V=R=RST=UUVL6v{LZ`abbK&K	'
 ==5r   r<   r=   c                 Z    | j                  |      }| j                  D ]
  } ||      } |S r?   )rM   r]   r:   r<   blocks      r    rA   zSemanticEncoder.forward   s5    yy.%% 	/E .L	/r   r   r   r   r2   r   rC   rA   rD   rE   s   @r    rT   rT   x   s#    6,ELL U\\ r   rT   c                   h     e Zd Zdedededef fdZdej                  dej                  fdZ xZ	S )	SemanticDecoderBlockr'   r(   r)   r,   c                 h   t         	|           |dk(  r!t        j                  ||dddd      | _        n:d|z  }|dz   dz  }|dz  dk(  rdnd}t        j
                  ||||||d      | _        t        j                  |j                  D cg c]  }t        ||||       c}      | _	        y c c}w )	Nr   r   TrI   r   r   FrV   )
r1   r2   r3   r7   rM   ConvTranspose1drJ   rK   r&   rL   )
r:   r'   r(   r)   r,   r0   r-   output_paddingr*   r;   s
            r    r2   zSemanticDecoderBlock.__init__   s    Q;		DI f*Kza'G"(1*/QqN**\;^cDI ^d^t^tuRZlHMu
us   B/r<   r=   c                 Z    | j                  |      }| j                  D ]
  } ||      } |S r?   )rM   rL   rP   s      r    rA   zSemanticDecoderBlock.forward   s3    yy.NN 	.D-L	.r   rR   rE   s   @r    rd   rd      s@    
| 
# 
S 
Z] 
.ELL U\\ r   rd   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )SemanticDecoderc                     t         |           t        j                  |j                  t        |j                  |j                  d   z        |j                  d|j                  dz  d      | _        g }t        |j                        D ]  \  }}t        |j                  |j                  |   z        }|t        |j                        dz
  k  r)t        |j                  |j                  |dz      z        }n|j                  }|t        ||||      gz  } t        j                  |      | _        t        j                  |j                  |j                  |j                  d|j                  dz  d      | _        y )Nr   r   r   F)r(   r)   r0   r,   r-   r/   )r,   r-   r/   )r1   r2   r3   r7   r[   rB   rY   r0   r8   r\   rX   rW   rd   rJ   r]   r9   )r:   r'   r]   r^   r,   r(   r)   r;   s          r    r2   zSemanticDecoder.__init__   s_   YY33V886;P;PQR;SST**&&!+

 "6>>2 	]IAvf99F<Q<QRS<TTUKC--.23"6#>#>AVAVWX[\W\A]#]^%::0lTZ[\\K	] ==5YY''''&&!+

r   r<   r=   c                 |    | j                  |      }| j                  D ]
  } ||      } | j                  |      }|S r?   )r8   r]   r9   r`   s      r    rA   zSemanticDecoder.forward   sC    zz,/%% 	/E .L	/zz,/r   rb   rE   s   @r    rj   rj      s#    
>ELL U\\ r   rj   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )XcodecEuclideanCodebookz!Codebook with Euclidean distance.c                    t         |           t        j                  |j                  |j
                        }|j                  | _        | j                  dt        j                  dg             | j                  dt        j                  |j                               | j                  d|       | j                  d|j                                y )NinitedTcluster_sizeembed	embed_avg)	r1   r2   r   zeroscodebook_sizecodebook_dimregister_bufferrC   clone)r:   r'   rr   r;   s      r    r2   z XcodecEuclideanCodebook.__init__   s    F00&2E2EF#11Xu||TF';<^U[[9M9M-NOWe,[%++-8r   c                    | j                   j                         }|j                  d      j                  dd      }|d|z  |z  z
  |j                  d      j                  dd      z    }|j	                  d      j
                  }|S )Nr   r   T)keepdimr   dim)rr   tpowsummaxindices)r:   hidden_statesrr   scaled_statesdist	embed_inds         r    quantizez XcodecEuclideanCodebook.quantize   s    

%))!,00D0A]!2U!::UYYq\=M=MaY]=M=^^_HHH$,,	r   c                     |j                   }|j                  d|d   f      }| j                  |      } |j                  |d d  }|S )Nr{   )shapereshaper   view)r:   r   r   r   s       r    encodezXcodecEuclideanCodebook.encode   sO    ##%--r59o>MM-0	"INNE#2J/	r   c                     t        j                  |j                  | j                  j                        | j                        }|S r?   )F	embeddingtorr   device)r:   r   	quantizeds      r    decodezXcodecEuclideanCodebook.decode   s/    KK	TZZ->-> ?L	r   )	r   r   r   r   r2   r   r   r   rD   rE   s   @r    rn   rn      s    +9r   rn   c                   4     e Zd ZdZdef fdZd Zd Z xZS )XcodecVectorQuantizationzY
    Vector quantization implementation. Currently supports only euclidean distance.
    r'   c                 B    t         |           t        |      | _        y r?   )r1   r2   rn   codebook)r:   r'   r;   s     r    r2   z!XcodecVectorQuantization.__init__  s    /7r   c                 b    |j                  ddd      }| j                  j                  |      }|S Nr   r   r   )permuter   r   )r:   r   embed_ins      r    r   zXcodecVectorQuantization.encode  s/    %--aA6==''6r   c                 b    | j                   j                  |      }|j                  ddd      }|S r   )r   r   r   )r:   r   r   s      r    r   zXcodecVectorQuantization.decode  s/    ==''	2##Aq!,r   )	r   r   r   r   r   r2   r   r   rD   rE   s   @r    r   r      s    8| 8
r   r   c                        e Zd ZdZdef fdZd ZddefdZdde	j                  de	j                  fdZd	e	j                  de	j                  fd
Z xZS ) XcodecResidualVectorQuantizationzv
    Residual vector quantization implementation. Follows Algorithm 1 in https://huggingface.co/papers/2107.03312
    r'   c                    t         |           t        j                  t	        |j
                        D cg c]  }t        |       c}      | _        |j                  | _        |j                  | _	        |j
                  | _        y c c}w r?   )
r1   r2   r3   rJ   rangenum_quantizersr   
quantizers
frame_rateru   )r:   r'   _r;   s      r    r2   z)XcodecResidualVectorQuantization.__init__  sk    --SXY_YnYnSo(pa)A&)I(pq ++#11$33 )qs   Bc                 `    t        j                  | j                        | j                  z  dz  S )zReturn bandwidth per quantizer.i  )mathlog2ru   r   )r:   s    r    get_bandwidth_per_quantizerz<XcodecResidualVectorQuantization.get_bandwidth_per_quantizer  s%    yy++,t>EEr   r=   c           	          | j                         }| j                  }|0|dkD  r+t        t        dt	        j
                  ||z                    }|S )z:Return num_quantizers based on specified target bandwidth.        r   )r   r   rB   r   r   floor)r:   	bandwidthbw_per_qr   s       r     get_num_quantizers_for_bandwidthzAXcodecResidualVectorQuantization.get_num_quantizers_for_bandwidth#  sL    335,, Y_ Q

9x3G(H!IJNr   
embeddingsc                     | j                  |      }|}g }| j                  d| D ]:  }|j                  |      }|j                  |      }||z
  }|j	                  |       < t        j                  |      }	|	S )a  
        Encode the input tensor into discrete indices using RVQ, with the number of quantizers selected based on the given bandwidth.
        Each quantizer /codebook residually quantizes the input and returns the nearest indices in terms of Euclidian distance.
        N)r   r   r   r   appendr   stack)
r:   r   r   r   residualall_indices	quantizerr   r   out_indicess
             r    r   z'XcodecResidualVectorQuantization.encode+  s    
 >>yI.9 	(I&&x0G!((1I)+Hw'		(
 kk+.r   codesc                     t        j                  d|j                        }t        |      D ]C  \  }}| j                  |   }|j                  |      }||j                  |j                        z   }E |S )z9Decode the given codes to their quantized representation.r   )r   )r   tensorr   r\   r   r   r   )r:   r   quantized_outr^   r   r   r   s          r    r   z'XcodecResidualVectorQuantization.decode;  sm    S>#E* 	GJAw*I!((1I)ILL,FFM	G r   r?   )r   r   r   r   r   r2   r   rB   r   r   rC   r   r   rD   rE   s   @r    r   r     s\    4| 4F#  %,,  ELL U\\ r   r   c                   x    e Zd ZdZeZdZdZdZdgZ	 e
j                         d        Zd Zd Zed	        ZddZy
)XcodecPreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    xcodecinput_valuesaudior   c                    t        |t        j                        rct        j                  |j
                  d| j                  j                         |j                   t        j                  |j                         yyt        |t        j                  t        j                  f      r?t        j                  |j                         t        j                  |j
                         yt        |t        j                        rt        j                  |j
                         |j                  `t        j                   |j"                  |j$                  |j&                  d   z  z        }t        j(                  |j                  | |       yy|j*                  j,                  dk(  r t        j                  |j.                         yt        |t        j0                        r|j3                          yt        |t        j4                        r#t        j                  |j
                  dd       yt        |t6              r|j8                  j;                         D ]^  }t        |t        j                        st        j<                  |j
                  d       t        j>                  |j                  d       ` |j@                  j;                         D ]^  }t        |t        j                        st        j<                  |j
                  d       t        j>                  |j                  d       ` yt        |tB              rt        jD                  |jF                  tI        jJ                  d	g             t        j                  |jL                         t        j                  |jN                         t        j                  |jP                         yy)
zInitialize the weightsr   )meanstdNr   )abSnake1dg{Gz?)r   T))
isinstancer3   Linearinitnormal_weightr'   initializer_ranger/   zeros_	LayerNorm	GroupNormones_r7   kaiming_normal_r   sqrtr.   r(   r0   uniform_r;   r   alpharf   reset_parameters	EmbeddingXcodecModelacoustic_encodermodulestrunc_normal_	constant_acoustic_decoderrn   copy_rp   r   rC   rq   rr   rs   )r:   modulek	submodules       r    _init_weightsz#XcodecPreTrainedModel._init_weightsR  sv    fbii(LLSdkk6S6ST{{&FKK( 'r|| <=KK$JJv}}%		*  /{{&IIfmmv/A/AFDVDVWXDY/YZ[fkkaR15 ' &&)3JJv||$ 2 23##%-LLSd;, $44<<> 6	i3&&y'7'7TBNN9>>156 $44<<> 6	i3&&y'7'7TBNN9>>156  78JJv}}ellD6&:;KK++,KK%KK(()	 9r   c                 8   t         j                  j                  j                  j                  } || j
                  j                          || j
                  j                         | j
                  j                  D ]`  } ||j                         |j                  |j                  |j                  fD ]&  } ||j                          ||j                         ( b  || j                  j                  d        || j                  j                  d       | j                  j                  D ]f  } ||j                  d       |j                  |j                  |j                  fD ]*  } ||j                  d        ||j                  d       , h y)znApply weight norm in the acoustic encoder and decoder because the original checkpoint has weight norm applied.r   nameN)r   r3   utilsparametrizationsweight_normr   r8   r9   ra   	res_unit1	res_unit2	res_unit3r   conv_t1)r:   r   ra   res_units       r    apply_weight_normz'XcodecPreTrainedModel.apply_weight_normx  s9   hhnn55AAD))//0D))//0**00 	,E$"__eoouO ,HNN+HNN+,	, 	D))//h?D))//h?**00 	;EH5"__eoouO ;HNN:HNN:;	;r   c                    | j                   | j                  fD ]  }|j                         D ]  }	 t        j                  j
                  j                  |d       t        |d      s<d|j                  v sKt        j                  j
                  j                  j                  |dd         y# t        t        f$ r Y hw xY w)z=Remove the weight norm from the acoustic encoder and decoder.r   r   r   T)leave_parametrizedN)r   r   r   r   r3   r   remove_weight_normrZ   AttributeErrorhasattrr   parametrizeremove_parametrizations)r:   r   ms      r    r   z(XcodecPreTrainedModel.remove_weight_norm  s    ,,d.C.CD 	mF^^% mHHNN55ah5G 101h!BTBT6THHNN..FFq(gkFlm	m #N3 s   +B22CCc                 R    dt         j                  ffdt         |            S )zA
        Recursively iterate to fetch all Conv1d layers.
        r   c                     g }t        | t        j                        r|j                  |        | j	                         D ]  }|j                   |              |S r?   )r   r3   r7   r   childrenextend)r   params_listchildget_conv1d_layers_recursives      r    r   zMXcodecPreTrainedModel._get_conv1d_layers.<locals>.get_conv1d_layers_recursive  sZ    K&")),""6*  * G""#>u#EFG r   )r3   Moduletuple)r:   r   r   s     @r    _get_conv1d_layersz(XcodecPreTrainedModel._get_conv1d_layers  s&    
			 
	 0899r   Nc                 V    || }| j                  |      }|D ]  }t        ||      } |S )zo
        For a given module, compute the output length that would be obtained after all Conv1d layers.
        )r   r   )r:   input_lengthr   conv1d_layerslayers        r    _get_conv1d_output_lengthsz0XcodecPreTrainedModel._get_conv1d_output_lengths  sC     >F//7" 	EE/|DL	E r   r?   )r   r   r   r   r   config_classbase_model_prefixmain_input_nameinput_modalities_no_split_modulesr   no_gradr   r   r   r   r   r  r   r   r    r   r   E  sg    
  L $O;<U]]_#* #*J;,	m : :&r   r   z$The Xcodec neural audio codec model.)custom_introc                       e Zd Z fdZedej                  fd       Zdej                  dej                  fdZ
e	 	 ddej                  dedz  d	edz  dej                  ez  fd
       Ze	 ddej                  d	edz  dej                  ez  fd       Zee	 	 ddej                  dej                  dz  dedz  dee   deej                  ej                  f   ez  f
d              Z xZS )r   c                 8   t         |   |       || _        |j                  dz  | _        t        j                  |j                        }|j                  | _	        |j                  | _        | j                  | j                         t        |      | _        t        |      | _        t        j                  |j"                        j%                         | _        t)        j*                  |j,                  |j,                        | _        t)        j*                  |j,                  |j"                  j,                        | _        t)        j*                  |j,                  |j                  j,                        | _        t5        |      | _        | j9                          y )Nr   )r1   r2   r'   
hop_lengthpadr   from_configacoustic_model_configencoderr   decoderr   _adjust_dac_decoderrT   encoder_semanticrj   decoder_semanticsemantic_model_configevalsemantic_modelr3   r   hidden_sizefcfc1fc2r   r   	post_init)r:   r'   acoustic_modelr;   s      r    r2   zXcodecModel.__init__  s)    $$)"..v/K/KL . 6 6 . 6 6  !6!67 / 7 / 7'33F4P4PQVVX))F..0B0BC99V//1M1M1Y1YZ99V//1M1M1Y1YZ9&A 	r   r  c                 z   | j                         D ]]  }t        |t        j                        st        |j                  t
              r|j                  d   n|j                  }|dz  f|_        _ t        | d      r?t        | j                  t        j                        rt        j                         | _        yyy)z
        DAC implemented in Xcodec is slightly different from the HF version.
        DAC in Xcodec adjusts the output padding in every ConvTranspose1d in the decoder and removes
        the final `nn.Tanh` activation function.
        r   r   tanhN)r   r   r3   rf   r,   r   rg   r   r   TanhIdentity)r  r   r,   s      r    r  zXcodecModel._adjust_dac_decoder  s     oo' 	6F&""4"45-7u-Mq)SYS`S`)/!%	6 7F#
7<<(I;;=GL )J#r   r   r=   c                 T   |d d dd d f   }t        j                  || j                  | j                  f      }t        j                         5  | j	                  |d      }|j
                  }d d d        t        j                  d      }|j                  d      S # 1 sw Y   2xY w)Nr   T)output_hidden_statesr   r|   )r   r  r   r	  r  r   r   r   )r:   r   outputsr   stackeds        r    _extract_semantic_featuresz&XcodecModel._extract_semantic_features  s    #Aq!G,uu\DHHdhh+?@]]_ 	2)),T)RG#11M	2 ++m3|||""	2 	2s    BB'Nr   return_dictc                    ||n| j                   j                  }|j                  d   }|dk7  rt        d|       || j                   j                  d   }n>|| j                   j                  vr&t        d| d| j                   j                   d      | j                  |      j                         }| j                  |j                  dd            }| j                  |j                  d   | j                        |j                  d   k7  r<| j                  t        j                  || j                  | j                  f            }n| j                  |      }t        j                  |j                  |j                         |gd      }| j#                  |j                  dd            j                  dd      }| j$                  j'                  ||      }	|	j                  d	d      }	|s|	S t)        |	      S )
ac  
        input_values (`torch.FloatTensor` of shape `(batch_size, channels, num_samples)`):
            Float values of the input audio waveform.
        bandwidth (`float`, *optional*):
            The target bandwidth in (kbps) supports only values in `config.target_bandwidths`.
            Defaults to the highest available bandwidth `4.0` kbps.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`].

        Returns:
            `torch.LongTensor` of shape `(batch_size, num_quantizers, codes_length)` containing the discrete encoded audio codes.
        r   zAudio must be mono, but got r{   z)This model doesn't support the bandwidth z. Select one of .r   r|   r   )r'   r(  r   rZ   target_bandwidthsr'  detachr  	transposer  r   r   r  r   catr   r   r  r   r   r"   )
r:   r   r   r(  channelse_semantic_input
e_semantic
e_acousticr   r   s
             r    r   zXcodecModel.encode  s   & &1%<k$++BYBY%%a(q=;H:FGG55b9Idkk;;;;I;FVW[WbWbWtWtVuuvw   ::<HOOQ**+;+E+Ea+KL
 **<+=+=a+@$BWBWX\f\l\lmn\oo..quu\DHHdhhCW/XYJ..|<JYY
j.?.? @*MSTU
WWZ11!Q78BB1aH
nn++J	B!++Aq1";//r   r   c                 2   ||n| j                   j                  }|j                  dd      }| j                  j	                  |      }| j                  |j                  dd            j                  dd      }| j                  |      }|s|S t        |      S )a  
        audio_codes (`torch.LongTensor`  of shape `(batch_size, num_quantizers, codes_length)`):
            Discrete code indices computed using `model.encode`.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`]

        Returns:
            Decoded audio values of shape `(batch_size, channels, num_samples)` obtained using the decoder part of
            Xcodec.
        r   r   r   )r'   r(  r-  r   r   r  r   r$   )r:   r   r(  r   quantized_acousticr   s         r    r   zXcodecModel.decode  s      &1%<k$++BYBY!++Aq1NN))+6	!XXi&9&9!Q&?@JJ1aP,,-?@"<00r   kwargsc                     |j                   d   }|| j                  ||d      }| j                  |d      d   dd|f   }t        ||      S )	a+  
        input_values (`torch.FloatTensor` of shape `(batch_size, channels, num_samples)`):
            The raw float values of the input audio waveform.
        audio_codes (`torch.LongTensor`  of shape `(batch_size, num_quantizers, codes_length)`:
            Discrete code indices computed using `model.encode`.
        bandwidth (`float`, *optional*):
            Target bandwidth in kbps. Must be one of `config.target_bandwidths`. Defaults to the highest available bandwidth.
        bandwidth (`float`, *optional*):
            Target bandwidth in kbps. Must be one of `config.target_bandwidths`. Defaults to the highest available bandwidth.

        Returns:
            `XcodecOutput` or tuple `(audio_codes, audio_values)`:
            - `audio_codes` of shape `(batch_size, num_quantizers, codes_length)`: the quantized discrete codes.
            - `audio_values` of shape `(batch_size, channels, num_samples)`: the reconstructed audio waveform given the codes.

        Example:

        ```python
        >>> from datasets import load_dataset
        >>> from transformers import AutoFeatureExtractor, XcodecModel

        >>> model_id = "hf-audio/xcodec-hubert-librispeech"
        >>> model = XcodecModel.from_pretrained(model_id)
        >>> feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)

        >>> dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
        >>> dataset = dataset.cast_column("audio", Audio(sampling_rate=feature_extractor.sampling_rate))
        >>> audio_sample = dataset[0]['audio']['array']

        >>> inputs = feature_extractor(raw_audio=audio_sample, return_tensors="pt")

        >>> outputs = model(**inputs)
        >>> audio_codes = outputs.audio_codes
        >>> audio_values = outputs.audio_values
        ```
        r{   NF)r(  Tr   .)r   r   )r   r   r   r   )r:   r   r   r   r5  lengthr   s          r    rA   zXcodecModel.forward9  s`    Z ##B'++lI5+QK{{;D{A!DS'6'\R,OOr   )NNr?   )r   r   r   r2   staticmethodr3   r   r  r   r   r'  r   rC   floatboolr"   r   r$   r   r   r	   r   r   r   rA   rD   rE   s   @r    r   r     so   & )RYY ) )#u7H7H #UM^M^ #  #'#'	10ll10 4<10 D[	10
 
+	+10 10f  $(1\\1 D[1 
+	+	1 16  ,0"&	2Pll2P \\D(2P 4<	2P
 +,2P 
u||U\\)	*\	92P  2Pr   r   )-r   r   dataclassesr   	functoolsr   r   torch.nnr3   torch.nn.functional
functionalr    r   r   audio_utilsr   modeling_utilsr   processing_utilsr	   r   r
   r   r   r   autor   configuration_xcodecr   r   r"   r$   r   r&   rG   rT   rd   rj   rn   r   r   r   r   __all__r   r   r    <module>rG     sh   !  !      & / : &   . 
2; 
2 
2 0+ 0 0 2+ 2 2, ,6 &bii <299 >%bii %Pbii @ryy ,/ryy /d s8 s sl GHpP' pP IpPf 1
2r   