
    ijZA                         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 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e             Zy)    N)backend)dtype_policies)tree)keras_export)KerasTensor)remat)any_symbolic_tensors)is_nnx_enabled)Node)KerasSaveable)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
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d Z xZS )	Operationc                     |t        | j                  j                        }t        |t              rd|v rt        d| dt        |       d      || _        g | _        g | _	        t        j                         | _        y )N/zRArgument `name` must be a string and cannot contain character `/`. Received: name=z
 (of type ))r   	__class____name__
isinstancestr
ValueErrortypename_inbound_nodes_outbound_nodesr   get_current_remat_mode_remat_mode)selfr   s     l/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/keras/src/ops/operation.py__init__zOperation.__init__   s~    <T^^445D$$t""&z$t*Q@ 
 	 ! 779    c                 2   t        j                         rt        ||      r| j                  }n| j                  rOt        | 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 | j                  rYt        | dd       &  | j                  | j                  g|i ||i |S   | j                  | j                  g|i ||i |S t        | dd        | j                  |i |S  | j                  |i |S )Nquantization_modez.call())object_name)r   is_traceback_filtering_enabledr	   symbolic_callr   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,,##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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/   r6   s       r!   r(   zOperation.symbolic_callS   s3    *$**D;F; 	d	
 r#   c                     t         NNotImplementedErrorr    r.   r/   s      r!   r,   zOperation.calla       !!r#   c                     t         r9   r:   r<   s      r!   r+   zOperation.quantized_calld   r=   r#   c                    | j                   s|S d }| j                   j                  dk(  rt        j                  |      S | j                   j                  dk(  rt | j                  |i |}t	        t        j                  t        j                  ||                  }|r.|| j                   j                  kD  rt        j                  |      S |S )zEnable rematerialization dynamically for an operation's call method.

        Args:
            fn: The original `call` or `quantized_call` method of an operation.

        Returns:
            Rematerialized method.
        c                     t        | t              r1t        j                  | j                  D cg c]  }|xs d
 c}      S dS c c}w )N   r   )r   r   mathprodshape)xds     r!   compute_sizez3Operation.rematerialized_call.<locals>.compute_sizes   sC     a- 		1773a16634 3s   Afulllarger_than)	r   moder   r7   sumr   flattenmap_structureoutput_size_threshold)r    fnr.   r/   rG   output_specoutput_sizes          r!   r*   zOperation.rematerialized_callg   s     I	   F*;;r?" ""m32$22DCFCKT//kJKK $"2"2"H"HH{{2&	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   r7   r,   	Exceptionr   r   r   with_traceback__traceback__)r    r.   r/   enew_es        r!   r7   zOperation.compute_output_spec   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                         dk(  r7t	               r-ddlm} 	 |j                  j                         t        |      d<   d}t        j                  | j                        }t        j                   | j                        }	  |j"                  dg|i |}|rvt'        |j(                  j+                         D 	
cg c]>  \  }	}
|	|j,                  d   k7  r'|
j.                  t        j0                  j2                  k(  @ c}
}	      r	d}d|_        |rj7                          |j8                  }||j;                  |j<                  i       z  }|j;                  |j,                  d          |j;                  d	d       |j>                  2|j;                  |j>                        }tA        |      dkD  r	d}d|_        tB        tD        tF        tH        tK        d      f}	 tM        jN                  |      }|D ]  }tQ        ||      rd} n 	 d|_)        |rdd
l*m+}  |jX                  di ||_-        nd|_-        d|_)        |S # t        $ r* |j                  j                         t        |      d<   Y 1w xY w# t$        $ r d}Y w xY wc c}
}	w # t$        $ r d}Y w 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()`.
        jaxr   )nnx_pytree__state_object__stateTNFr   )serialization_lib )/superr   __new__r   r
   flaxrZ   	pytreelibPytreeStatevarsAttributeErrorobjectObjectStateinspect	signaturer"   getfullargspecbind	TypeErrorany
parametersitemsr.   kind	ParameterPOSITIONAL_ONLY_auto_config_error_argsapply_defaults	argumentspopvarkwvarargslenr   intfloatboolr   r   rL   r   _lockkeras.src.savingr]   SerializableDict_auto_configRecursionError)clsr.   r/   instancerZ   auto_configri   argspecbound_parametersr   paramrx   supported_typesflat_arg_valuesvaluer]   r   s                   r!   r`   zOperation.__new__   s    C05??%.*: L36==3L3L3NX/0
 %%cll3	((6	 -y~~dDTDVD 3 $-#7#7#=#=#?D%7<<?* 

g//???
  K/3H,++-%//Ffjj33FJJw||A'JJvt$* **W__5w<!#"'K7;H4 UD$t*=	 "ll62O( !%9"'K	"HN>(J(9(J(J ))% )-%!HN W " L36::3I3I3KX/0L  	   K	 P  	 K	   	 	sT   &I" $J AJ*
>&J0 %J0 +5K "/JJJ'&J'0J>=J>	KKc                    d| j                   i}t        j                  | j                        s|S t	        | dd      |j                  | j                  j                         t        j                  | j                        j                  }d|v }d|v xr* |d   j                  t        j                  j                  k(  }|s|s|j                  dd       |S d}t	        | dd      r8t!        t#        j$                  d| j&                  j(                   d	|             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   r   Nr/   a  
            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
            rs   Fz
            Object a   was created by passing
            positional only or variadic positional arguments (e.g.,
            `*args`) to `__init__()`, which is not supported by the
            automatic config generation. Please remove all positional
            only and variadic arguments from `__init__()`
            or override `get_config()` and `from_config()` to make
            the object serializatble.

            Example:

            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:

            )r   r   
is_default
get_configr)   updater   configrh   ri   r"   rn   rp   rq   VAR_KEYWORDrv   r;   textwrapdedentr   r   )r    r   init_paramsinit_has_nameinit_has_kwargsexample_strs         r!   r   zOperation.get_config   s\    DII
 &&t7M 4.:MM$++223!++DMM:EEK"k1MK' P)..'2C2C2O2OO  !

64(MK t6>)OONN++, 
- M " *OONN++, - 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.
        dtypeNz Error when deserializing class 'z' using config=z.

Exception encountered: r^   )r   dictcopyr   deserializeDTypePolicyMapr%   r   rS   rl   r   )r   r   policyrV   s       r!   from_configzOperation.from_configB  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__l  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_indexr   s    r!   r   zOperation.inputo  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   r   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.rA   r   )r   re   r   ry   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)Nr   r^   r   s    r!   	_obj_typezOperation._obj_type  s    r#   c                      y)5Can be overridden for per backend post build actions.Nr^   r   s    r!   _post_buildzOperation._post_build      r#   c                 
    ||fS )r   r^   )r    r   r   s      r!   _setattr_hookzOperation._setattr_hook  s    U{r#   c                      y)z5Can be overridden for per backend post track actions.Nr^   r    variables     r!   _post_track_variablezOperation._post_track_variable  r   r#   c                      y)z7Can be overridden for per backend post untrack actions.Nr^   r   s     r!   _post_untrack_variablez Operation._post_untrack_variable  r   r#   r9   )r   
__module____qualname__r"   r   filter_tracebackr1   r(   r,   r+   r*   r7   r`   r   defaultr   classmethodr   r   propertyr   r   r   r   r   r   r   r   __classcell__)r   s   @r!   r   r      s    : %%,2 &,2\"""HBYv J JX ' 'R/ 	N 	N 	P 	PBr#   r   )rh   rB   r   	keras.srcr   r   r   keras.src.api_exportr   keras.src.backendr   keras.src.backend.commonr   %keras.src.backend.common.keras_tensorr	   keras.src.backend.configr
   keras.src.ops.noder   keras.src.saving.keras_saveabler   keras.src.utilsr   r   keras.src.utils.namingr   r   r^   r#   r!   <module>r      sY        $  - ) * F 3 # 9 ( + ,  e e !er#   