o
    ƠiG                     @  s\  d dl m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mZmZmZmZ d dlZd dlmZ d dlmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ ejrzd dl Zd dl!m"Z" d dl#m$Z% d dl&m'Z' eeej(e"f Z)neeej(f Z)ej*rd dl+Z,d dl-m$Z. d dl-m/Z0 ej1rd dl2m3Z3 d dl4m$Z5 d dl4m6Z7 d dl4m8Z9 d dl4m:Z; d dl4m<Z= d dl>m?Z@ eAeBZCg dZDdTddZEG dd deZFG dd deZGdUd"d#ZHdVd'd(ZI	)				dWdXd3d4Z$e				dYdZd9d:Z8d[d;d<ZJ	=				)	d\d]d@dAZ/d^d_dCdDZ:d^d_dEdFZK	Bd^dGddddHdIdJd`dRdSZLdS )a    )annotationsN)Enum)AnyCallableListOptionalSequenceSetUnion)dtype)ENABLED_FEATURESneeds_cross_compile)Input)	_defaults)CudaGraphsTorchTensorRTModule)	TypeGuard)InputTensorSpec)compile)LowerPrecision)convert_method_to_trt_engine)ExportedProgram)1convert_exported_program_to_serialized_trt_engine)cross_compile_for_windows)$load_cross_compiled_exported_program)$save_cross_compiled_exported_program)trace)r   r   r   r   saveloadinputsSequence[Input | torch.Tensor]return%TypeGuard[List[Input | torch.Tensor]]c                 C     t dd | D S )Nc                 s       | ]}t |tjtfV  qd S N)
isinstancetorchTensorr   .0i r+   K/home/ubuntu/.local/lib/python3.10/site-packages/torch_tensorrt/_compile.py	<genexpr>E       z*_non_fx_input_interface.<locals>.<genexpr>allr   r+   r+   r,   _non_fx_input_interfaceB      r2   c                   @  s$   e Zd ZdZdZdZdZdZdZdS )_IRTypez?Enum to determine the type of IR selected for model compilationr               N)	__name__
__module____qualname____doc__tsfxdynamotorch_compileexported_programr+   r+   r+   r,   r4   H   s    r4   c                   @  s    e Zd ZdZdZdZdZdZdS )_ModuleTypez5Enum to determine the type of model provided as inputr   r5   r6   r7   N)r9   r:   r;   r<   nnr=   r>   epr+   r+   r+   r,   rB   R   s    rB   moduler   c                   sh   t  fddtjjtjjfD rtjS t tjj	rtjS t t
r&tjS t tjjr0tjS td)Nc                 3  s    | ]}t  |V  qd S r$   )r%   r)   trE   r+   r,   r-   \   s
    
z%_parse_module_type.<locals>.<genexpr>zModule is an unknown format)anyr&   jitScriptModuleScriptFunctionrB   r=   r%   r>   GraphModuler   rD   rC   ModuleRuntimeErrorrH   r+   rH   r,   _parse_module_type[   s   
rP   module_typeirstrc           	        s~  t fddtjtjfD }t fddtjtjfD }tjk}t  fdddD } dk} dk} dk}|rH|rHtjrDtjS t	d	|r^|r^t
jd
tdd tjrZtjS t	d|sb|rn|rntjrjtjS t	d|r||r|tjrxtjS t	d dkrtjr|rtd tjS tjr|rtjrtd tjS tjr|rtd tjS t	dtj dtj dtj dt	d)Nc                 3      | ]} |kV  qd S r$   r+   rF   rQ   r+   r,   r-   l       z!_get_target_fe.<locals>.<genexpr>c                 3  rT   r$   r+   rF   rU   r+   r,   r-   m   rV   c                 3  rT   r$   r+   )r)   opt)rR   r+   r,   r-   p   rV   )torchscriptr=   r>   r?   r@   zdRequested using the TS frontend but the TS frontend is not available in this build of Torch-TensorRTBFX frontend is deprecated. Please use the Dynamo frontend instead.r6   
stacklevelzdRequested using the FX frontend but the FX frontend is not available in this build of Torch-TensorRTzlRequested using the Dynamo frontend but the Dynamo frontend is not available in this build of Torch-TensorRTzRequested using the Torch-TensorRT torch.compile backend but the Torch-TensorRT torch.compile backend is not available in this build of Torch-TensorRTdefaultz,ir was set to default, using dynamo frontendzInput is a torchscript module but the ir was not specified (default=dynamo), please set ir=torchscript to suppress the warning.zLModule was provided in an unsupported format
Installed frontends:
	Dynamo - z
	TorchScript - z
	FX - )zUnknown ir was requested)rI   rB   rC   r=   r>   rD   r   torchscript_frontendr4   
ValueErrorwarningswarnDeprecationWarningfx_frontenddynamo_frontendr?   r@   loggerinfowarning)	rQ   rR   module_is_tsablemodule_is_fxablemodule_is_exportableir_targets_torchscriptir_targets_fxir_targets_dynamoir_targets_torch_compiler+   )rR   rQ   r,   _get_target_fek   sj   





ro   r\   Optional[Sequence[InputType]]
arg_inputs!Optional[Sequence[Sequence[Any]]]kwarg_inputsOptional[dict[Any, Any]]enabled_precisions(Optional[Set[Union[torch.dtype, dtype]]]kwargsTtorch.nn.Module | torch.jit.ScriptModule | torch.fx.GraphModule | Callable[..., Any]c                 K  s
  |dur|ng }|dur|nt j}t| }	t|	|}
|
tjkrD| }|	tjkr1t	d t
j| }t|s7J t|f||d|}|S |
tjkrtjdtdd tjsXtdt
j|v sctjj|v rgtj}nt
j|v srtjj|v rvtj}ntd| d	ddd}||sJ t | |f|ddd|}|S |
tj!kr|du r|du rt"d|dur|durt"d|dur|}|du ri }ddl#m$} t%|t&j'j(s|g}||}||}t)| |fd|i|}t*|f||d|}|S |
tj+krt+| fd|i|S td)a  Compile a PyTorch module for NVIDIA GPUs using TensorRT

    Takes a existing PyTorch module and a set of settings to configure the compiler
    and using the path specified in ``ir`` lower and compile the module to TensorRT
    returning a PyTorch Module back

    Converts specifically the forward method of a Module

    Arguments:
        module (Union(torch.nn.Module,torch.jit.ScriptModule): Source module

    Keyword Arguments:
        inputs (List[Union(torch_tensorrt.Input, torch.Tensor)]): **Required** 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. ::

                inputs=[
                    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
                ]
        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.
        enabled_precision (Set(Union(torch.dtype, torch_tensorrt.dtype))): The set of datatypes that TensorRT can use when selecting kernels
        ir (str): The requested strategy to compile. (Options: default - Let Torch-TensorRT decide, ts - TorchScript with scripting path)
        **kwargs: Additional settings for the specific requested strategy (See submodules for more info)

    Returns:
        torch.nn.Module: Compiled Module, when run it will execute via TensorRT
    NModule was provided as a torch.nn.Module, trying to script the module with torch.jit.script. In the event of a failure please preconvert your module to TorchScript)r   ru   rY   r6   rZ   z<FX frontend is not enabled, cannot compile with target_ir=fxz
Precision z not supported on FXr   0Sequence[Input | torch.Tensor | InputTensorSpec]r    /TypeGuard[List[InputTensorSpec | torch.Tensor]]c                 S  r"   )Nc                 s  r#   r$   )r%   r&   r'   r   r(   r+   r+   r,   r-     r.   z7compile.<locals>._fx_input_interface.<locals>.<genexpr>r/   r1   r+   r+   r,   _fx_input_interface  r3   z$compile.<locals>._fx_input_interfaceTF)lower_precisionexplicit_batch_dimensiondynamic_batch2'arg_inputs' and 'inputs' should not both be None.>'arg_inputs' and 'inputs' should not be used at the same time.r   prepare_inputsrs   rq   ru   ru   :Module is an unknown format or the ir requested is unknown)r   rz   r    r{   ),r   ENABLED_PRECISIONSrP   ro   r4   r=   rB   rC   re   rf   r&   rJ   scriptr2   torchscript_compiler>   r`   ra   rb   r   rc   rO   float16torch_tensorrtr   halfr   FP16float32floatFP32r_   
fx_compiler?   AssertionErrortorch_tensorrt.dynamo.utilsr   r%   collectionsabcr   dynamo_tracedynamo_compiler@   )rE   rR   r   rq   rs   ru   rw   
input_listenabled_precisions_setrQ   	target_irts_modcompiled_ts_moduler}   r|   compiled_fx_moduler   torchtrt_arg_inputstorchtrt_kwarg_inputsexp_programtrt_graph_moduler+   r+   r,   r      s   0







r   torch.nn.Module	file_path(Optional[Sequence[Input | torch.Tensor]]Nonec                 K  s  t  dkst  d dkrtdt  dt  d |s$td|dur*|ntj}|du r9|du r9td|durE|durEtd	|pH|}|du rOi }dd
lm	} t
|tjjs_|g}||}	||}
t| |	fd|
i|}td t|f|	|d|}t|| td dS )a  Compile a PyTorch module using TensorRT in Linux for Inference in Windows

    Takes an existing PyTorch module and a set of settings to configure the compiler
    and it will convert methods to AOT graphs which call equivalent TensorRT serialized
    engine info into the disk in the specified file_path user provided.
    It will then allow user to load the deserialized model from the disk in Windows.
    Note: the model cross compiled for windows in Linux environmen can only be loaded
    in Windows.

    Argument:
        module (torch.nn.Module): Source module
        file_path (str): the file path to store the serialized module into the disk

    Keyword Arguments:
        inputs (List[Union(torch_tensorrt.Input, torch.Tensor)]): **Required** 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. ::

                inputs=[
                    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
                ]
        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.
        enabled_precision (Set(Union(torch.dtype, torch_tensorrt.dtype))): The set of datatypes that TensorRT can use when selecting kernels
        **kwargs: Additional settings for the specific requested strategy (See submodules for more info)

    Linuxr   64bitznCross compile for windows is only supported on x86-64 Linux architecture, current platform: platform.system()=z, platform.architecture()[0]=;File path cannot be empty. Please provide a valid file pathNr   r   r   rs   z successfully exported the moduler   z6successfully compiled and saved the module for windows)platformsystemarchitecturerO   r_   r   r   r   r   r   r%   r   r   r   r   re   debug dynamo_cross_compile_for_windows+dynamo_save_cross_compiled_exported_program)rE   r   r   rq   rs   ru   rw   r   r   r   r   r   trt_gmr+   r+   r,   r   F  sT   .

r   c                 K  s&   ddl m} tj| |di |d}|S )z
    Returns a boxed model which is the output of torch.compile.
    This does not compile the model to TRT. Execute this model on
    sample inputs to compile the model to TRT.
    r   )torch_tensorrt_backendF)backenddynamicoptions)torch_tensorrt.dynamo.backendr   r&   r   )rE   rw   r   boxed_fnr+   r+   r,   r@     s
   r@   forwardmethod_namebytesc                 K  sP  |dur|nt jh}|du r|du rtd|dur"|dur"td|p%|}t| }	t|	|}
|
tjkrT| }|	tjkrFt	
d t j| }t|f|||d|}|S |
tjkr]td|
tjkr|du rhi }ddlm} t|tjjsx|g}||}||}tjj| |fd	|i|}t|ft|||d
|S |
tjkrtdtd)a  Convert a TorchScript module method to a serialized TensorRT engine

    Converts a specified method of a module to a serialized TensorRT engine given a dictionary of conversion settings

    Arguments:
        module (Union(torch.nn.Module,torch.jit.ScriptModule): Source module

    Keyword Arguments:
        inputs (List[Union(torch_tensorrt.Input, torch.Tensor)]): **Required** 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
                ]

        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.
        enabled_precision (Set(Union(torch.dtype, torch_tensorrt.dtype))): The set of datatypes that TensorRT can use when selecting kernels
        ir (str): The requested strategy to compile. (Options: default - Let Torch-TensorRT decide, ts - TorchScript with scripting path)
        **kwargs: Additional settings for the specific requested strategy (See submodules for more info)
    Returns:
        bytes: Serialized TensorRT engine, can either be saved to a file or deserialized via TensorRT APIs
    Nr   r   ry   )r   r   ru   z<convert_method_to_trt_engine call is not supported for ir=fxr   r   rs   )rq   rs   ru   zGconvert_method_to_trt_engine call is not supported for ir=torch_compiler   )r&   r   r   rP   ro   r4   r=   rB   rC   re   rf   rJ   r   ts_convert_method_to_trt_enginer>   rO   r?   r   r   r%   r   r   r   r   r   8dynamo_convert_exported_program_to_serialized_trt_enginetupler@   )rE   r   r   rq   rs   rR   ru   rw   r   rQ   r   r   serialized_enginer   r   r   r   r+   r+   r,   r     sz   +





r    c                 C  s   t | S )a)  
    Load an ExportedProgram file in Windows which was previously cross compiled in Linux

    Arguments:
        file_path (str): Path to file on the disk

    Raises:
        ValueError: If the api is not called in windows or there is no file or the file is not a valid ExportedProgram file
    )+dynamo_load_cross_compiled_exported_program)r   r+   r+   r,   r   '  s   
r   c                 C  s   zt d|  d tj| }|W S  ty&   t jd|  ddd Y nw zt d|  d tj| }|W S  tyS   t jd|  ddd td|  d	w )
aj  
    Load either a Torchscript model or ExportedProgram.

    Loads a TorchScript or ExportedProgram file from disk. File type will be detect the type using try, except.

    Arguments:
        file_path (str): Path to file on the disk

    Raises:
        ValueError: If there is no file or the file is not either a TorchScript file or ExportedProgram file
    zLoading the provided file z using torch.jit.load()z5 via torch.jit.load() failed with the following errorT)exc_infoz using torch.export.load()z8 via torch.export.load() failed with the following errorz	The file zb doesn't correspond to a valid Torchscript module or ExportedProgram. Please verify the file path.)	re   r   r&   rJ   r   	Exceptionrf   exportr_   )r   	ts_moduler   r+   r+   r,   r   4  s0   


r   rA   Tr6   )output_formatr   rq   rs   retracepickle_protocolr    Optional[Sequence[torch.Tensor]]Optional[dict[str, Any]]r   boolr   intc                  s  t | tr| j} t| }	h d}
|dur!tdd |D s!td|r)|r)td|p,|}|du r3i }|rDtdd | D rDtd |
vrPtd	  d
 dkrdt	
 dkrdtdt	
  d|sjtd|	tjkrstd|	tjkrt fdddD std|durtd tj| | dS |	tjkrۈ dkrtd|durtd  dkrtjj| ||d dS  dkri }d|v r|d }tjj| ||d dS td|	tjkr dkrtjj| ||d}tj|| dS |s=ddlm} |dur
td  || } dkrtjj|||d dS  dkr9i }d|v r.|d }tjj|||d dS td|du rFtd!tjj| t||d"d#} dkrbtjj|||d dS  dkr}i }d|v rr|d }tjj|||d dS tddS )$a  
    Save the model to disk in the specified output format.

    Arguments:
        module (Optional(torch.jit.ScriptModule | torch.export.ExportedProgram | torch.fx.GraphModule | CudaGraphsTorchTensorRTModule)): Compiled Torch-TensorRT module
        inputs (torch.Tensor): Torch input tensors
        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.
        output_format (str): Format to save the model. Options include exported_program | torchscript | aot_inductor.
        retrace (bool): When the module type is a fx.GraphModule, this option re-exports the graph using torch.export.export(strict=False) to save it.
                This flag is experimental for now.
        pickle_protocol (int): The pickle protocol to use to save the model. Default is 2. Increase this to 4 or higher for large models
    >   rX   aot_inductorrA   Nc                 s  s    | ]	}t |tjV  qd S r$   )r%   r&   r'   )r)   inputr+   r+   r,   r-   v  s    
zsave.<locals>.<genexpr>zQNot all inputs provided are torch.tensors. Please provide torch.tensors as inputsr   c                 s  s    | ]}|d u V  qd S r$   r+   )r)   valuer+   r+   r,   r-     rV   zkwargs should not include None.zProvided output_format zG is not supported. Supported options are exported_program | torchscriptr   r   z4The AOT Inductor format is only supported on Linux, z, is not a supported platform for this formatr   zInput model is of type nn.Module. Saving nn.Module directly is not supported. Supported model types torch.jit.ScriptModule | torch.fx.GraphModule | torch.export.ExportedProgram.c                 3  rT   r$   r+   )r)   fr   r+   r,   r-     rV   )rA   r   zProvided model is a torch.jit.ScriptModule but the output_format specified is not torchscript. Other output formats are not supportedz^Provided model is a torch.jit.ScriptModule, inputs or arg_inputs is not necessary during save.rX   zProvided model is a torch.export.ExportedProgram but the output_format specified is torchscript. Please verify the output_formatzProvided model is a torch.export.ExportedProgram, inputs or arg_inputs is not necessary during save, it uses the inputs or arg_inputs provided during export and compilerA   )r   inductor_configs)r   package_pathzAttempted to serialize an exported program with an unsupported format. Exported programs support exported_program and aot_inductor)example_kwarg_inputsr   )r   zqProvided model is a torch.fx.GraphModule and retrace is False, inputs or arg_inputs is not necessary during save.zProvided model is a torch.fx.GraphModule and retrace is True, however the inputs or arg_inputs are empty. Please provide valid torch.tensors as inputs or arg_inputs to trace and save the modelF)rw   strict)r%   r   compiled_modulerP   r0   r_   r   rI   valuesr   r   rB   rC   r=   re   rg   r&   rJ   r   rD   r   	_inductoraoti_compile_and_packagerO   r>   r   torch_tensorrt.dynamo._exporterr   )rE   r   r   r   rq   rs   r   r   rw   rQ   accepted_formatsr   	module_tsr   r   r+   r   r,   r   Y  s   

















r   )r   r   r    r!   )rE   r   r    rB   )rQ   rB   rR   rS   r    r4   )r\   NNNN)rE   r   rR   rS   r   rp   rq   rr   rs   rt   ru   rv   rw   r   r    rx   )NNNN)rE   r   r   rS   r   r   rq   rr   rs   rt   ru   rv   rw   r   r    r   )rE   r   rw   r   r    r   )r   NNNr\   N)rE   r   r   rS   r   r   rq   rr   rs   rt   rR   rS   ru   rv   rw   r   r    r   )r   )r   rS   r    r   )rE   r   r   rS   r   rS   r   r   rq   r   rs   r   r   r   r   r   rw   r   r    r   )M
__future__r   collections.abcr   loggingr   r`   enumr   typingr   r   r   r   r   r	   r
   r&   torch_tensorrt._enumsr   torch_tensorrt._featuresr   r   torch_tensorrt._Inputr   torch_tensorrt.dynamor   <torch_tensorrt.dynamo.runtime._CudaGraphsTorchTensorRTModuler   typing_extensionsr   rc   torch.fxtorch_tensorrt.fxr   torch_tensorrt.fx.lowerr   r   torch_tensorrt.fx.utilsr   r'   	InputTyper^   torch_tensorrt.tsr   torch_tensorrt.ts._compilerr   r   r   rd   torch.exportr   torch_tensorrt.dynamo._compilerr   r   r   r   r   r   r   r   r   torch_tensorrt.dynamo._tracerr   r   	getLoggerr9   re   __all__r2   r4   rB   rP   ro   r@   r   r   r+   r+   r+   r,   <module>   s    $




	
D 
cm'