
    &`i                         d dl Z d dlZ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	m
Z
mZmZ d dlmZ d dlmZ d dlmZ d dlmZ  ej        e          Zdd	d
dZde	eef         de
e         fdZ G d de          ZdS )    N)Path)DictListOptionalTuple)try_to_create_directory)RuntimeEnvContext)RuntimeEnvPlugin)RuntimeEnvSetupErrorzcuda,cudnn,cublas,nvtxz'worker_process_%p'true)tozstop-on-exitnsight_configreturnc                     ddg}|                                  D ]>\  }}t          |          dk    r|                    d| d|            4|d| |gz  }?|S )z
    Function to convert dictionary of nsight options into
    nsight command line

    The function returns:
    - List[str]: nsys profile cmd line split into list of str
    nsysprofile   z--=-)itemslenappend)r   
nsight_cmdoption
option_vals       s/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/runtime_env/nsight.pyparse_nsight_configr      s     )$J+1133 5 5
 v;;??8688J889999<v<<44JJ    c                       e Zd ZdZdefdZdeeef         deeef         fdZ	e
fdee         dd	d
edej        def
dZe
fdee         dd	d
edeej                 fdZdS )NsightPlugin_nsightresources_dirc                     g | _         t          j                            |          \  }}t	          |          dz  dz  | _        t          | j                   d S )Nlogsnsight)r   ospathsplitr   _nsight_dirr   )selfr#   session_dirruntime_dirs       r   __init__zNsightPlugin.__init__1   sT     $&7==#?#? [,,v5@ 011111r   r   r   c                   K   t          j        |          }t          t          | j                  dz            |d<   t          |          }	 |t          j        ddgz   }t          j	        |t          j        t          j        d d{V }|                                 d{V \  }}|                                dk    r|                                n|                                }d|d          d	g}t          j	        |t          j        t          j        d d{V }	|	                                 d{V \  }
}
|j        d
k    rdS d|fS # t          $ r Y dS w xY w)a]  
        Function to validate if nsight_config is a valid nsight profile options
        Args:
            nsight_config: dictionary mapping nsight option to it's value
        Returns:
            a tuple consists of a boolean indicating if the nsight_config
            is valid option and an error message if the nsight_config is invalid
        emptyr   z-cz"")stdoutstderrN rmz	.nsys-repr   )TNF)Fznsight is not installed)copydeepcopystrr   r*   r   sys
executableasynciocreate_subprocess_exec
subprocessPIPEcommunicatestrip
returncodeFileNotFoundError)r+   r   nsight_config_copyr   processr1   r2   	error_msgclean_up_cmdcleanup_process_s              r   _check_nsight_scriptz!NsightPlugin._check_nsight_script9   s      "]=99"%d4+;&<&<w&F"G"G3();<<
	6#s~tT&BBJ#:!!        G
 $+#6#6#8#8888888NFF*0,,..B*>*>FLLNNI !%7%<"G"G"GHL$+$B!!% % %      O
 )4466666666DAq!Q&&!zi''  	6 	6 	6555	6s   C<E E 
EEuriruntime_env
RuntimeEnvcontextloggerc                 *  K   |                                 }|sdS |rt          j        dk    rt          d          t	          |t
                    r!|dk    rt          }nt          d| d          |                     |           d {V \  }}|s'|                    |           t          d|           t          t          | j
                  |                    dt          d                   z            |d<   t          |          | _        dS )	Nr   linuxzNsight CLI is only available in Linux.
More information can be found in https://docs.nvidia.com/nsight-compute/NsightComputeCli/index.htmldefaultzUnsupported nsight config: zC. The supported config is 'default' or Dictionary of nsight optionsz@nsight profile failed to run with the following error message:
 r   )r&   r8   platformr   
isinstancer7   NSIGHT_DEFAULT_CONFIGrH   warningr   r*   getr   r   )r+   rI   rJ   rL   rM   r   is_valid_nsight_cmdrD   s           r   createzNsightPlugin.createb   sa      $**,, 	1 	S\W44&U   mS)) 		)) 5*3- 3 3 3   04/H/H/W/W)W)W)W)W)W)W&Y" 	NN9%%%&0$-0 0  
 !!""]%6%6s<QRU<V%W%WW
 
c .m<<qr   urisc                 t    |                     d           d                    | j                  dz   |_        d S )NzRunning nsight profiler z python)infojoinr   py_executable)r+   rX   rJ   rL   rM   s        r   modify_contextzNsightPlugin.modify_context   s8     	-... # 9 9I Er   N)__name__
__module____qualname__namer7   r.   r   r   boolrH   default_loggerr   r	   loggingLoggerintrW   r   r^    r   r   r!   r!   .   s       D2c 2 2 2 2'6!#s(^'6	tSy	'6 '6 '6 '6\ "0) )c]) ") #	)
 ) 
) ) ) )` ,:F F3iF "F #	F
 (F F F F F Fr   r!   )r:   r5   re   r'   r<   r8   pathlibr   typingr   r   r   r   ray._common.utilsr    ray._private.runtime_env.contextr	   ray._private.runtime_env.pluginr
   ray.exceptionsr   	getLoggerr_   rd   rS   r7   r   r!   rh   r   r   <module>rp      sk      				     



       . . . . . . . . . . . .      ? > > > > > < < < < < < / / / / / /""8,, 
"	  tCH~ $s)    &gF gF gF gF gF# gF gF gF gF gFr   