o
    hI                     @   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.   l/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/onnx/config.pyr"   /   s   
 r"   c                   @   s`  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 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   last_hidden_state)r3   
pred_boxes
pred_masksr   )r3   r8   )start_logits
end_logits
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-lmrA   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keysrD   _patching_specsr&   dataclassesreplacegetattrr#   r$   append)selfrC   rD   rE   spec
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
        )rD   r.   clsrC   rD   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rO   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
        )rG   rD   copydeepcopyrO   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)hasattrrF   rX   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_batchrX   r.   r.   r/   default_batch_size   rZ   zOnnxConfig.default_batch_sizec                 C   rc   )zu
        The default sequence length to use if no other indication

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

        Returns:
            Integer > 0
        )r0   default_fixed_num_choicesrX   r.   r.   r/   default_num_choices   rh   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_OPSETrX   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.   rX   r.   r.   r/   atol_for_validation   rm   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   transformers.utilsro   r   parsetorch_onnx_minimum_version)rO   ro   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)rt   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   uint8RGB)	rangenprandomrandrN   r    	fromarrayastypeconvert)rO   ry   rz   r{   r|   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)endpointg      ?r   )r   r   linspaceintrN   sinpi)rO   ry   r   r   r   
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_dimensionnum_token_to_add0 rB   )	text_pairc                    s   g | ]
}||   qS r.   r.   ).0ir   vr.   r/   
<listcomp>r      z4OnnxConfig.generate_dummy_inputs.<locals>.<listcomp>)tensor_type)return_tensorspixel_valuesz*The `preprocessor` is an image processor (zC) and expects `model_input_names[0]` to be "pixel_values", but got )r   )r   r   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   
isinstancerH   warningswarnFutureWarningloggerwarningr   r0   rd   num_special_tokens_to_addrf   	unk_tokenlenjoinrD   ri   itemsr   dictconvert_to_tensorsmodel_input_names	__class__r(   r   r   )rO   r   ry   r   r   r   r   rz   r|   r{   r   r   r   r   r   r   r   token_to_addinput_tokendummy_input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.   )rO   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)rJ   r'   r%   setattrr#   r$   )rO   rP   r%   r.   r.   r/   	patch_ops     
zOnnxConfig.patch_opsc                 C   r   r   )rJ   r'   r&   r   r#   r$   )rO   rP   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)rU   r$   r   r   r.   r   r/   "flatten_output_collection_property  s   z-OnnxConfig.flatten_output_collection_property)rA   NrA   )r   r?   rx   rx   )r   r   r   r   )r   r   r   FNr?   rx   rx   r   r   r   N)0r(   r)   r*   r+   rd   rf   ri   r   rq   rr   r   rG   r-   r
   r"   rR   classmethodrV   propertyr   r   r   rY   r_   r   r   rb   re   rg   rj   rl   floatrn   boolrs   staticmethodrw   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 ).OnnxConfigWithPastrA   NFrC   r   rD   rE   use_pastc                    s   t  j|||d || _d S )N)rD   rE   )superrR   r   )rO   rC   rD   rE   r   r   r.   r/   rR     s   
zOnnxConfigWithPast.__init__rS   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)rD   r   r.   rT   r.   r.   r/   	with_past  s   zOnnxConfigWithPast.with_pastc                    s    t  j}| jr| j|dd |S )Nr_   	direction)r   r_   r   fill_with_past_key_values_r]   r   r.   r/   r_     s   zOnnxConfigWithPast.outputsc                 C   s   t | jdrd| jiS d S )Nr`   )ra   rF   r   rX   r.   r.   r/   rb     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)ra   rF   AttributeErrorr   rX   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)ra   rF   r   r   rX   r.   r.   r/   r     r   z&OnnxConfigWithPast.num_attention_headsr   r   r   ry   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ry   r   r   r   ACannot generate dummy past_keys inputs without PyTorch installed.r   	input_idsr   attention_mask)dtyper   )dimpast_key_values)r   r   r   r   rH   torchshaper   rF   hidden_sizer   catonesr   r   rN   zeros)rO   r   ry   r   r   r   common_inputsr   r4   seqlenpast_key_values_lengthr   
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.

        rY   r_   4direction must either be "inputs" or "outputs", but 
 was givenrY   r   presentr4   zpast_sequence + sequencer   r   r   .keyr6   .valueN)rH   r   r   )rO   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.   rO   flattened_outputr$   r   r   r.   r.   r/   _flatten_past_key_values_?  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   )rO   r$   r   r  r   r   r   r.   r/   r   C  s   z5OnnxConfigWithPast.flatten_output_collection_property)rA   NFr   r   r   FN)F)r(   r)   r*   r-   r   listr"   r   rR   r   r   r   r   r   r_   r   rb   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 )OnnxSeq2SeqConfigWithPastrS   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   r_   r   )r   r   r_   r   r   r   )rO   r^   r$   
axes_namessequence_nameaxis_idxr   r.   r/   r_   O  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   ra   rF   r  r  )rO   r   r   r.   r/   r   `     

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   ra   rF   r  r  )rO   r   r   r.   r/   r   p  r  z-OnnxSeq2SeqConfigWithPast.num_attention_headsr   FNr   r   ry   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 )decoder_r.   )r   r$   tensorr.   r.   r/   r     r   zCOnnxSeq2SeqConfigWithPast.generate_dummy_inputs.<locals>.<dictcomp>r   r   r   decoder_input_idsr?   r   r  decoderr.   )r   r   r   r   r   r   r   rH   r   r   r   rF   r   r   minmaxr   rN   r   )rO   r   ry   r   r   r   encoder_inputsdecoder_seq_lengthdecoder_inputsr   r   r4   encoder_seq_lengthnum_encoder_attention_headsnum_decoder_attention_headsencoder_shape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  rY   r   r  r  r  past_encoder_sequencepast_decoder_sequencez past_decoder_sequence + sequencer4   r  r   .decoder.key.decoder.value.encoder.key.encoder.valuer  )rH   r   r  r  r   )rO   r   r   r$   r'  r(  r)  r*  r+  r  r@   r   	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   r0  r?   r1  r.   r  r.   r.   r/   r	    s   z3OnnxSeq2SeqConfigWithPast._flatten_past_key_values_r
  )r(   r)   r*   r   r   r-   r   r_   r   r   r   r   r   r   r   r   r   r	  r  r.   r.   r   r/   r  N  s4    $
"Er  )3r[   rK   r   abcr   r   collectionsr   typingr   r   r   r   r	   r
   r   r   r   r   numpyr   	packagingr   utilsr   r   r   r   r   r   r   configuration_utilsr   r   r   r   r   r   r   PILr    
get_loggerr(   r   rk   rv   	dataclassr"   r0   r   r  r.   r.   r.   r/   <module>   s8   0
  y 