
    ~Wh                     L    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dS )
zOptimizer utilities.    N)
tf_loggingc                 t   t          |           } t          |           }|rt          j        j                                        rSd |D             }t          j                                                            t          j        j        j	        |          }n<t          j                                        
                    t          |f          }ng }g }d}| D ]A\  }}||                    d|f           |                    ||         |f           |dz  }B|t          |          k    s
J d            |S )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.
    c                     g | ]
}|d          S )r    ).0pairs     \/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/keras/optimizers/utils.py
<listcomp>z,all_reduce_sum_gradients.<locals>.<listcomp>$   s    AAAT!WAAA    )argsr   N   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gradsreducedreduced_with_nonesreduced_posgvs           r	   all_reduce_sum_gradientsr$      sQ    .))N4^DD ?%EEGG 		AA)@AAAEm7799DD&*E GG
 m7799DD"*A)C E  GG K  19%%tQi0000%%w{';Q&?@@@1KK#g,,&&&(E&&&r   c                 L   t          |           } | s| S g }g }| D ]4\  }}||                    |           |                    ||f           5t          |          }|s#d | D             f}t          d| d|  d          |rt          j        dd |D                        |S )zDFilter out `(grad, var)` pairs that have a gradient equal to `None`.Nc                 "    g | ]\  }}|j         S r   name)r   _r#   s      r	   r
   z*filter_empty_gradients.<locals>.<listcomp>L   s    7771QV777r   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?c                     g | ]	}|j         
S r   r'   )r   r#   s     r	   r
   z*filter_empty_gradients.<locals>.<listcomp>V   s    444af444r   )tupler   
ValueErrorloggingwarning)r   filteredvars_with_empty_gradsgradvarvariables         r	   r   r   <   s   >**N H# ) )	c<!((----OOT3K((((XH 
777779>x > >,:> > >
 
 	
  
) 543444		
 	
 	
 Or   c                       d S  fd}|S )@Creates a gradient transformation function for clipping by norm.Nc                     | S Nr   r   s    r	   <lambda>z+make_gradient_clipnorm_fn.<locals>.<lambda>^       n r   c                 B   t          t          j                                        t          j        j        j        t          j        j        j        j        j        f          r/t          dt          j                                         d          fd| D             }|S )NzQ`clipnorm` is not supported with `CenteralStorageStrategy`. The strategy used is r*   c                 D    g | ]\  }}t          j        |          |fS r   )r   clip_by_norm)r   r"   r#   clipnorms      r	   r
   zKmake_gradient_clipnorm_fn.<locals>.gradient_clipnorm_fn.<locals>.<listcomp>n   s;     "
 "
 "
26!QR_Q))1-"
 "
 "
r   	
isinstancer   r   get_strategyexperimentalCentralStorageStrategycompatv1r-   )r   clipped_grads_and_varsr?   s     r	   gradient_clipnorm_fnz7make_gradient_clipnorm_fn.<locals>.gradient_clipnorm_fn`   s    M&&((*A	'4K
 
 
	 H(*(B(B(D(DH H H  
"
 "
 "
 "
:H"
 "
 "
 &%r   r   r?   rH   s   ` r	   make_gradient_clipnorm_fnrJ   [   s3    444& & & & &&  r   c                       d S  fd}|S )r6   Nc                     | S r8   r   r9   s    r	   r:   z2make_global_gradient_clipnorm_fn.<locals>.<lambda>y   r;   r   c                    t          t          j                                        t          j        j        j        t          j        j        j        j        j        f          r/t          dt          j                                         d          t          |  \  }}t          j
        |          \  }}t          t          ||                    }|S )NzX`global_clipnorm` is not supported with `CenteralStorageStrategy`. The strategy used is r*   )rA   r   r   rB   rC   rD   rE   rF   r-   zipclip_by_global_normr   )r   r   	variablesclipped_gradsr)   rG   r?   s         r	   rH   z>make_global_gradient_clipnorm_fn.<locals>.gradient_clipnorm_fn{   s    M&&((*A	'4K
 
 	 H(*(B(B(D(DH H H   /y1%BBq!%c-&C&C!D!D%%r   r   rI   s   ` r	    make_global_gradient_clipnorm_fnrR   v   s3    444& & & & &(  r   c                       d S  fd}|S )zACreates a gradient transformation function for clipping by value.Nc                     | S r8   r   r9   s    r	   r:   z,make_gradient_clipvalue_fn.<locals>.<lambda>   r;   r   c                 B   t          t          j                                        t          j        j        j        t          j        j        j        j        j        f          r/t          dt          j                                         d          fd| D             }|S )NzR`clipvalue` is not supported with `CenteralStorageStrategy`. The strategy used is r*   c                 H    g | ]\  }}t          j        |           |fS r   )r   clip_by_value)r   r"   r#   	clipvalues      r	   r
   zMmake_gradient_clipvalue_fn.<locals>.gradient_clipvalue_fn.<locals>.<listcomp>   sC     "
 "
 "
1 a)Y77;"
 "
 "
r   r@   )r   rG   rX   s     r	   gradient_clipvalue_fnz9make_gradient_clipvalue_fn.<locals>.gradient_clipvalue_fn   s    M&&((*A	'4K
 
 
	 H(*(B(B(D(DH H H  
"
 "
 "
 "
&"
 "
 "
 &%r   r   )rX   rY   s   ` r	   make_gradient_clipvalue_fnrZ      s3    444& & & & &( ! r   c                 `    | j                             t          j        j        j        |          S r8   )extendedbatch_reduce_tor   r   r   r   )distributionr   s     r	   r   r      s*     00
"N  r   )__doc__tensorflow.compat.v2rE   v2r   tensorflow.python.platformr   r.   r$   r   rJ   rR   rZ   r   r   r   r	   <module>rc      s      ! ! ! ! ! ! ! ! ! = < < < < <" " "J  >     6     8! ! !8    r   