o
    ?eN                     @   s   d 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 Zdd Zdd Zdd ZdS )z%Shared utilities related to backprop.    )flags)	types_pb2)dtypes)indexed_slices)ops)tensor)tensor_util)	array_ops)handle_data_util)math_opsc                    sz   | j }|jtjkr;t| tjr| j}nt	| }|dur;|j
r;|jr;|jd j   tjkr;t fdd|jD r; S |S )zEExtract either `tensor.dtype` or the unanimous sub-type of a variant.Nr   c                 3   s    | ]}|j  kV  qd S N)dtype).0shape_and_typeZ
first_type f/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/eager/backprop_util.py	<genexpr>/   s    z#_DTypeFromTensor.<locals>.<genexpr>)r   
base_dtyper   variant
isinstancer   ZEagerTensorZ_handle_datar
   Zget_resource_handle_datais_setr   r   Z
DT_INVALIDall)r   r   handle_datar   r   r   _DTypeFromTensor   s$   

r   c                 C   s|   t | r
t| }n| }t|}tjtjtjtjtj	tj
tjtjg}t j r9|tjtjtjtjtjg |j|v S )zDDetermines whether a tensor or dtype supports infinitesimal changes.)r   Z
is_tf_typer   r   Zas_dtypeZfloat16Zfloat32Zfloat64Z	complex64Z
complex128resourcer   Zbfloat16r   configZ enable_quantized_dtypes_trainingvalueextendZqint8Zqint16Zqint32Zquint8Zquint16r   )Ztensor_or_dtyper   Ztrainable_dtypesr   r   r   IsTrainable5   s   



r   c                 C   s\   t | tjsJ t | jtjr| S t | jtjsJ t| j}t|jt| j	|j	|j
S r   )r   r   IndexedSlicesvalues
tensor_libTensorFlattenNestedIndexedSlicesr	   gatherindicesdense_shape)Zgradgr   r   r   r$   E   s   
r$   c                 C   s   t | dk rdS t | dkr| d S dd | D } tdd | D r't| S t| } dd | D } ttjd	d | D dd
tjdd | D dd
| d j	}|S )z1Aggregates gradients containing `IndexedSlices`s.   Nr   c                 S   s   g | ]}|d ur|qS r   r   r   r(   r   r   r   
<listcomp>V   s    z3AggregateIndexedSlicesGradients.<locals>.<listcomp>c                 s   s    | ]	}t |tjV  qd S r   )r   r"   r#   r*   r   r   r   r   Y   s    z2AggregateIndexedSlicesGradients.<locals>.<genexpr>c                 S   s   g | ]}t |qS r   )r$   r   xr   r   r   r+   a   s    c                 S      g | ]}|j qS r   )r!   r,   r   r   r   r+   d       )Zaxisc                 S   r.   r   )r&   r,   r   r   r   r+   e   r/   )
lenanyr   Zadd_nZ_as_indexed_slices_listr   r    r	   concatr'   )ZgradsZconcat_gradr   r   r   AggregateIndexedSlicesGradientsP   s   

r3   N)__doc__Ztensorflow.core.configr   Ztensorflow.core.frameworkr   Ztensorflow.python.frameworkr   r   r   r   r"   r   Ztensorflow.python.opsr	   r
   r   r   r   r$   r3   r   r   r   r   <module>   s   