o
    Ơi<#                     @   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Zd dlZd dlm	Z	 d dl
mZmZmZ eeZdadadedeeef fdd	Zd
edee defddZdddZG dd de	ZdddZe  dS )    N)
ModuleType)AnyDictListF versionreturnc                 C   s8   |  d}t|dk r|d |d |d |d dS )N.   r   r         )majorminorpatch)splitlenappend)r   r    r   W/home/ubuntu/.local/lib/python3.10/site-packages/torch_tensorrt/_TensorRTProxyModule.py_parse_semver   s   

r   namepathsc                 C   s@   |D ]}t j|| }t j|r|  S qtd|  d| )NzCould not find z
  Search paths: )ospathjoinisfileFileNotFoundError)r   r   r   libpathr   r   r   	_find_lib   s   r   c            
      C   s
  t jdd } | d u s| dkr|  dkrtd d S tjds3t	dtj  t j
d d S g }dt jv rG|t jd t jj t jdkrT|d	 nt jd
kr`|d |D ]2}t jt j|drztjt j|dtjd |}W  n ty } zW Y d }~qbd }~ww qb|d u rt	d t j
d d S t j|dt jd< dd l}|t  d }t jt d| d}t j|dd t j|d}|t jd< t j|d}	t j|rt | t j|	rt |	 t d| d d S )N$TORCHTRT_ENABLE_TENSORRT_API_CAPTURE1truez+Capturing TensorRT API calls is not enabledlinuxz{Capturing TensorRT API calls is only supported on Linux, therefore ignoring the capture_tensorrt_api_recording setting for LD_LIBRARY_PATHx86_64/usr/lib/x86_64-linux-gnuaarch64/usr/lib/aarch64-linux-gnuzlibtensorrt_shim.so)modezCapturing TensorRT API calls is enabled, but libtensorrt_shim.so is not found, make sure TensorRT lib is in the LD_LIBRARY_PATH, therefore ignoring the capture_tensorrt_api_recording settingzlibnvinfer.soTRT_SHIM_NVINFER_LIB_NAMEr   torch_tensorrt_z/shimT)exist_okz	shim.jsonTRT_SHIM_OUTPUT_JSON_FILEzshim.binzRCapturing TensorRT API calls feature is enabled and the captured output is in the z
 directory)!r   environgetlower_LOGGERdebugsysplatform
startswithwarningpopextendr   r   pathsepuname	processorr   r   r   ctypesCDLLRTLD_GLOBAL	Exceptionpwdgetpwuidgetuidtempfile
gettempdirmakedirsexistsremoveinfo)
os_env_flaglinux_lib_pathr   tensorrt_lib_pather?   current_usershim_temp_dirjson_file_namebin_file_namer   r   r   %enable_capture_tensorrt_api_recording"   sl   









rP   c                   @   sB   e Zd ZdeddfddZdedefddZdee fd	d
Z	dS )TensorRTProxyModuletarget_moduler   Nc                 C   s   t jjdd d}|| _|j| _|j| _|j| _|j| _|j| _|| _	d | _
d| _t|dr2|j| _
n	t|dr;|j| _
| j
rD| j
| _d S d S )Ntensorrt)loaderr   tensorrt_rtx)	importlibutilspec_from_loader__spec____package____path____file__
__loader____version___target_module_nested_module_package_namehasattrrU   rS   )selfrR   specr   r   r   __init__c   s"   


zTensorRTProxyModule.__init__r   c                 C   sF   zt | j|W S  ty"   td|  |dkr!| jr!| j Y S  w )NzAttributeError: rS   )getattrr_   AttributeErrorprintr`   )rc   r   r   r   r   __getattr__{   s   
zTensorRTProxyModule.__getattr__c                 C   s
   t | jS )N)dirr_   )rc   r   r   r   __dir__   s   
zTensorRTProxyModule.__dir__)
__name__
__module____qualname__r   re   strr   ri   listrk   r   r   r   r   rQ   b   s    rQ   c               
   C   sj  t rd S d} tjd }d ur| dkrd} | rdnda| s#t  ztt}t	|}t|_
|tjd< da W d S  ty4 } ztdtd|  td	tjd
  | r}ddlm} t|}|d }|d }d| d| dgd| gd}	n)ddlm}
 t|
}|d }|d }d| dd| dgd| d| gd}	ddlm} tjdr|	d }tjd tjj}|D ]
}tt|| qnTtjdrd| ddd g}d
tjv r|tjd
 tjj7 }t jd!kr|d"g7 }nt jd#kr	|d$g7 }|	d }|D ]}tt|| qW Y d }~d S W Y d }~d S W Y d }~d S d }~ww )%NFUSE_TRT_RTXr!   TrU   rS   z-import error when try to import package_name=z got error z2make sure tensorrt lib is in the LD_LIBRARY_PATH: r#   r   )__tensorrt_rtx_version__r   r   tensorrt_rtx__z.dllzlibtensorrt_rtx.so.)winr"   )__tensorrt_version__nvinfer_nvinfer_plugin_zlibnvinfer.so.zlibnvinfer_plugin.so.)__cuda_version__ru   PATHr"   z/usr/local/cuda-z/lib64z/usr/libz
/usr/lib64r$   r%   r&   r'   )package_importedr   r-   r.   r/   package_namerP   rV   import_modulerQ   ra   r2   modulesImportErrorrh   torch_tensorrt._versionrr   r   rv   torch_tensorrtry   r3   r4   r   r   r8   r;   r<   r   r9   r:   )use_rtxuse_rtx_env_varrR   proxyrK   rr   tensorrt_versiontensorrt_majortensorrt_minortensorrt_librv   ry   WIN_LIBS	WIN_PATHSlibLINUX_PATHS
LINUX_LIBSr   r   r   alias_tensorrt   s   


	




r   )r   N)r;   rV   importlib.utilloggingr   r3   r2   rB   typesr   typingr   r   r   	getLoggerrl   r0   r{   r|   ro   r   r   rP   rQ   r   r   r   r   r   <module>   s&    

	@
(
\