o
    Ơi                     @   s  U d dl mZ d dlmZmZmZmZmZ d dlZ	d dl
Zd dlZd dlmZ d dlmZ d dlmZ d dlmZmZ d dlmZ d	d
lmZmZ G dd deZejeje	jejejejejiej eje	j ejej ejej iej!eje	j!ejej!ejej!iej"eje	j"ejej"ejej"iej#eje	j#ejej#ejej#iej$eje$ejej$ejej$iiZ%eeeeeee	j&ej&f f f e'd< ej(dkreje	j)ejej$ejej$ie%ej$< G dd deZ*deeej&e	j&f dedee	j&ej&ef fddZ+dedee, fddZ-dd Z.dde/fddZ0dS )     )Enum)CallableDictListOptionalUnionN)make_fx)functionalize)sanitized_torch_version)replace_op_with_indicesrun_const_fold)version   )ShapeTRTDataTypec                   @   s   e Zd ZdZdZdZdS )
FrameworksnumpytorchtrtN)__name__
__module____qualname__NUMPYTORCHTRT r   r   K/home/ubuntu/.local/lib/python3.10/site-packages/torch_tensorrt/fx/utils.pyr      s    r   DataTypeEquivalencez7.0c                   @   s6   e Zd ZdZdZdZdZedede	d  fddZ
d	S )
LowerPrecisionfp32fp16int8bf16labelreturnc                 C   s<   | dv rt jS | dv rt jS | dv rt jS | dv rt jS d S )N)r   float32floatztorch.float32)r    float16halfz
torch.halfztorch.float16r!   )r"   bfloat16ztorch.bfloat16)r   FP32FP16INT8BF16)r#   r   r   r   from_strM   s   zLowerPrecision.from_strN)r   r   r   r*   r+   r,   r-   staticmethodstrr   r.   r   r   r   r   r   G   s    r   dtypetor$   c                 C   s
  |t v sJ d| ttjdd }| tjtjtjfv r&ttj | S |dkr;| tj	tj
tj
fv r;ttj
 | S | tjtjtjfv rLttj | S | tjtjtjfv r]ttj | S | tjtjtjfv rnttj | S | tjtjtjfv rttj | S td|  )aM  
    Convert TensorRT, Numpy, or Torch data types to any other of those data types.

    Args:
        dtype (TRTDataType, torch.dtype, np.dtype): A TensorRT, Numpy, or Torch data type.
        to (Frameworks): The framework to convert the data type to.

    Returns:
        The equivalent data type in the requested framework.
    z.Expected valid Framework for translation, got .r      z%s is not a supported dtype)r   intr   __version__splitnpr!   r   r   bool_boolint32int64r'   r%   	TypeError)r1   r2   trt_major_versionr   r   r   unified_dtype_converter[   s   r?   shapec                 C   s,   g }t | D ]\}}|dkr|| q|S )aB  
    This function finds the dynamic dimensions in the given
    shape. A dimension is dynamic if it's -1.

    Args:
        shape (Shape): A sequence of integer that represents
            the shape of a tensor.

    Returns:
        A list of integers contains all the dynamic dimensions
        in the given shape
    )	enumerateappend)r@   dynamic_dimsisr   r   r   get_dynamic_dimsz   s   
rG   c                    sB        fdd}tt||   j  t  t   S )Nc                     s    |  S )Nr   )inpmodr   r   f   s   zproxytensor_trace.<locals>.f)evalr   r	   grapheliminate_dead_coder   r   )rJ   inputsrK   r   rI   r   proxytensor_trace   s   
rP   2.devmin_torch_versionc                    s   dt f fdd}|S )ak  
    Create a decorator which verifies the Torch version installed
    against a specified version range

    Args:
        min_torch_version (str): The minimum required Torch version
        for the decorated function to work properly

    Returns:
        A decorator which raises a descriptive error message if
        an unsupported Torch version is used
    rK   c                    s    fdd}|S )Nc                     sN   t }t t }||k r td dd  dtj   | i |S )NzExpected Torch version z or greater, zwhen calling z. Detected version )r   parser
   AssertionErrorr   r6   )argskwargsmin_versioncurrent_version)rK   rR   r   r   function_wrapper   s   

zEreq_torch_version.<locals>.nested_decorator.<locals>.function_wrapperr   )rK   rY   rR   )rK   r   nested_decorator   s   z+req_torch_version.<locals>.nested_decorator)r   )rR   r[   r   rZ   r   req_torch_version   s   r\   )rQ   )1enumr   typingr   r   r   r   r   r   r8   tensorrtr   r   	functorchr   functorch.experimentalr	   torch_tensorrt._utilsr
   )torch_tensorrt.fx.passes.lower_basic_passr   r   	packagingr   typesr   r   r   r!   r   r   r   r;   r<   r'   r%   r:   r   r1   __annotations__r6   r9   r   r?   r5   rG   rP   r0   r\   r   r   r   r   <module>   s|    	
#

