
    BVh?                     x   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lmZ ddlmZ dd	lmZ dd
lmZ  G d de      Zd Zd Zd Zd Z edg       d        Zd Z edg       d        Z G d d      Zd Zd Z G d d      Z G d d      Z G d d       Z  G d! d"      Z! G d# d$      Z" G d% d&      Z# G d' d(      Z$ G d) d*      Z% G d+ d,      Z& G d- d.      Z' G d/ d0      Z( e        e        e         e#        e!        e$        e"        e%        e        e(        e&       gZ)y)1a  Module that encodes (decodes) nested structures into (from) protos.

The intended use is to serialize everything needed to restore a `Function` that
was saved into a SavedModel. This may include concrete function inputs and
outputs, signatures, function specs, etc.

Example use:
# Encode into proto.
signature_proto = nested_structure_coder.encode_structure(
    function.input_signature)
# Decode into a Python object.
restored_signature = nested_structure_coder.decode_proto(signature_proto)
    N)
struct_pb2)dtypes)type_spec_registry)internal)compat)nest)collections_abc)	tf_exportc                       e Zd ZdZy)NotEncodableErrorz2Error raised when a coder cannot encode an object.N)__name__
__module____qualname____doc__     d/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/saved_model/nested_structure_coder.pyr   r   +   s    :r   r   c                 .    t         j                  |        y)zRegisters a codec to use for encoding/decoding.

  Args:
    x: The codec object to register. The object must implement can_encode,
      do_encode, can_decode, and do_decode. See the various _*Codec classes for
      examples.
  N)_codecsappend)xs    r   register_codecr   /   s     
..r   c                  `    t         D  cg c]  } | j                  | j                  f c} S c c} w N)r   
can_encode	do_encodecs    r   _get_encodersr   :   #    /6	7!1<<
%	77	7   +c                  `    t         D  cg c]  } | j                  | j                  f c} S c c} w r   )r   
can_decode	do_decoder   s    r   _get_decodersr%   >   r    r!   c                     t        |      D ]3  \  }} ||       st        j                  t        |      } || |      c S  t	        dt        |        dt        |        d      )N)coderszNo encoder for object z	 of type .)reversed	functoolspartial_map_structurer   strtype)pyobjr'   candorecursion_fns        r   r,   r,   B   sm     &! %gc2
5z&&~fEl|$$% 	s5zl)DK=B	D Dr   z)__internal__.saved_model.encode_structure)v1c                 *    t        | t                     S )zEncodes nested structures composed of encodable types into a proto.

  Args:
    nested_structure: Structure to encode.

  Returns:
    Encoded proto.

  Raises:
    NotEncodableError: For values for which there are no encoders.
  )r,   r   nested_structures    r   encode_structurer7   M   s     
(-/	::r   c                 :    	 t        |        y# t        $ r Y yw xY w)zDetermines whether a nested structure can be encoded into a proto.

  Args:
    nested_structure: Structure to encode.

  Returns:
    True if the nested structured can be encoded.
  FT)r7   r   r5   s    r   r   r   ]   s)    %& 
 
 s    	z%__internal__.saved_model.decode_protoc                 *    t        | t                     S )zDecodes proto representing a nested structure.

  Args:
    proto: Proto to decode.

  Returns:
    Decoded structure.

  Raises:
    NotEncodableError: For values for which there are no encoders.
  )r,   r%   )protos    r   decode_protor;   m   s     
}	//r   c                   (    e Zd ZdZd Zd Zd Zd Zy)
_ListCodeczCodec for lists.c                 "    t        |t              S r   )
isinstancelistselfr/   s     r   r   z_ListCodec.can_encode       eT""r   c                    t        j                         }|j                  j                  t        j                                |D ];  }|j                  j
                  j                         j                   ||             = |S r   )r   StructuredValue
list_valueCopyFrom	ListValuevaluesadd)rB   rF   	encode_fnencoded_listelements        r   r   z_ListCodec.do_encode   sl    --/L$$Z%9%9%;< H$$((*33Ig4FGHr   c                 $    |j                  d      S )NrF   HasFieldrB   values     r   r#   z_ListCodec.can_decode       >>,''r   c                 `    |j                   j                  D cg c]
  } ||       c}S c c}w r   )rF   rI   )rB   rR   	decode_fnrM   s       r   r$   z_ListCodec.do_decode   s'    .3.>.>.E.EF7IgFFFs   +Nr   r   r   r   r   r   r#   r$   r   r   r   r=   r=   }   s    #(Gr   r=   c                 >    t        |        xr t        | t              S r   )_is_named_tupler?   tuple)objs    r   	_is_tupler[      s    S!	!	<je&<<r   c                     t        | t              syt        | d      xrB t        | j                  t        j
                        xr t        d | j                  D              S )zReturns True iff `instance` is a `namedtuple`.

  Args:
    instance: An instance of a Python object.

  Returns:
    True if `instance` is a `namedtuple`.
  F_fieldsc              3   <   K   | ]  }t        |t                y wr   r?   r-   ).0fs     r   	<genexpr>z"_is_named_tuple.<locals>.<genexpr>   s     ;QjC ;s   )r?   rY   hasattrr]   r	   Sequenceall)instances    r   rX   rX      sT     
He	$
(I
& <
X%%'?'?
@<
;(*:*:;
;=r   c                   (    e Zd ZdZd Zd Zd Zd Zy)_TupleCodeczCodec for tuples.c                     t        |      S r   )r[   rA   s     r   r   z_TupleCodec.can_encode   s    Ur   c                    t        j                         }|j                  j                  t        j                                |D ];  }|j                  j
                  j                         j                   ||             = |S r   )r   rE   tuple_valuerG   
TupleValuerI   rJ   )rB   rk   rK   encoded_tuplerM   s        r   r   z_TupleCodec.do_encode   sl    ..0M&&z'<'<'>? J&&**,55i6HIJr   c                 $    |j                  d      S )Nrk   rO   rQ   s     r   r#   z_TupleCodec.can_decode       >>-((r   c                 T    t        fd|j                  j                  D              S )Nc              3   .   K   | ]  } |        y wr   r   )r`   rM   rU   s     r   rb   z(_TupleCodec.do_decode.<locals>.<genexpr>   s     L7#Ls   )rY   rk   rI   rB   rR   rU   s     `r   r$   z_TupleCodec.do_decode   s     L53D3D3K3KLLLr   NrV   r   r   r   rh   rh      s    )Mr   rh   c                   (    e Zd ZdZd Zd Zd Zd Zy)
_DictCodeczCodec for dicts.c                 6    t        |t        j                        S r   )r?   r	   MappingrA   s     r   r   z_DictCodec.can_encode   s    e_4455r   c                    t        j                         }|j                  j                  t        j                                |j                         D ]3  \  }}|j                  j                  |   j                   ||             5 |S r   )r   rE   
dict_valuerG   	DictValueitemsfields)rB   rx   rK   encoded_dictkeyrR   s         r   r   z_DictCodec.do_encode   su    --/L$$Z%9%9%;< &&( E
U$$S)229U3CDEr   c                 $    |j                  d      S )Nrx   rO   rQ   s     r   r#   z_DictCodec.can_decode   rS   r   c                     |j                   j                  j                         D ci c]  \  }}| ||       c}}S c c}}w r   )rx   r{   rz   )rB   rR   rU   r}   vals        r   r$   z_DictCodec.do_decode   s8    050@0@0G0G0M0M0OPHCC3PPPs   ?NrV   r   r   r   rt   rt      s    6(Qr   rt   c                   (    e Zd ZdZd Zd Zd Zd Zy)_NamedTupleCodeczCodec for namedtuples.

  Encoding and decoding a namedtuple reconstructs a namedtuple with a different
  actual Python type, but with the same `typename` and `fields`.
  c                     t        |      S r   )rX   rA   s     r   r   z_NamedTupleCodec.can_encode   s    5!!r   c                    t        j                         }|j                  j                  t        j                                |j
                  j                  |j                  _        |j                  D ]_  }|j                  j                  j                         }||_        |j                  j                   ||j                         |                a |S r   )r   rE   named_tuple_valuerG   NamedTupleValue	__class__r   namer]   rI   rJ   r}   rR   _asdict)rB   r   rK   encoded_named_tupler}   pairs         r   r   z_NamedTupleCodec.do_encode   s    $446))22:3M3M3OP!!** )). (( G 2299==?ddh
jj)$5$=$=$?$DEFG r   c                 $    |j                  d      S )Nr   rO   rQ   s     r   r#   z_NamedTupleCodec.can_decode   s    >>-..r   c                 0   |j                   j                  }|D cg c]   }|j                   ||j                        f" }}t	        j
                  |j                   j                  |D cg c]  }|d   	 c}      } |di t        |      S c c}w c c}w )Nr   r   )r   rI   r}   rR   collections
namedtupler   dict)rB   rR   rU   key_value_pairsr   rz   itemnamed_tuple_types           r   r$   z_NamedTupleCodec.do_decode   s    --44O;JK4dhh	$**-.KEK"--e.E.E.J.JCH.I4tAw.IK*d5k** L.Is   %B*B
NrV   r   r   r   r   r      s    "	/+r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)_Float64CodeczCodec for floats.c                 "    t        |t              S r   )r?   floatrA   s     r   r   z_Float64Codec.can_encode   s    eU##r   c                 >    ~t        j                         }||_        |S r   )r   rE   float64_value)rB   r   rK   rR   s       r   r   z_Float64Codec.do_encode   s     &&(E'ELr   c                 $    |j                  d      S )Nr   rO   rQ   s     r   r#   z_Float64Codec.can_decode   s    >>/**r   c                     ~|j                   S r   )r   rr   s      r   r$   z_Float64Codec.do_decode   s    r   NrV   r   r   r   r   r      s    $+r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)_Int64Codecz5Codec for Python integers (limited to 64 bit values).c                 H    t        |t               xr t        |t              S r   )r?   boolintrA   s     r   r   z_Int64Codec.can_encode  s    %&&A:eS+AAr   c                 >    ~t        j                         }||_        |S r   )r   rE   int64_value)rB   	int_valuerK   rR   s       r   r   z_Int64Codec.do_encode  s     &&(E!ELr   c                 $    |j                  d      S )Nr   rO   rQ   s     r   r#   z_Int64Codec.can_decode  ro   r   c                 .    ~t        |j                        S r   )r   r   rr   s      r   r$   z_Int64Codec.do_decode  s    u  !!r   NrV   r   r   r   r   r     s    =B)"r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)_StringCodeczpCodec for strings.

  See StructuredValue.string_value in proto/struct.proto for more detailed
  explanation.
  c                 "    t        |t              S r   r_   rA   s     r   r   z_StringCodec.can_encode  s    eS!!r   c                 >    ~t        j                         }||_        |S r   )r   rE   string_value)rB   r   rK   rR   s       r   r   z_StringCodec.do_encode  s     &&(E%ELr   c                 $    |j                  d      S )Nr   rO   rQ   s     r   r#   z_StringCodec.can_decode%  s    >>.))r   c                 B    ~t        j                  |j                        S r   )r   as_strr   rr   s      r   r$   z_StringCodec.do_decode(  s    ==++,,r   NrV   r   r   r   r   r     s    "*-r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)
_NoneCodeczCodec for None.c                 
    |d u S r   r   rA   s     r   r   z_NoneCodec.can_encode0  s    D=r   c                     ~~t        j                         }|j                  j                  t        j                                |S r   )r   rE   
none_valuerG   	NoneValue)rB   r   rK   rR   s       r   r   z_NoneCodec.do_encode3  s7    :&&(E	j2245Lr   c                 $    |j                  d      S )Nr   rO   rQ   s     r   r#   z_NoneCodec.can_decode9  rS   r   c                      ~~y r   r   rr   s      r   r$   z_NoneCodec.do_decode<  s
    5r   NrV   r   r   r   r   r   -  s    (r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)
_BoolCodeczCodec for booleans.c                 "    t        |t              S r   )r?   r   rA   s     r   r   z_BoolCodec.can_encodeD  rC   r   c                 >    ~t        j                         }||_        |S r   )r   rE   
bool_value)rB   r   rK   rR   s       r   r   z_BoolCodec.do_encodeG  s     &&(E!ELr   c                 $    |j                  d      S )Nr   rO   rQ   s     r   r#   z_BoolCodec.can_decodeM  rS   r   c                     ~|j                   S r   )r   rr   s      r   r$   z_BoolCodec.do_decodeP  s    r   NrV   r   r   r   r   r   A  s    #(r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)_TensorTypeCodeczCodec for `TensorType`.c                 6    t        |t        j                        S r   )r?   r   DTyperA   s     r   r   z_TensorTypeCodec.can_encodeX  s    eV\\**r   c                 R    ~t        j                         }|j                  |_        |S r   )r   rE   as_datatype_enumtensor_dtype_value)rB   r   rK   encoded_tensor_types       r   r   z_TensorTypeCodec.do_encode[  s)    $446-?-P-P*r   c                 $    |j                  d      S )Nr   rO   rQ   s     r   r#   z_TensorTypeCodec.can_decodea  s    >>.//r   c                 B    ~t        j                  |j                        S r   )r   r   r   rr   s      r   r$   z_TensorTypeCodec.do_decoded  s    <<0011r   NrV   r   r   r   r   r   U  s    +02r   r   c                   6    e Zd ZdZg Zg Zd Zd Zd Zd Z	d Z
y)BuiltInTypeSpecCodeca  Codec for built-in `TypeSpec` classes.

  Built-in TypeSpec's that do not require a custom codec implementation
  register themselves by instantiating this class and passing it to
  register_codec.

  Attributes:
    type_spec_class: The built-in TypeSpec class that the
      codec is instantiated for.
    type_spec_proto_enum: The proto enum value for the built-in TypeSpec class.
  c                    t        |t        j                        st        d| d      || j                  v rt        d| d      || j
                  v rt        d| d      t        |t              r
|dk  s|dkD  rt        d| d      || _        || _	        | j                  j                  |       | j
                  j                  |       y )	N
The type 'z ' does not subclass tf.TypeSpec.z$' already has an instantiated codec.zThe proto value 'z' is already registered.r   
   z' is invalid.)
issubclassr   TypeSpec
ValueError_BUILT_IN_TYPE_SPECS_BUILT_IN_TYPE_SPEC_PROTOSr?   r   type_spec_classtype_spec_proto_enumr   )rB   r   r   s      r   __init__zBuiltInTypeSpecCodec.__init__y  s    ox'8'89''G
HJ J $333''K
LN N t>>>233K
L  +S11$"$*+?*@NOO*D 4D$$_5##**+?@r   c                 .    t        || j                        S )z@Returns true if `pyobj` can be encoded as the built-in TypeSpec.)r?   r   rA   s     r   r   zBuiltInTypeSpecCodec.can_encode  s    eT1122r   c                 N   |j                         }t        t        j                  |j                  d            }t        j                         }|j                  j                  t        j                  | j                   ||      | j                  j                  |             |S )z9Returns an encoded proto for the given built-in TypeSpec.Texpand_compositesr   
type_statetype_spec_class_namenum_flat_components)
_serializelenr   flatten_component_specsr   rE   type_spec_valuerG   TypeSpecProtor   r   r   )rB   r   rK   r   r   encoded_type_specs         r   r   zBuiltInTypeSpecCodec.do_encode  s     ++-Jdll((DB C"224%%..   55 ,!%!5!5!>!> 3		56 r   c                 p    |j                  d      r%|j                  j                  }|| j                  k(  S y)zBReturns true if `value` can be decoded into its built-in TypeSpec.r   F)rP   r   r   r   )rB   rR   type_spec_class_enums      r   r#   zBuiltInTypeSpecCodec.can_decode  s5    ~~'("22BB!T%>%>>>r   c                 p    |j                   }| j                  j                   ||j                              S )z=Returns the built in `TypeSpec` encoded by the proto `value`.)r   r   _deserializer   )rB   rR   rU   type_spec_protos       r   r$   zBuiltInTypeSpecCodec.do_decode  s4    ++O,,/,,- r   N)r   r   r   r   r   r   r   r   r   r#   r$   r   r   r   r   r   i  s/    
  "A23r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)_TypeSpecCodeczCodec for `tf.TypeSpec`.c                     t        |t        j                        r 	 t        j                  t        |             yy# t        $ r Y yw xY w)z5Returns true if `pyobj` can be encoded as a TypeSpec.TF)r?   r   r   r   get_namer.   r   rA   s     r   r   z_TypeSpecCodec.can_encode  sG     %**+##DK0   s   < 	AAc                    t        j                  t        |            }t        j                  j
                  }t        j                  d|z         |j                         }t        t        j                  |j                  d            }t        j                         }|j                  j                  t        j                  | ||      ||             |S )z5Returns an encoded proto for the given `tf.TypeSpec`.z}Encoding a StructuredValue with type %s; loading this StructuredValue will require that this type be imported and registered.Tr   r   )r   r   r.   r   r   REGISTERED_TYPE_SPECwarningswarnr   r   r   r   r   rE   r   rG   )rB   r   rK   r   r   r   r   r   s           r   r   z_TypeSpecCodec.do_encode  s    -66tO7LM ..CCO MM -/CD E !++-J_55NP"224%%..  + ,!5 3		56 r   c                 $    |j                  d      S )z<Returns true if `value` can be decoded into a `tf.TypeSpec`.r   rO   rQ   s     r   r#   z_TypeSpecCodec.can_decode  s    >>+,,r   c                 T   |j                   }|j                  }|j                  }|t        j                  j
                  k(  r	 t        j                  |      }nt        d| d      |j                   ||j                              S # t        $ r}t        d| d      |d}~ww xY w)z7Returns the `tf.TypeSpec` encoded by the proto `value`.r   zr' has not been registered.  It must be registered before you load this object (typically by importing its module).Nz' is not supported by this version of TensorFlow. (The object you are loading must have been created with a newer version of TensorFlow.))r   r   r   r   r   r   r   lookupr   r   r   )rB   rR   rU   r   r   
class_namer   es           r   r$   z_TypeSpecCodec.do_decode  s    ++O*:: 55Jz77LLL#,33J? zl #1 12 2 ''	/2L2L(MNN  # %  "#	##s   B	 		B'B""B'NrV   r   r   r   r   r     s     
,-Or   r   )*r   r   r*   r   tensorflow.core.protobufr   tensorflow.python.frameworkr   r   tensorflow.python.typesr   tensorflow.python.utilr   r   tensorflow.python.util.compatr	    tensorflow.python.util.tf_exportr
   	Exceptionr   r   r   r%   r,   r7   r   r;   r=   r[   rX   rh   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s]      / . : , ) ' 9 6;	 ;88D 62>; ?;  2r:0 ;0G G(== M M(Q Q(+ +@ (" "(- -0 ( (2 2(H HX>O >OD LMNOLMLLr   