o
    Ơi                     @  s   d dl mZ d dlZd dlZd dlmZmZmZ ejdkr$d dlm	Z	 nd dl
m	Z	 d dlZd dlZd dlmZ d dlmZ G dd	 d	eZdS )
    )annotationsN)AnyOptionalTuple)      )Self)
DeviceType)needs_torch_tensorrt_runtimec                   @  s   e Zd ZU dZejZded< dZded< dZ	ded< dZ
d	ed
< d+ddZd,ddZd,ddZed-ddZed.ddZed/ddZed0d"d#Zd1d&d'Zed,d(d)Zd*S )2Devicea  
    Defines a device that can be used to specify target devices for engines

    Attributes:
        device_type (torch_tensorrt.DeviceType): Target device type (GPU or DLA). Set implicitly based on if dla_core is specified.
        gpu_id (int): Device ID for target GPU
        dla_core (int): Core ID for target DLA core
        allow_gpu_fallback (bool): Whether falling back to GPU if DLA cannot support an op should be allowed
    r	   device_typeintgpu_iddla_coreFboolallow_gpu_fallbackargsr   kwargsc                 O  sR  t |dkr1t|d tstdt|d \| _}| jtjkr-|| _	d| _
td nN|| _
nJt |dkrqd|v s?d|v rmd|v rH|d | _	d|v rQ|d | _
| j	dkrhtj| _| j
dkrgd| _
td ntj| _ntdtd	t | d
d|v rt|d tstd|d | _d|v rt|d tjrt|d | _dS dS dS )a  __init__ Method for torch_tensorrt.Device

        Device accepts one of a few construction patterns

        Args:
            spec (str): String with device spec e.g. "dla:0" for dla, core_id 0

        Keyword Arguments:
            gpu_id (int): ID of target GPU (will get overridden if dla_core is specified to the GPU managing DLA). If specified, no positional arguments should be provided
            dla_core (int): ID of target DLA core. If specified, no positional arguments should be provided.
            allow_gpu_fallback (bool): Allow TensorRT to schedule operations on GPU if they are not supported on DLA (ignored if device type is not DLA)

        Examples:
            - Device("gpu:1")
            - Device("cuda:1")
            - Device("dla:0", allow_gpu_fallback=True)
            - Device(gpu_id=0, dla_core=0, allow_gpu_fallback=True)
            - Device(dla_core=0, allow_gpu_fallback=True)
            - Device(gpu_id=1)
           r   zHWhen specifying Device through positional argument, argument must be strzJSetting GPU id to 0 for device because device 0 manages DLA on AGX Devicesr   r   zLSetting GPU id to 0 for device because device 0 manages DLA on AGX PlatformszfEither gpu_id or dla_core or both must be defined if no string with device specs is provided as an argzFUnexpected number of positional arguments for class Device 
    Found zA arguments, expected either zero or a single positional argumentsr   z!allow_gpu_fallback must be a boolr   N)len
isinstancestr	TypeErrorr   _parse_device_strr   r	   DLAr   r   loggingwarningGPU
ValueErrorr   r   trt_from)selfr   r   id r$   J/home/ubuntu/.local/lib/python3.10/site-packages/torch_tensorrt/_Device.py__init__&   sV   





zDevice.__init__returnr   c                 C  sB   | j tjkrdn
d| j d| j d}d| j  d| j | }|S )N)z, dla_core=z, allow_gpu_fallback=zDevice(type=z	, gpu_id=)r   r	   r   r   r   r   )r"   suffixdev_strr$   r$   r%   __str__n   s   zDevice.__str__c                 C  s   |   S N)r+   )r"   r$   r$   r%   __repr__w   s   zDevice.__repr__d#Optional[Self | torch.device | str]c                 C  sX   t |tr|S t |tjr|jdkrtd| |jdS |du r(| tj dS | |S )zmCast a device-type to torch_tensorrt.Device

        Returns the corresponding torch_tensorrt.Device
        cudaz(Torch Device specs must have type "cuda"r   N)	r   r   torchdevicetyper   indexr0   current_device)clsr.   r$   r$   r%   r!   z   s   

zDevice._from	torch_devtorch.devicec                 C  s
   |  |S r,   )r!   )r7   r8   r$   r$   r%   _from_torch_device   s   
zDevice._from_torch_devicec                 C  s   t j }| |dS )Nr1   )r2   r0   r6   )r7   dev_idr$   r$   r%   _current_device   s   

zDevice._current_devicesTuple[trt.DeviceType, int]c                 C  sl   |   } | d}|d dks|d dkrtjt|d fS |d dkr-tjt|d fS td|d  )N:r   gpur0   r   dlazUnknown device type )lowersplitr	   r   r   r   r   )r=   specr$   r$   r%   r      s   
zDevice._parse_device_strtr4   c                 C  s0   |t jkr| jdkrt | jS tdtd)Nr   z4Invalid GPU ID provided for the CUDA device providedz-Unsupported target type for device conversion)r2   r3   r   r   r   )r"   rE   r$   r$   r%   to   s
   

z	Device.toc                 C  sn   t jj d }t j| j}| j|j|jt	| j
tj|jg}dd |D }||}td|  |S )Nr   c                 S  s   g | ]}t |qS r$   )r   ).0ir$   r$   r%   
<listcomp>   s    z3Device._to_serialized_rt_device.<locals>.<listcomp>zSerialized Device Info: )r2   opstensorrtSERIALIZED_RT_DEVICE_DELIMr0   get_device_propertiesr   majorminorr   r   rF   r    r	   namejoinr   debug)r"   delimdev_infort_infopacked_rt_infor$   r$   r%   _to_serialized_rt_device   s   
zDevice._to_serialized_rt_deviceN)r   r   r   r   )r'   r   )r.   r/   r'   r   )r8   r9   r'   r   )r'   r   )r=   r   r'   r>   )rE   r4   r'   r9   )__name__
__module____qualname____doc__r	   UNKNOWNr   __annotations__r   r   r   r&   r+   r-   classmethodr!   r:   r<   staticmethodr   rF   r
   rW   r$   r$   r$   r%   r      s,   
 



H
	

	r   )
__future__r   r   systypingr   r   r   version_infor   typing_extensionsrK   r    r2   torch_tensorrt._enumsr	   torch_tensorrt._featuresr
   objectr   r$   r$   r$   r%   <module>   s    
