
    BVh                     n    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 Zd Zd	 Zd
 Zd Zd Zd Zy)zOptimizer utilities.    )central_storage_strategy)distribute_lib)reduce_util)clip_ops)
tf_loggingc                    t        |       } t        |       }|rt               rY|D cg c]  }|d   	 }}t        j                         j
                  j                  t        j                  j                  |      }n-t        j                         j                  t        |f      }ng }g }d}| D ]6  \  }}||j                  d|f       |j                  ||   |f       |dz  }8 |t        |      k(  sJ d       |S c c}w )zReturns all-reduced gradients aggregated via summation.

  Args:
    grads_and_vars: List of (gradient, variable) pairs.

  Returns:
    List of (gradient, variable) pairs where gradients have been all-reduced.
  r   )argsN   zFailed to add all gradients)listfilter_empty_gradientsstrategy_supports_no_merge_callr   get_strategyextended_replica_ctx_all_reduceds_reduce_utilReduceOpSUMget_replica_context
merge_call_all_reduce_sum_fnappendlen)	grads_and_varsfiltered_grads_and_varspairgradsreducedreduced_with_nonesreduced_posgvs	            Z/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/keras/optimizer_v2/utils.pyall_reduce_sum_gradientsr#      s    '.2>B&(#:;4tAw;e;++-66NN

!
!
%
%u.g 224??
$;#= @ ?g G+ dayq	*!5q 9:Qk 
G	$C&CC	$	' <s   C?c           	      v   t        |       } | s| S g }g }| D ],  \  }}||j                  |       |j                  ||f       . t        |      }|s,t        d| D cg c]  \  }}|j                   c}}d      |r.t	        j
                  d|D cg c]  }|j                   c}       |S c c}}w c c}w )zDFilter out `(grad, var)` pairs that have a gradient equal to `None`.z(No gradients provided for any variable: .zAGradients do not exist for variables %s when minimizing the loss.)tupler   
ValueErrornameloggingwarning)r   filteredvars_with_empty_gradsgradvar_r!   s          r"   r   r   ;   s    (.	(! #idC|""3'ootSk"	#
 8_(	
*89$!Qqvv9< = =OO	L/	0Q!&&	03 
/ : 
1s   B0B6
c                       d S  fd}|S )@Creates a gradient transformation function for clipping by norm.c                     | S N r   s    r"   <lambda>z+make_gradient_clipnorm_fn.<locals>.<lambda>W       .     c                     t        t        j                         t        j                  t        j
                  f      rt        d      | D cg c]  \  }}t        j                  |      |f }}}|S c c}}w )Nz:`clipnorm` is not supported with `CenteralStorageStrategy`)	
isinstancer   r   r   CentralStorageStrategyCentralStorageStrategyV1r'   r   clip_by_norm)r   r    r!   clipped_grads_and_varsclipnorms       r"   gradient_clipnorm_fnz7make_gradient_clipnorm_fn.<locals>.gradient_clipnorm_fnY   s    .--/+BB+DDFG 
FH H =K48Aq		q(	+Q/  "!s   "A5r4   r?   r@   s   ` r"   make_gradient_clipnorm_fnrB   T       00" 
r8   c                       d S  fd}|S )r1   c                     | S r3   r4   r5   s    r"   r6   z2make_global_gradient_clipnorm_fn.<locals>.<lambda>l   r7   r8   c                    t        t        j                         t        j                  t        j
                  f      rt        d      t        |  \  }}t        j                  |      \  }}t        t        ||            }|S )NzA`global_clipnorm` is not supported with `CenteralStorageStrategy`)r:   r   r   r   r;   r<   r'   zipr   clip_by_global_normr   )r   r   	variablesclipped_gradsr/   r>   r?   s         r"   r@   z>make_global_gradient_clipnorm_fn.<locals>.gradient_clipnorm_fnn   s    .--/+BB+DDFG 
MO O N+E933E8DM1!#mY"?@!!r8   r4   rA   s   ` r"    make_global_gradient_clipnorm_fnrK   i   rC   r8   c                       d S  fd}|S )zACreates a gradient transformation function for clipping by value.c                     | S r3   r4   r5   s    r"   r6   z,make_gradient_clipvalue_fn.<locals>.<lambda>   r7   r8   c           	          t        t        j                         t        j                  t        j
                  f      rt        d      | D cg c]  \  }}t        j                  |       |f! }}}|S c c}}w )Nz;`clipvalue` is not supported with `CenteralStorageStrategy`)	r:   r   r   r   r;   r<   r'   r   clip_by_value)r   r    r!   r>   	clipvalues       r"   gradient_clipvalue_fnz9make_gradient_clipvalue_fn.<locals>.gradient_clipvalue_fn   s    .--/+BB+DDFG 
GI I
 +9:"&!Q  (55a)6? ABCE : : "!:s   $A7r4   )rP   rQ   s   ` r"   make_gradient_clipvalue_fnrR   ~   s    00" 
r8   c                 j    | j                   j                  t        j                  j                  |      S r3   )r   batch_reduce_tor   r   r   )distributionr   s     r"   r   r      s,    				.	.~/F/F/J/J/=
? ?r8   c                      t        j                         syt        j                         } | j                  j	                          S )zDReturns if the current Strategy can operate in pure replica context.T)r   has_strategyr   r   _use_merge_call)strategys    r"   r   r      s7    		$	$	&((*(..0	00r8   N)__doc__tensorflow.python.distributer   r   r   r   tensorflow.python.opsr   tensorflow.python.platformr   r)   r#   r   rB   rK   rR   r   r   r4   r8   r"   <module>r^      s=     A 7 F * < F2***?
1r8   