o
    * i                     @  s  d dl mZ d dlZd dlZd dlmZ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 d dlmZ erXd dlmZ d dlmZ d dl
mZ G d	d
 d
eZeeejddZeZeZ ej!Z"eZ#eZeZ$d*ddZ%d+ddZ&dd Z'de( fd,d(d)Z)e%e_*e&e_+e'e_,dS )-    )annotationsN)TYPE_CHECKINGAny	TypedDict)core)AnalysisConfigPaddleDTypePaddleInferPredictorPaddleInferTensorPaddlePlaceconvert_to_mixed_precision_bind)
get_logger)Unpack)Tensorc                   @  s   e Zd ZU ded< dS )
_WhiteListset[str]
white_listN)__name__
__module____qualname____annotations__ r   r   d/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/inference/wrapper.pyr   )   s   
 r   z&%(asctime)s-%(levelname)s: %(message)s)fmtdatanpt.NDArray[Any] | list[str]returnNonec                 C  sF   t |tjst |trt|dkrt |d tr| | dS td)zA
    Support input type check based on tensor.copy_from_cpu.
    r   zHIn copy_from_cpu, we only support numpy ndarray and list[str] data type.N)
isinstancenpZndarraylistlenstrZ_copy_from_cpu_bind	TypeErrorselfr   r   r   r   tensor_copy_from_cpu9   s   r&   r   c                 C  sT   t |tjr| | dS t |tjr| | dS t |tjjj	r&t
dt
d)zG
    Support input type check based on tensor.share_external_data.
    zThe interface 'share_external_data' can only be used in dynamic graph mode. Maybe you called 'paddle.enable_static()' and you are in static graph mode now. Please use 'copy_from_cpu' instead.z?In share_external_data, we only support Tensor and DenseTensor.N)r   r   ZDenseTensorZ_share_external_data_bindpaddler   Z'_share_external_data_paddle_tensor_bindbaseZ	frameworkVariabler#   r$   r   r   r   tensor_share_external_dataG   s   r*   c                 C  s   |  |||| dS )z9
    Support tensor.share_external_data_by_ptr_name.
    N)Z%_share_external_data_by_ptr_name_bind)r%   r   shapeZdtypeZplacer   r   r   &tensor_share_external_data_by_ptr_name[   s   r,   T
model_filer"   params_filemixed_model_filemixed_params_filemixed_precisionPrecisionTypebackend	PlaceTypekeep_io_typesbool
black_listr   kwargsUnpack[_WhiteList]c              
   K  s   |t ju rt std tj|}	t	|dkr tj|n|	}
tj
|
s-t|
 |dt }t| ||||||||	 dS )a  
    Convert a fp32 model to mixed precision model.

    Args:
        model_file: fp32 model file, e.g. inference.pdmodel.
        params_file: fp32 params file, e.g. inference.pdiparams.
        mixed_model_file: The storage path of the converted mixed-precision model.
        mixed_params_file: The storage path of the converted mixed-precision params.
        mixed_precision: The precision, e.g. PrecisionType.Half.
        backend: The backend, e.g. PlaceType.GPU.
        keep_io_types: Whether the model input and output dtype remains unchanged.
            Default is True.
        black_list: Operators that do not convert precision.
        kwargs: Supported keys including 'white_list'.
            - white_list: Operators that do convert precision.
    zOYou should use PaddlePaddle compiled with GPU when backend set to PlaceType.GPUr   r   N)r4   ZGPUr   Zis_compiled_with_cuda_loggererrorospathdirnamer!   existsmakedirsgetsetr   )r-   r.   r/   r0   r1   r3   r5   r7   r8   Zmixed_model_dirnameZmixed_params_dirnamer   r   r   r   convert_to_mixed_precisionb   s.   
rC   )r   r   r   r   )r   r   r   r   )r-   r"   r.   r"   r/   r"   r0   r"   r1   r2   r3   r4   r5   r6   r7   r   r8   r9   r   r   )-
__future__r   loggingr<   typingr   r   r   numpyr   r'   Zpaddle.baser   Zpaddle.base.corer   r   r	   r
   r   r   Zpaddle.base.log_helperr   Znumpy.typingZnptZtyping_extensionsr   r   r   r   INFOr:   ZDataTyper4   Z	Precisionr2   ZConfigZ	Predictorr&   r*   r,   rB   rC   Zcopy_from_cpuZshare_external_dataZshare_external_data_by_ptr_namer   r   r   r   <module>   s@    

9
