Ë
    Ûéñiw"  ã                   ó  — 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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   fd„Zd dee   d
dfd„Zd de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   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   fd„Zy)#é    N)Úproton)Úgetenvé   )Úflags)ÚHookManagerÚ
LaunchHookÚInstrumentationHook)Ú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    úY/var/www/pod-logistic/pod-ai/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_envr5      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Únamer9   r:   r;   c                ó¤  — t         j                  s$t        j                  j                  j
                  rydt         _        | €t        n| } |€
t        «       n|}t        ||«      }t        |«       t        j                  | ||||«      }|dk(  rt        j                  t        «       |«       |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"].
                                               For "roctracer", available options are [None].
                                               For "instrumentation", available options are [None].
                                               Each mode has a set of control knobs following with the mode name.
                                               For example, "pcsampling" has an "interval" control knob, expressed as "pcsampling:interval=1000".
        hook (str, optional): The hook to use for profiling.
                              Available options are [None, "launch"].
                              Defaults to None.
    Returns:
        session (int): The session ID of the profiling session.
    NTr   r   )r   Úcommand_liner   r(   r   ÚdisableÚprofiling_onÚDEFAULT_PROFILE_NAMEr   r$   r5   Ú	libprotonÚstartr   Úregisterr   r	   )r<   r9   r:   r   r   r;   Úmode_strÚsessions           r   rC   rC   2   s­   € ô^ ×ÒœVŸ\™\×0Ñ0×8Ò8àà„EÔà#' <Õ°T€DØ#* ?ŒoÔ¸€Gä˜W dÓ+€HäˆwÔä�o‰o˜d G¨T°7¸HÓE€GàˆxÒÜ×ÑœZ›\¨7Ô3ØÐ#Ò#Ü×ÑÔ0°Ó6¸Ô@à€Nr   rF   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   ÚactivaterB   Úactivate_all©rF   s    r   rH   rH   x   sL   € ô ×Ò˜g¨šlÜÐ`ÓaÐaä×Ñ˜Ô!à€Ü×ÑÕ ä×Ñ˜7Õ#r   c                 óÆ   — t         j                  r| dk7  rt        d«      ‚t        j                  | «       | €t        j                  «        yt        j                  | «       y)a  
    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)

    Returns:
        None
    r   zGOnly one session can be deactivated when running from the command line.N)r   r>   r   r   Ú
deactivaterB   Údeactivate_allrJ   s    r   rL   rL   Ž   sL   € ô ×Ò˜g¨šlÜÐbÓcÐcä×Ñ˜7Ô#à€Ü× Ñ Õ"ä×Ñ˜WÕ%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", "chrome_trace"].

    Returns:
        None
    NFr   zEOnly one session can be finalized when running from the command line.)	r   Ú
unregisterr   r@   rB   Úfinalize_allr>   r   Úfinalize)rF   rN   s     r   rR   rR   ¤   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 )Nr8   )rC   rL   )ÚargsÚkwargsrF   Úretr   r9   r:   Úfuncr;   r   r<   s       €€€€€€€r   Úwrapperz_profiling.<locals>.wrapperÏ   s5   ø€ ä˜ g°DÀ'ÐPTÐ[_Ô`ˆÙ�DÐ#˜FÑ#ˆÜ�7ÔØˆ
r   )Ú	functoolsÚwraps)rX   r<   r9   r:   r   r   r;   rY   s   ``````` r   Ú
_profilingr\   ¼   s,   þ€ ô& ‡_�_�TÓ÷ñ ó ðð €Nr   ©r<   r9   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   r9   r:   r;   r   r<   s    €€€€€€r   Ú	decoratorzprofile.<locals>.decoratorö   s   ø€ Ü˜a d°GÀ$ÐPWÐ^bÐimÔnÐnr   r]   r`   )rX   r<   r9   r:   r   r   r;   rb   s    `````` r   Úprofilerc   Ù   s9   ý€ ð6 €|÷	oñ 	oð Ðô ˜$ T°7ÀÈwÐ]aÐhlÔmÐmr   )N)Nr!   )Nr6   r7   NNN)rZ   r   Útriton._C.libprotonr   rB   Útriton._C.libtritonr   r   Úhooksr   r   r	   r   r
   Útypingr   r   rA   r"   r   r$   r5   rC   ÚintrH   rL   rR   r\   rc   © r   r   ú<module>rj      s/  ðÛ Û å 3Ý &Ý ß ?Ñ ?Ý ß "àÐ ðL˜ó Lð%˜3ð % h¨u°S¸(°]Ñ/CÑ&Dð %Èó %ð9˜ð 9 ó 9ð( ðCð &Ø Ø!Ø+/ØòCØ
�3‰-ðCð �c‰]ðCð �3‰-ð	Cð
 �c‰]ðCð �5˜˜h˜Ñ'Ñ
(ðCð �3‰-óCñL$�h˜s‘mð $¨tó $ñ,&˜ ™ð &°ó &ñ,3�h˜s‘mð 3¸8ÀC¹=ð 3ÐRVó 3ð4 Ø%Ø Ø!ØØñà
�3‰-ðð �c‰]ðð �3‰-ð	ð
 �c‰]ðð �3‰-ðð �3‰-óð< 
ð#nð Ø%Ø Ø!ØØò#nð �3‰-ð#nð �c‰]ð	#nð
 �3‰-ð#nð �c‰]ð#nð �3‰-ð#nð �3‰-ô#nr   