
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	J
r
  SSK	Jr  SSK	Jr  SS	KJr  SS
KJr  SSKJr  S r    SS jr SS jrg)z,Utilities for running training jobs locally.    )absolute_import)division)unicode_literalsN)execution_utils)log)
properties)encoding)files)rangec                  >   [         R                  R                  R                  R	                  5       n U =(       d2    [
        R                  " S5      =(       d    [        R                  " 5       nUSS/n[
        R                  " [        R                  5       n[        R                  " U[        R                  US9nSSS5        WR                  5       nUS:w  a&  [         R"                  " SR%                  U5      5        gUR&                  R)                  5       nS	[+        U5      ;   a  UR-                  S
5      nUR/                  S5      (       a  gUR/                  S5      (       a  g[         R"                  " SR%                  U5      5        g! , (       d  f       N= f)a  Get the primary node name.

Returns:
  str, the name of the primary node. If running in tensorflow 1.x,
  return 'master'. If running in tensorflow 2.x, return 'chief'.
  If tensorflow is not installed in local envrionment, it will return
  the default name 'chief'.
Raises:
  ValueError: if there is no python executable on the user system thrown by
    execution_utils.GetPythonExecutable.
pythonz-cz2import tensorflow as tf; print(tf.version.VERSION))stdoutstderrNr   au  
    Cannot import tensorflow under path {}. Using "chief" for cluster setting.
    If this is not intended, Please check if tensorflow is installed. Please also
    verify if the python path used is correct. If not, to change the python path:
    use `gcloud config set ml_engine/local_python $python_path`
    Eg: gcloud config set ml_engine/local_python /usr/bin/python3chiefdecodezutf-8z1.masterz2.zgUnexpected tensorflow version {}, using the default primary node name, aka "chief" for cluster settings)r   VALUES	ml_enginelocal_pythonGetr
   FindExecutableOnPathr   GetPythonExecutable
FileWriterosdevnull
subprocessPopenPIPEwaitr   warningformatr   readdirr   
startswith)exe_overridepython_executablecmdfprocreturn_code
tf_versions          7lib/googlecloudsdk/command_lib/ml_engine/local_train.pyGetPrimaryNodeNamer-   "   sP    "",,99==?, ,e00: ,))+  
		=	?# 

#qC
BD $		+AKK E
 FLVF {{!*Z ""7+J4  T""++55;VJ5GI 
/ $#s   %F
Fc                 ,   Uc  / n[         R                  R                  R                  R	                  5       nU=(       d2    [
        R                  " S5      =(       d    [        R                  " 5       nUSU /U-   n	XS.U(       a  XES.O0 U=(       d    0 SS.n
[        R                  " SR                  SR                  U	5      [        R                  " U
S	S
S9S95        [        R                   R#                  5       n[        R                  " U
5      US'   U[%        5       :X  a  [        R&                  " U	4US
US.UD6$ [(        R*                  " U5      n[,        R.                  " U	4UUS.UD6n[0        R2                  " [        R4                  U5        U$ )a  Make a Popen object that runs the module, with the correct env.

If task_type is primary instead replaces the current process with the
subprocess via execution_utils.Exec
Args:
  module_name: str. Name of the module to run, e.g. trainer.task
  package_root: str. Absolute path to the package root for the module.
    used as CWD for the subprocess.
  args: [str]. Additional user args. Any relative paths will not work.
  cluster: dict. Cluster configuration dictionary. Suitable for passing to
    tf.train.ClusterSpec.
  task_type: str. Task type of this process. Only relevant if cluster is
    specified.
  index: int. Task index of this process.
  **extra_popen_args: extra args passed to Popen. Used for testing.
Returns:
  a subprocess.Popen object corresponding to the subprocesses or an int
  corresponding to the return value of the subprocess
  (if task_type is primary)
Raises:
  ValueError: if there is no python executable on the user system thrown by
    execution_utils.GetPythonExecutable.
r   z-m)job_nameargs)typeindexcloud)jobtaskclusterenvironmentz<launching training process:
command: {cmd}
 config: {config}    T)indent	sort_keys)r'   config	TF_CONFIG)envno_exitcwd)r>   r@   )r   r   r   r   r   r
   r   r   r   r   infor!   joinjsondumpsr   environcopyr-   Execr	   	EncodeEnvr   r   atexitregisterKillSubprocess)module_namepackage_rootr0   r6   	task_typer2   extra_popen_argsr%   r&   r'   r<   r>   r5   s                r,   MakeProcessrP   O   s   < 
\D"",,99==?, ,e00: ,))+  
D+.5#%45<y1"B	& (( 017HHSMzz&dC 28 2EF
 	

# ZZ'#k$&&JdJ8HJ J 

S
!C 	D OOO22D9K    c                 0   [        [        XUU-   U-   S-   5      5      n[        5       SR                  US   S9/SUSUS-     Vs/ s H  nSR                  US9PM     snSXrS-   S  Vs/ s H  nSR                  US9PM     sn0n	U	R	                  5        H>  u  pU
[        5       :w  d  M  [        [        U5      5       H  n[        U UUU
UU	S9  M     M@     [        U5       H  n[        U UUS	UU	S9  M     [        U UU[        5       SU	S9$ s  snf s  snf )
a-  Create a cluster configuration and start processes for the cluster.

Args:
  module_name: str. Python module to use as the task.
  package_root: str. Absolute path to the package root of the module.
  num_ps: int. Number of parameter servers
  num_workers: int. Number of workers.
  num_evaluators: int. Number of evaluators.
  start_port: int. First port for the contiguous block of ports used
    by the cluster.
  user_args: [str]. Additional user args for the task. Any relative paths will
    not work.
Returns:
  int. the retval of primary subprocess
   zlocalhost:{port}r   )portpsworkerN)r0   rN   r2   r6   	evaluator)listr   r-   r!   itemslenrP   )rL   rM   num_psnum_workersnum_evaluators
start_port	user_argsportspr6   rN   	addressesis                r,   RunDistributedrd      sW   , uZf!4{!BQ!FG
H%/66E!H6EF
Afqj)+)  &&A&.)+
,.,Q $***2,.	' &mmoi&((S^$!K "'#	% % .  a%! ! 
[!#13$
& &++.s   D
)D)NNNN)N)__doc__
__future__r   r   r   rI   rC   r   r   googlecloudsdk.corer   r   r   googlecloudsdk.core.utilr	   r
   	six.movesr   r-   rP   rd    rQ   r,   <module>rk      sV    3 &  '   	  / # * - * *^ AT "3&rQ   