
    sg)                         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       r
ddlZdd
lmZ  e       rddlmZ  ej6                  e      Z G d de      Zy)z@
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            
       "   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ee   g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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y)PyTorchBenchmarkargsconfigsPyTorch	frameworkc                 "    t         j                  S N)torch__version__)selfs    S/var/www/html/venv/lib/python3.12/site-packages/transformers/benchmark/benchmark.pyframework_versionz"PyTorchBenchmark.framework_version6   s           
model_name
batch_sizesequence_lengthreturnc                 J    | j                  |||      }| j                  |      S r   )_prepare_inference_func_measure_speedr   r"   r#   r$   
_inferences        r   _inference_speedz!PyTorchBenchmark._inference_speed:   s'    11*j/Z
"":..r!   c                 J    | j                  |||      }| j                  |      S r   )r'   _measure_memoryr)   s        r   _inference_memoryz"PyTorchBenchmark._inference_memory>   s)     11*j/Z
##J//r!   c                 J    | j                  |||      }| j                  |      S r   )_prepare_train_funcr(   r   r"   r#   r$   _trains        r   _train_speedzPyTorchBenchmark._train_speedD   s'    ))*j/R""6**r!   c                 J    | j                  |||      }| j                  |      S r   )r0   r-   r1   s        r   _train_memoryzPyTorchBenchmark._train_memoryH   s)     ))*j/R##F++r!   Nc                    | j                   |   }| j                  j                  rd|_        t        |d      xr4 t	        |j
                  t              xr t        |j
                        dkD  }| j                  j                  s5|r3	 |j
                  d   }t        d|g      }t        ||      } ||      }	nt        |j                     |      }	|	j                          |	j                  | j                  j                          t        |d      r|j"                  n|j$                  j"                  }
t'        j(                  |
||ft&        j*                  | j                  j                         | j                  j,                  rFt.        j1                  d	       | j                  j2                  st5        d
      |	j7                          | j                  j                  r>t'        j8                         5  t&        j:                  j=                  |	      d d d        n|	fd}fd}|j>                  r|}|S |}|S # t        $ r t         d      w xY w# 1 sw Y   DxY w)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dtypedevice&Running training in Mixed Precision...)Mixed precision is possible only for GPU.c                  p    t        j                         5         } d d d        | S # 1 sw Y    S xY w)N)decoder_input_idsr   no_gradoutputsinference_model	input_idss    r   encoder_decoder_forwardzIPyTorchBenchmark._prepare_inference_func.<locals>.encoder_decoder_forward|   s5     R))yQRNRNs   +5c                  l    t        j                         5         } d d d        | S # 1 sw Y    S xY wr   rD   rF   s    r   encoder_forwardzAPyTorchBenchmark._prepare_inference_func.<locals>.encoder_forward   s0     5))45N5Ns   	)3) config_dictr   torchscripthasattr
isinstancer7   listlenonly_pretrain_model
__import__getattrImportErrorr   	__class__evaltor?   r<   encoderr   randintlongfp16loggerinfois_gpu
ValueErrorhalfrE   jittraceis_encoder_decoder)r   r"   r#   r$   confighas_model_class_in_configmodel_classtransformers_module	model_clsmodelr<   rJ   rL   _forwardrH   rI   s                 @@r   r'   z(PyTorchBenchmark._prepare_inference_funcN   s!   !!*-99  !%F FO, .6//6.F(()A- 	"
 yy,,1J	$2215&0;-&X##$7E	!&) "&"2"23F;E

!!" +2&,*GV&&V^^MfMf
MM*z?.KSXS]S]fjfofofvfvw	99>>KK@A99## !LMM JJL99   D"'))//%"CD D $O	
	
 /5.G.G* N]Q  !"m $W W 0D Ds   
1I :!I!I!I*c                 "   | j                   |   }t        |d      xr4 t        |j                  t              xr t        |j                        dkD  }| j                  j                  s5|r3	 |j                  d   }t        d|g      }t        ||      } ||      }	nt        |j                     |      }	| j                  j                  rt        d      |	|	j                          |	j!                  | j                  j"                         t        |d      r|j$                  n|j&                  j$                  }
t)        j*                  |
||ft(        j,                  | j                  j"                        | j                  j.                  rFt0        j3                  d	       | j                  j4                  st7        d
      |	j9                          fd}fd}|j:                  r|}|S |}|S # t        $ r t         d      w xY w)Nr7   r   r8   r9   r;   z5Training for torchscript is currently not implementedr<   r=   r@   rA   c                  B           d   } | j                          | S )N)labelsr   backwardlossrI   train_models    r   !compute_loss_and_backprob_encoderzOPyTorchBenchmark._prepare_train_func.<locals>.compute_loss_and_backprob_encoder   s"    y;A>DMMOKr!   c                  D           d   } | j                          | S )N)rC   ro   r   rp   rr   s    r   )compute_loss_and_backprob_encoder_decoderzWPyTorchBenchmark._prepare_train_func.<locals>.compute_loss_and_backprob_encoder_decoder   s%    yIiXYZ[DMMOKr!   )rM   rO   rP   r7   rQ   rR   r   rS   rT   rU   rV   r   rW   rN   NotImplementedErrortrainrY   r?   r<   rZ   r   r[   r\   r]   r^   r_   r`   ra   rb   re   )r   r"   r#   r$   rf   rg   rh   ri   rj   rk   r<   ru   rw   r2   rI   rt   s                 @@r   r0   z$PyTorchBenchmark._prepare_train_func   s   !!*- FO, .6//6.F(()A- 	"
 yy,,1J	$2215&0;-&X##$7E	!&) /v/?/?@HE99  %&]^^K!!" +2&,*GV&&V^^MfMf
MM*z?.KSXS]S]fjfofofvfvw	99>>KK@A99## !LMM JJL	
	 (( 6 	
  3 	
 Y  !"m $W W s   -1G6 6Hc                 &   	 | j                   j                  s| j                   j                  r-t        j	                  d       t        j                  |dd       t        j                  || j                   j                  d      }| j                   j                  r>| j                   j                  r(dd lm	c m
} | j                  |j                                t        |      dz  S # t        $ r}| j                  d|        Y d }~y	d }~ww xY w)
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_tpurN   r^   r_   timeitr|   torch_xla_tpu_print_metricstorch_xla.debug.metricsdebugmetricsprint_fnmetrics_reportminRuntimeError)r   funcruntimesmetes        r   r(   zPyTorchBenchmark._measure_speed   s    	yy499#8#8pq }}yy''H yyDII$I$I55c0023x=4'' 	MM045	s   C&C) )	D2DDr   c                     	 | j                   j                  rt        d      }| j                   j                  rt	        d      | j                   j
                  rt               st        j                  d       d}nt        j                  d       t        j                           |        t        j                  | j                   j                        }t        j                  |      }|j                  }t!        |      }t        j"                          n(t%        |      }t'        |t(              rt!        |      n|}| j                   j                  rt+              }nd }||fS # t,        $ r}	| j/                  d|	        Y d }	~	yd }	~	ww xY w)Nr8   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.r   zlMeasuring total GPU usage on GPU device. Make sure to not have additional processes running on the same GPU.r   )r   N)r   trace_memory_line_by_liner   r   rx   r`   r	   r^   warningr_   nvmlnvmlInitnvmlDeviceGetHandleByIndex
device_idxnvmlDeviceGetMemoryInfousedr   nvmlShutdownr   rP   intr   r   r   )
r   r   rd   memoryhandlememinfomax_bytes_in_usememory_bytessummaryr   s
             r   r-   z PyTorchBenchmark._measure_memory   sF   ,	yy22,^<yy)<  !!+-NN^ #FKK,
 MMOF!<<TYY=Q=QRF"::6BG'.||$#$45F%%'  7t<1;L#1N-T`yy22-e47?" 	MM045	s   E#E& &	F/FF)__name__
__module____qualname__r   __annotations__r   r   strpropertyr    r   floatr+   r   r   r   r.   r3   r5   r   r'   r0   r(   r-    r!   r   r   r   1   sZ   
##Is! !/3 /C /RU /Z_ /00+.0AD0
(=)	*0+s + +c +V[ +,,+.,AD,
(=)	*,9# 93 9Y\ 9aijlnrjras 9v:c :s :UX :]efhjnfn]o :xe 8-HRX$6 -FM;R -r!   r   )__doc__r   typingr   r   configuration_utilsr   models.auto.modeling_autor   r   utilsr	   r
   r   benchmark_utilsr   r   r   r   r   r   r   benchmark_argsr   py3nvml.py3nvmlpy3nvmlr   
get_loggerr   r^   r   r   r!   r   <module>r      sc      % 2 Q E E  9 " 
		H	%]y ]r!   