o
    0j                     @   s   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
 ddlmZ ddlmZmZ d	d
lmZ G dd deZG dd deZG dd deZdS )    N)ThreadPoolExecutor)AnyDictOptionalUnion   )device   ImageBatchSampler)	HPIConfigPaddlePredictorOption   )BasePipelinec                       s6   e Zd Zdd fdd
Zedd Zdd Z  ZS )	"MultiDeviceSimpleInferenceExecutorNpostprocess_resultc                   s    t    || _|| _|| _d S N)super__init__
_pipelines_batch_sampler_postprocess_result)self	pipelinesbatch_samplerr   	__class__ f/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/pipelines/_parallel.pyr      s   

z+MultiDeviceSimpleInferenceExecutor.__init__c                 C      | j S r   )r   r   r   r   r   r          z,MultiDeviceSimpleInferenceExecutor.pipelinesc              
   o   s   t t| jdt}| |}d}|sug }| jD ]*}zt|}	W n ty,   d}Y  nw |	j}
|dd ||
||}||	|f q|D ]%\}	}|	 }t
|	j|D ]\}}||d< qS| jre| ||	}|E d H  qE|rW d    d S W d    d S 1 sw   Y  d S )N)max_workersFTc                 S   s   t | j|g|R i |S r   )listpredict)pipelineinput_instancesargskwargsr   r   r   <lambda>7   s    z<MultiDeviceSimpleInferenceExecutor.execute.<locals>.<lambda>
input_path)r   lenr   r   nextStopIterationZ	instancesZsubmitappendresultzipZinput_pathsr   )r   inputr(   r)   poolZinput_batchesZout_of_dataZinput_future_pairsr&   input_batchr'   futurer0   r+   Zresult_itemr   r   r   execute$   sB   

	
"z*MultiDeviceSimpleInferenceExecutor.execute)__name__
__module____qualname__r   propertyr   r6   __classcell__r   r   r   r   r      s
    
r   c                       s   e Zd Zddddddddedee dee deeeef  dee d	ed
ee	eeef e
f  ddf fddZedd Zdd Zdd Zejdd Zejdd Zejdd Zdd Z  ZS )#AutoParallelSimpleInferencePipelineNFr   engineengine_config	pp_optionuse_hpip
hpi_configconfigr   r>   r?   r@   rA   rB   returnc             	      s   t  jd||||||d| || _d| _| jdur]t| j\}	}
|
dur]t|
dkr]d| _g | _|
D ]}| 	|t
|	|g}| j| q5| |}| |}t| j|| jd| _| jsj| 	|| j| _dS dS )a  Initializes the auto-parallel simple inference pipeline.

        Args:
            config (Dict): Configuration dictionary containing various settings.
            device (Optional[str], optional): The device to use for prediction. Defaults to `None`.
            engine (Optional[str], optional): Inference engine. Defaults to `None`.
            engine_config (Optional[Dict[str, Any]], optional): Engine-specific config. Defaults to `None`.
            pp_option (Optional[PaddlePredictorOption], optional): Paddle predictor options.
                Defaults to `None`.
            use_hpip (bool, optional): Whether to use HPIP. Defaults to `False`.
            hpi_config (Optional[Union[Dict[str, Any], HPIConfig]], optional): HPIP configuration.
                Defaults to `None`.
        r=   FNr   Tr   r   )r   r   _init_kwargs_multi_device_inferencer   device_utilsZparse_devicer,   r   _create_internal_pipelineZconstr_devicer/   _get_batch_size_create_batch_samplerr   r   	_executor	_pipeline)r   rC   r   r>   r?   r@   rA   rB   r)   Zdevice_typeZ
device_idsZ	device_idr&   
batch_sizer   r   r   r   r   N   sB   



z,AutoParallelSimpleInferencePipeline.__init__c                 C   r    r   )rF   r!   r   r   r   multi_device_inference   r"   z:AutoParallelSimpleInferencePipeline.multi_device_inferencec                 C   s(   | j r| jjd }t||S t| j|S )Nr   )rF   rK   r   getattrrL   )r   nameZfirst_pipeliner   r   r   __getattr__   s   
z/AutoParallelSimpleInferencePipeline.__getattr__c                 o   sP    | j r| jj|g|R i |E d H  d S | jj|g|R i |E d H  d S r   )rF   rK   r6   rL   r%   )r   r2   r(   r)   r   r   r   r%      s    z+AutoParallelSimpleInferencePipeline.predictc                 C      t r   NotImplementedError)r   rC   r   r   r   r   rH         z=AutoParallelSimpleInferencePipeline._create_internal_pipelinec                 C   rR   r   rS   )r   rC   r   r   r   rI      rU   z3AutoParallelSimpleInferencePipeline._get_batch_sizec                 C   rR   r   rS   r   rM   r   r   r   rJ      rU   z9AutoParallelSimpleInferencePipeline._create_batch_samplerc                 C   s   |S r   r   )r   r0   r4   r   r   r   r      s   z7AutoParallelSimpleInferencePipeline._postprocess_result)r7   r8   r9   r   r   strr   r   boolr   r   r   r:   rN   rQ   r%   abcabstractmethodrH   rI   rJ   r   r;   r   r   r   r   r<   M   sF    	;



r<   c                   @   s6   e Zd Zeejdd Zdd Zdd Zdd Z	d	S )
(AutoParallelImageSimpleInferencePipelinec                 C   rR   r   rS   r!   r   r   r   _pipeline_cls   s   z6AutoParallelImageSimpleInferencePipeline._pipeline_clsc              	   C   s0   | j |f|| j| j| j| j| jd| j}|S )Nr=   )r\   r>   r?   r@   rA   rB   rE   )r   rC   r   r&   r   r   r   rH      s   
zBAutoParallelImageSimpleInferencePipeline._create_internal_pipelinec                 C   s   t |S r   r
   rV   r   r   r   rJ      s   z>AutoParallelImageSimpleInferencePipeline._create_batch_samplerc                 C   s"   t |j|D ]\}}||d< q|S )N
page_index)r1   Zpage_indexes)r   r0   r4   r]   itemr   r   r   r      s   
z<AutoParallelImageSimpleInferencePipeline._postprocess_resultN)
r7   r8   r9   r:   rY   rZ   r\   rH   rJ   r   r   r   r   r   r[      s    r[   )rY   concurrent.futuresr   typingr   r   r   r   utilsr   rG   Zcommon.batch_samplerr   modelsr   r   baser   objectr   r<   r[   r   r   r   r   <module>   s   4j