Ë
    1²j)&  ã                   ó@  — 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
                  } | dk(  ry| dk(  ryt        d«      ‚)NÚcudaÚcuptiÚhipÚ	roctracerz/No backend is available for the current target.)ÚtritonÚruntimeÚdriverÚactiveÚget_current_targetÚbackendÚ
ValueError)r   s    úV/var/www/html/ia-enhanced/venv/lib/python3.12/site-packages/triton/profiler/profile.pyÚ_select_backendr      sH   € Ü�n‰n×#Ñ#×*Ñ*×=Ñ=Ó?×GÑG€GØ�&ÒØØ	�EÒ	ØäÐJÓKÐKó    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k(  r&ddg}|D ]  }t        |d «      €Œt        d|› d�«      ‚ t        j                  j                  j
                  j                  «       D ]Š  \  }}|j                  }t        |d «      �Œt        t        j                  j                  |«      }|€ŒFt        j                  j                  |«      x}sŒht        j                  j                  ||d   «       ŒŒ y )Nr   Ú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` insteadr   )r   r   r   Úknobsr   Úknob_descriptorsÚitemsÚkeyÚgetattrÚtoenvÚsetenv)r   Úhip_device_envsÚenvÚattrÚdescr,   ÚvalÚenv_vals           r   Ú
_check_envr6       sã   € Ø�+ÒØ0Ð2HÐIˆØ"ò 	ˆCÜ�c˜4Ó Ñ,Ü ØIÈ#Èð  OZð  [óð ð	ô —l‘l×)Ñ)×:Ñ:×@Ñ@ÓBò 9‰
ˆˆdØ�h‰hˆÜ�#�tÓÑ$Üœ&Ÿ,™,×-Ñ-¨tÓ4ˆCØ‰Ü$Ÿl™l×0Ñ0°Ó5Ð5�7Ñ5Ü—L‘L×'Ñ'¨¨W°Q©ZÕ8ñ9r   ÚshadowÚtree©ÚcontextÚdatar   r   ÚhookÚnamer:   r;   r<   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 )a-  
    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", "roctracer", "instrumentation"].
                                 Defaults to None, which automatically selects the backend matching the current active runtime.
        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 "roctracer", available options are ["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%   r6   Ú	libprotonÚstartÚ
isinstancer
   r   Úregisterr   r   r	   )r=   r:   r;   r   r   r<   Úmode_strÚsessions           r   rD   rD   3   sà   € ôf ×Òœ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   rH   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   r?   r   r   ÚactivaterC   Úactivate_all)rH   s    r   rJ   rJ   �   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   r?   r   r   Ú
deactivaterC   Údeactivate_all)rH   rL   s     r   rN   rN   —   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   rA   rC   Úfinalize_allr?   r   Úfinalize)rH   rP   s     r   rT   rT   ®   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 )Nr9   )rD   rN   )ÚargsÚkwargsrH   Úretr   r:   r;   Úfuncr<   r   r=   s       €€€€€€€r   Úwrapperz_profiling.<locals>.wrapperÙ   s5   ø€ ä˜ g°DÀ'ÐPTÐ[_Ô`ˆÙ�DÐ#˜FÑ#ˆÜ�7ÔØˆ
r   )Ú	functoolsÚwraps)rZ   r=   r:   r;   r   r   r<   r[   s   ``````` r   Ú
_profilingr^   Æ   s,   þ€ ô& ‡_�_�TÓ÷ñ ó ðð €Nr   ©r=   r:   r;   r   r   r<   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 )Nr_   ©r^   )Úfr   r:   r;   r<   r   r=   s    €€€€€€r   Ú	decoratorzprofile.<locals>.decorator   s   ø€ Ü˜a d°GÀ$ÐPWÐ^bÐimÔnÐnr   r_   rb   )rZ   r=   r:   r;   r   r   r<   rd   s    `````` r   Úprofilere   ã   s9   ý€ ð6 €|÷	oñ 	oð Ðô ˜$ T°7ÀÈwÐ]aÐhlÔmÐmr   )N)NF)Nr"   )Nr7   r8   NNN) r\   r   Útriton._C.libprotonr   rC   Útriton._C.libtritonr   r   Úhooksr   r   r	   Ú
hooks.hookr
   r   r   Útypingr   r   rB   r#   r   r%   r6   ÚintrD   rJ   ÚboolrN   rT   r^   re   © r   r   ú<module>rn      sf  ðÛ Û å 3Ý &Ý ß ?Ñ ?Ý Ý ß "àÐ ðL˜ó Lð%˜3ð % h¨u°S¸(°]Ñ/CÑ&Dð %Èó %ð9˜ð 9 ó 9ð( ðKð &Ø Ø!Ø+/Ø'+òKØ
�3‰-ðKð �c‰]ðKð �3‰-ð	Kð
 �c‰]ðKð �5˜˜h˜Ñ'Ñ
(ðKð �5˜˜d˜Ñ#Ñ
$ðKð ˆc�]óKñ\$�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   