o
    bi                     @   s   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eZdd	d
dZde	eef de
e fddZG d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                 C   sT   ddg}|   D ]\}}t|dkr|d| d|  q|d| |g7 }q|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_val r   S/home/ubuntu/.local/lib/python3.10/site-packages/ray/_private/runtime_env/nsight.pyparse_nsight_config   s   r   c                   @   s   e Zd ZdZdefddZdeeef deeef fddZ	e
fd	ee d
ddedejdef
ddZe
fdee d
ddedeej fddZdS )NsightPlugin_nsightresources_dirc                 C   s6   g | _ tj|\}}t|d d | _t| j d S )Nlogsnsight)r   ospathsplitr   _nsight_dirr   )selfr    session_dirruntime_dirr   r   r   __init__1   s   zNsightPlugin.__init__r   r   c                    s   t |}tt| jd |d< t|}zZ|tjddg }tj	|t
jt
jdI dH }| I dH \}}| dkr?| n| }d|d  d	g}tj	|t
jt
jdI dH }	|	 I dH \}
}
|jd
krkW dS d|fW S  tyy   Y dS 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   processr,   r-   	error_msgclean_up_cmdcleanup_process_r   r   r   _check_nsight_script9   s4   


z!NsightPlugin._check_nsight_scripturiruntime_env
RuntimeEnvcontextloggerc                    s   |  }|s	dS |rtjdkrtdt|tr(|dkr t}ntd| d| |I d H \}}|s@|| td| tt	| j
|dtd  |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"   r3   platformr
   
isinstancer2   NSIGHT_DEFAULT_CONFIGrC   warningr   r&   getr   r   )r'   rD   rE   rG   rH   r   is_valid_nsight_cmdr?   r   r   r   createb   s6   



zNsightPlugin.createurisc                 C   s    | d d| jd |_d S )NzRunning nsight profiler z python)infojoinr   py_executable)r'   rR   rE   rG   rH   r   r   r   modify_context   s   
zNsightPlugin.modify_contextN)__name__
__module____qualname__namer2   r*   r   r   boolrC   default_loggerr   r   loggingLoggerintrQ   r   rW   r   r   r   r   r   .   s<    


.
0r   )r5   r0   r^   r#   r7   r3   pathlibr   typingr   r   r   r   ray._common.utilsr    ray._private.runtime_env.contextr   ray._private.runtime_env.pluginr	   ray.exceptionsr
   	getLoggerrX   r]   rM   r2   r   r   r   r   r   r   <module>   s&    
