o
    2h                     @   s|   d Z ddlZddlZddlmZ zddlZW n	 ey   Y nw dd Zdd ZG dd	 d	e	Z
G d
d de	Zdd ZdS )z/Utilities related to layer/model functionality.    N)object_identityc                 C   s   t | do
t| t S )&Implicit check for Layer-like objects.	_is_layer)hasattr
isinstancetype)obj r	   b/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/trackable/layer_utils.pyis_layer    s   r   c                 C   s,   t t| dot t| d}|ot| t S )r   trainable_weightsnon_trainable_weights)r   r   r   )r   
has_weightr	   r	   r
   has_weights&   s   r   c                   @   s(   e Zd ZdZdZdd Zedd ZdS )MutationSentinelz?Container for tracking whether a property is in a cached state.Fc                 C   s   || j k}|| _ |S N_in_cached_state)selfvaluemay_affect_upstreamr	   r	   r
   mark_as3   s   
zMutationSentinel.mark_asc                 C   s   | j S r   r   r   r	   r	   r
   in_cached_state8   s   z MutationSentinel.in_cached_stateN)__name__
__module____qualname____doc__r   r   propertyr   r	   r	   r	   r
   r   /   s    r   c                       sZ   e Zd ZdZdddZ fddZdd Zd	d
 Zdd Zdd Z	dd Z
dd Z  ZS )AttributeSentinelzContainer for managing attribute cache state within a Layer.

  The cache can be invalidated either on an individual basis (for instance when
  an attribute is mutated) or a layer-wide basis (such as when a new dependency
  is added).
  Fc                 C   s    t  | _tt| _|| _d S r   )weakrefWeakSet_parentscollectionsdefaultdictr   
attributesalways_propagate)r   r&   r	   r	   r
   __init__E   s   

zAttributeSentinel.__init__c                    s&   d tt|  dd | j D S )Nz{}
  {}c                 S   s   i | ]\}}||j qS r	   )r   ).0kvr	   r	   r
   
<dictcomp>R   s    z.AttributeSentinel.__repr__.<locals>.<dictcomp>)formatsuperr   __repr__r%   itemsr   	__class__r	   r
   r.   O   s   zAttributeSentinel.__repr__c                 C   s   | j | |  d S r   )r"   addinvalidate_all)r   noder	   r	   r
   
add_parentT   s   zAttributeSentinel.add_parentc                 C   s   | j | jS r   )r%   r   r   keyr	   r	   r
   get_   s   zAttributeSentinel.getc                 C   s8   | j | |}|s| jr| jD ]	}|| qd S d S r   )r%   r   r&   r"   
invalidate)r   r7   r   r   r4   r	   r	   r
   _setc   s   

zAttributeSentinel._setc                 C      |  |d d S )NTr:   r6   r	   r	   r
   mark_cachedj      zAttributeSentinel.mark_cachedc                 C   r;   NFr<   r6   r	   r	   r
   r9   n   r>   zAttributeSentinel.invalidatec                 C   s8   | j  D ]
}| j | d q| jD ]}|  qd S r?   )r%   keysr   r"   r3   )r   r7   r4   r	   r	   r
   r3   r   s
   

z AttributeSentinel.invalidate_all)F)r   r   r   r   r'   r.   r5   r8   r:   r=   r9   r3   __classcell__r	   r	   r0   r
   r   =   s    

r   c                 c   sx    t  }| ddd }|r:| }||v rq|| t|r$|V  nt|ddp+g }||ddd  |sdS dS )z4Filter out empty Layer-like containers and uniquify.Nlayers)r   ObjectIdentitySetpopr2   r   getattrextend)
layer_listexistingto_visitr   
sub_layersr	   r	   r
   filter_empty_layer_containers|   s   
rL   )r   r#   r    tensorflow.python.utilr   typingImportErrorr   r   objectr   r   rL   r	   r	   r	   r
   <module>   s   	?