o
    Diw"                     @   s  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dZdedeeeef  d
efddZded
dfd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dZd*dee d
dfddZd*dee d
dfdd Zd+dee d"ee d
dfd#d$Z						d,dee dee dee dee dee dee fd%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(d)ZdS )-    N)proton)getenv   )flags)HookManager
LaunchHookInstrumentationHook)BaseMode)OptionalUnionr   returnc                  C   s0   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    r   a/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/triton/profiler/profile.py_select_backend   s   r   r   modec                 C   s>   | dkrt jjj j}|r| d| S |S |rt|S dS )Ninstrumentation: )r   r   r   r   r   r   str)r   r   prefixr   r   r   _get_mode_str   s   r!   c                 C   s   | dkrddg}|D ]}t |d d urtd| dq
tjjj D ]*\}}|j}t |d d u rMttjj|}|d urMtj	| }rMtj
||d  q#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_valr   r   r   
_check_env   s"   
r1   shadowtreecontextdatar   r   hooknamer5   r6   r7   c                C   s   t jstjjjr
dS dt _| du rtn| } |du rt n|}t	||}t
| t| ||||}|dkr;tt | |dkrGt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!   r1   	libprotonstartr   registerr   r   )r8   r5   r6   r   r   r7   mode_strsessionr   r   r   r>   2   s   /
r>   rA   c                 C   B   t jr| dkrtdt|  | du rt  dS t|  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   r9   r   r   activater=   activate_allrA   r   r   r   rC   x      
rC   c                 C   rB   )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   r9   r   r   
deactivater=   deactivate_allrE   r   r   r   rG      rF   rG   r   output_formatc                 C   sL   t |  | du rdt_t| dS tjr| dkrtdt| | 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", "chrome_trace"].

    Returns:
        None
    NFr   zEOnly one session can be finalized when running from the command line.)	r   
unregisterr   r;   r=   finalize_allr9   r   finalize)rA   rI   r   r   r   rL      s   
rL   c                    s&   t  fdd}|S )z
    Context manager for profiling. Internally use only.

    Args:
        See start() for the arguments.

    Returns:
        wrapper (function): The wrapped function.
    c                     s.   t  d}| i |}t| |S )Nr4   )r>   rG   )argskwargsrA   retr   r5   r6   funcr7   r   r8   r   r   wrapper   s   z_profiling.<locals>.wrapper)	functoolswraps)rQ   r8   r5   r6   r   r   r7   rR   r   rP   r   
_profiling   s   rU   r8   r5   r6   r   r   r7   c             	      s8   | du r fdd}|S t |  d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              	      s   t |  dS )NrV   rU   )fr   r5   r6   r7   r   r8   r   r   	decorator   s   zprofile.<locals>.decoratorrV   rW   )rQ   r8   r5   r6   r   r   r7   rZ   r   rY   r   profile   s   r[   )N)Nr   )Nr2   r3   NNN)rS   r   triton._C.libprotonr   r=   triton._C.libtritonr   r   hooksr   r   r   r   r	   typingr
   r   r<   r   r   r!   r1   r>   intrC   rG   rL   rU   r[   r   r   r   r   <module>   s    "

F 
