
    sg,                        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	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jR                  e*      Z+dZ,dZ-ej\                   G d d             Z/ G d de      Z0 G d de0e      Z1 G d de1      Z2y)    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                   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)        K/var/www/html/venv/lib/python3.12/site-packages/transformers/onnx/config.pyr$   r$   /   s7    
 F
I"&GXh&%)J")r1   r$   c                   2   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   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d7d8d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)r7   
pred_boxes
pred_masksr   )r7   r<   )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keysrH   _patching_specsr(   dataclassesreplacegetattrr%   r&   append)selfrG   rH   rI   spec
final_specs         r2   __init__zOnnxConfig.__init__o   s    t444&CDDaDaDfDfDhCij  	!&4&@Nb 	4DJ||#(00wtvvtyy?YZ
  ''
3		4r1   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
        )rH   r0   clsrG   rH   s      r2   from_model_configzOnnxConfig.from_model_config   s     6%%r1   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rT   s    r2   inputszOnnxConfig.inputs   s     "##r1   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
        )rL   rH   copydeepcopy)rT   common_outputss     r2   outputszOnnxConfig.outputs   s'     66tyyA}}^,,r1   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)hasattrrK   r_   s    r2   values_overridezOnnxConfig.values_override   s      4<<-''r1   c                 "    t         j                  S )zp
        The default batch size to use if no other indication

        Returns:
            Integer > 0
        )r4   default_fixed_batchr_   s    r2   default_batch_sizezOnnxConfig.default_batch_size   s     ---r1   c                 "    t         j                  S )zu
        The default sequence length to use if no other indication

        Returns:
            Integer > 0
        )r4   default_fixed_sequencer_   s    r2   default_sequence_lengthz"OnnxConfig.default_sequence_length   s     000r1   c                 "    t         j                  S )zw
        The default number of choices to use if no other indication

        Returns:
            Integer > 0
        )r4   default_fixed_num_choicesr_   s    r2   default_num_choiceszOnnxConfig.default_num_choices   s     333r1   c                     t         S )z{
        Which onnx opset to use when exporting the model

        Returns:
            Integer ONNX Opset version
        )DEFAULT_ONNX_OPSETr_   s    r2   default_onnx_opsetzOnnxConfig.default_onnx_opset   s
     "!r1   c                      y)z
        What absolute tolerance value to use during model conversion validation.

        Returns:
            Float absolute tolerance value.
        gh㈵>r0   r_   s    r2   atol_for_validationzOnnxConfig.atol_for_validation   s     r1   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.utilsry   r   parsetorch_onnx_minimum_version)rT   ry   s     r2   is_torch_support_availablez%OnnxConfig.is_torch_support_available   s.     <==!2!459X9XXXr1   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    r2   use_external_data_formatz#OnnxConfig.use_external_data_format   s!     /~?T?TU./	
r1   
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randrS   r!   	fromarrayastypeconvert)rT   r   r   r   r   images_datas           r2   _generate_dummy_imagesz!OnnxConfig._generate_dummy_images  sj     z" 	PA99>>,\JSPDMM%//$++g*>?GGNO	P r1   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intrS   sinpi)rT   r   r   r   r   
audio_datar   ts           r2   _generate_dummy_audioz OnnxConfig._generate_dummy_audio  sv     
z" 	GAA}c--2O.P[`aA cBFF1ruu9y+@1+D$EEF	G r1   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(  rt        |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.zSOverwriting the `preprocessor` argument with `tokenizer` to generate dummmy inputs.r   )fixed_dimensionnum_token_to_add0 rF   )	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    
isinstancerM   warningswarnFutureWarningloggerwarningr   r4   rk   num_special_tokens_to_addrn   	unk_tokenlenjoinrH   rq   itemsr   dictconvert_to_tensorsmodel_input_names	__class__r*   r   r   )rT   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                           r2   generate_dummy_inputsz OnnxConfig.generate_dummy_inputs  s    ` 	FAEl$;<AVopp MM+
 NNpq$Ll$;<9J,J,J]^J (AA'JL9J,M,M`lJ !**63|?U?U;VYZ;Z && 
 88[M2Z?@:MKyy-- ?1U1Uhi *K7".{k"R+113 iDAqJOPQSVWXSY[fJg)hQ!AK*@)hOA&iO>>9>UVV[KLL&:;--a0NB @AWAWA`A`@a bMMYMkMklmMnLoq 
 :*V`VtVtuJ55j,P\^ijKK	RSS&<=,B`B`abBcguBu9*V`VtVtuJ55j,P\^ijKK	RSS|%;<A_A_`aAbfvAv :*V`VtVtuJ44ZP]_hiK[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
        r0   )rT   r   s     r2   !generate_dummy_inputs_onnxruntimez,OnnxConfig.generate_dummy_inputs_onnxruntime  s
     &%r1   c                     | j                   D ]V  }|j                  |j                  n|j                  |j                        }t        |j                  |j
                  |       X y N)rO   r)   r'   setattrr%   r&   )rT   rU   r'   s      r2   	patch_opszOnnxConfig.patch_ops  sO    (( 	2D*.//*AtW[WeWeGfIDFFDIIy1	2r1   c                     | j                   D ]V  }|j                  |j                  n|j                  |j                        }t        |j                  |j
                  |       X y r   )rO   r)   r(   r   r%   r&   )rT   rU   r(   s      r2   restore_opszOnnxConfig.restore_ops  sO    (( 	0D&*oo&=dll4??SWS_S_C`GDFFDIIw/	0r1   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)r[   r&   r   r   idxitems         r2   "flatten_output_collection_propertyz-OnnxConfig.flatten_output_collection_property  s?     	$7@ATATUZA[7\])#t4&#%]]]s   :)rE   NrE   )r   rC   (   r   )r   "V        @   )r   r   FNrC   r   r   r   r   r   N)0r*   r+   r,   r-   rk   rn   rq   r   r{   r|   r   rL   r/   r   r$   rW   classmethodr\   propertyr   r   r   r`   re   r   r   ri   rl   ro   rr   ru   floatrw   boolr}   staticmethodr   r   r   r   r   r   r   r   r   r
   r	   r   r0   r1   r2   r4   r4   D   s     !!.u!5 (J,G!HI 3Z5PQR +X7z7R,S T)%*5")j9")j9
 !(J,G!HI (J,G!HI&1g,'?@'%*5")j9
 *$+
 ;")j9
 "-hGYafm8n-o!p!8=O-P"QR#.1g,/G#H +X7z7R,S T$hG
0K%LM$hG
0K%LM?  D41 4 4Z^_kZl 4  
&'9 
& 
&Ua 
& 
& $WS#X%6 67 $  $ -gc3h&7!78 - - 
'#s(*;!< 
 
 .C . . 1 1 1 4S 4 4 "C " " U   D   
 
 
 
" fh14HK_b mp25NSfi  *.""/3vghv v 	v
 v v J'v v v v v v v -v 
c	vp&PSUXPXHY &^efiknfn^o &2
0
 ^c ^(3- ^TXY\^aYaTb ^ ^r1   r4   c                   v    e Zd Z	 	 	 dddded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 ) OnnxConfigWithPastrG   r   rH   rI   use_pastc                 8    t         |   |||       || _        y )N)rH   rI   )superrW   r   )rT   rG   rH   rI   r   r   s        r2   rW   zOnnxConfigWithPast.__init__  s      	d>J r1   rX   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)rH   r   r0   rZ   s      r2   	with_pastzOnnxConfigWithPast.with_past  s     6t44r1   c                 Z    t         |   }| j                  r| j                  |d       |S )Nre   	direction)r   re   r   fill_with_past_key_values_)rT   rd   r   s     r2   re   zOnnxConfigWithPast.outputs  s,    ==++Ni+Pr1   c                 L    t        | j                  d      rd| j                  iS y )Nrg   )rh   rK   r   r_   s    r2   ri   z"OnnxConfigWithPast.values_override  s"    4<<-//r1   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_layerszcould not find the number of layers attribute in the model configuration, override the num_layers property of the model OnnxConfig to solve this)rh   rK   AttributeErrorr   r_   s    r2   r   zOnnxConfigWithPast.num_layers  s7     t||\2 B  ||&&&r1   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_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)rh   rK   r   r   r_   s    r2   r   z&OnnxConfigWithPast.num_attention_heads  s8     t||%:; V  ||///r1   r   r    r   r   r   r   c                 0   t         |   |||||      }| j                  rt               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   rM   torchshaper   rK   hidden_sizer   catonesr   r   rS   zeros)rT   r   r   r   r   r   common_inputsr  r8   seqlenpast_key_values_lengthr  
mask_dtyper   r   s                 r2   r   z(OnnxConfigWithPast.generate_dummy_inputs  s@    5*W`i 6 
 ==%' !dee)+6<<ME6%+aZ"((&((D,D,DD	E  =0*+;<BB
27))"#34ejjH^fpj6qr 3< 3./
 02M+,4??+ b/077U9KU[[Y^M_8`ab r1   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`   re   4direction must either be "inputs" or "outputs", but 
 was givenr`   r  presentr8   zpast_sequence + sequencer   r   r   .keyr:   .valueN)rM   r   r   )rT   r  r   r  r&   r   s         r2   r   z-OnnxConfigWithPast.fill_with_past_key_values_&  s     11ST]S^^hijj$-$9 yt' 	eA7>C]3^as$/0$=DIc9d!TF!A3f"56=DIc9d!TF!A3f"56	er1   c                 <    |d   || d| d<   |d   || d| d<   y )Nr   r   r  r   r  r0   rT   flattened_outputr&   r   r   s        r2   _flatten_past_key_values_z,OnnxConfigWithPast._flatten_past_key_values_?  s:    01!D63%t,-23A$D63%v./r1   r&   r   c                     i }|dv r)t        |      D ]  \  }}| j                  ||||        |S t        |   ||      }|S )N)r  r  )r   r  r   r   )rT   r&   r   r  r   r   r   s         r2   r   z5OnnxConfigWithPast.flatten_output_collection_propertyC  sd    11#E* OQ../?sANO
    %wI$PUVr1   )rE   NFr   r   r   FN)F)r*   r+   r,   r/   r   r$   r   rW   r   r   r   r   r   re   r   r   ri   r   r   r   r   r   r  r
   r	   r   __classcell__r   s   @r2   r   r     s    -1!"! ! \*	!
 ! 
51 
5 
5Ma 
5 
5 gc3h&7!78   '#s(*;!<   
'C 
' 
' 
0S 
0 
0 *.(,( ( 	(
 ( J'( 
c	(V qve!(gc3h.?)?!@eMPeime27 s  8C=  UYZ]_bZbUc    r1   r   c                        e Zd Zedeeeeef   f   f fd       Zedee   f fd       Z	edee   f 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eeeeef   f   defdZd Z xZS )OnnxSeq2SeqConfigWithPastrX   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_sequencerD   r9   re   r   )r   r   re   r   r   r   )rT   rd   r&   
axes_namessequence_nameaxis_idxr   s         r2   re   z!OnnxSeq2SeqConfigWithPast.outputsO  s    14@ . 4 4 6 	0D*2;t2C.I[M","2"2"4 0$%+8Jx( ,0Jx(0	0 ==++Ni+Pr1   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_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   rh   rK   r(  r)  )rT   r   r   s     r2   r   z$OnnxSeq2SeqConfigWithPast.num_layers`  s    
	+J$j1J   	t||%5674<<Qa;b"ll994<<;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_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   rh   rK   r,  r-  )rT   r   r   s     r2   r   z-OnnxSeq2SeqConfigWithPast.num_attention_headsp  s    	"''"=#68K"L #"  	t||%>?GDLLZsDt'+||'K'KT\\MqMq&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                  rt               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_idsrC   r  r"  decoderr0   )r   r   r   r   r   r   r   rM   r  r  r   rK   r  r   minmaxr   rS   r  )rT   r   r   r   r   r   encoder_inputsdecoder_seq_lengthdecoder_inputsr&   tensorr	  r  r8   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                             r2   r   z/OnnxSeq2SeqConfigWithPast.generate_dummy_inputs  so    14N*W`i O 

 04}}Z!14N*9KU\hq O 
 IWH\H\H^_fHTF+V3__@~@@==%' !dee!+.44Q7E!.{!;!A!A!!D!./B!C!I!I!!LGKG_G_D')D+"((,GG	M +"Q&((,GGM 02M+,59__2 2 !35GHN !35GH>YN/ADV/V)\e>* 
 /077M2M2M2M2	
 &9I%EM=E>>: b/077U9KU[[Y^M_8`ab 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"  r1  past_encoder_sequencepast_decoder_sequencez past_decoder_sequence + sequencer8   r  r   .decoder.key.decoder.value.encoder.key.encoder.valuer  )rM   r   r2  r3  r   )rT   r  r   r&   r=  r>  r?  r@  rA  r#  rD   r   	axes_infos                r2   r   z4OnnxSeq2SeqConfigWithPast.fill_with_past_key_values_  ss   11ST]S^^hijj$-$9 y 26../1CD/1CD~U+=@R+RiXa26?86K2Qs~& 	_A?FK[;\as,78AHM]=^as.9:?FK[;\as,78AHM]=^as.9:		_ ~~6 	SA"i/ ',<=	 ',<=	IRas!,?+@EF	Sr1   c                 t    |d   || d| d<   |d   || d| d<   |d   || d| d<   |d   || d| d	<   y )
Nr   r   rE  r   rF  r   rG  rC   rH  r0   r  s        r2   r  z3OnnxSeq2SeqConfigWithPast._flatten_past_key_values_  sr    89!D63%|45:;A$D63%~6789!D63%|45:;A$D63%~67r1   r  )r*   r+   r,   r   r   r/   r   re   r   r   r   r   r   r   r   r   r   r  r  r  s   @r2   r   r   N  s   gc3h&7!78    E#J   #U3Z # #$ *.C,C C 	C
 C J'C 
c	CJSGCQTVYQYIZDZ<[ Shk S8?r1   r   )3rb   rP   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   rt   r   	dataclassr$   r4   r   r   r0   r1   r2   <module>rU     s       # # f f f   P P h h 6A=A 			H	%   #9  * * *(t^ t^nP S P fW? 2 W?r1   