
    %	&h                       d dl Z d dlmZmZmZ d dlZd dl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 ddlmZmZ dd	lmZmZmZ dd
lmZ ddlmZmZmZmZmZ ddl m!Z!  e       rddlm"Z"  ejF                  e$      Z%dZ&dZ' G d de	jP                        Z) G d de	jP                        Z* G d de	jP                        Z+ G d de	jP                        Z, G d de	jP                        Z- G d de	jP                        Z. G d de	jP                        Z/ G d d e	jP                        Z0 G d! d"e0      Z1 G d# d$e0      Z2 G d% d&e	jP                        Z3e0e2e1d'Z4 G d( d)e	jP                        Z5 G d* d+e	jP                        Z6 G d, d-e	jP                        Z7 G d. d/e	jP                        Z8 G d0 d1e	jP                        Z9 G d2 d3e      Z:	 	 dLd4ee;e;f   d5e<d6e;d7eejz                     d8e;d9ej|                  fd:Z?d;Z@d<ZA ed=e@       G d> d?e:             ZBdZCd@ZDdAZE edBe@       G dC dDe:             ZFdEZGdFZHdGZI edHe@       G dI dJe:             ZJg dKZKy)M    N)OptionalTupleUnion)CrossEntropyLoss   )ACT2FN)is_deepspeed_zero3_enabled)is_fsdp_managed_module)!flash_attn_supports_top_left_maskis_flash_attn_available)BaseModelOutputCausalLMOutputSequenceClassifierOutput)PreTrainedModel)add_code_sample_docstringsadd_start_docstrings%add_start_docstrings_to_model_forwardloggingreplace_return_docstrings   )HubertConfig)_flash_attention_forwardzfacebook/hubert-large-ls960-ftr   c                   $     e Zd Z fdZd Z xZS )HubertPositionalConvEmbeddingc                    t         |           t        j                  |j                  |j                  |j
                  |j
                  dz  |j                        | _        d | _        |j                  r&t        j                  |j                        | _        nt        j                  j                  }t        t        j                  j                  d      r$t        j                  j                  j                  }t               r(dd l}|j"                  j%                  | j                  j&                  d      5   || j                  dd      | _        d d d        t        | j                  d      rU| j                  j                  j&                  j(                  }| j                  j                  j&                  j*                  }n,| j                  j,                  }| j                  j.                  }|j"                  j1                  | |       |j"                  j1                  | |       n || j                  dd      | _        t3        |j
                        | _        t6        |j8                     | _        y # 1 sw Y   'xY w)	N   )kernel_sizepaddinggroupsweight_normr   modifier_rankweight)namedimparametrizations)super__init__nnConv1dhidden_sizenum_conv_pos_embeddingsnum_conv_pos_embedding_groupsconv
batch_normconv_pos_batch_normBatchNorm1dutilsr    hasattrr&   r	   	deepspeedzeroGatheredParametersr#   	original0	original1weight_gweight_vregister_external_parameterHubertSamePadLayerr   r   feat_extract_activation
activation)selfconfigr    r4   r9   r:   	__class__s         /var/www/pru.catia.catastroantioquia-mas.com/valormas/lib/python3.12/site-packages/transformers/models/hubert/modeling_hubert.pyr(   z&HubertPositionalConvEmbedding.__init__-   s   II6622a777
	 %% nnV-?-?@DO((..Krxx00-@ hh77CC)+ ^^66tyy7G7GWX6Y M +DIIH! LDIM499&89#yy99@@JJH#yy99@@JJH#yy11H#yy11H::4J::4J'		aH	)&*H*HI !?!?@M Ms   ?I??J	c                     |j                  dd      }| j                  | j                  |      }| j                  |      }| j                  |      }| j	                  |      }|j                  dd      }|S Nr   r   )	transposer/   r.   r   r>   r?   hidden_statess     rB   forwardz%HubertPositionalConvEmbedding.forwardR   sn    %//15??& OOM:M		-0]36%//15    __name__
__module____qualname__r(   rH   __classcell__rA   s   @rB   r   r   ,   s    #AJ	rI   r   c                   $     e Zd Z fdZd Z xZS )r<   c                 P    t         |           |dz  dk(  rd| _        y d| _        y )Nr   r   r   )r'   r(   num_pad_remove)r?   r,   rA   s     rB   r(   zHubertSamePadLayer.__init___   s)    #:Q#>!#CarI   c                 V    | j                   dkD  r|d d d d d | j                    f   }|S Nr   )rR   rF   s     rB   rH   zHubertSamePadLayer.forwardc   s6    ")!Q0F43F3F2F0F*FGMrI   rJ   rO   s   @rB   r<   r<   ^   s    KrI   r<   c                   &     e Zd Zd fd	Zd Z xZS )HubertNoLayerNormConvLayerc                 d   t         |           |dkD  r|j                  |dz
     nd| _        |j                  |   | _        t        j                  | j                  | j                  |j                  |   |j                  |   |j                        | _
        t        |j                     | _        y )Nr   r   r   stridebias)r'   r(   conv_dimin_conv_dimout_conv_dimr)   r*   conv_kernelconv_stride	conv_biasr.   r   r=   r>   r?   r@   layer_idrA   s      rB   r(   z#HubertNoLayerNormConvLayer.__init__j   s    <DqL6??8a<8a"OOH5II**84%%h/!!
	 !!?!?@rI   c                 J    | j                  |      }| j                  |      }|S N)r.   r>   rF   s     rB   rH   z"HubertNoLayerNormConvLayer.forwardx   s$    		-06rI   r   rJ   rO   s   @rB   rV   rV   i   s    ArI   rV   c                   &     e Zd Zd fd	Zd Z xZS )HubertLayerNormConvLayerc                    t         |           |dkD  r|j                  |dz
     nd| _        |j                  |   | _        t        j                  | j                  | j                  |j                  |   |j                  |   |j                        | _
        t        j                  | j                  d      | _        t        |j                     | _        y )Nr   r   rX   T)elementwise_affine)r'   r(   r[   r\   r]   r)   r*   r^   r_   r`   r.   	LayerNorm
layer_normr   r=   r>   ra   s      rB   r(   z!HubertLayerNormConvLayer.__init__   s    <DqL6??8a<8a"OOH5II**84%%h/!!
	 ,,t'8'8TR !?!?@rI   c                     | j                  |      }|j                  dd      }| j                  |      }|j                  dd      }| j                  |      }|S )N)r.   rE   rk   r>   rF   s     rB   rH   z HubertLayerNormConvLayer.forward   sV    		-0%//B76%//B76rI   re   rJ   rO   s   @rB   rg   rg   ~   s    ArI   rg   c                   &     e Zd Zd fd	Zd Z xZS )HubertGroupNormConvLayerc                    t         |           |dkD  r|j                  |dz
     nd| _        |j                  |   | _        t        j                  | j                  | j                  |j                  |   |j                  |   |j                        | _
        t        |j                     | _        t        j                  | j                  | j                  d      | _        y )Nr   r   rX   T)
num_groupsnum_channelsaffine)r'   r(   r[   r\   r]   r)   r*   r^   r_   r`   r.   r   r=   r>   	GroupNormrk   ra   s      rB   r(   z!HubertGroupNormConvLayer.__init__   s    <DqL6??8a<8a"OOH5II**84%%h/!!
	 !!?!?@,,$2C2CRVRcRclpqrI   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rd   )r.   rk   r>   rF   s     rB   rH   z HubertGroupNormConvLayer.forward   s2    		-066rI   re   rJ   rO   s   @rB   rp   rp      s    r rI   rp   c                   .     e Zd ZdZ fdZd Zd Z xZS )HubertFeatureEncoderz.Construct the features from raw audio waveformc           	         t         |           |j                  dk(  rDt        |d      gt	        |j
                  dz
        D cg c]  }t        ||dz          c}z   }nV|j                  dk(  r.t	        |j
                        D cg c]  }t        ||       }}nt        d|j                   d      t        j                  |      | _        d| _        d	| _        y c c}w c c}w )
Ngroupr   )rb   r   layerz`config.feat_extract_norm` is z), but has to be one of ['group', 'layer']FT)r'   r(   feat_extract_normrp   rangenum_feat_extract_layersrV   rg   
ValueErrorr)   
ModuleListconv_layersgradient_checkpointing_requires_grad)r?   r@   ir   rA   s       rB   r(   zHubertFeatureEncoder.__init__   s    ##w.3FQGHLQRXRpRpstRtLuLGH*6AEBL K %%0QVW]WuWuQvwA3FQGwKw01I1I0JJst  ==5&+#"L xs   C"	C'c                 J    | j                         D ]	  }d|_         d| _        y NF)
parametersrequires_gradr   r?   params     rB   _freeze_parametersz'HubertFeatureEncoder._freeze_parameters   s(    __& 	(E"'E	(#rI   c                 
   |d d d f   }| j                   r| j                  rd|_        | j                  D ]K  }| j                   r5| j                  r)| j                  r| j                  |j                  |      }D ||      }M |S )NT)r   trainingr   r   r   _gradient_checkpointing_func__call__)r?   input_valuesrG   
conv_layers       rB   rH   zHubertFeatureEncoder.forward   s    $QW- 4==*.M'** 	:J""t'B'Bt}} $ A A''!!
 !+= 9	: rI   )rK   rL   rM   __doc__r(   r   rH   rN   rO   s   @rB   rx   rx      s    8#"$
rI   rx   c                   $     e Zd Z fdZd Z xZS )HubertFeatureProjectionc                 n   t         |           |j                  | _        | j                  r3t        j                  |j
                  d   |j                        | _        t        j                  |j
                  d   |j                        | _
        t        j                  |j                        | _        y )Nrn   eps)r'   r(   feat_proj_layer_normr)   rj   r[   layer_norm_epsrk   Linearr+   
projectionDropoutfeat_proj_dropoutdropoutr?   r@   rA   s     rB   r(   z HubertFeatureProjection.__init__   s}    $*$?$?!$$ ll6??2+>FDYDYZDO))FOOB$79K9KLzz&":":;rI   c                     | j                   r| j                  |      }| j                  |      }| j                  |      }|S rd   )r   rk   r   r   rF   s     rB   rH   zHubertFeatureProjection.forward   s;    $$ OOM:M6]3rI   rJ   rO   s   @rB   r   r      s    <rI   r   c                       e Zd ZdZ	 	 	 	 	 ddededededededee   f fd	Z	d
e
j                  dedefdZ	 	 	 	 	 dde
j                  dee
j                     deee
j                        dee
j                     dee
j                     dedee
j                  ee
j                     eee
j                        f   fdZ xZS )HubertAttentionz=Multi-headed attention from 'Attention Is All You Need' paper	embed_dim	num_headsr   
is_decoderrZ   	is_causalr@   c                 
   t         |           || _        || _        || _        ||z  | _        || _        | j
                  |z  | j                  k7  rt        d| j                   d| d      | j
                  dz  | _        || _	        || _
        t        j                  |||      | _        t        j                  |||      | _        t        j                  |||      | _        t        j                  |||      | _        y )Nz;embed_dim must be divisible by num_heads (got `embed_dim`: z and `num_heads`: z).g      )rZ   )r'   r(   r   r   r   head_dimr@   r   scalingr   r   r)   r   k_projv_projq_projout_proj)	r?   r   r   r   r   rZ   r   r@   rA   s	           rB   r(   zHubertAttention.__init__   s     	""!Y.MMI%$..8MdnnM]$YKr3  }}d*$"ii	94@ii	94@ii	94@		)YTBrI   tensorseq_lenbszc                     |j                  ||| j                  | j                        j                  dd      j	                         S rD   )viewr   r   rE   
contiguousr?   r   r   r   s       rB   _shapezHubertAttention._shape  s7    {{3GQQRSUVWbbddrI   rG   key_value_statespast_key_valueattention_masklayer_head_maskoutput_attentionsreturnc                 
   |du}|j                         \  }}	}
| j                  |      | j                  z  }|r0|.|d   j                  d   |j                  d   k(  r|d   }|d   }n
|rE| j	                  | j                  |      d|      }| j	                  | j                  |      d|      }n|}| j	                  | j                  |      d|      }| j	                  | j                  |      d|      }t        j                  |d   |gd      }t        j                  |d   |gd      }nD| j	                  | j                  |      d|      }| j	                  | j                  |      d|      }| j                  r||f}|| j                  z  d| j                  f} | j	                  ||	|      j                  | } |j                  | } |j                  | }|j                  d      }t        j                  ||j                  dd            }|j                         || j                  z  |	|fk7  r/t!        d|| j                  z  |	|f d|j                                |{|j                         |d|	|fk7  r#t!        d	|d|	|f d|j                                |j                  || j                  |	|      |z   }|j                  || j                  z  |	|      }t"        j$                  j'                  |d      }||j                         | j                  fk7  r*t!        d
| j                  f d|j                                |j                  dddd      |j                  || j                  |	|      z  }|j                  || j                  z  |	|      }|r?|j                  || j                  |	|      }|j                  || j                  z  |	|      }nd}t"        j$                  j)                  || j(                  | j*                        }t        j                  ||      }|j                         || j                  z  |	| j                  fk7  r9t!        d|| j                  z  |	| j                  f d|j                                |j                  || j                  |	| j                        }|j                  dd      }|j                  ||	| j,                        }| j/                  |      }|||fS )#Input shape: Batch x Time x ChannelNr   r   r   rn   r%   z$Attention weights should be of size 	, but is z!Attention mask should be of size z/Head mask for a single layer should be of size )pr    `attn_output` should be of size )sizer   r   shaper   r   r   torchcatr   r   r   r   reshapebmmrE   r   r)   
functionalsoftmaxr   r   r   r   )r?   rG   r   r   r   r   r   is_cross_attentionr   tgt_len_query_states
key_statesvalue_states
proj_shapesrc_lenattn_weightsattn_weights_reshaped
attn_probsattn_outputs                       rB   rH   zHubertAttention.forward  s    .T9',,.Wa {{=1DLL@ *q!''*.>.D.DQ.GG (*J)!,LT[[1A%BBLJ;;t{{3C'Db#NL'T[[%?SIJ;;t{{='A2sKLN1$5z#BJJ 99nQ&7%FANL T[[%?SIJ;;t{{='A2sKL?? ),7NDNN*B>
Ct{{<#>CCZP'Z''4
+|++Z8//!$yyz/C/CAq/IJ3#7'"JJ6dnn8LgW^7_6` a %%'(* 
 %""$a'(BB 7a'8R7SS\]k]p]p]r\st  (,,S$..'7SVddL',,S4>>-A7GTL}},,\r,B&##%$..):: Et~~FWEX Y',,./1  +//2q!<|?P?PQTVZVdVdfmov?wwL',,S4>>-A7GTL
 %1$5$5c4>>7T[$\!055cDNN6JGU\]L$(!]]**<4<<RVR_R_*`
ii
L9#"6!OO2C$..4H'SWS`S`3a2b c$$&') 
 "&&sDNNGT]]S!++Aq1 "))#wGmmK01>AArI   )        FTFNNNNNF)rK   rL   rM   r   intfloatboolr   r   r(   r   Tensorr   r   rH   rN   rO   s   @rB   r   r      sM   G  )-CC C 	C
 C C C &C>eU\\ eC ec e 488<1526"'vB||vB #5<<0vB !u||!45	vB
 !.vB "%,,/vB  vB 
u||Xell3XeELL>Q5RR	SvBrI   r   c                   V    e Zd ZdZ fdZdej                  dedefdZ	 	 	 	 	 ddej                  de	ej                     d	e	e
ej                        d
e	ej                     de	ej                     dede
ej                  e	ej                     e	e
ej                        f   fdZ xZS )HubertFlashAttention2aH  
    Hubert flash attention module. This module inherits from `HubertAttention` as the weights of the module stays
    untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
    flash attention and deal with padding tokens in case the input contains any of them.
    c                 B    t        |   |i | t               | _        y rd   )r'   r(   r   _flash_attn_uses_top_left_mask)r?   argskwargsrA   s      rB   r(   zHubertFlashAttention2.__init__  s#    $)&)
 /P.Q+rI   r   r   r   c                 R    |j                  ||| j                  | j                        S rd   )r   r   r   r   s       rB   _reshapezHubertFlashAttention2._reshape  s    {{3GGrI   rG   r   r   r   r   r   r   c           
         |rt        d      |d u}|j                         \  }}	}
| j                  | j                  |      d|      }|rP|N|d   j                  d   |j                  d   k(  r,|d   j                  dd      }|d   j                  dd      }n*|rE| j                  | j                  |      d|      }| j                  | j                  |      d|      }n|| j                  | j                  |      d|      }| j                  | j                  |      d|      }t        j                  |d   j                  dd      |gd      }t        j                  |d   j                  dd      |gd      }nD| j                  | j                  |      d|      }| j                  | j                  |      d|      }| j                  r$|j                  dd      |j                  dd      f}|j                  d   }|||d   j                  d   z  }|j                  }|t        j                  k(  rt        j                         rt        j                         }nMt        | j                   d      r| j                   j"                  }n | j                  j$                  j                  }t&        j)                  d	| d
       |j+                  |      }|j+                  |      }|j+                  |      }t-        |||||	| j.                  r| j0                  nd| j2                  | j4                        }|j7                  ||	d      }| j9                  |      }|sd }||fS )NzBHubertFlashAttention2 attention does not support output_attentionsrn   r   r   r   r   rm   _pre_quantization_dtypezThe input hidden states seems to be silently casted in float32, this might be related to the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in .r   )r   r   use_top_left_mask)r   r   r   r   r   rE   r   r   r   r   r   dtypefloat32is_autocast_enabledget_autocast_gpu_dtyper3   r@   r   r#   loggerwarning_oncetor   r   r   r   r   r   r   )r?   rG   r   r   r   r   r   r   r   q_lenr   r   r   r   
kv_seq_leninput_dtypetarget_dtyper   r   s                      rB   rH   zHubertFlashAttention2.forward  s0    abb .T9%**,UA }}T[[%?SI *q!''*.>.D.DQ.GG (*44Q:J)!,66q!<Lt{{3C'Db#NJ==5E)FCPL't{{='A2sKJ==])CRMLN1$5$?$?1$Ez#RXYZJ 99nQ&7&A&A!Q&G%V\]^L t{{='A2sKJ==])CRML?? )221a8,:P:PQRTU:VWN%%b)
%.+11"55J #((%--'((*$;;=&?@#{{BB#{{1177 >$ (??<8L#|4J'??<8L.$(MMDLLsnn"AA	
 "))#ub9mmK0 LL.88rI   r   )rK   rL   rM   r   r(   r   r   r   r   r   r   r   rH   rN   rO   s   @rB   r   r     s    RHu|| Hc H H 488<1526"'i9||i9 #5<<0i9 !u||!45	i9
 !.i9 "%,,/i9  i9 
u||Xell3XeELL>Q5RR	Si9rI   r   c                   $    e Zd Z	 	 	 	 	 d	dej                  deej                     deeej                        deej                     deej                     dedeej                  eej                     eeej                        f   f fdZ xZ	S )
HubertSdpaAttentionrG   r   r   r   r   r   r   c                 z   |s|*t         j                  d       t        |   ||||||      S |du}|j	                         \  }}	}
| j                  |      }|r0|.|d   j                  d   |j                  d   k(  r|d   }|d   }n
|rE| j                  | j                  |      d|      }| j                  | j                  |      d|      }n|}| j                  | j                  |      d|      }| j                  | j                  |      d|      }t        j                  |d   |gd      }t        j                  |d   |gd      }nD| j                  | j                  |      d|      }| j                  | j                  |      d|      }| j                  r||f}| j                  ||	|      }| j                  r	||	dkD  rd	nd
}t        j                  j                  j!                  ||||| j"                  r| j$                  nd|      }|j	                         || j&                  |	| j(                  fk7  r7t+        d|| j&                  |	| j(                  f d|j	                                |j-                  dd      }|j/                  ||	| j0                        }| j3                  |      }|d|fS )r   Na  HubertModel is using HubertSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True` or `layer_head_mask` not None. Falling back to the manual attention implementation, but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.)r   r   r   r   r   r   r   r   rn   r   TFr   )	attn_mask	dropout_pr   r   r   )r   r   r'   rH   r   r   r   r   r   r   r   r   r   r   r)   r   scaled_dot_product_attentionr   r   r   r   r   rE   r   r   r   )r?   rG   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rA   s                   rB   rH   zHubertSdpaAttention.forward  s     ;l 7?!1-- /"3 #   .T9',,.Wa {{=1 *q!''*.>.D.DQ.GG (*J)!,LT[[1A%BBLJ;;t{{3C'Db#NL'T[[%?SIJ;;t{{='A2sKLN1$5z#BJJ 99nQ&7%FANL T[[%?SIJ;;t{{='A2sKL?? ),7N{{<#>
 !NN~/E'TU+D[`	 hh))FF$&*mmdll G 
 #t~~w!NN2CRVR_R_3`2a b$$&') 
 "++Aq1 "))#wGmmK0D.00rI   r   )
rK   rL   rM   r   r   r   r   r   rH   rN   rO   s   @rB   r   r     s     488<1526"'f1||f1 #5<<0f1 !u||!45	f1
 !.f1 "%,,/f1  f1 
u||Xell3XeELL>Q5RR	Sf1 f1rI   r   c                   $     e Zd Z fdZd Z xZS )HubertFeedForwardc                    t         |           t        j                  |j                        | _        t        j                  |j                  |j                        | _	        t        |j                  t              rt        |j                     | _        n|j                  | _        t        j                  |j                  |j                        | _        t        j                  |j                         | _        y rd   )r'   r(   r)   r   activation_dropoutintermediate_dropoutr   r+   intermediate_sizeintermediate_dense
isinstance
hidden_actstrr   intermediate_act_fnoutput_densehidden_dropoutoutput_dropoutr   s     rB   r(   zHubertFeedForward.__init__v  s    $&JJv/H/H$I!"$))F,>,>@X@X"Yf''-'-f.?.?'@D$'-'8'8D$IIf&>&>@R@RS jj)>)>?rI   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }|S rd   )r   r  r   r  r  rF   s     rB   rH   zHubertFeedForward.forward  sX    //>00?11-@))-8++M:rI   rJ   rO   s   @rB   r   r   u  s    @rI   r   )eagersdpaflash_attention_2c                   &     e Zd Z fdZddZ xZS )HubertEncoderLayerc                    t         |           t        |j                     |j                  |j
                  |j                  d      | _        t        j                  |j                        | _        t        j                  |j                  |j                        | _        t        |      | _        t        j                  |j                  |j                        | _        y )NFr   r   r   r   r   )r'   r(   HUBERT_ATTENTION_CLASSES_attn_implementationr+   num_attention_headsattention_dropout	attentionr)   r   r  r   rj   r   rk   r   feed_forwardfinal_layer_normr   s     rB   r(   zHubertEncoderLayer.__init__  s    1&2M2MN((00,,	
 zz&"7"78,,v'9'9v?T?TU-f5 "V-?-?VEZEZ [rI   c                     |}| j                  |||      \  }}}| j                  |      }||z   }| j                  |      }|| j                  |      z   }| j	                  |      }|f}|r||fz  }|S Nr   r   )r  r   rk   r  r  r?   rG   r   r   attn_residualr   r   outputss           rB   rH   zHubertEncoderLayer.forward  s    %)-.L] *8 *
&|Q ]3%56%(9(9-(HH--m< "&GrI   r   rJ   rO   s   @rB   r  r    s    \rI   r  c                   r     e Zd Z fdZ	 	 	 	 ddej
                  deej                     dededef
dZ	 xZ
S )	HubertEncoderc                    t         |           || _        t        |      | _        t        j                  |j                  |j                        | _	        t        j                  |j                        | _        t        j                  t        |j                        D cg c]  }t!        |       c}      | _        d| _        |j&                  dk(  | _        y c c}w Nr   Fr
  )r'   r(   r@   r   pos_conv_embedr)   rj   r+   r   rk   r   r  r   r   r}   num_hidden_layersr  layersr   r  _use_flash_attention_2r?   r@   r   rA   s      rB   r(   zHubertEncoder.__init__  s    ;FC,,v'9'9v?T?TUzz&"7"78mmvOgOgIh$iA%7%?$ij&+#&,&A&AEX&X# %j   !CrG   r   r   output_hidden_statesreturn_dictc                 4   |rdnd }|rdnd }||j                  d      j                  dd|j                  d         }d|| <   | j                  r|d|v r|nd }nd|d d d d d d f   j	                  |j
                        z
  }|t        j                  |j
                        j                  z  }|j                  |j                  d   d|j                  d   |j                  d         }| j                  |      }	||	z   }| j                  |      }| j                  |      }t               xs t        |       }
| j                  D ]  }|r||fz   }t        j                   g       }| j"                  r|| j$                  j&                  k  rdnd	}|r|
rG| j(                  r+| j"                  r| j+                  |j,                  |||      }n ||||
      }|d   }|rd}|s|d   fz   } |r||fz   }|st/        d |||fD              S t1        |||      S )N rn   r   r   r         ?r   TFr  NNc              3   &   K   | ]	  }||  y wrd   r)  .0vs     rB   	<genexpr>z(HubertEncoder.forward.<locals>.<genexpr>       mq_`_lm   last_hidden_staterG   
attentions)	unsqueezerepeatr   r#  r   r   r   finfominexpandr   rk   r   r	   r
   r"  randr   r@   	layerdropr   r   r   tupler   r?   rG   r   r   r&  r'  all_hidden_statesall_self_attentionsexpand_attention_maskposition_embeddingssynced_gpusr{   dropout_probabilityskip_the_layerlayer_outputss                  rB   rH   zHubertEncoder.forward  s[    #7BD$5b4%$2$<$<R$@$G$G1mNaNabcNd$e!45M001**4B4NSTXfSfmq "%~atQ6F'G'J'JQ^QdQd'J'e!e!/%++m>Q>Q2R2V2V!V!/!6!6"((+Q0D0DR0H.J^J^_aJb" #11-@%(;;6]302R6LT6R[[ 	PE#$58H$H! #(**R.%)]]8KdkkNcNc8cTjoN![..4==$($E$E%&)	%M %*%nXi%M !.a 0 , &9]1=M<O&O#7	P:   1]4D Dm]4EGZ$[mmm++*
 	
rI   NFFT)rK   rL   rM   r(   r   r   r   r   r   rH   rN   rO   s   @rB   r  r    s_    Y 26"'%* G
||G
 !.G
  	G

 #G
 G
rI   r  c                   >     e Zd Z fdZdej
                  fdZ xZS )HubertAttnAdapterLayerc                    t         |           |j                  | _        |j                  | _        t        j                  | j
                        | _        t        j                  | j
                  | j                        | _
        t        j                         | _        t        j                  | j                  | j
                        | _        y)z
        Implements adapter modules directly with 3D tensor weight as parameters and without using ModuleList to speed
        up training throughput.
        N)r'   r(   adapter_attn_dim	input_dimr+   
hidden_dimr)   rj   normr   linear_1ReLUact_fnlinear_2r   s     rB   r(   zHubertAttnAdapterLayer.__init__  s    
 	00 ,,LL1			$//4>>Bggi		$..$//BrI   rG   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }|S rd   )rO  rP  rR  rS  rF   s     rB   rH   zHubertAttnAdapterLayer.forward  s@    		-0m4M2m4rI   )rK   rL   rM   r(   r   FloatTensorrH   rN   rO   s   @rB   rJ  rJ    s    CU%6%6 rI   rJ  c                   f     e Zd Z fdZ	 	 ddej
                  deej
                     defdZ xZ	S )!HubertEncoderLayerStableLayerNormc                    t         |           t        |j                     |j                  |j
                  |j                  d      | _        t        j                  |j                        | _        t        j                  |j                  |j                        | _        t        |      | _        t        j                  |j                  |j                        | _        t%        |dd       t'        |      | _        y d | _        y )NFr  r   rL  )r'   r(   r  r  r+   r  r  r  r)   r   r  r   rj   r   rk   r   r  r  getattrrJ  adapter_layerr   s     rB   r(   z*HubertEncoderLayerStableLayerNorm.__init__&  s    1&2M2MN((00,,	
 zz&"7"78,,v'9'9v?T?TU-f5 "V-?-?VEZEZ [6-t4@!7!?D!%DrI   rG   r   r   c                 $   |}| j                  |      }| j                  |||      \  }}}| j                  |      }||z   }|| j                  | j	                  |            z   }| j
                  || j                  |      z   }|f}|r||fz  }|S r  )rk   r  r   r  r  rZ  r  s           rB   rH   z)HubertEncoderLayerStableLayerNorm.forward8  s     &6)-.L] *8 *
&|Q ]3%5%(9(9$:O:OP]:^(__))D,>,>},MMM "&GrI   r   )
rK   rL   rM   r(   r   r   r   r   rH   rN   rO   s   @rB   rW  rW  %  s>    &* 26"'	|| !.  	rI   rW  c                   .     e Zd Z fdZ	 	 	 	 ddZ xZS )HubertEncoderStableLayerNormc                    t         |           || _        t        |      | _        t        j                  |j                  |j                        | _	        t        j                  |j                        | _        t        j                  t        |j                        D cg c]  }t!        |       c}      | _        d| _        |j&                  dk(  | _        y c c}w r  )r'   r(   r@   r   r   r)   rj   r+   r   rk   r   r  r   r   r}   r!  rW  r"  r   r  r#  r$  s      rB   r(   z%HubertEncoderStableLayerNorm.__init__S  s    ;FC,,v'9'9v?T?TUzz&"7"78mm@EfF^F^@_`1.v6`
 ',#&,&A&AEX&X# ar%  c                 f   |rdnd }|rdnd }||j                  d      j                  dd|j                  d         }||j                  |j                        z  }| j
                  r|d|v r|nd }nd|d d d d d d f   j                  |j                        z
  }|t        j                  |j                        j                  z  }|j                  |j                  d   d|j                  d   |j                  d         }| j                  |      }	||	z   }| j                  |      }t               xs t        |       }
| j                  D ]  }|r||fz   }t        j                  g       }| j                   r|| j"                  j$                  k  rdnd	}|r|
rG| j&                  r+| j                   r| j)                  |j*                  |||      }n ||||
      }|d   }|rd}|s|d   fz   } | j-                  |      }|r||fz   }|st/        d |||fD              S t1        |||      S )Nr)  rn   r   r   r+  r   r*  TFr  r,  c              3   &   K   | ]	  }||  y wrd   r)  r.  s     rB   r1  z7HubertEncoderStableLayerNorm.forward.<locals>.<genexpr>  r2  r3  r4  )r7  r8  r   r   r   r#  r   r9  r:  r;  r   r   r	   r
   r"  r<  r   r@   r=  r   r   r   rk   r>  r   r?  s                  rB   rH   z$HubertEncoderStableLayerNorm.forward_  sn    #7BD$5b4%$2$<$<R$@$G$G1mNaNabcNd$e!),A,D,D=K^K^,D,__M**4B4NSTXfSfmq "%~atQ6F'G'J'JQ^QdQd'J'e!e!/%++m>Q>Q2R2V2V!V!/!6!6"((+Q0D0DR0H.J^J^_aJb" #11-@%(;;]302R6LT6R[[ 	PE#$58H$H! #(**R.%)]]8KdkkNcNc8cTjoN![ ..4==$($E$E%&)	%M %*%nXi%M !.a 0 , &9]1=M<O&O#9	P< 6 1]4D Dm]4EGZ$[mmm++*
 	
rI   rH  rJ   rO   s   @rB   r]  r]  R  s    
Y "I
rI   r]  c                   |    e Zd ZdZeZdZdZdZdZ	dZ
d Zdeej                  ef   fdZded	ej                  fd
Zy)HubertPreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    hubertr   Tc                 z   t        |t        j                        rm|j                  j                  j                  d| j                  j                         |j                  %|j                  j                  j                          yyt        |t        j                  t        j                  t        j                  f      rJ|j                  j                  j                          |j                  j                  j                  d       yt        |t        j                        r_t               rddl}t#        |d      r|t#        |d      rp|j$                  j'                  |j(                  |j*                  gd      5  t        j,                  j/                  |j                  j                         ddd       n|j$                  j'                  |j                  d      5  t        j,                  j/                  |j                  j                         ddd       n3t        j,                  j/                  |j                  j                         |j                  %|j                  j                  j                          yyt        |t0              r2t#        |d	      r%|j2                  j                  j5                          yyt        |t6              rMt#        |d
      r@|j8                  j                  j                  d| j                  j:                  dz   z         yyy# 1 sw Y   xY w# 1 sw Y   xY w)zInitialize the weightsr   )meanstdNr*  r   r:   r9   r!   masked_spec_embedlayer_weightsr   )r   r)   r   r#   datanormal_r@   initializer_rangerZ   zero_rj   ru   r1   fill_r*   r	   r4   r3   r5   r6   r:   r9   initkaiming_normal_HubertModelrg  uniform_HubertForSequenceClassificationrh  r!  )r?   moduler4   s      rB   _init_weightsz#HubertPreTrainedModel._init_weights  sP   fbii( MM&&CT[[5R5R&S{{&  &&( 'r||R^^ LMKK""$MM$$S)		*)+ 6:.76:3N"::FOOV__;]mn:o D//0B0BCD D #::6==XY:Z D//0B0BCD D ''(:(:;{{&  &&( ',v23((--668 4 ?@v/$$))//t{{7T7TWX7X0YZ 0 AD DD Ds   ?4L%#4L1%L.1L:input_lengthsc                     d }t        | j                  j                  | j                  j                        D ]  \  }} ||||      } |S )zH
        Computes the output length of the convolutional layers
        c                 >    t        j                  | |z
  |d      dz   S )Nfloor)rounding_moder   )r   div)input_lengthr   rY   s      rB   _conv_out_lengthzPHubertPreTrainedModel._get_feat_extract_output_lengths.<locals>._conv_out_length  s"     99\K7wWZ[[[rI   )zipr@   r^   r_   )r?   ru  r|  r   rY   s        rB    _get_feat_extract_output_lengthsz6HubertPreTrainedModel._get_feat_extract_output_lengths  sQ    
	\
 $'t{{'>'>@W@W#X 	QK,]KPM	Q rI   feature_vector_lengthr   c                    | j                  |j                  d            j                  t        j                        }|j
                  d   }t        j                  ||f|j                  |j                        }d|t        j                  |j
                  d   |j                        |dz
  f<   |j                  dg      j                  d      j                  dg      j                         }|S )Nrn   r   )r   devicer   )r  )r~  sumr   r   longr   zerosr   r  arangeflipcumsumr   )r?   r  r   output_lengths
batch_sizes        rB   "_get_feature_vector_attention_maskz8HubertPreTrainedModel._get_feature_vector_attention_mask  s    >>~?Q?QRT?UVYYZ_ZdZde#))!,
./~7K7KTbTiTi
 uv^%9%9!%<^EZEZ[]kno]opq',,bT299"=BBB4HMMOrI   N)rK   rL   rM   r   r   config_classbase_model_prefixmain_input_namesupports_gradient_checkpointing_supports_flash_attn_2_supports_sdpart  r   r   
LongTensorr   r~  r  r)  rI   rB   rb  rb    sh    
  L $O&*#!N[BeEDTDTVYDY>Z 
 
]b]m]m 
rI   rb  r   	mask_probmask_lengthr   	min_masksr   c                    | \  }dk  rt        d      kD  rt        d d d      t        j                  j                  d      j	                         fd}|-|j                         j                  d      j                         nt        |      D cg c]  } c}}t        j                  |ft        	      }	g }
 |      }|d
k(  r|	S |D ]  } ||      }t        j                  j                  t        j                  |dz
  z
        |d      }t        |      d
k(  rdz
  }n|d
   }t        j                  |t        j                  ||z
  t        j                   	      |z  g      }|
j#                  |        t        j$                  |
      }
t        j&                  |
dddddf   ||f      }
|
j)                  ||z        }
t        j                        ddddf   }t        j&                  |||f      j)                  ||z        }|
|z   }
|
j+                         dz
  kD  rdz
  |
|
dz
  kD  <   t        j,                  |	|
dd       |	S c c}w )af  
    Computes random mask spans for a given shape. Used to implement [SpecAugment: A Simple Data Augmentation Method for
    ASR](https://arxiv.org/abs/1904.08779). Note that this method is not optimized to run on TPU and should be run on
    CPU as part of the preprocessing during training.

    Args:
        shape: The shape for which to compute masks. This should be of a tuple of size 2 where
               the first element is the batch size and the second element is the length of the axis to span.
        mask_prob:  The percentage of the whole axis (between 0 and 1) which will be masked. The number of
                    independently generated mask spans of length `mask_length` is computed by
                    `mask_prob*shape[1]/mask_length`. Note that due to overlaps, `mask_prob` is an upper bound and the
                    actual percentage will be smaller.
        mask_length: size of the mask
        min_masks: minimum number of masked spans
        attention_mask: A (right-padded) attention mask which independently shortens the feature axis of
                        each batch dimension.
    r   z&`mask_length` has to be bigger than 0.zO`mask_length` has to be smaller than `sequence_length`, but got `mask_length`: z and `sequence_length`: `c                     t        | z  z  z         }t        |      }|z  kD  rz  }| dz
  z
  |k  rt        | dz
  z
  d      }|S )z;Given input length, compute how many spans should be maskedr   r   )r   max)r{  num_masked_spanepsilonr  r  r  sequence_lengths     rB   compute_num_masked_spanz6_compute_mask_indices.<locals>.compute_num_masked_span  so    i,6DwNOoy9 [(?:-<O ;?+o=!,+/"BAFOrI   Nrn   r+  r   F)replace)r   nprandomr<  itemdetachr  tolistr}   r  r   choicer  lenconcatenateonesint32appendarraybroadcast_tor   r  put_along_axis)r   r  r  r   r  r  r  r   ru  spec_aug_maskspec_aug_mask_idxsmax_num_masked_spanr{  r  spec_aug_mask_idxdummy_mask_idxoffsetsr  r  s    `` `            @@rB   _compute_mask_indicesr    s   0 #(JQABB_$]^i]j&&7q:
 	
 iinnQ$$&G $ % 	##B'..0',Z'89!o9  HHj/:$GM1/Ba% 51,? II,,IIlkAo67RW - 
  !Q& -q0N.q1NNN(;o(MUWU]U] ^ao op
 	!!"34/52 "45 1a:&5H+(V ,33J@SVa@ab ii$T4]3Goog
4G'UV^^'+5G ,g5 /A"55GVYZGZ-!0CCD m%7B?w :s   $	I+a!  
    Hubert was proposed in [HuBERT: Self-Supervised Speech Representation Learning by Masked Prediction of Hidden
    Units](https://arxiv.org/abs/2106.07447) by Wei-Ning Hsu, Benjamin Bolte, Yao-Hung Hubert Tsai, Kushal Lakhotia,
    Ruslan Salakhutdinov, Abdelrahman Mohamed.

    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving etc.).

    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`HubertConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
a  
    Args:
        input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`):
            Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file
            into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install
            soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and
            conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2Processor.__call__`] for details.
        attention_mask (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing convolution and attention on padding token indices. Mask values selected in `[0,
            1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)

            <Tip warning={true}>

            `attention_mask` should only be passed if the corresponding processor has `config.return_attention_mask ==
            True`. For all models whose processor has `config.return_attention_mask == False`, such as
            [hubert-base](https://huggingface.co/facebook/hubert-base-ls960), `attention_mask` should **not** be passed
            to avoid degraded performance when doing batched inference. For such models `input_values` should simply be
            padded with 0 and passed without `attention_mask`. Be aware that these models also yield slightly different
            results depending on whether `input_values` is padded or not.

            </Tip>

        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
z`The bare Hubert Model transformer outputting raw hidden-states without any specific head on top.c                   P    e Zd Zdef fdZ	 	 ddej                  deej                     deej                     fdZ	 e
e       eee      	 	 	 	 	 ddeej                     deej                     deej                     d	ee   d
ee   dee   deeef   fd              Z xZS )rp  r@   c                    t         |   |       || _        t        |      | _        t        |      | _        |j                  dkD  s|j                  dkD  rEt        j                  t        j                  |j                        j                               | _        |j                   rt#        |      | _        nt'        |      | _        | j)                          y )Nr   )r'   r(   r@   rx   feature_extractorr   feature_projectionmask_time_probmask_feature_probr)   	Parameterr   r   r+   rq  rg  do_stable_layer_normr]  encoderr  	post_initr   s     rB   r(   zHubertModel.__init__  s     !5f!="9&"A   3&&*B*BS*H%'\\%,,v?Q?Q2R2[2[2]%^D"&&7?DL(0DL 	rI   rG   mask_time_indicesr   c                    t        | j                  dd      s|S |j                         \  }}}|)| j                  j	                  |j
                        ||<   n| j                  j                  dkD  r| j                  rt        ||f| j                  j                  | j                  j                  || j                  j                        }t        j                  ||j                  t        j                        }| j                  j	                  |j
                        ||<   | j                  j                  dkD  r| j                  rt        ||f| j                  j                  | j                  j                   | j                  j"                        }t        j                  ||j                  t        j                        }|dddf   j%                  d|d      }d||<   |S )	z
        Masks extracted features along time axis and/or along feature axis according to
        [SpecAugment](https://arxiv.org/abs/1904.08779).
        apply_spec_augmentTNr   )r  r  r   r  )r  r   )r  r  r  rn   )rY  r@   r   rg  r   r   r  r   r  mask_time_lengthmask_time_min_masksr   r   r  r   r  mask_feature_lengthmask_feature_min_masksr;  )r?   rG   r  r   r  r  r+   mask_feature_indicess           rB   _mask_hidden_stateszHubertModel._mask_hidden_states  s    t{{$8$?   4A3E3E3G0
O[(/3/E/E/H/HI\I\/]M+,[[''!+ 5_-++44 KK88-++99! !&->}G[G[chcmcm n/3/E/E/H/HI\I\/]M+,;;((1,#8[)++77 KK;;++<<	$  $)<<0D]MaMainisis#t #74#@#G#GO]_#` 23M./rI   )output_typer  r   r   r&  r'  r   c                    ||n| j                   j                  }||n| j                   j                  }||n| j                   j                  }| j	                  |      }|j                  dd      }|| j                  |j                  d   |      }| j                  |      }| j                  ||      }| j                  |||||      }	|	d   }|s	|f|	dd z   S t        ||	j                  |	j                        S )aZ  

        Returns:

        Example:

        ```python
        >>> from transformers import AutoProcessor, HubertModel
        >>> from datasets import load_dataset
        >>> import soundfile as sf

        >>> processor = AutoProcessor.from_pretrained("facebook/hubert-large-ls960-ft")
        >>> model = HubertModel.from_pretrained("facebook/hubert-large-ls960-ft")


        >>> def map_to_array(batch):
        ...     speech, _ = sf.read(batch["file"])
        ...     batch["speech"] = speech
        ...     return batch


        >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
        >>> ds = ds.map(map_to_array)

        >>> input_values = processor(ds["speech"][0], return_tensors="pt").input_values  # Batch size 1
        >>> hidden_states = model(input_values).last_hidden_state
        ```Nr   r   )r  r   r   r&  r'  r   r4  )r@   r   r&  use_return_dictr  rE   r  r   r  r  r  r   rG   r6  )
r?   r   r   r  r   r&  r'  extract_featuresrG   encoder_outputss
             rB   rH   zHubertModel.forward  s,   L 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B]11,?+55a;%!DDEUE[E[\]E^`noN//0@A00Rc0d,,)/!5# ' 
 (*!#oab&999+)77&11
 	
rI   r,  NNNNN)rK   rL   rM   r   r(   r   rU  r   r  r  r   HUBERT_INPUTS_DOCSTRINGr   r   _CONFIG_FOR_DOCr   r   r   r   rH   rN   rO   s   @rB   rp  rp    s   
| * :>59	,((, $E$5$56, !!1!12	,\ ++BC?Y 269=,0/3&*E
u||,E
 !.E
 $E$5$56	E

 $D>E
 'tnE
 d^E
 
uo%	&E
 Z DE
rI   rp  z['MISTER QUILTER IS THE APOSTLE OF THE MIDDLE CLASSES AND WE ARE GLAD TO WELCOME HIS GOSPEL'gGz6@zdHubert Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC).c                       e Zd Zddee   f fdZd Zd Zd Zd Z	 e
e       eeeeee      	 	 	 	 	 ddeej&                     d	eej&                     d
ee   dee   dee   deej&                     deeef   fd              Z xZS )HubertForCTCtarget_langc                    t         |   |       t        |      | _        t	        j
                  |j                        | _        || _        |j                  t        d| j                   d      t        |d      r|j                  r|j                  n|j                  }t	        j                   ||j                        | _        | j%                          y )NzYou are trying to instantiate z with a configuration that does not define the vocabulary size of the language model head. Please instantiate the model as follows: `HubertForCTC.from_pretrained(..., vocab_size=vocab_size)`. or define `vocab_size` of your model's configuration.add_adapter)r'   r(   rp  rc  r)   r   final_dropoutr   r  
vocab_sizer   rA   r3   r  output_hidden_sizer+   r   lm_headr  )r?   r@   r  r  rA   s       rB   r(   zHubertForCTC.__init__@  s     !&)zz&"6"67&$00@ AH H  *1)GFL^L^F%%djdvdv 	 yy!3V5F5FG 	rI   c                     | j                   }|&t        | j                  dd      t        d| d      |-t        | j                  dd      t        j                  d       y|| j                  |d       yy)a'  
        This method overwrites [`~PreTrainedModel.tie_weights`] so that adapter weights can be correctly loaded when
        passing `target_lang=...` to `from_pretrained(...)`.

        This method is **not** supposed to be called by the user and is prone to be changed in the future.
        NrL  zCannot pass `target_lang`: z- if `config.adapter_attn_dim` is not defined.z)By default `target_lang` is set to 'eng'.T)
force_load)r  rY  r@   r   r   infoload_adapter)r?   r  s     rB   tie_weightszHubertForCTC.tie_weightsW  s     &&"wt{{<NPT'U']:;-Gtuvv WT[[:Ld%S%_KKCD$kd; %rI   c                 X    t        j                  dt               | j                          y)
        Calling this function will disable the gradient computation for the feature encoder so that its parameter will
        not be updated during training.
        The method `freeze_feature_extractor` is deprecated and will be removed in Transformers v5. Please use the equivalent `freeze_feature_encoder` method instead.NwarningswarnFutureWarningfreeze_feature_encoderr?   s    rB   freeze_feature_extractorz%HubertForCTC.freeze_feature_extractorl  '    
 	Q	

 	##%rI   c                 L    | j                   j                  j                          yr  Nrc  r  r   r  s    rB   r  z#HubertForCTC.freeze_feature_encoderx      
 	%%88:rI   c                 P    | j                   j                         D ]	  }d|_         yz
        Calling this function will disable the gradient computation for the base model so that its parameters will not
        be updated during training. Only the classification head will be updated.
        FNrc  r   r   r   s     rB   freeze_base_modelzHubertForCTC.freeze_base_model  (    
 [[++- 	(E"'E	(rI   )
checkpointr  r  expected_outputexpected_lossr   r   r   r&  r'  labelsr   c           
         ||n| j                   j                  }|I|j                         | j                   j                  k\  r"t	        d| j                   j                         | j                  |||||      }|d   }| j                  |      }| j                  |      }	d}
|b||n$t        j                  |t        j                        }| j                  |j                  d            j                  t        j                        }|dk\  }|j                  d      }|j                  |      }t        j                   j#                  |	dt        j$                        j'                  dd      }t        j(                  j*                  j-                  d	
      5  t        j                   j/                  ||||| j                   j0                  | j                   j2                  | j                   j4                        }
ddd       |s|	f|t6        d z   }|
|
f|z   S |S t9        |
|	|j:                  |j<                        S # 1 sw Y   ExY w)a  
        labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*):
            Labels for connectionist temporal classification. Note that `target_length` has to be smaller or equal to
            the sequence length of the output logits. Indices are selected in `[-100, 0, ..., config.vocab_size - 1]`.
            All labels set to `-100` are ignored (masked), the loss is only computed for labels in `[0, ...,
            config.vocab_size - 1]`.
        Nz$Label values must be <= vocab_size: r  r   r+  rn   )r%   r   r   F)enabled)blank	reductionzero_infinitylosslogitsrG   r6  )r@   r  r  r  r   rc  r   r  r   	ones_liker  r~  r  r   masked_selectr)   r   log_softmaxr   rE   backendscudnnflagsctc_losspad_token_idctc_loss_reductionctc_zero_infinity_HIDDEN_STATES_START_POSITIONr   rG   r6  )r?   r   r   r   r&  r'  r  r  rG   r  r  ru  labels_masktarget_lengthsflattened_targets	log_probsoutputs                    rB   rH   zHubertForCTC.forward  s'   0 &1%<k$++B]B]&**,$++2H2H"HCDKKDZDZC[\]]++)/!5#  
  
]3m, #1"<%//R^fkfpfpBq  !AA.BTBTUWBXY\\]b]g]ghM !A+K(__R0N & 4 4[ A 11&b1V``abdefI%%++E+: 	}}--%!"++22"kk<<"&++"?"? . 	 Y)F)G!HHF)-)9TGf$EvEfG4I4IV]VhVh
 	
	 	s   A#IIrd   r  )rK   rL   rM   r   r  r(   r  r  r  r  r   r  r   _CHECKPOINT_FOR_DOCr   r  _CTC_EXPECTED_OUTPUT_CTC_EXPECTED_LOSSr   r   r   r   r   rH   rN   rO   s   @rB   r  r  ;  s    
HSM .<*
&;( ++BC&"$,( 26,0/3&*)-D
u||,D
 !.D
 $D>	D

 'tnD
 d^D
 &D
 
un$	%D
 DD
rI   r  zsuperb/hubert-base-superb-ksz'_unknown_'g(\!@z
    Hubert Model with a sequence classification head on top (a linear layer over the pooled output) for tasks like
    SUPERB Keyword Spotting.
    c                       e Zd Z fdZd Zd Zd Z ee       e	e
eedee      	 	 	 	 	 ddeej"                     deej"                     d	ee   d
ee   dee   deej"                     deeef   fd              Z xZS )rr  c                    t         |   |       t        |d      r|j                  rt	        d      t        |      | _        |j                  dz   }|j                  r0t        j                  t        j                  |      |z        | _        t        j                  |j                  |j                         | _        t        j                  |j                   |j$                        | _        | j)                          y )Nr  z]Sequence classification does not support the use of Hubert adapters (config.add_adapter=True)r   )r'   r(   r3   r  r   rp  rc  r!  use_weighted_layer_sumr)   r  r   r  rh  r   r+   classifier_proj_size	projector
num_labels
classifierr  )r?   r@   
num_layersrA   s      rB   r(   z(HubertForSequenceClassification.__init__  s     6=)f.@.@o  "&)--1
((!#ejj.Dz.Q!RD6#5#5v7R7RS))F$?$?ARARS 	rI   c                 X    t        j                  dt               | j                          y)z
        Calling this function will disable the gradient computation for the feature encoder so that its parameters will
        not be updated during training.
        r  Nr  r  s    rB   r  z8HubertForSequenceClassification.freeze_feature_extractor  r  rI   c                 L    | j                   j                  j                          yr  r  r  s    rB   r  z6HubertForSequenceClassification.freeze_feature_encoder   r  rI   c                 P    | j                   j                         D ]	  }d|_         yr  r  r   s     rB   r  z1HubertForSequenceClassification.freeze_base_model  r  rI   audio)r  r  r  modalityr  r  r   r   r   r&  r'  r  r   c                 <   ||n| j                   j                  }| j                   j                  rdn|}| j                  |||||      }| j                   j                  rr|t           }t        j                  |d      }t        j                  j                  | j                  d      }	||	j                  ddd      z  j                  d      }n|d   }| j                  |      }||j                  d      }
n| j                  |j                   d   |      }|j#                  d      j%                  dd|j                   d         }d	|| <   |j                  d      |j                  d      j                  dd      z  }
| j'                  |
      }d}|Ft)               } ||j                  d| j                   j*                        |j                  d            }|s|f|t        d z   }||f|z   S |S t-        |||j.                  |j0                  
      S )a  
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        NTr  r   r   rn   r   r   r   r  )r@   r  r  rc  r  r   stackr)   r   r   rh  r   r  r  re  r  r   r7  r8  r  r   r  r   rG   r6  )r?   r   r   r   r&  r'  r  r  rG   norm_weightspooled_outputpadding_maskexpand_padding_maskr  r  loss_fctr  s                    rB   rH   z'HubertForSequenceClassification.forward  s   2 &1%<k$++B]B]'+{{'I'ItOc++)/!5#  
 ;;--#$ABM!KK1=M==001C1C0LL*\->->r1a-HHMMRSMTM#AJM}5!)..1.5MBB=CVCVWXCY[ijL"."8"8"<"C"CAq-J]J]^_J`"a25M../)--!-4|7G7GA7G7N7S7STVXY7ZZM/')HFKKDKK,B,BCV[[QS_UDY)F)G!HHF)-)9TGf$EvE'!//))	
 	
rI   r  )rK   rL   rM   r(   r  r  r  r   r  r   _SEQ_CLASS_CHECKPOINTr   r  _SEQ_CLASS_EXPECTED_OUTPUT_SEQ_CLASS_EXPECTED_LOSSr   r   r   r   r   r   rH   rN   rO   s   @rB   rr  rr    s    "
&;( ++BC(,$2. 26,0/3&*)-<
u||,<
 !.<
 $D>	<

 'tn<
 d^<
 &<
 
u..	/<
 D<
rI   rr  )r  rr  rp  rb  rT   )Lr  typingr   r   r   numpyr  r   torch.nnr)   r   activationsr   integrations.deepspeedr	   integrations.fsdpr
   modeling_flash_attention_utilsr   r   modeling_outputsr   r   r   modeling_utilsr   r2   r   r   r   r   r   configuration_hubertr   r   
get_loggerrK   r   r  r  Moduler   r<   rV   rg   rp   rx   r   r   r   r   r   r  r  r  rJ  rW  r]  rb  r   r   r  ndarrayr  HUBERT_START_DOCSTRINGr  rp  r  r  r  r  r$  r%  r&  rr  __all__r)  rI   rB   <module>r6     s    ) )    % ! @ 7 h Y Y -  / J 
		H	% 7  !/BII /d  *ryy 6ryy 0)299 )Xbii $[Bbii [B|{9O {9|g1/ g1T		 2 .    FR
BII R
jRYY 2*		 *ZV
299 V
rGO G\ 26tc?tt t U--.	t
 t ZZtn &# L fH
' H
	H
V !"  u   nT
( T
	T
n 7 *    r
&; r
r
j frI   