
    {-j                         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e          Z G d de          Z G d de          ZdS )    N)ThreadPoolExecutor)AnyDictOptionalUnion   )device   ImageBatchSampler)	HPIConfigPaddlePredictorOption   )BasePipelinec                   @     e Zd Zdd fd
Zed             Zd Z xZS )"MultiDeviceSimpleInferenceExecutorNpostprocess_resultc                r    t                                                       || _        || _        || _        d S N)super__init__
_pipelines_batch_sampler_postprocess_result)self	pipelinesbatch_samplerr   	__class__s       e/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddlex/inference/pipelines/_parallel.pyr   z+MultiDeviceSimpleInferenceExecutor.__init__   s6    #+#5       c                     | j         S r   )r   r   s    r    r   z,MultiDeviceSimpleInferenceExecutor.pipelines    s
    r!   c           	   /   H  K   t          t          | j                            5 }|                     |          }d}|sg }| j        D ]^}	 t	          |          }	n# t
          $ r d}Y  n=w xY w|	j        }
|                    d ||
||          }|                    |	|f           _|D ]^\  }	}|	                                }t          |	j        |          D ]
\  }}||d<   | j        r|                     ||	          }|E d {V  _|d d d            d S # 1 swxY w Y   d S )N)max_workersFTc                 >    t           | j        |g|R i |          S r   )listpredict)pipelineinput_instancesargskwargss       r    <lambda>z<MultiDeviceSimpleInferenceExecutor.execute.<locals>.<lambda>7   s2    ,H,_NtNNNvNNI I r!   
input_path)r   lenr   r   nextStopIteration	instancessubmitappendresultzipinput_pathsr   )r   inputr+   r,   poolinput_batchesout_of_datainput_future_pairsr)   input_batchr*   futurer5   r.   result_items                  r    executez*MultiDeviceSimpleInferenceExecutor.execute$   s       C,@,@AAA  	&T //66MK! &%'" $ E EH&*=&9&9(   &* '2&;O![[  !' F '--{F.CDDDD
 ,> & &'K#]]__F36{7NPV3W3W ? ?/
K4>L11/ O!%!9!9&+!N!N%%%%%%%%%; " & 	&  	&  	&  	&  	&  	&  	&  	&  	&  	&  	&  	&  	&  	&  	&  	&  	&  	&s6   %DADA+'D*A++BDDD)__name__
__module____qualname__r   propertyr   r@   __classcell__r   s   @r    r   r      so        GK 6 6 6 6 6 6 6   X&& && && && && && &&r!   r   c                   D    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Zed             Zd Zd Zej        d             Zej        d             Zej        d             Zd Z xZS )#AutoParallelSimpleInferencePipelineNFr	   engineengine_config	pp_optionuse_hpip
hpi_configconfigr	   rJ   rK   rL   rM   rN   returnc          
      j    t                      j        d||||||d| || _        d| _        | j        t          j        | j                  \  }	}
|
t          |
          dk    rd| _        g | _        |
D ]F}| 	                    |t          j
        |	|g                    }| j                            |           G|                     |          }|                     |          }t          | j        || j                  | _        | j        s"| 	                    || 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`.
        rI   FNr   Tr    )r   r   _init_kwargs_multi_device_inferencer	   device_utilsparse_devicer/   r   _create_internal_pipelineconstr_devicer4   _get_batch_size_create_batch_samplerr   r   	_executor	_pipeline)r   rO   r	   rJ   rK   rL   rM   rN   r,   device_type
device_ids	device_idr)   
batch_sizer   r   s                  r    r   z,AutoParallelSimpleInferencePipeline.__init__N   sq   2 	 	
'!	
 	
 	
 	
 	
 #',$;"&2&?&L&L#K%#j//A*=*=/3,"$!+ 5 5I#==$2;LL   H O**84444!11&99
 $ : :: F F!CO!'+'?" " "
 + 	Q!;;FDKPPDNNN	Q 	Qr!   c                     | j         S r   )rT   r#   s    r    multi_device_inferencez:AutoParallelSimpleInferencePipeline.multi_device_inference   s    ++r!   c                 ~    | j         r"| j        j        d         }t          ||          S t          | j        |          S )Nr   )rT   r[   r   getattrr\   )r   namefirst_pipelines      r    __getattr__z/AutoParallelSimpleInferencePipeline.__getattr__   s>    ' 	1!^5a8N>40004>4000r!   c              /      K   | j         r | j        j        |g|R i |E d {V  d S  | j        j        |g|R i |E d {V  d S r   )rT   r[   r@   r\   r(   )r   r8   r+   r,   s       r    r(   z+AutoParallelSimpleInferencePipeline.predict   s       ' 	-t~-              .t~-             r!   c                     t           r   NotImplementedError)r   rO   r	   s      r    rW   z=AutoParallelSimpleInferencePipeline._create_internal_pipeline       !!r!   c                     t           r   rj   )r   rO   s     r    rY   z3AutoParallelSimpleInferencePipeline._get_batch_size   rl   r!   c                     t           r   rj   r   r`   s     r    rZ   z9AutoParallelSimpleInferencePipeline._create_batch_sampler   rl   r!   c                     |S r   rR   )r   r5   r=   s      r    r   z7AutoParallelSimpleInferencePipeline._postprocess_result   s    r!   )rA   rB   rC   r   r   strr   r   boolr   r   r   rD   rb   rg   r(   abcabstractmethodrW   rY   rZ   r   rE   rF   s   @r    rH   rH   M   s       
 !% $2659AE9Q 9Q 9Q9Q 	9Q
 9Q  S#X/9Q 129Q 9Q U4S>9#<=>9Q 
9Q 9Q 9Q 9Q 9Q 9Qv , , X,1 1 1  & 	" " " 	" " " 	" " "      r!   rH   c                   P    e Zd Zeej        d                         Zd Zd Zd Z	dS )(AutoParallelImageSimpleInferencePipelinec                     t           r   rj   r#   s    r    _pipeline_clsz6AutoParallelImageSimpleInferencePipeline._pipeline_cls   s
     "!r!   c           
      l     | j         |f|| j        | j        | j        | j        | j        d| j        }|S )NrI   )rx   rJ   rK   rL   rM   rN   rS   )r   rO   r	   r)   s       r    rW   zBAutoParallelImageSimpleInferencePipeline._create_internal_pipeline   sT    %4%	
;,n]	
 	
 	
 	
 r!   c                      t          |          S r   r   ro   s     r    rZ   z>AutoParallelImageSimpleInferencePipeline._create_batch_sampler   s     ,,,r!   c                 F    t          |j        |          D ]
\  }}||d<   |S )N
page_index)r6   page_indexes)r   r5   r=   r|   items        r    r   z<AutoParallelImageSimpleInferencePipeline._postprocess_result   s5     #K$<f E E 	, 	,J!+Dr!   N)
rA   rB   rC   rD   rs   rt   rx   rW   rZ   r   rR   r!   r    rv   rv      sh        " "  X"  - - -    r!   rv   )rs   concurrent.futuresr   typingr   r   r   r   utilsr	   rU   common.batch_samplerr   modelsr   r   baser   objectr   rH   rv   rR   r!   r    <module>r      s'   


 1 1 1 1 1 1 - - - - - - - - - - - - + + + + + + 4 4 4 4 4 4 5 5 5 5 5 5 5 5      1& 1& 1& 1& 1& 1& 1& 1&hg g g g g, g g gT    /R     r!   