Ë
    ÄÓ^jÁ†  ã            
       ó¸  — U d dl Z d dlZd dlZd dlZd dlmZm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 d dlZd dlmc mZ d dlmc mZ d dlmZ d d	lm Z  d d
l!m"Z" d dl#m$Z$ ejJ                  jM                  e'd«      Z(dZ)dZ*dZ+ ed«      Z, ed«      Z-de.fd„Z/de.ej`                  z  dz  de.fd„Z1eg e jd                  d   f   Z3da4e3dz  e5d<   de3dz  de3dz  fd„Z6e jn                  ded   fd„«       Z8dee,e-f   dee,e-f   fd„Z9i Z:e;e.edef   f   e5d<   ddœde.dedef   d e<ddfd!„Z=dedef   dedef   fd"„Z>d:d#„Z?deeee,f   e-f   deeee,f   e-f   fd$„Z@ G d%„ d&«      ZAd'„ ZBd(„ ZCd)„ ZD e=d*eBd+¬«        e=d,eCd+¬«        e=d-eDd+¬«       d.ed/ed0eEd1edeFf
d2„ZG G d3„ d4eA«      ZH G d5„ d6eH«      ZI G d7„ d8eI«      ZJdeAfd9„ZK eK«       ZLy);é    N)ÚCallableÚIterator)Úcached_propertyÚwraps)Úchain)Úmedian)ÚAnyÚConcatenate)Ú	ParamSpecÚSelfÚTypeVar)Úget_interface_for_device)Úcounters)Ú	DebugMode)Ú
OrderedSetÚbenchmarking)ÚcudaÚxpuÚmtiaÚ	_CALLABLEiè  ÚPÚTÚreturnc                  óÎ   — t         D � cg c]"  } t        t        | «      j                  «       r| ‘Œ$ }} t	        |«      dk  sJ ‚t	        |«      dk(  rdS |j                  «       S c c} w )Né   r   r   )ÚGPU_BENCHMARK_DEVICE_TYPESÚgetattrÚtorchÚis_availableÚlenÚpop)Údevice_typeÚ
avail_gpuss     úo/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_inductor/runtime/benchmarking.pyÚ_get_default_gpu_device_typer%       sj   € ô 6öàÜ”5˜+Ó&×3Ñ3Ô5ò 	ð€Jð ô
 ˆz‹?˜aÒÐÐÜ˜“_¨Ò)ˆ6Ð?¨z¯~©~Ó/?Ð?ùòs   ‰'A"r"   c                 ó¤   — | €
t        «       S t        | t        j                  «      r| j                  S t        j                  | «      j                  S ©N)r%   Ú
isinstancer   ÚdeviceÚtype©r"   s    r$   Ú_normalize_gpu_device_typer,   *   s@   € ØÐÜ+Ó-Ð-Ü�+œuŸ|™|Ô,Ø×ÑÐÜ�<‰<˜Ó$×)Ñ)Ð)ó    Ú_gpu_benchmark_lock_contextÚcontext_factoryc                 ó   — t         }| a |S )ag  Override the process-local GPU benchmark lock context.

    This lets benchmark harnesses provide the context used by Inductor GPU
    benchmark calls. Some benchmark helpers delegate to other benchmark
    methods, so harness contexts should support nested entry from the same
    thread. Returning the previous context lets callers restore it in tests.
    ©r.   )r/   Úpreviouss     r$   Úset_gpu_benchmark_lock_contextr3   6   s   € ô +€HØ"1ÐØ€Or-   c               #   óf   K  — t         } | €d–— y | «       5  d–— ddd«       y# 1 sw Y   yxY w­w)z;Optionally enter the registered GPU benchmark lock context.Nr1   )r/   s    r$   Úmaybe_gpu_benchmark_lockr5   F   s6   è ø€ ô 2€OØÐÛØÙ	Ó	ñ Û÷÷ ñ üs   ‚1—%œ	1¥.ª1Úfnc                 ó|   ‡ — t        ‰ «      dt        j                  dt        j                  dt        fˆ fd„«       }|S )NÚargsÚkwargsr   c                  óT   •— t        «       5   ‰| i |¤Žcd d d «       S # 1 sw Y   y xY wr'   )r5   )r8   r9   r6   s     €r$   Úwrapperz#gpu_benchmark_lock.<locals>.wrapperR   s+   ø€ ä%Ó'ñ 	'Ù�tÐ&˜vÑ&÷	'÷ 	'ò 	'ús   Œž')r   r   r8   r9   r   ©r6   r;   s   ` r$   Úgpu_benchmark_lockr=   Q   s=   ø€ Ü
ˆ2ƒYð'”q—v‘vð '¬¯©ð '´aô 'ó ð'ð €Nr-   .Ú_BENCHMARK_DISPATCHF)Úoverrider?   c                ó®   — t        | t        «      r| st        d«      ‚t        |«      st	        d«      ‚|s| t
        v rt        d| › d�«      ‚|t
        | <   y)a  
    Register a device-type specific benchmarker.

    Args:
        device_type: torch.device.type string (e.g., "cuda", "cpu", "mps", "xpu").
        fn: callable(self, _callable, *, warmup, rep, **kwargs) -> Any
        override: allow overriding an existing registration.
    zAdevice_type must be a non-empty string matching torch.device.typezfn must be callablezBenchmarker for device_type 'z' already registeredN)r(   ÚstrÚ
ValueErrorÚcallableÚ	TypeErrorr>   )r"   r6   r?   s      r$   Úregister_benchmarkerrE   a   sg   € ô �k¤3Ô'©{ÜØOó
ð 	
ô �BŒ<ÜÐ-Ó.Ð.Ù˜Ô':Ñ:ÜØ+¨K¨=Ð8LÐMó
ð 	
ð (*Ô˜Ò$r-   c           
      ó²  ‡ ‡‡— ddl mŠ ‰j                  j                  dk(  r‰ S dt        t
           t        t
        df   z  t
        z  dt        t
           t        t
        df   z  t
        z  fˆˆfd„Št        j                  ‰ «      dt        t           d	t        t        t        f   dt        t
           t        t
        df   z  t
        z  fˆˆ fd
„«       }|S )Nr   )ÚconfigÚ Úms.r   c                 ó  •— t        | t        t        f«      r t        | «      ˆfd„| D «       «      S ‰j                  j
                  }t        | t        «      sJ ‚|dk(  r	| rd| z  S dS |dk(  rdd l}|j                  «       S t        d|› �«      ‚)Nc              3   ó.   •K  — | ]  } ‰|«      –— Œ y ­wr'   © )Ú.0ÚvalÚdistorts     €r$   ú	<genexpr>zCmay_distort_benchmarking_result.<locals>.distort.<locals>.<genexpr>†   s   øè ø€ Ò7¨S™G CŸLÑ7ùs   ƒÚinverseg      ð?ç        Úrandomr   zUnrecognized distort method )	r(   ÚlistÚtupler*   Útest_configsÚdistort_benchmarking_resultÚfloatrS   ÚRuntimeError)rI   Údistort_methodrS   rG   rO   s      €€r$   rO   z0may_distort_benchmarking_result.<locals>.distort‚   s�   ø€ ô �bœ4¤˜-Ô(Ø”4˜“8Ó7°BÔ7Ó7Ð7à×,Ñ,×HÑHˆÜ˜"œeÔ$Ð$Ð$Ø˜YÒ&Ù!�3˜‘8Ð* sÐ*Ø˜xÒ'Ûà—=‘=“?Ð"äÐ!=¸nÐ=MÐNÓOÐOr-   r8   r9   c                  ó$   •—  ‰| i |¤Ž} ‰|«      S r'   rL   )r8   r9   rI   rO   r6   s      €€r$   r;   z0may_distort_benchmarking_result.<locals>.wrapper“   s   ø€ ñ �Ð ˜Ñ ˆá�r‹{Ðr-   )Útorch._inductorrG   rV   rW   rT   rX   rU   Ú	functoolsr   r	   ÚdictrA   )r6   r;   rG   rO   s   ` @@r$   Úmay_distort_benchmarking_resultr_   |   sÄ   ú€ Ý&à×Ñ×6Ñ6¸"Ò<Øˆ	ðPÜ”‰Kœ%¤ s 
Ñ+Ñ+¬eÑ3ðPä	Œe‰”uœU C˜ZÑ(Ñ	(¬5Ñ	0öPô" ‡_�_�RÓðÜ”C‰yðÜ$(¬¬c¨¡Nðä	Œe‰”uœU C˜ZÑ(Ñ	(¬5Ñ	0õó ðð €Nr-   c                  ób   — t         j                  j                  j                  rt	        d«      ‚y )Naî  In the deterministic mode of Inductor, we will avoid those
        benchmarkings that would cause non-deterministic results. Only benchmarkings in the vetted
        scenarios are allowed. Examples include autotuning for triton configs of pointwise kernels.

        When you see this exception, you can do one of the following two things:
        1. if the benchmarking you are doing does not introduce any non-determinism, you can just
        add is_vetted_benchmarking=True to your benchmark_gpu call. That would solve the issue.

        2. if the benchmarking you are doing indeed introduces non-determinism, you'll need to disable
        such feature in deterministic mode or find an alternative implementation that is deterministic.
        )r   Ú	_inductorrG   ÚdeterministicrY   rL   r-   r$   Úmay_ban_benchmarkingrc   ž   s,   € Ü‡�×Ñ×+Ò+Üð 
ó 
ð 
	ð ,r-   c           	      óˆ   ‡ — t        ‰ «      dt        dt        j                  dt        j                  dt
        fˆ fd„«       }|S )am  
    Wraps `fn` to increment 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.

    NOTE: If you're tempted to add a dynamo_timed call here, this function can be
    called enough that the dynamo_timed overhead is not negligible.
    Úselfr8   r9   r   c                 ó”   •— | j                   j                  › d‰j                  › �}t        d   d|› �xx   dz  cc<    ‰| g|¢­i |¤ŽS )Nú.Úinductorzbenchmarking.r   )Ú	__class__Ú__name__r   )re   r8   r9   Úfn_qual_namer6   s       €r$   r;   ztime_and_count.<locals>.wrapper¹   sS   ø€ àŸ.™.×1Ñ1Ð2°!°B·K±K°=ÐAˆÜ�Ñ˜}¨\¨NÐ;Ó<ÀÑAÓ<Ù�$Ð(˜Ò( Ñ(Ð(r-   )r   r	   r   r8   r9   r   r<   s   ` r$   Útime_and_countrl   ­   sF   ø€ ô ˆ2ƒYð)”cð )¤!§&¡&ð )´A·H±Hð )Äô )ó ð)ð
 €Nr-   c                   óT  — e Zd ZdZdeddfd„Zdededej                  fd„Z	e
	 	 	 dded	ed
ef   deed
f   dz  deeef   dz  deej                  z  dz  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e
ededeg ef   dedefd„«       «       Zy)ÚBenchmarkerzo
    A device-agnostic benchmarking utility for measuring the runtime of
    inductor generated callables.
    re   r   Nc                  ó   — y r'   rL   ©re   s    r$   Ú__init__zBenchmarker.__init__È   s   € Ør-   Úfn_argsÚ	fn_kwargsc                 ó  — d }t        ||j                  «       «      D ]`  }t        j                  |«      D ]F  }t	        |t
        j                  «      sŒ|€|j                  }Œ-|j                  |k7  sŒ=t        d«      ‚ Œb |€t        d«      ‚|S )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`. Use a direct benchmarking method instead e.g. `Benchmarker.benchmark_cpu` or `Benchmarker.benchmark_gpu`.)	r   ÚvaluesÚpytreeÚtree_leavesr(   r   ÚTensorr)   rB   )re   rr   rs   Úinferred_deviceÚarg_or_kwargÚarg_or_kwarg_leafs         r$   Úinfer_devicezBenchmarker.infer_deviceË   s§   € Ø/3ˆÜ! '¨9×+;Ñ+;Ó+=Ó>ò 	ˆLô &,×%7Ñ%7¸Ó%Eò Ð!Ü!Ð"3´U·\±\ÔBØØ"Ð*Ø&7×&>Ñ&>‘OØ&×-Ñ-°Ó@Ü$Ø}óð ñð	ð Ð"ÜðNóð ð Ðr-   r6   .r)   r9   c                 ó|  ‡‡‡— d}|�(t        |t        «      rt        j                  |«      n|}n5|€|€t	        d«      ‚|xs
 t        «       }|xs i } | j                  |i |¤Ž}t        |t        j                  «      sJ ‚|xs
 t        «       }|xs i }t        |«      dk(  rt        |«      dk(  rdt        fˆfd„}n|Š|Šdt        fˆˆˆfd„}|j                  dt        j                  «      }|j                  dt        j                  «      }	t        j                  «       5  t        j!                  |j"                  «      }
|
� |
| |f||	d	œ|¤Žcddd«       S |t        j                  d
«      k(  r | j$                  |f||	d	œ|¤Žcddd«       S  | j&                  |f||	d	œ|¤Žcddd«       S # 1 sw Y   yxY w)a7  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. To bypass device inference, provide the device to the `device`
        parameter.

        WARNING: if `fn` mutates `fn_args` or `fn_kwargs`, benchmarking may fail unexpectedly.
        For example, if `fn` clears a mutable object, subsequent invocations of `fn` during
        benchmarking will fail. In such cases, `fn` should handle cloning its arguments internally.
        If device inference is required, `Benchmarker.infer_device` can be used prior to calling
        this method without any arguments for `fn_args` and `fn_kwargs`.

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

        Keyword Arguments:
        - device: Which device to use for benchmarking. If not provided the device will be attempted
        to be inferred from `fn_args` and `fn_kwargs`.
        - **kwargs: The benchmarking implementation's kwargs.

        Returns:
        - The runtime of `fn(*fn_args, **fn_kwargs)`, in milliseconds.
        NzJ`fn_args` and `fn_kwargs` cannot both be None if `device` is not provided.r   r   c                  ó   •—  ‰ «       S r'   rL   )r6   s   €r$   Ú	_callablez(Benchmarker.benchmark.<locals>._callable  s
   ø€ Ù“t�r-   c                  ó   •—  ‰‰ i ‰¤ŽS r'   rL   )Ú_argsÚ_kwargsr6   s   €€€r$   r   z(Benchmarker.benchmark.<locals>._callable%  s   ø€ Ù˜5Ð, GÑ,Ð,r-   ÚwarmupÚrep©rƒ   r„   Úcpu)r(   rA   r   r)   rB   rU   r|   r    r	   r!   Úinductor_configÚ inductor_default_autotune_warmupÚinductor_default_autotune_repr   Ú_benchmarking_inductorr>   Úgetr*   Úbenchmark_cpuÚbenchmark_gpu)re   r6   rr   rs   r)   r9   ry   r   rƒ   r„   Úbenchmark_fnr�   r‚   s    `         @@r$   Ú	benchmarkzBenchmarker.benchmarkã   sÎ  ú€ ðJ 04ˆØÐä(2°6¼3Ô(?”—‘˜VÔ$ÀVñ ð ˆ 9Ð#4Ü Ø`óð ð Ò(¤£ˆGØ!š RˆIØ/˜d×/Ñ/°ÐF¸IÑFˆOä˜/¬5¯<©<Ô8Ð8Ð8àÒ$œU›WˆØ’O ˆ	ô ˆw‹<˜1Ò¤ Y£°1Ò!4ðœsö ð ˆEØˆGð-œs÷ -ð —‘˜H¤o×&VÑ&VÓWˆØ�j‰j˜¤× MÑ MÓNˆô ×-Ñ-Ó/ñ 	Sä6I×6MÑ6MØ×$Ñ$ó7ˆLð Ð'Ù# D¨)ÐV¸FÈÑVÈvÑV÷	Sñ 	Sð ¤%§,¡,¨uÓ"5Ò5Ø)�t×)Ñ)¨)ÐV¸FÈÑVÈvÑV÷	Sñ 	Sð &�4×%Ñ% iÐR¸ÀCÑRÈ6ÑR÷	S÷ 	Sò 	Sús   Ä$.F2Å-F2ÆF2Æ2F;r   rƒ   r„   c                 ó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.
        rI   r   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)rI   ÚtimingsÚrun_start_tÚstart_tÚend_tr   s        €r$   Úrun_forz*Benchmarker.benchmark_cpu.<locals>.run_forO  sp   ø€ ØˆGÜ×+Ñ+Ó-ˆKØÜ×+Ñ+Ó-�Ù”Ü×)Ñ)Ó+�Ø—‘ ¨¡Ô3JÑJÔKØ˜[Ñ(Ô,CÑCÀrÒIØØˆNð r-   )ÚintrT   rX   r   )re   r   rƒ   r„   rš   s    `   r$   rŒ   zBenchmarker.benchmark_cpu;  s2   ø€ ð(
	œð 
	¤¤U¡õ 
	ñ 	�ŒÜ‘g˜c“lÓ#Ð#r-   r8   c                 ó   — t         ‚r'   )ÚNotImplementedError)re   r8   r9   s      r$   r�   zBenchmarker.benchmark_gpu^  s   € ä!Ð!r-   c                 óx  —  |«        t         j                  j                  «        t         j                  j                  «       }t         j                  j	                  |d¬«      5   |«        ddd«       t         j                  j                  «         | j
                  |j                  fi |¤ŽS # 1 sw Y   ŒDxY w)a  Benchmark a GPU callable using CUDA graph capture and replay.

        This captures the callable into a CUDA graph and benchmarks the graph replay,
        which eliminates kernel launch overhead for fair comparison between different
        implementations.
        Úthread_local)Úcapture_error_modeN)r   r   ÚsynchronizeÚ	CUDAGraphÚgraphr�   Úreplay)re   r   r9   Ú
cuda_graphs       r$   Úbenchmark_gpu_with_cuda_graphz)Benchmarker.benchmark_gpu_with_cuda_graphb  sŒ   € ñ 	ŒÜ�
‰
×ÑÔ ô —Z‘Z×)Ñ)Ó+ˆ
Ü�Z‰Z×Ñ˜j¸^ÐÓLñ 	ÙŒK÷	ä�
‰
×ÑÔ à!ˆt×!Ñ! *×"3Ñ"3Ñ>°vÑ>Ð>÷		ð 	ús   Á%B0Â0B9)NNN)é   éd   )rj   Ú
__module__Ú__qualname__Ú__doc__r   rq   r	   r   r)   r|   rl   r   rU   r^   rA   rX   r�   r›   rŒ   r�   r=   r¦   rL   r-   r$   rn   rn   Â   sŸ  „ ñð
�tð  ó ð Sð °sð ¸u¿|¹|ó ð0 ð +/Ø+/Ø,0ñUSØðUSà�S˜#�XÑðUSð �s˜C�x‘ 4Ñ'ðUSð ˜˜S˜‘> DÑ(ð	USð
 �e—l‘lÑ" TÑ)ðUSð ðUSð 
òUSó ðUSðn àORñ $Øð $Ø'¨¨C¨Ñ0ð $Ø:=ð $ØILð $à	ò $ó ð $ðD ð"˜Dð "¨ð "¸ð "Àò "ó ð"ð Øð?Øð?à˜B ˜GÑ$ð?ð ð?ð 
ò	?ó ó ñ?r-   rn   c                ó.   —  | j                   |f||dœ|¤ŽS )Nr…   )rŒ   ©re   Úfrƒ   r„   Úkws        r$   Ú_default_cpu_benchr°   }  s!   € Øˆ4×Ñ˜aÐ>¨°CÑ>¸2Ñ>Ð>r-   c                óR   — |j                  dd«        | j                  |f||dœ|¤ŽS )Nr"   r   r…   ©Ú
setdefaultr�   r­   s        r$   Ú_default_cuda_benchr´   �  s/   € Ø‡M�M�- Ô(Øˆ4×Ñ˜aÐ>¨°CÑ>¸2Ñ>Ð>r-   c                óR   — |j                  dd«        | j                  |f||dœ|¤ŽS )Nr"   r   r…   r²   r­   s        r$   Ú_default_xpu_benchr¶   †  s/   € Ø‡M�M�- Ô'Øˆ4×Ñ˜aÐ>¨°CÑ>¸2Ñ>Ð>r-   r†   Tr   r   Úkineto_eventsÚprofiler_eventsr„   Úprofiler_device_typec           
      óZ  ‡‡‡— ddl mŠ t        «       Šdt        dd fˆˆˆfd„Š|D �cg c]0  }|j                  t
        k(  r|j                  ‰j                  k(  r|‘Œ2 }}t        |«      |k7  r"t        d|› dt
        › dt        |«      › d	�«      ‚|D ]
  } ‰|«       Œ g }| D ]g  }|j	                  «       t
        k(  sŒ|j                  «       ‰j                  k(  sŒ9|j                  |j                  «       |j                  «       f«       Œi |r{| D ]v  }|j                  «       ‰j                  k7  rŒ!|j                  «       }|j                  «       }	|D ]1  \  }
}||
k\  sŒ|	|k  sŒ‰j                  |j                  «       «        Œv Œx d
}| D ]–  }|j                  «       }|j                  «       }|j!                  «       }|j                  «       |k(  sŒG|dk7  sŒM|‰v s|dk(  sŒW|‰v sŒ\|j	                  «       dk7  sŒp||j                  «       |j                  «       z
  dz  z  }Œ˜ |S c c}w )Nr   ©Ú
DeviceTypeÚeventr   c                 ó¢   •— | j                   ‰j                  k7  ry ‰j                  | j                  «       | j                  D ]
  } ‰|«       Œ y r'   )r"   ÚCPUÚaddÚidÚcpu_children)r½   Úchildr¼   Úbenchmark_event_idsÚcollect_cpu_event_idss     €€€r$   rÅ   zB_get_callable_device_kernel_time_us.<locals>.collect_cpu_event_idsš  sG   ø€ Ø×Ñ 
§¡Ò.Øà×Ñ §¡Ô)Ø×'Ñ'ò 	)ˆEÙ! %Õ(ñ	)r-   z	Expected ú z profiling events. Found z events.rR   Úgpu_user_annotationzContext Syncç     @�@)Útorch.autogradr¼   r   r	   ÚnameÚ_CALLABLE_PROFILE_EVENT_NAMEr"   r¿   r    rY   r”   Ústart_nsÚend_nsrÀ   Úcorrelation_idÚlinked_correlation_idÚactivity_type)r·   r¸   r„   r¹   r½   Úbenchmark_eventsÚcallable_kineto_windowsÚevÚev_startÚev_endÚ	win_startÚwin_endÚdevice_time_usrÏ   rÎ   rÐ   r¼   rÄ   rÅ   s                   @@@r$   Ú#_get_callable_device_kernel_time_usrÙ   �  sL  ú€ õ *ä+5«<Ðð)¤Sð )¨T÷ )ð %öàØ�:‰:Ô5Ò5Ø×Ñ §¡Ò/ò 	ðÐð ô ÐÓ Ò#ÜØ˜�u˜AÔ:Ð;ð <ÜÐ)Ó*Ð+¨8ð5ó
ð 	
ð
 "ò %ˆÙ˜eÕ$ð%ð 68ÐØò Iˆà�G‰G‹IÔ5Ó5Ø—‘Ó  J§N¡NÓ2à#×*Ñ*¨B¯K©K«M¸2¿9¹9»;Ð+GÕHðIñ Øò 	ˆBØ�~‰~Ó :§>¡>Ò1ØØ—{‘{“}ˆHØ—Y‘Y“[ˆFØ&=ò Ñ"�	˜7Ø˜yÓ(¨V°wÓ->Ø'×+Ñ+¨B×,=Ñ,=Ó,?Ô@Ùñð	ð €NØò KˆØ %× ;Ñ ;Ó =ÐØ×-Ñ-Ó/ˆØ×+Ñ+Ó-ˆà×ÑÓÐ#7Ó7ØÐ!6Ó6à%Ð)<Ñ<à)¨QÓ.°>ÐEXÒ3Xð —
‘
“ Ó.à˜uŸ|™|›~°·±Ó0@Ñ@ÀFÑJÑJ‰NðKð" Ðùòss   ¨5H(c                   óx   — e Zd Zedededef   fd„«       Zee	e
	 d
dedeg ef   dededef
d„«       «       «       Zy	)ÚTritonBenchmarkerre   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.testingrÝ   ÚImportErrorr�   )re   rÝ   Úes      r$   Útriton_do_benchz!TritonBenchmarker.triton_do_benchß  s4   € ð	@Ý/ð ˆøô ò 	@Ü%Ð&7Ó8¸aÐ?ûð	@ús   ‚
 Š	$“Ÿ$r   Úis_vetted_benchmarkingr9   c                 óô  — |s
t        «        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 # t        $ rI}t        |«      j                  «       }d|v r&t        j                  d|«       t        d«      cY d}~S ‚ d}~ww xY w)	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   zinvalid configurationz9Skipping benchmark due to invalid configuration error: %sÚinfN)rc   ÚinspectÚ	signaturerá   Ú
parametersrT   ÚkeysÚ	ExceptionrA   ÚlowerÚloggerÚwarningrX   )re   r   râ   r9   Údo_bench_paramsÚkwargrà   Ú	error_strs           r$   r�   zTritonBenchmarker.benchmark_gpuè  s  € ñ4 &Ü Ô"ä!×+Ñ+¨D×,@Ñ,@ÓA×LÑLˆÜ˜&Ÿ+™+›-Ó(ò 	"ˆEØ˜OÒ+Ø˜5‘Mð	"ð	Ø˜fÑ$Ø+�t×+Ñ+¨IÑ@¸Ñ@ÀÑCÐCØ &Ñ(Ø+�t×+Ñ+¨IÑ@¸Ñ@Ð@Ø'�4×'Ñ'¨	ÑR°VÑRÈÒRÐRøÜò 
	ô ˜A›Ÿ™›ˆIØ&¨)Ñ3Ü—‘ØOØôô ˜U“|Õ#Øûð
	ús0   ÁB% Á7B% ÂB% Â%	C7Â.=C2Ã+C7Ã1C2Ã2C7N)F)rj   r©   rª   r   r   r   r	   rá   r_   rl   r=   ÚboolrX   r�   rL   r-   r$   rÛ   rÛ   Þ  s‹   „ Øð˜dð  x°°S°Ñ'9ò ó ðð %ØØð
 (-ñ-Øð-à˜B ˜GÑ$ð-ð !%ð-ð ð	-ð
 
ò-ó ó ó %ñ-r-   rÛ   c                   óŒ  — e Zd Zededefd„«       Z	 ddedeej                  z  dz  defd„Z
	 ddededeej                  z  dz  deeeef      fd„Zded	eeeef      defd
„Zeee	 	 	 	 	 	 	 	 ddedeg ef   dedededededeej*                     dz  dedeej                  z  dz  dedeee   z  fd„«       «       «       Zy)ÚInductorBenchmarkerre   r   c                 ó"   — | j                  «       S )z7Get the L2 cache size, in bytes, of the current device.)Úget_device_cache_sizerp   s    r$   ÚL2_cache_sizez!InductorBenchmarker.L2_cache_size  s   € ð ×)Ñ)Ó+Ð+r-   Nr"   c                 óà   — d| j                   v r| j                   d   S t        |«      }t        |«      }|j                  «       }|j	                  |«      }dD ]  }t        ||d«      }|sŒ|c S  y)z>Get the L2/global cache size, in bytes, of the current device.r÷   )r÷   Úlast_level_cache_sizeNé   )Ú__dict__r,   r   Úcurrent_deviceÚget_device_propertiesr   )re   r"   Údevice_interfacer)   ÚpropsÚattrÚ
cache_sizes          r$   rö   z)InductorBenchmarker.get_device_cache_size"  s}   € ð ˜dŸm™mÑ+Ø—=‘= Ñ1Ð1ä0°Ó=ˆÜ3°KÓ@ÐØ!×0Ñ0Ó2ˆØ ×6Ñ6°vÓ>ˆØ>ò 	"ˆDÜ  ¨¨dÓ3ˆJÚØ!Ò!ð	"ð !r-   Úitersc                 óª   — t        t        |«      «      }t        |«      D �cg c]&  }|j                  d¬«      |j                  d¬«      f‘Œ( c}S c c}w )z#Get `iters` pairs of device events.T)Úenable_timing)r   r,   ÚrangeÚEvent)re   r  r"   rþ   Ú_s        r$   Úget_event_pairsz#InductorBenchmarker.get_event_pairs3  sc   € ô 4Ü& {Ó3ó
Ðô ˜5“\ö
ð
 ð !×&Ñ&°TÐ&Ó:Ø ×&Ñ&°TÐ&Ó:òò
ð 	
ùò 
s   ¢+AÚevent_pairsc           	      óh   — t        |D ��cg c]  \  }}|j                  |«      ‘Œ c}}«      S c c}}w )zDGet the minimum timing, in milliseconds, for a group of event pairs.)ÚminÚelapsed_time)re   r	  Ústart_eventÚ	end_events       r$   Úget_event_pairs_min_timingz.InductorBenchmarker.get_event_pairs_min_timingB  s=   € ô ð /:÷á*�K ð ×(Ñ(¨Õ3óó
ð 	
ùós   ‹.
r   Úestimation_itersÚmemory_warmup_itersÚbenchmark_itersÚmax_benchmark_durationrå   Úgrad_to_nonerâ   r9   c
           	      óh  — |s
t        «        t        |	«      }	t        |	«      }|j                  «         |«        |j                  «        t	        j
                  | j                  |	«      dz  t        j                  |	¬«      }|j                  «        | j                  ||	¬«      }|D ]L  \  }}|�|D ]	  }d|_
        Œ |j                  «        |j                  «         |«        |j                  «        ŒN |j                  «        | j                  |«      }|dkD  r"t        t        |t        ||z  «      «      d«      }t        |«      D ]  }|j                  «        Œ | j                  ||	¬«      }|D ]L  \  }}|�|D ]	  }d|_
        Œ |j                  «        |j                  «         |«        |j                  «        ŒN |j                  «        ~|dk(  r%|D ��cg c]  \  }}|j!                  |«      ‘Œ }}}|S |dk(  r| j                  |«      }t        ||«      S t#        d	|› d
�«      ‚c c}}w )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 allotted maximum duration.
        - return_mode: Return mode for benchmark results. Options are "min" (default),
        "all" (returns all measurements).
        - grad_to_none: Optionally, a list of tensors whose gradients should be cleared
        before each benchmark iteration.
        - is_vetted_benchmarking: in deterministic mode, we only allow
        benchmarking in vetted cases.
        - **kwargs: Additional kwargs that may be passed to the fallback.

        Returns:
        - If return_mode="min": The minimum runtime of `_callable`, in milliseconds.
        - If return_mode="all": List of all runtime measurements, in milliseconds.
        é   ©Údtyper)   r+   Nr   r   Úallr  úUnsupported return_mode: z. Use 'min' or 'all'.)rc   r,   r   r¡   r   Úemptyrö   r›   Úzero_r  ÚgradÚrecordr  Úmaxr  r  r  rB   )re   r   r  r  r  r  rå   r  râ   r"   r9   rþ   Úbufferr	  r  r  ÚxÚestimated_timingr  Úall_timingsÚbenchmarked_timings                        r$   r�   z!InductorBenchmarker.benchmark_gpuM  s_  € ñ\ &Ü Ô"ä0°Ó=ˆÜ3°KÓ@Ðð 	×$Ñ$Ô&ñ 	ŒØ×$Ñ$Ô&ô —‘Ø×&Ñ& {Ó3°qÑ8Ü—)‘)Øô
ˆð
 	�‰Œð ×*Ñ*Ð+;ÈÐ*ÓUˆØ&1ò 	Ñ"ˆK˜àÐ'Ø%ò "�AØ!�A•Fð"à�L‰LŒNØ×ÑÔ ÙŒKØ×ÑÕð	ð 	×$Ñ$Ô&Ø×:Ñ:¸;ÓGÐð ˜aÒÜ!Ü�O¤SÐ)?ÐCSÑ)SÓ%TÓUÐWXóˆOô
 Ð*Ó+ò 	ˆAØ�L‰L�Nð	ð ×*Ñ*¨?ÈÐ*ÓTˆØ&1ò 	Ñ"ˆK˜àÐ'Ø%ò "�AØ!�A•Fð"à�L‰LŒNØ×ÑÔ ÙŒKØ×ÑÕð	ð 	×$Ñ$Ô&ð ð ˜%Òð /:÷á*�K ð ×(Ñ(¨Õ3ðˆKñ ð ÐØ˜EÒ!Ø!%×!@Ñ!@ÀÓ!MÐô Ð'Ð);Ó<Ð<äØ+¨K¨=Ð8MÐNóð ùós   ÇH.r'   )é   r¨   r¨   é   r  NFN)rj   r©   rª   r   r   r›   r÷   rA   r   r)   rö   rT   rU   r	   r  rX   r  r_   rl   r=   r   rx   rò   r�   rL   r-   r$   rô   rô     s²  „ Øð,˜Dð , Sò ,ó ð,ð
 >Bñ!Øð!Ø!$ u§|¡|Ñ!3°dÑ!:ð!à	ó!ð$ JNñ
Øð
Øð
Ø-0°5·<±<Ñ-?À$Ñ-Fð
à	ˆe�C˜�H‰oÑ	ó
ð	
Øð	
Ø!% e¨C°¨H¡oÑ!6ð	
à	ó	
ð %ØØð !"Ø#&Ø"Ø&(Ø Ø26Ø',Ø15ñyØðyà˜B ˜GÑ$ðyð ðyð !ð	yð
 ðyð !$ðyð ðyð ˜5Ÿ<™<Ñ(¨4Ñ/ðyð !%ðyð ˜5Ÿ<™<Ñ'¨$Ñ.ðyð ðyð 
��e‘Ñ	òyó ó ó %ñyr-   rô   c                   ó¸   — e Zd ZdZee	 	 	 	 	 	 	 	 ddedeg ef   de	de	de	de	d	e	d
e
deej                     dz  de
ej                  z  dz  dedefd„«       «       Zy)ÚTorchProfilerBenchmarkerzABenchmarker that uses torch.profiler for GPU kernel benchmarking.Nre   r   rƒ   r„   r  r  r  rå   r  r"   r9   r   c
           	      ó  — t        |	«      }	t        |	«      }|j                  «         |«        |j                  «        t        j                  j
                  rd}n| j                  |	«      }t        j                  |dz  t        j                  |	¬«      }|j                  «        | j                  ||	¬«      }|D ]L  \  }}|�|D ]	  }d|_        Œ |j                  «        |j                  «         |«        |j                  «        ŒN |j                  «        | j                  |«      }|dkD  r"t        t        |t        ||z  «      «      d«      }t!        |«      D ]  }|j                  «        Œ |	j#                  «       }t%        t        j&                  j(                  |«      }t        j&                  j+                  t        j&                  j(                  j,                  |gd¬	«      5 }t!        |«      D ]Q  }|�|D ]	  }d|_        Œ |j                  «        t        j&                  j/                  d
«      5   |«        ddd«       ŒS 	 ddd«       |j                  «        ddlm} t%        ||«      }d}j&                  j4                  j7                  «       D ]U  }|j9                  «       t:        k(  sŒ|j=                  «       |k(  sŒ/||j?                  «       |jA                  «       z
  dz  z  }ŒW |dk  r>tC        |j&                  j4                  j7                  «       |j7                  «       ||«      }|dk  rtE        d|› d�«      ‚|}||z  dz  }~|dv r|S tG        d|› d�«      ‚# 1 sw Y   �Œ~xY w# 1 sw Y   �Œ.xY w)a‘  Benchmark a GPU callable using torch.profiler.

        Arguments:
        - _callable: The callable to benchmark.

        Keyword Arguments:
        - warmup: Ignored (kept for API compat). Warmup is handled by the
        estimation phase which runs estimation_iters with cache flushing.
        - rep: Optionally, the maximum number of iterations to run during benchmarking.
        - estimation_iters: Optionally, the number of iterations used to estimate
        the runtime of `_callable` for dynamic rep adjustment. These iterations
        also serve as warmup for the callable.
        - memory_warmup_iters: Optionally, the number of buffer.zero_() iterations
        to run after estimation to bring the cache into a steady state before
        the profiled benchmark phase.
        - max_benchmark_duration: Optionally, the maximum duration of the profiled
        benchmark phase, in milliseconds. The rep count is reduced if the
        estimated total would exceed this budget.
        - return_mode: Return mode for benchmark results. Options are "min", "mean" (default),
        or "max".
        - grad_to_none: Optionally, a list of tensors whose gradients should be cleared
        before each benchmark iteration.
        - **kwargs: Additional kwargs that may be passed to the fallback.

        Returns:
        - The runtime of `_callable` in milliseconds, computed according to return_mode.
        rú   r  r  r+   Nr   r   F)Ú
activitiesÚrecord_shapesr   r»   rR   rÈ   z&TorchProfilerBenchmarker: '_CALLABLE' zS event not found in raw kineto results, and no correlated device events were found.)r  Úmeanr  r  z. Use 'min', 'mean', or 'max'.)$r,   r   r¡   r   ÚversionÚhiprö   r  r›   r  r  r  r  r  r  r  r  Úupperr   ÚprofilerÚProfilerActivityÚprofiler¿   Úrecord_functionrÉ   r¼   Úkineto_resultsÚeventsrÊ   rË   r"   rÍ   rÌ   rÙ   ÚAssertionErrorrB   )re   r   rƒ   r„   r  r  r  rå   r  r"   r9   rþ   Úbuffer_size_bytesr   r	  r  r  r!  Úestimated_msr  Údevice_type_upperÚprofile_activityÚprofÚ_DeviceTyper¹   Úcallable_gpu_time_usÚkineto_eventÚtotal_time_usÚavg_time_mss                                r$   r�   z&TorchProfilerBenchmarker.benchmark_gpuÏ  s‡  € ôT 1°Ó=ˆÜ3°KÓ@Ðð 	×$Ñ$Ô&ñ 	ŒØ×$Ñ$Ô&ô
 �=‰=×ÒØ 1Ñà $× :Ñ :¸;Ó GÐÜ—‘Ø Ñ"¬%¯)©)¸Kô
ˆð 	�‰Œð
 ×*Ñ*Ð+;ÈÐ*ÓUˆØ&1ò 	Ñ"ˆK˜ØÐ'Ø%ò "�AØ!�A•Fð"à�L‰LŒNØ×ÑÔ ÙŒKØ×ÑÕð	ð 	×$Ñ$Ô&Ø×6Ñ6°{ÓCˆØ˜!ÒÜ”c˜#œsÐ#9¸LÑ#HÓIÓJÈAÓNˆCô Ð*Ó+ò 	ˆAØ�L‰L�Nð	ð (×-Ñ-Ó/ÐÜ"¤5§>¡>×#BÑ#BÐDUÓVÐÜ�^‰^×#Ñ#ä—‘×/Ñ/×3Ñ3Ø ðð  ð $ó 
ð 	 ð Ü˜3“Zò  �ØÐ+Ø)ò &˜Ø!%˜�ð&à—‘”ä—^‘^×3Ñ3°KÓ@ñ  Ù”K÷ ð  ñ ÷	 ð  	×$Ñ$Ô&õ 	=ä& {Ð4EÓFÐØ"ÐØ ŸM™M×8Ñ8×?Ñ?ÓAò 	ˆLà×!Ñ!Ó#Ô'CÓCØ ×,Ñ,Ó.Ð2FÓFà$Ø ×'Ñ'Ó)¨L×,AÑ,AÓ,CÑCØñ)ñ Ñ$ð	ð   1Ò$Ü#FØ—‘×,Ñ,×3Ñ3Ó5Ø—‘“ØØ$ó	$Ð ð   1Ò$Ü Ø8Ð9JÐ8Kð LRð Róð ð -ˆØ$ sÑ*¨fÑ4ˆð ð
 Ð0Ñ0ØÐäØ+¨K¨=Ð8VÐWóð ÷e ñ  ú÷	 ñ 	 ús%   Ç*AM7È8M*É 
M7Í*M4Í/M7Í7N)r&  r¨   r%  é
   r&  r,  NN)rj   r©   rª   r«   rl   r=   r   r   r	   r›   rA   rT   r   rx   r)   rX   r�   rL   r-   r$   r(  r(  Ì  sà   „ ÙKàØð ØØ !Ø#%Ø&(Ø!Ø26Ø15ñZØðZà˜B ˜GÑ$ðZð ðZð ð	Zð
 ðZð !ðZð !$ðZð ðZð ˜5Ÿ<™<Ñ(¨4Ñ/ðZð ˜5Ÿ<™<Ñ'¨$Ñ.ðZð ðZð 
òZó ó ñZr-   r(  c                  ó~   — t         j                  r
t        «       S t         j                  r
t	        «       S t        «       S r'   )r‡   Úuse_torch_profiler_benchmarkerr(  Úuse_experimental_benchmarkerrô   rÛ   rL   r-   r$   Ú_make_default_benchmarkerrE  n  s/   € Ü×5Ò5Ü'Ó)Ð)Ü×3Ò3Ü"Ó$Ð$ÜÓÐr-   )r   N)MÚ
contextlibr]   rç   r’   Úcollections.abcr   r   r   r   Ú	itertoolsr   Ú
statisticsr   Útypingr	   r
   Útyping_extensionsr   r   r   r   Útorch._inductor.configra   rG   r‡   Útorch.utils._pytreeÚutilsÚ_pytreerv   Útorch._dynamo.device_interfacer   Útorch._dynamo.utilsr   Útorch.utils._debug_moder   Útorch.utils._ordered_setr   Ú_loggingÚgetArtifactLoggerrj   rí   r   rË   r•   r   r   rA   r%   r)   r,   ÚAbstractContextManagerÚ_GpuBenchmarkLockContextr.   Ú__annotations__r3   Úcontextmanagerr5   r=   r>   r^   rò   rE   r_   rc   rl   rn   r°   r´   r¶   r›   rX   rÙ   rÛ   rô   r(  rE  ÚbenchmarkerrL   r-   r$   ú<module>r[     sÂ  ðÜ Û Û Û ß .ß ,Ý Ý ß #ß 6Ñ 6ã ß 0Ð 0ß $Ð $Ý CÝ (Ý -Ý /ð 
�‰×	)Ñ	)¨(°NÓ	C€Ø4Ð Ø*Ð ð Ð áˆcƒN€ÙˆCƒL€ð@ có @ð*¨C°%·,±,Ñ,>ÀÑ,Eð *È#ó *ð $ B¨
×(IÑ(IÈ$Ñ(OÐ$OÑPÐ Ø?CÐ Ð5¸Ñ<Ó CðØ-°Ñ4ðà Ñ$óð  ×Ñð (¨4¡.ò ó ðð˜8 A q D™>ð ¨h°q¸!°t©nó ð 68Ð �T˜#˜x¨¨S¨Ñ1Ð1Ñ2Ó 7ð ò	*Øð*à��c�Ñð*ð ð	*ð
 
ó*ð6¨°°c°Ñ(:ð ¸xÈÈSÈÑ?Qó óDðØ�˜S !˜VÑ$ aÐ'Ñ(ðàˆk˜#˜q˜&Ñ! 1Ð$Ñ%ó÷*w?ñ w?òv?ò?ò
?ñ
 �UÐ.¸Õ >Ù �VÐ0¸4Õ @Ù �UÐ.¸Õ >ðKØðKàðKð 
ðKð ð	Kð
 óKô\;˜ô ;ô|mÐ+ô mô`_Ð2ô _ðD ;ó ñ (Ó)�r-   