
    BVh?,              	           d Z ddl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 dZ G d d      Z G d d ej.                  dg d            Zd Zy)zContains the `Node` class.    N)ops)tensor)tensor_util)backend)base_layer_utils)
json_utils)tf_utils)nest_CONSTANT_VALUEc                       e Zd ZdZ	 	 	 ddZed        Zed        Zd Zd Z	d Z
ed	        Zed
        Zed        Zed        Zed        Zed        Zy)Nodea  A `Node` describes the connectivity between two layers.

  Each time a layer is connected to some new input,
  a node is added to `layer._inbound_nodes`.
  Each time the output of a layer is used by another layer,
  a node is added to `layer._outbound_nodes`.

  Args:
      layer: The Layer for the Layer.__call__ this node represents.
      call_args: The positional arguments the Layer was called with.
      call_kwargs: The keyword arguments the Layer was called with.
      outputs: The outputs of the Layer.__call__
  Nc                 l   |g n|}|i n|}|g n|}|| _         | xr | | _        t        j                  d |      }t        j                  d |      }t        j                  d |      | _        || _        || _        t        j                  | j
                  | j                  f      | _        | j                   xr< t        | j
                        dk(  xr" t        j                  | j
                  d         | _        t        j                         sY| j                  D ]J  }t        |t         j"                        st%        j&                  |d      s6t%        j(                  |       L g | _        g | _        t/        | j                        D ]_  \  }}t1        |      s| j*                  j3                  |       t5        t7        |            }|}	| j,                  j3                  ||	f       a | j                   j8                  j3                  |        | j:                  D ]6  }
|
j<                  j                   }||j>                  j3                  |        8 t        | j                   j8                        dz
  }t/        t        j                  |            D ]  \  }}tA        |||      |_         | j*                  D cg c]  }t5        t7        |             c}| _!        t        j                  | j                        D cg c]  }t5        t7        |             c}| _"        y c c}w c c}w )	Nc                     | S N ts    S/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/keras/engine/node.py<lambda>zNode.__init__.<locals>.<lambda>E   s    Q     c                     | S r   r   r   s    r   r   zNode.__init__.<locals>.<lambda>F   s    q r   c                     | S r   r   r   s    r   r   zNode.__init__.<locals>.<lambda>G   s     r      r   T)ignore_call_contextlayer
node_indextensor_index)#r   is_inputr
   map_structureoutputs	call_argscall_kwargsflatten_flat_argumentslenr   
is_tf_type _single_positional_tensor_passedr   #executing_eagerly_outside_functions
isinstance
tensor_libTensorr   needs_keras_historycreate_keras_history_keras_inputs_keras_inputs_ids_and_indices	enumerateis_keras_tensorappendstrid_inbound_nodeskeras_inputs_keras_history_outbound_nodesKerasHistoryflat_input_idsflat_output_ids)selfr   r"   r#   r!   objielekt_idkt_indexktinbound_layerr   r   r   s                  r   __init__zNode.__init__3   s   
  'YI#+"KObGDJ!M5+oDM "";	:I$$[+>K%%k7;DLDN"D  <<9I9I(JKD151A1A-A .KcGG .K!,!7!7q8I!J 	) 224%% 5#sJ--.00/

/
/
4	5 D)+D&D001 E3		!!#&BsG**115(2CDE 	JJ$$T* 3''--m		"%%,,T23 TZZ../!3Jt||G45 >	6**1>f>
 04/A/AB!3r!u:BD04T\\0JK1C1JKD CKs   L,L1c                     | j                   S )zFTensors input to this node that can be traced back to a `keras.Input`.)r/   r=   s    r   r7   zNode.keras_inputss   s     r   c                     g }| j                   D ]O  }|j                  j                  }|j                  j                  }|2|j	                  |j
                  |          Q |S )zFReturns all the `Node`s whose output this node immediately depends on.)r7   r8   r   r   r3   r6   )r=   	node_depsrC   r   r   s        r   parent_nodeszNode.parent_nodesx   sg     I ;%%e$$//j		--j9:	;
 r   c              #      K   | j                   D ]:  }|j                  }|j                  }|j                  }|j                  }||||f < yw)zYields tuples representing the data inbound from other nodes.

    Yields:
      tuples like: (inbound_layer, node_index, tensor_index, tensor).
    N)r7   r8   r   r   r   )r=   rC   keras_historyr   r   r   s         r   iterate_inboundzNode.iterate_inbound   sX       0''m!!e ++j"//l:|R//0s   AAc                 b   | j                   r(| j                  d   \  }}||   j                         fi fS t        j                  | j                        }| j                  D ]  \  }}||   j                         ||<    t        j                  | j                  | j                  f|      \  }}||fS )z;Maps Keras Tensors to computed Tensors using `tensor_dict`.r   )	r(   r0   popcopyr%   r
   pack_sequence_asr"   r#   )r=   tensor_dictrA   _flat_argumentsrB   argskwargss           r   map_argumentszNode.map_arguments   s    ,,33A6heQ% $$&(",,yy!5!56n!?? </%#.u#5#9#9#;x < **DNND<L<L+M+9;ldF6\r   c                    	
 | j                   | j                  c}
| j                  j                  |
      \  }}
t	        t        | j                  j                  dd |            }|j                  
       |
fd	t        j                  	
      
	 t        j                  
t        j                         	
fd	}t        j                  ||      }t        j$                  |      s| j                  j&                  s|g}t)        j*                  |      }|S # t        $ rR t        j                  t        
      }t        d| j                  j                   z   dz   dz   t#        |      z   dz         w xY w)
z4Serializes `Node` for Functional API's `get_config`.r   Nc                    t        | d      rj| j                  }|j                  } |j                  j                  |      }j                  |d      }|j                  j                  ||j                  gS t        | t        j                        r| j                         S t        | t        j                        r#t        j                  |       j                         S | S )z,Serializes a single Tensor passed to `call`.r8   r   )hasattrr8   r   r   namegetr   r*   npndarraytolistr+   r,   r   	get_value)r   khr   node_keynew_node_indexmake_node_keynode_conversion_maps        r   _serialize_keras_tensorz/Node.serialize.<locals>._serialize_keras_tensor   s    	$	%]]
 
;,001=~r??	Arzz	"xxz	Az((	)  #**,,hr   )defaultzLayer z- was passed non-JSON-serializable arguments. zArguments had types: z6. They cannot be serialized out when saving the model.c                 <   t        |       rl| j                  }|j                  } |j                  j                  |      }	j                  |d      }|j                  j                  ||j                  g}nt        d |       g}t        j                  |      S )Nr   )
r2   r8   r   r   r[   r\   r   r   r	   ListWrapper)
r   ra   r   rb   rc   datarf   rV   rd   re   s
         r   serialize_first_arg_tensorz2Node.serialize.<locals>.serialize_first_arg_tensor   s    		]]
 
;,001=~rG  %<Q%?H!!$''r   )r"   r#   r   _split_out_first_argdictzip_call_fn_argsupdater
   r    jsondumpsr   get_json_type	TypeErrortyper[   r4   	is_nested#_preserve_input_structure_in_configr	   convert_inner_node_data)r=   rd   re   rU   inputs	argumentskwarg_typesrl   rk   rf   rV   s    ``      @@r   	serializezNode.serialize   sY    >>4#3#3LD&::::4HFD& S11!"5t<=IVF"  7@F0
jj!9!9:( 8&ADNN4 JJ::Vd++D1DK=  0&&tV4kh0EF-. +&'*// 0 00s   %D" "AE=c                 R    | j                   r| j                  gS | j                  d   S )Nr   )r   r!   r"   rG   s    r   input_tensorszNode.input_tensors   s$    }}ll^>>!r   c                 L    | j                   r| j                  gS | j                  S r   )r   r!   rG   s    r   output_tensorszNode.output_tensors   s    }}ll^<<r   c                     t        j                  t        j                  | j                        }t        |      dk(  r| j                  s|d   S |S )Nr   r   )r
   r    r   	int_shaper   r&   r   )r=   input_shapess     r   r   zNode.input_shapes   sC    %%g&7&79K9KLL
<Admm!_r   c                 ^    t        j                  t        j                  | j                        S r   )r
   r    r   r   r   rG   s    r   output_shapeszNode.output_shapes   s!    g//1D1DEEr   c                     | j                   S r   )r   rG   s    r   outbound_layerzNode.outbound_layer   s    ::r   c                 j    | j                   rg S t        j                  d | j                  d         }|S )Nc                 .    | j                   j                  S r   )r8   r   r   s    r   r   z%Node.inbound_layers.<locals>.<lambda>  s    !2B2B2H2H r   r   )r   r
   r    r"   )r=   inbound_layerss     r   r   zNode.inbound_layers   s3    }}i''(H(,q(9;Nr   )NNN)__name__
__module____qualname____doc__rE   propertyr7   rJ   rM   rW   r}   r   r   r   r   r   r   r   r   r   r   r   $   s      	>L@    0=J  
  
   F F    r   r   c                       e Zd ZdZdZy)r:   a}  Tracks the Layer call that created a Tensor, for Keras Graph Networks.

  During construction of Keras Graph Networks, this metadata is added to
  each Tensor produced as the output of a Layer, starting with an
  `InputLayer`. This allows Keras to track how each Tensor was produced, and
  this information is later retraced by the `keras.engine.Network` class to
  reconstruct the Keras Graph Network.

  Attributes:
    layer: The Layer that produced the Tensor.
    node_index: The specific call to the Layer that produced this Tensor. Layers
      can be called multiple times in order to share weights. A new node is
      created every time a Layer is called.
    tensor_index: The output index for this Tensor. Always zero if the Layer
      that produced this Tensor only has one output. Nested structures of
      Tensors are deterministically assigned an index via `nest.flatten`.
  r   N)r   r   r   r   	__slots__r   r   r   r:   r:     s    & )r   r:   r   c                     t        | d      S )Nr8   )rZ   )r>   s    r   r2   r2   !  s    	&	''r   )r   collectionsrP   rr   numpyr]   tensorflow.python.frameworkr   r   r+   r   tensorflow.python.kerasr   tensorflow.python.keras.enginer   *tensorflow.python.keras.saving.saved_modelr   tensorflow.python.keras.utilsr	   tensorflow.python.utilr
   r   r   
namedtupler:   r2   r   r   r   <module>r      se   " !     + < 3 + ; A 2 '#a aHK>BD2(r   