
    (Th                        S SK r S SKrS SKrS SKrS SKJr  SSKJrJ	r	  SSK
JrJrJr  S SKJr  SrS\4S	 jrS
\S\4S jrS
\SS4S jr SSSSSS.S\\   S\\   S\\   S
\\   S\\   4
S jjjrSS\\   SS4S jjrSS\\   SS4S jjrSS\\   S\SS4S jjr     SS\\   S\\   S\\   S
\\   S\\   4
S jjr SSSSSSS.S\\   S\\   S\\   S
\\   S\\   4
S jjjrg)     N)proton   )register_triton_hookunregister_triton_hook)set_profiling_offset_profiling_onis_command_line)Optionalr   returnc                      [         R                  R                  R                  R	                  5       R
                  n U S:X  a  gU S:X  a  g[        S5      e)Ncudacuptihip	roctracerz/No backend is available for the current target.)tritonruntimedriveractiveget_current_targetbackend
ValueError)r   s    O/var/www/auris/envauris/lib/python3.13/site-packages/triton/profiler/profile.py_select_backendr      sH    nn##**==?GGG&	E	JKK    r   c                     SnU S:X  ak  [         R                  " SS 5      nUcQ  [        [        R                  " [
        5      R                  R                  R                  5       S-  S-  S-  S-  5      nU$ )N r   TRITON_CUPTI_LIB_PATHbackendsnvidialib)osgetenvstrpathlibPath__file__parentabsolute)r   lib_paths     r   _get_backend_default_pathr*      sl    H'994d; 7<<188??HHJZWZbbejjmttuHOr   c                 z    U S:X  a5  SS/nU H*  n[         R                  " US 5      c  M  [        SU S35      e   g g )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)r!   r"   r   )r   hip_device_envsenvs      r   
_check_envr0   $   sV    +02HI"Cyyd#/ I#  OZ  [  # r   shadowtreecontextdatar   hooknamer4   r5   r6   c                    [        5       (       a  gU c  [        n Uc
  [        5       n[        U5        [	        U5      n[        5         U(       a  US:X  a
  [        5         [        R                  " XX#U5      $ )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   r0   r*   r   r   	libprotonstart)r7   r4   r5   r   r6   backend_paths         r   r;   r;   .   sc    J |#!#w,W5L ??4$FFr   sessionc                     [        5       (       a  U S:w  a  [        S5      eU c  [        R                  " 5         g[        R                  " U 5        g)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:   activate_allactivater=   s    r   r@   r@   g   s>     W\`aa 7#r   c                     [        5       (       a  U S:w  a  [        S5      eU c  [        R                  " 5         g[        R                  " U 5        g)z
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:   deactivate_all
deactivaterA   s    r   rD   rD   z   s>     W\bcc  "W%r   output_formatc                     U c+  [        5         [        R                  " U5        [        5         g[	        5       (       a  U S:w  a  [        S5      e[        R                  " X5        g)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=   rE   s     r   rH   rH      sL     }- Adee72r   c                 Z   ^ ^^^^^ [         R                  " T 5      UUUU UU4S j5       nU$ )z
Context manager for profiling. Internally use only.

Args:
    See start() for the arguments.

Returns:
    wrapper (function): The wrapped function.
c                  H   > [        T	TTTTS9nT" U 0 UD6n[        U5        U$ )Nr3   )r;   rD   )
argskwargsr=   retr   r4   r5   funcr6   r7   s
       r   wrapper_profiling.<locals>.wrapper   s2    gD'PTUD#F#7
r   )	functoolswraps)rN   r7   r4   r5   r   r6   rO   s   `````` r   
_profilingrS      s,    $ __T   Nr   r7   r4   r5   r   r6   c          	      F   ^^^^^ U c  UUUUU4S jnU$ [        U TTTTTS9$ )z
Decorator for profiling.

Usage:

```python
@proton.profile
def foo():
    pass
```

Args:
    See start() for the arguments.

Returns:
    decorator (function): The decorator function.
c           	          > [        U TTTTTS9$ )NrT   rS   )fr   r4   r5   r6   r7   s    r   	decoratorprofile.<locals>.decorator   s    adG$PW^bccr   rT   rW   )rN   r7   r4   r5   r   r6   rY   s    ````` r   profiler[      s6    4 |	d 	d  $T7w]abbr   )N)Nhatchet)Nr1   r2   NN)rQ   r   r!   r$   triton._C.libprotonr   r:   r6   r   r   flagsr   r   r	   typingr
   r9   r#   r   r*   r0   r;   intr@   rD   rH   rS   r[    r   r   <module>rb      s     	  3 > G G  L L	s 	s 	   6G & !6G
3-6G c]6G 3-	6G
 c]6G 3-6Gr$hsm $t $&& & &&3hsm 33 3t 32 % !
3- c] 3-	
 c] 3-: 
"c % !"c 3-"c c]	"c
 3-"c c]"c 3-"cr   