o
    ?e@#                     @   s   d 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 dd Zd	d
 Zdd Zd#ddZd#ddZdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd  d eZd!d" ZdS )$z3Exposes the Python wrapper conversion to trt_graph.    N)version)_pywrap_py_utils)rewriter_config_pb2)dtypesc                 C   s~   t jj}|| _|| _d| j_|| _|| _|| _	d| _
d| _|| _|| _|| _|| _t jj| _d| _|| _|| _|| _|| _dS )z>Modifies rewriter_config to disable all non-TRT optimizations.FTN)r   ZRewriterConfigZOFFZarithmetic_optimizationZauto_mixed_precisionZauto_parallelenableZconstant_foldingZdebug_stripperZdependency_optimizationZdisable_meta_optimizerZdisable_model_pruningZfunction_optimizationZimplementation_selectorZlayout_optimizerZloop_optimizationZ
NO_MEM_OPTZmemory_optimizationZmin_graph_nodesZpin_to_host_optimizationZ	remappingZscoped_allocator_optimizationZshape_optimization)Zrewriter_configoff r	   j/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/compiler/tensorrt/utils.py-disable_non_trt_optimizers_in_rewriter_config   s(   
r   c                 C   s6   t | tsJ t| dksJ dd | D } d| S )N   c                 S   s   g | ]}t |qS r	   )str).0xr	   r	   r
   
<listcomp>:   s    z+version_tuple_to_string.<locals>.<listcomp>.)
isinstancetuplelenjoin)Z	ver_tupler	   r	   r
   version_tuple_to_string6   s   
r   c                 C   s$   t t| } t t|}| |kS N)r   Versionr   )Ztrt_verZ
target_verr	   r	   r
   "_is_tensorrt_version_greater_equal>   s   r   c                 C      t  }t|| ||fS r   )r   Zget_linked_tensorrt_versionr   majorminorpatchverr	   r	   r
   (is_linked_tensorrt_version_greater_equalE      r    c                 C   r   r   )r   Zget_loaded_tensorrt_versionr   r   r	   r	   r
   (is_loaded_tensorrt_version_greater_equalJ   r!   r"   c                 C   s   | t jjddddv S )a!  Determines if a TF-TRT experimental feature is enabled.

  This helper function checks if an experimental feature was enabled using
  the environment variable `TF_TRT_EXPERIMENTAL_FEATURES=feature_1,feature_2`.

  Args:
    feature_name: Name of the feature being tested for activation.
  ZTF_TRT_EXPERIMENTAL_FEATURES )default,)osenvirongetsplit)Zfeature_namer	   r	   r
   !is_experimental_feature_activatedO   s   
r*   c                 C   s"   t | tr
tj|  S dd | D S )zEHelper function to convert a dtype id to a corresponding string name.c                 S   s   g | ]}t j| qS r	   )r   _TYPE_TO_STRING)r   dr	   r	   r
   r   c   s    z,_convert_dtype_id_to_str.<locals>.<listcomp>)r   intr   r+   )dtyper	   r	   r
   _convert_dtype_id_to_str^   s   

r/   c                 C   s   dD ]M}z9| j | }|dkr6|jd}|dkrW q|dkr"W  dS |dkr*W  dS |d	kr2W  d
S W  dS t|jW   S  tyO } zW Y d}~qd}~ww dS )z-Returns the compute DType of a GraphDef Node.)precision_modeZDstTr.   Tr0   zutf-8r#   ZFP32Zfloat32ZFP16Zfloat16ZINT8Zint8unknownN)attrsdecoder/   type	Exception)nodeZtype_keyZprecision_valer	   r	   r
   get_node_compute_dtypef   s(   
r:   c                 C   s2   g }| j | jjD ]}|dd |jD  q	|S )z3Returns the input/output shapes of a GraphDef Node.c                 S   s   g | ]}|j qS r	   )size)r   dimr	   r	   r
   r      s    z&get_node_io_shapes.<locals>.<listcomp>)r3   listshapeappendr<   )r8   keyZ	out_shaper>   r	   r	   r
   get_node_io_shapes   s   rA   c                 C      t | j| jjS )z1Returns the input/output dtypes of a TRTEngineOp.)r/   r3   r=   r6   r8   r@   r	   r	   r
   get_trtengineop_io_dtypes      rD   c                 C   rB   )z:Returns the number of input/output nodes of a TRTEngineOp.)r   r3   r=   r6   rC   r	   r	   r
   get_trtengineop_io_nodes_count   rE   rF   c                 C   sd   t t}| jjD ]$}| d|jjkr-t|j}|jD ]}||j	  d7  < q ||fS q	||fS )z?Counts the number of nodes and OP types of a given TRTEngineOp.Z_native_segment   )
collectionsdefaultdictr-   Zlibraryfunction	signaturenamer   Znode_defop)graphdef	node_nameZops_in_enginefunc
node_countr8   r	   r	   r
   get_trtengineop_node_op_count   s   


rR   c                   @   s   e Zd ZdZdd ZdS )
DTypeIndexzBHelper class to create an index of dtypes with incremental values.c                 C   s    || vrt | d | |< | | S )NrG   )r   )selfr.   r	   r	   r
   get_dtype_index   s   zDTypeIndex.get_dtype_indexN)__name__
__module____qualname____doc__rU   r	   r	   r	   r
   rS      s    rS   c              
   C   st  t  }t|d}td|d td|d td|d td|d td|d g }| jD ]x}|j}t|}||}|jdd	 }	|	sGd
}	|j	dkr\t
| |\}
}| d|
 d}n|j	 }d| d|	 d}td| d| d| d|d t|jr|jD ]}|d}tdd|d }td| d| d|d qq-|| q-td|d td|d td|d td|d td |d | D ]\}}td!| d"| d#| d$|d qtd|d td%|d | D ]}|D ]}td| d| d&|d qqtd'|d W d(   n	1 sw   Y  td) td*| d+ td, td- td. td/ td0 d(S )1a9  Exports a GraphDef to GraphViz format.

  - Step 1: Drawing Each Node of the compute GraphDef.
  - Step 2: Create nodes for each collected dtype in the graph.
  - Step 3: Creating invisible links to align properly the legend.

  Each node consequently mentions:
  - Op Type
  - Compute Dtype
  - Compute Device
  wzdigraph tftrt_converted_graph {)filez)  graph [fontsize=10 fontname="Verdana"];z?  node [style=filled height=0.55 colorscheme=set312 shape=box];z
  subgraph tensorflow_graph {z    node [width=1.35];/r   zdevice:UnspecifiedZTRTEngineOpz []z<b>z</b>  <br/><i>z</i>z    "z
" [label=<z> fillcolor=z];:z^\^r#   r   z  "z" -> "z";z  }z
  subgraph cluster_legend {z!    label="Compute Dtype Legend";z    margin="30";z    node [width=2];z    z [fillcolor=z label=<<b>z</b>>];z'
  edge[style="invisible", dir="none"];"}NzD
===================================================================z"Graph Visualization Exported to: `z`.zBWe recommend using https://edotor.net/ to visualize the .dot file.zBYou can also use `graphviz` utility to convert them to PNG format:z"  - `sudo apt install -y graphviz`z=  - `dot -Tpng <input_filename>.dot -o <output_filename>.png`zD===================================================================
)rS   openprintr8   rL   r:   rU   Zdevicer)   rM   rR   r   inputresubr?   itemskeys)rN   Zdot_output_filenameZdtype_indexfZnodes_with_no_inputsr8   Zoutput_nameZnode_precisionZ	color_idxZ
device_keyrQ   _Z
node_labelZinput_full_namepartsZ
input_namer.   rO   r	   r	   r
   draw_graphdef_as_graphviz   sz   





Crk   )r   r   )rY   rH   r&   rd   	packagingr   Ztensorflow.compiler.tf2tensorrtr   Ztensorflow.core.protobufr   Ztensorflow.python.frameworkr   r   r   r   r    r"   r*   r/   r:   rA   rD   rF   rR   dictrS   rk   r	   r	   r	   r
   <module>   s,   

	