a
    d                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZmZ e j	ddZ
e
jddddd	 e
jd
deddd dddZd ddZd!ddZdd Zdd Zdd Zedkre  dS )"    N)FlopCountAnalysisflop_count_strActivationCountAnalysiszOpenCLIP Profiler)descriptionz--modelNAME zmodel(s) to profile)metavardefaulthelpz--results-fileZFILENAMEzOutput csv file for results)r	   typer   r
         r   M      Fc                 C   s   |r|  d} t|  jt|  j }}tj|f| ||d}tj|f| |tjd}	t| ||	f}
t	| ||	f}|rt
|
}t| |
 | fS Ncpu)devicedtype)tonext
parametersr   r   torchonesint64r   r   r   printtotal)modelimage_input_sizetext_input_size
batch_sizedetailed	force_cpur   r   Zexample_image_inputZexample_text_inputfcaacafcs r'   Y/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/training/profile.pyprofile_fvcore   s    
r)   c           
      C   sl   |r|  d} t|  j}tj|f| |tjd}t| |}t| |}|r\t	|}	t
|	 | | fS r   )r   r   r   r   r   r   r   r   r   r   r   r   )
r   r    r!   r"   r#   r   example_inputr$   r%   r&   r'   r'   r(   profile_fvcore_text'   s    


r+   c                 C   sz   |r|  d} t|  jt|  j }}tj|f| ||d}t| |}t| |}	|rjt	|}
t
|
 | |	 fS r   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r!   r"   r#   r   r   r*   r$   r%   r&   r'   r'   r(   profile_fvcore_image:   s    


r,   c                 C   s   t dd |  D S )Nc                 S   s   g | ]}|  qS r'   )Znumel).0mr'   r'   r(   
<listcomp>N       z count_params.<locals>.<listcomp>)sumr   )r   r'   r'   r(   count_paramsM   s    r2   c              
   C   s:  t j| ddd}|  tj r*| }t|jjt	t
frVdt	|jjdd   }nd|jj|jjf}d}i }| |d< |d	 |d
< t | }|rt jf i |d }t jf i |d }t|j|d< t|j|d< t|d |d< nd|d< d|d< d|d< d}|r6|d	8 }zt|||| d\}	}
t|j|| d\}}t|j|| d\}}t|	d d|d< t|
d d|d< tt|d d|d< t|d d|d< t|d d|d< tt|jd d|d< t|d d|d< t|d d|d< tt|jd d|d< W n& ty0 } zW Y d }~n
d }~0 0 q|S ) NTF)Zforce_custom_textZpretrained_hf)r   r   r   r   r   
image_size
vision_cfgtext_cfgZimage_width
text_widthZ	embed_dimr      )r   r    r#   )r   r#   )r    r#   g    eAgmacsg    .AZmactsZmparamsZimage_gmacsZimage_mactsZimage_mparamsZ
text_gmacsZ
text_mactsZtext_mparams)	open_clipZcreate_modelevalr   ZcudaZis_available
isinstancevisualr4   tuplelistZget_model_configZCLIPVisionCfgZCLIPTextCfgintwidthr)   r,   r+   textroundr2   RuntimeError)Z
model_namer   r   r    resultsZ	model_cfgr5   r6   retriesZmacsZactsZ
image_macsZ
image_actsZ	text_macsZ	text_actser'   r'   r(   profile_modelQ   s\    







rH   c                  C   s   t  } | jdkrt }n| jd}g }|D ]}t|}|| q0tj	||d 
 d}|d}t| | jr|j| jdd d S )Nall,r   )columnsr9   F)index)parser
parse_argsr   r:   Zlist_modelssplitrH   appendpdZ	DataFramekeysZsort_valuesr   Zresults_fileZto_csv)argsZparsed_modelrE   r.   rowZdfr'   r'   r(   main   s    


rU   __main__)r   r   r   FF)r   r   FF)r   r   FF)argparser   r:   ZpandasrQ   Z	fvcore.nnr   r   r   ArgumentParserrM   add_argumentstrr)   r+   r,   r2   rH   rU   __name__r'   r'   r'   r(   <module>   s@   
     
    
    
7