U
    9%e3                     @   s   d Z ddlZddlZddlm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 d	dlmZ e rddlmZ  e!e"Z#e$e$dddZ%e&e&e&dgdddZ'G dd deZ(dS )zD
    Benchmarking the library on inference and training in PyTorch.
    N)wraps)CallableOptional   )PretrainedConfig)TF_MODEL_MAPPINGTF_MODEL_WITH_LM_HEAD_MAPPING)is_py3nvml_availableis_tf_availablelogging   )	BenchmarkMemoryMemorySummarymeasure_peak_memory_cpustart_memory_tracingstop_memory_tracing)ResourceExhaustedError)TensorFlowBenchmarkArgumentsdo_eager_modeuse_xlac                    s    fdd}|S )Nc                    sX   t   fdd}t  tjd fdd}dkrPdk	rLtd|S |S d S )	Nc                     s
    | |S N argskwargsfuncr   b/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/benchmark/benchmark_tf.pyrun_in_eager_mode5   s    zFrun_with_tf_optimizations.<locals>.run_func.<locals>.run_in_eager_mode)Zexperimental_compilec                     s
    | |S r   r   r   r   r   r   run_in_graph_mode9   s    zFrun_with_tf_optimizations.<locals>.run_func.<locals>.run_in_graph_modeTFzcCannot run model in XLA, if `args.eager_mode` is set to `True`. Please set `args.eager_mode=False`.)r   tffunction
ValueError)r   r    r!   r   r   r   run_func4   s    
z+run_with_tf_optimizations.<locals>.run_funcr   )r   r   r%   r   r   r   run_with_tf_optimizations3   s    r&   z	tf.Tensor)
batch_sizesequence_length
vocab_sizereturnc                    s:   t    fddt| | D }tj|| |ftjdS )Nc                    s   g | ]}  d d qS )r   r   )randint).0irngr)   r   r   
<listcomp>L   s     z$random_input_ids.<locals>.<listcomp>)shapeZdtype)randomRandomranger"   ZconstantZint32)r'   r(   r)   valuesr   r.   r   random_input_idsJ   s    r6   c                   @   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dd
dZee
e
eee g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 )TensorFlowBenchmarkr   ZconfigsZ
TensorFlow	frameworkc                 C   s   t jS r   )r"   __version__)selfr   r   r   framework_versionU   s    z%TensorFlowBenchmark.framework_version)
model_namer'   r(   r*   c                 C   s0   | j j}|d krtd| |||}| |S N@A device strategy has to be initialized before using TensorFlow.)r   strategyr$   _prepare_inference_func_measure_speedr:   r<   r'   r(   r?   
_inferencer   r   r   _inference_speedY   s
    z$TensorFlowBenchmark._inference_speedc                 C   s0   | j j}|d krtd| |||}| |S r=   )r   r?   r$   _prepare_train_funcrA   r:   r<   r'   r(   r?   _trainr   r   r   _train_speeda   s
    z TensorFlowBenchmark._train_speedc                 C   sT   | j jr$tjj| j j| j j d | j j}|d kr<t	d| 
|||}| |S NTr>   )r   is_gpur"   configexperimentalset_memory_growthgpu_list
device_idxr?   r$   r@   _measure_memoryrB   r   r   r   _inference_memoryh   s    z%TensorFlowBenchmark._inference_memoryc                 C   sT   | j jr$tjj| j j| j j d | j j}|d kr<t	d| 
|||}| |S rI   )r   rJ   r"   rK   rL   rM   rN   rO   r?   r$   rE   rP   rF   r   r   r   _train_memoryt   s    z!TensorFlowBenchmark._train_memoryNc                    s(  | j | }| jjrtdt|do<t|jto<t|jdk}| jj	s|rz2d|jd  }t
d|gd}t||}||W q tk
r   t| dY qX nt|j |t|dr|jn|jj}	t|||	 t| jj| jj fd	d
}
t| jj| jj fdd}|jr |
n|}|S )N+Mixed precision is currently not supported.architecturesr   TF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`.r)   c                      s     ddS )NF)decoder_input_idstrainingr   r   Z	input_idsmodelr   r   encoder_decoder_forward   s    zLTensorFlowBenchmark._prepare_inference_func.<locals>.encoder_decoder_forwardc                      s    ddS )NF)r[   r   r   r\   r   r   encoder_forward   s    zDTensorFlowBenchmark._prepare_inference_func.<locals>.encoder_forward)config_dictr   fp16NotImplementedErrorhasattr
isinstancerT   listlenonly_pretrain_model
__import__getattrImportErrorr   	__class__r)   encoderr6   r&   
eager_moder   is_encoder_decoder)r:   r<   r'   r(   rK   has_model_class_in_configmodel_classtransformers_module	model_clsr)   r^   r_   rC   r   r\   r   r@      s6    



z+TensorFlowBenchmark._prepare_inference_funcc                    s<  | j | }| jjdk	rtd| jjr.tdt|doPt|jt	oPt
|jdk}| jjs|rz2d|jd  }td|gd}t||}||W q tk
r   t| d	Y qX nt|j |t|d
r|jn|jj}	t|||	 t| jj| jj fdd}
t| jj| jj fdd}|jr4|
n|}|S )NFzWTraining cannot be done in eager mode. Please make sure that `args.eager_mode = False`.rS   rT   r   rU   rV   rW   rY   r)   c                     s&      ddd } t | j}|S )NT)rZ   labelsr[   r   r"   	gradientsZtrainable_variablesZlossru   r\   r   r   encoder_decoder_train   s    zFTensorFlowBenchmark._prepare_train_func.<locals>.encoder_decoder_trainc                     s$     ddd } t | j}|S )NT)rs   r[   r   rt   rv   r\   r   r   encoder_train   s    z>TensorFlowBenchmark._prepare_train_func.<locals>.encoder_train)r`   r   rm   r$   ra   rb   rc   rd   rT   re   rf   rg   rh   ri   rj   r   rk   r)   rl   r6   r&   r   rn   )r:   r<   r'   r(   rK   ro   rp   rq   rr   r)   rw   rx   rG   r   r\   r   rE      s:    



z'TensorFlowBenchmark._prepare_train_func)r*   c                 C   s   | j j  zX| j js | j jr:td tj|ddd tj|| j jdd}t	|d W W  5 Q R  S  t
k
r } z| d|  W 5 d }~X Y nX W 5 Q R X d S )NzCDo inference on TPU. Running model 5 times to stabilize compilationr      )repeatnumber
   g      $@Doesn't fit on GPU. )r   r?   scopeis_tpur   loggerinfotimeitrz   minr   print_fn)r:   r   Zruntimeser   r   r   rA      s    
z"TensorFlowBenchmark._measure_speed)r   r*   c           
      C   s|  t d | jj Z z| jjr>| jjs6tdtd}| jj	rPt
dn| jjrt snt d d}qt d t  |  t| jj}t|}|j}t|}t  n6| jjrt d d }nt|}t|trt|n|}| jjrt|}|d kr|j}nd }||fW W  5 Q R  S  tk
rl }	 z$| d	|	  W Y W 5 Q R  d
S d }	~	X Y nX W 5 Q R X d S )NzNote that TensorFlow allocates more memory than it might need to speed up computation. The memory reported here corresponds to the memory reported by `nvidia-smi`, which can vary depending on total available memory on the GPU that is used.zu`args.eager_mode` is set to `False`. Make sure to run model in eager mode to measure memory consumption line by line.rV   zuMemory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking with `args.memory=False`zypy3nvml not installed, we won't log GPU memory usage. Install py3nvml (pip install py3nvml) to log information about GPU.N/AzlMeasuring total GPU usage on GPU device. Make sure to not have additional processes running on the same GPU.z\When enabling line by line tracing, the max peak memory for CPU is inaccurate in TensorFlow.r}   )r   N)r   r   r   r?   r~   Ztrace_memory_line_by_linerm   r$   r   r   rb   rJ   r	   warningnvmlZnvmlInitZnvmlDeviceGetHandleByIndexrO   ZnvmlDeviceGetMemoryInfousedr   ZnvmlShutdownr   rd   intr   totalr   r   )
r:   r   traceZmemoryhandleZmeminfoZmax_bytes_in_useZmemory_bytessummaryr   r   r   r   rP      s^    



z#TensorFlowBenchmark._measure_memory)__name__
__module____qualname__r   __annotations__r   r8   strpropertyr;   r   floatrD   rH   r   r   r   rQ   rR   r   r@   rE   rA   rP   r   r   r   r   r7   P   s*   

  
  
)0r7   ))__doc__r2   r   	functoolsr   typingr   r   Zconfiguration_utilsr   Zmodels.auto.modeling_tf_autor   r   utilsr	   r
   r   Zbenchmark_utilsr   r   r   r   r   r   Z
tensorflowr"   Z'tensorflow.python.framework.errors_implr   Zbenchmark_args_tfr   Zpy3nvml.py3nvmlZpy3nvmlr   Z
get_loggerr   r   boolr&   r   r6   r7   r   r   r   r   <module>   s$    

