§
    ÕóÉjr(  ã                   óð  — d dl Z d dlZd dlmZ d dlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ d d	lmZmZ d
Zdefd„Zdedeeeef                  defd„Zdeddfd„Z	 d$ddddddœdee         dee         dee         dee         deeeef                  deeeef                  dee         fd„Zd$dee         ddfd„Zd%dee         deddfd„Zd&dee         dee         ddfd „Z	 	 	 	 	 	 d'dee         dee         dee         dee         dee         deeeef                  fd!„Z	 d$ddddddd"œdee         dee         dee         dee         dee         deeeef                  fd#„ZdS )(é    N)Úproton)Úgetenvé   )Úflags)ÚHookManagerÚ
LaunchHookÚInstrumentationHook)ÚHook)ÚBaseMode)ÚOptionalÚUnionr   Úreturnc                  ó„   — t           j        j        j                             ¦   «         j        } t          j        | ¦  «        S ©N)ÚtritonÚruntimeÚdriverÚactiveÚget_current_targetÚbackendÚ	libprotonÚ#select_profiler_from_triton_backend)r   s    ú|/home/lighthouse/badge-speaker-benchmark/.benchmark-venv-community/lib64/python3.11/site-packages/triton/profiler/profile.pyÚ_select_backendr      s/   € ÝŒnÔ#Ô*×=Ò=Ñ?Ô?ÔG€GÝÔ8¸ÑAÔAÐAó    r   Úmodec                 ó¤   — | dk    r8t           j        j        j                             ¦   «         j        }|r|› d|› �n|S |rt          |¦  «        ndS )NÚinstrumentationú:Ú )r   r   r   r   r   r   Ústr)r   r   Úprefixs      r   Ú_get_mode_strr#      s]   € ØÐ#Ò#Ð#Ý”Ô&Ô-×@Ò@ÑBÔBÔJˆØ%)Ð5�&Ð!Ð!˜4Ð!Ð!Ð!¨vÐ5ØÐ$�3ˆt‰9Œ9ˆ9 "Ð$r   c                 ó¨  — | dv r,ddg}|D ]%}t          |d ¦  «        �t          d|› d�¦  «        ‚Œ&| dk    o0t          j        j        j                             ¦   «         j        dk    }t          j        j	        j
                             ¦   «         D ]Á\  }}|j        }t          |d ¦  «        €¥|dk    r7|r5t          j                             |t          j        j	        j        ¦  «         ŒYt          t          j        j	        |¦  «        }|�Gt          j                             |¦  «        x}r&t          j                             ||d	         ¦  «         ŒÂd S )
N)ÚrocprofilerÚ	roctracerÚHIP_VISIBLE_DEVICESÚCUDA_VISIBLE_DEVICESz3Proton does not work when the environment variable zK is set on AMD GPUs. Please unset it and use `ROCR_VISIBLE_DEVICES` insteadÚcuptiéd   ÚTRITON_CUPTI_LIB_PATHr   )r   Ú
ValueErrorr   r   r   r   r   ÚarchÚknobsr   Úknob_descriptorsÚitemsÚkeyÚsetenvÚcupti_lib_blackwell_dirÚgetattrÚtoenv)	r   Úhip_device_envsÚenvÚuse_blackwell_cuptiÚattrÚdescr1   ÚvalÚenv_vals	            r   Ú
_check_envr=      sj  € ØÐ.Ð.Ð.Ø0Ð2HÐIˆØ"ð 	ð 	ˆCÝ�c˜4Ñ Ô Ð,Ý ð [È#ð  [ð  [ð  [ñô ð ð -ð
 " WÒ,Ðnµ´Ô1FÔ1M×1`Ò1`Ñ1bÔ1bÔ1gÐknÒ1nÐõ ”lÔ)Ô:×@Ò@ÑBÔBð 
9ð 
9‰
ˆˆdØŒhˆÝ�#�tÑÔÐ$ØÐ-Ò-Ð-Ð2EÐ-å”×#Ò# C­¬Ô)<Ô)TÑUÔUÐUØÝ�&œ,Ô-¨tÑ4Ô4ˆCØˆÝ$œl×0Ò0°Ñ5Ô5Ð5�7ð 9Ý”L×'Ò'¨¨W°Q¬ZÑ8Ô8Ð8øð
9ð 
9r   ÚshadowÚtree©ÚcontextÚdatar   r   ÚhookÚnamerA   rB   rC   c                ó,  — t           j        st          j        j        j        rdS dt           _        | €t          n| } |€t          ¦   «         n|}t          ||¦  «        }t          |¦  «         t          j        | ||||¦  «        }t          |t          ¦  «        rt          j        ||¦  «         n<|dk    r"t          j        t#          ¦   «         |¦  «         n|�t%          d|›�¦  «        ‚|dk    r"t          j        t'          |¦  «        |¦  «         |S )ah  
    Start profiling with the given name and backend.

    Usage:

        ```python
        proton.start("my_profile")
        # do something
        proton.finalize()
        ```

    Args:
        name (str, optional): The name (with path) of the profiling session.
                              If not provided, the default name is "~/proton.<suffix>", where suffix is the default
                              format according to the data type. For example, if data is "tree", the default name is "~/proton.hatchet".
        context (str, optional): The context to use for profiling.
                                 Available options are ["shadow", "python"].
                                 Defaults to "shadow".
        data (str, optional): The data structure to use for profiling.
                              Available options are ["tree", "trace"].
                              Defaults to "tree".
        backend (str, optional): The backend to use for profiling.
                                 Available options are [None, "cupti", "rocprofiler", "roctracer", "instrumentation"].
                                 Defaults to None, which automatically selects the backend matching the current active runtime.
                                 On AMD GPUs, "rocprofiler" is preferred and will fall back to "roctracer" if
                                 rocprofiler-sdk is not available.
        mode (Union[str, BaseMode], optional): The "mode" to use for profiling, which is specific to the backend.
                                               Can be a string or an instance of BaseMode (or any subclass thereof).
                                               Defaults to None.
                                               For "cupti", available options are [None, "pcsampling", "periodic_flushing"].
                                               For "rocprofiler", available options are [None, "periodic_flushing"].
                                               For "roctracer", available options are [None, "periodic_flushing"].
                                               For "instrumentation", available options are [None].
                                               Each mode has a set of control knobs following with the mode name.
                                               For example, "periodic_flushing" mode has a knob:
                                               - format: The output format of the profiling results. Available options are ["hatchet", "hatchet_msgpack", "chrome_trace"]. Default is "hatchet".
                                               The can be set via `mode="periodic_flushing:format=chrome_trace"`.
        hook (Union[str, Hook], optional): The hook to use for profiling.
                                           You may pass either:
                                           - a string hook name, e.g. "triton" (kernel launch metadata), or
                                           - a custom Hook instance.
                                           Defaults to None.
    Returns:
        session (Optional[int]): The session ID of the profiling session, or None if profiling is disabled.
    NTr   zUnsupported hook: r   )r   Úcommand_liner   r.   r   ÚdisableÚprofiling_onÚDEFAULT_PROFILE_NAMEr   r#   r=   r   ÚstartÚ
isinstancer
   r   Úregisterr   r,   r	   )rD   rA   rB   r   r   rC   Úmode_strÚsessions           r   rJ   rJ   4   s  € õl Ôð �Vœ\Ô0Ô8ð àˆtà…EÔà#' <ÕÐ°T€DØ#* ?�oÑÔÐ¸€Gå˜W dÑ+Ô+€HåˆwÑÔÐåŒo˜d G¨T°7¸HÑEÔE€Gå�$�ÑÔð 8ÝÔ˜T 7Ñ+Ô+Ð+Ð+Ø	�Ò	Ð	ÝÔ�Z™\œ\¨7Ñ3Ô3Ð3Ð3Ø	Ð	ÝÐ6¨dÐ6Ð6Ñ7Ô7Ð7ØÐ#Ò#Ð#ÝÔÕ0°Ñ6Ô6¸Ñ@Ô@Ð@à€Nr   rN   c                 óÆ   — t           j        r| dk    rt          d¦  «        ‚t          j        | ¦  «         | €t          j        ¦   «          dS t          j        | ¦  «         dS )zó
    Activate the specified session.
    The profiling session will be active and data will be recorded.

    Args:
        session (int): The session ID of the profiling session. Defaults to None (all sessions)

    Returns:
        None
    r   zEOnly one session can be activated when running from the command line.N)r   rF   r,   r   Úactivater   Úactivate_all)rN   s    r   rP   rP   …   si   € õ Ôð b˜g¨šl˜lÝÐ`ÑaÔaÐaåÔ˜Ñ!Ô!Ð!à€ÝÔÑ Ô Ð Ð Ð åÔ˜7Ñ#Ô#Ð#Ð#Ð#r   FÚflushingc                 óÊ   — t           j        r| dk    rt          d¦  «        ‚t          j        | ¦  «         | €t          j        |¦  «         dS t          j        | |¦  «         dS )ap  
    Stop the specified session.
    The profiling session's data will still be in the memory, but no more data will be recorded.

    Args:
        session (int): The session ID of the profiling session. Defaults to None (all sessions)
        flushing (bool): Whether to flush the profiling data before deactivating. Defaults to True.

    Returns:
        None
    r   zGOnly one session can be deactivated when running from the command line.N)r   rF   r,   r   Ú
deactivater   Údeactivate_all)rN   rR   s     r   rT   rT   ›   sm   € õ Ôð d˜g¨šl˜lÝÐbÑcÔcÐcåÔ˜7Ñ#Ô#Ð#à€ÝÔ  Ñ*Ô*Ð*Ð*Ð*åÔ˜W hÑ/Ô/Ð/Ð/Ð/r   r    Úoutput_formatc                 óâ   — t          j        | ¦  «         | €"dt          _        t	          j        |¦  «         dS t          j        r| dk    rt          d¦  «        ‚t	          j        | |¦  «         dS )aÕ  
    Finalizes a profiling session.
    Flush and write the profiling data to the file specified by the session name.

    Args:
        session (int, optional): The session ID to finalize. If None, all sessions are finalized. Defaults to None.
        output_format (str, optional): The output format for the profiling results.
                                       Available options are ["hatchet", "hatchet_msgpack", "chrome_trace"].

    Returns:
        None
    NFr   zEOnly one session can be finalized when running from the command line.)	r   Ú
unregisterr   rH   r   Úfinalize_allrF   r,   Úfinalize)rN   rV   s     r   rZ   rZ   ²   su   € õ Ô˜7Ñ#Ô#Ð#à€Ø"�ÔÝÔ˜}Ñ-Ô-Ð-Ð-Ð-åÔð 	f '¨Q¢, ,ÝÐdÑeÔeÐeÝÔ˜7 MÑ2Ô2Ð2Ð2Ð2r   c                 ó^   ‡ ‡‡‡‡‡‡— t          j        ‰ ¦  «        ˆˆˆˆ ˆˆˆfd„¦   «         }|S )z¯
    Context manager for profiling. Internally use only.

    Args:
        See start() for the arguments.

    Returns:
        wrapper (function): The wrapped function.
    c                  ó`   •— t          ‰
‰‰‰‰	‰¬¦  «        } ‰| i |¤Ž}t          |¦  «         |S )Nr@   )rJ   rT   )ÚargsÚkwargsrN   Úretr   rA   rB   ÚfuncrC   r   rD   s       €€€€€€€r   Úwrapperz_profiling.<locals>.wrapperÝ   sF   ø€ å˜ g°DÀ'ÐPTÐ[_Ð`Ñ`Ô`ˆØˆd�DÐ#˜FÐ#Ð#ˆÝ�7ÑÔÐØˆ
r   )Ú	functoolsÚwraps)r`   rD   rA   rB   r   r   rC   ra   s   ``````` r   Ú
_profilingrd   Ê   s^   øøøøøøø€ õ& „_�TÑÔðð ð ð ð ð ð ð ð ð ñ Ôðð €Nr   ©rD   rA   rB   r   r   rC   c          	      óV   ‡‡‡‡‡‡— | €ˆˆˆˆˆˆfd„}|S t          | ‰‰‰‰‰‰¬¦  «        S )zë
    Decorator for profiling.

    Usage:

    ```python
    @proton.profile
    def foo():
        pass
    ```

    Args:
        See start() for the arguments.

    Returns:
        decorator (function): The decorator function.
    Nc           	      ó0   •— t          | ‰‰‰‰‰‰¬¦  «        S )Nre   ©rd   )Úfr   rA   rB   rC   r   rD   s    €€€€€€r   Ú	decoratorzprofile.<locals>.decorator  s#   ø€ Ý˜a d°GÀ$ÐPWÐ^bÐimÐnÑnÔnÐnr   re   rh   )r`   rD   rA   rB   r   r   rC   rj   s    `````` r   Úprofilerk   ç   st   øøøøøø€ ð6 €|ð	oð 	oð 	oð 	oð 	oð 	oð 	oð 	oð 	oð 	oð Ðõ ˜$ T°7ÀÈwÐ]aÐhlÐmÑmÔmÐmr   r   )NF)Nr    )Nr>   r?   NNN) rb   r   Útriton._C.libprotonr   r   Útriton._C.libtritonr   r   Úhooksr   r   r	   Ú
hooks.hookr
   r   r   Útypingr   r   rI   r!   r   r#   r=   ÚintrJ   rP   ÚboolrT   rZ   rd   rk   © r   r   ú<module>rt      sµ  ðØ Ð Ð Ð Ø €€€à 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø &Ð &Ð &Ð &Ð &Ð &Ø Ð Ð Ð Ð Ð Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "Ð "Ð "àÐ ðB˜ð Bð Bð Bð Bð
%˜3ð % h¨u°S¸(°]Ô/CÔ&Dð %Èð %ð %ð %ð %ð9˜ð 9 ð 9ð 9ð 9ð 9ð4 ðNð &Ø Ø!Ø+/Ø'+ðNð Nð NØ
�3Œ-ðNð �cŒ]ðNð �3Œ-ð	Nð
 �cŒ]ðNð �5˜˜h˜Ô'Ô
(ðNð �5˜˜d˜Ô#Ô
$ðNð ˆc„]ðNð Nð Nð Nðb$ð $�h˜s”mð $¨tð $ð $ð $ð $ð,0ð 0˜ œð 0¸ð 0Èð 0ð 0ð 0ð 0ð.3ð 3�h˜s”mð 3¸8ÀC¼=ð 3ÐRVð 3ð 3ð 3ð 3ð4 Ø%Ø Ø!ØØ'+ðð à
�3Œ-ðð �cŒ]ðð �3Œ-ð	ð
 �cŒ]ðð �3Œ-ðð �5˜˜d˜Ô#Ô
$ðð ð ð ð< 
ð#nð Ø%Ø Ø!ØØ'+ð#nð #nð #nð �3Œ-ð#nð �cŒ]ð	#nð
 �3Œ-ð#nð �cŒ]ð#nð �3Œ-ð#nð �5˜˜d˜Ô#Ô
$ð#nð #nð #nð #nð #nð #nr   