o
    Ơi                     @   s\  d dl Z d dl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 d dlmZ d dlmZmZ e eZ		dd	ejd
ejdededef
ddZ		dd	ejdejdededef
ddZdejjdee fddZ	d dejjdee deeeef  deeeee f eeee f f fddZ	d dejjdeee  dee e f fddZ!dS )!    N)AnyDictOptionalSequenceSetTuple)
FakeTensor)unset_fake_temporarily)Input)contains_sym_intextract_var_range_info Finput_shapeinput_dtypenameis_shape_tensorreturnc           	   
   C   s   g }g }g }| D ]=}t |tjr6t|}||d  ||dt|d |d d   ||d  q|| || || qt||||||dS )a=  
    Constructs a torch_tensorrt.Input based on a symbolic input
    Args:
        input_shape: A symbolic shape / regular shape of a tensor (which can have a  mix of SymInt nodes and static values)
    Returns:
        A dynamic shaped torch_tensorrt.Input which has the properties of the symbolic shaped input.
    minoptmax   )	min_shape	opt_shape	max_shapedtyper   r   )
isinstancetorchSymIntr   appendgetintr
   )	r   r   r   r   r   r   r   dimmin_max_opt r#   ]/home/ubuntu/.local/lib/python3.10/site-packages/torch_tensorrt/dynamo/partitioning/common.pyconstruct_dynamic_input   s,   

r%   r   c                 C   s(   t | rt| |||dS t| |||dS )zd
    Based on type of dimensions in the input_shape, construct regular or dynamic shaped inputs
    r   r   )shaper   r   r   )r   r%   r
   )r   r   r   r   r#   r#   r$   	get_input5   s   	r(   modulec              
   C   s`  t   g }dd | jjD }|D ]}|jrd|jv rq|jd }t|ttjfr9| }|	t
||j|jd qt|tjrN|	t
|gtj|jdd qt|tjrc|	t
dgtj|jdd qtd	|j d
t| dd|jv r|jd }|j}|	t
||j|jd qtd|j dtd|j d|W  d   S 1 sw   Y  dS )a  
    Construct torch_tensorrt Inputs based on the module inputs.
    The module inputs will have meta data which has the shape and dtype info
    Args:
        module: Input FX GraphModule
    Returns:
        Sequence of torch_tensorrt.Input's representing inputs to given module
    c                 S   s   g | ]	}|j d kr|qS )placeholder)op).0noder#   r#   r$   
<listcomp>S   s    z.construct_submodule_inputs.<locals>.<listcomp>val)r   Tr&      FzThe meta val for input node z is of type : z7. Supported types: torch.Tensor|FakeTensor|torch.SymInttensor_metazInput zq does not contain val and tensor_meta fields in the metadata. Please ensure you have exported the graph correctlyzO does not contain metadata. Please ensure you have exported the graph correctlyN)r	   graphnodesmetar   r   r   Tensorsizer   r(   r   r   r   int64SymFloatfloat32
ValueErrortargettyper'   AssertionError)r)   torchtrt_inputsmodule_inputsinput
input_metar   r#   r#   r$   construct_submodule_inputsH   sb   	

	

$rB   parent_moduleinputskwarg_inputsc           
         s   i }i }g  g dt dttj dttj dd f fdd}|d u r$i }|  D ]A\}}t| |}||}	| |i | |	  t t	t
frOdd  D n jg||< tt	t
frcd	d D njg||< q(||fS )
NselfrD   outputsr   c                    s   | |d S Nr#   )rF   rD   rG   
sub_inputssub_outputsr#   r$   get_submodule_io   s   z,run_shape_analysis.<locals>.get_submodule_ioc                 S      g | ]}|j qS r#   r'   )r,   r@   r#   r#   r$   r.          z&run_shape_analysis.<locals>.<listcomp>c                 S   rM   r#   rN   )r,   outputr#   r#   r$   r.      rO   )r   r   r   r5   named_childrengetattrregister_forward_hookremover   tuplelistr'   )
rC   rD   rE   submod_inputs_shape_mapsubmod_outputs_shape_maprL   r   _	submodulehandler#   rI   r$   run_shape_analysis   s:   

r\   graph_moduletorch_executed_opsc                 C   sr   ddl m} ||d}t|  }d}d}| jjD ]}|jdkr.|d7 }|||r.|d7 }q|jdd ||fS )a  Helper function to get converter support overview pre-partitioning

    Args:
        graph_module: FX GraphModule to determine support for
        verbose: Bool representing whether to print operator support
        torch_executed_ops: Collection of operations to run in Torch, regardless of converter coverage
    Returns:
        The number of supported call_function nodes in the graph
    r0   )TorchTensorRTOperatorSupport)r^   r   call_functionT)print_node_support)	_global_partitionerr_   dictnamed_modulesr2   r3   r+   is_node_supportedprint_support_overview)r]   r^   r_   
op_supportmodule_dictnumber_of_supported_nodestotal_functional_nodesr-   r#   r#   r$   get_graph_converter_support   s   

rk   )r   FrH   )"loggingtypingr   r   r   r   r   r   r   torch._subclasses.fake_tensorr   "torch.fx.experimental.proxy_tensorr	   torch_tensorrt._Inputr
   torch_tensorrt.dynamo.utilsr   r   	getLogger__name__loggerSizer   strboolr%   r(   fxGraphModulerB   rc   r\   r    rk   r#   r#   r#   r$   <module>   sj     

+
D"
+

