Ë
    ©š¾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y)&é    N)Úproton)Úgetenvé   )Úflags)ÚHookManagerÚ
LaunchHookÚInstrumentationHook)ÚHook)ÚBaseMode)ÚOptionalÚUnionr   Úreturnc                  ó¤   — t         j                  j                  j                  j	                  «       j
                  } t        j                  | «      S ©N)ÚtritonÚruntimeÚdriverÚactiveÚget_current_targetÚbackendÚ	libprotonÚ#select_profiler_from_triton_backend)r   s    úa/home/spazio/kingdom_calls/work5/transenv/lib/python3.12/site-packages/triton/profiler/profile.pyÚ_select_backendr      s7   € Ü�n‰n×#Ñ#×*Ñ*×=Ñ=Ó?×GÑG€GÜ×8Ñ8¸ÓAÐAó    r   Úmodec                 ó¸   — | dk(  rGt         j                  j                  j                  j	                  «       j
                  }|r|› d|› �S |S |rt        |«      S dS )NÚinstrumentationú:Ú )r   r   r   r   r   r   Ústr)r   r   Úprefixs      r   Ú_get_mode_strr#      sX   € ØÐ#Ò#Ü—‘×&Ñ&×-Ñ-×@Ñ@ÓB×JÑJˆÙ%)�&�˜˜4˜&Ð!Ð5¨vÐ5ÙŒ3ˆt‹9Ð$ "Ð$r   c                 óö  — | dv r&ddg}|D ]  }t        |d «      €Œt        d|› d�«      ‚ | dk(  xr? t        j                  j                  j
                  j                  «       j                  dk\  }t        j                  j                  j                  j                  «       D ]Ô  \  }}|j                  }t        |d «      �Œ|dk(  rE|rCt        j                  j                  |t        j                  j                  j                  «       Œit        t        j                  j                  |«      }|€Œ�t        j                  j!                  |«      x}sŒ²t        j                  j                  ||d	   «       ŒÖ y )
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=      sN  € ØÐ.Ñ.Ø0Ð2HÐIˆØ"ò 	ˆCÜ�c˜4Ó Ñ,Ü ØIÈ#Èð  OZð  [óð ð	ð " WÑ,Òn´·±×1FÑ1F×1MÑ1M×1`Ñ1`Ó1b×1gÑ1gÐknÑ1nÐô —l‘l×)Ñ)×:Ñ:×@Ñ@ÓBò 
9‰
ˆˆdØ�h‰hˆÜ�#�tÓÑ$ØÐ-Ò-Ñ2Eä—‘×#Ñ# C¬¯©×)<Ñ)<×)TÑ)TÔUØÜœ&Ÿ,™,×-Ñ-¨tÓ4ˆCØ‰Ü$Ÿl™l×0Ñ0°Ó5Ð5�7Ñ5Ü—L‘L×'Ñ'¨¨W°Q©ZÕ8ñ
9r   ÚshadowÚtree©ÚcontextÚdatar   r   ÚhookÚnamerA   rB   rC   c                ó  — t         j                  s$t        j                  j                  j
                  rydt         _        | €t        n| } |€
t        «       n|}t        ||«      }t        |«       t        j                  | ||||«      }t        |t        «      rt        j                   ||«       n4|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Ñ0×8Ò8àà„EÔà#' <Õ°T€DØ#* ?ŒoÔ¸€Gä˜W dÓ+€HäˆwÔä�o‰o˜d G¨T°7¸HÓE€Gä�$œÔÜ×Ñ˜T 7Õ+Ø	�Ò	Ü×ÑœZ›\¨7Õ3Ø	Ð	ÜÐ-¨d¨XÐ6Ó7Ð7ØÐ#Ò#Ü×ÑÔ0°Ó6¸Ô@à€Nr   rN   c                 óÆ   — t         j                  r| dk7  rt        d«      ‚t        j                  | «       | €t        j                  «        yt        j                  | «       y)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   …   sL   € ô ×Ò˜g¨šlÜÐ`ÓaÐaä×Ñ˜Ô!à€Ü×ÑÕ ä×Ñ˜7Õ#r   Úflushingc                 óÊ   — t         j                  r| dk7  rt        d«      ‚t        j                  | «       | €t        j                  |«       yt        j                  | |«       y)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   ›   sP   € ô ×Ò˜g¨šlÜÐbÓcÐcä×Ñ˜7Ô#à€Ü× Ñ  Õ*ä×Ñ˜W hÕ/r   Úoutput_formatc                 óà   — t        j                  | «       | €!dt        _        t	        j
                  |«       yt        j                  r| dk7  rt        d«      ‚t	        j                  | |«       y)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   ²   sX   € ô ×Ñ˜7Ô#à€Ø"ŒÔÜ×Ñ˜}Õ-ä×Ò '¨Q¢,ÜÐdÓeÐeÜ×Ñ˜7 MÕ2r   c                 óZ   ‡ ‡‡‡‡‡‡— 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                  óP   •— t        ‰
‰‰‰‰	‰¬«      } ‰| i |¤Ž}t        |«       |S )Nr@   )rJ   rT   )ÚargsÚkwargsrN   Úretr   rA   rB   ÚfuncrC   r   rD   s       €€€€€€€r   Úwrapperz_profiling.<locals>.wrapperÝ   s5   ø€ ä˜ g°DÀ'ÐPTÐ[_Ô`ˆÙ�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          	      óN   ‡‡‡‡‡‡— | €ˆˆˆˆˆˆ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.
    c           	      ó(   •— t        | ‰‰‰‰‰‰¬«      S )Nre   ©rd   )Úfr   rA   rB   rC   r   rD   s    €€€€€€r   Ú	decoratorzprofile.<locals>.decorator  s   ø€ Ü˜a d°GÀ$ÐPWÐ^bÐimÔnÐnr   re   rh   )r`   rD   rA   rB   r   r   rC   rj   s    `````` r   Úprofilerk   ç   s9   ý€ ð6 €|÷	oñ 	oð Ðô ˜$ T°7ÀÈwÐ]aÐhlÔ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      sf  ðÛ Û å 3Ý &Ý ß ?Ñ ?Ý Ý ß "àÐ ðB˜ó Bð
%˜3ð % h¨u°S¸(°]Ñ/CÑ&Dð %Èó %ð9˜ð 9 ó 9ð4 ðNð &Ø Ø!Ø+/Ø'+òNØ
�3‰-ðNð �c‰]ðNð �3‰-ð	Nð
 �c‰]ðNð �5˜˜h˜Ñ'Ñ
(ðNð �5˜˜d˜Ñ#Ñ
$ðNð ˆc�]óNñb$�h˜s‘mð $¨tó $ñ,0˜ ™ð 0¸ð 0Èó 0ñ.3�h˜s‘mð 3¸8ÀC¹=ð 3ÐRVó 3ð4 Ø%Ø Ø!ØØ'+ñà
�3‰-ðð �c‰]ðð �3‰-ð	ð
 �c‰]ðð �3‰-ðð �5˜˜d˜Ñ#Ñ
$óð< 
ð#nð Ø%Ø Ø!ØØ'+ò#nð �3‰-ð#nð �c‰]ð	#nð
 �3‰-ð#nð �c‰]ð#nð �3‰-ð#nð �5˜˜d˜Ñ#Ñ
$ô#nr   