
    wg                        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de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y)    N)proton   )register_triton_hookunregister_triton_hook)set_profiling_offset_profiling_onis_command_line)Optionalr   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    \/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/triton/profiler/profile.py_select_backendr      sH    nn##**==?GGG&	E	JKK    shadowtreecontextdatar   hooknamer   r   r   r    c                    t               ry| 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 ["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startr!   r   r   r   r    s        r   r%   r%      sP    J |#!# ??4$88r   sessionc                 b    t               r| dk7  rt        d      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 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'   s    r   r)   r)   K   s*     W\`aawr   c                 b    t               r| dk7  rt        d      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 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*   s    r   r,   r,   [   s*     W\bcc!r   output_formatc                     | *t                t        j                  |       t                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.
                                       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'   r-   s     r   r0   r0   k   sK     }- Adee7M2r   c                 V     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                  N    t        	      } | i |}t        |       |S )Nr   )r%   r,   )
argskwargsr'   retr   r   r   funcr    r!   s
       r   wrapperz_profiling.<locals>.wrapper   s2    gD'PTUD#F#7
r   )	functoolswraps)r6   r!   r   r   r   r    r7   s   `````` r   
_profilingr:      s,    $ __T   Nr   r&   c                H    | 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!   s    r   	decoratorzprofile.<locals>.decorator   s    adG$PW^bccr   r&   r=   )r6   r!   r   r   r   r    r?   s    ````` r   profiler@      s6    4 |	d 	d  $T7w]abbr   )N)r   )Nhatchet)Nr   r   NN)r8   r   triton._C.libprotonr   r$   r    r   r   flagsr   r   r	   typingr
   r#   strr   r%   intr)   r,   r0   r:   r@    r   r   <module>rH      s     3 > G G  L L 29 & !29
3-29 c]29 3-	29
 c]29 3-29j hsm  D   " "d " 3hsm 33 3t 32 % !
3- c] 3-	
 c] 3-: 
"c % !"c 3-"c c]	"c
 3-"c c]"c 3-"cr   