o
    0j9#                     @   s   d 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 ddlZddlmZmZmZ ddlmZ d	d
lmZ ddlmZ ddlmZ ddgZG dd deZedG dd deZdS )zONNX Runtime runner.    )PathLike)Path)AnyDictListOptionalSequenceUnionN)	BaseModel
ConfigDictmodel_validator   )class_requires_deps   )get_model_paths   )InferenceRunner)sort_inputsONNXRuntimeRunnerConfigONNXRuntimeRunnerc                   @   s  e Zd ZU dZeddZdZee e	d< dZ
ee e	d< dZeee  e	d< dZeeeeeef  eeef f  e	d< dZee e	d	< dZee e	d
< dZee e	d< dZee e	d< dZee e	d< dZee e	d< dZee e	d< dZeeeef  e	d< edddd ZdS )r   z(Engine config for onnxruntime inference.Zforbid)extraNdevice_type	device_id	providersprovider_optionsgraph_optimization_levelintra_op_num_threadsinter_op_num_threadsexecution_modelog_severity_levelenable_mem_patternenable_cpu_mem_arenasession_optionsafter)modec                 C   sb   | j d urt| jtrt| j t| jkrtd| j d ur/t| jtr/t| j dkr/td| S )Nz;Length mismatch between `providers` and `provider_options`.r   zQWhen `provider_options` is a dict, `providers` must contain exactly one provider.)r   
isinstancer   listlen
ValueErrordictself r,   t/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/models/runners/onnxruntime_runner.pycheck_provider_options2   s   



z.ONNXRuntimeRunnerConfig.check_provider_options)__name__
__module____qualname____doc__r   Zmodel_configr   r   str__annotations__r   intr   r   r   r	   r   r   r   r   r   r   r   r    boolr!   r"   r   r.   r,   r,   r,   r-   r       s"   
 
,onnxruntimec                
       s   e Zd ZdZ	ddeeef dedeeee	f  ddf fddZ
	dd	eeej ejdf d
e	deej fddZd ddZdd ZdefddZdd Zdd Zdd Zdd Zdd Zdd Z  ZS )!r   zONNX Runtime inference runner.N	model_dirmodel_file_prefixconfigreturnc                    sz   dd l }t   t|| _|| _t|pi jdd| _	|| _
|  | _dd | j D | _dd | j D | _d S )Nr   T)Zexclude_nonec                 S      g | ]}|j qS r,   name.0metar,   r,   r-   
<listcomp>[       z.ONNXRuntimeRunner.__init__.<locals>.<listcomp>c                 S   r<   r,   r=   r?   r,   r,   r-   rB   \   rC   )r7   super__init__r   r8   r9   r   Zmodel_validateZ
model_dump_config_ort_create_sessionsessionZ
get_inputs_input_namesZget_outputs_output_names)r+   r8   r9   r:   Zort	__class__r,   r-   rE   K   s   


zONNXRuntimeRunner.__init__xkwargsc                 K   s   |d u rd|v r|d }|d u rt dt|tjr|g}t| jt|kr5tdt| j dt| t|| j}dd t| j|D }| j	
| j|S )NrN   z)`ONNXRuntimeRunner.__call__` requires `x`z/The number of inputs does not match the model: z vs c                 S   s   i | ]
\}}|t |qS r,   )npZascontiguousarray)r@   r>   Zinput_r,   r,   r-   
<dictcomp>q   s    
z.ONNXRuntimeRunner.__call__.<locals>.<dictcomp>)	TypeErrorr%   rP   ndarrayr'   rJ   r(   r   ziprI   runrK   )r+   rN   rO   Zfeedsr,   r,   r-   __call__^   s&   
zONNXRuntimeRunner.__call__c                 C   s   d S Nr,   r*   r,   r,   r-   closew   s   zONNXRuntimeRunner.closec                 C   s4   |   }|  }|  \}}| jjt||||dS )N)sess_optionsr   r   )_get_model_path_build_session_options_resolve_providersrG   ZInferenceSessionr3   )r+   Z
model_pathr"   r   r   r,   r,   r-   rH   z   s   z!ONNXRuntimeRunner._create_sessionc                 C   s&   t | j| j}d|vrtd|d S )NZonnxzNo valid ONNX model found)r   r8   r9   RuntimeError)r+   Zmodel_pathsr,   r,   r-   rZ      s   z!ONNXRuntimeRunner._get_model_pathc                 C   s   | j  }| jd}|d ur| j ||_| jd}|d ur&| ||_dD ]}| j|}|d ur:t||| q(| jdi 	 D ]\}}t
||sTtd|t||| qD|S )Nr   r   )r   r   r   r    r!   r"   z%Invalid ONNX Runtime session option: )rG   ZSessionOptionsrF   getZGraphOptimizationLevelr   _resolve_execution_moder   setattritemshasattrr(   )r+   rY   Zgraph_levelr   keyvaluer,   r,   r-   r[      s&   

z(ONNXRuntimeRunner._build_session_optionsc                 C   sJ   t |tr| j|S | }|dv r| jjjS |dv r!| jjjS td)N>   Zort_sequential
sequential>   Zort_parallelZparallelzeInvalid `execution_mode`, expected one of 'sequential', 'parallel', 'ORT_SEQUENTIAL', 'ORT_PARALLEL'.)r%   r5   rG   ZExecutionModelowerZORT_SEQUENTIALZORT_PARALLELr(   )r+   r   r$   r,   r,   r-   r_      s   


z)ONNXRuntimeRunner._resolve_execution_modec                 C   s   | j d}| j d}|d u r,|  \}}|d u r|}nt|tr+|g|dd  }nt|tr4|g}| ||}| | ||fS )Nr   r   r   )rF   r^   _default_providersr%   r)    _inject_default_provider_options_validate_providers)r+   r   r   Zdefault_provider_optionsr,   r,   r-   r\      s    


z$ONNXRuntimeRunner._resolve_providersc                 C   sb   | j dpd }| j d}|dkrdgd fS |dkr-|d u r#d}ddgd|ii gfS td)	Nr   cpur   ZCPUExecutionProviderZgpur   CUDAExecutionProviderzp`engine='onnxruntime'` only supports `device_type` of 'cpu' or 'gpu' unless `providers` is specified explicitly.)rF   r^   rf   r(   )r+   r   r   r,   r,   r-   rg      s   

z$ONNXRuntimeRunner._default_providersc                    sB   t | j   fdd|D }|rtd|dt dd S )Nc                    s   g | ]}| vr|qS r,   r,   )r@   provider	availabler,   r-   rB      s    z9ONNXRuntimeRunner._validate_providers.<locals>.<listcomp>z2Requested ONNX Runtime providers are unavailable: z. Available providers: .)setrG   Zget_available_providersr]   sorted)r+   r   missingr,   rm   r-   ri      s   z%ONNXRuntimeRunner._validate_providersc                 C   sj   |d u rdd |D }nt |}| jd}|d u r|S t|D ]\}}|dkr2d|i|| ||< q |S )Nc                 S   s   g | ]}i qS r,   r,   )r@   _r,   r,   r-   rB      s    zFONNXRuntimeRunner._inject_default_provider_options.<locals>.<listcomp>r   rk   )r&   rF   r^   	enumerate)r+   r   r   r   idxrl   r,   r,   r-   rh      s   z2ONNXRuntimeRunner._inject_default_provider_optionsrW   )r;   N)r/   r0   r1   r2   r	   r3   r   r   r   r   rE   r   rP   rS   r   rV   rX   rH   r   rZ   r[   r_   r\   rg   ri   rh   __classcell__r,   r,   rL   r-   r   G   s:    


	)r2   osr   pathlibr   typingr   r   r   r   r   r	   numpyrP   Zpydanticr
   r   r   Z
utils.depsr   Zutils.model_pathsr   Zinference_runnerr   utilsr   __all__r   r   r,   r,   r,   r-   <module>   s    '