
    AVh                         d 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 ddZddZ ej                   d      d        Z ej                   d      d        Zy
)z6Operations for ExtensionTypes (aka Composite Tensors).    )composite_tensor_variant_pb2)composite_tensor)dtypes)ops)tensor)gen_composite_tensor_ops)nested_structure_coder)nestNc                    t        | t        j                        st        dt	        |        d      || j
                  }|j                  |       st        d| d| d      t        j                         }|j                  j                  t        j                  |      j                         t        j                   t#        j$                  | d      |j'                         |      S )a~  Encodes `value` as a scalar variant tensor.

  Args:
    value: The `ExtensionType` value to encode.
    type_spec: Information about the value's type that should be included in the
      encoding.
    name: Optional name for the operation.

  Returns:
    A Tensor with shape=`()` and dtype=`tf.variant`.

  Raises:
    ValueError: If `type_spec` is not compatible with `value`.
  z3Expected `value` to be a CompositeTensor. Received .z`type_spec` z  is not compatible with `value` Texpand_composites)
componentsmetadataname)
isinstancer   CompositeTensor	TypeErrortype
_type_specis_compatible_with
ValueErrorr   CompositeTensorVariantMetadatatype_spec_protoCopyFromr	   encode_structuretype_spec_valuer   $CompositeTensorVariantFromComponentsr
   flattenSerializeToString)value	type_specr   r   s       Z/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/composite_tensor_ops.pycomposite_tensor_to_variantsr$      s     
E+;;	<
   $U}A/ 0 0   I		%	%e	,
|I;.Ny# $ $)HHJ(
##--i8HHJ 
"	F	Fet<))+
     c                 ^   t        | t        j                        st        d| d      | j                  t
        j                  k7  rt        d| d      | j                  j                  d       t        j                         }|j                  j                  t        j                  |      j                         t!        j"                  |d      D cg c]  }|j                   }}t%        j&                  | |j)                         ||      }t!        j*                  ||d      S c c}w )a  Returns the `ExtensionType` value encoded by a variant scalar tensor.

  Args:
    encoded: A Tensor returned by `composite_tensor_to_variants`.
    type_spec: The `TypeSpec` of the original value.  This is used to determine
      the number and types of the component tensors that comprise the decoded
      value.  Must be compatible with the `TypeSpec` serilized in `encoded`.
    name: Optional name for the operation.

  Returns:
    An `ExtensionType` value that is compatible with `TypeSpec`.

  Raises:
    TypeError: If `encoded` is not a Tensor with dtype=variant.
    InvalidArgumentError: If `encoded` is not compatible with `type_spec`.
  z'Expected `encoded` to be a Tensor, got r   z.Expected `encoded` to have dtype=variant, got  Tr   )encodedr   Tcomponentsr   )r   r   Tensorr   dtyper   variantshapeassert_is_compatible_withr   r   r   r   r	   r   r   r
   r   r   "CompositeTensorVariantToComponentsr    pack_sequence_as)r(   r"   r   r   tcomponent_dtypesr   s          r#   composite_tensor_from_variantr3   =   s   " 
GV]]	+
=g[J
KK]]fnn$
Dk$ % %	--))"-)HHJ(
##--i8HHJ ||IFagg  (JJ))+"	*
 
		y*	MMs   D*r   c                 n    t        j                  || j                  d      | j                  d            S )Nr   r)   )r(   r   r)   )r   r/   get_attr)opgrads     r#   "_composite_tensor_to_variants_gradr8   e   s0    	!	D	D{{:&++m,
. .r%   r/   c                    t        |      t        | j                        k(  sJ t        t        |            D cg c]  }||   | j                  |   n||    }}t        j                  || j                  d            S c c}w )Nr   )r   r   )lenoutputsranger   r   r5   )r6   r7   ir   s       r#   #_composite_tensor_from_variant_gradr>   m   s    	Tc"**o	%%	% >C3t9=M89tAwbjjmDG3*  
"	F	Fbkk*&=
? ?s    B)NN)N)__doc__tensorflow.core.protobufr   tensorflow.python.frameworkr   r   r   r   tensorflow.python.opsr   tensorflow.python.saved_modelr	   tensorflow.python.utilr
   r$   r3   RegisterGradientr8   r>   r'   r%   r#   <module>rF      ss    = A 8 . + . : @ 'D%NP <=. >. :;? <?r%   