
    Xh                     2   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d	efd
Zded	efdZded	d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dS )"    N)knobs)proton   )register_triton_hookunregister_triton_hook)set_profiling_offset_profiling_onis_command_line)Optionalr   returnc                      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   s    i/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/triton/profiler/profile.py_select_backendr      sM    n#*==??GG&w	E		{JKKK    r   c                     d}| dk    rat           j        j        }|Nt          t	          j        t                    j        j                                        dz  dz  dz  dz            }|S )N r   backendsnvidialib)	r   r   	cupti_dirstrpathlibPath__file__parentabsolute)r   lib_paths     r   _get_backend_default_pathr)      si    H'<) 7<118?HHJJZWZbbejjmttuuHOr   c                 x    | dk    r1ddg}|D ],}t          j        |d           t          d| d          +d S 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` instead)osgetenvr   )r   hip_device_envsenvs      r   
_check_envr1   %   s|    +02HI" 	 	Cyd##/  [#  [  [  [   0 	 	r   shadowtreecontextdatar   hooknamer5   r6   r7   c                   t                      rdS | t          } |t                      }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 [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   r1   r)   r	   r   	libprotonstart)r8   r5   r6   r   r7   backend_paths         r   r<   r<   /   s    J  |#!##w,W55L   ?4$FFFr   sessionc                     t                      r| dk    rt          d          | t          j                     dS t          j        |            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;   activate_allactivater>   s    r   rA   rA   h   s[      bW\\`aaa     7#####r   c                     t                      r| dk    rt          d          | t          j                     dS t          j        |            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;   deactivate_all
deactivaterB   s    r   rE   rE   {   s[      dW\\bccc """""W%%%%%r   hatchetoutput_formatc                     | 2t                       t          j        |           t                       dS t	                      r| dk    rt          d          t          j        | |           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;   finalize_allr   r
   r   finalize)r>   rG   s     r   rJ   rJ      sw     }---      	fAdeee7M22222r   c                 Z     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                  ^    t          	          } | i |}t          |           |S )Nr4   )r<   rE   )
argskwargsr>   retr   r5   r6   funcr7   r8   s
       r   wrapperz_profiling.<locals>.wrapper   sC    gD'PTUUUdD#F##7
r   )	functoolswraps)rP   r8   r5   r6   r   r7   rQ   s   `````` r   
_profilingrT      sX    $ _T          Nr   r8   r5   r6   r   r7   c                P    | 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.
    Nc                 .    t          |           S )NrU   rT   )fr   r5   r6   r7   r8   s    r   	decoratorzprofile.<locals>.decorator   s     adG$PW^bccccr   rU   rX   )rP   r8   r5   r6   r   r7   rZ   s    ````` r   profiler[      sj    4 |	d 	d 	d 	d 	d 	d 	d 	d 	d  $T7w]abbbbr   )N)NrF   )Nr2   r3   NN)rR   r   r-   r#   r   triton._C.libprotonr   r;   r7   r   r   flagsr   r	   r
   typingr   r:   r"   r   r)   r1   r<   intrA   rE   rJ   rT   r[    r   r   <module>ra      s        				        3 3 3 3 3 3 > > > > > > > > G G G G G G G G G G       L L L L L	s 	s 	 	 	 	      6G & !6G 6G 6G
3-6G c]6G 3-	6G
 c]6G 3-6G 6G 6G 6Gr$ $hsm $t $ $ $ $&& & & & & & &&3 3hsm 33 3t 3 3 3 32 % ! 
3- c] 3-	
 c] 3-   : 
"c % !"c "c "c 3-"c c]	"c
 3-"c c]"c 3-"c "c "c "c "c "cr   