a
    d *                     @   s   d Z ddlZddlmZmZ ddlmZ ddlmZm	Z	 ddl
mZmZmZ dd	lmZmZmZmZmZmZ e rddlZdd
lmZ e rddlmZ eeZG dd deZdS )zD
    Benchmarking the library on inference and training in PyTorch.
    N)CallableOptional   )PretrainedConfig)MODEL_MAPPINGMODEL_WITH_LM_HEAD_MAPPING)is_py3nvml_availableis_torch_availablelogging   )	BenchmarkMemoryMemorySummarymeasure_peak_memory_cpustart_memory_tracingstop_memory_tracing)PyTorchBenchmarkArgumentsc                   @   s   e Zd ZU eed< eed< dZeed< edd Z	ee
e
eddd	Zee
e
eee gdd
dZee
e
edddZee
e
eee gdddZee
e
eg df dddZee
e
eg df dddZedddZeg df eegdddZdS )PyTorchBenchmarkargsZconfigsZPyTorch	frameworkc                 C   s   t jS N)torch__version__)self r   i/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/transformers/benchmark/benchmark.pyframework_version7   s    z"PyTorchBenchmark.framework_version)
model_name
batch_sizesequence_lengthreturnc                 C   s   |  |||}| |S r   )_prepare_inference_func_measure_speedr   r   r   r   Z
_inferencer   r   r   _inference_speed;   s    z!PyTorchBenchmark._inference_speedc                 C   s   |  |||}| |S r   )r!   _measure_memoryr#   r   r   r   _inference_memory?   s    z"PyTorchBenchmark._inference_memoryc                 C   s   |  |||}| |S r   )_prepare_train_funcr"   r   r   r   r   _trainr   r   r   _train_speedE   s    zPyTorchBenchmark._train_speedc                 C   s   |  |||}| |S r   )r'   r%   r(   r   r   r   _train_memoryI   s    zPyTorchBenchmark._train_memoryNc                    s  | j | }| jjrd|_t|do:t|jto:t|jdk}| jjs|rz.|jd }t	d|gd}t
||}||}	W q ty   t| dY q0 nt|j |}	|	  |	| jj t|dr|jn|jj}
tj|
||ftj| jjd| jjr td	 | jjstd
|	  | jjrdt  tj|	 W d    n1 sX0    Y  n|	  fdd} fdd}|jr|n|}|S )NTarchitecturesr   transformersfromlist does not exist. If you just want to test the pretrained model, you might want to set `--only_pretrain_model` or `args.only_pretrain_model=True`.
vocab_sizeZdtypedevice&Running training in Mixed Precision...)Mixed precision is possible only for GPU.c                     s8   t    d} W d    n1 s*0    Y  | S )N)decoder_input_idsr   no_gradoutputsZinference_model	input_idsr   r   encoder_decoder_forward}   s    
*zIPyTorchBenchmark._prepare_inference_func.<locals>.encoder_decoder_forwardc                     s4   t    } W d    n1 s&0    Y  | S r   r7   r9   r;   r   r   encoder_forward   s    
&zAPyTorchBenchmark._prepare_inference_func.<locals>.encoder_forward) config_dictr   torchscripthasattr
isinstancer,   listlenonly_pretrain_model
__import__getattrImportErrorr   	__class__evaltor3   r1   encoderr   randintlongfp16loggerinfois_gpu
ValueErrorhalfr8   Zjittraceis_encoder_decoder)r   r   r   r   confighas_model_class_in_configmodel_classtransformers_module	model_clsmodelr1   r=   r>   Z_forwardr   r;   r   r!   O   sH    









0z(PyTorchBenchmark._prepare_inference_funcc                    sX  | j | }t|do,t|jto,t|jdk}| jjs|rz.|jd }td|gd}t	||}||}	W q t
y   t
| dY q0 nt|j |}	| jjrtdn|	|	  |	| jj t|dr|jn|jj}
tj|
||ftj| jjd | jjr(td	 | jjs td
|	   fdd} fdd}|jrP|n|}|S )Nr,   r   r-   r.   r0   z5Training for torchscript is currently not implementedr1   r2   r4   r5   c                     s     dd } |    | S )N)labelsr   ZbackwardZlossr<   Ztrain_modelr   r   !compute_loss_and_backprob_encoder   s    zOPyTorchBenchmark._prepare_train_func.<locals>.compute_loss_and_backprob_encoderc                     s      dd } |    | S )N)r6   r]   r   r^   r_   r`   r   r   )compute_loss_and_backprob_encoder_decoder   s    zWPyTorchBenchmark._prepare_train_func.<locals>.compute_loss_and_backprob_encoder_decoder)r?   rA   rB   r,   rC   rD   r   rE   rF   rG   rH   r   rI   r@   NotImplementedErrorZtrainrK   r3   r1   rL   r   rM   rN   rO   rP   rQ   rR   rS   rT   rV   )r   r   r   r   rW   rX   rY   rZ   r[   r\   r1   ra   rb   r)   r   r`   r   r'      sH    








z$PyTorchBenchmark._prepare_train_func)r    c              
   C   s   z|| j js| j jr,td tj|ddd tj|| j jdd}| j jrp| j jrpdd lm	  m
} | |  t|d W S  ty } z| d|  W Y d }~d	S d }~0 0 d S )
NzRDo inference on TPU or torchscript. Running model 5 times to stabilize compilationr      )repeatnumber
   r   g      $@Doesn't fit on GPU. N/A)r   is_tpur@   rP   rQ   timeitre   Ztorch_xla_tpu_print_metricsZtorch_xla.debug.metricsdebugZmetricsprint_fnZmetrics_reportminRuntimeError)r   funcZruntimesZmeter   r   r   r"      s(    
zPyTorchBenchmark._measure_speed)rp   r    c           
   
   C   s  z| j jrtd}| j jr$tdn| j jrt sBtd d}qt	d t
  |  t
| j j}t
|}|j}t|}t
  nt|}t|trt|n|}| j jrt|}nd }||fW S  t y }	 z| d|	  W Y d }	~	dS d }	~	0 0 d S )Nr-   zMemory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking with `--no-memory` or `args.memory=False`zypy3nvml not installed, we won't log GPU memory usage. Install py3nvml (pip install py3nvml) to log information about GPU.ri   zlMeasuring total GPU usage on GPU device. Make sure to not have additional processes running on the same GPU.rh   )ri   N)r   Ztrace_memory_line_by_liner   rj   rc   rR   r   rP   warningrQ   nvmlZnvmlInitZnvmlDeviceGetHandleByIndexZ
device_idxZnvmlDeviceGetMemoryInfousedr   ZnvmlShutdownr   rB   intr   ro   rm   )
r   rp   rU   ZmemoryhandleZmeminfoZmax_bytes_in_useZmemory_bytessummaryrq   r   r   r   r%      s@    



z PyTorchBenchmark._measure_memory)__name__
__module____qualname__r   __annotations__r   r   strpropertyr   ru   floatr$   r   r   r   r&   r*   r+   r   r!   r'   r"   r%   r   r   r   r   r   2   s"   



;<r   )__doc__rk   typingr   r   Zconfiguration_utilsr   Zmodels.auto.modeling_autor   r   utilsr   r	   r
   Zbenchmark_utilsr   r   r   r   r   r   r   Zbenchmark_argsr   Zpy3nvml.py3nvmlZpy3nvmlrs   Z
get_loggerrx   rP   r   r   r   r   r   <module>   s    

