
    BVh!A                     ,   d Z ddlm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 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 Zd Zd Zd%dej<                  fdZ edg      	 d&deej@                  ej<                  f   fd       Z! edg       	 d'deej@                  ej<                  f   fd       Z"deej@                  ej<                  f   fdZ#dej<                  fdZ$d Z%	 	 	 d(dZ& ejN                  ejP                  e&d       	 	 	 d(d Z) ejN                  ejT                  e)d        G d! d"      Z+ ejX                   e+               G d# d$      Z- ejX                   e-              y))zvOperations that generate constants.

See the [constants guide](https://tensorflow.org/api_guides/python/constant_op).
    )UnionN)	types_pb2)
struct_pb2)context)execute)constant_tensor_conversion)dtypes)ops)tensor)tensor_conversion_registry)tensor_shape)tensor_util)trace)nested_structure_coder)	tf_exportc                    | j                         }t        j                  |g|t        j                  t        j
                  gt        j                        \  }\  }| |g}d|d|f}t        j                  dd|||      \  }|S )zEEager-only version of Reshape op; requires tensor is an eager Tensor.TTshapes   Reshape   inputsattrsctx)_datatype_enumr   args_to_matching_eagerr	   int32int64)r   shaper   attr_tattr_tshapeinputs_flatr   results           W/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/framework/constant_op.py_eager_reshaper$   ,   s{      "&!88gsV\\6<<0&,,@+x++
.%__!Ku#?(6	-    c                     |j                   j                  }t        | |t        j                        } | |g}d|dt
        j                  f}t        j                  dd|||      \  }|S )zAEager-only version of Fill op; requires value is an eager Tensor.r   
index_types   Fillr   r   )dtypeas_datatype_enumconvert_to_eager_tensorr	   r   r   DT_INT32r   )dimsvaluer   r   r!   r   r"   s          r#   _eager_fillr.   8   s`    ;;''&	 sFLL	9$u+i&8&8
9%__qEs<(6	-r%   c                 p    d| j                   j                  f}t        j                  dd| g||      \  }|S )zFEager-only version of Identity op; requires tensor is an eager Tensor.r   s   Identityr   r   )r(   r)   r   )r   r   r   r"   s       r#   _eager_identityr0   C   s8    --
.%__1fXU=(6	-r%   returnc           	         t        | t        j                        r| j                         } t        | t        j
                        r2|.| j                  |k7  rt        d|  d|d| j                  d      | S |	 |j                  }|j                          t	        j
                  | |j                  |      S # t        $ r" t        j                  |      j                  }Y [w xY w)a  Converts the given `value` to an `EagerTensor`.

  Note that this function could return cached copies of created constants for
  performance reasons.

  Args:
    value: value to convert to EagerTensor.
    ctx: value of context.context().
    dtype: optional desired dtype of the converted EagerTensor.

  Returns:
    EagerTensor created from value.

  Raises:
    TypeError: if `dtype` is not compatible with the type of t.
  zExpected tensor z with dtype z, but got dtype .)
isinstancenpndarraycopyr
   EagerTensorr(   	TypeErrorr)   AttributeErrorr	   as_dtypeensure_initializeddevice_name)r-   r   r(   s      r#   r*   r*   K   s    " rzz" JJLEs'U[[E1(|E9 E${{oQ0 1 1L
6$$e 		77  6ooe$55e6s   :B7 7(C"!C"constant)v1c                 $    t        | ||||d      S )a{  Creates a constant tensor.

  The resulting tensor is populated with values of type `dtype`, as
  specified by arguments `value` and (optionally) `shape` (see examples
  below).

  The argument `value` can be a constant value, or a list of values of type
  `dtype`. If `value` is a list, then the length of the list must be less
  than or equal to the number of elements implied by the `shape` argument (if
  specified). In the case where the list length is less than the number of
  elements specified by `shape`, the last element in the list will be used
  to fill the remaining entries.

  The argument `shape` is optional. If present, it specifies the dimensions of
  the resulting tensor. If not present, the shape of `value` is used.

  If the argument `dtype` is not specified, then the type is inferred from
  the type of `value`.

  For example:

  ```python
  # Constant 1-D Tensor populated with value list.
  tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]

  # Constant 2-D tensor populated with scalar value -1.
  tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.]
                                               [-1. -1. -1.]]
  ```

  `tf.constant` differs from `tf.fill` in a few ways:

  *   `tf.constant` supports arbitrary constants, not just uniform scalar
      Tensors like `tf.fill`.
  *   `tf.constant` creates a `Const` node in the computation graph with the
      exact value at graph construction time. On the other hand, `tf.fill`
      creates an Op in the graph that is expanded at runtime.
  *   Because `tf.constant` only embeds constant values in the graph, it does
      not support dynamic shapes based on other runtime Tensors, whereas
      `tf.fill` does.

  Args:
    value:          A constant value (or list) of output type `dtype`.

    dtype:          The type of the elements of the resulting tensor.

    shape:          Optional dimensions of resulting tensor.

    name:           Optional name for the tensor.

    verify_shape:   Boolean that enables verification of a shape of values.

  Returns:
    A Constant Tensor.

  Raises:
    TypeError: if shape is incorrectly specified or unsupported.
  Fverify_shapeallow_broadcast_constant_impl)r-   r(   r   namerB   s        r#   constant_v1rG   o   s    | 
ueT(-
/ /r%   c                 $    t        | |||dd      S )a#  Creates a constant tensor from a tensor-like object.

  Note: All eager `tf.Tensor` values are immutable (in contrast to
  `tf.Variable`). There is nothing especially _constant_ about the value
  returned from `tf.constant`. This function is not fundamentally different from
  `tf.convert_to_tensor`. The name `tf.constant` comes from the `value` being
  embedded in a `Const` node in the `tf.Graph`. `tf.constant` is useful
  for asserting that the value can be embedded that way.

  If the argument `dtype` is not specified, then the type is inferred from
  the type of `value`.

  >>> # Constant 1-D Tensor from a python list.
  >>> tf.constant([1, 2, 3, 4, 5, 6])
  <tf.Tensor: shape=(6,), dtype=int32,
      numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>
  >>> # Or a numpy array
  >>> a = np.array([[1, 2, 3], [4, 5, 6]])
  >>> tf.constant(a)
  <tf.Tensor: shape=(2, 3), dtype=int64, numpy=
    array([[1, 2, 3],
           [4, 5, 6]])>

  If `dtype` is specified, the resulting tensor values are cast to the requested
  `dtype`.

  >>> tf.constant([1, 2, 3, 4, 5, 6], dtype=tf.float64)
  <tf.Tensor: shape=(6,), dtype=float64,
      numpy=array([1., 2., 3., 4., 5., 6.])>

  If `shape` is set, the `value` is reshaped to match. Scalars are expanded to
  fill the `shape`:

  >>> tf.constant(0, shape=(2, 3))
    <tf.Tensor: shape=(2, 3), dtype=int32, numpy=
    array([[0, 0, 0],
           [0, 0, 0]], dtype=int32)>
  >>> tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
  <tf.Tensor: shape=(2, 3), dtype=int32, numpy=
    array([[1, 2, 3],
           [4, 5, 6]], dtype=int32)>

  `tf.constant` has no effect if an eager Tensor is passed as the `value`, it
  even transmits gradients:

  >>> v = tf.Variable([0.0])
  >>> with tf.GradientTape() as g:
  ...     loss = tf.constant(v + v)
  >>> g.gradient(loss, v).numpy()
  array([2.], dtype=float32)

  But, since `tf.constant` embeds the value in the `tf.Graph` this fails for
  symbolic tensors:

  >>> with tf.compat.v1.Graph().as_default():
  ...   i = tf.compat.v1.placeholder(shape=[None, None], dtype=tf.float32)
  ...   t = tf.constant(i)
  Traceback (most recent call last):
  ...
  TypeError: ...

  `tf.constant` will create tensors on the current device. Inputs which are
  already tensors maintain their placements unchanged.

  Related Ops:

  * `tf.convert_to_tensor` is similar but:
    * It has no `shape` argument.
    * Symbolic tensors are allowed to pass through.

    >>> with tf.compat.v1.Graph().as_default():
    ...   i = tf.compat.v1.placeholder(shape=[None, None], dtype=tf.float32)
    ...   t = tf.convert_to_tensor(i)

  * `tf.fill`: differs in a few ways:
    *   `tf.constant` supports arbitrary constants, not just uniform scalar
        Tensors like `tf.fill`.
    *   `tf.fill` creates an Op in the graph that is expanded at runtime, so it
        can efficiently represent large tensors.
    *   Since `tf.fill` does not embed the value, it can produce dynamically
        sized outputs.

  Args:
    value: A constant value (or list) of output type `dtype`.
    dtype: The type of the elements of the resulting tensor.
    shape: Optional dimensions of resulting tensor.
    name: Optional name for the tensor.

  Returns:
    A Constant Tensor.

  Raises:
    TypeError: if shape is incorrectly specified or unsupported.
    ValueError: if called on a symbolic tensor.
  FTrA   rD   )r-   r(   r   rF   s       r#   r>   r>      s    F 
ueT(,
. .r%   c                 4   t        j                          }|j                         rMt        j                  r.t        j                  d      5  t        || |||      cddd       S t        || |||      S t        j                  | |||||      }|S # 1 sw Y   4xY w)zImplementation of constant.ztf.constantN)r   executing_eagerlyr   enabledTrace_constant_eager_implr
   _create_graph_constant)r-   r(   r   rF   rB   rC   r   const_tensors           r#   rE   rE     s     	#}};;}% L#CulKL LUE5,GG++UE4, 
L Ls   
BBc                    t        || |      }||S t        j                  |      }||j                  k(  r|S |r4t	        d| d| dt        |       dt        |j                         d	      |j                  j                         }||j                         k(  rt        ||j                         |       S |dk(  r|j                  t        j                  k(  rOt        j                  d      5  t        |j                         t        ||       |       }ddd       t        |       S t        |j                         ||       S t	        d	| d| d
|dd| d|j                          d      # 1 sw Y   ZxY w)z)Creates a constant on the current device.NzExpected Tensor z (converted from z) with shape z, but got shape r3   r   z/device:CPU:0z>Eager execution of tf.constant with unsupported shape. Tensor z) has dz elements, but got `shape` z with z elements).)r*   r   as_shaper   r9   tuplenum_elementsr$   as_listr(   r	   boolr
   devicer.   r0   )r   r-   r(   r   rB   tnum_txs           r#   rM   rM   )  s|    eS%0!
]H



&%
aggH
&qc):5'U|n$4U177^4DAG H H
''


 %
e  ""!U]]_c22
aZww&++ ::o& GC)@#FGQ$$!S11 3/wfU1I F//4gV'')*+7 	8 8G Gs   -&E,,E5c                 r    t        | t        j                        r| j                  }n| }|j                  dk(  S )NConst)r4   
tensor_libTensoroptype)tensor_or_opr_   s     r#   is_constantrb   I  s/    j//0	B	B	G	r%   c                    |}| j                         st        d|  d      | j                         }d}|D ]  }|dk\  s	|} n |X|t        j                  t        j
                  fvrt        d|  d| d      |t        j                  k(  r3|r1t        d|  d      |rt        j
                  nt        j                  }|d	}t        |||
      S )z*Function to convert TensorShape to Tensor.z-Cannot convert a partially known TensorShape  to a Tensor.r   l        zCannot convert TensorShape 
 to dtype +. Allowed dtypes are tf.int32 and tf.int64.zC to dtype int32; a dimension is too large. Consider using tf.int64.shape_as_tensorr(   rF   )is_fully_defined
ValueErrorrU   r	   r   r   r9   r>   )sr(   rF   as_ref_s_listint64_valuedims           r#   (_tensor_shape_tensor_conversion_functionrq   Q  s    
 !	
			

7s-HJ J99;&+ c
e|k
 V\\6<<003A3j HB B C C4QC 8L M N N (FLLV\\E	\D	&D	11r%   d   c                     |}| j                   t        d|  d      |4|t        j                  t        j                  fvr"t        d|  d| d      t        j                  }|d}t        | j                   ||      S )z(Function to convert Dimension to Tensor.z!Cannot convert unknown Dimension rd   zCannot convert Dimension re   rf   rg   rh   )r-   rj   r	   r   r   r9   r>   )rQ   r(   rF   rl   rm   s        r#   %_dimension_tensor_conversion_functionrt   s  s    
 !WW_
8=I
JJ
V\\6<<001!Jug FB B C C LLE	\D	!''T	22r%   c                   (    e Zd ZdZd Zd Zd Zd Zy)_ConstantTensorCodeczCodec for Tensor.c                 6    t        |t        j                        S N)r4   r]   r^   selfpyobjs     r#   
can_encodez_ConstantTensorCodec.can_encode  s    eZ..//r%   c                    ~t        j                         }t        |t        j                        r>|j
                  j                  t        j                  |j                                      |S |j                  j                  dk(  r6|j
                  j                  |j                  j                  d             |S t        j                  dt        |       dt        |       d      )z;Returns an encoded `TensorProto` for the given `tf.Tensor`.r\   r-   zNo encoder for object z	 of type r3   )r   StructuredValuer4   r
   r8   tensor_valueCopyFromr   make_tensor_protonumpyr_   r`   get_attrr   NotEncodableErrorstr)rz   r   	encode_fnencoded_tensors       r#   	do_encodez_ConstantTensorCodec.do_encode  s    //1N,0!!**

'
'(:(:(<
=  
			(##,,\__-E-Eg-NO 	 %66$S%6$7 8\"#1&
 	
r%   c                 $    |j                  d      S )Nr   HasFieldrz   r-   s     r#   
can_decodez_ConstantTensorCodec.can_decode  s    >>.))r%   c                 \    ~|j                   }t        t        j                  |            }|S )z5Returns the `tf.Tensor` encoded by the proto `value`.)r   r>   r   MakeNdarray)rz   r-   	decode_fntensor_protor   s        r#   	do_decodez_ConstantTensorCodec.do_decode  s*    %%Lk--l;<FMr%   N__name__
__module____qualname____doc__r|   r   r   r    r%   r#   rv   rv     s    0$*r%   rv   c                   (    e Zd ZdZd Zd Zd Zd Zy)_NumpyCodeczCodec for Numpy.c                 6    t        |t        j                        S rx   )r4   r5   r6   ry   s     r#   r|   z_NumpyCodec.can_encode  s    eRZZ((r%   c                     ~t        j                         }|j                  j                  t	        j
                  |             |S )z2Returns an encoded `TensorProto` for `np.ndarray`.)r   r~   numpy_valuer   r   r   )rz   r   r   encoded_numpys       r#   r   z_NumpyCodec.do_encode  s=    ..0M&&%%k2 r%   c                 $    |j                  d      S )Nr   r   r   s     r#   r   z_NumpyCodec.can_decode  s    >>-((r%   c                 J    ~|j                   }t        j                  |      }|S )z6Returns the `np.ndarray` encoded by the proto `value`.)r   r   r   )rz   r-   r   r   r   s        r#   r   z_NumpyCodec.do_decode  s%    $$L##L1ELr%   Nr   r   r%   r#   r   r     s    ))r%   r   rx   )NNr\   F)NNr\   )NNF).r   typingr   r   r5   tensorflow.core.frameworkr   tensorflow.core.protobufr   tensorflow.python.eagerr   r   tensorflow.python.frameworkr   r	   r
   r   r]   r   r   r   tensorflow.python.profilerr   tensorflow.python.saved_modelr    tensorflow.python.util.tf_exportr   r$   r.   r0   _EagerTensorBaser*   	OperationrG   r>   rE   rM   rb   rq   #register_tensor_conversion_functionTensorShapert   	Dimensionrv   register_codecr   r   r%   r#   <module>r      s     / / + +
 C . + < B 4 3 , @ 6	!8s7K7K !8H zl>C>/
3==#.../>/ >/B :"(/c.
3==#.../c. c.L
3==#.../"88@ 4826492< ?  > >FM
 15/3163& ?  > >A3H   F &  % %&:&< = 4 &  % %km 4r%   