o
    [5iF                    @  s  d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZmZ ddlZddl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!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= ddl>m?Z? ddl@mAZAmBZB ddlCmDZD ddlEmFZF ddlGmHZHmIZI ddlJmKZK ddlLmMZMmNZN ddlOmPZP ddlQmRZR ddlSmTZT erddlmUZU eVeWZXdZYdZZdZ[dZ\dZ]dZ^d Z_e
G d!d" d"e2Z`G d#d$ d$eKeIZad%Zbe)e\G d&d' d'eaZcd(Zde)e\G d)d* d*eaZed+Zfe)e\G d,d- d-eaZgd.Zhe)e\G d/d0 d0eaZid1Zje)e\G d2d3 d3eaZkd4Zle)e\G d5d6 d6eaZmd7Zne)e\G d8d9 d9eaZoe)e\G d:d; d;eaZpd<Zqe)e\G d=d> d>eaZrd?Zse)e\G d@dA dAeaZtdBZue)e\G dCdD dDeaZvdEZwe)e\G dFdG dGeaZxdHZye)e\G dIdJ dJeaZzdKZ{e)e\G dLdM dMeaZ|dNZ}e)e\G dOdP dPeaZ~dS )QziORTModelForXXX classes, allowing to run ONNX Models with ONNX Runtime using the same API as Transformers.    )annotationsN)Sequence)	dataclass)Path)TemporaryDirectory)TYPE_CHECKINGAny)HUGGINGFACE_HUB_CACHE)
AutoConfig	AutoModelAutoModelForAudioClassification$AutoModelForAudioFrameClassificationAutoModelForAudioXVectorAutoModelForCTCAutoModelForImageClassificationAutoModelForImageToImageAutoModelForMaskedLMAutoModelForMultipleChoiceAutoModelForQuestionAnswering AutoModelForSemanticSegmentation"AutoModelForSequenceClassificationAutoModelForTokenClassification'AutoModelForZeroShotImageClassificationGenerationMixin)add_end_docstringsadd_start_docstrings%add_start_docstrings_to_model_forward)BaseModelOutputCausalLMOutputImageClassifierOutputImageSuperResolutionOutputMaskedLMOutputModelOutputMultipleChoiceModelOutputQuestionAnsweringModelOutputSemanticSegmenterOutputSequenceClassifierOutputTokenClassifierOutputXVectorOutput)-MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES)cached_fileis_offline_mode)Self)InferenceSessionSessionOptions)main_export)TasksManager)FROM_PRETRAINED_START_DOCSTRINGOptimizedModel)ORTSessionMixin)ONNX_FILE_PATTERNONNX_WEIGHTS_NAME)&prepare_providers_and_provider_options)find_files_matching_pattern)maybe_save_preprocessors)PretrainedConfigAutoTokenizerAutoFeatureExtractorAutoProcessora2  
    This model inherits from [`~onnxruntime.modeling_ort.ORTModel`], check its documentation for the generic methods the
    library implements for all its model (such as downloading or saving).

    This class should be initialized using the [`onnxruntime.modeling_ort.ORTModel.from_pretrained`] method.
a.  
    Args:
        input_ids (`Union[torch.Tensor, np.ndarray, None]` of shape `({0})`, defaults to `None`):
            Indices of input sequence tokens in the vocabulary.
            Indices can be obtained using [`AutoTokenizer`](https://huggingface.co/docs/transformers/autoclass_tutorial#autotokenizer).
            See [`PreTrainedTokenizer.encode`](https://huggingface.co/docs/transformers/main_classes/tokenizer#transformers.PreTrainedTokenizerBase.encode) and
            [`PreTrainedTokenizer.__call__`](https://huggingface.co/docs/transformers/main_classes/tokenizer#transformers.PreTrainedTokenizerBase.__call__) for details.
            [What are input IDs?](https://huggingface.co/docs/transformers/glossary#input-ids)
        attention_mask (`Union[torch.Tensor, np.ndarray, None]` of shape `({0})`, defaults to `None`):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.
            [What are attention masks?](https://huggingface.co/docs/transformers/glossary#attention-mask)
        token_type_ids (`Union[torch.Tensor, np.ndarray, None]` of shape `({0})`, defaults to `None`):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0, 1]`:
            - 1 for tokens that are **sentence A**,
            - 0 for tokens that are **sentence B**.
            [What are token type IDs?](https://huggingface.co/docs/transformers/glossary#token-type-ids)
aj  
    Args:
        pixel_values (`Union[torch.Tensor, np.ndarray, None]` of shape `({0})`, defaults to `None`):
            Pixel values corresponding to the images in the current batch.
            Pixel values can be obtained from encoded images using [`AutoFeatureExtractor`](https://huggingface.co/docs/transformers/autoclass_tutorial#autofeatureextractor).
a;  
    Args:
        input_values (`torch.Tensor` of shape `({0})`):
            Float values of input raw speech waveform..
            Input values can be obtained from audio file loaded into an array using [`AutoFeatureExtractor`](https://huggingface.co/docs/transformers/autoclass_tutorial#autofeatureextractor).
c                   @  sL   e Zd ZU dZdZded< dZded< dZded< dZded< dd
dZ	dS )!ZeroShotImageClassificationOutputaM  logits_per_image (`torch.FloatTensor` of shape `(image_batch_size, text_batch_size)`):
        The scaled dot product scores between `image_embeds` and `text_embeds`. This represents the image-text
        similarity scores.
    logits_per_text (`torch.FloatTensor` of shape `(text_batch_size, image_batch_size)`):
        The scaled dot product scores between `text_embeds` and `image_embeds`. This represents the text-image
        similarity scores.
    text_embeds (`torch.FloatTensor` of shape `(batch_size, output_dim`):
        The text embeddings obtained by applying the projection layer to the pooled output of [`MetaClip2TextModel`].
    image_embeds (`torch.FloatTensor` of shape `(batch_size, output_dim`):
        The image embeddings obtained by applying the projection layer to the pooled output of [`MetaClip2VisionModel`].
    Nztorch.FloatTensorlogits_per_imagelogits_per_texttext_embedsimage_embedsreturn
tuple[Any]c                   s   t  fdd  D S )Nc                 3  s    | ]} | V  qd S N ).0kselfrE   j/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/optimum/onnxruntime/modeling.py	<genexpr>   s    z=ZeroShotImageClassificationOutput.to_tuple.<locals>.<genexpr>)tuplekeysrH   rE   rH   rJ   to_tuple   s   z*ZeroShotImageClassificationOutput.to_tuple)rB   rC   )
__name__
__module____qualname____doc__r>   __annotations__r?   r@   rA   rN   rE   rE   rE   rJ   r=      s   
 r=   c                      s  e Zd ZU dZdZeZdZded< ddddddK fddZ	dLddZ
edddddefdMd%d&Ze	dNdOd,d-Zedddddeddd.ddddejdfdPd;d<ZedddddedfdQd=d>Zeeedddddddedd.ddddfdR fdBdCZdSdDdEZdTdIdJZ  ZS )UORTModela  Base class for implementing models using ONNX Runtime.

    The ORTModel implements generic methods for interacting with the Hugging Face Hub as well as exporting vanilla
    transformers models to ONNX using `optimum.exporters.onnx` toolchain.

    Class attributes:
        - model_type (`str`, *optional*, defaults to `"onnx_model"`) -- The name of the model type to use when
        registering the ORTModel classes.
        - auto_model_class (`Type`, *optional*, defaults to `AutoModel`) -- The "AutoModel" class to represented by the
        current ORTModel class.

    Args:
        - config ([`~transformers.PretrainedConfig`] -- The configuration of the model.
        - session (`~onnxruntime.InferenceSession`) -- The ONNX Runtime InferenceSession that is running the model.
        - use_io_binding (`bool`, *optional*, defaults to `True`) -- Whether to use I/O bindings with **ONNX Runtime
        with the CUDAExecutionProvider**, this can significantly speedup inference depending on the task.
        - model_save_dir (`Path`) -- The directory where the model exported to ONNX is saved.
        By defaults, if the loaded model is local, the directory where the original model will be used. Otherwise, the
        cache directory is used.
    
onnx_modelN
str | None_library_nameconfigsessionuse_io_bindingmodel_save_dirrY   r9   rZ   r-   r[   bool | Noner\   &str | Path | TemporaryDirectory | Nonec                  s   t  j||d | j||d d | _|d u rt|jj| _nt|t	r-|| _t|j
| _nt|tr8t|| _n|| _t| jt t| jdrR| jt| j d S d S )N)modelrY   )rZ   r[   register)super__init__initialize_ort_attributes&_model_save_dir_tempdirectory_instancer   _model_pathparentr\   
isinstancer   namestrr
   r`   
model_typehasattrauto_model_class	__class__)rI   rY   rZ   r[   r\   rm   rE   rJ   rb      s   

zORTModel.__init__save_directory
str | Pathc                 C  s   |  | dS )a  Saves the underlying ONNX model and its external data files (if any) to the specified directory.
        This method is called by the `save_pretrained` method of the `OptimizedModel` class.
        The model is copied from `self.session._model_path` to `save_directory`.

        Args:
            save_directory (`Union[str, Path]`):
                Directory where to save the model file.
        N)save_session)rI   ro   rE   rE   rJ   _save_pretrained   s   	zORTModel._save_pretrained mainFpath_or_repo_idfilenameri   	subfolderrevisionforce_downloadboollocal_files_onlytokenbool | str | None	cache_dirrB   r   c           	      C  sl   t | |||||||d}tt t | |d ||||||d W d   t|S 1 s-w   Y  t|S )zMReturns the path to the cached file and downloads external data if necessary.)rv   rw   rx   r~   ry   r{   r|   _dataN)r*   
contextlibsuppressOSErrorr   )	ru   rv   rw   rx   ry   r{   r|   r~   cached_pathrE   rE   rJ   _cached_file   s2   

zORTModel._cached_filepattern
onnx_files
list[Path]standard_file_nametarget_file_namec                   sV  dur;fdd|D }t |dkrtd d| dt |dkr(|d S td	 d
|d j d |d S fdd|D }t |dkrWtd d| d nt |dkra|d S td d
|d j d |d S  fdd|D }t |dkrtd| d  dt |dkr|d S td  d
|d j d |d S )a  Infer the ONNX file path to load among a list of candidates.
        The inference is done in three steps:
        1. We look for the `target_file_name` if provided.
        2. Otherwise, we look for the `standard_file_name`.
        3. Finally, we look for files matching the `pattern`.
        In case of multiple matches, the first one is returned and a warning is logged.

        Args:
            pattern (`str`):
                The regex pattern that the target ONNX file should match.
            onnx_files (`List[Path]`):
                List of candidate ONNX files.
            standard_file_name (`str`):
                The standard file name to look for.
            target_file_name (`str`, *optional*):
                The target file name to look for first.

        Returns:
            `Path`: The inferred ONNX file path.

        Raises:
            `FileNotFoundError`: If no ONNX file is found with the target file name when provided.
        Nc                      g | ]	}|j  kr|qS rE   rh   rF   file)r   rE   rJ   
<listcomp>"      z-ORTModel._infer_file_path.<locals>.<listcomp>r   z4Could not find any ONNX files with target file name z, only found: z|. Please make sure that the `file_name` and/or `subfolder` arguments correspond to an ONNX file in the repository/directory.   z0Found multiple ONNX files with target file name z, using z. Please make sure to pass a `subfolder` argument to `from_pretrained` when loading from a repository/directory containing multiple ONNX files with the same target file name.c                   r   rE   r   r   )r   rE   rJ   r   3  r   z6Could not find any ONNX files with standard file name z, files found: z. Please make sure to pass a `file_name` and/or `subfolder` argument to `from_pretrained` when loading an ONNX file with non-standard file names.z2Found multiple ONNX files with standard file name z. Please make sure to pass a `subfolder` argument to `from_pretrained` when loading from a repository/directory containing multiple standard-named ONNX files in different subfolders.c                   s    g | ]}t  t|r|qS rE   )researchri   )rF   p)r   rE   rJ   r   E       z=Could not find any ONNX model file in the list of candidates z matching the pattern z. Please make sure to pass a `file_name` and/or `subfolder` argument to `from_pretrained` when loading from a repository/directory containing non-pattern-matching ONNX files.z/Found multiple ONNX files matching the pattern z. Please make sure to pass a `file_name` and/or `subfolder` argument to `from_pretrained` when loading from a repository/directory containing multiple pattern-matching ONNX files.)lenFileNotFoundErrorloggerwarningrh   )r   r   r   r   target_filesstandard_filespattern_filesrE   )r   r   r   rJ   _infer_file_path  sD   zORTModel._infer_file_pathCPUExecutionProvidermodel_idtrust_remote_code	file_nameprovider	providersSequence[str] | Noneprovider_options0Sequence[dict[str, Any]] | dict[str, Any] | Nonesession_optionsSessionOptions | Nonedtypetorch.dtypec              
     s   t  td|||	d}t|dkrtd  t  r& fdd|D }| jt||
td}| j |j	|j
 |||||	d}|d u rG|j
}t|||d	\}}t||||d
}| ||||dS )N	**/*.onnx)glob_patternrw   rx   r|   r   z&Could not find any ONNX model file in c                   s   g | ]}|  qS rE   )relative_to)rF   fr   rE   rJ   r   {  s    z-ORTModel._from_pretrained.<locals>.<listcomp>)r   r   r   )rv   rw   r{   ry   r~   rx   r|   )r   r   r   )r   r   sess_optionsrX   )r7   r4   r   r   r   is_dirr   r5   r   rh   rf   as_posixr6   r-   )clsr   rY   rw   rx   ry   r{   r   r~   r|   r   r   r   r   r   r[   r   r\   r   	file_path
model_pathrZ   rE   r   rJ   _from_pretrainedV  sZ   
zORTModel._from_pretrainedc
                 K  s   t j| jd}|
dd urtd| j d| dt }t|j}t	|||dd||||	|||| j
d t|||d	 | j||fd
|i|
S )N)model_classtaskz?The `task` argument is not needed when exporting a model with `z;`. The `task` is automatically inferred from the class as `z`.FT)model_name_or_pathoutputr   do_validationno_post_processrw   rx   r~   r|   r{   ry   r   library_name)src_subfolderr\   )r0   %_infer_task_from_model_or_model_classrl   get
ValueErrorrO   r   r   rh   r/   rW   r8   r   )r   r   rY   rw   rx   ry   r{   r   r~   r|   kwargsr   r\   model_save_pathrE   rE   rJ   _export  s4   

zORTModel._exportPretrainedConfig | Noneexportr,   c                   s  t |tr	| }t r|std d}|}zs|r^tj|s^|	dd}tj
|	d| }tj
tj
|d|p<d}t|}| }W d   n1 sQw   Y  tj
|d	|}t|td
||
|d}t|dk}||A r|rtd| d d}n	td| d W n ty } ztd| d|  W Y d}~nd}~ww |r|dd}|durtd| d t j|f||||
|	|||||||||d|S )a  provider (`str`, defaults to `"CPUExecutionProvider"`):
            ONNX Runtime provider to use for loading the model.
            See https://onnxruntime.ai/docs/execution-providers/ for possible providers.
        providers (`Optional[Sequence[str]]`, defaults to `None`):
            List of execution providers to use for loading the model.
            This argument takes precedence over the `provider` argument.
        provider_options (`Optional[Dict[str, Any]]`, defaults to `None`):
            Provider option dictionaries corresponding to the provider used. See available options
            for each provider: https://onnxruntime.ai/docs/api/c/group___global.html .
        session_options (`Optional[onnxruntime.SessionOptions]`, defaults to `None`),:
            ONNX Runtime session options to use for loading the model.
        use_io_binding (`Optional[bool]`, defaults to `None`):
            Whether to use IOBinding during inference to avoid memory copy between the host and device, or between numpy/torch tensors and ONNX Runtime ORTValue. Defaults to
            `True` if the execution provider is CUDAExecutionProvider. For [~onnxruntime.ORTModelForCausalLM], defaults to `True` on CPUExecutionProvider,
            in all other cases defaults to `False`.
        kwargs (`Dict[str, Any]`):
            Will be passed to the underlying model loading methods.

        > Parameters for decoder models (ORTModelForCausalLM, ORTModelForSeq2SeqLM, ORTModelForSeq2SeqLM, ORTModelForSpeechSeq2Seq, ORTModelForVision2Seq)

        use_cache (`Optional[bool]`, defaults to `True`):
            Whether or not past key/values cache should be used. Defaults to `True`.

        use_merged (`Optional[bool]`, defaults to `None`):
            whether or not to use a single ONNX that handles both the decoding without and with past key values reuse. This option defaults
            to `True` if loading from a local repository and a merged decoder is found. When exporting with `export=True`,
            defaults to `False`. This option should be set to `True` to minimize memory usage.

        Returns:
            `ORTModel`: The loaded ORTModel model.
        z-Offline mode: setting `local_files_only=True`T/z--zmodels--refsrt   N	snapshotsr   )r   r   rw   r|   rx   r   z
The model z was already converted to ONNX but got `export=True`, the model will be converted to ONNX once again. Don't forget to save the resulting model with `.save_pretrained()`zNo ONNX files were found for zx, setting `export=True` to convert the model to ONNX. Don't forget to save the resulting model with `.save_pretrained()`zXCould not infer whether the model was already converted or not to ONNX, keeping `export=z`.
r   z`file_name` was set to `z<` but will be ignored as the model will be converted to ONNX)rY   r   ry   r|   r~   rw   r{   r   rx   r   r   r   r   r[   )rg   r   r   r+   r   infoospathisdirreplacejoinopenreadr7   r4   r   r   	Exceptionpopra   from_pretrained)r   r   rY   r   rw   rx   ry   r{   r   r~   r|   r   r   r   r   r[   r   r   	object_idcached_model_dir	refs_filer   	_revisionr   	exceptionr   rn   rE   rJ   r     s   
8



	


zORTModel.from_pretrainedc                 C  s
   t | tS )zEReturns whether this model can generate sequences with `.generate()`.)rg   r   rH   rE   rE   rJ   can_generateR  s   
zORTModel.can_generater   dict[str, Any]Nonec                 C  s0   |rt | jj dd|  d dS dS )zzWarn about unhandled input arguments.

        Args:
            kwargs: Dictionary of unhandled input arguments.
        z
 received z, a  , but do not handle those arguments. Please use `ORTModelForCustomTasks` if your model takes/returns arbitrary or custom tensor inputs/outputs. Or open an issue/PR in optimum repository (https://github.com/huggingface/optimum) if this argument needs to be supported in this class.N)r   r   rm   rO   r   rM   )rI   r   rE   rE   rJ   _warn_on_unhandled_inputsV  s
   z"ORTModel._warn_on_unhandled_inputs)rY   r9   rZ   r-   r[   r]   r\   r^   )ro   rp   )ru   rp   rv   ri   rw   ri   rx   ri   ry   rz   r{   rz   r|   r}   r~   ri   rB   r   rD   )
r   ri   r   r   r   ri   r   rV   rB   r   )$r   rp   rY   r9   rw   ri   rx   ri   ry   rz   r{   rz   r   rz   r~   ri   r|   r}   r   rV   r   ri   r   r   r   r   r   r   r[   r]   r   r   r\   r^   rB   rT   )r   rp   rY   r9   rw   ri   rx   ri   ry   rz   r{   rz   r   rz   r~   ri   r|   r}   rB   rT   ) r   rp   rY   r   r   rz   rw   ri   rx   ri   ry   rz   r{   rz   r   rz   r~   ri   r|   r}   r   ri   r   r   r   r   r   r   r[   r]   rB   r,   )rB   rz   )r   r   rB   r   )rO   rP   rQ   rR   rj   r   rl   rW   rS   rb   rr   staticmethodr	   r   r   classmethodtorchfloat32r   r   r   r1   r   r   r   __classcell__rE   rE   rn   rJ   rT      s   
 
"SM/
|rT   a!  
    Example of feature extraction:

    ```python
    >>> from transformers import {processor_class}
    >>> from optimum.onnxruntime import {model_class}
    >>> import torch

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")

    >>> inputs = tokenizer("My name is Philipp and I live in Germany.", return_tensors="np")

    >>> outputs = model(**inputs)
    >>> last_hidden_state = outputs.last_hidden_state
    >>> list(last_hidden_state.shape)
    [1, 12, 384]
    ```

    Example using `transformers.pipeline`:

    ```python
    >>> from transformers import {processor_class}, pipeline
    >>> from optimum.onnxruntime import {model_class}

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")
    >>> onnx_extractor = pipeline("feature-extraction", model=model, tokenizer=tokenizer)

    >>> text = "My name is Philipp and I live in Germany."
    >>> pred = onnx_extractor(text)
    ```
c                   @  sf   e Zd ZU dZeZdZded< ee	
dej
ed dd 										dd	d
dddZdS )ORTModelForFeatureExtractionz'ONNX Model for feature-extraction task.transformersrV   rW   batch_size, sequence_lengthzoptimum/all-MiniLM-L6-v2processor_classr   
checkpointNTreturn_dict	input_ids torch.Tensor | np.ndarray | Noneattention_masktoken_type_idsposition_idspixel_valuesvisual_embedsvisual_attention_maskvisual_token_type_idsinput_featuresinput_valuesr   rz   c                K  s6  |  | |||||||||	|
g
}ttdd |}t|tj}| | |d u r:d| jv r:|r5t|nt	|}|||||||||	|
d
}| j
rz| |\}}| jjdkr_| j| j n| j  | j| j | j  |d |d }n| ||}| jd |}| ||}|d }|s|fS t|dS )Nc                 S  s   | d uS rD   rE   )xrE   rE   rJ   <lambda>  s    z6ORTModelForFeatureExtraction.forward.<locals>.<lambda>r   )
r   r   r   r   r   r   r   r   r   r   cpulast_hidden_state)r   )r   nextfilterrg   r   Tensorraise_on_numpy_input_io_bindinginput_names
zeros_likenpr[   _prepare_io_bindingdevicetyperZ   run_with_iobinding_io_bindingsynchronize_inputssynchronize_outputsview_prepare_onnx_inputsrun_prepare_onnx_outputsr   )rI   r   r   r   r   r   r   r   r   r   r   r   r   tensor_inputsfirst_tensor	use_torchmodel_inputsoutput_shapesoutput_buffersr   onnx_inputsonnx_outputsmodel_outputsrE   rE   rJ   forward  sV   




z$ORTModelForFeatureExtraction.forward)
NNNNNNNNNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rz   )rO   rP   rQ   rR   r   rl   rW   rS   r   ONNX_TEXT_INPUTS_DOCSTRINGformatFEATURE_EXTRACTION_EXAMPLE_TOKENIZER_FOR_DOCr  rE   rE   rE   rJ   r     s4   
 
r   a  
    Example of feature extraction:

    ```python
    >>> from transformers import {processor_class}
    >>> from optimum.onnxruntime import {model_class}
    >>> import torch

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")

    >>> inputs = tokenizer("The capital of France is [MASK].", return_tensors="np")

    >>> outputs = model(**inputs)
    >>> logits = outputs.logits
    >>> list(logits.shape)
    [1, 8, 28996]
    ```

    Example using `transformers.pipeline`:

    ```python
    >>> from transformers import {processor_class}, pipeline
    >>> from optimum.onnxruntime import {model_class}

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")
    >>> fill_masker = pipeline("fill-mask", model=model, tokenizer=tokenizer)

    >>> text = "The capital of France is [MASK]."
    >>> pred = fill_masker(text)
    ```
c                   @  X   e Zd ZU dZeZdZded< ee	
dej
ed dd 			dd	d
dddZdS )ORTModelForMaskedLMa  ONNX Model with a MaskedLMOutput for masked language modeling tasks. This class officially supports albert, bert, camembert, convbert, data2vec-text, deberta, deberta_v2, distilbert, electra, flaubert, ibert, mobilebert, roberta, roformer, squeezebert, xlm, xlm_roberta.r   rV   rW   r   z'optimum/bert-base-uncased-for-fill-maskr   NTr   r   r   r   r   r   rz   c                K  s   |  | t|tj}| | d| jv r%|d u r%|r t|nt|}|||d}| jr^| 	|\}}	| j
jdkrC| j| j n| j  | j| j | j  |	d |d }
n| ||}| jd |}| ||}|d }
|sz|
fS t|
dS Nr   r   r   r   r   logitsr  )r   rg   r   r   r   r   r   r   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r!   rI   r   r   r   r   r   r  r  r  r  r  r  r  r  rE   rE   rJ   r    s0   




zORTModelForMaskedLM.forwardNNNr   r   r   r   r   r   r   rz   )rO   rP   rQ   rR   r   rl   rW   rS   r   r  r  MASKED_LM_EXAMPLEr  r  rE   rE   rE   rJ   r    &   
 
r  a  
    Example of question answering:

    ```python
    >>> from transformers import {processor_class}
    >>> from optimum.onnxruntime import {model_class}
    >>> import torch

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")

    >>> question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"
    >>> inputs = tokenizer(question, text, return_tensors="np")
    >>> start_positions = torch.tensor([1])
    >>> end_positions = torch.tensor([3])

    >>> outputs = model(**inputs, start_positions=start_positions, end_positions=end_positions)
    >>> start_scores = outputs.start_logits
    >>> end_scores = outputs.end_logits
    ```
    Example using `transformers.pipeline`:

    ```python
    >>> from transformers import {processor_class}, pipeline
    >>> from optimum.onnxruntime import {model_class}

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")
    >>> onnx_qa = pipeline("question-answering", model=model, tokenizer=tokenizer)

    >>> question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"
    >>> pred = onnx_qa(question, text)
    ```
c                   @  r  )ORTModelForQuestionAnsweringaN  ONNX Model with a QuestionAnsweringModelOutput for extractive question-answering tasks like SQuAD. This class officially supports albert, bart, bert, camembert, convbert, data2vec-text, deberta, deberta_v2, distilbert, electra, flaubert, gptj, ibert, mbart, mobilebert, nystromformer, roberta, roformer, squeezebert, xlm, xlm_roberta.r   rV   rW   r   zoptimum/roberta-base-squad2r   NTr   r   r   r   r   r   rz   c                K  s  |  | t|tj}| | |d u r%d| jv r%|r t|nt|}|||d}| jrg| 	|\}}	| j
jdkrC| j| j n| j  | j| j | j  |	d |d }
|	d |d }n| ||}| jd |}| ||}|d }
|d }|s|
|fS t|
|dS )Nr   r  r   start_logits
end_logits)r%  r&  )r   rg   r   r   r   r   r   r   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r$   )rI   r   r   r   r   r   r  r  r  r  r%  r&  r  r  r  rE   rE   rJ   r  o  s4   



z$ORTModelForQuestionAnswering.forwardr   r!  )rO   rP   rQ   rR   r   rl   rW   rS   r   r  r  QUESTION_ANSWERING_EXAMPLEr  r  rE   rE   rE   rJ   r$  h  r#  r$  a  
    Example of single-label classification:

    ```python
    >>> from transformers import {processor_class}
    >>> from optimum.onnxruntime import {model_class}
    >>> import torch

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")

    >>> inputs = tokenizer("Hello, my dog is cute", return_tensors="np")

    >>> outputs = model(**inputs)
    >>> logits = outputs.logits
    >>> list(logits.shape)
    [1, 2]
    ```

    Example using `transformers.pipelines`:

    ```python
    >>> from transformers import {processor_class}, pipeline
    >>> from optimum.onnxruntime import {model_class}

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")
    >>> onnx_classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)

    >>> text = "Hello, my dog is cute"
    >>> pred = onnx_classifier(text)
    ```

    Example using zero-shot-classification `transformers.pipelines`:

    ```python
    >>> from transformers import {processor_class}, pipeline
    >>> from optimum.onnxruntime import {model_class}

    >>> tokenizer = {processor_class}.from_pretrained("optimum/distilbert-base-uncased-mnli")
    >>> model = {model_class}.from_pretrained("optimum/distilbert-base-uncased-mnli")
    >>> onnx_z0 = pipeline("zero-shot-classification", model=model, tokenizer=tokenizer)

    >>> sequence_to_classify = "Who are you voting for in 2020?"
    >>> candidate_labels = ["Europe", "public health", "politics", "elections"]
    >>> pred = onnx_z0(sequence_to_classify, candidate_labels, multi_label=True)
    ```
c                   @  r  )!ORTModelForSequenceClassificationaq  ONNX Model with a sequence classification/regression head on top (a linear layer on top of the
    pooled output) e.g. for GLUE tasks. This class officially supports albert, bart, bert, camembert, convbert, data2vec-text, deberta, deberta_v2, distilbert, electra, flaubert, ibert, mbart, mobilebert, nystromformer, roberta, roformer, squeezebert, xlm, xlm_roberta.
    r   rV   rW   r   z7optimum/distilbert-base-uncased-finetuned-sst-2-englishr   NTr   r   r   r   r   r   rz   c                K     |  | t|tj}| | |d u r%d| jv r%|r t|nt|}|||d}| jr^| 	|\}}	| j
jdkrC| j| j n| j  | j| j | j  |	d |d }
n| ||}| jd |}| ||}|d }
|sz|
fS t|
dS r  )r   rg   r   r   r   r   r   r   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r&   r  rE   rE   rJ   r    0   




z)ORTModelForSequenceClassification.forwardr   r!  )rO   rP   rQ   rR   r   rl   rW   rS   r   r  r  SEQUENCE_CLASSIFICATION_EXAMPLEr  r  rE   rE   rE   rJ   r(    &   
 
r(  a  
    Example of token classification:

    ```python
    >>> from transformers import {processor_class}
    >>> from optimum.onnxruntime import {model_class}
    >>> import torch

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")

    >>> inputs = tokenizer("My name is Philipp and I live in Germany.", return_tensors="np")

    >>> outputs = model(**inputs)
    >>> logits = outputs.logits
    >>> list(logits.shape)
    [1, 12, 9]
    ```

    Example using `transformers.pipelines`:

    ```python
    >>> from transformers import {processor_class}, pipeline
    >>> from optimum.onnxruntime import {model_class}

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")
    >>> onnx_ner = pipeline("token-classification", model=model, tokenizer=tokenizer)

    >>> text = "My name is Philipp and I live in Germany."
    >>> pred = onnx_ner(text)
    ```
c                   @  r  )ORTModelForTokenClassificationav  ONNX Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g.
    for Named-Entity-Recognition (NER) tasks. This class officially supports albert, bert, bloom, camembert, convbert, data2vec-text, deberta, deberta_v2, distilbert, electra, flaubert, gpt2, ibert, mobilebert, roberta, roformer, squeezebert, xlm, xlm_roberta.

    r   rV   rW   r   zoptimum/bert-base-NERr   NTr   r   r   r   r   r   rz   c                K  r)  r  )r   rg   r   r   r   r   r   r   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r'   r  rE   rE   rJ   r  J  s0   




z&ORTModelForTokenClassification.forwardr   r!  )rO   rP   rQ   rR   r   rl   rW   rS   r   r  r  TOKEN_CLASSIFICATION_EXAMPLEr  r  rE   rE   rE   rJ   r-  @  s&   
 
r-  a  
    Example of multiple choice:

    ```python
    >>> from transformers import {processor_class}
    >>> from optimum.onnxruntime import {model_class}

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}", export=True)

    >>> num_choices = 4
    >>> first_sentence = ["Members of the procession walk down the street holding small horn brass instruments."] * num_choices
    >>> second_sentence = [
    ...     "A drum line passes by walking down the street playing their instruments.",
    ...     "A drum line has heard approaching them.",
    ...     "A drum line arrives and they're outside dancing and asleep.",
    ...     "A drum line turns the lead singer watches the performance."
    ... ]
    >>> inputs = tokenizer(first_sentence, second_sentence, truncation=True, padding=True)

    # Unflatten the inputs values expanding it to the shape [batch_size, num_choices, seq_length]
    >>> for k, v in inputs.items():
    ...     inputs[k] = [v[i: i + num_choices] for i in range(0, len(v), num_choices)]
    >>> inputs = dict(inputs.convert_to_tensors(tensor_type="pt"))
    >>> outputs = model(**inputs)
    >>> logits = outputs.logits
    ```
c                   @  r  )ORTModelForMultipleChoiceap  ONNX Model with a multiple choice classification head on top (a linear layer on top of the pooled output and a
    softmax) e.g. for RocStories/SWAG tasks. This class officially supports albert, bert, camembert, convbert, data2vec-text, deberta_v2, distilbert, electra, flaubert, ibert, mobilebert, nystromformer, roberta, roformer, squeezebert, xlm, xlm_roberta.
    r   rV   rW   r   z!ehdwns1516/bert-base-uncased_SWAGr   NTr   r   r   r   r   r   rz   c                K  r)  r  )r   rg   r   r   r   r   r   r   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r#   r  rE   rE   rJ   r    r*  z!ORTModelForMultipleChoice.forwardr   r!  )rO   rP   rQ   rR   r   rl   rW   rS   r   r  r  MULTIPLE_CHOICE_EXAMPLEr  r  rE   rE   rE   rJ   r/    r,  r/  a  
    Example of image classification:

    ```python
    >>> import requests
    >>> from PIL import Image
    >>> from optimum.onnxruntime import {model_class}
    >>> from transformers import {processor_class}

    >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
    >>> image = Image.open(requests.get(url, stream=True).raw)

    >>> preprocessor = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")

    >>> inputs = preprocessor(images=image, return_tensors="np")

    >>> outputs = model(**inputs)
    >>> logits = outputs.logits
    ```

    Example using `transformers.pipeline`:

    ```python
    >>> import requests
    >>> from PIL import Image
    >>> from transformers import {processor_class}, pipeline
    >>> from optimum.onnxruntime import {model_class}

    >>> preprocessor = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")
    >>> onnx_image_classifier = pipeline("image-classification", model=model, feature_extractor=preprocessor)

    >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
    >>> pred = onnx_image_classifier(url)
    ```
c                   @  sB   e Zd ZdZeZeede	je
d dd dddddZdS )ORTModelForImageClassificationzONNX Model for image-classification tasks. This class officially supports beit, convnext, convnextv2, data2vec-vision, deit, dinov2, levit, mobilenet_v1, mobilenet_v2, mobilevit, poolformer, resnet, segformer, swin, swinv2, vit.'batch_size, num_channels, height, widthzoptimum/vit-base-patch16-224r   Tr   r   torch.Tensor | np.ndarrayr   rz   c                K     |  | t|tj}| | d|i}| jrG| |\}}| jjdkr,| j	
| j n| j  | j	
| j | j  |d |d }n| ||}	| j	d |	}
| ||
}|d }|sc|fS t|dS )Nr   r   r  r  )r   rg   r   r   r   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r   )rI   r   r   r   r  r  r  r  r  r  r  r  rE   rE   rJ   r    s(   




z&ORTModelForImageClassification.forwardNr   r3  r   rz   )rO   rP   rQ   rR   r   rl   r   ONNX_IMAGE_INPUTS_DOCSTRINGr  IMAGE_CLASSIFICATION_EXAMPLE_FEATURE_EXTRACTOR_FOR_DOCr  rE   rE   rE   rJ   r1  	  s    r1  c                   @  s:   e Zd ZdZeZeede	d 	ddd
dZ
dS )&ORTModelForZeroShotImageClassificationzeONNX Model for zero-shot-image-classification tasks. This class officially supports clip, metaclip-2.r   r2  Nr   r3  r   r   r   c                 K  sH  |  | t|tj}| | |d u r%d| jv r%|r t|nt|}|||d}| jry| 	|\}}| j
jdkrC| j| j n| j  | j| j | j  |d |d }	|d |d }
|d |d }|d |d }n#| ||}| jd |}| ||}|d }	|d }
|d }|d }t|	|
||dS )	Nr   )r   r   r   r   r?   r>   r@   rA   )r?   r>   r@   rA   )r   rg   r   r   r   r   	ones_liker   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r=   )rI   r   r   r   r   r  r  r  r  r?   r>   r@   rA   r  r  r  rE   rE   rJ   r  G  sB   



z.ORTModelForZeroShotImageClassification.forwardrD   )r   r3  r   r3  r   r   )rO   rP   rQ   rR   r   rl   r   r  r  r6  r  rE   rE   rE   rJ   r9  A  s    r9  a  
    Example of semantic segmentation:

    ```python
    >>> import requests
    >>> from PIL import Image
    >>> from optimum.onnxruntime import {model_class}
    >>> from transformers import {processor_class}

    >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
    >>> image = Image.open(requests.get(url, stream=True).raw)

    >>> preprocessor = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")

    >>> inputs = preprocessor(images=image, return_tensors="np")

    >>> outputs = model(**inputs)
    >>> logits = outputs.logits
    ```

    Example using `transformers.pipeline`:

    ```python
    >>> import requests
    >>> from PIL import Image
    >>> from transformers import {processor_class}, pipeline
    >>> from optimum.onnxruntime import {model_class}

    >>> preprocessor = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")
    >>> onnx_image_segmenter = pipeline("image-segmentation", model=model, feature_extractor=preprocessor)

    >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
    >>> pred = onnx_image_segmenter(url)
    ```
c                      sR   e Zd ZdZeZ fddZee	de
j	ed dd dddddZ  ZS )ORTModelForSemanticSegmentationzONNX Model for semantic-segmentation, with an all-MLP decode head on top e.g. for ADE20k, CityScapes. This class officially supports maskformer, segformer.c                   s"   t  j|i | | jjtd< d S )Nort_semantic_segmentation)ra   rb   rm   rO   r)   )rI   argsr   rn   rE   rJ   rb     s   z(ORTModelForSemanticSegmentation.__init__r2  z*optimum/segformer-b0-finetuned-ade-512-512r   Tr   r   r3  r   rz   c                K  s&  |  | t|tj}| | d|i}| jrV| |\}}| jjdkr,| j	
| j n| j  | j	
| j | j  |d |d }d }	d|v rU|d |d }	n!| ||}
| j	d |
}| ||}|d }d }	d|v rw|d }	|s|	d ur||	fS |fS |	d urt||	dS t|dS )Nr   r   r  
pred_masks)r  r>  r  )r   rg   r   r   r   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r"   r%   )rI   r   r   r   r  r  r  r  r  r>  r  r  r  rE   rE   rJ   r    s>   




z'ORTModelForSemanticSegmentation.forwardr5  )rO   rP   rQ   rR   r   rl   rb   r   r6  r  SEMANTIC_SEGMENTATION_EXAMPLEr8  r  r   rE   rE   rn   rJ   r;    s    	r;  a  
    Example of audio classification:

    ```python
    >>> from transformers import {processor_class}
    >>> from optimum.onnxruntime import {model_class}
    >>> from datasets import load_dataset
    >>> import torch

    >>> dataset = load_dataset("hf-internal-testing/librispeech_asr_demo", "clean", split="validation")
    >>> dataset = dataset.sort("id")
    >>> sampling_rate = dataset.features["audio"].sampling_rate

    >>> feature_extractor = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")

    >>> # audio file is decoded on the fly
    >>> inputs = feature_extractor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt")

    >>> with torch.no_grad():
    ...     logits = model(**inputs).logits

    >>> predicted_class_ids = torch.argmax(logits, dim=-1).item()
    >>> predicted_label = model.config.id2label[predicted_class_ids]
    ```
    Example using `transformers.pipeline`:

    ```python
    >>> from transformers import {processor_class}, pipeline
    >>> from optimum.onnxruntime import {model_class}

    >>> feature_extractor = {processor_class}.from_pretrained("{checkpoint}")
    >>> dataset = load_dataset("hf-internal-testing/librispeech_asr_demo", "clean", split="validation")
    >>> dataset = dataset.sort("id")

    >>> model = {model_class}.from_pretrained("{checkpoint}")
    >>> onnx_ac = pipeline("audio-classification", model=model, feature_extractor=feature_extractor)

    >>> pred = onnx_ac(dataset[0]["audio"]["array"])
    ```
c                   @  r  )ORTModelForAudioClassificationaI  ONNX Model for audio-classification, with a sequence classification head on top (a linear layer over the pooled output) for tasks like
    SUPERB Keyword Spotting. This class officially supports audio_spectrogram_transformer, data2vec-audio, hubert, sew, sew-d, unispeech, unispeech_sat, wavlm, wav2vec2, wav2vec2-conformer.
    r   rV   rW   r   zoptimum/hubert-base-superb-ksr   NTr   r   r   r   r   r   rz   c                K  s  |  | | jjdkr|d u rtdd}|}n|d u r&td| jj dd}|}t|tj}| | ||d|i}	| jrn| 	|	\}
}| j
jdkrS| j| j n| j  | j| j | j  |d	 |
d	 }n| ||	}| jd |}| ||}|d	 }|s|fS t|d
S )Nwhisperz8input_features must be provided for this model (whisper)r   z.input_values must be provided for this model ()r   r   r   r  r  )r   rY   rj   r   rg   r   r   r   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r&   )rI   r   r   r   r   r   
input_namemodel_inputr  r  r  r  r  r  r  r  rE   rE   rJ   r  (  s<   




z&ORTModelForAudioClassification.forwardr   )r   r   r   r   r   r   r   rz   )rO   rP   rQ   rR   r   rl   rW   rS   r   ONNX_AUDIO_INPUTS_DOCSTRINGr  AUDIO_CLASSIFICATION_EXAMPLEr8  r  rE   rE   rE   rJ   r@    r,  r@  a  
    Example of CTC:

    ```python
    >>> from transformers import {processor_class}, HubertForCTC
    >>> from optimum.onnxruntime import {model_class}
    >>> from datasets import load_dataset
    >>> import torch

    >>> dataset = load_dataset("hf-internal-testing/librispeech_asr_demo", "clean", split="validation")
    >>> dataset = dataset.sort("id")
    >>> sampling_rate = dataset.features["audio"].sampling_rate

    >>> processor = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")

    >>> # audio file is decoded on the fly
    >>> inputs = processor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt")
    >>> with torch.no_grad():
    ...     logits = model(**inputs).logits
    >>> predicted_ids = torch.argmax(logits, dim=-1)

    >>> transcription = processor.batch_decode(predicted_ids)
    ```
c                   @  sV   e Zd ZU dZeZdZded< ee	
dej
ed dd 		dd	d
dddZdS )ORTModelForCTCzONNX Model with a language modeling head on top for Connectionist Temporal Classification (CTC). This class officially supports data2vec-audio, hubert, sew, sew-d, unispeech, unispeech_sat, wavlm, wav2vec2, wav2vec2-conformer.r   rV   rW   r   zoptimum/hubert-large-ls960-ftr   NTr   r   r   r   r   rz   c                K  s  | j jdkr|d usJ dd}|}n|d usJ dd}|}| | t|tj}| | ||i}| jrf| |\}	}
| j	j
dkrK| j| j n| j  | j| j | j  |
d |	d }n| ||}| jd |}| ||}|d }|s|fS t|dS )	Nmctctz.input_features must be provided for this modelr   z,input_values must be provided for this modelr   r   r  r  )rY   rj   r   rg   r   r   r   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r   )rI   r   r   r   r   rC  rD  r  r  r  r  r  r  r  r  rE   rE   rJ   r    s6   




zORTModelForCTC.forward)NN)r   r   r   r   r   rz   )rO   rP   rQ   rR   r   rl   rW   rS   r   rE  r  CTC_EXAMPLE_PROCESSOR_FOR_DOCr  rE   rE   rE   rJ   rG    s$   
 
rG  a  
    Example of Audio XVector:

    ```python
    >>> from transformers import {processor_class}
    >>> from optimum.onnxruntime import {model_class}
    >>> from datasets import load_dataset
    >>> import torch

    >>> dataset = load_dataset("hf-internal-testing/librispeech_asr_demo", "clean", split="validation")
    >>> dataset = dataset.sort("id")
    >>> sampling_rate = dataset.features["audio"].sampling_rate

    >>> feature_extractor = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")

    >>> # audio file is decoded on the fly
    >>> inputs = feature_extractor(
    ...     [d["array"] for d in dataset[:2]["audio"]], sampling_rate=sampling_rate, return_tensors="pt", padding=True
    ... )
    >>> with torch.no_grad():
    ...     embeddings = model(**inputs).embeddings

    >>> embeddings = torch.nn.functional.normalize(embeddings, dim=-1).cpu()

    >>> cosine_sim = torch.nn.CosineSimilarity(dim=-1)
    >>> similarity = cosine_sim(embeddings[0], embeddings[1])
    >>> threshold = 0.7
    >>> if similarity < threshold:
    ...     print("Speakers are not the same!")
    >>> round(similarity.item(), 2)
    ```
c                   @  T   e Zd ZU dZeZdZded< ee	
dej
ed dd 	dd	d
dddZdS )ORTModelForAudioXVectorzONNX Model with an XVector feature extraction head on top for tasks like Speaker Verification. This class officially supports data2vec-audio, unispeech_sat, wavlm, wav2vec2, wav2vec2-conformer.r   rV   rW   r   zoptimum/wav2vec2-base-superb-svr   NTr   r   r   r   rz   c                K  s   |  | t|tj}| | d|i}| jrP| |\}}| jjdkr,| j	
| j n| j  | j	
| j | j  |d |d }|d |d }	n| ||}
| j	d |
}| ||}|d }|d }	|sq||	fS t||	dS )Nr   r   r  
embeddings)r  rM  )r   rg   r   r   r   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r(   )rI   r   r   r   r  r  r  r  r  rM  r  r  r  rE   rE   rJ   r    s,   



zORTModelForAudioXVector.forwardrD   r   r   r   rz   )rO   rP   rQ   rR   r   rl   rW   rS   r   rE  r  AUDIO_XVECTOR_EXAMPLEr8  r  rE   rE   rE   rJ   rL    "   
 
rL  a  
    Example of audio frame classification:

    ```python
    >>> from transformers import {processor_class}
    >>> from optimum.onnxruntime import {model_class}
    >>> from datasets import load_dataset
    >>> import torch

    >>> dataset = load_dataset("hf-internal-testing/librispeech_asr_demo", "clean", split="validation")
    >>> dataset = dataset.sort("id")
    >>> sampling_rate = dataset.features["audio"].sampling_rate

    >>> feature_extractor = {processor_class}.from_pretrained("{checkpoint}")
    >>> model =  {model_class}.from_pretrained("{checkpoint}")

    >>> inputs = feature_extractor(dataset[0]["audio"]["array"], return_tensors="pt", sampling_rate=sampling_rate)
    >>> with torch.no_grad():
    ...     logits = model(**inputs).logits

    >>> probabilities = torch.sigmoid(logits[0])
    >>> labels = (probabilities > 0.5).long()
    >>> labels[0].tolist()
    ```
c                   @  rK  )#ORTModelForAudioFrameClassificationzONNX Model with a frame classification head on top for tasks like Speaker Diarization. This class officially supports data2vec-audio, unispeech_sat, wavlm, wav2vec2, wav2vec2-conformer.r   rV   rW   r   zoptimum/wav2vec2-base-superb-sdr   NTr   r   r   r   rz   c                K  r4  )Nr   r   r  r  )r   rg   r   r   r   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r'   )rI   r   r   r   r  r  r  r  r  r  r  r  rE   rE   rJ   r  G  s(   




z+ORTModelForAudioFrameClassification.forwardrD   rN  )rO   rP   rQ   rR   r   rl   rW   rS   r   rE  r  "AUDIO_FRAME_CLASSIFICATION_EXAMPLEr8  r  rE   rE   rE   rJ   rQ  @  rP  rQ  a  
    Example of image-to-image (Super Resolution):

    ```python
    >>> from transformers import {processor_class}
    >>> from optimum.onnxruntime import {model_class}
    >>> from PIL import Image

    >>> image = Image.open("path/to/image.jpg")

    >>> image_processor = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")

    >>> inputs = image_processor(images=image, return_tensors="pt")

    >>> with torch.no_grad():
    ...     logits = model(**inputs).logits
    ```
c                   @  sP   e Zd ZU dZeZdZded< ee	
dej
ed dd dd	dddZdS )ORTModelForImageToImageztONNX Model for image-to-image tasks. This class officially supports pix2pix, cyclegan, wav2vec2, wav2vec2-conformer.r   rV   rW   r2  z-caidas/swin2SR-realworld-sr-x4-64-bsrgan-psnrr   Tr   r   r3  r   rz   c                K  r4  )Nr   r   reconstruction)rT  )r   rg   r   r   r   r[   r   r  r  rZ   r  r  r  r  r  r  r	  r
  r    )rI   r   r   r   r  r  r  r  rT  r  r  r  rE   rE   rJ   r    s(   




zORTModelForImageToImage.forwardNr5  )rO   rP   rQ   rR   r   rl   rW   rS   r   r6  r  IMAGE_TO_IMAGE_EXAMPLErJ  r  rE   rE   rE   rJ   rS    s   
 rS  a*  
    Example of custom tasks(e.g. a sentence transformers taking `pooler_output` as output):

    ```python
    >>> from transformers import {processor_class}
    >>> from optimum.onnxruntime import {model_class}

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")

    >>> inputs = tokenizer("I love burritos!", return_tensors="np")

    >>> outputs = model(**inputs)
    >>> last_hidden_state = outputs.last_hidden_state
    >>> pooler_output = outputs.pooler_output
    ```

    Example using `transformers.pipelines`(only if the task is supported):

    ```python
    >>> from transformers import {processor_class}, pipeline
    >>> from optimum.onnxruntime import {model_class}

    >>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
    >>> model = {model_class}.from_pretrained("{checkpoint}")
    >>> onnx_extractor = pipeline("feature-extraction", model=model, tokenizer=tokenizer)

    >>> text = "I love burritos!"
    >>> pred = onnx_extractor(text)
    ```
c                   @  s.   e Zd ZdZeejed ddd	ddZdS )
ORTModelForCustomTaskszONNX Model for any custom tasks. It can be used to leverage the inference acceleration for any single-file ONNX model, that may use custom inputs and outputs.z'optimum/sbert-all-MiniLM-L6-with-poolerr   r  r3  c                   s   t tt| tj}| | | jrF| |\} | j	j
dkr)| j| j n| j  | j| j | j   fdd| D }n| ||}| jd |}| ||}tdi |S )Nr   c                   s    i | ]\}}| |  |qS rE   )r  )rF   rh   shaper  rE   rJ   
<dictcomp>  r   z2ORTModelForCustomTasks.forward.<locals>.<dictcomp>rE   )rg   r   itervaluesr   r   r   r[   r   r  r  rZ   r  r  r  r  itemsr  r	  r
  r"   )rI   r  r  r  r  r  r  rE   rX  rJ   r    s   


zORTModelForCustomTasks.forwardN)r  r3  )	rO   rP   rQ   rR   r   CUSTOM_TASKS_EXAMPLEr  r  r  rE   rE   rE   rJ   rV    s    rV  )rR   
__future__r   r   loggingr   r   collections.abcr   dataclassesr   pathlibr   tempfiler   typingr   r   numpyr   r   huggingface_hub.constantsr	   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   transformers.file_utilsr   r   r   transformers.modeling_outputsr   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   &transformers.models.auto.modeling_autor)   transformers.utilsr*   r+   typing_extensionsr,   onnxruntimer-   r.   optimum.exporters.onnxr/   optimum.exporters.tasksr0   optimum.modeling_baser1   r2   optimum.onnxruntime.baser3   optimum.onnxruntime.constantsr4   r5   optimum.onnxruntime.utilsr6   optimum.utils.file_utilsr7   optimum.utils.save_utilsr8   r9   	getLoggerrO   r   r  r8  rJ  ONNX_MODEL_END_DOCSTRINGr  r6  rE  r=   rT   r  r   r"  r  r'  r$  r+  r(  r.  r-  r0  r/  r7  r1  r9  r?  r;  rF  r@  rI  rG  rO  rL  rR  rQ  rU  rS  r]  rV  rE   rE   rE   rJ   <module>   s   H8
   K#Z#>$@2A#AA'7?'K+JA#:76!