
    ci*#                     0   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
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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)knobs)proton   )set_profiling_offset_profiling_onis_command_line)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    Q/var/www/html/engine/venv/lib/python3.12/site-packages/triton/profiler/profile.py_select_backendr      sH    nn##**==?GGG&	E	JKK    r   c                     d}| dk(  rlt         j                  j                  }|Pt        t	        j
                  t              j                  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,      sj    H'<<)) 7<<188??HHJZWZbbejjmttuHOr   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   r   r%   )r   r-   prefixs      r   _get_mode_strr2   &   sX    ##&&--@@BJJ%)&4&!5v53t9$"$r   c                 p    | dk(  r1ddg}|D ]'  }t        j                  |d       t        d| d       y 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` instead)osgetenvr   )r   hip_device_envsenvs      r   
_check_envr:   -   s[    +02HI" 	Cyyd#/ I#  OZ  [ 	 r   shadowtreecontextdatar   r-   hooknamer>   r?   r@   c                f   t               ryt                | t        n| } |
t               n|}t	        |      }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.
    Nr   r/   )r   r   DEFAULT_PROFILE_NAMEr   r,   r2   r:   	libprotonstartr	   registerr
   r   )	rA   r>   r?   r   r-   r@   backend_pathmode_strsessions	            r   rE   rE   7   s    ^ #'<TD#*?oG,W5LWd+Hw oodGT7HlSGxZ\73##06@Nr   rI   c                     t               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	   activaterD   activate_allrI   s    r   rK   rK   ~   sI     W\`aa! 7#r   c                     t               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	   
deactivaterD   deactivate_allrM   s    r   rO   rO      sI     W\bcc7#  "W%r   output_formatc                     t        j                  |        |  t                t        j                  |       yt               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
    Nr   zEOnly one session can be finalized when running from the command line.)r	   
unregisterr   rD   finalize_allr   r   finalize)rI   rQ   s     r   rU   rU      sS     7#}-Adee7M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=   )rE   rO   )argskwargsrI   retr   r>   r?   funcr@   r-   rA   s       r   wrapperz_profiling.<locals>.wrapper   s5    gD'PT[_`D#F#7
r   )	functoolswraps)r[   rA   r>   r?   r   r-   r@   r\   s   ``````` r   
_profilingr_      s,    & __T   Nr   rA   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-   rA   s    r   	decoratorzprofile.<locals>.decorator   s    adG$PW^bimnnr   r`   rc   )r[   rA   r>   r?   r   r-   r@   re   s    `````` r   profilerf      s9    6 |	o 	o  $T7w]ahlmmr   )N)Nr    )Nr;   r<   NNN)"r]   r   r6   r&   r   triton._C.libprotonr   rD   flagsr   r   r   hooksr	   r
   r   r-   r   typingr   r   rC   r%   r   r,   r2   r:   rE   intrK   rO   rU   r_   rf    r   r   <module>rm      sK     	   3 G G ? ?  " L L	s 	s 	%3 %huS(]/C&D % %   D & !+/D
3-D c]D 3-	D
 c]D 5h'
(D 3-DN$hsm $t $,& & &,3hsm 38C= 3RV 34 % !
3- c] 3-	
 c] 3- 3-< 
#n % !#n 3-#n c]	#n
 3-#n c]#n 3-#n 3-#nr   