o
    ?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   p/home/www/facesmatcher.com/pyenv/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   Z	job_tasksr   format)r   	task_typetask_idr   Ztask_type_ordered_listZ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      )Z_servicer   _cluster_specr   Z
_task_typer   Z_task_idZmaster_targetZ_evaluation_masterZ_masteris_chiefZ	_is_chief	EVALUATORr   Z_num_ps_replicasr   CHIEFZ_num_worker_replicasr   Z_global_id_in_clusterr   ClusterSpec)configZworker_contextr   r   r   $_init_run_config_from_worker_contextV   s4   


r&   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.ZclusterzXCannot set both "cluster_spec" of TF_CONFIG and `experimental_distribute.remote_cluster`zLRunConfig initialized for Distribute Coordinator with STANDALONE_CLIENT modeNZmaster!Not using Distribute Coordinator.zMRunConfig initialized for Distribute Coordinator with INDEPENDENT_WORKER mode)Z_experimental_distributetrain_distribute_train_distribute
ValueErroreval_distribute_eval_distributer   r$   r
   Z._init_distributed_setting_from_environment_varZremote_clusterdcCoordinatorModeSTANDALONE_CLIENT_distribute_coordinator_moder    logginginfor   Z8_maybe_overwrite_session_config_for_distributed_trainingINDEPENDENT_WORKER)r%   Z	tf_configr   r   r   r   init_run_config{   sF   








r4   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.r0   Nr'   Fz*Unexpected distribute_coordinator_mode: %rzZRunning `train_and_evaluate` locally, ignoring `experimental_distribute_coordinator_mode`.T)hasattrr0   r1   r2   
isinstancesixstring_typesr-   r.   r/   r3   warningr   )r%   r   r   r   !should_run_distribute_coordinator   s&   


r:   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_contextr&   r1   r2   strvars_train_distributionr0   r-   r.   r3   r!   listr?   trainr=   r>   )strategylocal_estimatorcontextr?   )	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.r<   N)r@   rA   rB   r,   r&   rC   rD   r1   r2   rE   rF   _eval_distributionr0   Z_start_continuous_evaluation)rJ   rK   executor)rM   	eval_specexecutor_clsrO   r   r   _eval_fn   s   

z$train_and_evaluate.<locals>._eval_fnNmoder   session_config)r%   r0   r*   r-   r.   r/   r   run_distribute_coordinatorr(   r+   rX   )rM   rO   rS   rT   rP   rU   r   r   )rM   rS   rT   rN   rO   r   train_and_evaluate   s,   !

rZ   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 )r;   r<   )r@   rA   rB   r)   rC   rD   r&   r1   r2   rE   rF   rG   r!   rJ   rK   rL   Zchief_hooksrM   r?   train_distributed_fnr   r   rP   <  s   
z#estimator_train.<locals>._worker_fnrV   )rB   r0   r   r   normalize_cluster_specr)   r   r*   r-   r.   r/   extendedexperimental_between_graph	__class____name__rY   r(   rX   )rM   r`   r?   rN   r   rP   r   r_   r   estimator_train#  s2   

rf   c                    r[   )z=Run distribute coordinator for Estimator's `evaluate` method.r\   r]   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.r<   )r@   rA   rB   r,   rC   rD   r&   r1   r2   rE   rF   rQ   r!   r^   rM   evaluate_distributed_fnr?   r   r   rP   m  s   
z&estimator_evaluate.<locals>._worker_fnrV   )rB   r0   r   r   ra   r,   r   r*   r-   r.   r/   rb   rc   rd   re   rY   r+   rX   )rM   rh   r?   rN   r   rP   r   rg   r   estimator_evaluateT  s2   

ri   )__doc__r@   r7   Ztensorflow.python.distributer   r-   r   rC   r   Ztensorflow.python.platformr   r1   Ztensorflow.python.trainingr   Z	_TaskTyper#   r"   r   r   r   r   r   r&   r4   r:   rZ   rf   ri   r   r   r   r   <module>   s*   	
%6\1