<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, update 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]
        )
    )
)


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}
    )
)<jupyter_output><empty_output><jupyter_text>Create PyTorchJob<jupyter_code>pytorch_client = PyTorchJobClient()
pytorch_client.create(pytorchjob)<jupyter_output><empty_output><jupyter_text>Get the created PyTorchJob<jupyter_code>pytorch_client.get('pytorch-dist-mnist-gloo')<jupyter_output><empty_output><jupyter_text>Update the created PyTorchJob<jupyter_code>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}
    )
)

pytorch_client.patch('pytorch-dist-mnist-gloo', pytorchjob)<jupyter_output><empty_output><jupyter_text>Delete the PyTorchJob<jupyter_code>pytorch_client.delete('pytorch-dist-mnist-gloo')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0907202216066482, 'fraction_numerical': 0.009349030470914127, 'mean_word_length': 5.4630872483221475, 'pattern_counts': {'":': 3, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '91084', 'license': 'Apache-2.0', 'n_tokens_mistral': 1006, 'n_tokens_neox': 1041, 'n_words': 170, 'path': 'sdk/python/examples/kubeflow-pytorchjob-sdk.ipynb', 'repo_name': 'xrmzju/pytorch-operator', 'size': '2888'}