o
    Me                     @   sJ   d dl Z d dlmZ d dlZd dlZd dlZd dlmZ G dd dZ	dS )    N)corec                   @   s>   e Zd Zdd Zdd ZddgfddZd	d
 ZdddZdS )	CostModelc                 C   s   d S )N )selfr   r   LD:\Projects\ConvertPro\env\Lib\site-packages\paddle/cost_model/cost_model.py__init__   s   zCostModel.__init__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_staticstaticZProgramZprogram_guarddatannZfcmeanZ	optimizerZSGDZminimizeprintformat)r   r   r	   r   ZhiddenZlossr   r   r   build_program   s    
	zCostModel.build_programgputimec           
      C   sl   t d}tjjddd}t j|}|| t jj	
d |j|d|ig d t }||}	d S )Nr   )r   r   )sizer   ZAllr
   )feedZ
fetch_list)r   Z
set_devicenprandomZastyper   ExecutorrunZfluidZprofilerZstart_profilerr   r   ZProfileMeasure)
r   r	   r   ZdeviceZfetch_cost_listZplacexexeZ
cost_modelZ	cost_datar   r   r   profile_measure.   s   

zCostModel.profile_measurec                 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_data?   s   zCostModel.static_cost_dataTr   c                 C   sf   |d k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   Zop_nameforwardr   Zop_costZop_datar   r   r   get_static_op_timeH   s   
zCostModel.get_static_op_timeN)Tr   )__name__
__module____qualname__r   r   r#   r/   r4   r   r   r   r   r      s    
	r   )
r   Zpaddle.staticr   numpyr   r+   r%   Zpaddle.fluidr   r   r   r   r   r   <module>   s   