
    zj                    h   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r d dlmZ d dlmZ d dl
mZ  G d	 d
e          Z eeej        d          ZeZeZ ej!        Z"eZ#eZeZ$d&dZ%d'dZ&d Z'd e(            f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                      e Zd ZU ded<   dS )
_WhiteListset[str]
white_listN)__name__
__module____qualname____annotations__     h/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/inference/wrapper.pyr   r   )   s         r   r   z&%(asctime)s-%(levelname)s: %(message)s)fmtdatanpt.NDArray[Any] | list[str]returnNonec                   t          |t          j                  sCt          |t                    rEt	          |          dk    r2t          |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npndarraylistlenstr_copy_from_cpu_bind	TypeErrorselfr   s     r   tensor_copy_from_cpur,   9   sz     $
## 
4
#&t99q==ZQ5M5M=  &&&&&V
 
 	
r   r   c                J   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   DenseTensor_share_external_data_bindpaddler   '_share_external_data_paddle_tensor_bindbase	frameworkVariabler)   r*   s     r   tensor_share_external_datar5   G   s     $()) 
&&t,,,,,	D&-	(	( 
44T:::::	D&+/8	9	9 	
2
 
 	
 M
 
 	
r   c                6    |                      ||||           dS )z9
    Support tensor.share_external_data_by_ptr_name.
    N)%_share_external_data_by_ptr_name_bind)r+   r   shapedtypeplaces        r   &tensor_share_external_data_by_ptr_namer;   [   s$     	..tUE5IIIIIr   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                   |t           j        u r-t          j                    st                              d           t          j                            |          }	t          |          dk    rt          j                            |          n|	}
t          j        
                    |
          st          j        |
           |                    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)rC   GPUr   is_compiled_with_cuda_loggererrorospathdirnamer&   existsmakedirsgetsetr   )r<   r=   r>   r?   r@   rB   rD   rF   rG   mixed_model_dirnamemixed_params_dirnamer   s               r   convert_to_mixed_precisionrW   b   s    6 )-(B(D(D]	
 	
 	
 '//*:;;  !!Q&& 	)***  
 7>>.// *
()))L#%%00J#
 
 
 
 
r   )r   r   r   r    )r   r   r   r    )r<   r'   r=   r'   r>   r'   r?   r'   r@   rA   rB   rC   rD   rE   rF   r   rG   rH   r   r    )-
__future__r   loggingrN   typingr   r   r   numpyr#   r0   paddle.baser   paddle.base.corer   r	   r
   r   r   r   paddle.base.log_helperr   numpy.typingnpttyping_extensionsr   r   r   r   INFOrL   DataTyperC   	PrecisionrA   Config	Predictorr,   r5   r;   rT   rW   copy_from_cpushare_external_datashare_external_data_by_ptr_namer   r   r   <module>rj      s   # " " " " "  				 0 0 0 0 0 0 0 0 0 0                           . - - - - - ((((((    Y    *gl H   	(		 	
 
 
 

 
 
 
(J J J 3556 6 6 6 6r , 7 )O & & &r   