
    |h                     J    d Z ddlmc mZ ddlmZ d Zd Z	d Z
d Zd Zd	 Zy)
zOptimizer utilities.    N)
tf_loggingc                 l   t        |       } t        |       }|rt        j                  j                  j                         rc|D cg c]  }|d   	 }}t        j                  j                         j                  t        j                  j                  j                  |      }n7t        j                  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tf__internal__
distributestrategy_supports_no_merge_callget_replica_context
all_reduceReduceOpSUM
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	            \/var/www/html/test/engine/venv/lib/python3.12/site-packages/tf_keras/src/optimizers/utils.pyall_reduce_sum_gradientsr      s-    .)N4^D??%%EEG)@AT!WAEAmm779DD&&**EG
 mm779DD"*A)C E G K 19%%tQi0%%w{';Q&?@1K #g,&E(EE&+ Bs   D1c                    t        |       } | s| S g }g }| D ],  \  }}||j                  |       |j                  ||f       . t        |      }|s2| D cg c]  \  }}|j                   c}}f}t        d| d|  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: z. Provided `grads_and_vars` is .zGradients do not exist for variables %s when minimizing the loss. If you're using `model.compile()`, did you forget to provide a `loss` argument?)tupler   name
ValueErrorloggingwarning)r   filteredvars_with_empty_gradsgradvar_r   variables           r   r   r   <   s    >*NH# )	c<!((-OOT3K(	)
 XH(671QVV796xj A,,:+;1>
 	
 ) 44aff4		
 O 8 5s   B6B<
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>^       n     c                    t        t        j                  j                         t        j                  j                  j
                  t        j                  j                  j                  j                  j
                  f      r+t        dt        j                  j                          d      | D cg c]  \  }}t        j                  |      |f }}}|S c c}}w )NzQ`clipnorm` is not supported with `CenteralStorageStrategy`. The strategy used is r!   )

isinstancer	   r   get_strategyexperimentalCentralStorageStrategycompatv1r$   clip_by_norm)r   r   r   clipped_grads_and_varsclipnorms       r   gradient_clipnorm_fnz7make_gradient_clipnorm_fn.<locals>.gradient_clipnorm_fn`   s    MM&&(**AA		''44KK
 ((*(B(B(D'EQH  ;I"
26!QR__Q)1-"
 "
 &%"
s   4"Cr1   r?   r@   s   ` r   make_gradient_clipnorm_fnrB   [   s    44&$  r5   c                       d S  fd}|S )r.   c                     | S r0   r1   r2   s    r   r3   z2make_global_gradient_clipnorm_fn.<locals>.<lambda>x   r4   r5   c                    t        t        j                  j                         t        j                  j                  j
                  t        j                  j                  j                  j                  j
                  f      r+t        dt        j                  j                          d      t        |  \  }}t        j                  |      \  }}t        t        ||            }|S )NzX`global_clipnorm` is not supported with `CenteralStorageStrategy`. The strategy used is r!   )r7   r	   r   r8   r9   r:   r;   r<   r$   zipclip_by_global_normr   )r   r   	variablesclipped_gradsr+   r>   r?   s         r   r@   z>make_global_gradient_clipnorm_fn.<locals>.gradient_clipnorm_fnz   s    MM&&(**AA		''44KK
 ((*(B(B(D'EQH  /y11%Bq!%c-&C!D%%r5   r1   rA   s   ` r    make_global_gradient_clipnorm_fnrJ   u   s    44&&  r5   c                       d S  fd}|S )zACreates a gradient transformation function for clipping by value.c                     | S r0   r1   r2   s    r   r3   z,make_gradient_clipvalue_fn.<locals>.<lambda>   r4   r5   c           	         t        t        j                  j                         t        j                  j                  j
                  t        j                  j                  j                  j                  j
                  f      r+t        dt        j                  j                          d      | D cg c]  \  }}t        j                  |       |f! }}}|S c c}}w )NzR`clipvalue` is not supported with `CenteralStorageStrategy`. The strategy used is r!   )
r7   r	   r   r8   r9   r:   r;   r<   r$   clip_by_value)r   r   r   r>   	clipvalues       r   gradient_clipvalue_fnz9make_gradient_clipvalue_fn.<locals>.gradient_clipvalue_fn   s    MM&&(**AA		''44KK
 ((*(B(B(D'EQH  '"
1 a)Y7;"
 "
 &%	"
s   4$Cr1   )rO   rP   s   ` r   make_gradient_clipvalue_fnrQ      s    44&& ! r5   c                 ~    | j                   j                  t        j                  j                  j
                  |      S r0   )extendedbatch_reduce_tor	   r   r   r   )distributionr   s     r   r   r      s0      00
""N r5   )__doc__tensorflow.compat.v2r;   v2r	   tensorflow.python.platformr   r%   r   r   rB   rJ   rQ   r   r1   r5   r   <module>rZ      s4     ! ! ="J> 4 6!6r5   