
    rhi                        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&        k/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/transformers/onnx/config.pyr!   r!   0   s7    
 F
I"&GXh&%)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&CDDaDaDfDfDhCij  	!&4&@Nb 	4DJ||#(00wtvvtyy?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6tyy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.
        gh㈵>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9X9X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99>>,\JSPDMM%//$++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[`aA cBFF1ruu9y+@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(  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.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 MM+
 NNop$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
        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    (( 	2D*.//*AtW[WeWeGfIDFFDIIy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    (( 	0D&*oo&=dll4??SWS_S_C`GDFFDIIw/	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@ATATUZA[7\])#t4&#%]]]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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?  F qu4(4034QYZ^_kZlQm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  *.""9=vghv v 	v
 v v J'v v v v v v v 56v 
c	vp&PSUXPXHY &^efiknfn^o &2
0
 ^c ^(3- ^TXY\^aYaTb ^ ^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,    ==++Ni+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_layerszcould 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_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)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                  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   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<<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 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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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!D63%t,-23A$D6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../?sA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 
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    r.   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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    14@ . 4 4 6 	0D*2;t2C.I[M","2"2"4 0$%+8Jx( ,0Jx(0	0 ==++Ni+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_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   re   rH   r&  r'  )rQ   r   r   s     r/   r   z$OnnxSeq2SeqConfigWithPast.num_layersc  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   re   rH   r*  r+  )rQ   r   r   s     r/   r   z-OnnxSeq2SeqConfigWithPast.num_attention_headss  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_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    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   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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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!D63%|45:;A$D63%~6789!D63%|45:;A$D6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   gc3h&7!78    E#J   #U3Z # #$ *.C56C C 	C
 C J'C 
c	CJSGCQTVYQYIZDZ<[ 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.   