o
    i eQ(                  
   @   s  d Z ddlZddlZddlm  mZ ddlm	Z	 ddl
mZ dZdZdZdZd	ZejejejgZejjjjejjjjejjjjejjjjejjjjejjjjejjjjejjjjejjjj ejjjj!g
Z"d
d Z#dd Z$dd Z%dd Z&dd Z'G dd dej(j)e	j)Z*dS ).Base class for testing saving/loading with DS.    N)parameterized)model_combinationsi9  serving_defaultgKH9gHz>   c                   C   s   t jjjjttdgdS Neager)model_and_inputdistributionmodetf__internal__testcombinationscombinesimple_models
strategies r   r   Y/var/www/myenv/lib/python3.10/site-packages/keras/src/distribute/saved_model_test_base.pysimple_models_with_strategies8   s   
r   c                   C   s   t jjjjtttdgdS Nr   )r	   distribution_for_savingdistribution_for_restoringr   r   r   r   r   r   !simple_models_with_strategy_pairs>   s   
r   c                   C   s   t jjjjtjgtdgdS r   r   r   r   r   r   r   simple_tfmodule_modelr   r   r   r   r   tfmodule_models_with_strategiesG   s
   
r   c                   C   s   t jjjjtjgttdgdS r   r   r   r   r   r   #tfmodule_models_with_strategy_pairsO   s   
r   c           
      C   s|   t j|}| r/| |}tt|}| j|jt |fd}|| }| 	|}t 
|d}	|	S |jt tt|}|| S )z:Loads a saved_model using tf.saved_model API, and runs it.)argsr   )r   saved_modelloadexperimental_distribute_datasetnextiterrun
signatures_DEFAULT_FUNCTION_KEYexperimental_local_resultsconcat)
r
   	saved_dirpredict_datasetoutput_namefuncdist_predict_datasetper_replica_predict_dataresultreducedr)   r   r   r   !load_and_run_with_saved_model_apiX   s$   

r2   c                       sl   e Zd ZdZ fddZdd Z	dddZd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Z  ZS )TestSavedModelBaser   c                    s.   t jt tjjt d| _t	 
  d S )Nbase)nprandomseed_RANDOM_SEEDr   compatv1set_random_seed	_root_dirsupersetUp)self	__class__r   r   r>   v   s   zTestSavedModelBase.setUpc                 C      t d)zSave the given model to the given saved_dir.

        This method needs to be implemented by the subclasses.

        Args:
          model: a keras model object to save.
          saved_dir: a string representing the path to save the keras model
        "must be implemented in descendantsNotImplementedError)r?   modelr*   r   r   r   _save_model|   s   	zTestSavedModelBase._save_modeloutput_1c                 C   rB   )a)  Load the model and run 1 step of predict with it.

        This method must be implemented by the subclasses.

        Args:
          distribution: the distribution strategy used to load the model. None
            if no distribution strategy is used
          saved_dir: the string representing the path where the model is saved.
          predict_dataset: the data used to do the predict on the model for
            cross_replica context.
          output_name: the string representing the name of the output layer of
            the model.
        rC   rD   )r?   r
   r*   r+   r,   r   r   r   _load_and_run_model   s   z&TestSavedModelBase._load_and_run_modelc                 C   s8   t jj||f}| }||}|j|ddd d S )Nr   d   )xepochssteps_per_epoch)r   dataDatasetfrom_tensor_slicesrepeatbatchfit)r?   rF   x_trainy_train
batch_sizetraining_datasetr   r   r   _train_model   s   
zTestSavedModelBase._train_modelc                 C   s   |j |tdS )N)steps)predictPREDICT_STEPS)r?   r
   rF   r+   r   r   r   _predict_with_model   s   z&TestSavedModelBase._predict_with_modelc                 C   s$   t jj|}| }||}|S )N)r   rN   rO   rP   rQ   rR   )r?   	x_predictrV   r+   r   r   r   _get_predict_dataset   s   
z'TestSavedModelBase._get_predict_datasetc                 C   s   t j|  d}| }| \}}}| }| ||}	| |||| | 	d||	}
| 
|| |  | j|||	d}W d   n1 sJw   Y  | |
| dS )z0Save a model without DS, and restore it with DS.0Nr
   r*   r+   )ospathjoinget_temp_dir	get_modelget_dataget_batch_sizer^   rX   r\   rG   scoperI   assertAllClose)r?   r	   r
   r*   rF   rT   rU   r]   rV   r+   result_before_saveresult_after_saver   r   r   *run_test_save_no_strategy_restore_strategy   s$   
z=TestSavedModelBase.run_test_save_no_strategy_restore_strategyc                 C   s   t j|  d}| , | }| \}}}| }	| ||||	 | 	||	}
| 
|||
}W d   n1 s<w   Y  |r^|  | || W d   n1 sXw   Y  n| || | jd||
d}| || dS )z0Save a model with DS, and restore it without DS.1Nr`   ra   rb   rc   rd   rh   re   rf   rg   rX   r^   r\   rG   rI   ri   )r?   r	   r
   save_in_scoper*   rF   rT   rU   r]   rV   r+   rj   load_resultr   r   r   *run_test_save_strategy_restore_no_strategy   s.   

z=TestSavedModelBase.run_test_save_strategy_restore_no_strategyc                 C   s  t j|  d}| , | }| \}}}	| }
| ||||
 | 	|	|
}| 
|||}W d   n1 s<w   Y  |r^|  | || W d   n1 sXw   Y  n| || |  | j|||d}W d   n1 s{w   Y  | || dS )zKSave a model with DS, and restore it with potentially different
        DS.2Nr`   rn   )r?   r	   r   r   ro   r*   rF   rT   rU   r]   rV   r+   rj   rp   r   r   r   'run_test_save_strategy_restore_strategy   s2   	


z:TestSavedModelBase.run_test_save_strategy_restore_strategyc           
      C   s   t j|  d}|  | }| \}}}| }	| ||||	 W d   n1 s/w   Y  |rS|  | 	|| W d   |S 1 sLw   Y  |S | 	|| |S )zSave a model with DS.3N)
ra   rb   rc   rd   rh   re   rf   rg   rX   rG   )
r?   r	   r
   ro   r*   rF   rT   rU   _rV   r   r   r   run_test_save_strategy  s    


z)TestSavedModelBase.run_test_save_strategy)rH   )__name__
__module____qualname____doc__r>   rG   rI   rX   r\   r^   rl   rq   rs   rv   __classcell__r   r   r@   r   r3   s   s    

 &r3   )+rz   ra   numpyr5   tensorflow.compat.v2r9   v2r   absl.testingr   keras.src.distributer   r8   r'   
_TOLERANCE_TPU_TOLERANCEr[   simple_functional_modelsimple_sequential_modelsimple_subclass_modelr   r   
distributer   default_strategyone_device_strategyone_device_strategy_gpumirrored_strategy_with_one_cpumirrored_strategy_with_one_gpu"mirrored_strategy_with_gpu_and_cpumirrored_strategy_with_two_gpustpu_strategytpu_strategy_packed_var&central_storage_strategy_with_two_gpusr   r   r   r   r   r2   r   TestCaser3   r   r   r   r   <module>   s@   









		