
    sg_I                         d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
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  ej,                  e      Z G d de      Z G d de      Zy)zBART model configuration    N)OrderedDict)AnyMappingOptional   )PreTrainedTokenizer)PretrainedConfig)
OnnxConfigOnnxConfigWithPastOnnxSeq2SeqConfigWithPast) compute_effective_axis_dimension)
TensorTypeis_torch_availableloggingc                   l     e Zd ZdZdZdgZdddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Z xZS )	
BartConfiga  
    This is the configuration class to store the configuration of a [`BartModel`]. It is used to instantiate a BART
    model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
    defaults will yield a similar configuration to that of the BART
    [facebook/bart-large](https://huggingface.co/facebook/bart-large) architecture.

    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
    documentation from [`PretrainedConfig`] for more information.


    Args:
        vocab_size (`int`, *optional*, defaults to 50265):
            Vocabulary size of the BART model. Defines the number of different tokens that can be represented by the
            `inputs_ids` passed when calling [`BartModel`] or [`TFBartModel`].
        d_model (`int`, *optional*, defaults to 1024):
            Dimensionality of the layers and the pooler layer.
        encoder_layers (`int`, *optional*, defaults to 12):
            Number of encoder layers.
        decoder_layers (`int`, *optional*, defaults to 12):
            Number of decoder layers.
        encoder_attention_heads (`int`, *optional*, defaults to 16):
            Number of attention heads for each attention layer in the Transformer encoder.
        decoder_attention_heads (`int`, *optional*, defaults to 16):
            Number of attention heads for each attention layer in the Transformer decoder.
        decoder_ffn_dim (`int`, *optional*, defaults to 4096):
            Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
        encoder_ffn_dim (`int`, *optional*, defaults to 4096):
            Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
        activation_function (`str` or `function`, *optional*, defaults to `"gelu"`):
            The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
            `"relu"`, `"silu"` and `"gelu_new"` are supported.
        dropout (`float`, *optional*, defaults to 0.1):
            The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
        attention_dropout (`float`, *optional*, defaults to 0.0):
            The dropout ratio for the attention probabilities.
        activation_dropout (`float`, *optional*, defaults to 0.0):
            The dropout ratio for activations inside the fully connected layer.
        classifier_dropout (`float`, *optional*, defaults to 0.0):
            The dropout ratio for classifier.
        max_position_embeddings (`int`, *optional*, defaults to 1024):
            The maximum sequence length that this model might ever be used with. Typically set this to something large
            just in case (e.g., 512 or 1024 or 2048).
        init_std (`float`, *optional*, defaults to 0.02):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        encoder_layerdrop (`float`, *optional*, defaults to 0.0):
            The LayerDrop probability for the encoder. See the [LayerDrop paper](see https://arxiv.org/abs/1909.11556)
            for more details.
        decoder_layerdrop (`float`, *optional*, defaults to 0.0):
            The LayerDrop probability for the decoder. See the [LayerDrop paper](see https://arxiv.org/abs/1909.11556)
            for more details.
        scale_embedding (`bool`, *optional*, defaults to `False`):
            Scale embeddings by diving by sqrt(d_model).
        use_cache (`bool`, *optional*, defaults to `True`):
            Whether or not the model should return the last key/values attentions (not used by all models).
        num_labels (`int`, *optional*, defaults to 3):
            The number of labels to use in [`BartForSequenceClassification`].
        forced_eos_token_id (`int`, *optional*, defaults to 2):
            The id of the token to force as the last generated token when `max_length` is reached. Usually set to
            `eos_token_id`.

    Example:

    ```python
    >>> from transformers import BartConfig, BartModel

    >>> # Initializing a BART facebook/bart-large style configuration
    >>> configuration = BartConfig()

    >>> # Initializing a model (with random weights) from the facebook/bart-large style configuration
    >>> model = BartModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```bartpast_key_valuesencoder_attention_headsd_model)num_attention_headshidden_sizec                    || _         || _        || _        || _        || _        || _        || _        || _        || _        || _	        || _
        || _        || _        || _        |	| _        |
| _        || _        || _        || _        || _        t)        | T  d|||||||d| | j,                  H|j/                  dd      r5| j0                  | _        t3        j4                  d| j0                   d       y y y )N)
num_labelspad_token_idbos_token_ideos_token_idis_encoder_decoderdecoder_start_token_idforced_eos_token_idforce_bos_token_to_be_generatedFz:Please make sure the config includes `forced_bos_token_id=zT` in future versions. The config can simply be saved and uploaded again to be fixed. )
vocab_sizemax_position_embeddingsr   encoder_ffn_dimencoder_layersr   decoder_ffn_dimdecoder_layersdecoder_attention_headsdropoutattention_dropoutactivation_dropoutactivation_functioninit_stdencoder_layerdropdecoder_layerdropclassifier_dropout	use_cachenum_hidden_layersscale_embeddingsuper__init__forced_bos_token_idgetr   warningswarn)selfr#   r$   r&   r%   r   r(   r'   r)   r/   r0   r-   r   r*   r+   r,   r.   r1   r4   r2   r   r   r   r   r   r   r    kwargs	__class__s                               ^/var/www/html/venv/lib/python3.12/site-packages/transformers/models/bart/configuration_bart.pyr6   zBartConfig.__init__o   s,   < %'>$.,'>$.,'>$!2"4#6  !2!2"4"!/. 		
!%%%1#9 3		
 		
 ##+

;\^c0d'+'8'8D$MMLTM^M^L_ `Q Q 1e+    )iY              rA   rB   rC           rD   gelur@   g?rD   rD   g{Gz?rD   FTr      r      TrG   rG   )	__name__
__module____qualname____doc__
model_typekeys_to_ignore_at_inferenceattribute_mapr6   __classcell__r=   s   @r>   r   r      s    IV J#4"5,EV_`M  $ " "" 7D Dr?   r   c                   ^    e Zd Zedeeeeef   f   fd       Zedeeeeef   f   f fd       Z	 	 	 	 dde	dedede
dee   deeef   fd	Z	 	 	 	 dde	dedede
dee   deeef   fd
Z	 	 	 	 dde	dedede
dee   deeef   fdZ	 	 	 	 dde	dedede
dee   deeef   fdZ fdZ xZS )BartOnnxConfigreturnc           	         | j                   dv rdt        ddddfddddfg      }| j                  rddi|d<   dd	d|d
<   nddd|d<   ddd|d
<   | j                  r| j                  |d       |S | j                   dk(  r\t        ddddfddddfg      }| j                  r7| j                  \  }}t        |      D ]  }ddd|d| d<   ddd|d| d<    |S t        ddddfddddfddddfd
dddfg      }|S )Ndefaultz
seq2seq-lm	input_idsbatchencoder_sequence)r   rF   attention_maskr   decoder_input_idsz past_decoder_sequence + sequencedecoder_attention_maskdecoder_sequenceinputs)	direction	causal-lmpast_sequence + sequencer   rG   zpast_key_values..key.value)taskr   use_pastfill_with_past_key_values_
num_layersrange)r;   common_inputsnum_encoder_layers_is        r>   r^   zBartOnnxConfig.inputs   s   9911' g2D"EF%77I'JKM }}67\12>EJl:m679@EW5X12>EJ\:]67}}///R0 / YY+%' g2D"EF%77I'JKM }}(,%"A12 nADKPj@kM$4QCt"<=FMRlBmM$4QCv">?n  ( g2D"EF%77I'JK(g:L*MN-7?Q/RS	M r?   c                     | j                   dv rt        |   }|S t        t        | 
  }| j                  r7| j
                  \  }}t        |      D ]  }ddd|d| d<   ddd|d| d<    |S )NrU   rX   ra   rb   zpresent.rc   rd   )re   r5   outputsr   rf   rh   ri   )r;   common_outputsrk   rl   rm   r=   s        r>   ro   zBartOnnxConfig.outputs   s    9911"W_N  ##5tDN}}(,%"A12 gA=DIc9dNXaS#56?FKe;fNXaS#78g r?   	tokenizer
batch_size
seq_lengthis_pair	frameworkc           	      F   | j                  |||||      }| j                  s|nd}| j                  |||||      }|j                         D 	
ci c]  \  }	}
d|	 |
 }}	}
t        di ||}| j                  rt	               st        d      dd l}|d   j                  \  }}|d   j                  d   }| j                  \  }}|||| j                  j                  |z  f}|dz   }|||| j                  j                  |z  f}|j                  |d   |j                  ||      gd	      |d<   g |d
<   | j                  \  }}t        ||      }t        ||      |z
  }||kD  rdnd}t!        |      D ]V  }|d
   j#                  |j%                  |      |j%                  |      |j%                  |      |j%                  |      f       X |dk(  r|n|}t!        ||      D ]6  }|d
   j#                  |j%                  |      |j%                  |      f       8 |S c c}
}	w )NrF   decoder_ACannot generate dummy past_keys inputs without PyTorch installed.r   rW   r[   r   r\   dimr   encoderdecoderr"   )I_generate_dummy_inputs_for_sequence_classification_and_question_answeringrf   itemsdictr   
ValueErrortorchshaper   _configr   catonesrh   minmaxri   appendzeros)r;   rq   rr   rs   rt   ru   encoder_inputsdecoder_seq_lengthdecoder_inputsnametensorrj   r   rX   encoder_seq_lengthnum_encoder_attention_headsnum_decoder_attention_headsencoder_shapedecoder_past_lengthdecoder_shaperk   num_decoder_layersmin_num_layersmax_num_layersremaining_side_namerl   r   s                              r>   1_generate_dummy_inputs_for_default_and_seq2seq_lmz@BartOnnxConfig._generate_dummy_inputs_for_default_and_seq2seq_lm   s    ggz:w	

 04}}Z!ggz#5w	
 IWH\H\H^_fHTF+V3__@~@@==%' !dee(5k(B(H(H%E%!./B!C!I!I!!LGKG_G_D')D+"((,GG	M #5q"8+#((,GG	M 7<ii78%**UL_:`agh 7@ 7M23 02M+,59__2 2 !35GHN !35GH>YN/ADV/V)\e>* /077M2M2M2M2	 &9I%EM=E>>: b/077U9KU[[Y^M_8`abc `s   Hc                    | j                  |||||      }| j                  rt               st        d      dd l}|d   j
                  \  }}	|	dz   }
| j                  \  }}| j                  \  }}|||
| j                  j                  |z  f}|d   j                  }|j                  |d   |j                  ||
|      gd      |d<   t        |      D cg c]$  }|j                  |      |j                  |      f& c}|d	<   |S c c}w )
Nrx   r   rW   rG   rZ   )dtyperF   ry   r   )r}   rf   r   r   r   r   rh   r   r   r   r   r   r   ri   r   )r;   rq   rr   rs   rt   ru   rj   r   rX   seqlenpast_key_values_lengthrk   rl   r   
past_shape
mask_dtypes                   r>   $_generate_dummy_inputs_for_causal_lmz3BartOnnxConfig._generate_dummy_inputs_for_causal_lm4  s5    ffz:w	
 ==%' !dee)+6<<ME6%+aZ"$(OO!-1-E-E*'+&((,GG	J ''78>>J.3ii/0%**UDZbl*2mntu /8 /M*+ MRRdLe0GHZ(%++j*AB0M+, 0s   )Dc                    t        |t        j                  d      }|j                  |      }t        |t        j                  |      }dj                  |j                  g      |z  g|z  }t         |||            }|S )Nr   )fixed_dimensionnum_token_to_add )return_tensors)r   r
   default_fixed_batchnum_special_tokens_to_adddefault_fixed_sequencejoin	unk_tokenr   )	r;   rq   rr   rs   rt   ru   token_to_adddummy_inputrj   s	            r>   r}   zXBartOnnxConfig._generate_dummy_inputs_for_sequence_classification_and_question_answeringZ  s     6
(F(FYZ


 !::7C5
(I(I\h


 xx!4!4 56CDzQY{9MNr?   c                     | j                   dv r| j                  |||||      }|S | j                   dk(  r| j                  |||||      }|S | j                  |||||      }|S )NrU   )rr   rs   rt   ru   r`   )re   r   r   r}   )r;   rq   rr   rs   rt   ru   rj   s          r>   generate_dummy_inputsz$BartOnnxConfig.generate_dummy_inputst  s     9911 RRjZQXdm S M  YY+% EEjZQXdm F M 	 !jjjZQXdm k M r?   c                 t    | j                   dv rt        | 	  ||||      }y t        t        |   ||||      }y )NrU   )re   r5   _flatten_past_key_values_r   )r;   flattened_outputr   idxtr=   s        r>   r   z(BartOnnxConfig._flatten_past_key_values_  sF    9911$w@AQSWY\^_`$%>_ $Q r?   )r   FN)rH   rI   rJ   propertyr   strintr^   ro   r   boolr   r   r   r   r   r}   r   r   rO   rP   s   @r>   rR   rR      s   )WS#X%6 67 ) )V 
gc3h&7!78 
 
 *.B&B B 	B
 B J'B 
c	BN *.$&$ $ 	$
 $ J'$ 
c	$R *.&  	
  J' 
c	: *.&  	
  J' 
c	0 r?   rR   )rK   r9   collectionsr   typingr   r   r    r   configuration_utilsr	   onnxr
   r   r   
onnx.utilsr   utilsr   r   r   
get_loggerrH   loggerr   rR   r"   r?   r>   <module>r      s^      # ) ) # 3 M M : < < 
		H	%T! Tn\. \r?   