o
    ?e                     @   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.Z	_is_layer)hasattr
isinstancetype)obj r   h/home/www/facesmatcher.com/pyenv/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   Ztrainable_weightsZnon_trainable_weights)r   r   r   )r   Z
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   r3   r   r   r0   r   r   r	   _setc   s   

zAttributeSentinel._setc                 C      |  |d d S )NTr6   r2   r   r   r	   mark_cachedj      zAttributeSentinel.mark_cachedc                 C   r7   NFr8   r2   r   r   r	   r5   n   r:   zAttributeSentinel.invalidatec                 C   s8   | j  D ]
}| j | d q| jD ]}|  qd S r;   )r!   keysr   r   r/   )r   r3   r0   r   r   r	   r/   r   s
   

z AttributeSentinel.invalidate_all)F)r   r   r   r   r#   r*   r1   r4   r6   r9   r5   r/   __classcell__r   r   r,   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.NZlayers)r   ZObjectIdentitySetpopr.   r
   getattrextend)Z
layer_listexistingZto_visitr   Z
sub_layersr   r   r	   filter_empty_layer_containers|   s   
rC   )r   r   r   Ztensorflow.python.utilr   typingImportErrorr
   r   objectr   r   rC   r   r   r   r	   <module>   s   	?