a
    kh                     @   s  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ed	d
dZeedddZeddddZd$dddddee ee ee ee ee dddZd%ee ddddZd&ee ddddZd'ee eddddZd(ee ee ee ee ee dd d!Zd)ddddddee ee ee ee ee dd"d#ZdS )*    N)knobs)proton   )register_triton_hookunregister_triton_hook)set_profiling_offset_profiling_onis_command_line)Optionalr   )returnc                  C   s4   t jjj j} | dkrdS | dkr(dS tdd S )NZcudacuptiZhip	roctracerz/No backend is available for the current target.)tritonZruntimeZdriveractiveZget_current_targetbackend
ValueError)r    r   E/var/www/auris/lib/python3.9/site-packages/triton/profiler/profile.py_select_backend   s    r   )r   r   c                 C   sF   d}| dkrBt jj}|d u rBtttjj d d d d }|S )N r   backendsZnvidialib)	r   r   Z	cupti_dirstrpathlibPath__file__parentabsolute)r   Zlib_pathr   r   r   _get_backend_default_path   s    &r   c                 C   s>   | dkr:ddg}|D ]$}t |d d urtd| dqd S )Nr   ZHIP_VISIBLE_DEVICESZ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   Zhip_device_envsenvr   r   r   
_check_env%   s    
r"   shadowtreecontextdatar   hooknamer&   r'   r   r(   c                C   s^   t  r
dS | du rt} |du r$t }t| t|}t  |rL|dkrLt  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.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   r"   r   r   r   	libprotonstart)r*   r&   r'   r   r(   backend_pathr   r   r   r-   /   s    %r-   )sessionr   c                 C   s6   t  r| dkrtd| du r(t  n
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	   r   r,   Zactivate_allactivater/   r   r   r   r0   h   s
    
r0   c                 C   s6   t  r| dkrtd| du r(t  n
t|  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,   Zdeactivate_all
deactivater1   r   r   r   r2   {   s
    
r2   hatchet)r/   output_formatr   c                 C   sF   | du r t   t| t  n"t r6| dkr6t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.
                                       Aavailable options are ["hatchet"].

    Returns:
        None
    Nr   zEOnly one session can be finalized when running from the command line.)r   r,   Zfinalize_allr   r	   r   finalize)r/   r4   r   r   r   r5      s    
r5   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 )Nr%   )r-   r2   )argskwargsr/   retr   r&   r'   funcr(   r*   r   r   wrapper   s    z_profiling.<locals>.wrapper)	functoolswraps)r:   r*   r&   r'   r   r(   r;   r   r9   r   
_profiling   s    r>   c                   s8   | du r  fdd}|S t |  dS 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 )Nr)   r>   )fr   r&   r'   r(   r*   r   r   	decorator   s    zprofile.<locals>.decoratorr)   r?   )r:   r*   r&   r'   r   r(   rB   r   rA   r   profile   s    rC   )N)N)N)Nr3   )Nr#   r$   NN)N)r<   r   r   r   r   Ztriton._C.libprotonr   r,   r(   r   r   flagsr   r   r	   typingr
   r+   r   r   r   r"   r-   intr0   r2   r5   r>   rC   r   r   r   r   <module>   sl   
 9      