
    &VfW                     *   d dl Z d dlZd dlZd dl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  G d dej                  Z G d de          Zd Zd ZddZ d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)dS )    N)context)	callbacks)metrics)
optimizers)tree)trainer)array_slicing)data_adapter_utils)EpochIterator)traceback_utilsc                       e Zd Z fdZed             Zed             Zej        d             Zd Zd Z	d Z
dd	Zdd
ZddZej        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd            Zej        	 	 	 	 	 	 	 	 dd            Zej        	 dd            Z	 	 	 	 d dZ	 	 	 d!dZd Zed             Zd"dZ	 d#dZd"dZ xZS )$TensorFlowTrainerc                    t                                                       d | _        d | _        d | _        t
          j                                        r%t
          j                                        | _	        d S d | _	        d S N)
super__init__train_functiontest_functionpredict_functiontf
distributehas_strategyget_strategy_distribute_strategy)self	__class__s    a/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/backend/tensorflow/trainer.pyr   zTensorFlowTrainer.__init__   ss    "! $ =%%'' 	-(*(B(B(D(DD%%%(,D%%%    c                 L    | j         pt          j                                        S r   )r   r   r   r   r   s    r   distribute_strategyz%TensorFlowTrainer.distribute_strategy!   s    (HBM,F,F,H,HHr   c                     | j         pdS )Nauto_distribute_reduction_methodr    s    r   distribute_reduction_methodz-TensorFlowTrainer.distribute_reduction_method%   s    0:F:r   c                     || _         d S r   r$   )r   values     r   r&   z-TensorFlowTrainer.distribute_reduction_method)   s    ,1)))r   c           	         t          j        |          \  }}}t          j                    5 }| j        r | |d          }n | |          }|                     ||||          }| j                            |t          j        t          j
        |          d                   d                    | j        | j                            |          }d d d            n# 1 swxY w Y   | j        rF| j        }|                    ||          }	| j                            t!          |	|                     nt#          j        d           |                     ||||          S )NTtrainingxyy_predsample_weightr   r0   z.The model does not have any trainable weights.)r
   unpack_x_y_sample_weightr   GradientTape_call_has_training_argcompute_loss_loss_trackerupdate_stateshaper   flatten	optimizer
scale_losstrainable_weightsgradientapply_gradientszipwarningswarncompute_metrics)
r   datar-   r.   r0   taper/   lossr<   	gradientss
             r   
train_stepzTensorFlowTrainer.train_step-   s   0I$OO1m _ 	7$* !a$///a$$q} %  D ++BHT\!__Q-?$@$@$C ,    ~)~0066	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 ! 	L $ 6d,=>>I N**3y:K+L+LMMMMMJKKK##Aq&#NNNs   B'CC#&C#c                 n   t          j        |          \  }}}| j        r | |d          }n | |          }|                     ||||          }| j                            |t          j        t          j	        |          d                   d                    | 
                    ||||          S )NFr*   r,   r   r1   )r
   r2   r4   r5   r6   r7   r   r8   r   r9   rB   )r   rC   r-   r.   r0   r/   rE   s          r   	test_stepzTensorFlowTrainer.test_stepK   s    0I$OO1m& 	T!e,,,FFT!WWF  1V= ! 
 
 	''a); < <Q ? 	( 	
 	
 	
 ##Aq&#NNNr   c                 v    t          j        |          \  }}}| j        r | |d          }n | |          }|S )NFr*   )r
   r2   r4   )r   rC   r-   _r/   s        r   predict_stepzTensorFlowTrainer.predict_stepY   sK    $=dCC1a& 	T!e,,,FFT!WWFr   Fc                      j         	|s j         S t          j        j        j         fd             j        st          j        d j                  t          j        j        j         fd            t          j        j        j         fd            } j        dk    r|}n} j        st          j        |d          }| _         d S )Nc                 .                         |           S )z/Runs a single training step on a batch of data.)rG   rC   r   s    r   one_step_on_dataz?TensorFlowTrainer.make_train_function.<locals>.one_step_on_datae   s     ??4(((r   Treduce_retracingjit_compilec                     t          |           }j                            |f          }t          |j        d          }|S )z5Runs a single training step given a Dataset iterator.argsr#   	reductionnextr!   runreduce_per_replicaiteratorrC   outputsrP   r   s      r   one_step_on_iteratorzCTensorFlowTrainer.make_train_function.<locals>.one_step_on_iteratorq   ]     >>D.22 w 3  G )(   G
 Nr   c                 L    t          j                  D ]} |           }|S r   rangesteps_per_executionr^   rK   r_   r`   r   s      r   multi_step_on_iteratorzETensorFlowTrainer.make_train_function.<locals>.multi_step_on_iterator   4    4344 9 9..x88Nr      rR   )	r   r   	autographexperimentaldo_not_convertrun_eagerlyfunctionrS   re   )r   forcerg   r   rP   r`   s   `   @@r   make_train_functionz%TensorFlowTrainer.make_train_functiona   s(   *5*&&		"	1	) 	) 	) 	) 
2	1	)  	!{ !% ,      
	"	1	 	 	 	 	 
2	1	 
	"	1	 	 	 	 	 
2	1	
 #a''3NN1N 	P[$OOON,r   c                      j         	|s j         S t          j        j        j         fd             j        s j        rt          j        dd          t          j        j        j         fd            t          j        j        j         fd            } j        dk    r|}n} j        st          j        |d          }| _         d S )Nc                 .                         |           S )z+Runs a single test step on a batch of data.)rI   rO   s    r   rP   z>TensorFlowTrainer.make_test_function.<locals>.one_step_on_data   s     >>$'''r   TrQ   c                     t          |           }j                            |f          }t          |j        d          }|S )z1Runs a single test step given a Dataset iterator.rU   r#   rW   rY   r]   s      r   r`   zBTensorFlowTrainer.make_test_function.<locals>.one_step_on_iterator   ra   r   c                 L    t          j                  D ]} |           }|S r   rc   rf   s      r   rg   zDTensorFlowTrainer.make_test_function.<locals>.multi_step_on_iterator   rh   r   ri   rj   )	r   r   rk   rl   rm   rn   rS   ro   re   )r   rp   rg   r   rP   r`   s   `   @@r   make_test_functionz$TensorFlowTrainer.make_test_function   s-   )%)%%		"	1	( 	( 	( 	( 
2	1	(  	D$4 	!{ 4T      
	"	1	 	 	 	 	 
2	1	 
	"	1	 	 	 	 	 
2	1	
 #a''2MM0M 	NKMMMM*r   c                      j         	|s j         S t          j        j        j         fd             j        s j        rt          j        dd          t          j        j        j         fd            t          j        j        j        fd            } j        dk    r|}n} j        st          j        |d          }| _         d S )Nc                 .                         |           S )z,Runs a predict test step on a batch of data.)rL   rO   s    r   rP   zATensorFlowTrainer.make_predict_function.<locals>.one_step_on_data   s     $$T***r   TrQ   c                     | d         } j                             | f          }t          |j         d          }|S )Nr   rU   concatrW   )r!   r[   r\   )rC   r_   rP   r   s     r   one_step_on_data_distributedzMTensorFlowTrainer.make_predict_function.<locals>.one_step_on_data_distributed   sY    7D.22 w 3  G )("  G
 Nr   c                      | d d                   }| dd          D ]%} |g          }t          j        d ||          }&|S )Nri   c                 $    t          | |g          S r   )rz   )t1t2s     r   <lambda>zUTensorFlowTrainer.make_predict_function.<locals>.multi_step_on_data.<locals>.<lambda>   s    62r(#3#3 r   )r   map_structure)rC   r_   single_step_datastep_outputsr{   s       r   multi_step_on_datazCTensorFlowTrainer.make_predict_function.<locals>.multi_step_on_data   sl    2248<<G$(H   ;;=M<NOO,33Wl  Nr   ri   rj   )	r   r   rk   rl   rm   rn   rS   ro   re   )r   rp   r   r   rP   r{   s   `   @@r   make_predict_functionz'TensorFlowTrainer.make_predict_function   s4    ,U,((		"	1	+ 	+ 	+ 	+ 
2	1	+  	D$4 	!{ 4T      
	"	1
	 
	 
	 
	 
	 
2	1
	 
	"	1	 	 	 	 
2	1	 #a''1; 	!{ 4      !1r   Nri   r#           Tr   c                 f   |                      d           d | _        |r"| t          j        |||f|          \  \  }}}}|t	          j        |          \  }}}t          ||||||	|
| j        | j        	  	        }t          |t          j                  s$t          j        |d|dk    |||j        |           }d| _        |                                  |                                 d }d }| j        p|}t#          ||          D ]}|                                  |                    |           |                                5  |                                D ]c\  }}|                    |           |                     |          }|                     |          }|                    ||           | j        r ndd d d            n# 1 swxY w Y   t5          |                     |                    }||                     ||          rt;          | dd           (t          ||||p|| j        | j        |d	          | _        |                     ||||p|||dd
          }d |                                D             }|                     |           |!                    ||           |}| j        r nt          | j"        tF          j$                  r%|dk    r| j"        %                    | j&                   t;          | dd           | `|'                    |           | j(        S )Nfit)validation_split)	r-   r.   r0   
batch_sizesteps_per_epochshuffleclass_weightr!   re   Tr   add_historyadd_progbarverboseepochsstepsmodelF_eval_epoch_iterator)r-   r.   r0   r   r!   re   r   r   )r-   r.   r0   r   r   r   return_dict_use_cached_eval_datasetc                      i | ]\  }}d |z   |S )val_ ).0namevals      r   
<dictcomp>z)TensorFlowTrainer.fit.<locals>.<dictcomp>]  s/       +44FTM3  r   )logs))_assert_compile_calledr   r	   train_validation_splitr
   r2   TFEpochIteratorr!   re   
isinstancecallbacks_moduleCallbackListnum_batchesstop_trainingrq   on_train_begin_initial_epochrd   reset_metricson_epoch_begincatch_stop_iterationenumerate_epochon_train_batch_beginr   _pythonify_logson_train_batch_enddict_get_metrics_result_or_logs_should_evalgetattrevaluateitemsupdateon_epoch_endr:   optimizers_module	Optimizerfinalize_variable_valuesr<   on_train_endhistory)r   r-   r.   r   r   r   r   r   validation_datar   r   r0   initial_epochr   validation_stepsvalidation_batch_sizevalidation_freqval_xval_yval_sample_weightepoch_iteratortraining_logsr   epochstepr^   
epoch_logsval_logss                               r   r   zTensorFlowTrainer.fit   s(   ( 	##E***$(! 		 7 "/!EA}%8H" " "	  &
 #;OLL	! )'!+% $ 8 $ 8

 

 

 )%5%BCC 		(5 #qL$0  I #  """  """+<}=&11 1	 1	E   $$U+++4466  &4&D&D&F&F  ND(224888..x88D//55D00t<<<)                d>>tDDEEJ *t/@/@0 0* 4!7>>F0?&7#8#FJ,0,D,0,D(8 %	1 	1 	1D-  =="34B
*' $-1 ) 	 	 8@8H8H   !!(+++""5*555&M!  t~'8'BCC	L

N33D4JKKK 4/66B)M222|s   A9GG	G	c	           
         |                      d           |	                    dd          }
|	rt          d|	           |
r| j        }n!t	          |||||d| j        | j                  }t          |t          j	                  s$t          j	        |d|dk    |d|j
        | 	          }|                                  d| _        |                                 d }|                                  |                                5  |                                D ]c\  }}|                    |           |                     |          }|                     |          }|                    ||           | j        r ndd d d            n# 1 swxY w Y   |                     |          }|                    |           |r|S |                     |          S )
Nr   r   FzArguments not recognized: )r-   r.   r0   r   r   r   r!   re   Tr   ri   r   )r   pop
ValueErrorr   r   r!   re   r   r   r   r   rv   stop_evaluatingon_test_beginr   r   r   on_test_batch_beginr   r   on_test_batch_endr   on_test_end_flatten_metrics_in_order)r   r-   r.   r   r   r0   r   r   r   kwargsuse_cached_eval_datasetr   r   r   r^   s                  r   r   zTensorFlowTrainer.evaluates  sX    	##J///"(**-G"O"O 	DB&BBCCC" 	!6NN -+% %$($<$($<	 	 	N )%5%BCC 		(5 #qL$0  I 	!!!$!!!0022 	 	"0"@"@"B"B  h--d333))(33++D11++D$777' E	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 //55d### 	K--d333s   A9FFFc           	          t          |||d j         j                  }t          |t          j                  s$t	          j        |d|dk    |d|j                   }d } fd}                                  d _        |	                                 d }	|
                                5  |                                D ]g\  }
}|                    |
            ||          }                     |          } |||	          }	|                    |
d	|i            j        r nhd d d            n# 1 swxY w Y   |                                 t!          j        |t$          |	          }	t!          j        t(          |	          S )
NF)r-   r   r   r   r!   re   Tr   ri   r   c                 h    |t          j        d |           }nt          j        | d ||            |S )Nc                     | gS r   r   )batch_outputs    r   r   zFTensorFlowTrainer.predict.<locals>.append_to_outputs.<locals>.<lambda>  s    , r   c                 ,    |                      |          S r   )append)outputr   s     r   r   zFTensorFlowTrainer.predict.<locals>.append_to_outputs.<locals>.<lambda>  s    |1L1L r   )r   r   map_structure_up_to)batch_outputsr_   s     r   append_to_outputsz4TensorFlowTrainer.predict.<locals>.append_to_outputs  sQ    ,77! 
 (!LL!	   Nr   c                 (   g }t          j                  D ]y}	 t          |           }nQ# t          t          j        j        f$ r3}t          |d          rt          |          dk    r	|cY d}~c S |d}~ww xY w|	                    |           z|S )z$Returns data for the next execution.__len__r   N)
rd   re   rZ   StopIterationr   errorsOutOfRangeErrorhasattrlenr   )r^   rC   rK   r   er   s        r   get_dataz+TensorFlowTrainer.predict.<locals>.get_data  s    D4344 . .
 '+H~~$$%ry'@A      tY//  CIIMM#
    ,----Ks!   +A9$A4*A92A44A9r_   )r   r!   re   r   r   r   r   r   stop_predictingon_predict_beginr   r   on_predict_batch_beginr   on_predict_batch_endon_predict_endr   r   potentially_ragged_concatr   convert_to_np_if_not_ragged)r   r-   r   r   r   r   r   r   r   r_   r   r^   rC   r   s   `             r   predictzTensorFlowTrainer.predict  s   
 )!! $ 8 $ 8
 
 
 )%5%BCC 		(5 #qL$0  I	 	 		 	 	 	 	$ 	""$$$$""$$$0022 	 	"0"@"@"B"B  h00666x)) $ 5 5d ; ;++M7CC..ti5OPPP' E	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  """*4g
 
 !"=wGGGs   ,A=D55D9<D9c                 \   |                      d           |                                  |,t          d d|           t          j        |          fd}|                      |                      }t          j        d |          }|r|S |                     |          S )Ntrain_on_batchzkArguments `sample_weight` and `class_weight` cannot be specified at the same time. Received: sample_weight=z, class_weight=c               3      K    fV  d S r   r   r0   r-   r.   s   r   rC   z.TensorFlowTrainer.train_on_batch.<locals>.data         a''''''r   c                 *    t          j        |           S r   nparrayr-   s    r   r   z2TensorFlowTrainer.train_on_batch.<locals>.<lambda>      BHQKK r   )	r   rq   r   r
   class_weight_to_sample_weightsr   r   r   r   )r   r-   r.   r0   r   r   rC   r   s    ```    r   r   z TensorFlowTrainer.train_on_batch  s     	##$4555  """#( 3/<3 3 %13 3   /M< M	( 	( 	( 	( 	( 	( 	( ""4466**!"7"7>> 	K--d333r   c                     |                      d           |                                  fd}|                      |                      }t          j        d |          }|r|S |                     |          S )Ntest_on_batchc               3      K    fV  d S r   r   r   s   r   rC   z-TensorFlowTrainer.test_on_batch.<locals>.data,  r   r   c                 *    t          j        |           S r   r   r   s    r   r   z1TensorFlowTrainer.test_on_batch.<locals>.<lambda>0  r   r   )r   rv   r   r   r   r   )r   r-   r.   r0   r   rC   r   s    ```   r   r   zTensorFlowTrainer.test_on_batch"  s     	##O444!!!	( 	( 	( 	( 	( 	( 	( !!$$&&))!"7"7>> 	K--d333r   c                     |                                   |                     |fg          }t          j        t          |          }|S r   )r   r   r   r   r   )r   r-   r   s      r   predict_on_batchz"TensorFlowTrainer.predict_on_batch5  sJ    ""$$$--tf55*'
 
 r   c                 6      G  fdd          } |            S )Nc                       e Zd Zd fd	ZdS )DTensorFlowTrainer.compiled_metrics.<locals>.DeprecatedCompiledMetricNc                 4                         |||          S )Nr1   )_compiled_metrics_update_state)rK   r.   r/   r0   r   s       r   r7   zQTensorFlowTrainer.compiled_metrics.<locals>.DeprecatedCompiledMetric.update_stateA  s'    ::v] ;   r   r   )__name__
__module____qualname__r7   r    s   r   DeprecatedCompiledMetricr  @  s3               r   r  r   )r   r  s   ` r   compiled_metricsz"TensorFlowTrainer.compiled_metrics>  sF    	 	 	 	 	 	 	 	 	 	 (')))r   c                     t          j        dd           | j        D ]L}t          |t          j                  r|                    ||           4|                    |||           Md S )Nz`model.compiled_metrics()` is deprecated. Instead, use e.g.:
```
for metric in self.metrics:
    metric.update_state(y, y_pred)
```
   
stacklevelr1   )r@   rA   r   r   metrics_moduleMeanr7   )r   r.   r/   r0   metrics        r   r  z0TensorFlowTrainer._compiled_metrics_update_stateH  s     	
 	
 	
 	
 l 	L 	LF&."566 L##F-#HHHH##Av]#KKKK		L 	Lr   c                 \    t          j        d           |                     d |||          S )Nzf`model.compiled_loss()` is deprecated. Instead, use `model.compute_loss(x, y, y_pred, sample_weight)`.r,   r@   rA   r5   )r   r.   r/   r0   regularization_lossess        r   compiled_losszTensorFlowTrainer.compiled_lossX  sF     	N	
 	
 	
   am ! 
 
 	
r   c                 \    t          j        d           |                     d |||          S )Nz[`model.loss` is deprecated. Instead, use `model.compute_loss(x, y, y_pred, sample_weight)`.r,   r  )r   r.   r/   r0   s       r   rE   zTensorFlowTrainer.lossc  sD    N	
 	
 	
   am ! 
 
 	
r   )F)NNNri   r#   Nr   NTNNr   NNNri   )NNNr#   NNNF)Nr#   NN)NNNF)NNFr   )NN)r	  r
  r  r   propertyr!   r&   setterrG   rI   rL   rq   rv   r   r   filter_tracebackr   r   r   r   r   r  r  r  r  rE   __classcell__r   s   @r   r   r      s       - - - - - I I XI ; ; X; !'2 2 ('2O O O<O O O  ,- ,- ,- ,-\*+ *+ *+ *+X/1 /1 /1 /1b % 
"#D D D &%DL % 
?4 ?4 ?4 &%?4B %HLKH KH KH &%KH` 4 4 4 4D 4 4 4 4&   * * X*L L L L" DH	
 	
 	
 	

 
 
 
 
 
 
 
r   r   c                   R     e Zd Zd fd	Zd Zd Zd Zej        d             Z	 xZ
S )r   Nc                      t                      j        |i | || _        |                                 }t	          |t
          j        j                  s| j                            |          }|| _	        d| _
        d S Nr   )r   r   r   _get_iteratorr   r   r   DistributedDatasetexperimental_distribute_dataset_distributed_dataset_steps_seen)r   r!   rV   r   datasetr   s        r   r   zTFEpochIterator.__init__n  s    $)&)))$7!$$&&'2=#CDD 	/OO G %,!r   c                 4    | j                                         S r   )data_adapterget_tf_datasetr    s    r   r"  zTFEpochIterator._get_iteratory  s     //111r   c              #     K   | j         rJ| j        st          | j                  | _        t	          d| j         | j                  D ]}|| j        fV  n^t          | j                  }| j        r%t	          d| j        | j                  D ]}||fV  	nd}	 || j        z  }|dz   | _        ||fV  | j        	                                 d S )Nr   Tri   )
r   _current_iteratoriterr%  rd   re   r   r&  r)  r   )r   r   r^   s      r   r   zTFEpochIterator.enumerate_epoch|  s(      	)) I)-d.G)H)H&4')A  3 3 D2222223
 D566H 
)!t')A  ) )D .(((()
 )D44D'+axD$.((() 	&&(((((r   c                 ,    t          j                     d S r   )
tf_context
async_waitr    s    r   tf_synczTFEpochIterator.tf_sync  s    r   c              #     K   	 dV  |                                   dS # t          t          j        j        f$ rM | j        | j        | _        t          j        dd           d| _	        | j
                                         Y dS w xY w)z1Catches errors when an iterator runs out of data.NzYour input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches. You may need to use the `.repeat()` function when building your dataset.r  r  )r2  r   r   r   r   _num_batchesr&  r@   rA   r-  r)  r   r    s    r   r   z$TFEpochIterator.catch_stop_iteration  s      	-EEELLNNNNNry89 	- 	- 	- ($($4!M7
     &*D"**,,,,,,	-s    A$BBr   )r	  r
  r  r   r"  r   r2  
contextlibcontextmanagerr   r  r  s   @r   r   r   m  s        	 	 	 	 	 	2 2 2) ) ).      - - - - - - -r   r   c                     dk    r$t          t          j        j                  rdndfd}t	          j        ||           S )a	  Attempt to reduce the structure `values` to single values.

    Given `values` (a `tf.Tensor` or a `PerReplica` structure),
    which represents the values across all the replicas, `reduce_per_replica`
    attempts to "reduce" those values and returns the corresponding structure
    that represents only single values.

    Currently, `reduce_per_replica` is only used for reducing the metric results
    from `tf.distribute.Strategy.run()`. Depending on the underlying
    `Strategy` implementation, `values` may be a `PerReplica` object,
    which can be thought of as a collection of values across the replicas,
    or a `tf.Tensor`, if the strategy has already conducted the reduction
    for the downstream library.

    There are five possible outcomes of reduction:

    1) if the `values` is a structure of simple `tf.Tensor`s, meaning that
       reduction is not actually needed, `reduce_per_replica` returns the
       structure as-is.
    2) else, if `reduction="auto"`, then the best reduction strategy is
       chosen based on the current environment. This should only be used
       for training cases (`fit()`).
    3) else, if `reduction="first"`, then `reduce_per_replica`
       returns the values of the first replica. This is used in the case of
       training and evaluation, where `values` is expected to hold the same
       value across the replicas as a result of `Strategy`'s synchronization
       across the replicas.
       `reduce_per_replica` does not synchronize the values.
    4) else, if `reduction="sum"`, then `reduce_per_replica` returns the sum
       of values for all replicas. This may be used in the custom training loop
       case, where each replica contain different values which are not
       synchronized.
    5) else, if `reduction="concat"`, then `reduce_per_replica`
       returns the concatenation of the values across the replicas, along the
       axis of dimension 0. This is used in the inference case (`predict()`).

    Args:
        values: Structure of `PerReplica` objects or `tf.Tensor`s.
            `tf.Tensor`s are returned as-is.
        strategy: `tf.distribute.Strategy` object.
        reduction: One of `"auto"`, `"first"`, `"concat"`, `"mean"`, or `"sum"`.
            `"auto"` will select `"first"` when used under a TPUStrategy, or
            `"mean"` otherwise.

    Returns:
        Structure of `Tensor`s, representing the result of reduction.
    r#   firstmeanc                    t                    rPdk    rt          |           S dk    r                    d|           S dk    r                    d| d          S t          |           s| S dk    r                    |           d         S dk    rAt                    rt          |           S t                              |                     S dk    r't          j	                            |                     S dk    r)t          j
                            |           d          S t          d	 d
          )z$Reduce a single `PerReplica` object.rz   sumSUMr9  MEANr   axisr8  z\`reduction` must be one of "first", "concat", "mean", "sum", or "auto". Received: reduction=.)#_collective_all_reduce_multi_worker_multi_worker_concatreduce_is_per_replica_instanceexperimental_local_results_is_tpu_multi_host_tpu_multi_host_concatrz   r   
reduce_sumreduce_meanr   )vrX   strategys    r   _reducez#reduce_per_replica.<locals>._reduce  s|   .x88 	:H$$+Ax888e##ua000f$$vqq999'** 	H'!!66q99!<<(""!(++ F-a:::hAA!DDEEE%=!D!DQ!G!GHHH&  >33A66Q    4'04 4 4  r   )r   r   r   TPUStrategyr   r   )valuesrK  rX   rL  s    `` r   r\   r\     si    b Fh 9:: 	III     @ gv...r   c                    |                     | d          }t          |           r=t          j        d | j        D             d          }|                     |d          }nC|                     t          j        t          j        |           d         d          d          }t          j        |||j                  }g }t          |j
        j                  }t          |          D ]}|||d|         z  }t          |          S )zDOrder PerReplica objects for CollectiveAllReduceStrategy and concat.r   r>  c                 j    g | ]0}t          j        t          j        |          d          d           1S )r   r>  )r   expand_dimsr8   )r   single_values     r   
<listcomp>z(_multi_worker_concat.<locals>.<listcomp>  sF         rx55a8qAAA  r   )num_or_size_splitsnumN)gatherrD  r   rz   rN  rQ  r8   splitnum_replicas_in_syncr   extendedworker_devicesrd   )rJ  rK  replicasshapes
all_shapesordered_replicasnum_replicas_per_worker
replica_ids           r   rB  rB    s8   qq))H"" 
 $%H   
 
 
 __V!_44

 __N28A;;q>222 % 
 

 x%)  H
 !("3"BCC344 J J
HZ%H1H%HII"###r   c                    t          | d         t          j                  r!t          j                            ||           S t          | d                   rt          j        | |          S t          j        | |          S )zConcats `tensor`s along `axis`.r   r?  	sp_inputsr>  )r   r   SparseTensorsparserz   
_is_scalarstack)tensorsr?  s     r   rz   rz   $  ss    '!*bo.. -yTW===	GAJ		 -xd++++yt,,,,r   c                     |                     |           }|j        j        }g }t          |          D ]}|||d|         z  }t	          |          S )z'Correctly order TPU PerReplica objects.N)rE  rY  num_replicas_per_hostrd   rz   )rJ  rK  r[  rj  r^  r`  s         r   rG  rG  .  sn    22155H
 %-C122 H H
HZ%F1F%FGG"###r   c                 r    t          | t          j        j                  o| j                                        S r   )r   r   r   MultiWorkerMirroredStrategyrY  _in_multi_worker_moderK  s    r   rA  rA  <  s0    8R]FGG4


1
1
3
34r   c                 ~    t          | t          j        j                  ot          | t          j        j                  S r   )r   r   r   DistributedValues__internal__CompositeTensor)objs    r   rD  rD  B  s6    c2=:;; 
R_,A A r   c                 n    t          | t          j        t          j        f          o| j        j        dk    S r!  )r   r   TensorVariabler8   rankr   s    r   rf  rf  H  s)    a")R[122Hqw|q7HHr   c                 @    t          |           o| j        j        dk    S )Nri   )_is_tpu_strategyrY  	num_hostsrn  s    r   rF  rF  L  s     H%%I(*;*E*IIr   c                 *    t          | j                  S r   )_is_tpu_strategy_classr   rn  s    r   ry  ry  P  s    !("4555r   c                 p    d } ||           rdS t          t          t          | j                            S )Nc                 6    | j                             d          S )NrM  )r	  
startswith)ks    r   is_tpu_stratz,_is_tpu_strategy_class.<locals>.is_tpu_stratU  s    z$$]333r   T)anymapr|  	__bases__)clzr  s     r   r|  r|  T  sE    4 4 4 |C ts)3=99:::r   c                 b    t          | t          j                  r| S |                                 S r   )r   r   RaggedTensornumpyr   s    r   r   r   ]  s)    !R_%% 7799r   c                    t          |           dk    r| d         S t          | d         t          j                  r!t          j                            d|           S t          | d         t          j                  rt          j        | d          S t          j        d | D                       }t          j        	                    ||dd         k    d          }t          j        	                    |          
                                                                rAt          | d                   rt          j        | d          S t          j        | d          S |
                                                                ddd                             d          }|dk    rd}n| d         j        | d         }t          j                            d	 | D             |
                              dd          S )a  Concats `Tensor`s along their first dimension.

    Args:
        tensors: List of `Tensor`s.

    Returns:
        Concatenation of the inputs along the first dimension -- of type
        `np.ndarray` if all input shapes are compatible, or `tf.RaggedTensor`
        if not.
    ri   r   rb  r>  c                 F    g | ]}t          j        |          d d         S )ri   N)r   r8   r   tensors     r   rS  z-potentially_ragged_concat.<locals>.<listcomp>u  s+     L L L&&!1!1!""!5 L L Lr   Nr,  Fc                 6    g | ]}|                                 S r   )r  r  s     r   rS  z-potentially_ragged_concat.<locals>.<listcomp>  s     ...F...r   )inner_shape)r   r   r   rd  re  rz   r  rg  math
reduce_allr  itemrf  tolistindexr8   raggedconstant
merge_dims)rh  non_batch_shapesconstant_dimsconstant_inner_dimensionsconstant_inner_shapes        r   r   r   c  s    7||qqz	GAJ	0	0 *yQ':::	GAJ	0	0 *yq))))x L LG L L LMMG&&,RaR00q '  M 
w-((..005577 .gaj!! 	.8G!,,,,9W1----
 	$$&&ttt,22599  !A%%#&qz/1J0J0K0KL9..g...<P   jAr   )r   )*r5  r@   r  r   
tensorflowr   tensorflow.python.eagerr   r0  	keras.srcr   r   r   r  r   r   r   keras.src.trainersr   base_trainer keras.src.trainers.data_adaptersr	   r
   !keras.src.trainers.epoch_iteratorr   keras.src.utilsr   Trainerr   r   r\   rB  rz   rG  rA  rD  rf  rF  ry  r|  r   r   r   r   r   <module>r     s                9 9 9 9 9 9 3 3 3 3 3 3 / / / / / / 5 5 5 5 5 5       6 6 6 6 6 6 : : : : : : ? ? ? ? ? ? ; ; ; ; ; ; + + + + + +W	
 W	
 W	
 W	
 W	
, W	
 W	
 W	
t;- ;- ;- ;- ;-m ;- ;- ;-|W/ W/ W/t$ $ $>- - - -$ $ $4 4 4  I I IJ J J6 6 6; ; ;  ) ) ) ) )r   