/usr/local/lib/python3.6/site-packages/transformers/onnx/__pycache__
NameSizeModeActions
config.cpython-36.pyc208370644editdlrm
convert.cpython-36.pyc128450644editdlrm
features.cpython-36.pyc102140644editdlrm
utils.cpython-36.pyc15240644editdlrm
__init__.cpython-36.pyc8760644editdlrm
__main__.cpython-36.pyc27020644editdlrm
Edit: /usr/local/lib/python3.6/site-packages/transformers/onnx/__pycache__/config.cpython-36.pyc (20837B)
3 Eg\f@s:ddlZddlZddlZddlmZmZddlmZddlm Z m Z m Z m Z m Z mZmZmZmZmZddlZddlmZddlmZmZmZmZdd lmZmZmZe rdd lm Z dd l!m"Z"dd l#m$Z$erdd l%m&Z&ej'e(Z)dZ*dZ+ej,GdddZ-GdddeZ.Gddde.eZ/Gddde/Z0dS)N)ABCabstractmethod) OrderedDict) TYPE_CHECKINGAnyCallableDictIterableListMappingOptionalTupleUnion)version) TensorTypeis_torch_availableis_vision_availablelogging)ParameterFormat compute_effective_axis_dimension"compute_serialized_parameters_size)PretrainedConfig)FeatureExtractionMixin)PreTrainedTokenizerBase)Image ic@s6eZdZUdZeee dZ e e dZ e e dS) 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. N) __name__ __module__ __qualname____doc__rostrnamer custom_oporig_opr op_wrapperr)r)B/usr/local/lib/python3.6/site-packages/transformers/onnx/config.pyr.s  rc @s:eZdZdZdZdZejdZe ddddie d dddie d dddie d dd die d d diie d dddie d d diie ddddddd e d dddid Z d>de e e dddZed?de ddddZeeee eee ffdddZeee eee ffdddZeeee efdddZeedddZeedd d!Zeedd"d#Zeedd$d%Zeedd&d'Z e!eed(d)d*Z"d@eeeed-d.d/Z#dCe$dDeeeee%eeed2ee efd4 d5d6Z&d7d8Z'd9d:Z(ee e)ee*e efd;d 0 )r+default_fixed_batch)r=r)r)r*default_batch_sizes zOnnxConfig.default_batch_sizecCstjS)zu The default sequence length to use if no other indication Returns: Integer > 0 )r+default_fixed_sequence)r=r)r)r*default_sequence_lengthsz"OnnxConfig.default_sequence_lengthcCstS)z{ Which onnx opset to use when exporting the model Returns: Integer ONNX Opset version )DEFAULT_ONNX_OPSET)r=r)r)r*default_onnx_opsetszOnnxConfig.default_onnx_opsetcCsdS)z What absolute tolerance value to use during model conversion validation. Returns: Float absolute tolerance value. gh㈵>r))r=r)r)r*atol_for_validationszOnnxConfig.atol_for_validationcCs$trddlm}||jkSdSdS)z The minimum PyTorch version required to export the model. Returns: `bool`: Whether the installed version of PyTorch is compatible with the model. r) torch_versionFN)rZtransformers.utilsrStorch_onnx_minimum_version)r=rSr)r)r*is_torch_support_availables  z%OnnxConfig.is_torch_support_available)num_parametersr@cCst|tjtkS)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 )rrZFloatEXTERNAL_DATA_FORMAT_SIZE_LIMIT)rVr)r)r*use_external_data_formats z#OnnxConfig.use_external_data_format() batch_size num_channels image_height image_widthcCsJg}x@t|D]4}tjj|||d}|jtj|jdjdqW|S)NZuint8ZRGB) rangenprandomZrandr<rZ fromarrayZastypeconvert)r=r[r\r]r^images_datar)r)r*_generate_dummy_imagess  z!OnnxConfig._generate_dummy_imagesrFrr) preprocessorr[ seq_lengthis_pair frameworkr\r^r] tokenizerr@c Csddlm} ddlm} t|| r2| dk r2td| dk rTtjdtt j d| }t|| rt |t j dd }|j|} t |t j| d }d j|jg|g|} t|| |d St|| r|jdd krt |t j d }|j||||} t|| |dStddS)a, Generate inputs to provide to the ONNX exporter for the specific framework Args: preprocessor: ([`PreTrainedTokenizerBase`] or [`FeatureExtractionMixin`]): 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). 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. Returns: Mapping[str, Tensor] holding the kwargs to provide to the model's forward function r)r)rNzPYou 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_dimensionZnum_token_to_add )return_tensorsZ pixel_values)rm)rdroz\Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.)feature_extraction_utilsrtokenization_utils_baser isinstancer6warningswarn FutureWarningloggerwarningrr+rLZnum_special_tokens_to_addrNjoinZ unk_tokendictZmodel_input_namesrg)r=rhr[rirjrkr\r^r]rlrrZ token_to_addZ dummy_inputr)r)r*generate_dummy_inputss0$     z OnnxConfig.generate_dummy_inputscCs@x:|jD]0}|jdkr|jn |j|j}t|j|j|qWdS)N)r8r(r&setattrr#r%)r=r>r&r)r)r* patch_ops5s zOnnxConfig.patch_opscCs@x:|jD]0}|jdkr|jn |j|j}t|j|j|qWdS)N)r8r(r'r{r#r%)r=r>r'r)r)r* restore_ops:s zOnnxConfig.restore_ops)r%fieldr@cs(ddlm}fddt|j|DS)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)chaincs i|]\}}|d|qS).r)).0idxitem)r%r)r* OszAOnnxConfig.flatten_output_collection_property..) itertoolsr enumerate from_iterable)rAr%r~rr))r%r*"flatten_output_collection_property?s z-OnnxConfig.flatten_output_collection_property)r0N)r0)rrYrZrZr)rrFNrYrZrZN)rr),rr r!r"rLrNrparserTrr5r$r rr? classmethodrBpropertyrr intrDrHr rrKrMrOrQfloatrRboolrU staticmethodrXrgrrrzr|r}r rrr)r)r)r*r+Csf  "        <r+c s"eZdZd!deeeedfdd Zed"deddd d Z e e ee e effd fd d Z e ee eefd ddZe e d ddZe e d ddZd%de e eeee eefdfdd Ze ee e effedddZddZeeeeeefdfdd ZZS)&OnnxConfigWithPastr0NFr)r1r2r3use_pastcstj|||d||_dS)N)r2r3)superr?r)r=r1r2r3r) __class__r)r*r?SszOnnxConfigWithPast.__init__)r1r2r@cCs|||ddS)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)r2rr))rAr1r2r)r)r* with_past]s zOnnxConfigWithPast.with_past)r@cs tj}|jr|j|dd|S)NrH) direction)rrHrfill_with_past_key_values_)r=rG)rr)r*rHjszOnnxConfigWithPast.outputscCst|jdrd|jiSdS)NrI)rJr4r)r=r)r)r*rKrs  z"OnnxConfigWithPast.values_overridecCst|jdstd|jjS)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)rJr4AttributeErrorr)r=r)r)r*rys zOnnxConfigWithPast.num_layerscCst|jdstd|jjS)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)rJr4rr)r=r)r)r*rs z&OnnxConfigWithPast.num_attention_headsrr)rlr[rirjrkr@c stj|||||}|jrts*tdnddl}|dj\}} | d} ||j| |jj |jf} d|kr|j |d|j || gdd|d<g|d<x0t |j D]"} |dj|j| |j| fqW|S) NzACannot generate dummy past_keys inputs without PyTorch installed.r input_idsrZattention_maskr)Zdimpast_key_values)rrzrrr6torchshaperr4 hidden_sizecatZonesr`rr<zeros) r=rlr[rirjrk common_inputsrr-ZseqlenZpast_key_values_lengthrre)rr)r*rzs$  "z(OnnxConfigWithPast.generate_dummy_inputs)inputs_or_outputsrcCst|dkrtd|d|dkr$dnd}xFt|jD]8}ddd ||d |d <ddd ||d |d <q4Wd S)a_ Fill the input_or_ouputs 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. rDrHz4direction must either be "inputs" or "outputs", but z was givenrpresentr-zpast_sequence + sequence)rrrz.keyz.valueN)rDrH)r6r`r)r=rrr%ir)r)r*rs z-OnnxConfigWithPast.fill_with_past_key_values_cCs4|d||d|d<|d||d|d<dS)Nrrz.keyrz.valuer))r=flattened_outputr%rtr)r)r*_flatten_past_key_values_sz,OnnxConfigWithPast._flatten_past_key_values_)r%r~r@csFi}|dkr4x4t|D]\}}|j||||qWntj||}|S)Nrr)rr)rrrr)r=r%r~rrr)rr)r*rs z5OnnxConfigWithPast.flatten_output_collection_property)r0NF)r0rr)rrFN)rr r!r$r rrr?rrrr rrHr rrKrrrrzrrr rr __classcell__r)r))rr*rRs* $  rc seZdZeeeeeeffdfdd Zeeedfdd Z eeedfdd Z dd eee e e eeefd fd d ZeeeeeffedddZddZZS)OnnxSeq2SeqConfigWithPast)r@csztt|j}xT|jD]H\}}d|kr*dnd}x.|jD]"\}}d|krR|||<q8|||<q8WqW|jrv|j|dd|S)Nencoderencoder_sequencer/r.rH)r)rrrHitemsrr)r=rGr%Z axes_namesZ sequence_nameZaxis_idx)rr)r*rHs  z!OnnxSeq2SeqConfigWithPast.outputsc s`ytj}||f}WnFtk rZt|jdrNt|jdrN|jj|jjf}ntdYnX|S)Nencoder_layersdecoder_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)rrrrJr4rr)r=r)rr)r*rs  z$OnnxSeq2SeqConfigWithPast.num_layersc s`ytj}||f}WnFtk rZt|jdrNt|jdrN|jj|jjf}ntdYnX|S)Nencoder_attention_headsdecoder_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)rrrrJr4rr)r=r)rr)r*rs  z-OnnxSeq2SeqConfigWithPast.num_attention_headsrFNr)rlr[rirjrkr@cstt|j|||||}|js"|nd}tt|j|||||}dd|jD}tf||} |jrtsvtdnddl} | dj d} | dj d} | dj d}|j \} }| | | |j j | f}| ||d|j j |f}g| d <|j \}}t||}t|||}||krd nd }x@t|D]4}| d j| j|| j|| j|| j|fq$W|d krj|n|}x2t||D]$}| d j| j|| j|fqzW| S) NrcSsi|]\}}|d|qS)Zdecoder_r))rr%Ztensorr)r)r*rszCOnnxSeq2SeqConfigWithPast.generate_dummy_inputs..zACannot generate dummy past_keys inputs without PyTorch installed.rrZdecoder_input_idsrYrrdecoder)rrrzrrryrr6rrrr4rrminmaxr`r<r)r=rlr[rirjrkZencoder_inputsZdecoder_seq_lengthZdecoder_inputsrrr-Zencoder_seq_lengthZnum_encoder_attention_headsZnum_decoder_attention_headsZ encoder_shapeZ decoder_shapenum_encoder_layersnum_decoder_layersmin_num_layersmax_num_layersremaining_side_namerer)rr)r*rzsL      $z/OnnxSeq2SeqConfigWithPast.generate_dummy_inputs)rrc Cs:|dkrtd|d|dkr$dnd}|j\}}t||}t|||}||krVdnd}d } |dkrjd nd } xxt|D]l} d | d ||d| d<d | d ||d| d<d | d ||d| d<d | d ||d| d<qxWxLt||D]>} |dkrd | d } n d | d } | ||d| d|d<qWdS)NrDrHz4direction must either be "inputs" or "outputs", but z was givenrrrrZpast_encoder_sequenceZpast_decoder_sequencez past_decoder_sequence + sequencer-)rrrz .decoder.keyz.decoder.valuez .encoder.keyz.encoder.valuez.key)rDrH)r6rrrr`) r=rrr%rrrrrrr/rZ axes_infor)r)r*rNs&     z4OnnxSeq2SeqConfigWithPast.fill_with_past_key_values_cCsd|d||d|d<|d||d|d<|d||d|d<|d||d|d <dS) Nrrz .decoder.keyrz.decoder.valuerz .encoder.keyrYz.encoder.valuer))r=rr%rrr)r)r*rjsz3OnnxSeq2SeqConfigWithPast._flatten_past_key_values_rr)rrFN)rr r!rr r$rrHr rrrr rrrzrrrr)r))rr*rs$? rii l)1rEr9rsabcrr collectionsrtypingrrrrr r r r r rZnumpyra packagingrutilsrrrrrrrZconfiguration_utilsrrprrqrZPILr get_loggerrrvrPrWZ dataclassrr+rrr)r)r)r*s2 0