Ë
    S²jû  ã                   ó$  — d dl mZmZ d dlmZ d dlZd dlZd dlmZ d dl	m
Z
 ddgZda ej                  «       Z	 d d	lmZ d
Ze�r3dd„Zdd„Z	 	 	 ddej*                  j,                  ez  dej.                  ez  dedej2                  j4                  dz  dedz  f
d„Z	 	 	 	 	 ddej*                  j,                  ez  dej.                  ez  dededz  dedz  dej2                  j4                  dz  dej*                  j,                  ez  dz  fd„Z	 	 	 ddej*                  j,                  ez  dej.                  ez  dedej2                  j4                  dz  dej*                  j,                  ez  dz  f
d„Zyy# e$ r dZdZ ed«       Y �ŒKw xY w)é    )ÚAnyÚcast)ÚCallableN)ÚCompileCounterWithBackend)ÚTimerÚ	bench_allÚbenchmark_compileF)ÚtabulateTzJtabulate is not installed, please pip install tabulate to use this utilityc                  óP  — t         j                  j                  «       rˆt         j                  j                  j                  j
                  du rWt         j                  j                  «       dk\  r5t        j                  d«       t        st        d«       t        d«       day y y y y )NF)é   r   ÚhighzYour GPU supports tensor coreszWwe will enable it automatically by setting `torch.set_float32_matmul_precision('high')`T)
ÚtorchÚcudaÚis_availableÚbackendsÚmatmulÚ
allow_tf32Úget_device_capabilityÚset_float32_matmul_precisionÚ_warned_tensor_coresÚprint© ó    úb/var/www/html/ia-enhanced/venv/lib/python3.12/site-packages/torch/utils/benchmark/utils/compile.pyÚ_enable_tensor_coresr      s„   € ô �:‰:×"Ñ"Ô$Ü�~‰~×"Ñ"×)Ñ)×4Ñ4¸Ñ=Ä%Ç*Á*×BbÑBbÓBdÐhnÒBnÜ×2Ñ2°6Ô:Ý+ÜÐ:Ô;ÜÐsÔtØ+/Ñ(ð ,ð CoÐ=ð %r   c                  ó6   — t        j                  t        «       y )N)r   r   Ú_default_float_32_precisionr   r   r   Ú_disable_tensor_coresr   '   s   € Ü×*Ñ*Ô+FÕGr   ÚmodelÚsample_inputÚ	num_itersÚ	optimizerÚloss_fnc                 ó’   — |r|rd}nd}t        || |||dœ¬«      }|j                  |¬«      }|j                  dz  }t        |d«      S )Nz©
    output = model(sample_input)
    loss = loss_fn(output) if loss_fn else output.sum()
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()
            zmodel(sample_input))r   r    r"   r#   )ÚstmtÚglobals)Únumberiè  é   )r   ÚtimeitÚmeanÚround)	r   r    r!   r"   r#   r%   ÚtimerÚresultÚavg_times	            r   Ú
bench_loopr/   *   sc   € ñ ™ð‰Dð )ˆDô ØØ#°\ÐPYÐfmÑnô
ˆð —‘ Y�Ó/ˆð —;‘; Ñ%ˆÜ�X˜qÓ!Ð!r   ÚbackendÚmodec                 ó  — |r•	 t         j                  j                  «        t        |«      }t        j                  | ||¬«      }t        ||d||«      }	t        |||||«      }
|j                  dk(  rt        d«      ‚|j                  dkD  rt        d«      ‚n| }d}	t        |||||«      }
|	rt        |	d
«      nd}	|
rt        |
d
«      nd}
|	|
fS # t        $ r&}t        |«       t        d|› d|› �«       Y d}~y	d}~ww xY w)z=
        Use this utility to benchmark torch.compile
        )r0   r1   é   r   z,No compilation occurred during benchmarking.z+Recompilation occurred during benchmarking.zFailed to compile z with mode N)NNr(   )r   Ú_dynamoÚresetr   Úcompiler/   Úframe_countÚRuntimeErrorÚ	Exceptionr   r+   )r   r    r!   r0   r1   r"   r#   Úcompile_counter_with_backendÚ	opt_modelÚcompilation_timeÚrunning_timeÚes               r   r	   r	   L   s  € ñ ð"Ü—‘×#Ñ#Ô%Ü/HÈÓ/QÐ,Ü!ŸM™M¨%Ð9UÐ\`Ôa�	ô $.¨i¸ÀqÈ)ÐU\Ó#]Ð ä)¨)°\À9ÈiÐY`Óa�à/×;Ñ;¸qÒ@Ü&Ð'UÓVÐVà/×;Ñ;¸aÒ?Ü&Ð'TÓUÐUð @ð ˆIØ#ÐÜ% i°¸yÈ)ÐU\Ó]ˆLá9Iœ5Ð!1°1Ô5ÈtÐÙ1=”u˜\¨1Ô-À4ˆð   Ð-Ð-øô ò "Ü�a”ÜÐ*¨7¨)°;¸t¸fÐEÔFÜ!ûð"ús   „BC Ã	C>ÃC9Ã9C>c                 ó¸  — g d¢}g }d}t         j                  j                  «        t        | ||dd|«      \  }}|j	                  |rdndddd|› d�g«       t         j                  j                  «       D �]w  }	|	dk(  �rt        t        t        dz     t        t         j                  j                  «       j                  «       «      «      dgz   }
|
D ]¼  }|d	k(  rŒ	t         j                  j                  «        	 t         j                  j                  «       r
t        «        t        | |||	|||«      \  }}t         j                  j                  «       sŒƒt        «        |j	                  |rdnd|	r|	nd|�|nd|r|› d
�nd|r|› d
�ndg«       Œ¾ �Œt         j                  j                  «        t        | |||	d||«      \  }}|€�ŒT|j	                  |rdnd|	d|› d
�xs d|› d
�g«       �Œz t!        ||d¬«      S # t         j                  j                  «       r;t        «        |j	                  |rdnd|	r|	nd|�|ndr|› d
�ndr|› d
�ndg«       w w xY w)a�  
        This is a simple utility that can be used to benchmark torch.compile
        In particular it ensures that your GPU is setup to use tensor cores if it supports its
        It also tries out all the main backends and prints a table of results so you can easily compare them all
        Many of the backendds have their own optional dependencies so please pip install them separately

        You will get one table for inference and another for training
        If you'd like to leverage this utility for training make sure to pass in a torch.optim.Optimizer

        The important warnings are
        Your GPU supports tensor cores
        we will enable it automatically by setting `torch.set_float32_matmul_precision('high')`

        If a compilation fails for any reason including the dependency not being included
        then we will print Failed to compile {backend} with mode {mode}
        )zTrain/InferenceÚBackendÚModezCompilation TimezAverage Running TimeNÚTrainingÚ	InferenceÚEagerú-z msÚinductorÚdefaultz ms Úgithub)ÚheadersÚtablefmt)r   r4   r5   r	   ÚappendÚlist_backendsr   ÚlistÚstrÚ	_inductorÚlist_mode_optionsÚkeysr   r   r   r   r
   )r   r    r!   r"   r#   Úfield_namesÚtableÚ
eager_timeÚ_r0   Úmode_optionsr1   r<   r=   s                 r   r   r   y   su  € ò. iˆØˆð ˆ
Ü�‰×ÑÔÜ)¨%°¸yÈ$ÐPTÐV_Ó`‰ˆˆ:Ø�‰Ù%‰j¨;¸À#ÀsÈzÈlÐZ]ÐL^Ð_ô	
ô —}‘}×2Ñ2Ó4ó $	ˆGà˜*Ó$Ü#¤D¬¨t©Ñ$4´d¼5¿?¹?×;\Ñ;\Ó;^×;cÑ;cÓ;eÓ6fÓgÐkoÐjpÑp�Ø(ò �DØ˜yÒ(Ø Ü—M‘M×'Ñ'Ô)ðÜ Ÿ:™:×2Ñ2Ô4Ü0Ô2Ü9JØ! <°¸GÀTÈ9ÐV]ó:_Ñ6Ð(¨,ô !Ÿ:™:×2Ñ2Õ4Ü1Ô3Ø!ŸL™LÙ/8¡¸ká+2¡¸Ø(,Ð(8¡¸cÙ=MÐ#3Ð"4°DÑ 9ÐSVÙ9E < .°Ñ 5È3ð*õ òô, —‘×#Ñ#Ô%Ü1BØ˜<¨°G¸TÀ9Ègó2WÑ.Ð  ,ð  Ò+Ø—L‘LÙ'0™°kØ Ø+Ð,¨DÐ1Ò8°SØ'˜.¨Ð-ð	"ö ð?$	ôP ˜ {¸XÔFÐFøô5 !Ÿ:™:×2Ñ2Ô4Ü1Ô3Ø!ŸL™LÙ/8¡¸ká+2¡¸Ø(,Ð(8¡¸cÙ=MÐ#3Ð"4°DÑ 9ÐSVÙ9E < .°Ñ 5È3ð*õ ð 5ús   Ã:<G>Ç>AI)ÚreturnN)é   NN)rX   NrG   NN)Útypingr   r   Úcollections.abcr   r   Útorch._dynamoÚtorch._dynamo.testingr   Útorch.utils.benchmarkr   Ú__all__r   Úget_float32_matmul_precisionr   r
   ÚHAS_TABULATEÚModuleNotFoundErrorr   r   r   ÚnnÚModuleÚTensorÚintÚoptimÚ	Optimizerr/   rN   r	   r   r   r   r   ú<module>rh      s  ðç Ý $ã Û Ý ;Ý 'ð Ð+Ð
,€ð Ð Ø@˜e×@Ñ@ÓBÐ ðXå!à€Lò ó	0óHð Ø26Ø#'ñ "Ø�x‰x�‰ Ñ)ð "à—l‘l SÑ(ð "ð ð "ð —;‘;×(Ñ(¨4Ñ/ð	 "ð
 ˜D‘ó "ðJ Ø"Ø$Ø26Ø6:ñ*.Ø�x‰x�‰ Ñ)ð*.à—l‘l SÑ(ð*.ð ð*.ð �t‘ð	*.ð
 �D‰jð*.ð —;‘;×(Ñ(¨4Ñ/ð*.ð —(‘(—/‘/ HÑ,¨tÑ3ó*.ð` Ø26Ø6:ñJGØ—‘—‘ (Ñ*ðJGà—l‘l SÑ(ðJGð ðJGð —;‘;×(Ñ(¨4Ñ/ð	JGð
 —(‘(—/‘/ HÑ,¨tÑ3ôJGð} øð ò XØ€LØ€HÙ	Ð
V×WðXús   »E: Å:FÆF