
    g3                     @   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            rddlZddlmZ d	dlmZ  e            rddlmZ   ej!        e"          Z#de$de$fdZ%de&de&de&ddgfdZ' G d de          Z(dS )z@
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                       fd}|S )Nc                      t                      fd            }t                     t          j                   fd                        }du rdurt          d          |S |S )Nc                       | i |S N argskwargsfuncs     _/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/benchmark/benchmark_tf.pyrun_in_eager_modezFrun_with_tf_optimizations.<locals>.run_func.<locals>.run_in_eager_mode4   s    4((((    )experimental_compilec                       | i |S r   r   r   s     r!   run_in_graph_modezFrun_with_tf_optimizations.<locals>.run_func.<locals>.run_in_graph_mode8   s     4((((r#   TFzcCannot 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   s   `  r!   run_funcz+run_with_tf_optimizations.<locals>.run_func3   s    	t	) 	) 	) 	) 
	) 
t	'	2	2	2	) 	) 	) 	) 
3	2 
	) D  e## y   %$$$r#   r   )r   r   r*   s   `` r!   run_with_tf_optimizationsr+   2   s)    % % % % % %& Or#   
batch_sizesequence_length
vocab_sizereturnz	tf.Tensorc                     t          j                    fdt          | |z            D             }t          j        || |ft          j                  S )Nc                 B    g | ]}                     d dz
            S )r   r   )randint).0irngr.   s     r!   
<listcomp>z$random_input_ids.<locals>.<listcomp>K   s+    ZZZckk!Z!^,,ZZZr#   )shapedtype)randomRandomranger'   constantint32)r,   r-   r.   valuesr5   s     ` @r!   random_input_idsr?   I   sV    
-//CZZZZZeJ<X6Y6YZZZF;vj/%B"(SSSSr#   c            
       H   e Zd ZU eed<   eed<   dZeed<   ed             Z	dede
de
d	efd
Zdede
de
d	efdZdede
de
d	eee         gfdZdede
de
d	eee         gfdZdede
de
d	eg df         fdZdede
de
d	eg df         fdZd	efdZdeg df         d	eegfdZdS )TensorFlowBenchmarkr   configs
TensorFlow	frameworkc                     t           j        S r   )r'   __version__)selfs    r!   framework_versionz%TensorFlowBenchmark.framework_versionT   s
    ~r#   
model_namer,   r-   r/   c                     | j         j        }|t          d          |                     |||          }|                     |          S N@A device strategy has to be initialized before using TensorFlow.)r   strategyr)   _prepare_inference_func_measure_speedrG   rI   r,   r-   rM   
_inferences         r!   _inference_speedz$TensorFlowBenchmark._inference_speedX   sL    9%_```11*j/ZZ
"":...r#   c                     | j         j        }|t          d          |                     |||          }|                     |          S rK   )r   rM   r)   _prepare_train_funcrO   rG   rI   r,   r-   rM   _trains         r!   _train_speedz TensorFlowBenchmark._train_speed`   sL    9%_```))*j/RR""6***r#   c                 *   | j         j        r?t          j        j                            | j         j        | j         j                 d           | j         j        }|t          d          | 
                    |||          }|                     |          S NTrL   )r   is_gpur'   configexperimentalset_memory_growthgpu_list
device_idxrM   r)   rN   _measure_memoryrP   s         r!   _inference_memoryz%TensorFlowBenchmark._inference_memoryg   s     9 	eI"44TY5G	H\5]_cddd9%_```11*j/ZZ
##J///r#   c                 *   | j         j        r?t          j        j                            | j         j        | j         j                 d           | j         j        }|t          d          | 
                    |||          }|                     |          S rY   )r   rZ   r'   r[   r\   r]   r^   r_   rM   r)   rT   r`   rU   s         r!   _train_memoryz!TensorFlowBenchmark._train_memorys   s     9 	eI"44TY5G	H\5]_cddd9%_```))*j/RR##F+++r#   Nc                 *   | j         |         }| j        j        rt          d          t	          |d          o1t          |j        t                    ot          |j                  dk    }| j        j	        sa|r_	 d|j        d         z   }t          d|g          }t          ||          } ||          n:# t          $ r t          | d          w xY wt          |j                 |          t	          |d          r|j        n|j        j        }	t#          |||	          t%          | j        j        | j        j                  fd	            }
t%          | j        j        | j        j                  f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                          d          S )NF)decoder_input_idstrainingr   	input_idsmodels   r!   encoder_decoder_forwardzLTensorFlowBenchmark._prepare_inference_func.<locals>.encoder_decoder_forward   s    5i%PPPPr#   c                        d          S )NF)rn   r   ro   s   r!   encoder_forwardzDTensorFlowBenchmark._prepare_inference_func.<locals>.encoder_forward   s    5U3333r#   )config_dictr   fp16NotImplementedErrorhasattr
isinstancerf   listlenonly_pretrain_model
__import__getattrImportErrorr   	__class__r.   encoderr?   r+   
eager_moder   is_encoder_decoder)rG   rI   r,   r-   r[   has_model_class_in_configmodel_classtransformers_module	model_clsr.   rr   rt   rQ   rp   rq   s                @@r!   rN   z+TensorFlowBenchmark._prepare_inference_func   s   !*-9> 	U%&STTT FO,, .6/66.F())A- 	"
 y, 	?1J 	?	"V%9!%<<&0;-&X&X&X##$7EE	!	&))   !" W W W   %V%56v>>E +2&,*G*GfV&&V^Mf
$Z*MM		"49#79J	K	K	Q 	Q 	Q 	Q 	Q 
L	K	Q 
#49#79J	K	K	4 	4 	4 	4 	4 
L	K	4 170I^,,
s   <=B: :Cc                 d   | j         |         }| j        j        durt          d          | j        j        rt          d          t          |d          o1t          |j        t                    ot          |j                  dk    }| j        j        sa|r_	 d|j        d         z   }t          d|g          }t          ||          } ||          n:# t          $ r t          | d	          w xY wt          |j                 |          t          |d
          r|j        n|j        j        }	t'          |||	          t)          | j        j        | j        j                  fd            }
t)          | j        j        | j        j                  fd            }|j        r|
n|}|S )NFzWTraining cannot be done in eager mode. Please make sure that `args.eager_mode = False`.re   rf   r   rg   rh   ri   rk   r.   c                  f     d          d         } t          j        | j                  }|S )NT)rm   labelsrn   r   r'   	gradientstrainable_variableslossr   rp   rq   s     r!   encoder_decoder_trainzFTensorFlowBenchmark._prepare_train_func.<locals>.encoder_decoder_train   s<    5i	\`aaabcdDT5+DEEIr#   c                  d     d          d         } t          j        | j                  }|S )NT)r   rn   r   r   r   s     r!   encoder_trainz>TensorFlowBenchmark._prepare_train_func.<locals>.encoder_train   s8    59tDDDQGDT5+DEEIr#   )ru   r   r   r)   rv   rw   rx   ry   rf   rz   r{   r|   r}   r~   r   r	   r   r.   r   r?   r+   r   r   )rG   rI   r,   r-   r[   r   r   r   r   r.   r   r   rV   rp   rq   s                @@r!   rT   z'TensorFlowBenchmark._prepare_train_func   s   !*-9u,,vwww9> 	U%&STTT FO,, .6/66.F())A- 	"
 y, 	L1J 	L	"V%9!%<<&0;-&X&X&X##$7EE	!	&))   !" W W W   2&2BCFKKE +2&,*G*GfV&&V^Mf
$Z*MM		"49#79J	K	K	 	 	 	 	 
L	K	
 
#49#79J	K	K	 	 	 	 	 
L	K	
 +1*CV&&s   =C C4c                    | j         j                                        5  	 | j         j        s| j         j        r1t
                              d           t          j        |dd           t          j        || j         j        d          }t          |          dz  cd d d            S # t          $ r"}|                     d|            Y d }~nd }~ww xY w	 d d d            d S # 1 swxY w Y   d S )NzCDo inference on TPU. Running model 5 times to stabilize compilationr      )repeatnumber
   g      $@Doesn't fit on GPU. )r   rM   scopeis_tpur   loggerinfotimeitr   minr   print_fn)rG   r    runtimeses       r!   rO   z"TensorFlowBenchmark._measure_speed   sq   Y%%'' 	: 	::9# <ty'8 <KK efffM$q;;;; "=9+   8}}t+	: 	: 	: 	: 	: 	: 	: 	: * : : :8Q8899999999:9!	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:s5   C&A;B))
C3CC&CC&&C*-C*r    c                 f   t                               d           | j        j                                        5  	 | j        j        r*| j        j        st          d          t          d          }| j        j	        rt          d          | j        j        rt                      st                               d           d}nt                               d           t          j                      |             t          j        | j        j                  }t          j        |          }|j        }t)          |          }t          j                     n^| j        j        rt                               d           d }n5t-          |          }t/          |t0                    rt)          |          n|}| j        j        rt3          |          }||j        }nd }||fcd d d            S # t6          $ r.}	|                     d	|	            Y d }	~	d d d            d
S d }	~	ww xY w# 1 swxY w Y   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.rh   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   rM   r   trace_memory_line_by_liner   r)   r   r   rw   rZ   r
   warningnvmlnvmlInitnvmlDeviceGetHandleByIndexr_   nvmlDeviceGetMemoryInfousedr   nvmlShutdownr   ry   intr   totalr   r   )
rG   r    tracememoryhandlememinfomax_bytes_in_usememory_bytessummaryr   s
             r!   r`   z#TensorFlowBenchmark._measure_memory   s   A	
 	
 	
 Y%%'' ;	# ;	#:#96 A9/ (9   1@@E9# &i-4   Y%  i/11 ,b   "'8  
 !%!@AU!V!V"&">v"F"F+2<(!'(8!9!9)++++ y: i+   "&'>t'D'D9CLRU9V9V!h!5!5!5\h96 #1%88G~!("Gwq;	# ;	# ;	# ;	# ;	# ;	# ;	# ;	#r * # # #8Q88999"{{{w;	# ;	# ;	# ;	# ;	# ;	# ;	# ;	#r#s;	# ;	# ;	# ;	# ;	# ;	# ;	# ;	# ;	# ;	#s5   H&F#G++
H#5HH&H##H&&H*-H*)__name__
__module____qualname__r   __annotations__r   rD   strpropertyrH   r   floatrR   rW   r   r   r   ra   rc   r   rN   rT   rO   r`   r   r#   r!   rA   rA   O   s        
&&&&!Is!!!  X/3 /C /RU /Z_ / / / /+s + +c +V[ + + + +
0
0+.
0AD
0
(=)	*
0 
0 
0 
0
,
,+.
,AD
,
(=)	*
, 
, 
, 
,'# '3 'Y\ 'aijlnrjras ' ' ' 'R.c .s .UX .]efhjnfn]o . . . .`:e : : : :&C#HRX$6 C#FM;R C# C# C# C# C# C#r#   rA   ))__doc__r9   r   	functoolsr   typingr   r   configuration_utilsr   models.auto.modeling_tf_autor   r	   utilsr
   r   r   benchmark_utilsr   r   r   r   r   r   
tensorflowr'   'tensorflow.python.framework.errors_implr   benchmark_args_tfr   py3nvml.py3nvmlpy3nvmlr   
get_loggerr   r   boolr+   r   r?   rA   r   r#   r!   <module>r      s             % % % % % % % % 2 2 2 2 2 2 Z Z Z Z Z Z Z Z B B B B B B B B B B                ? @NNNNNN?????? #""""""		H	%	%T D    .T Ts T TQ\P] T T T T_# _# _# _# _#) _# _# _# _# _#r#   