§
    9˜ºhê  ã                   ó2  — d dl Z d dlZd dlZd dlZd dlmZ d dlmZ ddlm	Z	m
Z
 ddlmZmZmZ d dlmZ dZd	efd
„Zded	efd„Zded	dfd„Z	 ddddddœdee         dee         dee         dee         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d	dfd„Z	 	 	 	 	 d!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ee         dee         dee         dee         dee         f
d„ZdS )"é    N)Úknobs)Úprotoné   )Úregister_triton_hookÚunregister_triton_hook)Úset_profiling_offÚset_profiling_onÚis_command_line)ÚOptionalr   Ú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    ú_/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/triton/profiler/profile.pyÚ_select_backendr      sM   € ÝŒnÔ#Ô*×=Ò=Ñ?Ô?ÔG€GØ�&ÒÐØˆwØ	�EÒ	Ð	Øˆ{åÐJÑKÔKÐKó    r   c                 óØ   — d}| dk    rat           j        j        }|€Nt          t	          j        t          ¦  «        j        j                             ¦   «         dz  dz  dz  dz  ¦  «        }|S )NÚ r   ÚbackendsÚnvidiaÚlib)	r   r   Ú	cupti_dirÚstrÚpathlibÚPathÚ__file__ÚparentÚabsolute)r   Úlib_paths     r   Ú_get_backend_default_pathr)      si   € Ø€HØ�'ÒÐå”<Ô)ˆØÐõ �7œ<­Ñ1Ô1Ô8Ô?×HÒHÑJÔJÈZÑWÐZbÑbÐejÑjÐmtÑtÑuÔuˆHØ€Or   c                 óx   — | dk    r1ddg}|D ],}t          j        |d ¦  «        �t          d|› d�¦  «        ‚Œ+d S 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` instead)ÚosÚgetenvr   )r   Úhip_device_envsÚenvs      r   Ú
_check_envr1   %   s|   € Ø�+ÒÐØ0Ð2HÐIˆØ"ð 	ð 	ˆCÝŒy˜˜dÑ#Ô#Ð/Ý ð [È#ð  [ð  [ð  [ñô ð ð 0ð Ðð	ð 	r   ÚshadowÚtree©ÚcontextÚdatar   ÚhookÚnamer5   r6   r7   c                ó  — t          ¦   «         rdS | €t          } |€t          ¦   «         }t          |¦  «         t	          |¦  «        }t          ¦   «          |r|dk    rt          ¦   «          t          j        | ||||¦  «        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.hatchet".
        backend (str, optional): The backend to use for profiling.
                                 Available options are [None, "cupti", "cupti_pcsampling", "roctracer"].
                                 Defaults to None, which automatically selects the backend matching the current active runtime.
        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"].
                              Defaults to "tree".
        hook (str, optional): The hook to use for profiling.
                              Available options are [None, "triton"].
                              Defaults to None.
    Returns:
        session (int): The session ID of the profiling session.
    Nr   )	r
   ÚDEFAULT_PROFILE_NAMEr   r1   r)   r	   r   Ú	libprotonÚstart)r8   r5   r6   r   r7   Úbackend_paths         r   r<   r<   /   s�   € õJ ÑÔð àˆà€|Ý#ˆà€Ý!Ñ#Ô#ˆåˆwÑÔÐå,¨WÑ5Ô5€LåÑÔÐØð �˜Ò Ð ÝÑÔÐÝŒ?˜4 ¨$°¸ÑFÔFÐFr   Úsessionc                 ó¢   — t          ¦   «         r| dk    rt          d¦  «        ‚| €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;   Úactivate_allÚactivate©r>   s    r   rA   rA   h   s[   € õ ÑÔð b˜W¨š\˜\ÝÐ`ÑaÔaÐaØ€ÝÔÑ Ô Ð Ð Ð åÔ˜7Ñ#Ô#Ð#Ð#Ð#r   c                 ó¢   — t          ¦   «         r| dk    rt          d¦  «        ‚| €t          j        ¦   «          dS t          j        | ¦  «         dS )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;   Údeactivate_allÚ
deactivaterB   s    r   rE   rE   {   s[   € õ ÑÔð d˜W¨š\˜\ÝÐbÑcÔcÐcØ€ÝÔ Ñ"Ô"Ð"Ð"Ð"åÔ˜WÑ%Ô%Ð%Ð%Ð%r   ÚhatchetÚoutput_formatc                 óÞ   — | €2t          ¦   «          t          j        |¦  «         t          ¦   «          dS t	          ¦   «         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.
                                       Aavailable options are ["hatchet"].

    Returns:
        None
    Nr   zEOnly one session can be finalized when running from the command line.)r   r;   Úfinalize_allr   r
   r   Úfinalize)r>   rG   s     r   rJ   rJ   Ž   sw   € ð €ÝÑÔÐÝÔ˜}Ñ-Ô-Ð-ÝÑ Ô Ð Ð Ð åÑÔð 	f ¨A¢ ÝÐdÑeÔeÐeÝÔ˜7 MÑ2Ô2Ð2Ð2Ð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                  ó^   •— t          ‰	‰‰‰‰¬¦  «        } ‰| i |¤Ž}t          |¦  «         |S )Nr4   )r<   rE   )
ÚargsÚkwargsr>   Úretr   r5   r6   Úfuncr7   r8   s
       €€€€€€r   Úwrapperz_profiling.<locals>.wrapper·   sC   ø€ å˜ g°DÀ'ÐPTÐUÑUÔUˆØˆd�DÐ#˜FÐ#Ð#ˆÝ�7ÑÔÐØˆ
r   )Ú	functoolsÚwraps)rP   r8   r5   r6   r   r7   rQ   s   `````` r   Ú
_profilingrT   ¥   sX   øøøøøø€ õ$ „_�TÑÔðð ð ð ð ð ð ð ð ñ Ôðð €Nr   ©r8   r5   r6   r   r7   c                óP   ‡‡‡‡‡— | €ˆˆˆˆˆ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                 ó.   •— t          | ‰‰‰‰‰¬¦  «        S )NrU   ©rT   )Úfr   r5   r6   r7   r8   s    €€€€€r   Ú	decoratorzprofile.<locals>.decoratorÝ   s    ø€ Ý˜a d°GÀ$ÐPWÐ^bÐcÑcÔcÐcr   rU   rX   )rP   r8   r5   r6   r   r7   rZ   s    ````` r   Úprofiler[   Á   sj   øøøøø€ ð4 €|ð	dð 	dð 	dð 	dð 	dð 	dð 	dð 	dð 	dð Ðõ ˜$ T°7ÀÈwÐ]aÐbÑbÔbÐbr   )N)NrF   )Nr2   r3   NN)rR   r   r-   r#   r   Útriton._C.libprotonr   r;   r7   r   r   Úflagsr   r	   r
   Útypingr   r:   r"   r   r)   r1   r<   ÚintrA   rE   rJ   rT   r[   © r   r   ú<module>ra      s  ðØ Ð Ð Ð Ø €€€Ø 	€	€	€	Ø €€€à Ð Ð Ð Ð Ð Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø >Ð >Ð >Ð >Ð >Ð >Ð >Ð >Ø GÐ GÐ GÐ GÐ GÐ GÐ GÐ GÐ GÐ GØ Ð Ð Ð Ð Ð àÐ ðL˜ð Lð Lð Lð Lð	 sð 	¨sð 	ð 	ð 	ð 	ð˜ð  ð ð ð ð ð ð6Gð &Ø Ø!Øð6Gð 6Gð 6GØ
�3Œ-ð6Gð �cŒ]ð6Gð �3Œ-ð	6Gð
 �cŒ]ð6Gð �3Œ-ð6Gð 6Gð 6Gð 6Gðr$ð $�h˜s”mð $¨tð $ð $ð $ð $ð&&ð &˜ œð &°ð &ð &ð &ð &ð&3ð 3�h˜s”mð 3¸3ð 3Ètð 3ð 3ð 3ð 3ð2 Ø%Ø Ø!Øðð à
�3Œ-ðð �cŒ]ðð �3Œ-ð	ð
 �cŒ]ðð �3Œ-ðð ð ð ð: 
ð"cð Ø%Ø Ø!Øð"cð "cð "cð �3Œ-ð"cð �cŒ]ð	"cð
 �3Œ-ð"cð �cŒ]ð"cð �3Œ-ð"cð "cð "cð "cð "cð "cr   