§
    OŠtj)&  ã                   óð  — 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        } | dk    rdS | dk    rdS 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    úU/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/triton/profiler/profile.pyÚ_select_backendr      sM   € ÝŒnÔ#Ô*×=Ò=Ñ?Ô?ÔG€GØ�&ÒÐØˆwØ	�EÒ	Ð	Øˆ{åÐJÑKÔKÐKó    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k    r,ddg}|D ]%}t          |d ¦  «        �t          d|› d�¦  «        ‚Œ&t          j        j        j                             ¦   «         D ]„\  }}|j        }t          |d ¦  «        €ht          t          j        j        |¦  «        }|�Gt          j         	                    |¦  «        x}r&t          j         
                    ||d         ¦  «         Œ…d S )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Ñ Ô Ð,Ý ð [È#ð  [ð  [ð  [ñô ð ð -õ ”lÔ)Ô:×@Ò@ÑBÔBð 9ð 9‰
ˆˆdØŒhˆÝ�#�tÑÔÐ$Ý�&œ,Ô-¨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Únamer:   r;   r<   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 )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Ô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   rH   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   r?   r   r   ÚactivaterC   Úactivate_all)rH   s    r   rJ   rJ   �   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   r?   r   r   Ú
deactivaterC   Údeactivate_all)rH   rL   s     r   rN   rN   —   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   rA   rC   Úfinalize_allr?   r   Úfinalize)rH   rP   s     r   rT   rT   ®   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 )Nr9   )rD   rN   )ÚargsÚkwargsrH   Úretr   r:   r;   Úfuncr<   r   r=   s       €€€€€€€r   Úwrapperz_profiling.<locals>.wrapperÙ   sF   ø€ å˜ g°DÀ'ÐPTÐ[_Ð`Ñ`Ô`ˆØˆd�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          	      ó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 )Nr_   ©r^   )Úfr   r:   r;   r<   r   r=   s    €€€€€€r   Ú	decoratorzprofile.<locals>.decorator   s#   ø€ Ý˜a d°GÀ$ÐPWÐ^bÐimÐnÑnÔnÐnr   r_   rb   )rZ   r=   r:   r;   r   r   r<   rd   s    `````` r   Úprofilere   ã   st   øøøøøø€ ð6 €|ð	oð 	oð 	oð 	oð 	oð 	oð 	oð 	oð 	oð 	oð Ðõ ˜$ T°7ÀÈwÐ]aÐhlÐmÑmÔ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      sµ  ðØ Ð Ð Ð Ø €€€à 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø &Ð &Ð &Ð &Ð &Ð &Ø Ð Ð Ð Ð Ð Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "Ð "Ð "àÐ ðL˜ð Lð Lð Lð Lð%˜3ð % h¨u°S¸(°]Ô/CÔ&Dð %Èð %ð %ð %ð %ð9˜ð 9 ð 9ð 9ð 9ð 9ð( ðKð &Ø Ø!Ø+/Ø'+ðKð Kð KØ
�3Œ-ðKð �cŒ]ðKð �3Œ-ð	Kð
 �cŒ]ðKð �5˜˜h˜Ô'Ô
(ðKð �5˜˜d˜Ô#Ô
$ðKð ˆc„]ðKð Kð Kð Kð\$ð $�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   