o
    Zha                     @   sF  d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZmZmZmZmZ d dlZd dlmZ ddlmZmZmZmZ dd	lmZmZmZ e	rldd
lm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& e rud dl'm(Z( e)e*Z+dZ,dZ-ej.G dd dZ/G dd deZ0G dd de0eZ1G dd de1Z2dS )    N)ABCabstractmethod)OrderedDict)
TYPE_CHECKINGAnyCallableDictIterableListMappingOptionalTupleUnion)version   )
TensorTypeis_torch_availableis_vision_availablelogging   )ParameterFormat compute_effective_axis_dimension"compute_serialized_parameters_size)PretrainedConfigFeatureExtractionMixinImageProcessingMixinPreTrainedTokenizerBase)Image   l        c                   @   sJ   e Zd ZU dZeed< eed< eed< dZe	e ed< dZ
e	e ed< dS )PatchingSpeca  
    Data class that holds patching specifications.

    Args:
        o: Module / object where the op to patch is located
        name: Name of the op to monkey patch
        custom_op: Custom op that patches the original op
        orig_op: Original op that is being patched
        op_wrapper: Wrapper (optional) that wraps both the original and custom ops.
            It is useful for ops that are class or static methods for instance.
    oname	custom_opNorig_op
op_wrapper)__name__
__module____qualname____doc__r   __annotations__strr   r&   r   r'    r.   r.   G/var/www/auris/lib/python3.10/site-packages/transformers/onnx/config.pyr"   /   s   
 r"   c                   @   sf  e Zd ZdZdZdZdZedZ	e
dddd	ie
d
ddd	ie
dddd	ie
ddd	ddd	ddd	de
dddd	ie
dddd	ie
dddiie
ddd	ddd	de
ddd	ddd	de
ddddddie
dddd	ie
dddiie
dddd	ie
dddd	ie
dddd	idZ	d\dddedeee  fddZed]dddedd fddZeedeeeeef f fd d!Zedeeeeef f fd"d#Zedeeeef  fd$d%Zedefd&d'Zedefd(d)Zedefd*d+Zedefd,d-Zedefd.d/Z ede!fd0d1Z"e#d2ede!fd3d4Z$	6d^d7ed8ed9ed:efd;d<Z%	?d_d7ed@edAedBefdCdDZ&	E	E	E	F		5	6	6	=	>	?	d`dGe'dH d7edIedJedKe!dLee( d8ed:ed9ed@edAedBedMedN deeef fdOdPZ)dQeeef deeef fdRdSZ*dTdU Z+dVdW Z,edXedYe-e de.eef fdZd[Z/dS )a
OnnxConfigzv
    Base class for ONNX exportable model describing metadata on how to export the model through the ONNX format.
    r         z1.8logitsbatchsequencer   r   Zlast_hidden_state)r3   
pred_boxesZ
pred_masksr   )r3   r7   )Zstart_logitsZ
end_logitsZ
num_labelsheightwidth)r   r   r      decoder_sequence)z	causal-lmdefaultzimage-classificationzimage-segmentationz	masked-imz	masked-lmmultiple-choicezobject-detectionzquestion-answeringzsemantic-segmentationz
seq2seq-lmzsequence-classificationztoken-classificationzvision2seq-lmzspeech2seq-lmr<   Nconfigr   taskpatching_specsc                 C   s   || _ || jvrt| d| j  || _g | _|d ur |ng D ]}|}|jd u r7tj|t	|j
|jd}| j| q"d S )Nz+ is not a supported task, supported tasks: )r&   )_config_tasks_to_common_outputs
ValueErrorkeysr?   _patching_specsr&   dataclassesreplacegetattrr#   r$   append)selfr>   r?   r@   specZ
final_specr.   r.   r/   __init__o   s   

zOnnxConfig.__init__returnc                 C   s   | ||dS )z
        Instantiate a OnnxConfig for a specific model

        Args:
            config: The model's configuration to use when exporting to ONNX

        Returns:
            OnnxConfig for this model
        )r?   r.   clsr>   r?   r.   r.   r/   from_model_config   s   zOnnxConfig.from_model_configc                 C   s   t  )z
        Mapping containing the axis definition of the input tensors to provide to the model

        Returns:
            For each input: its name associated to the axes symbolic name and the axis position within the tensor
        )NotImplementedErrorrJ   r.   r.   r/   inputs      	zOnnxConfig.inputsc                 C   s   | j | j }t|S )z
        Mapping containing the axis definition of the output tensors to provide to the model

        Returns:
            For each output: its name associated to the axes symbolic name and the axis position within the tensor
        )rB   r?   copydeepcopyrJ   common_outputsr.   r.   r/   outputs   s   
zOnnxConfig.outputsc                 C   s   t | jdr
ddiS dS )z
        Dictionary of keys to override in the model's config before exporting

        Returns:
            Dictionary with the keys (and their corresponding values) to override
        	use_cacheFN)hasattrrA   rR   r.   r.   r/   values_override   s   zOnnxConfig.values_overridec                 C      t jS )zp
        The default batch size to use if no other indication

        Returns:
            Integer > 0
        )r0   default_fixed_batchrR   r.   r.   r/   default_batch_size   rT   zOnnxConfig.default_batch_sizec                 C   r]   )zu
        The default sequence length to use if no other indication

        Returns:
            Integer > 0
        )r0   default_fixed_sequencerR   r.   r.   r/   default_sequence_length      z"OnnxConfig.default_sequence_lengthc                 C   r]   )zw
        The default number of choices to use if no other indication

        Returns:
            Integer > 0
        )r0   default_fixed_num_choicesrR   r.   r.   r/   default_num_choices   rb   zOnnxConfig.default_num_choicesc                 C   s   t S )z{
        Which onnx opset to use when exporting the model

        Returns:
            Integer ONNX Opset version
        )DEFAULT_ONNX_OPSETrR   r.   r.   r/   default_onnx_opset      zOnnxConfig.default_onnx_opsetc                 C   s   dS )z
        What absolute tolerance value to use during model conversion validation.

        Returns:
            Float absolute tolerance value.
        gh㈵>r.   rR   r.   r.   r/   atol_for_validation   rg   zOnnxConfig.atol_for_validationc                 C   s(   t  rddlm} t| | jkS dS )z
        The minimum PyTorch version required to export the model.

        Returns:
            `bool`: Whether the installed version of PyTorch is compatible with the model.
        r   )get_torch_versionF)r   Ztransformers.utilsri   r   parsetorch_onnx_minimum_version)rJ   ri   r.   r.   r/   is_torch_support_available   s   z%OnnxConfig.is_torch_support_availablenum_parametersc                 C   s   t | tjtkS )a  
        Flag indicating if the model requires using external data format

        Args:
            num_parameters: Number of parameter on the model

        Returns:
            True if model.num_parameters() * size_of(float32) >= 2Gb False otherwise
        )r   r   FloatEXTERNAL_DATA_FORMAT_SIZE_LIMIT)rm   r.   r.   r/   use_external_data_format   s   
z#OnnxConfig.use_external_data_formatr:   (   
batch_sizenum_channelsimage_heightimage_widthc                 C   sF   g }t |D ]}tj|||d }|t|dd q|S )N   Zuint8RGB)	rangenprandomZrandrI   r    Z	fromarrayZastypeconvert)rJ   rr   rs   rt   ru   images_datar.   r.   r/   _generate_dummy_images  s
   z!OnnxConfig._generate_dummy_images"V        @   sampling_ratetime_duration	frequencyc                 C   sR   g }t |D ] }tjd|t|| dd}|dtdtj | |   q|S )Nr   F)Zendpointg      ?r   )rx   ry   ZlinspaceintrI   sinpi)rJ   rr   r   r   r   Z
audio_datar}   tr.   r.   r/   _generate_dummy_audio  s
   $z OnnxConfig._generate_dummy_audioFpreprocessor)r   r   r   
seq_lengthnum_choicesis_pair	framework	tokenizerr   c                    sL  ddl m} ddlm} ddlm} t||r|durtd|dur0t	dt
 td |}t||rt|tjd	d
}||}t|tj|d
}|jdurYt|jd	krY|jnd}d|g| g| }| jdkrt tjd	d
 |  }|||d}| D ]\} fddtd	t D ||< qt|j|dS t|||dS t||r|jd	 dkrtd|jj d|jd	  t|tjd}| |||	|}t|||dS t||r|jd	 dkrt|tjd}| |||	|}t|||dS t||r"|jd	 dkr"t|tjd}| ||
||}t|||dS td)am  
        Generate inputs to provide to the ONNX exporter for the specific framework

        Args:
            preprocessor: ([`PreTrainedTokenizerBase`], [`FeatureExtractionMixin`], or [`ImageProcessingMixin`]):
                The preprocessor associated with this model configuration.
            batch_size (`int`, *optional*, defaults to -1):
                The batch size to export the model for (-1 means dynamic axis).
            num_choices (`int`, *optional*, defaults to -1):
                The number of candidate answers provided for multiple choice task (-1 means dynamic axis).
            seq_length (`int`, *optional*, defaults to -1):
                The sequence length to export the model for (-1 means dynamic axis).
            is_pair (`bool`, *optional*, defaults to `False`):
                Indicate if the input is a pair (sentence 1, sentence 2)
            framework (`TensorType`, *optional*, defaults to `None`):
                The framework (PyTorch or TensorFlow) that the tokenizer will generate tensors for.
            num_channels (`int`, *optional*, defaults to 3):
                The number of channels of the generated images.
            image_width (`int`, *optional*, defaults to 40):
                The width of the generated images.
            image_height (`int`, *optional*, defaults to 40):
                The height of the generated images.
            sampling_rate (`int`, *optional* defaults to 22050)
                The sampling rate for audio data generation.
            time_duration (`float`, *optional* defaults to 5.0)
                Total seconds of sampling for audio data generation.
            frequency (`int`, *optional* defaults to 220)
                The desired natural frequency of generated audio.

        Returns:
            Mapping[str, Tensor] holding the kwargs to provide to the model's forward function
        r   r   r   r   NzPYou cannot provide both a tokenizer and a preprocessor to generate dummy inputs.ztThe `tokenizer` argument is deprecated and will be removed in version 5 of Transformers. Use `preprocessor` instead.zROverwriting the `preprocessor` argument with `tokenizer` to generate dummy inputs.r   )fixed_dimensionZnum_token_to_add0 r=   )Z	text_pairc                    s   g | ]
}||   qS r.   r.   ).0ir   vr.   r/   
<listcomp>t      z4OnnxConfig.generate_dummy_inputs.<locals>.<listcomp>)Ztensor_type)return_tensorsZpixel_valuesz*The `preprocessor` is an image processor (zC) and expects `model_input_names[0]` to be "pixel_values", but got )r   )r|   r   Zinput_featuresz\Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.) feature_extraction_utilsr   image_processing_utilsr   tokenization_utils_baser   
isinstancerC   warningswarnFutureWarningloggerwarningr   r0   r^   Znum_special_tokens_to_addr`   Z	unk_tokenlenjoinr?   rc   itemsrx   dictZconvert_to_tensorsZmodel_input_names	__class__r(   r   r   )rJ   r   rr   r   r   r   r   rs   ru   rt   r   r   r   r   r   r   r   Ztoken_to_addZinput_tokenZdummy_inputZtokenized_inputkr.   r   r/   generate_dummy_inputs  st   0



&
z OnnxConfig.generate_dummy_inputsreference_model_inputsc                 C   s   |S )a  
        Generate inputs for ONNX Runtime using the reference model inputs. Override this to run inference with seq2seq
        models which have the encoder and decoder exported as separate ONNX files.

        Args:
            reference_model_inputs ([`Mapping[str, Tensor]`):
                Reference inputs for the model.

        Returns:
            `Mapping[str, Tensor]`: The mapping holding the kwargs to provide to the model's forward function
        r.   )rJ   r   r.   r.   r/   !generate_dummy_inputs_onnxruntime  s   z,OnnxConfig.generate_dummy_inputs_onnxruntimec                 C   <   | j D ]}|jd u r|jn||j}t|j|j| qd S N)rE   r'   r%   setattrr#   r$   )rJ   rK   r%   r.   r.   r/   	patch_ops     
zOnnxConfig.patch_opsc                 C   r   r   )rE   r'   r&   r   r#   r$   )rJ   rK   r&   r.   r.   r/   restore_ops  r   zOnnxConfig.restore_opsr$   fieldc                    s(   ddl m}  fddt||D S )a  
        Flatten any potential nested structure expanding the name of the field with the index of the element within the
        structure.

        Args:
            name: The name of the nested structure
            field: The structure to, potentially, be flattened

        Returns:
            (Dict[str, Any]): Outputs with flattened structure and key mapping this new structure.

        r   )chainc                    s    i | ]\}}  d | |qS ).r.   )r   idxitemr$   r.   r/   
<dictcomp>  s     zAOnnxConfig.flatten_output_collection_property.<locals>.<dictcomp>)	itertoolsr   	enumeratefrom_iterable)rO   r$   r   r   r.   r   r/   "flatten_output_collection_property  s   z-OnnxConfig.flatten_output_collection_property)r<   Nr<   )r   r:   rq   rq   )r   r   r   r   )r   r   r   FNr:   rq   rq   r   r   r   N)0r(   r)   r*   r+   r^   r`   rc   r   rj   rk   r   rB   r-   r   r
   r"   rL   classmethodrP   propertyr   r   r   rS   rY   r   r\   r_   ra   rd   rf   floatrh   boolrl   staticmethodrp   r   r   r   r   r   r   r   r   r	   r   r   r.   r.   r.   r/   r0   D   s   
#

"	 

				


	


"x(r0   c                       sV  e Zd Z			d*dddedeee  def fd	d
Ze	d+dddedd fddZ
edeeeeef f f fddZedeeeef  fddZedefddZedefddZ				d,dddedededee deeef f fddZ	d-deeeeef f d ed!efd"d#Zd$d% Zd&ed'ee deeef f fd(d)Z  ZS ).OnnxConfigWithPastr<   NFr>   r   r?   r@   use_pastc                    s   t  j|||d || _d S )N)r?   r@   )superrL   r   )rJ   r>   r?   r@   r   r   r.   r/   rL     s   
zOnnxConfigWithPast.__init__rM   c                 C   s   | ||ddS )z
        Instantiate a OnnxConfig with `use_past` attribute set to True

        Args:
            config: The underlying model's config to use when exporting to ONNX

        Returns:
            OnnxConfig with `.use_past = True`
        T)r?   r   r.   rN   r.   r.   r/   	with_past  s   zOnnxConfigWithPast.with_pastc                    s    t  j}| jr| j|dd |S )NrY   	direction)r   rY   r   fill_with_past_key_values_rW   r   r.   r/   rY     s   zOnnxConfigWithPast.outputsc                 C   s   t | jdrd| jiS d S )NrZ   )r[   rA   r   rR   r.   r.   r/   r\     s   
z"OnnxConfigWithPast.values_overridec                 C      t | jds
td| jjS )z
        The number of layers attribute retrieved from the model config. Override this for model configs where the
        number of layers attribute is not called `num_layers`.
        
num_layerszcould not find the number of layers attribute in the model configuration, override the num_layers property of the model OnnxConfig to solve this)r[   rA   AttributeErrorr   rR   r.   r.   r/   r     
   zOnnxConfigWithPast.num_layersc                 C   r   )z
        The number of attention heads attribute retrieved from the model config. Override this for model configs where
        the number of attention heads attribute is not called `num_attention_heads`.
        num_attention_headszcould not find the number of attention heads attribute in the model configuration, override the num_attention_heads property of the model OnnxConfig to solve this)r[   rA   r   r   rR   r.   r.   r/   r     r   z&OnnxConfigWithPast.num_attention_headsr   r   r   rr   r   r   r   c                    s   t  j|||||d}| jrgt stddd l}|d j\}}	|	d }
|| j|
| jj	| j f}d|v rL|d j
}|j|d |j||
|dgdd	|d< g |d
< t| jD ]}|d
 ||||f qU|S )Nrr   r   r   r   ACannot generate dummy past_keys inputs without PyTorch installed.r   	input_idsr   Zattention_mask)dtyper   )dimpast_key_values)r   r   r   r   rC   torchshaper   rA   hidden_sizer   catZonesrx   r   rI   zeros)rJ   r   rr   r   r   r   common_inputsr   r4   ZseqlenZpast_key_values_lengthr   Z
mask_dtyper}   r   r.   r/   r     s0   	


 z(OnnxConfigWithPast.generate_dummy_inputsinputs_or_outputsr   inverted_values_shapec                 C   s   |dvrt d| d|dkrdnd}t| jD ],}ddd	|| d
| d< |r8ddd|| d
| d< qddd	|| d
| d< qdS )a  
        Fill the input_or_outputs mapping with past_key_values dynamic axes considering.

        Args:
            inputs_or_outputs: The mapping to fill.
            direction: either "inputs" or "outputs", it specifies whether input_or_outputs is the input mapping or the
                output mapping, this is important for axes naming.
            inverted_values_shape:
                If `True`, store values on dynamic axis 1, else on axis 2.

        rS   rY   4direction must either be "inputs" or "outputs", but 
 was givenrS   r   presentr4   zpast_sequence + sequencer   r   r   .keyr6   .valueN)rC   rx   r   )rJ   r   r   r   r$   r   r.   r.   r/   r   (  s   z-OnnxConfigWithPast.fill_with_past_key_values_c                 C   s4   |d || d| d< |d || d| d< d S )Nr   r   r   r   r   r.   rJ   flattened_outputr$   r   r   r.   r.   r/   _flatten_past_key_values_A  s   z,OnnxConfigWithPast._flatten_past_key_values_r$   r   c                    sD   i }|dv rt |D ]\}}| |||| q
|S t ||}|S )N)r   r   )r   r   r   r   )rJ   r$   r   r   r   r   r   r.   r/   r   E  s   z5OnnxConfigWithPast.flatten_output_collection_property)r<   NFr   r   r   FN)F)r(   r)   r*   r-   r   listr"   r   rL   r   r   r   r   r   rY   r   r\   r   r   r   r   r   r   r	   r   r   __classcell__r.   r.   r   r/   r     sd    

$
+
.r   c                       s   e Zd Zedeeeeef f f fddZedee f fddZ	edee f fddZ
					
dded dedededee deeef f fddZdeeeeef f defddZdd Z  ZS )OnnxSeq2SeqConfigWithPastrM   c                    sr   t t| j}| D ]"\}}d|v rdnd}| D ]\}}d|v r'|||< q|||< qq
| jr7| j|dd |S )Nencoderencoder_sequencer;   r5   rY   r   )r   r   rY   r   r   r   )rJ   rX   r$   Z
axes_namesZsequence_nameZaxis_idxr   r.   r/   rY   Q  s   

z!OnnxSeq2SeqConfigWithPast.outputsc                    \   zt  j}||f}W |S  ty-   t| jdr)t| jdr)| jj| jjf}Y |S tdw )Nencoder_layersdecoder_layerszcould not find the number of encoder and decoder layers attributes in the model configuration, override the num_layers property of the model OnnxConfig to solve this)r   r   r   r[   rA   r   r   )rJ   r   r   r.   r/   r   b     

z$OnnxSeq2SeqConfigWithPast.num_layersc                    r   )Nencoder_attention_headsdecoder_attention_headszcould not find the number of attention heads for the encoder and the decoder attributes in the model configuration, override the num_attention_heads property of the model OnnxConfig to solve this)r   r   r   r[   rA   r   r   )rJ   r   r   r.   r/   r   r  r   z-OnnxSeq2SeqConfigWithPast.num_attention_headsr   FNr   r   rr   r   r   r   c              	      s  t t| j|||||d}| js|nd}t t| j|||||d}dd | D }tdi ||}	| jrt s=tddd l}
|	d j	d }|	d j	d }|	d j	d }| j
\}}|||| jj| f}|||d	 | jj| f}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   r   c                 S   s   i | ]
\}}d | |qS )Zdecoder_r.   )r   r$   Ztensorr.   r.   r/   r     r   zCOnnxSeq2SeqConfigWithPast.generate_dummy_inputs.<locals>.<dictcomp>r   r   r   Zdecoder_input_idsr:   r   r   decoderr.   )r   r   r   r   r   r   r   rC   r   r   r   rA   r   r   minmaxrx   rI   r   )rJ   r   rr   r   r   r   Zencoder_inputsZdecoder_seq_lengthZdecoder_inputsr   r   r4   Zencoder_seq_lengthZnum_encoder_attention_headsZnum_decoder_attention_headsZencoder_shapeZdecoder_shapenum_encoder_layersnum_decoder_layersmin_num_layersmax_num_layersremaining_side_namer}   r   r   r.   r/   r     sX   









 z/OnnxSeq2SeqConfigWithPast.generate_dummy_inputsr   r   c              	   C   s0  |dvrt d| d|dkrdnd}| j\}}t||}t||| }||kr+dnd}d	}	|dkr5d
nd}
t|D ]6}d|
d|| d| d< d|
d|| d| d< d|	d|| d| d< d|	d|| d| d< q;t||D ]}|dkrd|	d}nd|
d}||| d| d| d< qwd S )Nr   r   r   rS   r   r   r   r   Zpast_encoder_sequenceZpast_decoder_sequencez past_decoder_sequence + sequencer4   r   r   .decoder.key.decoder.value.encoder.key.encoder.valuer   )rC   r   r   r   rx   )rJ   r   r   r$   r   r   r   r   r   r   r;   r   Z	axes_infor.   r.   r/   r     s(   


z4OnnxSeq2SeqConfigWithPast.fill_with_past_key_values_c                 C   sd   |d || d| d< |d || d| d< |d || d| d< |d || d| d	< d S )
Nr   r   r   r   r   r   r   r:   r  r.   r   r.   r.   r/   r     s   z3OnnxSeq2SeqConfigWithPast._flatten_past_key_values_r   )r(   r)   r*   r   r   r-   r   rY   r   r   r   r   r   r   r   r   r   r   r   r.   r.   r   r/   r   P  s4    $
"Er   )3rU   rF   r   abcr   r   collectionsr   typingr   r   r   r   r	   r
   r   r   r   r   numpyry   	packagingr   utilsr   r   r   r   r   r   r   Zconfiguration_utilsr   r   r   r   r   r   r   ZPILr    Z
get_loggerr(   r   re   ro   	dataclassr"   r0   r   r   r.   r.   r.   r/   <module>   s8   0
  { 