
    BVh+                     x    d 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dd	d
dZd Zd Zd Z G d d      Zd Zy)zGA utility class to generate the report HTML based on a common template.    N)toco_conversion_log_pb2)file_io)resource_loaderz&amp;z&quot;z&apos;z&gt;z&lt;)&"'><c                 2    dj                  d | D              S )N c              3   H   K   | ]  }t         j                  ||        y w)N)html_escape_tableget).0cs     U/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/lite/toco/logging/gen_html.py	<genexpr>zhtml_escape.<locals>.<genexpr>"   s     ;"&&q!,;s    ")join)texts    r   html_escaper   !   s    	;d;	;;    c                     | j                  d      }| j                  d      }| |dz   | }|j                  d      }d}t        t        |            D ]"  }|dz  dk(  r|dz  }n|d	z  }|||   z  }|d
z  }$ |dd S )a  Parses op_signature and returns a string denoting the input tensor type.

  Args:
    op_signature: a string specifying the signature of a particular operator.
      The signature of an operator contains the input tensor's shape and type,
      output tensor's shape and type, operator's name and its version. It has
      the following schema:
      INPUT:input_1_shape::input_1_type::input_2_shape::input_2_type::..
        ::OUTPUT:output_1_shape::output_1_type::output_2_shape::output_2_type::
        ..::NAME:operator_name ::VERSION:operator_version
     An example of an operator signature is:
     INPUT:[1,73,73,160]::float::[64,1,1,160]::float::[64]::float::
     OUTPUT:[1,73,73,64]::float::NAME:Conv::VERSION:1

  Returns:
    A string denoting the input tensors' type. In the form of shape/type
    separated
    by comma. For example:
    shape:[1,73,73,160],type:float,shape:[64,1,1,160],type:float,shape:[64],
    type:float
  :z::OUTPUT   z::r      r   zshape:ztype:,N)findsplitrangelen)op_signaturestartendinputslstout_stris          r   get_input_type_from_signaturer)   %   s    , 

C
 %*%#	#&&T#'S? a1uzggs1vGsNG 
"r   c                 @    | |j                   v ry| |j                  v ryy)NzBUILT-INz	CUSTOM OPz	SELECT OP)built_in_ops
custom_ops)op_nameconversion_logs     r   get_operator_typer/   J   s'    +++.+++r   c                   "    e Zd ZdZd Z	 	 ddZy)HTMLGeneratorz)Utility class to generate an HTML report.c                 T   t        j                  |      st        dj                  |            t        j                  |d      5 }|j                         | _        ddd       t        j                  t        j                  j                  |             || _        y# 1 sw Y   CxY w)al  Reads the HTML template content.

    Args:
      html_template_path: A string, path to the template HTML file.
      export_report_path: A string, path to the generated HTML report. This path
        should point to a '.html' file with date and time in its name.
        e.g. 2019-01-01-10:05.toco_report.html.

    Raises:
      IOError: File doesn't exist.
    zFile '{0}' does not exist.rN)_file_iofile_existsIOErrorformatFileIOreadhtml_templaterecursive_create_dirospathdirnameexport_report_path)selfhtml_template_pathr?   fs       r   __init__zHTMLGenerator.__init__V   s      230778JKLL	+S	1 $Q668d$ !!"''//2D"EF0D	$ $s   BB'c                    i }|rdnd|d<   t        |j                        |d<   t        |j                        |d<   t        t        |j                  j	                                     |d<   t        t        |j
                  j	                                     |d<   t        t        |j                  j	                                     |d<   |rd	nd
|d<   d}	d}
t        t        |j                              D ]Y  }|	d|j                  |   z   dz   z  }	|t        |j                        k  r"|	dt        |j                  |         z   dz   z  }	U|	dz  }	[ |j                  D ]  }t        ||      }|
d|z   dz   |z   dz   z  }
! |	|d<   |
|d<   ||d<   ||d<   |rt        |      |d<   nd|z   }t        |      |d<   | j                  }|D ]:  }|j                  |||   d      }|j!                  |      dk7  s.t#        d|z         t%        j&                  | j(                  d      5 }|j+                  |       ddd       y# 1 sw Y   yxY w)a  Generates the HTML report and writes it to local directory.

    This function uses the fields in `toco_conversion_log_before` and
    `toco_conversion_log_after` to populate the HTML content. Certain markers
    (placeholders) in the HTML template are then substituted with the fields
    from the protos. Once finished it will write the HTML file to the specified
    local file path.

    Args:
      toco_conversion_log_before: A `TocoConversionLog` protobuf generated
        before the model is converted by TOCO.
      toco_conversion_log_after: A `TocoConversionLog` protobuf generated after
        the model is converted by TOCO.
      post_training_quant_enabled: A boolean, whether post-training quantization
        is enabled.
      dot_before: A string, the dot representation of the model
        before the conversion.
      dot_after: A string, the dot representation of the model after
        the conversion.
      toco_err_log: A string, the logs emitted by TOCO during conversion. Caller
        need to ensure that this string is properly anonymized (any kind of
        user data should be eliminated).
      tflite_graph_path: A string, the filepath to the converted TFLite model.

    Raises:
      RuntimeError: When error occurs while generating the template.
    z,<span class="label label-danger">Fail</span>z0<span class="label label-success">Success</span>z<!--CONVERSION_STATUS-->z<!--TOTAL_OPS_BEFORE_CONVERT-->z<!--TOTAL_OPS_AFTER_CONVERT-->z<!--BUILT_IN_OPS_COUNT-->z<!--SELECT_OPS_COUNT-->z<!--CUSTOM_OPS_COUNT-->iszisn'tz"<!--POST_TRAINING_QUANT_ENABLED-->r   z<tr><td>z</td>z<td>z
</td></tr>z<td></td></tr>z	</td><td>z<!--REPEAT_TABLE1_ROWS-->z<!--REPEAT_TABLE2_ROWS-->z<!--DOT_BEFORE_CONVERT-->z<!--DOT_AFTER_CONVERT-->z<!--TOCO_INFO_LOG-->zLTFLite graph conversion successful. You can preview the converted model at: r   r   z!Could not populate marker text %rwN)str
model_sizesumr+   values
select_opsr,   r    r!   op_listop_signaturesr)   r/   r   r:   replacer   RuntimeErrorr4   r8   r?   write)r@   toco_conversion_log_beforetoco_conversion_log_afterpost_training_quant_enabled
dot_before	dot_aftertoco_err_logtflite_graph_path	html_dictpre_op_profilepost_op_profiler(   opsupported_typesuccess_infotemplatemarkerrB   s                     r   generatezHTMLGenerator.generatek   s   F I 
 	8N () 47"--4/I/025!,,3.I./-0%2299;<.>I)*+.%00779:,<I'(+.%00779:,<I'( , 23 NO 3199:; 	+
%?%G%G
&  n 
S+99:	:&#@&44Q7$9 9;GH 	H 	**	+ (// ((-FGn*r/K7.H&' (o(
 .<I)*-<I)*-7I)*,5I()*5l*Ci&'-0ABl*5l*Ci&' !!H I!!&)F*;Q?h	v	"	$>GHH	I 
00#	6 !ggh  s   !H<<IN)r   r   )__name__
__module____qualname____doc__rC   r`    r   r   r1   r1   S   s    116 !#^r   r1   c           	      z   t        j                  d      }t        j                  j	                  |      st        dj                  |            t        j                  j                  | d      }t        j                  j                  | d      }t        j                  j                  | d      }t        j                  j                  | d      }t        j                  j	                  |      st        dj                  |            t        j                  j	                  |      st        dj                  |            t        j                  j	                  |      st        dj                  |            t        j                  j	                  |      st        dj                  |            t        |t        j                  j                  | d            }t        j                         }	t        j                         }
t        |d      5 }|	j                  |j                                d	d	d	       t        |d      5 }|
j                  |j                                d	d	d	       t        j                  |d
d      5 }|j                         j                         }d	d	d	       t        j                  |d
d      5 }|j                         j                         }d	d	d	       |j!                  |	|
||
j"                  |       y	# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   OxY w)a  Generates an HTML report about the conversion process.

  Args:
    conversion_log_dir: A string specifying the file directory of the conversion
      logs. It's required that before calling this function, the
      `conversion_log_dir`
      already contains the following files: `toco_log_before.pb`,
        `toco_log_after.pb`, `toco_tf_graph.dot`,
        `toco_tflite_graph.dot`.
    quantization_enabled: A boolean, passed from the tflite converter to
      indicate whether post-training quantization is enabled during conversion.
    tflite_graph_path: A string, the filepath to the converted TFLite model.

  Raises:
    IOError: When any of the required files doesn't exist.
  ztemplate.htmlz2Failed to generate HTML: file '{0}' doesn't exist.ztoco_log_before.pbztoco_log_after.pbztoco_tf_graph.dotztoco_tflite_graph.dotztoco_conversion_summary.htmlrbNr3   zutf-8)encoding)_resource_loaderget_path_to_datafiler<   r=   existsr6   r7   r   r1   _toco_conversion_log_pb2TocoConversionLogopenParseFromStringr9   iorstripr`   toco_err_logs)conversion_log_dirquantization_enabledrW   template_filenametoco_log_before_pathtoco_log_after_pathdot_before_pathdot_after_pathhtml_generatorrQ   rR   rB   rT   rU   s                 r   gen_conversion_log_htmlr{      s   $ ';;OL	)	*
FMM   &8:NO%79LMGGLL!35HI/77<< 24KL.	,	-
FMM  	+	,
FMM  		(
FMM  		'
FMM   !ggll%'EFH.
  8IIK6HHJ $' 91..qvvx89& 8!--affh78 
wwg6 #!"J#	ww~sW5 "!I" 46O.
I3AA+-9 98 8# #" "s0    L LL%L1LL"%L.1L:)rd   rp   r<   tensorflow.lite.toco.loggingr   rl   tensorflow.python.lib.ior   r4   tensorflow.python.platformr   ri   r   r   r)   r/   r1   r{   re   r   r   <module>r      sU    N 	 	 \ 8 J 
				 <"Jv vr=-r   