Ë
    g^(hÐ+  ã                   óº  — d dl Z d dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZmZmZmZ d dlZd dlmZmZ d dlmZ ej.                  j1                  ed	«      Zexr ej6                  j9                  «       Zd
Z ed«      Z ed«      Zdeee
ef   ef   deee
ef   ef   fd„Z  G d„ d«      Z! G d„ de!«      Z" G d„ de"«      Z#er e#«       Z$y e"«       Z$y)é    N)Úcached_propertyÚwraps)Úchain)Úmedian)ÚAnyÚCallable)ÚConcatenateÚ	ParamSpecÚSelfÚTypeVar)ÚcountersÚdynamo_timed)Úuse_experimental_benchmarkerÚbenchmarkingiè  ÚPÚTÚfnÚreturnc           	      óˆ   ‡ — t        ‰ «      dt        dt        j                  dt        j                  dt
        fˆ fd„«       }|S )zðWraps `fn` with `dynamo_timed` context, and increments the appropriate dynamo
    counters. It is expected that `fn` is a method of `Benchmarker` or one of its
    subclasses; typing limitations prevent us from declaring this directly.
    ÚselfÚargsÚkwargsr   c                 óÚ   •— | j                   j                  › d‰j                  › �}t        d   d|› �xx   dz  cc<   t        |d¬«      5   ‰| g|¢­i |¤Žcd d d «       S # 1 sw Y   y xY w)Nú.Úinductorzbenchmarking.é   T)Úlog_pt2_compile_event)Ú	__class__Ú__name__r   r   )r   r   r   Úfn_qual_namer   s       €úb/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/_inductor/runtime/benchmarking.pyÚwrapperztime_and_count.<locals>.wrapper"   so   ø€ àŸ.™.×1Ñ1Ð2°!°B·K±K°=ÐAˆÜ�Ñ˜}¨\¨NÐ;Ó<ÀÑAÓ<Ü˜,¸dÔCñ 	-Ù�dÐ,˜TÒ, VÑ,÷	-÷ 	-ò 	-ús   ÁA!Á!A*)r   r   r   r   r   r   )r   r"   s   ` r!   Útime_and_countr#      sF   ø€ ô ˆ2ƒYð-”cð -¤!§&¡&ð -´A·H±Hð -Äô -ó ð-ð €Nó    c                   óº   — e Zd Zdeddfd„Zedededef   deedf   de	e
ef   d	edefd
„«       Ze	 ddedeg ef   dededef
d„«       Zededed	edefd„«       Zy)ÚBenchmarkerr   r   Nc                  ó   — y ©N© )r   s    r!   Ú__init__zBenchmarker.__init__-   s   € Ør$   r   .Úfn_argsÚ	fn_kwargsr   c                 óv  ‡‡‡— d}t        ‰‰j                  «       «      D ]F  }t        |t        j                  «      sŒ|€|j
                  }Œ-|j
                  |k7  sŒ=t        d«      ‚ |€t        d«      ‚ˆˆˆfd„}|t        j
                  d«      k(  r | j                  |fi |¤ŽS  | j                  |fi |¤ŽS )a  Benchmark `fn(*fn_args, *fn_kwargs)` and return the runtime, in milliseconds (the
        actual runtime calculation is dictated by the benchmarking implementation, but may be
        one of [mean, median, minimum, etc.]). Functions as a convenience wrapper around
        device-specific implementations, like `benchmark_cpu` and `benchmark_gpu`. Raises
        `ValueError(...)` if we can't safely infer the device type of `fn`; for example,
        if multiple device types are found in `fn_args` and `fn_kwargs`, or if no device
        types are found.

        Arguments:
        - fn: The function to benchmark.
        - fn_args: The function's arguments.
        - fn_kwargs: The function's kwargs.

        Keyword Arguments:
        - **kwargs: The benchmarking implementation's kwargs.

        Returns:
        - The runtime of `fn(*fn_args, **fn_kwargs)`, in milliseconds.
        NzcCan't safely infer the device type of `fn` with multiple device types in `fn_args` and `fn_kwargs`!z¡Can't safely infer the device type of `fn` with no device types in `fn_args` or `fn_kwargs`! You should be calling `.benchmark_cpu` or `.benchmark_gpu` directly.c                  ó   •—  ‰ ‰i ‰¤ŽS r(   r)   )r   r+   r,   s   €€€r!   ú<lambda>z'Benchmarker.benchmark.<locals>.<lambda>Y   s   ø€ ™B Ð5¨9Ñ5€ r$   Úcpu)	r   ÚvaluesÚ
isinstanceÚtorchÚTensorÚdeviceÚ
ValueErrorÚbenchmark_cpuÚbenchmark_gpu)r   r   r+   r,   r   Úinferred_deviceÚarg_or_kwargÚ	_callables    ```    r!   Ú	benchmarkzBenchmarker.benchmark0   sÎ   ú€ ð6 ˆÜ! '¨9×+;Ñ+;Ó+=Ó>ò 	ˆLÜ˜l¬E¯L©LÔ9ØØÐ&Ø".×"5Ñ"5‘Ø×$Ñ$¨Ó7Ü Øyóð ð	ð Ð"Üð tóð õ 6ˆ	ØœeŸl™l¨5Ó1Ò1Ø%�4×%Ñ% iÑ:°6Ñ:Ð:ð "ˆt×!Ñ! )Ñ6¨vÑ6Ð6r$   r;   ÚwarmupÚrepc                 óh   ‡— dt         dt        t           fˆfd„} ||«       t         ||«      «      S )aû  Benchmark the CPU callable, `_callable`, and return the median runtime,
        in milliseconds.

        Arguments:
        - _callable: The CPU callable to benchmark.

        Keyword Arguments:
        - warmup: Optionally, the duration, in milliseconds, to run `_callable`
        before benchmarking starts.
        - rep: Optionally, the duration, in milliseconds, to run `_callable`
        during benchmarking.

        Returns:
        - The median runtime of `_callable`, in milliseconds.
        Úmsr   c                 óì   •— g }t        j                  «       }	 t        j                  «       } ‰«        t        j                  «       }|j                  ||z
  t        z  «       ||z
  t        z  | kD  r	 |S Œ]r(   )ÚtimeÚperf_counterÚappendÚMILLISECONDS_PER_SECOND)r@   ÚtimingsÚrun_start_tÚstart_tÚend_tr;   s        €r!   Úrun_forz*Benchmarker.benchmark_cpu.<locals>.run_foru   sp   ø€ ØˆGÜ×+Ñ+Ó-ˆKØÜ×+Ñ+Ó-�Ù”Ü×)Ñ)Ó+�Ø—‘ ¨¡Ô3JÑJÔKØ˜[Ñ(Ô,CÑCÀrÒIØØˆNð r$   )ÚintÚlistÚfloatr   )r   r;   r=   r>   rJ   s    `   r!   r7   zBenchmarker.benchmark_cpua   s2   ø€ ð(
	œð 
	¤¤U¡õ 
	ñ 	�ŒÜ‘g˜c“lÓ#Ð#r$   r   c                 ó   — t         ‚r(   )ÚNotImplementedError)r   r   r   s      r!   r8   zBenchmarker.benchmark_gpu„   s   € ä!Ð!r$   )é   éd   )r   Ú
__module__Ú__qualname__r   r*   r#   r   r   ÚtupleÚdictÚstrrM   r<   rK   r7   r8   r)   r$   r!   r&   r&   ,   sô   „ ð�tð  ó ð ð.7Øð.7à�S˜#�XÑð.7ð �s˜C�x‘ð.7ð ˜˜S˜‘>ð	.7ð
 ð.7ð 
ò.7ó ð.7ð` àORñ $Øð $Ø'¨¨C¨Ñ0ð $Ø:=ð $ØILð $à	ò $ó ð $ðD ð"˜Dð "¨ð "¸ð "Àò "ó ñ"r$   r&   c            	       ó\   — e Zd Zedededef   fd„«       Zededeg ef   dede	fd„«       Z
y)	ÚTritonBenchmarkerr   r   .c                 óN   — 	 ddl m} |S # t        $ r}t        d«      |‚d}~ww xY w)z"Lazily import Triton's `do_bench`.r   )Údo_benchzrequires TritonN)Útriton.testingrZ   ÚImportErrorrO   )r   rZ   Úes      r!   Útriton_do_benchz!TritonBenchmarker.triton_do_benchŠ   s4   € ð	@Ý/ð ˆøô ò 	@Ü%Ð&7Ó8¸aÐ?ûð	@ús   ‚
 Š	$“Ÿ$r;   r   c                 ó0  — t        j                  | j                  «      j                  }t	        |j                  «       «      D ]
  }||vsŒ||= Œ d|v r | j                  |fi |¤Žd   S d|v r | j                  |fi |¤ŽS  | j                  |fi |¤ddi¤ŽS )a  Benchmark the GPU callable, `_callable`, and return the runtime, in milliseconds.

        Arguments:
        - _callable: The GPU callable to benchmark.

        Keyword Arguments:
        - quantiles: Optionally, a tuple of floats denoting the requested quantiles.
        - return_mode: Optionally, the requested return mode. Currently, Triton's
        `do_bench` supports min, max, mean, and median return modes.
        - **kwargs: Additional kwargs passed to Triton's `do_bench`.

        Returns:
        - The runtime of `callable`, in milliseconds. If `kwargs["quantiles"]` is specified,
        this is the first requested quantile. Else, if `kwargs["return_mode"]` is specified,
        this is the requested return mode. Otherwise, this is the median.
        Ú	quantilesr   Úreturn_moder   )ÚinspectÚ	signaturer^   Ú
parametersrL   Úkeys)r   r;   r   Údo_bench_paramsÚkwargs        r!   r8   zTritonBenchmarker.benchmark_gpu“   s­   € ô$ "×+Ñ+¨D×,@Ñ,@ÓA×LÑLˆÜ˜&Ÿ+™+›-Ó(ò 	"ˆEØ˜OÒ+Ø˜5‘Mð	"ð ˜&Ñ Ø'�4×'Ñ'¨	Ñ<°VÑ<¸QÑ?Ð?Ø˜fÑ$Ø'�4×'Ñ'¨	Ñ<°VÑ<Ð<Ø#ˆt×#Ñ# IÑN°ÑNÀXÒNÐNr$   N)r   rR   rS   r   r   r   r   r^   r#   rM   r8   r)   r$   r!   rX   rX   ‰   sk   „ Øð˜dð  x°°S°Ñ'9ò ó ðð ðO˜Dð O¨X°b¸#°gÑ->ð OÈ#ð OÐRWò Oó ñOr$   rX   c                   óT  — e Zd Zededefd„«       Zdededeee	j                  j                  e	j                  j                  f      fd„Zdedeee	j                  j                  e	j                  j                  f      defd„Ze	 	 	 	 ddedeg ef   d	ed
ededededefd„«       Zy)ÚInductorBenchmarkerr   r   c                 ó”   — t         j                  j                  «       }t         j                  j                  |«      }|j                  S )z7Get the L2 cache size, in bytes, of the current device.)r3   ÚcudaÚcurrent_deviceÚget_device_propertiesÚL2_cache_size)r   r5   Úpropss      r!   rn   z!InductorBenchmarker.L2_cache_size±   s6   € ô —‘×*Ñ*Ó,ˆÜ—
‘
×0Ñ0°Ó8ˆØ×"Ñ"Ð"r$   Úitersc                 óº   — t        |«      D �cg c]B  }t        j                  j                  d¬«      t        j                  j                  d¬«      f‘ŒD c}S c c}w )z!Get `iters` pairs of CUDA events.T)Úenable_timing)Úranger3   rk   ÚEvent)r   rp   Ú_s      r!   Úget_event_pairsz#InductorBenchmarker.get_event_pairs¸   sV   € ô ˜5“\ö
ð
 ô —
‘
× Ñ ¨tÐ Ó4Ü—
‘
× Ñ ¨tÐ Ó4òò
ð 	
ùò 
s   ŽAAÚevent_pairsc           	      óh   — t        |D ��cg c]  \  }}|j                  |«      ‘Œ c}}«      S c c}}w )zIGet the minimum timing, in milliseconds, for a group of CUDA event pairs.)ÚminÚelapsed_time)r   rw   Ústart_eventÚ	end_events       r!   Úget_event_pairs_min_timingz.InductorBenchmarker.get_event_pairs_min_timingÄ   s=   € ô ð /:÷á*�K ð ×(Ñ(¨Õ3óó
ð 	
ùós   ‹.
r;   Úestimation_itersÚmemory_warmup_itersÚbenchmark_itersÚmax_benchmark_durationr   c           	      ó°  — t         j                  j                  «         |«        t         j                  j                  «        t        j                  | j                  dz  t         j
                  d¬«      }|j                  «        | j                  |«      }|D ]<  \  }	}
|j                  «        |	j                  «         |«        |
j                  «        Œ> t         j                  j                  «        | j                  |«      }t        t        |t        ||z  «      «      d«      }t        |«      D ]  }|j                  «        Œ | j                  |«      }|D ]<  \  }	}
|j                  «        |	j                  «         |«        |
j                  «        Œ> t         j                  j                  «        | j                  |«      }~t        ||«      S )a<  Benchmark a GPU callable using a custom benchmarking implementation.

        Arguments:
        - _callable: The callable to benchmark.

        Keyword Arguments:
        - estimation_iters: Optionally, the number of iterations to run `_callable`
        during runtime estimation.
        - memory_warmup_iters: Optionally, the number of iterations to flush the L2
        cache before starting benchmarking.
        - benchmark_iters: Optionally, the number of iterations to run `_callable`
        during the benchmarking.
        - max_benchmark_duration: Optionally, the maximum duration of the benchmarking,
        in milliseconds. An estimated duration is calculated based on the values
        of `memory_warmup_iters` and `benchmark_iters`, along with the estimated
        runtime of `_callable` and various other factors, and we then shrink
        `benchmark_iters` to fit in the alloted maximum duration.
        - **kwargs: Additional kwargs that may be passed to the fallback.

        Returns:
        - The minimum runtime of `_callable`, in milliseconds.
        é   rk   )Údtyper5   r   )r3   rk   ÚsynchronizeÚemptyrn   rK   Úzero_rv   Úrecordr}   Úmaxry   rs   )r   r;   r~   r   r€   r�   r   Úbufferrw   r{   r|   Úestimated_timingru   Úbenchmarked_timings                 r!   r8   z!InductorBenchmarker.benchmark_gpuÏ   s’  € ôB 	�
‰
×ÑÔ ñ 	ŒÜ�
‰
×ÑÔ ô —‘˜T×/Ñ/°1Ñ4¼E¿I¹IÈfÔUˆØ�‰Œð ×*Ñ*Ð+;Ó<ˆØ&1ò 	Ñ"ˆK˜Ø�L‰LŒNØ×ÑÔ ÙŒKØ×ÑÕð		ô
 	�
‰
×ÑÔ Ø×:Ñ:¸;ÓGÐô Ü�¤Ð%;Ð?OÑ%OÓ!PÓQÐSTó
ˆô
 Ð*Ó+ò 	ˆAØ�L‰L�Nð	ð ×*Ñ*¨?Ó;ˆØ&1ò 	Ñ"ˆK˜Ø�L‰LŒNØ×ÑÔ ÙŒKØ×ÑÕð		ô
 	�
‰
×ÑÔ Ø!×<Ñ<¸[ÓIÐð ô Ð#Ð%7Ó8Ð8r$   N)é   rQ   rQ   é   )r   rR   rS   r   r   rK   rn   rL   rT   r3   rk   rt   rv   rM   r}   r#   r   r   r8   r)   r$   r!   ri   ri   °   s#  „ Øð#˜Dð # Sò #ó ð#ð

Øð

Øð

à	ˆe�E—J‘J×$Ñ$ e§j¡j×&6Ñ&6Ð6Ñ7Ñ	8ó

ð	
Øð	
Ø!% e¨E¯J©J×,<Ñ,<¸e¿j¹j×>NÑ>NÐ,NÑ&OÑ!Pð	
à	ó	
ð ð !"Ø#&Ø"Ø&(ñM9ØðM9à˜B ˜GÑ$ðM9ð ðM9ð !ð	M9ð
 ðM9ð !$ðM9ð ðM9ð 
òM9ó ñM9r$   ri   )%rb   rB   Ú	functoolsr   r   Ú	itertoolsr   Ú
statisticsr   Útypingr   r   Útyping_extensionsr	   r
   r   r   r3   Útorch._dynamo.utilsr   r   Útorch._inductor.configr   Ú_loggingÚgetArtifactLoggerr   Úloggerrk   Úis_availablerE   r   r   r#   r&   rX   ri   Úbenchmarkerr)   r$   r!   ú<module>r›      sô   ðÛ Û ß ,Ý Ý ß  ß CÓ Cã ß 6Ý ?ð 
�‰×	)Ñ	)¨(°NÓ	C€à Ò> U§Z¡Z×%<Ñ%<Ó%>ð ð
 Ð áˆcƒN€ÙˆCƒL€ðØ�˜S !˜VÑ$ aÐ'Ñ(ðàˆk˜#˜q˜&Ñ! 1Ð$Ñ%ó÷$Z"ñ Z"ôz$O˜ô $OôNm9Ð+ô m9ñb :ÑÓñ Ù?PÓ?Rñ r$   