
    6bip                        S r SSKJs  Jr  SSKJr  SSKJr  SSK	J
r
  Sr\
" S/ S9 " S	 S
5      5       r\R                  \R                  5        \
" S/ S9 " S S\5      5       r\
" S/ S9 " S S\5      5       r\R#                  \R$                  S5        \R#                  \R$                  S5        \R#                  \R$                  S5        \R#                  \R$                  S5        \R#                  \R$                  S5         " S S\R&                  5      r " S S\5      r " S S5      r\R                  \4\R.                  \4\R$                  \4\\4/rS rS rS rS!S jrS  rg)"z9Keras Input Tensor used to track functional API Topology.    N)object_identity)	structure)keras_export   zkeras.__internal__.KerasTensor)v1c                      \ rS rSrSrSS jr\S 5       r\S 5       r\	S 5       r
\	SS j5       rS	 rS
 rS r\S 5       rS rSrSS jr\S 5       rS rS rS r\S 5       rS r\S 5       rS r\S 5       r\	S 5       r\	S 5       rSrg)KerasTensor!   a+  A representation of a TF-Keras in/output during Functional API
construction.

`KerasTensor`s are tensor-like objects that represent the symbolic inputs
and outputs of TF-Keras layers during Functional model construction. They
are comprised of the `tf.TypeSpec` of the (Composite)Tensor that will be
consumed/produced in the corresponding location of the Functional model.

KerasTensors are intended as a private API, so users should never need to
directly instantiate `KerasTensor`s.

**Building Functional Models with KerasTensors**
`tf.keras.Input` produces `KerasTensor`s that represent the symbolic inputs
to your model.

Passing a `KerasTensor` to a `tf.keras.Layer` `__call__` lets the layer know
that you are building a Functional model. The layer __call__ will
infer the output signature and return `KerasTensor`s with `tf.TypeSpec`s
corresponding to the symbolic outputs of that layer call. These output
`KerasTensor`s will have all of the internal KerasHistory metadata attached
to them that TF-Keras needs to construct a Functional Model.

Currently, layers infer the output signature by:
  * creating a scratch `FuncGraph`
  * making placeholders in the scratch graph that match the input typespecs
  * Calling `layer.call` on these placeholders
  * extracting the signatures of the outputs before clearing the scratch
    graph

(Note: names assigned to KerasTensors by this process are not guaranteed to
be unique, and are subject to implementation details).

`tf.nest` methods are used to insure all of the inputs/output data
structures get maintained, with elements swapped between KerasTensors and
placeholders.

In rare cases (such as when directly manipulating shapes using Keras
layers), the layer may be able to partially infer the value of the output in
addition to just inferring the signature.
When this happens, the returned KerasTensor will also contain the inferred
value information. Follow-on layers can use this information.
during their own output signature inference.
E.g. if one layer produces a symbolic `KerasTensor` that the next layer uses
as the shape of its outputs, partially knowing the value helps infer the
output shape.

**Automatically converting TF APIs to layers**:
If you passing a `KerasTensor` to a TF API that supports dispatching,
TF-Keras will automatically turn that API call into a lambda
layer in the Functional model, and return KerasTensors representing the
symbolic outputs.

Most TF APIs that take only tensors as input and produce output tensors
will support dispatching.

Calling a `tf.function` does not support dispatching, so you cannot pass
`KerasTensor`s as inputs to a `tf.function`.

Higher-order APIs that take methods which produce tensors (e.g. `tf.while`,
`tf.map_fn`, `tf.cond`) also do not currently support dispatching. So, you
cannot directly pass KerasTensors as inputs to these APIs either. If you
want to use these APIs inside of a Functional model, you must put them
inside of a custom layer.

Args:
  type_spec: The `tf.TypeSpec` for the symbolic input created by
    `tf.keras.Input`, or symbolically inferred for the output
    during a symbolic layer `__call__`.
  inferred_value: (Optional) a non-symbolic static value, possibly partially
    specified, that could be symbolically inferred for the outputs during
    a symbolic layer `__call__`. This will generally only happen when
    grabbing and manipulating `tf.int32` shapes directly as tensors.
    Statically inferring values in this way and storing them in the
    KerasTensor allows follow-on layers to infer output signatures
    more effectively. (e.g. when using a symbolic shape tensor to later
    construct a tensor with that shape).
  name: (optional) string name for this KerasTensor. Names automatically
    generated by symbolic layer `__call__`s are not guaranteed to be unique,
    and are subject to implementation details.
Nc                    [        U[        R                  5      (       d  [        S5      eXl        X l        X0l        [        U[        R                  5      (       d  [        US5      (       d"  [        S[        U5      R                   S35      e[        UR                  [        R                  5      (       dB  [        S[        U5      R                   S[        UR                  5      R                   S35      egg)	zConstructs a KerasTensor.z6KerasTensors must be constructed with a `tf.TypeSpec`.shapezAKerasTensor only supports TypeSpecs that have a shape field; got z, which does not have a shape.zRKerasTensor requires that wrapped TypeSpec's shape is a TensorShape; got TypeSpec z(, whose shape field has unexpected type .N)
isinstancetfTypeSpec
ValueError
_type_spec_inferred_value_namer   NoneTensorSpechasattrtype__qualname__r   TensorShape	TypeErrordtype)self	type_specinferred_valuenames       Z/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/engine/keras_tensor.py__init__KerasTensor.__init__t   s    )R[[11H  $-
)Y%=%=>>9g.. ""&y/">">!? @33 
 ioor~~>>115i1M1M0N>IOO,99:!=  ? ?    c                     U R                   $ )zEReturns the `tf.TypeSpec` symbolically inferred for TF-Keras
output.
)r   r   s    r    r   KerasTensor.type_spec   s    
 r#   c                 .    U R                   R                  $ )zEReturns the `TensorShape` symbolically inferred for TF-Keras
output.
)r   r   r%   s    r    r   KerasTensor.shape   s    
 $$$r#   c                 <   [        U[        R                  5      (       a  [        USS5      n[        R                  " U5      nSnUR
                  [        R                  :X  a  UR                  R                  bq  UR                  R                  S:  aW  [        R                  " US9R                  nUR                  (       a&  UR                  5       n[        U5      [        :  a  SnOSn[        X4US9$ [        USS5      n[        R                  " U5      nU " X2S9$ )zCConvert a traced (composite)tensor to a representative
KerasTensor.r   N   r   )r   r   r   )r   r   Tensorgetattrtype_spec_from_valuer   int32r   rankonesdimsas_listlen_MAX_TENSOR_RANKr	   )clstensorr   r   r   s        r    from_tensorKerasTensor.from_tensor   s     fbii((6640D//7I!N288+OO((4OO((1,2 "$v!6!<!<!&&%3%;%;%=N>*-==)-%)Nt 
 6640D//7Iy,,r#   c                     U " XS9$ )N)r   r    r7   r   r   s      r    from_type_specKerasTensor.from_type_spec   s    Y22r#   c                 p   U R                   b  [        R                  " [        R                  R                  R                  U R                   [        R                  S95      nU R                  R                  R                  S:X  a  US   nU$ S n[        R                  R                  X R                  SS9$ )z5Convert this KerasTensor to a placeholder in a graph.)r   r   r   c                 ~    [         R                  R                  R                  U R                  U R
                  5      $ N)r   compatr   placeholderr   r   )	components    r    component_to_placeholder=KerasTensor._to_placeholder.<locals>.component_to_placeholder   s%    99<<++IOOY__MMr#   T)expand_composites)r   r   r   rC   r   rD   r0   r   r1   nestmap_structure)r   r   rF   s      r    _to_placeholderKerasTensor._to_placeholder   s     +  XX		((..bhh ) N
 ~~##((A- "0!2!!	N ww$$$nn % 
 	
r#   c                     U R                   $ rB   r+   r%   s    r    	get_shapeKerasTensor.get_shape   s    zzr#   c                     [        S5      e)Nae  Keras symbolic inputs/outputs do not implement `__len__`. You may be trying to pass TF-Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing TF-Keras from automatically converting the API call to a lambda layer in the Functional Model. This error will also get raised if you try asserting a symbolic input/output directly.r   r%   s    r    __len__KerasTensor.__len__   s    E	
 		
r#   c                     [        S5      e)Na	  Keras symbolic inputs/outputs do not implement `op`. You may be trying to pass TF-Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing TF-Keras from automatically converting the API call to a lambda layer in the Functional Model.rQ   r%   s    r    opKerasTensor.op  s    '
 	
r#   c                      [        SU  S35      e)Nz%Tensors are unhashable (this tensor: z(). Instead, use tensor.ref() as the key.rQ   r%   s    r    __hash__KerasTensor.__hash__  s     3D6 :4 4
 	
r#   d   c                      [        SU  S35      e)NzYou are passing a  , an intermediate TF-Keras symbolic input/output, to a TF API that does not allow registering custom dispatchers, such as `tf.cond`, `tf.function`, gradient tapes, or `tf.map_fn`. TF-Keras Functional model construction only supports TF API calls that *do* support dispatching, such as `tf.math.add` or `tf.reshape`. Other APIs cannot be called directly on symbolic Kerasinputs/outputs. You can work around this limitation by putting the operation in a custom TF-Keras layer `call` and calling that layer on this symbolic input/output.rQ   )r   r   s     r    	__array__KerasTensor.__array__!  s     tf 
%- 
-
 	
r#   c                     g)NTr<   r%   s    r    is_tensor_likeKerasTensor.is_tensor_like0  s    r#   c                 R   [        U[        R                  5      (       d  [        R                  " U5      nU R                  R	                  U5      (       d  [        SU R                   SU S35      eU R                  R                  U5      n[        U R                  U5      U l        g)zFUpdates the shape of this KerasTensor. Mimics
`tf.Tensor.set_shape()`.z$Keras symbolic input/output's shape z' is not compatible with supplied shape r   N)	r   r   r   r   is_compatible_withr   
merge_withtype_spec_with_shaper   )r   r   s     r    	set_shapeKerasTensor.set_shape4  s     %00NN5)Ezz,,U336tzzl C227; 
 JJ))%0E24??EJDOr#   c                 $   SnSnSn[        U S5      (       a'  U R                  R                  nSUR                  < S3nU R                  b  SU R                   3nU R                  b  SU R
                   S3nSU R                  < U< U< U< S	3$ )
N _keras_historyz!, description="created by layer 'z'"z, inferred_value=z, name=''zKerasTensor(type_spec=))r   ri   layerr   r   r   r   )r   symbolic_descriptioninferred_value_stringname_stringrl   s        r    __str__KerasTensor.__str__B  s    ! "4)**''--E

$  +&78L8L7M$N!99 $TZZL2KNN! 	
 	
r#   c                    SnSn[        U R                  [        R                  5      (       a'  SU R                   SU R
                  R                   3nOSU R                   3n[        U S5      (       a&  U R                  R                  nSUR                   S3nU R                  b  SU R                   3nS	U< U< U< S
3$ )Nrh   zshape=z dtype=z
type_spec=ri   z (created by layer 'z')z inferred_value=z<KerasTensor: >)r   r   r   
TensorSpecr   r   r   r   ri   rl   r   )r   rm   rn   type_spec_stringrl   s        r    __repr__KerasTensor.__repr__W  s    ! "dnnbmm44!'

|74::??:KL!+DNN+;<4)**''--E%9%**R#H +&6t7K7K6L$M!! 
 	
r#   c                 n   U R                   n[        US5      (       d"  [        S[        U5      R                   S35      e[        UR                  [        R                  5      (       dB  [        S[        U5      R                   S[        UR                  5      R                   S35      eUR                  $ )zDReturns the `dtype` symbolically inferred for this TF-Keras
output.
r   zKerasTensor wraps TypeSpec z, which does not have a dtype.zLKerasTensor requires that wrapped TypeSpec's dtype is a DType; got TypeSpec z(, whose dtype field has unexpected type r   )
r   r   AttributeErrorr   r   r   r   r   DTyper   )r   r   s     r    r   KerasTensor.dtypej  s    
 OO	y'** -d9o.J.J-K L/ /  )//28844''+I'C'C&D E3	(556a9  r#   c                 .    [         R                  " U 5      $ )ay  Returns a hashable reference object to this KerasTensor.

The primary use case for this API is to put KerasTensors in a
set/dictionary. We can't put tensors in a set/dictionary as
`tensor.__hash__()` is not available and tensor equality (`==`) is
supposed to produce a tensor representing if the two inputs are equal.

See the documentation of `tf.Tensor.ref()` for more info.
)r   	Referencer%   s    r    refKerasTensor.ref~  s     ((..r#   c                 b    [        U S5      (       a  U R                  u  pnUR                  U   $ g)a  Find the corresponding `Node` that produce this keras_tensor.

During functional model construction, TF-Keras will attach
`KerasHistory` to keras tensor to track the connectivity between calls
of layers. Return None if there isn't any KerasHistory attached to this
tensor.
ri   N)r   ri   inbound_nodes)r   rl   
node_index_s       r    nodeKerasTensor.node  s6     4)**#'#6#6 Eq&&z22r#   c                    S nU R                   R                  b/  U R                   R                   Vs/ s H  o"R                  PM     nnUc  [	        S5      eU(       d  [	        S5      eUS   c  [	        S5      e[        XS   5      $ s  snf )Nz0Cannot iterate over a Tensor with unknown shape.zCannot iterate over a scalar.r   z:Cannot iterate over a Tensor with unknown first dimension.)r   ndimsr3   valuer   _KerasTensorIterator)r   r   dims      r    __iter__KerasTensor.__iter__  s    ::'*.**//:/3YY/E:=NOO;<<8L  $D(33 ;s   Bc                     U R                   $ )zEReturns the (non-unique, optional) name of this symbolic Keras
value.)r   r%   s    r    r   KerasTensor.name  s     zzr#   c                     [         R                  R                   H  nU R                  X5        M     [	        US5      (       a  U R                  US5        gg)z%Register overloads for all operators.experimental_refN)r   r-   OVERLOADABLE_OPERATORS_overload_operatorr   )r7   tensor_classoperators      r    _overload_all_operators#KerasTensor._overload_all_operators  sL     		88H""<: 9
 <!344""<1CD 5r#   c                 L    [        X5      n[        USU5      n[        XU5        g)zOverload operator with the same implementation as the Tensor class.

We pull the operator out of the class dynamically to avoid ordering
issues.

Args:
  tensor_class: The (Composite)Tensor to get the method from.
  operator: string. The operator name.
__func__N)r.   setattr)r7   r   r   tensor_opers       r    r   KerasTensor._overload_operator  s(     l5
 k:{C{+r#   )r   r   r   )NNrB   )__name__
__module__r   __firstlineno____doc__r!   propertyr   r   classmethodr9   r>   rK   rN   rR   rU   rX   __array_priority__r\   r_   re   rp   rv   r   r~   r   r   r   r   r   __static_attributes__r<   r#   r    r	   r	   !   s)   Ob4   % % 2- 2-h 3 3 
D

 	
 	

 
  K
*
&  &
/  4  
 E E , ,r#   r	   z$keras.__internal__.SparseKerasTensorc                       \ rS rSrSrS rSrg)SparseKerasTensori  zA specialized KerasTensor representation for `tf.sparse.SparseTensor`s.

Specifically, it specializes the conversion to a placeholder in order
to maintain dense shape information.
c                     U R                   n[        R                  R                  R	                  UR
                  UR                  S9$ )N)r   r   )r   r   rC   r   sparse_placeholderr   r   )r   specs     r    rK   !SparseKerasTensor._to_placeholder  s:    ~~ yy||..**DJJ / 
 	
r#   r<   N)r   r   r   r   r   rK   r   r<   r#   r    r   r     s    

r#   r   z$keras.__internal__.RaggedKerasTensorc                   <   ^  \ rS rSrSrU 4S jr\S 5       rSrU =r	$ )RaggedKerasTensori  a  A specialized KerasTensor representation for `tf.RaggedTensor`s.

Specifically, it:

1. Specializes the conversion to a placeholder in order
to maintain shape information for non-ragged dimensions.
2. Overloads the KerasTensor's operators with the RaggedTensor versions
when they don't match the `tf.Tensor` versions
3. Exposes some of the instance method/attribute that are unique to
the RaggedTensor API (such as ragged_rank).
c                 j  > U R                   nUR                  S:X  d  UR                  R                  c  [        TU ]  5       $ UR                  UR                  S  n[        R                  R                  R                  UR                  U5      n/ nSnUR                   H/  nUb  Ub  [        USS5      c  S nOXV-  nUR                  U5        M1     [        UR                  SS5       H  nUR                  U   nUb  [        USS5      cd  XGS-
     nUb  US-   n[        R                  R                  R                  UR                  U/5      n	[        R                  R!                  X9SS9nM  [        R"                  " XaR                  5      n
[        R                  R%                  X:SS9nM     U$ )Nr      r   TF)validate)r   ragged_rankr   r1   superrK   r   rC   r   rD   r   r.   appendrangerow_splits_dtypeRaggedTensorfrom_row_splitsconstantfrom_uniform_row_length)r   ragged_spec
flat_shaperesultknown_num_splitsprod	axis_sizeaxis
num_splitssplitsrowlen	__class__s              r    rK   !RaggedKerasTensor._to_placeholder  s   nn""a';+<+<+A+A+I7*,, &&{'>'>'@A
))+*;*;ZH$**I$Iw5=D+D##D) + +111b9D#))$/I WY%F%N-Qh7
)!+aJ1100:, 88U 9  Y0L0LM@@U A  :" r#   c                 .    U R                   R                  $ rB   )r   r   r%   s    r    r   RaggedKerasTensor.ragged_rank  s    ~~)))r#   r<   )
r   r   r   r   r   rK   r   r   r   __classcell__r   s   @r    r   r     s"    
%N * *r#   r   __getitem____add____radd____mul____rmul__c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)UserRegisteredSpeci/  z7TypeSpec to represent user-registered symbolic objects.c                 (    Xl         X l        X l        g rB   )r   _dtyper   )r   r   r   s      r    r!   UserRegisteredSpec.__init__2  s    

r#   c                     [         erB   NotImplementedErrorr%   s    r    _component_specs#UserRegisteredSpec._component_specs7      !!r#   c                     [         erB   r   )r   
componentss     r    _from_components#UserRegisteredSpec._from_components:  r   r#   c                     [         erB   r   r%   s    r    
_serializeUserRegisteredSpec._serialize=  r   r#   c                     [         erB   r   )r   r   s     r    _to_components!UserRegisteredSpec._to_components@  r   r#   c                     [         erB   r   r%   s    r    
value_typeUserRegisteredSpec.value_typeC  r   r#   )r   r   r   N)r   r   r   r   r   r!   r   r   r   r   r   r   r<   r#   r    r   r   /  s#    A
"""""r#   r   c                   V   ^  \ rS rSrSrU 4S jr\S 5       r\SS j5       rS r	Sr
U =r$ )	UserRegisteredTypeKerasTensoriM  zAKerasTensor that represents legacy register_symbolic_tensor_type.c                    > UnX l         [        UR                  UR                  5      n[	        USS 5      n[
        TU ]  X45        g )Nr   ) _user_registered_symbolic_objectr   r   r   r.   r   r!   )r   user_registered_symbolic_objectxr   r   r   s        r    r!   &UserRegisteredTypeKerasTensor.__init__P  s=    +01-&qww8	q&$')r#   c                     U " U5      $ rB   r<   )r7   r8   s     r    r9   )UserRegisteredTypeKerasTensor.from_tensorX  s    6{r#   c                     [        SU-  5      e)Nz?You cannot instantiate a KerasTensor directly from TypeSpec: %sr   r=   s      r    r>   ,UserRegisteredTypeKerasTensor.from_type_spec\  s    !M
 	
r#   c                     U R                   $ rB   r   r%   s    r    rK   -UserRegisteredTypeKerasTensor._to_placeholderc  s    444r#   r   rB   )r   r   r   r   r   r!   r   r9   r>   rK   r   r   r   s   @r    r   r   M  s=    K*   
 
5 5r#   r   c                   *    \ rS rSrSrS rS rS rSrg)r   ig  zHIterates over the leading dim of a KerasTensor. Performs 0 error
checks.c                 *    Xl         SU l        X l        g )Nr   )_tensor_index_limit)r   r8   dim0s      r    r!   _KerasTensorIterator.__init__k  s    r#   c                     U $ rB   r<   r%   s    r    r   _KerasTensorIterator.__iter__p  s    r#   c                     U R                   U R                  :X  a  [        eU R                  U R                      nU =R                   S-  sl         U$ )Nr   )r   r   StopIterationr   )r   r   s     r    __next___KerasTensorIterator.__next__s  s=    ;;$++%dkk*qr#   )r   r   r   N)	r   r   r   r   r   r!   r   r   r   r<   r#   r    r   r   g  s    
r#   r   c                 2    [         R                  SX45        g)z>Register a specialized KerasTensor subclass for a Tensor type.r   N)keras_tensor_classesinsert)r7   keras_tensor_subclasss     r    $register_keras_tensor_specializationr    s     S$@Ar#   c                 P    [        U [        5      (       a  U R                  5       $ U $ )zFConstruct a graph placeholder to represent a KerasTensor when tracing.)r   r	   rK   )r   s    r    keras_tensor_to_placeholderr    s#    ![!!  ""r#   c                     Sn[          H  u  p#[        X5      (       d  M  Un  O   UR                  U 5      n[        U SS5      b  [	        U R
                  5      Ul        U$ )zCConvert a traced (composite)tensor to a representative KerasTensor.N_keras_mask)r  r   r9   r.   keras_tensor_from_tensorr	  )r8   keras_tensor_clstensor_typer7   outs        r    r
  r
    sc     0f**" 1
 
&
&v
.Cv}d+7263E3EFJr#   c                     SnU R                   n[         H  u  pE[        X45      (       d  M  Un  O   UR                  XS9$ )z3Convert a TypeSpec to a representative KerasTensor.Nr,   )r   r  
issubclassr>   )r   r   r  r   r  r7   s         r    keras_tensor_from_type_specr    sL     %%J0j.." 1
 **9*@@r#   c                    [        U [        R                  5      (       a  [        R                  " U5      nXl        U $ [        U [        R
                  5      (       aB  [        R
                  " UU R                  U R                  U R                  U R                  5      $ [        U [        R                  5      (       a   [        R                  " XR                  5      $ [        U S5      (       a  U R                  U5      $ [        S5      e)z@Returns a copy of TypeSpec `spec` with its shape set to `shape`.
with_shapezjKeras requires TypeSpec to have a `with_shape` method that returns a copy of `self` with an updated shape.)r   r   rt   r   _shapeRaggedTensorSpecr   r   r   flat_values_specSparseTensorSpecr   r  r   )r   r   s     r    rd   rd     s    $&& u%	D"--	.	.""JJ!!!!
 	
 
D"--	.	.""5**55	|	$	$ u%% C
 	
r#   rB   )r   tensorflow.compat.v2rC   v2r   tf_keras.src.utilsr   tensorflow.python.data.utilr    tensorflow.python.util.tf_exportr   r6   r	   r   r-   r   r   r   r   r   r   r   r   SparseTensorobjectr  r  r  r
  r  rd   r<   r#   r    <module>r     s   @ ! ! . 2 9   .26h, h, 7h,V  # #BII . 4<
 
 =
( 4<6* 6* =6*t  $ $R__m D  $ $R__i @  $ $R__j A  $ $R__i @  $ $R__j A" "<5K 54 : YY__'(__'([	 B"A
r#   