o
    "j                     @   s   d dl Z d dlZd dl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ee jddZeZeZe
jZe
ZeZeZdd	 Zd
d Zde fdedededededede dee fddZ!ee_"ee_#dS )    N)Set)core)AnalysisConfigPaddleDTypePaddleInferPredictorPaddleInferTensorPaddlePlaceconvert_to_mixed_precision_bind)
get_loggerz&%(asctime)s-%(levelname)s: %(message)s)fmtc                 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selfdata r   Y/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/inference/wrapper.pytensor_copy_from_cpu-   s   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 LoDTensor.N)r   r   Z	LoDTensorZ_share_external_data_bindpaddleTensorZ'_share_external_data_paddle_tensor_bindbaseZ	frameworkVariabler   r   r   r   r   tensor_share_external_data;   s   r   T
model_fileparams_filemixed_model_filemixed_params_filemixed_precisionbackendkeep_io_types
black_listc              
   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.
        black_list: Operators that do not convert precision.
        kwargs: Supported keys including 'white_list'.
            - white_list: Operators that do convert precision.
    zOYou shoule use PaddlePaddle compiled with GPU when backend set to PlaceType.GPUr   
white_listN)	PlaceTypeZGPUr   Zis_compiled_with_cuda_loggererrorospathdirnamer   existsmakedirsgetsetr	   )r   r   r   r    r!   r"   r#   r$   kwargsZmixed_model_dirnameZmixed_params_dirnamer%   r   r   r   convert_to_mixed_precisionO   s.   
r1   )$loggingr)   typingr   numpyr   r   Zpaddle.baser   Zpaddle.base.corer   r   r   r   r   r	   Zpaddle.base.log_helperr
   __name__INFOr'   ZDataTyper&   Z	PrecisionZPrecisionTypeZConfigr   Z	Predictorr   r   r/   r   boolr1   Zcopy_from_cpuZshare_external_datar   r   r   r   <module>   sP    
8
