o
    Ơi                     @   s   d dl Z d dlmZmZmZ d dlZd dlZe eZ	dddZ
dededejjd	ejjdef
d
dZdeded	ejjdeeejjf fddZdeded	ejjdeeeejjf  fddZdS )    N)ListOptionalTuplereturnc                   C   s<   t jjjstj dkrtdtj	  d d S d S d S )N   a_  Detected this engine is being instantitated in a multi-GPU system with multi-device safe mode disabled. For more on the implications of this as well as workarounds, see the linked documentation (https://pytorch.org/TensorRT/user_guide/runtime.html#multi-device-safe-mode). The engine is set to be instantiated on the current default cuda device, cuda:zd. If this is incorrect, please set the desired cuda device via torch.cuda.set_device(...) and retry.)
torch_tensorrtruntime_multi_device_safe_mode_PY_RT_MULTI_DEVICE_SAFE_MODEtorchcudadevice_countloggerwarningcurrent_device r   r   Q/home/ubuntu/.local/lib/python3.10/site-packages/torch_tensorrt/runtime/_utils.pymulti_gpu_device_check
   s   r   curr_device_idengine_device_idcurr_device_propertiesengine_device_propertiesc                 C   s   |j |jf|j |jfkr"td|j |jf d|j |jf d dS |j|jkr8td|j d|j d dS | |krJtd| d	|  d dS d
S )zODetermines whether a device switch is required based on input device parameterszConfigured SM capability z2 does not match with current device SM capability z. Switching device context.TzProgram compiled for z9 but current CUDA device is current device SM capability z?. Attempting to switch device context for better compatibility.zConfigured Device ID: z& is different than current device ID: F)majorminorr   r   name)r   r   r   r   r   r   r   _is_switch_required   s2   


r   c                 C   s    t | ||}|dusJ d|S )z@Wraps compatible device check and raises error if none are foundNzBCould not find a compatible device on the system to run TRT Engine)_get_most_compatible_device)r   r   r   new_target_device_optr   r   r   _select_rt_deviceB   s   
r   c           	         s   dd t tj D }td|  |j|jf  fdd|D }td|  t|dkr9|r7|d S dS d}|D ]"}|\}}|j	|j	kr_|| krR|} |S ||krY|}q=|du r_|}q=|S )	z6Selects a runtime device based on compatibility checksc                 S   s   g | ]
}|t j|fqS r   )r   r   get_device_properties).0ir   r   r   
<listcomp>Y   s    z/_get_most_compatible_device.<locals>.<listcomp>zAll available devices: c                    s(   g | ]\}}|j |jf kr||fqS r   )r   r   )r    r!   device_propertiestarget_device_smr   r   r"   a   s
    zFound candidate devices: r   r   N)
ranger   r   r   r   debugr   r   lenr   )	r   r   r   all_devicescandidate_devices
best_match	candidater!   r#   r   r$   r   r   S   s2   
r   )r   N)loggingtypingr   r   r   r   r   	getLogger__name__r   r   int_C_CudaDevicePropertiesboolr   r   r   r   r   r   r   <module>   sF    


(
