
    J                        d 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 d Zd Z ej&                  ej(                  j*                         G d dej,                               Z ej&                  ej(                  j0                  ej(                  j2                         G d de             ZddiZee_        y)z)ai-platform jobs submit training command.    )absolute_import)division)unicode_literals)jobs)base)
exceptions)flags)	jobs_util)labels_utilc                 "   t         j                  j                  |        t         j                  j                  |        t         j                  j                  |        t        j
                  d      j                  |        t        j                  | dd       t         j                  j                  |        t         j                  j                  |        t        j                  d      j                  |        t        j                  d      j                  |        t        j                         j                  j                  |        t         j                  j                  |        t        j                   | d       t         j"                  j                  |        t         j$                  j                  |        | j'                         }|j)                  d	d
dd       |j)                  dd
d       t+        j,                  |        y)z1Add arguments for `jobs submit training` command.F)requiredzmachine learning training jobsubmitT)upload_help)localzduring trainingz--async
store_trueasync_z(DEPRECATED) Display information about the operation in progress without waiting for the operation to complete. Enabled by default and can be omitted; use `--stream-logs` to run synchronously.)actiondesthelpz--stream-logszBlock until job completion and stream the logs while the job runs.

Note that even if command execution is halted, the job will still run until cancelled with

    $ gcloud ai-platform jobs cancel JOB_ID)r   r   N)r	   JOB_NAMEAddToParserPACKAGE_PATHPACKAGESGetModuleNameFlagcompute_flagsAddRegionFlagCONFIGSTAGING_BUCKETGetJobDirFlagGetUserArgsr
   ScaleTierFlagMap
choice_argRUNTIME_VERSIONAddPythonVersionFlagTRAINING_SERVICE_ACCOUNTENABLE_WEB_ACCESSadd_mutually_exclusive_groupadd_argumentr   AddCreateLabelsFlags)parser
sync_groups     /lib/surface/ai_platform/jobs/submit/training.py_AddSubmitTrainingArgsr-      s   ..V$  (..V$5)55f=f&E&(,,6"""6*$'33F;% ,,V4))55f=##F+V%67  ,,V4%%f-224* 8   :  < ""6*    c                    t        | j                  d      rq| j                  j                  j                         }|r|j	                         S dD ]5  }t        | |j                  dd      d      s!t        j                  dd       yy)zFParse CMEK resource arg, and check if the arg was partially specified.kms_key)zkms-keyzkms-keyringzkms-locationzkms-project-_Nz	--kms-keyz#Encryption key not fully specified.)	hasattrCONCEPTSr0   ParseRelativeNamegetattrreplacer   InvalidArgumentException)argskms_refkeywords      r,   _GetAndValidateKmsKeyr=   D   s|    T]]I&mm##))+G!!##N'4c2D933@B B O 'r.   c                   *    e Zd ZdZdZed        Zd Zy)Train#Submit an AI Platform training job.Fc                     t        |       t        j                  || j                         t        j                  |d       |j
                  j                  t        j                         y )Nsupport_tpu_tf_versionjob)	r-   r	   AddCustomContainerFlags_SUPPORT_TPU_TF_VERSIONAddKmsKeyFlagdisplay_info	AddFormatr
   
JOB_FORMATclsr*   s     r,   Argsz
Train.ArgsW   sN    6"	!!s'B'BD	&
!!)"6"67r.   c           	         t        j                  |j                  |j                        }t        j                         j                  |j                        }|r|j                  nd }t        j                         }t        j                  ||      }t         j                  j                  || j                        }|j                          t        j                  ||j                   fi d|j"                  d|j$                  d|j&                  d|j(                  d|d|j*                  d|j,                  d|j.                  d	|j0                  d
t3        |d
      r|j4                  nd d|j6                  d|d|d|j8                  dt;        |      d|d|j<                  }|r)|j>                  |j@                  jB                  urd| _"        |S )Njob_dirstaging_bucketpackagespackage_path
scale_tierconfigmodule_nameruntime_versionpython_versionnetworkservice_accountlabelsstream_logs	user_argsr0   custom_train_server_configenable_web_access   )#r
   GetStreamLogsr   r[   r!   GetEnumForChoicerS   namer   
JobsClientParseCreateLabelsTrainingCustomInputServerConfigFromArgsrF   ValidateConfigSubmitTrainingrD   rO   rP   rQ   rR   rT   rU   rV   rW   r3   rX   rY   r\   r=   r^   stateStateValueValuesEnum	SUCCEEDED	exit_code)	selfr:   r[   rS   scale_tier_namejobs_clientrZ   custom_container_configrD   s	            r,   Runz	Train.Run_   s   ))$++t7G7GHK++->>tOJ)3jooO//#K((d;F11::$..	0  **,

"
"2 2 **	2
 2 &&2 #2 {{2 $$2 ,,2 **2 !(i 8d2 ,,2 2  2  ..!2" &d+#2$ $;%2& 00'2C* syy(@(@(J(JJdnJr.   N)__name__
__module____qualname____doc__rF   classmethodrM   rq    r.   r,   r?   r?   Q   s"    +!8 8!r.   r?   c                   $    e Zd ZdZdZed        Zy)TrainAlphaBetar@   Tc                    t        |       t        j                  |d       t        j                  j	                  |       t        j
                  || j                         |j                  j                  t        j                         y )NrD   rB   )r-   r	   rG   NETWORKr   rE   rF   rH   rI   r
   rJ   rK   s     r,   rM   zTrainAlphaBeta.Args   s`    6"	&	MMf%	!!s'B'BD
!!)"6"67r.   N)rr   rs   rt   ru   rF   rv   rM   rw   r.   r,   ry   ry      s    + 8 8r.   ry   DESCRIPTIONaD  Submit an AI Platform training job.

This creates temporary files and executes Python code staged
by a user on Cloud Storage. Model code can either be
specified with a path, e.g.:

    $ {command} my_job \
            --module-name trainer.task \
            --staging-bucket gs://my-bucket \
            --package-path /my/code/path/trainer \
            --packages additional-dep1.tar.gz,dep2.whl

Or by specifying an already built package:

    $ {command} my_job \
            --module-name trainer.task \
            --staging-bucket gs://my-bucket \
            --packages trainer-0.0.1.tar.gz,additional-dep1.tar.gz,dep2.whl

If `--package-path=/my/code/path/trainer` is specified and there is a
`setup.py` file at `/my/code/path/setup.py`, the setup file will be invoked
with `sdist` and the generated tar files will be uploaded to Cloud Storage.
Otherwise, a temporary `setup.py` file will be generated for the build.

By default, this command runs asynchronously; it exits once the job is
successfully submitted.

To follow the progress of your job, pass the `--stream-logs` flag (note that
even with the `--stream-logs` flag, the job will continue to run after this
command exits and must be cancelled with `gcloud ai-platform jobs cancel JOB_ID`).

For more information, see:
https://cloud.google.com/ai-platform/training/docs/overview
N)ru   
__future__r   r   r    googlecloudsdk.api_lib.ml_enginer   googlecloudsdk.callioper   r   "googlecloudsdk.command_lib.computer	   r   $googlecloudsdk.command_lib.ml_enginer
   $googlecloudsdk.command_lib.util.argsr   r-   r=   ReleaseTracksReleaseTrackGACommandr?   ALPHABETAry   _DETAILED_HELPdetailed_helprw   r.   r,   <module>r      s    0 &  ' 1 ( . E 6 : <#+L
B D%%(().DLL . *.b D%%++T->->-C-CD8U 8 E8  !	$L % r.   