
    BVhU                         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	  G d dej                        Z ej                  dd	d
g      Z G d deej                        Z G d de      Zej"                  fdZy)z(Utilities for collecting TFLite metrics.    N)Text)converter_error_data_pb2)metricsc                       e Zd ZdZdZdZdZy)	Componentz5Enum class defining name of the converter components.PREPARE_TF_MODELCONVERT_TF_TO_TFLITE_MODELOPTIMIZE_TFLITE_MODELN)__name__
__module____qualname____doc__r   r	   r
        T/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/lite/python/convert_phase.pyr   r      s    ='  < 2r   r   SubComponentItemname	componentc                      e Zd ZdZd Zed        Zed        Z edd      Z	 ede
j                        Z ede
j                        Z ed	e
j                        Z ed
e
j                        Z ede
j                        Z ede
j                        Z ede
j                        Z ede
j                        Z ede
j(                        Z ede
j(                        Z ede
j(                        Z ede
j(                        Z ede
j2                        Z ede
j2                        Z ede
j2                        Z ede
j2                        Zy)SubComponentzEnum class defining name of the converter subcomponents.

  This enum only defines the subcomponents in Python, there might be more
  subcomponents defined in C++.
  c                 .    | j                   j                  S Nvaluer   selfs    r   __str__zSubComponent.__str__1   s    ::??r   c                 .    | j                   j                  S r   r   r   s    r   r   zSubComponent.name4   s    ::??r   c                 .    | j                   j                  S r   )r   r   r   s    r   r   zSubComponent.component8   s    ::r   UNSPECIFIEDNVALIDATE_INPUTSLOAD_SAVED_MODELFREEZE_SAVED_MODELCONVERT_KERAS_TO_SAVED_MODEL)CONVERT_CONCRETE_FUNCTIONS_TO_SAVED_MODELFREEZE_KERAS_MODELFREEZE_CONCRETE_FUNCTIONOPTIMIZE_TF_MODEL+CONVERT_GRAPHDEF_USING_DEPRECATED_CONVERTERCONVERT_GRAPHDEFCONVERT_SAVED_MODELCONVERT_JAX_HLO#QUANTIZE_USING_DEPRECATED_QUANTIZER	CALIBRATEQUANTIZESPARSIFY)r   r   r   r   r   propertyr   r   r   r    r   r   r!   r"   r#   r$   r%   r&   r'   r(   r	   r)   r*   r+   r,   r
   r-   r.   r/   r0   r   r   r   r   r   *   s          !5+ %%6%.%?%?A/ &&8&/&@&@B ((<(1(B(BD "2$i&@&@"B /?193M3M/O+ ((<(1(B(BD ..H.7.H.HJ '':'0'A'AC 1A3**1,-
 &&8&/&J&JL ))>)2)M)MO %%6%.%I%IK/ )9+Y-L-L)N% {I,K,KL) j)*I*IJ( j)*I*IJ(r   r   c                   H     e Zd ZdZ fdZdej                  fdZd Z xZ	S )ConverterErrorz4Raised when an error occurs during model conversion.c                 \    t         t        |   |       g | _        | j	                  |       y r   )superr3   __init__errors_parse_error_message)r   message	__class__s     r   r6   zConverterError.__init__   s'    	.$(1DKg&r   
error_datac                 :    | j                   j                  |       y r   )r7   append)r   r;   s     r   append_errorzConverterError.append_error   s    KKz"r   c                     dt         j                  j                  i}|j                         D ]>  \  }}||v st        j                         }||_        ||_        | j                  |        y y)a  If the message matches a pattern, assigns the associated error code.

    It is difficult to assign an error code to some errrors in MLIR side, Ex:
    errors thrown by other components than TFLite or not using mlir::emitError.
    This function try to detect them by the error message and assign the
    corresponding error code.

    Args:
      message: The error message of this exception.
    zFailed to functionalize Control Flow V1 ops. Consider using Control Flow V2 ops instead. See https://www.tensorflow.org/api_docs/python/tf/compat/v1/enable_control_flow_v2.N)r   ConverterErrorData!ERROR_UNSUPPORTED_CONTROL_FLOW_V1itemserror_message
error_coder>   )r   r9   error_code_mappingpatternrD   r;   s         r   r8   z#ConverterError._parse_error_message   sv    	/ %77..  2779 	G	-@@B
#*
  *
*%r   )
r   r   r   r   r6   r   r@   r>   r8   __classcell__)r:   s   @r   r3   r3   ~   s#    <'
#7JJ#r   r3   c                      t         vrt        d      t        vrt        d      t        j                  k7  rj                   k7  rt        d      dt
        j                  f fddt        ffdfd}|S )	a-  The decorator to identify converter component and subcomponent.

  Args:
    component: Converter component name.
    subcomponent: Converter subcomponent name.

  Returns:
    Forward the result from the wrapped function.

  Raises:
    ValueError: if component and subcomponent name is not valid.
  zGiven component name not foundz!Given subcomponent name not foundz+component and subcomponent name don't matchr;   c                     j                   | _        | j                  sj                  | _        t	        j
                         }|j                  |        y r   )r   r   subcomponentr   r   TFLiteConverterMetricsset_converter_error)r;   tflite_metricsr   rJ   s     r   report_errorz#convert_phase.<locals>.report_error   sD     %??J"" , 1 1j335N&&z2r   rC   c                 L    t        j                         }| |_         |       y r   )r   r@   rC   )rC   r;   rN   s     r   report_error_messagez+convert_phase.<locals>.report_error_message   s!    )<<>J,Jr   c                 H     t        j                          fd       }|S )Nc                      	  | i |S # t         $ rA}|j                  r|j                  D ]
  } |        |d  t        |             |d d }~wt        $ r} t        |             |d d }~ww xY wr   )r3   r7   str	Exception)argskwargsconverter_errorr;   errorfuncrN   rP   s        r   wrapperz8convert_phase.<locals>.actual_decorator.<locals>.wrapper   s    T$V$$ (!!+22 %j$% 4' s?3
44' SZ(s     	A5<AA5A00A5)	functoolswraps)rY   rZ   rN   rP   s   ` r   actual_decoratorz'convert_phase.<locals>.actual_decorator   s%    __T  Nr   )r   
ValueErrorr   r    r   r   r@   r   )r   rJ   r]   rN   rP   s   `` @@r   convert_phaser_      s     i
5
66%
8
99l...	)
B
CC37JJ 3$ 
& 
r   )r   collectionsenumr[   typingr   %tensorflow.compiler.mlir.lite.metricsr   tensorflow.lite.python.metricsr   Enumr   
namedtupler   r   rT   r3   r    r_   r   r   r   <module>rg      s    /     J 2	2		 	2 *;))*<+1;*?A QK#TYY QKh$Y $N +7*B*B 6r   