o
    diD                     @   s  d 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	m
Z
mZmZmZmZmZmZm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 ddlmZ ddl Z!ddl"m#Z# ddl$m%Z%m&Z& e	r|ddl'm(Z( ddl)m*Z* e+e,Z-dZ.dZ/dZ0dZ1dZ2dd Z3dd Z4G dd dZ5dd Z6de%d e%fd!d"Z7d#e8d$ed ej9fd%d&Z:d'ej9d e8fd(d)Z;d'eej9e8e<f d eej9ef fd*d+Z=d#e8fd,d-Z>	.		dJd#e8d/eee8  d$eeeee8e
f  ee8e
f f  fd0d1Z?dKd/ee8 d2ee@ d e@fd3d4ZAd5ee8 d6ed ee8 fd7d8ZBG d9d: d:eZC		dLd;d<d=d>d?eee8  d@eeegef  fdAdBZDdCdD ZEG dEdF dFZFdGe!jGd ejHfdHdIZIdS )Mz:Utility functions, classes and constants for ONNX Runtime.    N)Enum)	signature)	TYPE_CHECKINGAnyCallableDictListOptionalSequenceTupleUnion)version)tqdm)EvalPrediction)nested_concat)EvalLoopOutput)logging)
TypeHelper   )
OnnxConfigOnnxConfigWithLoss)Dataset   )ORTModelz
model.onnxzencoder_model.onnxzdecoder_model.onnxzdecoder_with_past_model.onnxzdecoder_model_merged.onnxc                  C   s(   t jtjd d} t j| rdS dS )z6
    Checks if onnxruntime-training is available.
    r   trainingTF)ospathjoinort__path__exists)path_training_dependecy r"   g/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/optimum/onnxruntime/utils.py!is_onnxruntime_training_available6   s   r$   c                   C   s   t jdduS )z&
    Checks if CuPy is available.
    cupyN)	importlibutil	find_specr"   r"   r"   r#   is_cupy_availableA   s   r)   c                   @   s,  e Zd ZdZi dddddddddddddd	d
ddd	ddddddddddd	d	dd	dd	i dd	dd	dd	dddd	dddddd	ddddddddd dd!dd"dd#dd$dddddd%Zed&ed'efd(d)Zed&efd*d+Zed&efd,d-Z	d.S )/ORTConfigManagera  
    A class that contains all the information needed by ONNX Runtime optimization for a given model type.

    Attributes:
        _conf (`Dict[str]`):
            A dictionary mapping each supported model type to the corresponding ONNX Runtime model type.
    albertbertbartbig_birdbigbird_pegasus
blenderbotbloomgpt2	camembertcodegendebertaz
deberta-v2dinov2vit
distilbertelectragpt_bigcodegpt_neogpt_neoxgptjgranitelongt5llamamarianmbartmistral
modernbertmpnetmt5m2m_100nystromformerpegasusroberta	segformert5)r7   whisperzxlm-roberta
pix2struct
model_typereturnc                 C   s   |  | | j| S N)check_supported_model_conf)clsrO   r"   r"   r#   get_model_ort_type~   s   

z#ORTConfigManager.get_model_ort_typec                 C   s8   || j vrd| j  }t| d| d| dd S )Nz, z' model type is not supported yet. Only ' are supported. If you want to support z) please propose a PR or open up an issue.)rS   r   keysKeyError)rT   rO   model_typesr"   r"   r#   rR      s   
z&ORTConfigManager.check_supported_modelc                 C   sJ   g d}|| j vs| j | |vr#td| dt| j   d| dd S )N)r-   r,   r2   tnlrrL   unetvaeclipr7   swinswinv2z7ONNX Runtime doesn't support the graph optimization of z yet. Only rV   zc please propose a PR or open up an issue in ONNX Runtime: https://github.com/microsoft/onnxruntime.)rS   NotImplementedErrorlistrW   )rT   rO   optimization_config&supported_model_types_for_optimizationr"   r"   r#   "check_optimization_supported_model   s   z3ORTConfigManager.check_optimization_supported_modelN)
__name__
__module____qualname____doc__rS   classmethodstrrU   rR   rd   r"   r"   r"   r#   r*   J   s    
	
 !"#)r*   c                 C   s   | j | j| | jS rQ   )parentjoinpathstemwith_suffixsuffix)filename
identifierr"   r"   r#   generate_identified_filename   s   rr   onnx_configrP   c                 C   s   t | S rQ   )r   )rs   r"   r"   r#   wrap_onnx_config_for_loss   s   rt   providerprovider_optionsc                 C   s*   | dv rt d|dd S t dS )zV
    Gets the PyTorch device (CPU/CUDA) associated with an ONNX Runtime provider.
    )CUDAExecutionProviderTensorrtExecutionProviderROCMExecutionProviderzcuda:	device_idr   cpu)torchdeviceget)ru   rv   r"   r"   r#   get_device_for_provider   s   
r   r}   c                 C   s&   | j  dkrdt v rdS dS dS )zW
    Gets the ONNX Runtime provider associated with the PyTorch device (CPU/CUDA).
    cudary   rw   CPUExecutionProvider)typelowerr   get_available_providersr}   r"   r"   r#   get_provider_for_device   s
   r   c                 C   sZ   | dkr
t d} n	t jj| d } i }| jdkr)| jdu r$t d} | j|d< | |fS )zuGets the relevant torch.device from the passed device, and if relevant the provider options (e.g. to set the GPU id).r{   r   r   Nzcuda:0rz   )r|   r}   _C_nn	_parse_tor   index)r}   rv   r"   r"   r#   parse_device   s   



r   c                 C   s  t tjt dk rtjdkr| dv rtjtjd dd}tjtjd dd}tjtjd dd}t	|d	k}|
 }d
|vsGd|vr|tj|r[tj|r[td|  dtj|rtt rt| dkrstd|  d|  dntd|  d| dkrtjdddkrtd| dkrtjdddkrtdW d   n1 sw   Y  t }| |vrtd|  d| ddS )z
    Ensure the ONNX Runtime execution provider `provider` is available, and raise an error if it is not.

    Args:
        provider (str): Name of an ONNX Runtime execution provider.
    z1.16.0nt)rw   rx   r   capiz libonnxruntime_providers_cuda.soz$libonnxruntime_providers_tensorrt.soz_ld_preload.pyrORT_CUDAORT_TENSORRTz`onnxruntime-gpu` is installed, but GPU dependencies are not loaded. It is likely there is a conflicting install between `onnxruntime` and `onnxruntime-gpu`. Please install only `onnxruntime-gpu` in order to use .rx   zAsked to use z5, but `onnxruntime-training` package doesn't support z-. Please use `CUDAExecutionProvider` instead.zw, but `onnxruntime-gpu` package was not found. Make sure to install `onnxruntime-gpu` package instead of `onnxruntime`.rw   ORT_CUDA_UNAVAILABLE01z`onnxruntime-gpu` package is installed, but CUDA requirements could not be loaded. Make sure to meet the required dependencies: https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.htmlORT_TENSORRT_UNAVAILABLEa4  `onnxruntime-gpu` package is installed, but TensorRT requirements could not be loaded. Make sure to meet the required dependencies following https://onnxruntime.ai/docs/execution-providers/TensorRT-ExecutionProvider.html and https://hf.co/docs/optimum/onnxruntime/usage_guides/gpu#tensorrtexecutionprovider .NzR as an ONNX Runtime execution provider, but the available execution providers are )r   parser   __version__r   namer   r   r   openreadisfileImportErrorr$   environr~   r   
ValueError)ru   path_cuda_libpath_trt_libpath_dependecy_loadingffile_stringavailable_providersr"   r"   r#   validate_provider_availability   sP   



r   r   	providersc                 C   s   |du r| g}|D ]} t |  q	|du ri gt| }||fS t|tr4|gi gt|d   }||fS t|t|krKtdt| dt| d||fS )a  
    Prepare the providers and provider options for ONNX Runtime.
    Args:
        provider (`str`):
            The provider to use. If `None`, the default provider will be used.
        providers (`Sequence[str]`, `optional`):
            The list of providers to use. If `None`, the default provider will be used.
        provider_options (`Union[Sequence[Dict[str, Any]], Dict[str, Any]]`, `optional`):
            The options to use for the providers. If `None`, the default options will be used.
    Nr   zdWhen passing a list of provider options, it should be the same length as the list of providers. Got z provider options for z providers.)r   len
isinstancedictr   )ru   r   rv   r"   r"   r#   &prepare_providers_and_provider_options	  s&   
	
r   use_io_bindingc                 C   sN   |du r| d dkrd}|S | d dkr%| d dkr%|du r#t d d}|S )z*
    Whether to use IOBinding or not.
    Nr   rw   Tr   zNo need to enable IO Binding if the provider used is neither CPUExecutionProvider nor CUDAExecutionProvider. IO Binding will be turned off.F)loggerwarning)r   r   r"   r"   r#   check_io_binding+  s   r   input_namesfuncc                 C   sL   t |}g }|jD ]}t| d}| D ]}t||r"|| qq	|S )a  
    Returns the input names from input_names keys ordered according to the signature of func. This is especially useful with the
    forward function when using IO Binding, as the input order of the ONNX and forward may be different.

    Method inspired from OnnxConfig.ordered_inputs.

    Args:
        input_names (`List[str]`):
            Names of the inputs of the ONNX model.
        func (`Callable`):
            Callable to remap the input_names order to.

    z(\.\d*)?)r   
parametersrecompilesearchappend)r   r   signature_func_ordered_input_namesparamparam_regexr   r"   r"   r#   get_ordered_input_names;  s   

r   c                   @   s`   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdS )ORTQuantizableOperatorGather	TransposeEmbedLayerNormalizationConvMatMulAddMulReluClip	LeakyReluSigmoidMaxPoolGlobalAveragePoolSplitPadReshapeSqueeze	UnsqueezeResizeAveragePoolConcatN)re   rf   rg   r   r   EmbedLayerNormalizationQuantr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r   S  s,    r   modelr   datasetr   label_namescompute_metricsc           
         s  d}d}t |ddD ]\ t fdd|D }|r/t fdd|D }t|dkr.|d }nd} fd	d
| jD  | di  }t|dkrL|d }|du rR|nt||dd}|du r_|nt||dd}q
|dur||dur||dur||t||d}	ni }	t|||	t|dS )az  
    Run evaluation and returns metrics and predictions.

    Args:
        model (`ORTModel`):
            The ONNXRuntime model to use for the evaluation step.
        dataset (`datasets.Dataset`):
            Dataset to use for the evaluation step.
        label_names (`List[str]`, `optional`):
            The list of keys in your dictionary of inputs that correspond to the labels.
        compute_metrics (`Callable[[EvalPrediction], Dict]`, `optional`):
            The function that will be used to compute metrics at evaluation. Must take an `EvalPrediction` and
            return a dictionary string to metric values.
    N
Evaluation)descc                 3   s    | ]
}  |d uV  qd S rQ   )r~   ).0kinputsr"   r#   	<genexpr>  s    z"evaluation_loop.<locals>.<genexpr>c                 3   s"    | ]}t  |gV  qd S rQ   )nparrayr~   )r   r   r   r"   r#   r     s     r   r   c                    s&   i | ]}| v r|t  | gqS r"   )r   r   )r   keyr   r"   r#   
<dictcomp>  s   & z#evaluation_loop.<locals>.<dictcomp>i)padding_index)predictions	label_ids)r   r   metricsnum_samplesr"   )r   alltupler   r   r   r   r   )
r   r   r   r   	all_preds
all_labels
has_labelslabelspredsr   r"   r   r#   evaluation_loopn  s(   r   c                    s   t | tjjrtj dt|  d d S t | tjjr0tj dt| j	j
d d S t | trKt | d tjjtjjfrK fdd| D S t | trlt tt|  tjjtjjfrl fdd|  D S | S )Nr   r   r   c                    s   g | ]}t | qS r"   np_to_pt_generators)r   ar   r"   r#   
<listcomp>  s    z'np_to_pt_generators.<locals>.<listcomp>c                    s   i | ]
\}}|t | qS r"   r   )r   r   vr   r"   r#   r     s    z'np_to_pt_generators.<locals>.<dictcomp>)r   r   randomRandomStater|   	Generatormanual_seedint	get_statebit_generatorstatera   r   nextitervaluesitems)	np_objectr}   r"   r   r#   r     s   ""$r   c                   @   "   e Zd Zdd ZG dd dZdS )DummyWhisperModelc                 C   s   |   | _d S rQ   )Encoderencoderselfr"   r"   r#   __init__  s   zDummyWhisperModel.__init__c                   @   r  )zDummyWhisperModel.Encoderc                 C   s    | j dd| _| j dd| _d S )N)r   stride)r   )r   conv1conv2r  r"   r"   r#   r    s   z"DummyWhisperModel.Encoder.__init__c                   @   s   e Zd Zdd ZdS )zDummyWhisperModel.Encoder.Convc                 C   s
   || _ d S rQ   r  )r  r	  r"   r"   r#   r    s   
z'DummyWhisperModel.Encoder.Conv.__init__N)re   rf   rg   r  r"   r"   r"   r#   r     s    r   N)re   rf   rg   r  r   r"   r"   r"   r#   r    s    r  N)re   rf   rg   r  r  r"   r"   r"   r#   r    s    r  sessionc                 C   sV   |   D ]}t|j}|jr|  S q|  D ]}t|j}|jr'|  S qtjS )z
    Returns the `torch.dtype` associated with the ONNX Runtime session.
    This dtype is inferred from the input/output dtypes of the session.
    If no floating point type is found, it defaults to `torch.float32`.
    )
get_inputsr   ort_type_to_torch_typer   is_floating_pointget_outputsr|   float32)r  inputtorch_dtypeoutputr"   r"   r#   get_dtype_from_session  s   r  )r   NNrQ   )NN)Jrh   r&   r   r   enumr   inspectr   typingr   r   r   r   r   r	   r
   r   r   numpyr   r|   	packagingr   r   transformersr   transformers.trainer_pt_utilsr   transformers.trainer_utilsr   transformers.utilsr   onnxruntimer   *onnxruntime.transformers.io_binding_helperr   exporters.onnxr   r   datasetsr   modeling_ortr   
get_loggerre   r   ONNX_WEIGHTS_NAMEONNX_ENCODER_NAMEONNX_DECODER_NAMEONNX_DECODER_WITH_PAST_NAMEONNX_DECODER_MERGED_NAMEr$   r)   r*   rr   rt   rj   r}   r   r   r   r   r   r   boolr   r   r   r   r   r  InferenceSessiondtyper  r"   r"   r"   r#   <module>   s~   ,
	Y
(7
"
 "

2