o
    pi                     @  s   d dl mZ 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 er<d dlmZ d dlmZ d dlmZ G d	d
 d
ZdS )    )annotationsN)TYPE_CHECKING)static)core)Sequence)CompiledProgram)Programc                   @  sJ   e Zd ZdddZd ddZdd	gfd!ddZd"ddZ	d#d$ddZdS )%	CostModelreturnNonec                 C  s   d S )N )selfr   r   c/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/cost_model/cost_model.py__init__"   s   zCostModel.__init__tuple[Program, Program]c                 C  s   t   t }t }tj||d* t jjdd dgdd}t jj|d}t |}t j	j
dd| W d    n1 s@w   Y  td	|  ||fS )
N)main_programstartup_programX   float32)nameshapedtype
   g{Gz?)Zlearning_ratezmain program is: )paddleZenable_staticr   r   Zprogram_guarddatannZfcmeanZ	optimizerZSGDZminimizeprint)r   r   r   r   ZhiddenZlossr   r   r   build_program%   s   


zCostModel.build_programgputimer   Program | CompiledProgramr   devicestrfetch_cost_listSequence[str]c                 C  sp   t d}tjjddd}t j|}|| t j	 }|
  |j|d|ig d t }	|	|}
d S )Nr    )r   r   )sizer   r   )feedZ
fetch_list)r   Z
set_devicenprandomZastyper   ExecutorrunZprofilerZProfilerstartr   r	   ZProfileMeasure)r   r   r   r#   r%   ZplacexexepZ
cost_modelZ	cost_datar   r   r   profile_measure8   s   


zCostModel.profile_measuredict[str, str | float]c                 C  sT   t jt jtd}t|d}t|}W d    n1 s w   Y  || _|S )Nzstatic_op_benchmark.jsonr)	ospathjoindirname__file__openjsonload_static_cost_data)r   Zstatic_cost_data_pathZload_f	load_dictr   r   r   static_cost_dataK   s   zCostModel.static_cost_dataTr   op_nameforwardboolr   c                 C  sf   |d u rt di }| jD ]#}|d |kr0||d v r0|r$|d |d< n|d |d< |d |d< q|S )Nz?op_name should not be empty when you want to get static op timeopconfigZpaddle_gpu_timeZop_timeZpaddle_gpu_time_backward)
ValueErrorr<   )r   r?   r@   r   Zop_costZop_datar   r   r   get_static_op_timeU   s   
zCostModel.get_static_op_timeN)r
   r   )r
   r   )
r   r"   r   r"   r#   r$   r%   r&   r
   r   )r
   r2   )Tr   )r?   r$   r@   rA   r   r$   r
   r2   )__name__
__module____qualname__r   r   r1   r>   rE   r   r   r   r   r	   !   s    


r	   )
__future__r   r:   r4   typingr   numpyr)   r   r   Zpaddle.baser   collections.abcr   Zpaddle.base.compilerr   Zpaddle.base.frameworkr   r	   r   r   r   r   <module>   s   