o
    Ơi                     @  s   d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	 d dl
Z
d dlmZmZ d dlmZ d dlmZ d dlmZmZ eeZ	d#ddd	d$ddZd%ddZd&ddZd'd!d"ZdS )(    )annotationsN)	signature)AnyOptionalTupleUnion)Dimexport)Input)default_device)get_torch_inputsto_torch_device)
arg_inputskwarg_inputsmod&torch.nn.Module | torch.fx.GraphModuleinputsOptional[Tuple[Any, ...]]r   r   Optional[dict[Any, Any]]kwargsr   returntorch.export.ExportedProgramc          
   	   K  s   |du r|du rt d|dur|durt d|p|}|du r"i }t|dt }t||}t||}t| |}|t| t| t	||||ddd}	|	S )a
	  Exports a ``torch.export.ExportedProgram`` from a ``torch.nn.Module`` or ``torch.fx.GraphModule`` specifically targeting being compiled with Torch-TensorRT

    Exports a ``torch.export.ExportedProgram`` from either a ``torch.nn.Module`` or torch.fx.GraphModule``. Runs specific operator decompositions geared towards
    compilation by Torch-TensorRT's dynamo frontend.

    Arguments:
        mod (torch.nn.Module | torch.fx.GraphModule): Source module to later be compiled by Torch-TensorRT's dynamo fronted
        inputs (Tuple[Any, ...]): List of specifications of input shape, dtype and memory layout for inputs to the module. This argument is required. Input Sizes can be specified as torch sizes, tuples or lists. dtypes can be specified using
            torch datatypes or torch_tensorrt datatypes and you can use either torch devices or the torch_tensorrt device type enum
            to select device type. ::

                input=[
                    torch_tensorrt.Input((1, 3, 224, 224)), # Static NCHW input shape for input #1
                    torch_tensorrt.Input(
                        min_shape=(1, 224, 224, 3),
                        opt_shape=(1, 512, 512, 3),
                        max_shape=(1, 1024, 1024, 3),
                        dtype=torch.int32
                        format=torch.channel_last
                    ), # Dynamic input shape for input #2
                    torch.randn((1, 3, 224, 244)) # Use an example tensor and let torch_tensorrt infer settings
                ]
    Keyword Arguments:
        arg_inputs (Tuple[Any, ...]): Same as inputs. Alias for better understanding with kwarg_inputs.
        kwarg_inputs (dict[Any, ...]): Optional, kwarg inputs to the module forward function.
        device (Union(torch.device, dict)): Target device for TensorRT engines to run on ::

            device=torch.device("cuda:0")

        debug (bool): Enable debuggable engine
        enable_experimental_decompositions (bool): Use the full set of operator decompositions. These decompositions may not be tested but serve to make the graph easier to convert to TensorRT, potentially increasing the amount of graphs run in TensorRT.
        **kwargs: Any,
    Returns:
        torch.fx.GraphModule: Compiled FX Module, when run it will execute via TensorRT
    Nz2'arg_inputs' and 'inputs' should not both be None.z>'arg_inputs' and 'inputs' should not be used at the same time.devicestrictF)r   dynamic_shapesr   )
AssertionErrorr   getr   r   get_dynamic_shapes_argsupdateget_dynamic_shapes_kwargsr	   tuple)
r   r   r   r   r   r   torch_arg_inputstorch_kwarg_inputsr   exp_program r$   Q/home/ubuntu/.local/lib/python3.10/site-packages/torch_tensorrt/dynamo/_tracer.pytrace   s,   -



r&    Union[dict[str, Any], list[Any]]c                 C  s   t | tri }|  D ]
\}}t|||< q|S t | tr!t| S t | ttfr8g }| D ]	}|t| q,|S t	dt
|  d)NzUnknown type .)
isinstancedictitemsr   r
   get_dynamic_shapeslistr    append	TypeErrortype)r   dynamic_shapes_kwargkvr   inputr$   r$   r%   r   Z   s   

r   torch.nn.Moduledict[str, Any]c                 C  sH   t t| jj }i }t||d t| D ]
\}}t|||< q|S N)r-   r   forward
parameterskeysziplenr,   )r   r   argsr   r4   
input_namer$   r$   r%   r   m   s
   r   r4   r
   dict[Any, Any]c                 C  s   t | tsi S i }| jtjjkrc| jd }| jd }| jd }t|t|  kr0t|ks3J  J tt|D ])}|| ||   krK|| krNn nq9t| j	d t
| || || d||< q9|S )N	min_shape	opt_shape	max_shape_)minmax)r)   r
   
shape_mode
_ShapeModeDYNAMICshaper<   ranger   namestr)r4   dynamic_dimsr@   rA   rB   dimr$   r$   r%   r,   w   s"   



($r,   r7   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r'   )r   r5   r   r   r   r6   )r4   r
   r   r?   )
__future__r   logginginspectr   typingr   r   r   r   torchtorch.exportr   r	   torch_tensorrt._Inputr
   torch_tensorrt.dynamo._defaultsr   torch_tensorrt.dynamo.utilsr   r   	getLogger__name__loggerr&   r   r   r,   r$   r$   r$   r%   <module>   s$    

J

