U
    ‹ÑhÈ  ã                   @   sD  d dl Z d dl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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)Ú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cudaZcuptiZhipZ	roctracerz/No backend is available for the current target.)ÚtritonZruntimeZdriverÚactiveZget_current_targetÚbackendÚ
ValueError)r   © r   úE/tmp/user/2202/pip-unpacked-wheel-sn6cy74c/triton/profiler/profile.pyÚ_select_backend   s    r   ÚshadowÚtree©ÚcontextÚdatar   Úhook©Únamer   r   r   r   c                C   sL   t ƒ r
dS | dkrt} |dkr$tƒ }tƒ  |r<|dkr<tƒ  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 ["cupti"].
                                 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   Ú	libprotonÚstartr   r   r   r   r      s    %r   )Úsessionr
   c                 C   s$   t ƒ r| dkrtdƒ‚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 0 (the first session started.)

    Returns:
        None
    r   zEOnly one session can be activated when running from the command line.N)r   r   r   Úactivate©r   r   r   r   r   K   s    r   c                 C   s$   t ƒ r| dkrtdƒ‚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 0 (the first session started.)

    Returns:
        None
    r   zGOnly one session can be deactivated when running from the command line.N)r   r   r   Ú
deactivater   r   r   r   r    [   s    r    Úhatchet)r   Úoutput_formatr
   c                 C   sF   | dk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   r"   r   r   r   r#   k   s    
r#   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�}ˆ| |Ž}t|ƒ |S )Nr   )r   r    )Ú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   r'   r   Ú
_profiling‚   s    r,   c                   s8   | dk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   r0   r   r/   r   Úprofilež   s    r1   )N)r   )r   )Nr!   )Nr   r   NN)N)r*   r   Ztriton._C.libprotonr   r   r   r   r   Úflagsr   r   r   Útypingr	   r   Ústrr   r   Úintr   r    r#   r,   r1   r   r   r   r   Ú<module>   sb    ÿúú5     úú ÿùù