<jupyter_start><jupyter_text>Sample for Kubeflow PyTorchJob SDK This is a sample for Kubeflow PyTorchJob SDK `kubeflow-pytorchjob`.The notebook shows how to use Kubeflow PyTorchJob SDK to create, get, wait, check and delete PyTorchJob.<jupyter_code>from kubernetes.client import V1PodTemplateSpec
from kubernetes.client import V1ObjectMeta
from kubernetes.client import V1PodSpec
from kubernetes.client import V1Container
from kubernetes.client import V1ResourceRequirements

from kubeflow.pytorchjob import constants
from kubeflow.pytorchjob import utils
from kubeflow.pytorchjob import V1ReplicaSpec
from kubeflow.pytorchjob import V1PyTorchJob
from kubeflow.pytorchjob import V1PyTorchJobSpec
from kubeflow.pytorchjob import PyTorchJobClient<jupyter_output><empty_output><jupyter_text>Define namespace where pytorchjob needs to be created to. If not specified, below function defines namespace to the current one where SDK is running in the cluster, otherwise it will deploy to default namespace.<jupyter_code>namespace = utils.get_default_target_namespace()<jupyter_output><empty_output><jupyter_text>Define PyTorchJob The demo only creates a worker of PyTorchJob to run mnist sample.<jupyter_code>container = V1Container(
    name="pytorch",
    image="gcr.io/kubeflow-ci/pytorch-dist-mnist-test:v1.0",
    args=["--backend","gloo"]
)

master = V1ReplicaSpec(
    replicas=1,
    restart_policy="OnFailure",
    template=V1PodTemplateSpec(
        spec=V1PodSpec(
            containers=[container]
        )
    )
)

worker = V1ReplicaSpec(
    replicas=1,
    restart_policy="OnFailure",
    template=V1PodTemplateSpec(
        spec=V1PodSpec(
            containers=[container]
        )
    )
)

pytorchjob = V1PyTorchJob(
    api_version="kubeflow.org/v1",
    kind="PyTorchJob",
    metadata=V1ObjectMeta(name="pytorch-dist-mnist-gloo",namespace=namespace),
    spec=V1PyTorchJobSpec(
        clean_pod_policy="None",
        pytorch_replica_specs={"Master": master,
                               "Worker": worker}
    )
)<jupyter_output><empty_output><jupyter_text>Create PyTorchJob<jupyter_code>pytorchjob_client = PyTorchJobClient()
pytorchjob_client.create(pytorchjob)<jupyter_output><empty_output><jupyter_text>Get the created PyTorchJob<jupyter_code>pytorchjob_client.get('pytorch-dist-mnist-gloo')<jupyter_output><empty_output><jupyter_text>Get the PyTorchJob status, check if the PyTorchJob has been started.<jupyter_code>pytorchjob_client.get_job_status('pytorch-dist-mnist-gloo', namespace=namespace)<jupyter_output><empty_output><jupyter_text>Wait for the specified PyTorchJob to finish<jupyter_code>pytorchjob_client.wait_for_job('pytorch-dist-mnist-gloo', namespace=namespace, watch=True)<jupyter_output>NAME                           STATE                TIME                          
pytorch-dist-mnist-gloo        Created              2020-01-14T09:21:44Z          
pytorch-dist-mnist-gloo        Running              2020-01-14T09:23:45Z          
pytorch-dist-mnist-gloo        Running              2020-01-14T09:23:45Z          
pytorch-dist-mnist-gloo        Running              2020-01-14T09:23:45Z          
pytorch-dist-mnist-gloo        Succeeded            2020-01-14T09:28:31Z<jupyter_text>Check if the PyTorchJob succeeded<jupyter_code>pytorchjob_client.is_job_succeeded('pytorch-dist-mnist-gloo', namespace=namespace)<jupyter_output><empty_output><jupyter_text>Get the PyTorchJob training logs.<jupyter_code>pytorchjob_client.get_logs('pytorch-dist-mnist-gloo', namespace=namespace)<jupyter_output>The logs of Pod pytorch-dist-mnist-gloo-master-0:
 Using distributed PyTorch with gloo backend
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
Processing...
Done!
Train Epoch: 1 [0/60000 (0%)]	loss=2.3000
Train Epoch: 1 [640/60000 (1%)]	loss=2.2135
Train Epoch: 1 [1280/60000 (2%)]	loss=2.1704
Train Epoch: 1 [1920/60000 (3%)]	loss=2.0766
Train Epoch: 1 [2560/60000 (4%)]	loss=1.8679
Train Epoch: 1 [3200/60000 (5%)]	loss=1.4135
Train Epoch: 1 [3840/60000 (6%)]	loss=1.0003
Train Epoch: 1 [4480/60000 (7%)]	loss=0.7762
Train Epoch: 1 [5120/60000 (9%)]	loss=0.4598
Train Epoch: 1 [5760/60000 (10%)]	loss=0.4860
Train Epoch: 1 [6400/60000 (11%)]	loss=0.4389
Train Epoch: 1 [7040/60000 (12%)]	loss=0.4084
Train Epoch: 1 [7680/60000 (13%)]	loss=0.4602
Train Epoch: 1[...]<jupyter_text>Delete the PyTorchJob<jupyter_code>pytorchjob_client.delete('pytorch-dist-mnist-gloo')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10631016042780748, 'fraction_numerical': 0.066524064171123, 'mean_word_length': 4.424593967517401, 'pattern_counts': {'":': 2, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '106162', 'license': 'Apache-2.0', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1766, 'n_words': 305, 'path': 'sdk/python/examples/kubeflow-pytorchjob-sdk.ipynb', 'repo_name': 'mesosphere/pytorch-operator', 'size': '4675'}