Ë
    îÍ:js  ã                   ó”  — 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
 d dl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rddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$  e«       rd dl%m&Z&  ejN                  e(«      Z)dZ*dZ+ejX                   G d„ d«      «       Z- G d„ de«      Z. G d„ de.e«      Z/ G d„ de/«      Z0y)é    N)ÚABCÚabstractmethod)ÚOrderedDict)ÚIterableÚMapping)ÚTYPE_CHECKINGÚAnyÚCallableÚOptionalÚ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                   óX   — 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<   y)Ú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&   © ó    úm/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/transformers/onnx/config.pyr!   r!   0   s7   … ñ
ð ƒFØ
ƒIØÓØ"&€GˆX�hÑÓ&Ø%)€J�˜Ñ"Ô)r.   r!   c                   ó@  — e Zd ZdZdZdZdZ ej                  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	 dAdddedeee      fd„ZedBdddedd fd„«       Zeedeeeeef   f   fd„«       «       Zedeeeeef   f   fd„«       Zedeeeef      fd„«       Zedefd „«       Zedefd!„«       Zedefd"„«       Zedefd#„«       Zedefd$„«       Z ede!fd%„«       Z"e#d&ede!fd'„«       Z$	 dCd(ed)ed*ed+efd,„Z%	 dDd(ed-ed.ed/efd0„Z&	 	 	 	 	 	 	 	 	 	 	 	 dEd1e'd2   d(ed3ed4ed5e!d6ee(   d)ed+ed*ed-ed.ed/ed7ed8   deeef   fd9„Z)d:eeef   deeef   fd;„Z*d<„ Z+d=„ Z,ed>ed?e-e   de.eef   fd@„«       Z/y)FÚ
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)r4   Ú
pred_boxesÚ
pred_masksr   )r4   r9   )Ú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-lmNÚconfigr   ÚtaskÚpatching_specsc                 ól  — || _         || j                  vr(t        |› d| j                  j                  «       › �«      ‚|| _        g | _        |�|ng D ]`  }|}|j                  €5t        j                  |t        |j                  |j                  «      ¬«      }| j
                  j                  |«       Œb y )Nz+ is not a supported task, supported tasks: )r%   )Ú_configÚ_tasks_to_common_outputsÚ
ValueErrorÚkeysrE   Ú_patching_specsr%   ÚdataclassesÚreplaceÚgetattrr"   r#   Úappend)ÚselfrD   rE   rF   ÚspecÚ
final_specs         r/   Ú__init__zOnnxConfig.__init__p   s°   € ð ˆŒà�t×4Ñ4Ñ4ÜØ�&ÐCÀD×DaÑDa×DfÑDfÓDhÐCiÐjóð ð ˆŒ	à!ˆÔØ&4Ð&@‘NÀbò 	4ˆDØˆJØ�|‰|Ð#Ü(×0Ñ0°¼wÀtÇvÁvÈtÏyÉyÓ?YÔZ�
Ø× Ñ ×'Ñ'¨
Õ3ñ		4r.   Úreturnc                 ó   —  | ||¬«      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
        )rE   r-   ©ÚclsrD   rE   s      r/   Úfrom_model_configzOnnxConfig.from_model_config‚   s   € ñ �6 Ô%Ð%r.   c                 ó   — 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©rQ   s    r/   ÚinputszOnnxConfig.inputs�   s   € ô "Ó#Ð#r.   c                 ó^   — | j                   | j                     }t        j                  |«      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
        )rI   rE   ÚcopyÚdeepcopy)rQ   Úcommon_outputss     r/   ÚoutputszOnnxConfig.outputsš   s'   € ð ×6Ñ6°t·y±yÑAˆÜ�}‰}˜^Ó,Ð,r.   c                 ó8   — t        | j                  d«      rddiS y)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)ÚhasattrrH   r\   s    r/   Úvalues_overridezOnnxConfig.values_override¥   s    € ô �4—<‘< Ô-Ø Ð'Ð'àr.   c                 ó"   — t         j                  S )zp
        The default batch size to use if no other indication

        Returns:
            Integer > 0
        )r1   Údefault_fixed_batchr\   s    r/   Údefault_batch_sizezOnnxConfig.default_batch_size²   s   € ô ×-Ñ-Ð-r.   c                 ó"   — t         j                  S )zu
        The default sequence length to use if no other indication

        Returns:
            Integer > 0
        )r1   Údefault_fixed_sequencer\   s    r/   Údefault_sequence_lengthz"OnnxConfig.default_sequence_length½   s   € ô ×0Ñ0Ð0r.   c                 ó"   — t         j                  S )zw
        The default number of choices to use if no other indication

        Returns:
            Integer > 0
        )r1   Údefault_fixed_num_choicesr\   s    r/   Údefault_num_choiceszOnnxConfig.default_num_choicesÇ   s   € ô ×3Ñ3Ð3r.   c                 ó   — t         S )z{
        Which onnx opset to use when exporting the model

        Returns:
            Integer ONNX Opset version
        )ÚDEFAULT_ONNX_OPSETr\   s    r/   Údefault_onnx_opsetzOnnxConfig.default_onnx_opsetÑ   s
   € ô "Ð!r.   c                  ó   — y)z˜
        What absolute tolerance value to use during model conversion validation.

        Returns:
            Float absolute tolerance value.
        gñhãˆµøä>r-   r\   s    r/   Úatol_for_validationzOnnxConfig.atol_for_validationÛ   s   € ð r.   c                 ór   — t        «       r-ddlm} t        j                   |«       «      | j
                  k\  S y)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.utilsrv   r   ÚparseÚtorch_onnx_minimum_version)rQ   rv   s     r/   Úis_torch_support_availablez%OnnxConfig.is_torch_support_availableå   s.   € ô ÔÝ<ä—=‘=Ñ!2Ó!4Ó5¸×9XÑ9XÑXÐXàr.   Únum_parametersc                 óD   — 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)r{   s    r/   Úuse_external_data_formatz#OnnxConfig.use_external_data_formatô   s!   € ô /¨~¼×?TÑ?TÓUÜ.ñ/ð	
r.   Ú
batch_sizeÚnum_channelsÚimage_heightÚimage_widthc                 óö   — g }t        |«      D ]h  }t        j                  j                  |||«      dz  }|j	                  t        j                  |j                  d«      «      j                  d«      «       Œj |S )Néÿ   Úuint8ÚRGB)	ÚrangeÚnpÚrandomÚrandrP   r   Ú	fromarrayÚastypeÚconvert)rQ   r€   r�   r‚   rƒ   ÚimagesÚ_Údatas           r/   Ú_generate_dummy_imagesz!OnnxConfig._generate_dummy_images  sj   € ð ˆÜ�zÓ"ò 	PˆAÜ—9‘9—>‘> ,°¸\ÓJÈSÑPˆDØ�M‰Mœ%Ÿ/™/¨$¯+©+°gÓ*>Ó?×GÑGÈÓNÕOð	Pð ˆr.   Úsampling_rateÚtime_durationÚ	frequencyc           	      óð   — g }t        |«      D ]e  }t        j                  d|t        ||z  «      d¬«      }|j	                  dt        j
                  dt        j                  z  |z  |z  «      z  «       Œg |S )Nr   F)Úendpointg      à?r   )rˆ   r‰   ÚlinspaceÚintrP   ÚsinÚpi)rQ   r€   r“   r”   r•   Ú
audio_datar�   Úts           r/   Ú_generate_dummy_audioz OnnxConfig._generate_dummy_audio  sv   € ð ˆ
Ü�zÓ"ò 	GˆAä—‘˜A˜}¬c°-À-Ñ2OÓ.PÐ[`ÔaˆAð ×Ñ˜c¤B§F¡F¨1¬r¯u©u©9°yÑ+@À1Ñ+DÓ$EÑEÕFð	Gð Ðr.   Úpreprocessor)r   r   r   Ú
seq_lengthÚnum_choicesÚis_pairÚ	frameworkÚ	tokenizerr   c                 ó  — ddl m} ddlm} ddlm} t        ||«      r|�t        d«      ‚|�1t        j                  dt        «       t        j                  d«       |}t        ||«      �rAt        |t        j                  d¬	«      }|j!                  |«      }t        |t        j"                  |¬	«      }|j$                  �$t'        |j$                  «      dkD  r|j$                  nd
}dj)                  |g«      |z  g|z  }| j*                  dk(  r‹t        |t        j,                  d¬	«      }||z  } |||¬«      }|j/                  «       D ]2  \  }}t1        dt'        |«      |«      D �cg c]
  }||||z    ‘Œ c}||<   Œ4 t3        |j5                  |¬«      «      S t3         |||¬«      «      S t        ||«      r†|j6                  d   dk7  r2t        d|j8                  j:                  › d|j6                  d   › �«      ‚t        |t        j                  ¬«      }| j=                  |||	|«      }t3         |||¬«      «      S t        ||«      rT|j6                  d   dk(  rBt        |t        j                  ¬«      }| j=                  |||	|«      }t3         |||¬«      «      S t        ||«      rT|j6                  d   dk(  rBt        |t        j                  ¬«      }| j?                  ||
||«      }t3         |||¬«      «      S t        d«      ‚c c}w )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   zPYou 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ú rC   )Ú	text_pair)Ú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   Ú
isinstancerJ   ÚwarningsÚwarnÚFutureWarningÚloggerÚwarningr   r1   rh   Únum_special_tokens_to_addrk   Ú	unk_tokenÚlenÚjoinrE   rn   Úitemsrˆ   ÚdictÚconvert_to_tensorsÚmodel_input_namesÚ	__class__r'   r’   rž   )rQ   rŸ   r€   r    r¡   r¢   r£   r�   rƒ   r‚   r“   r”   r•   r¤   r   r   r   Útoken_to_addÚinput_tokenÚdummy_inputÚtokenized_inputÚkÚvÚis                           r/   Úgenerate_dummy_inputsz OnnxConfig.generate_dummy_inputs  s   € õ` 	FÝAÝEä�lÐ$;Ô<ÀÐAVÜÐoÓpÐpØÐ Ü�M‰Mð+äôô
 �N‰NÐoÔpØ$ˆLÜ�lÐ$;Õ<ä9Ø¬J×,JÑ,JÐ]^ôˆJð (×AÑAÀ'ÓJˆLÜ9Ø¬J×,MÑ,MÐ`lôˆJð !×*Ñ*Ð6¼3¸|×?UÑ?UÓ;VÐYZÒ;Zð ×&Ò&àð ð
 Ÿ8™8 [ MÓ2°ZÑ?Ð@À:ÑMˆKØ�y‰yÐ-Ò-ô ?Ø´×1UÑ1UÐhiô�ð *¨KÑ7�á".¨{ÀkÔ"R�à+×1Ñ1Ó3ò i‘D�A�qÜJOÐPQÔSVÐWXÓSYÐ[fÓJgÖ)hÀQ¨!¨A°°K±Ò*@Ò)h�O AÒ&ðiä˜O×>Ñ>È9Ð>ÓUÓVÐVÜ™ [ÀÔKÓLÐLÜ˜Ð&:Ô;Ø×-Ñ-¨aÑ0°NÒBÜ Ø@À×AWÑAW×A`ÑA`Ð@að bMØMY×MkÑMkÐlmÑMnÐLoðqóð ô
 :¸*ÔV`×VtÑVtÔuˆJØ×5Ñ5°jÀ,ÐP\Ð^iÓjˆKÜ™¨KÈ	ÔRÓSÐSÜ˜Ð&<Ô=À,×B`ÑB`ÐabÑBcÐguÒBuä9¸*ÔV`×VtÑVtÔuˆJØ×5Ñ5°jÀ,ÐP\Ð^iÓjˆKÜ™¨KÈ	ÔRÓSÐSä�|Ð%;Ô<À×A_ÑA_Ð`aÑAbÐfvÒAvô :¸*ÔV`×VtÑVtÔuˆJØ×4Ñ4°ZÀÐP]Ð_hÓiˆKÜ™ [ÀÔKÓLÐLäØnóð ùò5 *is   Å(L
Úreference_model_inputsc                 ó   — |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-   )rQ   rÉ   s     r/   Ú!generate_dummy_inputs_onnxruntimez,OnnxConfig.generate_dummy_inputs_onnxruntime“  s
   € ð &Ð%r.   c                 óÎ   — | j                   D ]V  }|j                  €|j                  n|j                  |j                  «      }t        |j                  |j
                  |«       ŒX y ©N)rL   r&   r$   Úsetattrr"   r#   )rQ   rR   r$   s      r/   Ú	patch_opszOnnxConfig.patch_ops¡  sO   € Ø×(Ñ(ò 	2ˆDØ*.¯/©/Ð*A˜ŸšÀtÇÁÐW[×WeÑWeÓGfˆIÜ�D—F‘F˜DŸI™I yÕ1ñ	2r.   c                 óÎ   — | j                   D ]V  }|j                  €|j                  n|j                  |j                  «      }t        |j                  |j
                  |«       ŒX y rÍ   )rL   r&   r%   rÎ   r"   r#   )rQ   rR   r%   s      r/   Úrestore_opszOnnxConfig.restore_ops¦  sO   € Ø×(Ñ(ò 	0ˆDØ&*§o¡oÐ&=�d—l’lÀ4Ç?Á?ÐSW×S_ÑS_ÓC`ˆGÜ�D—F‘F˜DŸI™I wÕ/ñ	0r.   r#   Úfieldc                 ó€   — ddl m} t        |j                  |«      «      D ��ci c]  \  }}|› d|› �|“Œ c}}S c c}}w )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   )Úchainú.)Ú	itertoolsrÔ   Ú	enumerateÚfrom_iterable)rX   r#   rÒ   rÔ   ÚidxÚitems         r/   Ú"flatten_output_collection_propertyz-OnnxConfig.flatten_output_collection_property«  s?   € õ 	$ä7@À×ATÑATÐUZÓA[Ó7\×]©)¨#¨t�4�&˜˜#˜� Ñ%Ó]Ð]ùÓ]s   ¤:)rB   N©rB   )r   r@   é(   rÝ   )r   é"V  ç      @éÜ   )éÿÿÿÿrá   rá   FNr@   rÝ   rÝ   rÞ   rß   rà   N)0r'   r(   r)   r*   rh   rk   rn   r   rx   ry   r   rI   r,   r   Úlistr!   rT   ÚclassmethodrY   Úpropertyr   r   r™   r]   rb   r	   rf   ri   rl   ro   rr   Úfloatrt   Úboolrz   Ústaticmethodr   r’   rž   r   r   rÈ   rË   rÏ   rÑ   r   r½   rÛ   r-   r.   r/   r1   r1   E   sÐ  „ ñð ÐØÐØ !ÐØ!. §¡¨uÓ!5Ðá  (°¸JÑ,GÐ!HÓIÙÐ 3¸ÀZÑ5PÐQÓRÙ +¨X¸7ÀzÑ7RÐ,SÓ TÙ)à%¨*Ñ5Ø")¨jÑ9Ø")¨jÑ9ñó
ñ ! (°¸JÑ,GÐ!HÓIÙ  (°¸JÑ,GÐ!HÓIÙ&¨°1°g°,Ð'?Ó@Ù'à%¨*Ñ5Ø")¨jÑ9ñó
ñ *à$+°
Ñ ;Ø")¨jÑ9ñó
ñ "-¨h¸GÈÐYaÐfmÑ8nÐ-oÓ!pÙ! 8°Ð=OÑ-PÐ"QÓRÙ#.°¸1¸g¸,Ð/GÓ#HÙ +¨X¸7ÀzÑ7RÐ,SÓ TÙ$ h°GÀ
Ñ0KÐ%LÓMÙ$ h°GÀ
Ñ0KÐ%LÓMñ?  ÐðF quñ4Ø(ð4Ø03ð4ØQYÐZ^Ð_kÑZlÑQmó4ð$ ñ
&Ð'9ð 
&Àð 
&ÐUaò 
&ó ð
&ð Øð$˜  W¨S°#¨XÑ%6Ð 6Ñ7ò $ó ó ð$ð ð-˜  g¨c°3¨hÑ&7Ð!7Ñ8ò -ó ð-ð ð
 ¨'°#°s°(Ñ*;Ñ!<ò 
ó ð
ð ð. Cò .ó ð.ð ð1¨ò 1ó ð1ð ð4 Sò 4ó ð4ð ð" Cò "ó ð"ð ð Uò ó ðð ð¨Dò ó ðð ð
°ð 
¸ò 
ó ð
ð" fhñØðØ14ðØHKðØ_bóð mpñØðØ25ðØNSðØfióð  ØØØØ*.ØØØØ"Ø"ØØ9=ñvàÐgÑhðvð ðvð ð	vð
 ðvð ðvð ˜JÑ'ðvð ðvð ðvð ðvð ðvð ðvð ðvð Ð5Ñ6ðvð 
��c�Ñ	óvðp&ÈÐPSÐUXÐPXÑHYð &Ð^eÐfiÐknÐfnÑ^oó &ò2ò
0ð
 ð^°cð ^À(È3Á-ð ^ÐTXÐY\Ð^aÐYaÑTbò ^ó ñ^r.   r1   c                   ó|  ‡ — e Zd Z	 	 	 ddddedeee      defˆ fd„Ze	ddddedd fd„«       Z
edeeeeef   f   fˆ fd	„«       Zedeeeef      fd
„«       Zedefd„«       Zedefd„«       Z	 	 	 	 ddddedededee   deeef   fˆ fd„Z	 ddeeeeef   f   dedefd„Zd„ Zdedee   deeef   fˆ fd„Zˆ xZS ) ÚOnnxConfigWithPastrD   r   rE   rF   Úuse_pastc                 ó8   •— t         ‰| �  |||¬«       || _        y )N)rE   rF   )ÚsuperrT   rê   )rQ   rD   rE   rF   rê   rÀ   s        €r/   rT   zOnnxConfigWithPast.__init__¿  s    ø€ ô 	‰Ñ˜ d¸>ÐÔJØ ˆ�r.   rU   c                 ó   —  | ||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)rE   rê   r-   rW   s      r/   Ú	with_pastzOnnxConfigWithPast.with_pastÉ  s   € ñ �6 ¨tÔ4Ð4r.   c                 óZ   •— t         ‰| �  }| j                  r| j                  |d¬«       |S )Nrb   ©Ú	direction)rì   rb   rê   Úfill_with_past_key_values_)rQ   ra   rÀ   s     €r/   rb   zOnnxConfigWithPast.outputsÖ  s,   ø€ ä™™ˆØ�=Š=Ø×+Ñ+¨NÀiÐ+ÔPàÐr.   c                 óL   — t        | j                  d«      rd| j                  iS y )Nrd   )re   rH   rê   r\   s    r/   rf   z"OnnxConfigWithPast.values_overrideÞ  s"   € ä�4—<‘< Ô-Ø §¡Ð/Ð/àr.   c                 óp   — 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_layersz�could not find the number of layers attribute in the model configuration, override the num_layers property of the model OnnxConfig to solve this)re   rH   ÚAttributeErrorrõ   r\   s    r/   rõ   zOnnxConfigWithPast.num_layerså  s7   € ô �t—|‘| \Ô2Ü ðBóð ð �|‰|×&Ñ&Ð&r.   c                 óp   — t        | j                  d«      st        d«      ‚| j                  j                  S )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_headsz¢could 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)re   rH   rö   rø   r\   s    r/   rø   z&OnnxConfigWithPast.num_attention_headsò  s8   € ô �t—|‘|Ð%:Ô;Ü ðVóð ð �|‰|×/Ñ/Ð/r.   r¤   r   r€   r    r¢   r£   c                 ó0  •— t         ‰| �  |||||¬«      }| j                  rôt        «       st	        d«      ‚dd l}|d   j                  \  }}	|	dz   }
|| j                  |
| j                  j                  | j                  z  f}d|v r<|d   j                  }|j                  |d   |j                  ||
|¬«      gd¬	«      |d<   g |d
<   t        | j                  «      D ]6  }|d
   j                  |j!                  |«      |j!                  |«      f«       Œ8 |S )N©r€   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   rJ   ÚtorchÚshaperø   rH   Úhidden_sizerþ   ÚcatÚonesrˆ   rõ   rP   Úzeros)rQ   r¤   r€   r    r¢   r£   Úcommon_inputsr  r5   ÚseqlenÚpast_key_values_lengthr  Ú
mask_dtyper�   rÀ   s                 €r/   rÈ   z(OnnxConfigWithPast.generate_dummy_inputsÿ  s@  ø€ ô ™Ñ5Ø *¸ÈWÐ`ið 6ó 
ˆð �=Š=Ü%Ô'Ü Ð!dÓeÐeãà)¨+Ñ6×<Ñ<‰MˆE�6à%+¨a¡ZÐ"àØ×(Ñ(Ø&Ø—‘×(Ñ(¨D×,DÑ,DÑDð	ˆEð   =Ñ0Ø*Ð+;Ñ<×BÑB�
Ø27·)±)Ø"Ð#3Ñ4°e·j±jÀÐH^Ðfp°jÓ6qÐrØð 3<ó 3�Ð.Ñ/ð
 02ˆMÐ+Ñ,Ü˜4Ÿ?™?Ó+ò b�ØÐ/Ñ0×7Ñ7¸¿¹ÀUÓ9KÈUÏ[É[ÐY^ÓM_Ð8`Õaðbð Ðr.   Úinputs_or_outputsrñ   Úinverted_values_shapec                 óÊ   — |dvrt        d|› d�«      ‚|dk(  rdnd}t        | j                  «      D ]/  }ddd	œ||› d
|› d�<   |rdddœ||› d
|› d�<   Œ"ddd	œ||› d
|› d�<   Œ1 y)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.

        ©r]   rb   ú4direction must either be "inputs" or "outputs", but ú
 was givenr]   r   Úpresentr5   zpast_sequence + sequence©r   r   rÕ   ú.keyr7   ú.valueN)rJ   rˆ   rõ   )rQ   r  rñ   r  r#   rÇ   s         r/   rò   z-OnnxConfigWithPast.fill_with_past_key_values_)  s«   € ð Ð1Ñ1ÜÐSÐT]ÐS^Ð^hÐiÓjÐjà$-°Ò$9Ñ ¸yˆÜ�t—‘Ó'ò 	eˆAØ7>ÐC]Ñ3^Ð   a¨ s¨$Ð/Ñ0Ù$Ø=DÐIcÑ9dÐ! T F¨!¨A¨3¨fÐ"5Ò6à=DÐIcÑ9dÐ! T F¨!¨A¨3¨fÐ"5Ò6ñ	er.   c                 ó<   — |d   ||› d|› d�<   |d   ||› d|› d�<   y )Nr   rÕ   r  r   r  r-   ©rQ   Úflattened_outputr#   rÙ   r�   s        r/   Ú_flatten_past_key_values_z,OnnxConfigWithPast._flatten_past_key_values_B  s:   € Ø01°!±Ð˜D˜6  3 % tÐ,Ñ-Ø23°A±$Ð˜D˜6  3 % vÐ.Ò/r.   r#   rÒ   c                 ó†   •— i }|dv r)t        |«      D ]  \  }}| j                  ||||«       Œ |S t        ‰| �  ||«      }|S )N)r  r   )r×   r  rì   rÛ   )rQ   r#   rÒ   r  rÙ   r�   rÀ   s         €r/   rÛ   z5OnnxConfigWithPast.flatten_output_collection_propertyF  sd   ø€ ØÐØÐ1Ñ1Ü# EÓ*ò O‘��QØ×.Ñ.Ð/?ÀÀsÈAÕNðOð
  Ðô  %™wÑIÈ$ÐPUÓVÐàÐr.   )rB   NFrÜ   ©rá   rá   FN)F)r'   r(   r)   r,   r   râ   r!   ræ   rT   rã   rî   rä   r   r™   rb   r	   rf   rõ   rø   r   rÈ   rò   r  r   r½   rÛ   Ú__classcell__©rÀ   s   @r/   ré   ré   ¾  sÇ  ø„ ð Ø7;Øñ!à"ð!ð ð!ð !  lÑ!3Ñ4ð	!ð
 õ!ð ñ
5Ð1ð 
5¸ð 
5ÐMaò 
5ó ð
5ð ð˜  g¨c°3¨hÑ&7Ð!7Ñ8ô ó ðð ð ¨'°#°s°(Ñ*;Ñ!<ò ó ðð ð
'˜Cò 
'ó ð
'ð ð
0 Sò 
0ó ð
0ð ØØØ*.ñ(à,ð(ð ð(ð ð	(ð
 ð(ð ˜JÑ'ð(ð 
��c�Ñ	õ(ðV qvñeØ!(¨¨g°c¸3°hÑ.?Ð)?Ñ!@ðeØMPðeØimóeò27ð °sð  À8ÈCÁ=ð  ÐUYÐZ]Ð_bÐZbÑUc÷  ñ  r.   ré   c                   óø   ‡ — e Zd Zedeeeeef   f   fˆ fd„«       Zedeedf   fˆ fd„«       Z	edeedf   fˆ fd„«       Z
	 	 	 	 dded   ded	ed
edee   deeef   fˆ fd„Zdeeeeef   f   defd„Zd„ Zˆ xZS )ÚOnnxSeq2SeqConfigWithPastrU   c                 óð   •— t         t        | �
  }|j                  «       D ]4  \  }}d|v rdnd}|j                  «       D ]  \  }}d|v r|||<   Œ|||<   Œ Œ6 | j                  r| j                  |d¬«       |S )NÚencoderÚencoder_sequencerA   r6   rb   rð   )rì   ré   rb   r¼   rê   rò   )rQ   ra   r#   Ú
axes_namesÚsequence_nameÚaxis_idxrÀ   s         €r/   rb   z!OnnxSeq2SeqConfigWithPast.outputsR  s™   ø€ äÔ1°4Ñ@ˆà .× 4Ñ 4Ó 6ò 	0ÑˆD�*Ø2;¸tÑ2CÑ.ÐI[ˆMØ",×"2Ñ"2Ó"4ò 0‘�˜$Ø Ñ%Ø+8�J˜xÒ(ð ,0�J˜xÒ(ñ0ð	0ð �=Š=Ø×+Ñ+¨NÀiÐ+ÔPàÐr.   .c                 ó  •— 	 t         ‰| �  }||f}|S # t        $ rg t        | j                  d«      rEt        | j                  d«      r/| j                  j
                  | j                  j                  f}Y |S t        d«      ‚w xY w)NÚencoder_layersÚdecoder_layersz¥could 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ö   re   rH   r&  r'  )rQ   rõ   rÀ   s     €r/   rõ   z$OnnxSeq2SeqConfigWithPast.num_layersc  s�   ø€ ð
	Ü™Ñ+ˆJØ$ jÐ1ˆJð Ðøô ò 	Ü�t—|‘|Ð%5Ô6¼7À4Ç<Á<ÐQaÔ;bØ"Ÿl™l×9Ñ9¸4¿<¹<×;VÑ;VÐW‘
ð Ðô %ð^óð ð		úó   ƒ “A!BÁ7Bc                 ó  •— 	 t         ‰| �  }||f}|S # t        $ rg t        | j                  d«      rEt        | j                  d«      r/| j                  j
                  | j                  j                  f}Y |S t        d«      ‚w xY w)NÚencoder_attention_headsÚdecoder_attention_headszÃcould 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ö   re   rH   r*  r+  )rQ   rø   rÀ   s     €r/   rø   z-OnnxSeq2SeqConfigWithPast.num_attention_headss  s�   ø€ ð	Ü"'¡'Ñ"=ÐØ#6Ð8KÐ"LÐð #Ð"øô ò 	Ü�t—|‘|Ð%>Ô?ÄGÈDÏLÉLÐZsÔDtØ'+§|¡|×'KÑ'KÈTÏ\É\×MqÑMqÐ&rÑ#ð #Ð"ô %ðóð ð		úr(  r¤   r   r€   r    r¢   r£   c           	      ó  •— t         t        | �  |||||¬«      }| j                  s|nd}t         t        | �  |||||¬«      }|j	                  «       D �	�
ci c]  \  }	}
d|	› �|
“Œ }}	}
t        di |¤|¤Ž}| j                  �r…t        «       st        d«      ‚dd l}|d   j                  d   }|d   j                  d   }|d   j                  d   }| j                  \  }}|||| j                  j                  |z  f}|||dz   | j                  j                  |z  f}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 )Nrú   r   Údecoder_rû   r   rü   Údecoder_input_idsr@   r   r   Údecoderr-   )rì   ré   rÈ   rê   r¼   r½   r   rJ   r  r  rø   rH   r  rõ   ÚminÚmaxrˆ   rP   r  )rQ   r¤   r€   r    r¢   r£   Úencoder_inputsÚdecoder_seq_lengthÚdecoder_inputsr#   Útensorr  r  r5   Ú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À   s                             €r/   rÈ   z/OnnxSeq2SeqConfigWithPast.generate_dummy_inputsƒ  so  ø€ ô Ô1°4ÑNØ *¸ÈWÐ`ið Oó 
ˆð
 04¯}ª}™ZÀ!ÐÜÔ1°4ÑNØ *Ð9KÐU\Ðhqð Oó 
ˆð IW×H\ÑH\ÓH^×_¹¸¸f˜H T FÐ+¨VÑ3Ð_ˆÑ_ÜÑ@˜~Ð@°Ñ@ˆà�=‹=Ü%Ô'Ü Ð!dÓeÐeãØ! +Ñ.×4Ñ4°QÑ7ˆEØ!.¨{Ñ!;×!AÑ!AÀ!Ñ!DÐØ!.Ð/BÑ!C×!IÑ!IÈ!Ñ!LÐØGK×G_ÑG_ÑDÐ'Ð)DàØ+Ø"Ø—‘×(Ñ(Ð,GÑGð	ˆMð Ø+à" QÑ&Ø—‘×(Ñ(Ð,GÑGðˆMð 02ˆMÐ+Ñ,à59·_±_Ñ2ÐÐ 2Ü Ð!3Ð5GÓHˆNÜ Ð!3Ð5GÓHÈ>ÑYˆNØ/AÐDVÒ/V¡)Ð\eÐä˜>Ó*ò 
�ð Ð/Ñ0×7Ñ7àŸ™ MÓ2ØŸ™ MÓ2ØŸ™ MÓ2ØŸ™ MÓ2ð	õð
ð &9¸IÒ%E‘MÈ=ˆEÜ˜>¨>Ó:ò b�ØÐ/Ñ0×7Ñ7¸¿¹ÀUÓ9KÈUÏ[É[ÐY^ÓM_Ð8`Õaðbð Ðùóe `s   ÁH	r  rñ   c           	      ó¦  — |dvrt        d|› d�«      ‚|dk(  rdnd}| j                  \  }}t        ||«      }t        ||«      |z
  }||kD  rdnd}d	}	|dk(  rd
nd}
t	        |«      D ]:  }d|
dœ||› d|› d�<   d|
dœ||› d|› d�<   d|	dœ||› d|› d�<   d|	dœ||› d|› d�<   Œ< t	        ||«      D ]   }|dk(  rd|	dœ}nd|
dœ}|||› d|› d|› d�<   Œ" y )Nr  r  r  r]   r   r  r   r/  Úpast_encoder_sequenceÚpast_decoder_sequencez past_decoder_sequence + sequencer5   r  rÕ   ú.decoder.keyú.decoder.valueú.encoder.keyú.encoder.valuer  )rJ   rõ   r0  r1  rˆ   )rQ   r  rñ   r#   r;  r<  r=  r>  r?  r!  rA   rÇ   Ú	axes_infos                r/   rò   z4OnnxSeq2SeqConfigWithPast.fill_with_past_key_values_È  ss  € ØÐ1Ñ1ÜÐSÐT]ÐS^Ð^hÐiÓjÐjà$-°Ò$9Ñ ¸yˆð 26·±Ñ.ÐÐ.ÜÐ/Ð1CÓDˆÜÐ/Ð1CÓDÀ~ÑUˆØ+=Ð@RÒ+R™iÐXaÐà2ÐØ6?À8Ò6KÑ2ÐQsÐä�~Ó&ò 	_ˆAØ?FÐK[Ñ;\Ð   a¨ s¨,Ð7Ñ8ØAHÐM]Ñ=^Ð   a¨ s¨.Ð9Ñ:Ø?FÐK[Ñ;\Ð   a¨ s¨,Ð7Ñ8ØAHÐM]Ñ=^Ð   a¨ s¨.Ð9Ò:ð		_ô �~ ~Ó6ò 	SˆAØ" iÒ/Ø 'Ð,<Ñ=‘	à 'Ð,<Ñ=�	ØIRÐ   a¨ s¨!Ð,?Ð+@ÀÐEÒFñ	Sr.   c                 ót   — |d   ||› d|› d�<   |d   ||› d|› d�<   |d   ||› d|› d�<   |d   ||› d|› d	�<   y )
Nr   rÕ   rC  r   rD  r   rE  r@   rF  r-   r  s        r/   r  z3OnnxSeq2SeqConfigWithPast._flatten_past_key_values_ä  sr   € Ø89¸!¹Ð˜D˜6  3 % |Ð4Ñ5Ø:;¸A¹$Ð˜D˜6  3 % ~Ð6Ñ7Ø89¸!¹Ð˜D˜6  3 % |Ð4Ñ5Ø:;¸A¹$Ð˜D˜6  3 % ~Ð6Ò7r.   r  )r'   r(   r)   rä   r   r,   r™   rb   Útuplerõ   rø   r   ræ   r   r	   rÈ   rò   r  r  r  s   @r/   r  r  Q  s  ø„ Øð˜  g¨c°3¨hÑ&7Ð!7Ñ8ô ó ðð  ð˜E # s (™Oô ó ðð ð# U¨3°¨8¡_ô #ó ð#ð$ ØØØ*.ñCàÐ5Ñ6ðCð ðCð ð	Cð
 ðCð ˜JÑ'ðCð 
��c�Ñ	õCðJS¸GÀCÈÐQTÐVYÐQYÑIZÐDZÑ<[ð SÐhkó Sö8?r.   r  )1r_   rM   r³   Úabcr   r   Úcollectionsr   Úcollections.abcr   r   Útypingr   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¶   rq   r~   Ú	dataclassr!   r1   ré   r  r-   r.   r/   ú<module>rU     s¿   ðó Û Û ß #Ý #ß -ß @Õ @ã Ý ç PÓ Pß hÑ hñ Ý6ÝAÝ=ÝAñ ÔÝà	ˆ×	Ñ	˜HÓ	%€ð Ð ð #9Ð ð ×Ñ÷*ð *ó ð*ô(v^�ô v^ôrP ˜ Sô P ôfW?Ð 2õ W?r.   