o
    i ew>                     @   s   d 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 ejjZejjZejjZejjZdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )z?Training utilities for Estimator to use Distribute Coordinator.    N)distribute_coordinator)distribute_coordinator_context)multi_worker_util)
tf_logging)
server_libc                 C   s    | st dt|  tg S )z7Counts the number of parameter servers in cluster_spec.z?Internal error: `_count_ps` does not expect empty cluster_spec.)RuntimeErrorlenas_dictgetPS)cluster_spec r   ^/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/distribute/estimator_training.py	_count_ps$   s
   r   c                 C   s4   | st dt|  tg t|  |g  S )z?Counts the number of workers (including chief) in cluster_spec.zCInternal error: `_count_worker` does not expect empty cluster_spec.)r   r   r	   r
   WORKER)r   chief_task_typer   r   r   _count_worker-   s   r   c                    s   |sdS g } | j v r g}| fddt| j D  t| j v r'|t d}|D ]}||kr7||   S |t| |7 }q+td|| )z:Returns the global id of the given task type in a cluster.r   c                    s    g | ]}| kr|t kr|qS r   )r   ).0tr   r   r   
<listcomp>B   s    z"_get_global_id.<locals>.<listcomp>z=Internal Error: `task_type` ({}) is not in cluster_spec ({}).)	jobsextendsortedr   appendr   	job_tasksr   format)r   	task_typetask_idr   task_type_ordered_listnext_global_idr   r   r   r   _get_global_id7   s$   


r!   c                 C   s   d| _ |j| _|j| _|j| _|j| _|j| _	|j
| _| jrP| jtkr?t| j| _t| jtd| _t| j| j| jtd| _dS ti | _d| _d| _d| _dS d| _d| _d| _dS )zDInitializes run config from distribute coordinator's worker context.Nr   r      )_servicer   _cluster_specr   
_task_typer   _task_idmaster_target_evaluation_master_masteris_chief	_is_chief	EVALUATORr   _num_ps_replicasr   CHIEF_num_worker_replicasr!   _global_id_in_clusterr   ClusterSpec)configworker_contextr   r   r   $_init_run_config_from_worker_contextV   s4   


r4   c                 C   s
  | j r| j jr| jrtd| j j| _| j r&| j jr&| jr!td| j j| _t|di }| 	i  | jrU| j rU| j j
rU|rDtdtjj| _| j j
| _td dS |r_d|jv s_| jsrd| _| 	| |   td dS |svJ || _tjj| _td	 dS )
z2Initializes RunConfig for distribution strategies.zREither `train_distribute` or`experimental_distribute.train_distribute` can be set.zPEither `eval_distribute` or`experimental_distribute.eval_distribute` can be set.clusterzXCannot set both "cluster_spec" of TF_CONFIG and `experimental_distribute.remote_cluster`zLRunConfig initialized for Distribute Coordinator with STANDALONE_CLIENT modeNmaster!Not using Distribute Coordinator.zMRunConfig initialized for Distribute Coordinator with INDEPENDENT_WORKER mode)_experimental_distributetrain_distribute_train_distribute
ValueErroreval_distribute_eval_distributer   r1   r
   ._init_distributed_setting_from_environment_varremote_clusterdcCoordinatorModeSTANDALONE_CLIENT_distribute_coordinator_moder$   logginginfor   8_maybe_overwrite_session_config_for_distributed_trainingINDEPENDENT_WORKER)r2   	tf_configr   r   r   r   init_run_config{   sF   








rI   c                 C   sp   t | dr
| jdu rtd dS t| jtjr#| jtjj	tjj
fvr,td| j dS | js6td dS dS )z?Checks the config to see whether to run distribute coordinator.rC   Nr7   Fz*Unexpected distribute_coordinator_mode: %rzZRunning `train_and_evaluate` locally, ignoring `experimental_distribute_coordinator_mode`.T)hasattrrC   rD   rE   
isinstancesixstring_typesr@   rA   rB   rG   warningr   )r2   r   r   r   !should_run_distribute_coordinator   s"   



rO   c              	      s|    j js
td fdd} fdd}jtjjkr*j}|s)J nd}tj|j|j	j|j
d dS )a  Run distribute coordinator for Estimator's `train_and_evaluate`.

  Args:
    estimator: An `Estimator` instance to train and evaluate.
    train_spec: A `TrainSpec` instance to specify the training specification.
    eval_spec: A `EvalSpec` instance to specify the evaluation and export
      specification.
    executor_cls: the evaluation executor class of Estimator.

  Raises:
    ValueError: if `distribute_coordinator_mode` is None in RunConfig.
  z<Distribute coordinator mode is not specified in `RunConfig`.c                    s   t  }| |j_t }t|j| tdt	t
|j | |_jtjjks+|jr1tj}ng }d|j_|jjj|d dS )Function for worker task.Updated config: %sN)input_fn	max_stepshooks)copydeepcopy_configr:   
dc_contextget_current_worker_contextr4   rD   rE   strvars_train_distributionrC   r@   rA   rG   r*   listrT   trainrR   rS   )strategylocal_estimatorcontextrT   )	estimator
run_config
train_specr   r   
_worker_fn   s&   


z&train_and_evaluate.<locals>._worker_fnc                    s^   t  }| |j_t|jt  tdt	t
|j | |_d|j_|}|  dS )zFunction for evaluator task.rQ   N)rU   rV   rW   r=   r4   rX   rY   rD   rE   rZ   r[   _eval_distributionrC   _start_continuous_evaluation)r_   r`   executor)rb   	eval_specexecutor_clsrd   r   r   _eval_fn   s   

z$train_and_evaluate.<locals>._eval_fnNmoder   session_config)r2   rC   r;   r@   rA   rB   r   run_distribute_coordinatorr9   r<   rn   )rb   rd   ri   rj   re   rk   r   r   )rb   ri   rj   rc   rd   r   train_and_evaluate   s,   !

rp   c                        j jsJ  j } j jsJ t j j} j jsJ d|jv r%td j jtj	j
kr1td j jjjrAtd j jjj  fdd}tj| j j|j||jdS )z:Run distribute coordinator for Estimator's `train` method.	evaluatorF'evaluator' job is not supported if you don't use `train_and_evaluate`zJOnly `STANDALONE_CLIENT` mode is supported when you call `estimator.train`zL`Estimator.train` API is not supported for %s with `STANDALONE_CLIENT` mode.c                    sb   t  }| |j_t }t|j| tdt	t
|j | |_|jr'}ng }|| | |S )rP   rQ   )rU   rV   rW   r:   rX   rY   r4   rD   rE   rZ   r[   r\   r*   r_   r`   ra   chief_hooksrb   rT   train_distributed_fnr   r   re   <  s   
z#estimator_train.<locals>._worker_fnrl   )rW   rC   r   r   normalize_cluster_specr:   r   r;   r@   rA   rB   extendedexperimental_between_graph	__class____name__ro   r9   rn   )rb   rw   rT   rc   r   re   r   rv   r   estimator_train#  s2   

r}   c                    rq   )z=Run distribute coordinator for Estimator's `evaluate` method.rr   rs   zMOnly `STANDALONE_CLIENT` mode is supported when you call `Estimator.evaluate`zO`Estimator.evaluate` API is not supported for %s with `STANDALONE_CLIENT` mode.c                    s^   t  }| |j_t }t|j| tdt	t
|j | |_|jr'}ng }|| |S )zFunction for evaluation.rQ   )rU   rV   rW   r=   rX   rY   r4   rD   rE   rZ   r[   rf   r*   rt   rb   evaluate_distributed_fnrT   r   r   re   m  s   
z&estimator_evaluate.<locals>._worker_fnrl   )rW   rC   r   r   rx   r=   r   r;   r@   rA   rB   ry   rz   r{   r|   ro   r<   rn   )rb   r   rT   rc   r   re   r   r~   r   estimator_evaluateT  s2   

r   )__doc__rU   rL   tensorflow.python.distributer   r@   r   rX   r   tensorflow.python.platformr   rD   tensorflow.python.trainingr   	_TaskTyper.   r,   r   r   r   r   r!   r4   rI   rO   rp   r}   r   r   r   r   r   <module>   s*   	
%6\1