o
    Ơix/                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZ d dl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 e eZeG dd dZeG d	d
 d
Zdedeeef ddfddZdededefddZdejjde
e fddZdS )    N)	dataclassfield)AnyDictListUnion)CompilationSettings)ConverterRegistry)get_node_namec                   @   sr   e Zd ZU dZdZeed< dZeed< e	e
dZeed< e	e
dZeed< e	e
dZeed	< e	e
dZeed
< dS )PerSubgraphDataa  Class to track data on a per-subgraph level

    Args:
        subgraph_name (str): Name of the subgraph in the GraphModule
        subgraph_op_count (int): Number of operations in the subgraph
        input_shapes (Any): Shapes of input Tensors of the subgraph
        input_dtypes (Any): Input data types of the subgraph
        output_shapes (Any): Shapes of output Tensors of the subgraph
        output_dtypes (Any): Output data types of the subgraph
     subgraph_namer   subgraph_op_countdefault_factoryinput_shapesinput_dtypesoutput_shapesoutput_dtypesN)__name__
__module____qualname____doc__r   str__annotations__r   intr   listr   r   r   r   r    r   r   X/home/ubuntu/.local/lib/python3.10/site-packages/torch_tensorrt/dynamo/_DryRunTracker.pyr      s   
 r   c                   @   s   e Zd ZU dZdZeed< dZeed< ee	dZ
eed< ee	dZeed< ee	dZeed< ee	dZeed	< ee	dZee ed
< dZeed< eedZeed< eedZeeef ed< ee	dZee ed< dS )DryRunTrackeraJ  Class to track data on a graph-wide level

    Args:
        total_ops_in_graph (int): Total number of operators in graph
        supported_ops_in_graph (int): Number of supported operators in graph
        input_shapes (Any): Shapes of input Tensors of the graph
        input_dtypes (Any): Input data types of the graph
        output_shapes (Any): Shapes of output Tensors of the graph
        output_dtypes (Any): Output data types of the graph
        per_subgraph_data (List[PerSubgraphData]): Per-subgraph data, see above class
        tensorrt_graph_count (int): Number of TensorRT engines to be generated
        compilation_settings (CompilationSettings): User Compilation Settings
        unsupported_ops (Dict[str, int]): Set of operators not supported in TRT
        to_run_in_torch (List[str]): Set of nodes to run in Torch
    r   total_ops_in_graphsupported_ops_in_graphr   r   r   r   r   per_subgraph_datatensorrt_graph_countcompilation_settingsunsupported_opsto_run_in_torchN)r   r   r   r   r    r   r   r!   r   r   r   r   r   r   r   r"   r   r   r#   r   r$   dictr%   r   r   r&   r   r   r   r   r   %   s   
 r   dryrun_trackerdryrun_enabledreturnc              	      sP  d}|d7 }|d| j  d| j dt| jd | j  d d7 }| jr7dd	d
 | j D }|d| d7 }| jrHd| j}|d| d7 }|d| j d7 }t| j	| j
ks[J |d7 }|ddt| j| j d 7 }t| j	D ]A\}}|d7 }|dd|d  d|j d 7 }|ddt|j|j d 7 }|dd|j d 7 }|ddt|j|j d 7 }qs|d7 }|ddt| j| j d 7 }| j
dkr^tdd | j	D td d | j	D | j
  t d td!d | j	D |d"7 }|d#   7 }|d$  7 }|d%7 }|d& d' tfd(d
| j	D  d) 7 }t krE|d*t  d' t fd+d
| j	D  d) 7 }|d, d- tfd.d
| j	D  d) 7 }n|d/7 }|rt| t|trtj|rtd0| d1 d3S t |d2}|!| W d3   d3S 1 sw   Y  d3S d3S t"| d3S )4zCDisplays statistics about the dryrun either to debug logs or stdout
z++++++++++++++++++++++++++++++++++++++++++++++++++ Dry-Run Results for Graph ++++++++++++++++++++++++++++++++++++++++++++++++++

zThe graph consists of z Total Operators, of which z operators are supported, d      z% coverage

c                 S   s&   g | ]\}}t | d t | qS )z: )r   ).0kvr   r   r   
<listcomp>U   s   & z(dryrun_stats_display.<locals>.<listcomp>z{The following ops are currently unsupported or excluded from conversion, and are listed with their op-count in the graph:
 z

z7The following nodes are currently set to run in Torch:
zj
Note: Some of the above nodes may be supported, but were not included in a TRT graph by the partitioner

zCompiled with: z  Graph Structure:

z   zInputs: z    ...
z    zTRT Engine #   z - Submodule name: z     zEngine Inputs: zNumber of Operators in Engine: zEngine Outputs: z	Outputs: r   c                 s       | ]}|j V  qd S Nr   r.   trt_subgraphr   r   r   	<genexpr>   
    
z'dryrun_stats_display.<locals>.<genexpr>c                 s   r3   r4   r5   r6   r   r   r   r8      r9   c                 s   r3   r4   r5   r6   r   r   r   r8      r9   zF
  ------------------------- Aggregate Stats -------------------------z1

   Average Number of Operators per TRT Engine: z$
   Most Operators in a TRT Engine: z)

  ********** Recommendations **********z=

   - For minimal graph segmentation, select min_block_size=z which would generate c                       g | ]	}|j  krd qS r2   r5   r6   )most_ops_in_an_enginer   r   r1          z TRT engine(s)z=
   - For moderate graph segmentation, select min_block_size=c                    s    g | ]}|j t krd qS r;   )r   mathceilr6   )avg_ops_per_enginer   r   r1      s     zX
   - The current level of graph segmentation is equivalent to selecting min_block_size=z which generates c                    r:   r;   r5   r6   )min_ops_in_an_enginer   r   r1      r=   zE
  Aggregate stats not available since no TRT Engines were generated.zFile already exists at path z, not saving dryrun outputzw+N)#r    r!   roundr%   joinitemsr&   r$   lenr"   r#   input_formatterr   r   	enumerater   r   r   r   minsummaxr>   r?   print
isinstancer   ospathexistsloggerwarningopenwritedebug)r(   r)   formatted_stats
parsed_opsformatted_nodesitrt_subgraph_datafr   )r@   rA   r<   r   dryrun_stats_displayF   s   



$	r[   shapesdtypesc                    s,   dt dt dtf fdd  | |dd S )z@Format shapes and dtypes of input Tensors into a readable stringr\   r]   r*   c                    s  t | trGtdd | D rGd}| D ]}t |tr)|d|d  d|d  d7 }q|| d	7 }q|d
d d dt|dd
  d	 }|S t | trlt| dkrltdd |  D rld|  dt|dd
  d	S t | ttfrt | trzdnd}t| |D ]\}}| ||7 }q|d
d t | trd }|S d }|S t | trd}|  D ]\}}| ||| 7 }q|d
d d }|S t	dt
|  d)zHelper for input formatterc                 s   s    | ]
}t |ttfV  qd S r4   )rL   r   tupler.   eltr   r   r   r8      s    
zBinput_formatter.<locals>.input_formatter_helper.<locals>.<genexpr>z	Tensor: (z(min=r   z, max=r2   z), z, N)@      c                 s   s8    | ]\}}t |totd d |D o|dv V  qdS )c                 s   s    | ]}t |tV  qd S r4   )rL   r   r_   r   r   r   r8      s    zLinput_formatter.<locals>.input_formatter_helper.<locals>.<genexpr>.<genexpr>)	min_shape	opt_shape	max_shapeN)rL   r^   all)r.   r/   shaper   r   r   r8      s    

zTensor: zList[zTuple(z], zDict{z}, zInvalid input type z5 encountered in parse_complex_tensor_structs parsing.)rL   r^   ri   r   r'   rE   rD   r   zip
ValueErrortype)r\   r]   input_shape_stringr`   formatted_strrj   dtypekeyinput_formatter_helperr   r   rs      sL   
 (


z/input_formatter.<locals>.input_formatter_helperNra   )r   r   )r\   r]   r   rr   r   rF      s   7rF   graph_modulec                 C   sN   g }| j jD ]}|jdv r$|jtjkr$|dt|j dt	|  q|S )zParses call_function and call_method nodes from a GraphModule
    Excludes getitem nodes

    Returns a string representation of the nodes
    )call_functioncall_methodzNode: z, with layer location: )
graphnodesoptargetoperatorgetitemappendr	   qualified_name_or_strr
   )rt   r&   noder   r   r   parse_non_trt_nodes  s   
r   )loggingr>   r{   rM   dataclassesr   r   typingr   r   r   r   torchtorch_tensorrt.dynamo._settingsr   3torch_tensorrt.dynamo.conversion._ConverterRegistryr	   0torch_tensorrt.dynamo.conversion.converter_utilsr
   	getLoggerr   rP   r   r   boolr   r[   rF   fxGraphModuler   r   r   r   r   <module>   s2    
 

 =