o
    ZhI                     @   s   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eZG dd deZG dd deZddgZdS )zBART model configuration    N)OrderedDict)AnyMappingOptional   )PreTrainedTokenizer)PretrainedConfig)
OnnxConfigOnnxConfigWithPastOnnxSeq2SeqConfigWithPast) compute_effective_axis_dimension)
TensorTypeis_torch_availableloggingc                       sj   e Zd ZdZdZdgZdddZ					
				
																			d fdd	Z  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
    ```Zbartpast_key_valuesencoder_attention_headsd_model)num_attention_headshidden_sizeY                      gelu皙?{Gz?FTr      r      c              
      s   || _ || _|| _|| _|| _|| _|| _|| _|| _|| _	|| _
|| _|| _|| _|	| _|
| _|| _|| _|| _|| _t jd|||||||d| | jd u rh|ddrj| j| _td| j d d S d S d S )N)
num_labelspad_token_idbos_token_ideos_token_idis_encoder_decoderdecoder_start_token_idforced_eos_token_idZ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_cacheZnum_hidden_layersscale_embeddingsuper__init__Zforced_bos_token_idgetr#   warningswarn)selfr)   r*   r,   r+   r   r.   r-   r/   r5   r6   r3   r   r0   r1   r2   r4   r7   r9   r8   r!   r"   r#   r$   r%   r&   r'   kwargs	__class__r(   Z/var/www/auris/lib/python3.10/site-packages/transformers/models/bart/configuration_bart.pyr;   o   sJ   zBartConfig.__init__)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   FTr   r   r   r    Tr    r    )	__name__
__module____qualname____doc__Z
model_typeZkeys_to_ignore_at_inferenceZattribute_mapr;   __classcell__r(   r(   rA   rC   r      s@    K
r   c                       sD  e Zd Zedeeeeef f fddZedeeeeef f f fddZ				dd	e	d
edede
dee deeef fddZ				dd	e	d
edede
dee deeef fddZ				dd	e	d
edede
dee deeef fddZ				dd	e	d
edede
dee deeef fddZ fddZ  ZS )BartOnnxConfigreturnc                 C   s4  | j dv r@t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d |S | j dkr|tddddfddddfg}| jrz| j\}}t|D ]}ddd|d| d< ddd|d| d< qa|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batchZencoder_sequence)r   r   attention_maskr   decoder_input_idsz past_decoder_sequence + sequencedecoder_attention_maskZdecoder_sequenceinputs)	direction	causal-lmpast_sequence + sequencer   r    zpast_key_values..key.value)taskr   use_pastZfill_with_past_key_values_
num_layersrange)r?   common_inputsnum_encoder_layers_ir(   r(   rC   rR      sD   


	zBartOnnxConfig.inputsc                    sp   | j dv rt j}|S tt| j}| jr6| j\}}t|D ]}ddd|d| d< ddd|d| d< q|S )NrK   rN   rU   rV   zpresent.rW   rX   )rY   r:   outputsr
   rZ   r[   r\   )r?   Zcommon_outputsr^   r_   r`   rA   r(   rC   ra      s   

zBartOnnxConfig.outputsFN	tokenizer
batch_size
seq_lengthis_pair	frameworkc              	   C   s  |  |||||}| js|nd}|  |||||}dd | D }tdi ||}	| jrt s5tddd l}
|	d j\}}|	d jd }| j\}}|||| j	j
| f}|d }|||| j	j
| f}|
j|	d	 |
||gdd
|	d	< g |	d< | j\}}t||}t||| }||krdnd}t|D ]}|	d |
||
||
||
|f q|dkr|n|}t||D ]}|	d |
||
|f q|	S )Nr   c                 S   s   i | ]
\}}d | |qS )Zdecoder_r(   ).0nameZtensorr(   r(   rC   
<dictcomp>  s    zTBartOnnxConfig._generate_dummy_inputs_for_default_and_seq2seq_lm.<locals>.<dictcomp>ACannot generate dummy past_keys inputs without PyTorch installed.r   rM   rP   r   rQ   dimr   encoderdecoderr(   )I_generate_dummy_inputs_for_sequence_classification_and_question_answeringrZ   itemsdictr   
ValueErrortorchshaper   _configr   catonesr[   minmaxr\   appendzeros)r?   rc   rd   re   rf   rg   Zencoder_inputsZdecoder_seq_lengthZdecoder_inputsr]   rt   rN   Zencoder_seq_lengthnum_encoder_attention_headsZnum_decoder_attention_headsZencoder_shapeZdecoder_past_lengthZdecoder_shaper^   Znum_decoder_layersZmin_num_layersZmax_num_layersZremaining_side_namer_   ru   r(   r(   rC   1_generate_dummy_inputs_for_default_and_seq2seq_lm   s^   







	 z@BartOnnxConfig._generate_dummy_inputs_for_default_and_seq2seq_lmc                    s   |  |||||}| jr\t stddd l|d j\}}|d }	| j\}
}| j\}}|||	| jj	| f |d j
}j|d j||	|dgdd|d<  fd	d
t|
D |d< |S )Nrk   r   rM   r    rO   )dtyper   rl   c                    s    g | ]}    fqS r(   )r|   )rh   r_   Z
past_shapert   r(   rC   
<listcomp>U  s    zGBartOnnxConfig._generate_dummy_inputs_for_causal_lm.<locals>.<listcomp>r   )rp   rZ   r   rs   rt   ru   r[   r   rv   r   r   rw   rx   r\   )r?   rc   rd   re   rf   rg   r]   rN   ZseqlenZpast_key_values_lengthr^   r_   r}   Z
mask_dtyper(   r   rC   $_generate_dummy_inputs_for_causal_lm4  s0   






z3BartOnnxConfig._generate_dummy_inputs_for_causal_lmc           	      C   sV   t |tjdd}||}t |tj|d}d|jg| g| }t|||d}|S )Nr   )Zfixed_dimensionZnum_token_to_add )Zreturn_tensors)r   r	   Zdefault_fixed_batchZnum_special_tokens_to_addZdefault_fixed_sequencejoinZ	unk_tokenrr   )	r?   rc   rd   re   rf   rg   Ztoken_to_addZdummy_inputr]   r(   r(   rC   rp   Z  s   
zXBartOnnxConfig._generate_dummy_inputs_for_sequence_classification_and_question_answeringc                 C   s\   | j dv r| j|||||d}|S | j dkr"| j|||||d}|S | j|||||d}|S )NrK   )rd   re   rf   rg   rT   )rY   r~   r   rp   )r?   rc   rd   re   rf   rg   r]   r(   r(   rC   generate_dummy_inputst  s   




z$BartOnnxConfig.generate_dummy_inputsc                    s:   | j dv rt ||||}d S tt| ||||}d S )NrK   )rY   r:   _flatten_past_key_values_r   )r?   Zflattened_outputri   idxtrA   r(   rC   r     s
   

z(BartOnnxConfig._flatten_past_key_values_)rb   rb   FN)rD   rE   rF   propertyr   strintrR   ra   r   boolr   r   r   r~   r   rp   r   r   rH   r(   r(   rA   rC   rI      s     +$

G

)



rI   )rG   r=   collectionsr   typingr   r   r    r   Zconfiguration_utilsr   Zonnxr	   r
   r   Z
onnx.utilsr   utilsr   r   r   Z
get_loggerrD   loggerr   rI   __all__r(   r(   r(   rC   <module>   s   
  `