
    Foj)&                     b   S SK r S SKrS SKJr  S SKJr  SSKJr  SSKJ	r	J
r
Jr  SSKJr  SSKJr  S S	KJrJr  S
rS\4S jrS\S\\\\4      S\4S jrS\SS4S jr S"SSSSSS.S\\   S\\   S\\   S\\   S\\\\4      S\\\\4      S\\   4S j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4S jjr      S%S\\   S\\   S\\   S\\   S\\   S\\\\4      4S jjr S"SSSSSSS .S\\   S\\   S\\   S\\   S\\   S\\\\4      4S! jjjrg)&    N)proton)getenv   )flags)HookManager
LaunchHookInstrumentationHook)Hook)BaseMode)OptionalUnionr   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    Q/mnt/workspace/venv-train/lib/python3.13/site-packages/triton/profiler/profile.py_select_backendr      sH    nn##**==?GGG&	E	JKK    r   modec                     U S:X  aL  [         R                  R                  R                  R	                  5       R
                  nU(       a  U SU 3$ U$ U(       a  [        U5      $ S$ )Ninstrumentation: )r   r   r   r   r   r   str)r   r   prefixs      r   _get_mode_strr%      sX    ##&&--@@BJJ%)&4&!5v53t9$"$r   c                    U S:X  a)  SS/nU H  n[        US 5      c  M  [        SU S35      e   [        R                  R                  R
                  R                  5        H  u  p4UR                  n[        US 5      b  M   [        [        R                  R                  U5      nUc  MI  [        R                  R                  U5      =n(       d  Mq  [        R                  R                  XWS   5        M     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` insteadr   )r   r   r   knobsr   knob_descriptorsitemskeygetattrtoenvsetenv)r   hip_device_envsenvattrdescr,   valenv_vals           r   
_check_envr6       s    +02HI"Cc4 , I#  OZ  [  # ll))::@@B
hh#t$&,,--t4C$ll005575LL''QZ8 Cr   shadowtreecontextdatar   r   hooknamer:   r;   r<   c                >   [         R                  (       d)  [        R                  R                  R
                  (       a  gS[         l        U c  [        OU n Uc
  [        5       OUn[        X45      n[        U5        [        R                  " XX#U5      n[        U[        5      (       a  [        R                   " XW5        O8US:X  a   [        R                   " [#        5       U5        OUb  [%        SU< 35      eUS:X  a   [        R                   " ['        U5      U5        U$ )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.<suffix>", where suffix is the default
                          format according to the data type. For example, if data is "tree", the default name is "~/proton.hatchet".
    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", "trace"].
                          Defaults to "tree".
    backend (str, optional): The backend to use for profiling.
                             Available options are [None, "cupti", "roctracer", "instrumentation"].
                             Defaults to None, which automatically selects the backend matching the current active runtime.
    mode (Union[str, BaseMode], optional): The "mode" to use for profiling, which is specific to the backend.
                                           Can be a string or an instance of BaseMode (or any subclass thereof).
                                           Defaults to None.
                                           For "cupti", available options are [None, "pcsampling", "periodic_flushing"].
                                           For "roctracer", available options are ["periodic_flushing"].
                                           For "instrumentation", available options are [None].
                                           Each mode has a set of control knobs following with the mode name.
                                           For example, "periodic_flushing" mode has a knob:
                                           - format: The output format of the profiling results. Available options are ["hatchet", "hatchet_msgpack", "chrome_trace"]. Default is "hatchet".
                                           The can be set via `mode="periodic_flushing:format=chrome_trace"`.
    hook (Union[str, Hook], optional): The hook to use for profiling.
                                       You may pass either:
                                       - a string hook name, e.g. "triton" (kernel launch metadata), or
                                       - a custom Hook instance.
                                       Defaults to None.
Returns:
    session (Optional[int]): The session ID of the profiling session, or None if profiling is disabled.
NTr   zUnsupported hook: r    )r   command_liner   r)   r   disableprofiling_onDEFAULT_PROFILE_NAMEr   r%   r6   	libprotonstart
isinstancer
   r   registerr   r   r	   )r=   r:   r;   r   r   r<   mode_strsessions           r   rD   rD   3   s    f V\\0088E#'<TD#*?oGW+HwoodTHEG$T+		Z\73		-dX677##06@Nr   rH   c                     [         R                  (       a  U S:w  a  [        S5      e[        R                  " U 5        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   r   activaterC   activate_all)rH   s    r   rJ   rJ      sL     gl`aa! 7#r   flushingc                     [         R                  (       a  U S:w  a  [        S5      e[        R                  " U 5        U c  [
        R                  " U5        g[
        R                  " X5        g)aP  
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)
    flushing (bool): Whether to flush the profiling data before deactivating. Defaults to True.

Returns:
    None
r   zGOnly one session can be deactivated when running from the command line.N)r   r?   r   r   
deactivaterC   deactivate_all)rH   rL   s     r   rN   rN      sN     glbcc7#  *W/r   output_formatc                     [         R                  " U 5        U c"  S[        l        [        R
                  " U5        g[        R                  (       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.
                                   Available options are ["hatchet", "hatchet_msgpack", "chrome_trace"].

Returns:
    None
NFr   zEOnly one session can be finalized when running from the command line.)	r   
unregisterr   rA   rC   finalize_allr?   r   finalize)rH   rP   s     r   rT   rT      sV     7#"}-'Q,dee72r   c                 ^   ^ ^^^^^^ [         R                  " T 5      U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            	      J   > [        T
TTTT	TS9nT" U 0 UD6n[        U5        U$ )Nr9   )rD   rN   )argskwargsrH   retr   r:   r;   funcr<   r   r=   s       r   wrapper_profiling.<locals>.wrapper   s5    gD'PT[_`D#F#7
r   )	functoolswraps)rZ   r=   r:   r;   r   r   r<   r[   s   ``````` r   
_profilingr_      s,    & __T   Nr   r=   r:   r;   r   r   r<   c          
      L   ^^^^^^ U c  UUUUUU4S jnU$ [        U T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TS9$ )Nr`   r_   )fr   r:   r;   r<   r   r=   s    r   	decoratorprofile.<locals>.decorator   s    adG$PW^bimnnr   r`   rc   )rZ   r=   r:   r;   r   r   r<   re   s    `````` r   profilerg      s9    6 |	o 	o  $T7w]ahlmmr   )N)NF)Nr"   )Nr7   r8   NNN) r]   r   triton._C.libprotonr   rC   triton._C.libtritonr   r   hooksr   r   r	   
hooks.hookr
   r   r   typingr   r   rB   r#   r   r%   r6   intrD   rJ   boolrN   rT   r_   rg    r   r   <module>rp      sf     3 &  ? ?   " L L%3 %huS(]/C&D % %9 9 9( K & !+/'+K
3-K c]K 3-	K
 c]K 5h'
(K 5d#
$K c]K\$hsm $t $,0 0 0 0.3hsm 38C= 3RV 34 % !'+
3- c] 3-	
 c] 3- 5d#
$< 
#n % !'+#n 3-#n c]	#n
 3-#n c]#n 3-#n 5d#
$#nr   