
    2Vhn2                         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 d
lmZ  ed       G d d             Zy)    N)backend)dtype_policies)tree)keras_export)any_symbolic_tensors)Node)python_utils)traceback_utils)	auto_namezkeras.Operationc                        e Zd ZddZej
                  d        Zd Zd Zd Z	d Z
 fdZej                  d        Zed	        Zd
 Zed        Zed        Zd Zd Zd Zd Zd Z xZS )	Operationc                    |t        | j                  j                        }t        |t              rd|v rt        d| dt        |       d      t        j                  |      | _	        || _
        g | _        g | _        y )N/zRArgument `name` must be a string and cannot contain character `/`. Received: name=z
 (of type ))r   	__class____name__
isinstancestr
ValueErrortyper   get_dtype_policyname_inbound_nodes_outbound_nodes)selfdtyper   s      G/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/ops/operation.py__init__zOperation.__init__   s    <T^^445D$$t""&z$t*Q@ 
 ,//6	 !    c                 6   t        j                         rt        ||      r| j                  }nt	        | dd       Ot	        | dd       ! | j
                  | j                  g|i |}nG | j
                  | j                  g|i |}n&t	        | dd       | j                  }n| j                  }t        j                  || j                  j                   d      } ||i |S t        ||      r | j                  |i |S t	        | dd       Yt	        | dd       &  | j
                  | j                  g|i ||i |S   | j
                  | j                  g|i ||i |S t	        | dd        | j                  |i |S  | j                  |i |S )N_remat_modequantization_modez.call())object_name)r
   is_traceback_filtering_enabledr   symbolic_callgetattrrematerialized_callquantized_callcall!inject_argument_info_in_tracebackr   r   )r   argskwargscall_fns       r   __call__zOperation.__call__   s   99;#D&1,,45At%8$?K":$":": //#!# %# #;$":": II#(,#06# t%8$?K"&"5"5"&))%GG $ 7 78@G D+F++  f-%4%%t6v66T=$/;t0$7C/t//''*.28#!# # L/t//		KDKFK#  t0$7C*t**D;F;; tyy$1&11r    c                 H     | j                   |i |}t        | |||       |S )N)	operation	call_argscall_kwargsoutputs)compute_output_specr   )r   r,   r-   r4   s       r   r&   zOperation.symbolic_callN   s3    *$**D;F; 	d	
 r    c                     t         NNotImplementedErrorr   r,   r-   s      r   r*   zOperation.call\       !!r    c                     t         r7   r8   r:   s      r   r)   zOperation.quantized_call_   r;   r    c                 p   	 t        j                  | j                  g|i |S # t        $ r}|j	                  d| j
                   d| j                  j                   d| j                  j                   d| j                  j                   d| 
      }|j                  |j                        d d }~ww xY w)Nz;Could not automatically infer the output shape / dtype of 'z' (of type z). Either the `z<.call()` method is incorrect, or you need to implement the `zM.compute_output_spec() / compute_output_shape()` method. Error encountered:

)	r   r5   r*   	Exceptionr   r   r   with_traceback__traceback__)r   r,   r-   enew_es        r   r5   zOperation.compute_output_specb   s    	B..tyyJ4J6JJ 
	BKKII;k$..*A*A)B C#~~667 8NN++, -))*-E &&q7TA
	Bs   "% 	B5BB00B5c                    t         t        |   |       }t        j                  | j
                        j                  }|j                  t        t        |dt        |      dz    |                   |j                  dd      }|Nt        |t        j                        r4|j                  |j                   |d<   nt        j"                  |      |d<   t$        t&        t(        t*        t-        d      f}	 t/        j0                  |      }d}|D ]  }	t        |	|      rd} n 	 d|_        |rddlm}
  |
j:                  di ||_        nd|_        d|_        |S # t2        $ r d}Y Ew xY w# t>        $ r Y |S w xY w)	aJ  We override __new__ to saving serializable constructor arguments.

        These arguments are used to auto-generate an object serialization
        config, which enables user-created subclasses to be serializable
        out of the box in most cases without forcing the user
        to manually implement `get_config()`.
           r   NTFr   )serialization_lib ) superr   __new__inspectgetfullargspecr   r,   updatedictziplenr   r   r   DTypePolicyr#   r   	serializer   intfloatboolr   r   flatten	TypeError_lockkeras.src.savingrE   SerializableDict_auto_configRecursionError)clsr,   r-   instance	arg_namesr   supported_typesflat_arg_valuesauto_configvaluerE   r   s              r   rH   zOperation.__new__q   sx    C05 **3<<8==	d3ySY];TBCD 

7D)E>3M3M!N &&."'**w #1":":5"Aw UD$t*=	 "ll62OK( !%9"'K	"HN>(J(9(J(J ))% )-%!HN %  	 K	   	 	s*   4(E E #5E+ E('E(+	E87E8c                 2   d| j                   i}t        j                  | j                        s|S t	        | dd      t        |j                               }|j                  | j                  j                         t        j                  | j                        }|j                  dk7  r8||j                  |j                  dd       z
  D ]  }|j!                  |d        |S t#        t%        j&                  d| j(                  j*                   d            )zReturns the config of the object.

        An object config is a Python dictionary (serializable)
        containing the information needed to re-instantiate it.
        r   rY   Nr-   rD   z
        Object a   was created by passing
        non-serializable argument values in `__init__()`,
        and therefore the object must override `get_config()` in
        order to be serializable. Please implement `get_config()`.

        Example:

        class CustomLayer(keras.layers.Layer):
            def __init__(self, arg1, arg2, **kwargs):
                super().__init__(**kwargs)
                self.arg1 = arg1
                self.arg2 = arg2

            def get_config(self):
                config = super().get_config()
                config.update({
                    "arg1": self.arg1,
                    "arg2": self.arg2,
                })
                return config)r   r	   
is_default
get_configr'   setkeysrK   rY   configrI   rJ   r   varkwintersectionr,   popr9   textwrapdedentr   r   )r   rg   	xtra_argsargspeckeys        r   rd   zOperation.get_config   s     DII
 &&t7M 4.:FKKM*IMM$++223,,T]];G}}($y'='=gll12>N'OO *CJJsD)*M%''( )! r    c           	      ^   d|v rrt        |d   t              r_|j                         }t        j                  |d         }t        |t        j
                        s|j                  |j                  }||d<   	  | di |S # t        $ r#}t        d| j                   d| d|       d}~ww xY w)a  Creates an operation from its config.

        This method is the reverse of `get_config`, capable of instantiating the
        same operation from the config dictionary.

        Note: If you override this method, you might receive a serialized dtype
        config, which is a `dict`. You can deserialize it as follows:

        ```python
        if "dtype" in config and isinstance(config["dtype"], dict):
            policy = dtype_policies.deserialize(config["dtype"])
        ```

        Args:
            config: A Python dictionary, typically the output of `get_config`.

        Returns:
            An operation instance.
        r   Nz Error when deserializing class 'z' using config=z.

Exception encountered: rF   )r   rL   copyr   deserializeDTypePolicyMapr#   r   r>   rU   r   )r[   rg   policyrA   s       r   from_configzOperation.from_config   s    . fF7OT!B[[]F#//w@Fv~'D'DE,,4  $F7O	==  	23<<. A !=aSB 	s   8B   	B,	B''B,c                 "    d| j                    dS )Nz<Operation name=>)r   r   s    r   __repr__zOperation.__repr__  s    !$))A..r    c                 (    | j                  ddd      S )zRetrieves the input tensor(s) of a symbolic operation.

        Only returns the tensor(s) corresponding to the *first time*
        the operation was called.

        Returns:
            Input tensor or list of input tensors.
        r   input_tensorsinput_get_node_attribute_at_indexrx   s    r   r|   zOperation.input
  s     00OWMMr    c                 (    | j                  ddd      S )zRetrieves the output tensor(s) of a layer.

        Only returns the tensor(s) corresponding to the *first time*
        the operation was called.

        Returns:
            Output tensor or list of output tensors.
        r   output_tensorsoutputr}   rx   s    r   r   zOperation.output  s     004DhOOr    c           
      N   | j                   st        d| j                   d| d      t        | j                         |kD  s(t	        d| d| dt        | j                          d      t        | j                   |   |      }t        |t              rt        |      dk(  r|d	   S |S )
a  Private utility to retrieves an attribute (e.g. inputs) from a node.

        This is used to implement the properties:
        - output
        - input

        Args:
            node_index: Integer index of the node from which
                to retrieve the attribute.
            attr: Exact node attribute name.
            attr_name: Human-readable attribute name, for error messages.

        Returns:
            The operation's attribute `attr` at the node of index `node_index`.
        z
The layer z/ has never been called and thus has no defined .zAsked to get z	 at node z, but the operation has only z inbound nodes.rD   r   )r   AttributeErrorr   rN   r   r'   r   list)r   
node_indexattr	attr_namevaluess        r   r~   z&Operation._get_node_attribute_at_index"  s      "" TYYK (++4+Q8  4&&'*4	{),;t**+,O= 
 ,,Z8$?fd#Fq(8!9Mr    c                      y)5Can be overridden for per backend post build actions.NrF   rx   s    r   _post_buildzOperation._post_buildD      r    c                 
    ||fS )r   rF   )r   r   ra   s      r   _setattr_hookzOperation._setattr_hookH  s    U{r    c                      y)z5Can be overridden for per backend post track actions.NrF   r   variables     r   _post_track_variablezOperation._post_track_variableL  r   r    c                      y)z7Can be overridden for per backend post untrack actions.NrF   r   s     r   _post_untrack_variablez Operation._post_untrack_variableP  r   r    )NN)r   
__module____qualname__r   r
   filter_tracebackr/   r&   r*   r)   r5   rH   r	   defaultrd   classmethodru   ry   propertyr|   r   r~   r   r   r   r   __classcell__)r   s   @r   r   r      s    " %%,2 &,2\""B5n 2 2h ' 'R/ 	N 	N 	P 	PDr    r   )rI   rk   	keras.srcr   r   r   keras.src.api_exportr   %keras.src.backend.common.keras_tensorr   keras.src.ops.noder   keras.src.utilsr	   r
   keras.src.utils.namingr   r   rF   r    r   <module>r      sH       $  - F # ( + ,  B B !Br    