|
| 1 | ++++ |
| 2 | +title = "Getting Started" |
| 3 | +description = "Get started with Training Operator" |
| 4 | +weight = 30 |
| 5 | ++++ |
| 6 | + |
| 7 | +This guide describes how to get started with Training Operator and run a few simple examples. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +You need to install the following components to run examples: |
| 12 | + |
| 13 | +- Training Operator [installed](/docs/components/training/installation/#installing-training-operator). |
| 14 | +- Training Python SDK [installed](/docs/components/training/installation/#installing-training-python-sdk). |
| 15 | + |
| 16 | +## Getting Started with PyTorchJob |
| 17 | + |
| 18 | +You can create your first Training Operator distributed PyTorchJob using Python SDK. Define the |
| 19 | +training function that implements end-to-end model training. Each Worker will execute this |
| 20 | +function on the appropriate Kubernetes Pod. Usually, this function contains logic to |
| 21 | +download dataset, create model, and train the model. |
| 22 | + |
| 23 | +Training Operator will automatically set `WORLD_SIZE` and `RANK` for the appropriate PyTorchJob |
| 24 | +worker to perform [PyTorch Distributed Data Parallel (DDP)](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html). |
| 25 | + |
| 26 | +For example: |
| 27 | + |
| 28 | +```python |
| 29 | +def train_func(): |
| 30 | + import torch |
| 31 | + import torch.nn.functional as F |
| 32 | + from torch.utils.data import DistributedSampler |
| 33 | + from torchvision import datasets, transforms |
| 34 | + |
| 35 | + # [1] Setup PyTorch DDP. WORLD_SIZE and RANK environments will be set by Training Operator. |
| 36 | + torch.distributed.init_process_group(backend="nccl") |
| 37 | + Distributor = torch.nn.parallel.DistributedDataParallel |
| 38 | + |
| 39 | + # [2] Create PyTorch CNN Model. |
| 40 | + class Net(torch.nn.Module): |
| 41 | + def __init__(self): |
| 42 | + super(Net, self).__init__() |
| 43 | + self.conv1 = torch.nn.Conv2d(1, 20, 5, 1) |
| 44 | + self.conv2 = torch.nn.Conv2d(20, 50, 5, 1) |
| 45 | + self.fc1 = torch.nn.Linear(4 * 4 * 50, 500) |
| 46 | + self.fc2 = torch.nn.Linear(500, 10) |
| 47 | + |
| 48 | + def forward(self, x): |
| 49 | + x = F.relu(self.conv1(x)) |
| 50 | + x = F.max_pool2d(x, 2, 2) |
| 51 | + x = F.relu(self.conv2(x)) |
| 52 | + x = F.max_pool2d(x, 2, 2) |
| 53 | + x = x.view(-1, 4 * 4 * 50) |
| 54 | + x = F.relu(self.fc1(x)) |
| 55 | + x = self.fc2(x) |
| 56 | + return F.log_softmax(x, dim=1) |
| 57 | + |
| 58 | + # [3] Attach model to GPU and distributor. |
| 59 | + device = "cuda" |
| 60 | + model = Net().to(device) |
| 61 | + model = Distributor(model) |
| 62 | + optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.5) |
| 63 | + |
| 64 | + # [4] Setup FashionMNIST dataloader and distribute data across PyTorchJob workers. |
| 65 | + dataset = datasets.FashionMNIST( |
| 66 | + "./data", |
| 67 | + download=True, |
| 68 | + train=True, |
| 69 | + transform=transforms.Compose([transforms.ToTensor()]), |
| 70 | + ) |
| 71 | + train_loader = torch.utils.data.DataLoader( |
| 72 | + dataset=dataset, |
| 73 | + batch_size=128, |
| 74 | + sampler=DistributedSampler(dataset), |
| 75 | + ) |
| 76 | + |
| 77 | + # [5] Start model Training. |
| 78 | + for epoch in range(3): |
| 79 | + for batch_idx, (data, target) in enumerate(train_loader): |
| 80 | + # Attach Tensors to the device. |
| 81 | + data = data.to(device) |
| 82 | + target = target.to(device) |
| 83 | + |
| 84 | + optimizer.zero_grad() |
| 85 | + output = model(data) |
| 86 | + loss = F.nll_loss(output, target) |
| 87 | + loss.backward() |
| 88 | + optimizer.step() |
| 89 | + if batch_idx % 10 == 0: |
| 90 | + print( |
| 91 | + "Train Epoch: {} [{}/{} ({:.0f}%)]\tloss={:.4f}".format( |
| 92 | + epoch, |
| 93 | + batch_idx * len(data), |
| 94 | + len(train_loader.dataset), |
| 95 | + 100.0 * batch_idx / len(train_loader), |
| 96 | + loss.item(), |
| 97 | + ) |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +from kubeflow.training import TrainingClient |
| 102 | + |
| 103 | +# Start PyTorchJob with 3 Workers and 1 GPUs per Worker. |
| 104 | +TrainingClient().create_job( |
| 105 | + name="pytorch-ddp", |
| 106 | + train_func=train_func, |
| 107 | + num_workers=3, |
| 108 | + resources_per_worker={"gpu": "1"}, |
| 109 | +) |
| 110 | +``` |
| 111 | + |
| 112 | +## Getting Started with TFJob |
| 113 | + |
| 114 | +Similar to PyTorchJob example, you can use the Python SDK to create your first distributed |
| 115 | +TensorFlow job. Run the following script to create TFJob with pre-created Docker image: |
| 116 | +`docker.io/kubeflow/tf-mnist-with-summaries:latest` that contains |
| 117 | +[distributed TensorFlow code](https://github.com/kubeflow/training-operator/tree/e6b4300f9dfebb5c2a3269641c828add367688ee/examples/tensorflow/mnist_with_summaries): |
| 118 | + |
| 119 | +```python |
| 120 | +from kubeflow.training import TrainingClient |
| 121 | + |
| 122 | +TrainingClient().create_job( |
| 123 | + name="tensorflow-dist", |
| 124 | + job_kind="TFJob", |
| 125 | + base_image="docker.io/kubeflow/tf-mnist-with-summaries:latest", |
| 126 | + num_workers=3, |
| 127 | +) |
| 128 | +``` |
| 129 | + |
| 130 | +Run the following API to get logs from your TFJob: |
| 131 | + |
| 132 | +```python |
| 133 | +TrainingClient().get_job_logs( |
| 134 | + name="tensorflow-dist", |
| 135 | + job_kind="TFJob", |
| 136 | + follow=True, |
| 137 | +) |
| 138 | +``` |
| 139 | + |
| 140 | +## Next steps |
| 141 | + |
| 142 | +- Run [FashionMNIST example](https://github.com/kubeflow/training-operator/blob/7345e33b333ba5084127efe027774dd7bed8f6e6/examples/pytorch/image-classification/Train-CNN-with-FashionMNIST.ipynb) with using Training Operator Python SDK. |
| 143 | + |
| 144 | +- Learn more about [the PyTorchJob APIs](/docs/components/training/user-guides/pytorch/). |
0 commit comments