
    ~Wh5              	           d Z ddlZddlZddlZddlZddlmc 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d	iZ G d
 d          Z G d d ej        dg d                    Zd ZdS )zContains the `Node` class.    N)backend)base_layer_utils)
json_utils)tf_utils_CONSTANT_VALUE_TYPE	COMPOSITEc                       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dS )Nodea  A `Node` describes a layer `__call__()` event.

    A Functional model is a DAG with `Node` instances as nodes, and
    `KerasTensor` instances as edges. Nodes aren't `Layer` instances, because a
    single layer could be called multiple times, which would result in graph
    cycles.

    A `__call__()` event involves input tensors (and other input arguments),
    the layer that was called, and the resulting output tensors.
    A `Node` will include all this information.

    Since a single `Layer` could be called multiple times, the `Node` instances
    are stored on layers as a list. Each time a layer is called 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`.

    Every `KerasTensor` instance has a `KerasHistory` object attached,
    which tracks the `Node` that records the `__call__()` event that created
    the tensor. By recursively walking through `Node` instances
    via the `KerasHistory` metadata of `KerasTensor` instances, once can
    retrieve the entire DAG of a Functional model.

    Args:
        layer: The layer that was called in the `Layer.__call__()`
          event that 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 output tensors of the `Layer.__call__()`
    Nc                    |g n|}|i n|}|g n|}|| _         | o| | _        t          j                            d |          }t          j                            d |          }t          j                            d |          | _        || _        || _        t          j                            | j        | j        f          | _	        | j         o6t          | j                  dk    ot          j        | j        d                   | _        t          j        j                                        sN| j	        D ]F}t!          |t          j                  r*t%          j        |d          rt%          j        |           Gg | _        g | _        t/          | j	                  D ]h\  }}t1          |          rT| j                            |           t5          t7          |                    }|}	| j                            ||	f           i| j         j                            |            | j        D ]*}
|
j        j         }||j                            |            +t          | j         j                  dz
  }t/          t          j                            |                    D ]\  }}tA          |||          |_        d	 | j        D             | _!        d
 t          j                            | j                  D             | _"        d S )Nc                     | S N ts    W/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/keras/engine/node.py<lambda>zNode.__init__.<locals>.<lambda>Q   s    A     c                     | S r   r   r   s    r   r   zNode.__init__.<locals>.<lambda>R   s    a r   c                     | S r   r   r   s    r   r   zNode.__init__.<locals>.<lambda>S   s    q r      r   T)ignore_call_contextlayer
node_indextensor_indexc                 F    g | ]}t          t          |                    S r   strid.0r   s     r   
<listcomp>z!Node.__init__.<locals>.<listcomp>   s$    FFFas2a55zzFFFr   c                 F    g | ]}t          t          |                    S r   r   r!   s     r   r#   z!Node.__init__.<locals>.<listcomp>   s1      
  
  
C1JJ 
  
  
r   )#r   is_inputtfnestmap_structureoutputs	call_argscall_kwargsflatten_flat_argumentslen	is_tensor _single_positional_tensor_passedcompatv1#executing_eagerly_outside_functions
isinstanceTensorr   needs_keras_historycreate_keras_history_keras_inputs_keras_inputs_ids_and_indices	enumerateis_keras_tensorappendr   r    _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   tensors                 r   __init__zNode.__init__C   s   #+BB	'/bb[""W
%9k/ G))++yAA	g++KKEEw,,[['BB"&  "w^T-. 
  

    0DN##q(0T^A.// 	- y|??AA 	?+ ? ?  ?&:T  ?
 %9#>>>-/* 455 	M 	MFAss## M"))#...BsGG2995(:KLLL 	
!((...# 	; 	;B-3M(-44T::: 233a7
"27??7#;#;<< 	 	IAv$0
% % %F!!
 GF43EFFF 
  
 " = = 
  
  
r   c                     | j         S )zNTensors input to this node that can be traced back to a
        `keras.Input`.)r8   rD   s    r   r>   zNode.keras_inputs   s     !!r   c                     g }| j         D ]<}|j        j        }|j        j        }| |                    |j        |                    =|S )zNReturns all the `Node`s whose output this node immediately depends
        on.)r>   r?   r   r   r<   r=   )rD   	node_depsrJ   r   r   s        r   parent_nodeszNode.parent_nodes   s\     	# 	C 	CB%+E*5J   !5j!ABBBr   c              #   f   K   | j         D ]&}|j        }|j        }|j        }|j        }||||fV  'dS )zYields tuples representing the data inbound from other nodes.

        Yields:
          tuples like: (inbound_layer, node_index, tensor_index, tensor).
        N)r>   r?   r   r   r   )rD   rJ   keras_historyr   r   r   s         r   iterate_inboundzNode.iterate_inbound   s\       # 	6 	6B-M!'E&1J(5L\255555	6 	6r   c                 V   | j         r-| j        d         \  }}||                                         fi fS t          j        | j                  }| j        D ]"\  }}||                                         ||<   #t
          j                            | j        | j	        f|          \  }}||fS )z;Maps Keras Tensors to computed Tensors using `tensor_dict`.r   )
r0   r9   popcopyr-   r&   r'   pack_sequence_asr*   r+   )rD   tensor_dictrH   _flat_argumentsrI   argskwargss           r   map_argumentszNode.map_arguments   s    0 	 9!<HE1&**,,.22!Yt';<<N#'#E D Dx+6u+=+A+A+C+Cx((733!12N LD& <r   c                 >  	
 | j         | j        c}
| j        j                            |
          \  }}
t          t          | j        j        j        dd         |                    }|                    
           |
fd	t          j
                            	
          
	 t          j        
t          j                   nh# t           $ r[ t          j
                            t"          
          }t!          d| j        j        z   dz   dz   t'          |          z   dz             w xY w	
fd	}t          j
                            ||          }t          j
                            |          s| j        j        s|g}t-          j        |          }|S )
z4Serializes `Node` for Functional API's `get_config`.r   Nc                 8   t          | d          rN| j        }|j        } |j        j        |          }                    |d          }|j        j        ||j        gS t          | t          j	                  r| 
                                S t          | t          j                  r&t          j        |           
                                S t          | t          j        j                  r-t"          t%          j                                        |           fS | S )z,Serializes a single Tensor passed to `call`.r?   r   )hasattrr?   r   r   namegetr   r4   npndarraytolistr&   r5   r   	get_value__internal__CompositeTensor_COMPOSITE_TYPEr   Encoderencode)r   khr   node_keynew_node_indexmake_node_keynode_conversion_maps        r   _serialize_keras_tensorz/Node.serialize.<locals>._serialize_keras_tensor   s    q*++ H%]
(=
CC!4!8!81!E!E~rGG!RZ(( "xxzz!!RY'' 5(++22444 !R_<== I');)=)=)D)DQ)G)GHH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          |           rP| j        }|j        } |j        j        |          }	                    |d          }|j        j        ||j        g}nt          d |           g}t          j	        |          S )Nr   )
r;   r?   r   r   rc   rd   r   r   r   ListWrapper)
r   rn   r   ro   rp   datars   r^   rq   rr   s
         r   serialize_first_arg_tensorz2Node.serialize.<locals>.serialize_first_arg_tensor   s    q!! Q%]
(=
CC!4!8!81!E!E~rO (-D-DQ-G-GP'---r   )r*   r+   r   
_call_specsplit_out_first_argdictzip	arg_namesupdater&   r'   r(   jsondumpsr   get_json_type	TypeErrortyperc   r   	is_nested#_preserve_input_structure_in_configr   convert_inner_node_data)rD   rq   rr   r]   inputs	argumentskwarg_typesry   rx   rs   r^   s    ``      @@r   	serializezNode.serialize   s    ~t'7f#z4HH& 
  
f
 TZ2<QRR@$GGHH	   	 	 	 	 	 	, &&'>GG	Jvz'?@@@@@ 		 		 		'//f==K*/"AB ** k""	#
 KK  			. 	. 	. 	. 	. 	. 	. 	.  w$$%?HH!!$''	JB	 6D/55s   * C A%D0c                 :    | j         r| j        gS | j        d         S )Nr   )r%   r)   r*   rO   s    r   input_tensorszNode.input_tensors
  s#    = 	"L>!~a  r   c                 .    | j         r| j        gS | j        S r   )r%   r)   rO   s    r   output_tensorszNode.output_tensors  s    = 	"L>!|r   c                     t           j                            t          j        | j                  }t          |          dk    r| j        s|d         S |S )Nr   r   )r&   r'   r(   r   	int_shaper   r.   r%   )rD   input_shapess     r   r   zNode.input_shapes  sP    w,,t1
 
 |!!$-!?"r   c                 `    t           j                            t          j        | j                  S r   )r&   r'   r(   r   r   r   rO   s    r   output_shapeszNode.output_shapes  s     w$$W%68KLLLr   c                     | j         S r   )r   rO   s    r   outbound_layerzNode.outbound_layer#  s
    zr   c                     | j         rg S d | j        D             }t          j                            d |          }t          |          dk    r|d         S |S )z2Return all layers that feed into the current node.c                 Z    g | ](}t          j        |          t          |d           &|)S )r?   )r&   r/   rb   )r"   xs     r   r#   z'Node.inbound_layers.<locals>.<listcomp>,  sK     
 
 
|A
 $+1.>#?#?

 
 
r   c                     | j         j        S r   )r?   r   r   s    r   r   z%Node.inbound_layers.<locals>.<lambda>2  s    a&, r   r   r   )r%   r-   r&   r'   r(   r.   )rD   tensor_call_argsinbound_layerss      r   r   zNode.inbound_layers'  s}     = 	I
 
)
 
 

 ..,,.>
 
 ~!##!!$$r   )NNN)__name__
__module____qualname____doc__rM   propertyr>   rR   rU   r_   r   r   r   r   r   r   r   r   r   r   r   r   $   sC        <D
 D
 D
 D
L " " X"
 	 	 X	6 6 6      J J Jd ! ! X!
   X
   X M M XM   X   X  r   r   c                       e Zd ZdZdZdS )rA   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. The corresponding node
        that represents the call event that produced the Tensor can be found at
        `layer._inbound_nodes[node_index]`.
      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   rA   rA   9  s        
 , IIIr   rA   r   c                 "    t          | d          S )Nr?   )rb   )rE   s    r   r;   r;   W  s    3()))r   )r   collectionsrX   r   numpyre   tensorflow.compat.v2r1   v2r&   kerasr   keras.enginer   keras.saving.legacy.saved_modelr   keras.utilsr   r   rk   r   
namedtuplerA   r;   r   r   r   <module>r      s9  " !             ! ! ! ! ! ! ! ! !       ) ) ) ) ) ) 6 6 6 6 6 6            #K(R R R R R R R Rj    K???   <* * * * *r   