|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "66fe6c71-2cb2-4425-a12a-c5b531a28155", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Using KubeRay to run Distributed Workloads without CodeFlare\n", |
| 9 | + "\n", |
| 10 | + "This notebook demonstrates a quick workflow using Ray from a notebook without the codeflare-sdk.\n", |
| 11 | + "The current usage patterns for KubeRay require manual oc commands to be run from your notebook, so you will need to authenticate manually. We recommend usage of the codeflare-sdk alongside CodeFlare for an easier experience. An example notebook showing an almost identical usecase can be found at https://github.com/project-codeflare/codeflare-sdk/blob/main/demo-notebooks/guided-demos/3_basic_interactive.ipynb" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": null, |
| 17 | + "id": "163b9d63-709e-435e-9933-988328831eba", |
| 18 | + "metadata": {}, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "!pip install --upgrade ray==\"2.5.0\"\n", |
| 22 | + "!pip install pandas" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "id": "4b436207-9d87-4ce1-8909-6116e729a753", |
| 28 | + "metadata": { |
| 29 | + "tags": [] |
| 30 | + }, |
| 31 | + "source": [ |
| 32 | + "## You need to get your token to authenticate to the OpenShift Cluster.\n", |
| 33 | + "\n", |
| 34 | + "1. Go to the OpenShift Console\n", |
| 35 | + "2. Click on the arrow next to your username\n", |
| 36 | + "3. Click on \"Copy login command\"\n", |
| 37 | + "4. Once authenticated, copy the entire section under \"Log in with this token. It will look similar to the following\n", |
| 38 | + "oc login --token=<token> --server=<url>\n", |
| 39 | + "5. Run the following cell, making sure to use your token and server. The \"!\" at the beginning of the command is required." |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": null, |
| 45 | + "id": "7ea2c953-a9b7-4f19-941b-517a832ff379", |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "!oc login --token=<token> --server=<url>" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 13, |
| 55 | + "id": "88eb9d0a-846d-43ce-8730-020ac05cd4e9", |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [ |
| 58 | + { |
| 59 | + "name": "stdout", |
| 60 | + "output_type": "stream", |
| 61 | + "text": [ |
| 62 | + "raycluster.ray.io \"imdb-ray-test\" deleted\n", |
| 63 | + "raycluster.ray.io/imdb-ray-test created\n" |
| 64 | + ] |
| 65 | + } |
| 66 | + ], |
| 67 | + "source": [ |
| 68 | + "!oc delete -f test.yaml\n", |
| 69 | + "!oc apply -f test.yaml" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": null, |
| 75 | + "id": "cb671ebf-7317-4ae2-bb65-81b16b1f78e5", |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [], |
| 78 | + "source": [ |
| 79 | + "!oc get pods -o wide | grep imdb-ray-test | awk '{print $1, $6, $7 }'" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "markdown", |
| 84 | + "id": "0e8952d2-1633-4094-903c-a422b96ffbf5", |
| 85 | + "metadata": { |
| 86 | + "tags": [] |
| 87 | + }, |
| 88 | + "source": [ |
| 89 | + "As you can see from the above output, we have 2 worker nodes and a head node for the ray cluster. Each node has a separate IP address and different physical node it has been scheduled on." |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": null, |
| 95 | + "id": "d53c20b0-19b4-437d-9694-174a6d443426", |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [], |
| 98 | + "source": [ |
| 99 | + "!oc get svc | grep imdb-ray-test" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": null, |
| 105 | + "id": "11768c18-20c8-407c-9b4a-320264a0b8c5", |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "import ray\n", |
| 110 | + "from ray.air.config import ScalingConfig\n", |
| 111 | + "\n", |
| 112 | + "# Copy the service name from above. If you are using the default service and namespace,\n", |
| 113 | + "# the ray_cluster_uri is ray://imdb-ray-test-head-svc.opendatahub.svc:10001\n", |
| 114 | + "\n", |
| 115 | + "ray_cluster_uri = \"ray://imdb-ray-test-head-svc.opendatahub.svc:10001\"\n", |
| 116 | + "\n", |
| 117 | + "#install additional libraries that will be required for model training\n", |
| 118 | + "runtime_env = {\"pip\": [\"transformers\", \"datasets\", \"evaluate\", \"pyarrow<7.0.0\", \"accelerate\"]}\n", |
| 119 | + "\n", |
| 120 | + "# NOTE: This will work for in-cluster notebook servers (RHODS/ODH), but not for local machines\n", |
| 121 | + "# To see how to connect from your laptop, go to demo-notebooks/additional-demos/local_interactive.ipynb\n", |
| 122 | + "ray.init(address=ray_cluster_uri, runtime_env=runtime_env)\n", |
| 123 | + "\n", |
| 124 | + "print(\"Ray cluster is up and running: \", ray.is_initialized())" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": null, |
| 130 | + "id": "566eba0c-6be2-4cb4-9aa4-9e147433642e", |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [], |
| 133 | + "source": [ |
| 134 | + "@ray.remote\n", |
| 135 | + "def train_fn():\n", |
| 136 | + " from datasets import load_dataset\n", |
| 137 | + " import transformers\n", |
| 138 | + " from transformers import AutoTokenizer, TrainingArguments\n", |
| 139 | + " from transformers import AutoModelForSequenceClassification\n", |
| 140 | + " import numpy as np\n", |
| 141 | + " from datasets import load_metric\n", |
| 142 | + " import ray\n", |
| 143 | + " from ray import tune\n", |
| 144 | + " from ray.train.huggingface import HuggingFaceTrainer\n", |
| 145 | + "\n", |
| 146 | + " dataset = load_dataset(\"imdb\")\n", |
| 147 | + " tokenizer = AutoTokenizer.from_pretrained(\"distilbert-base-uncased\")\n", |
| 148 | + "\n", |
| 149 | + " def tokenize_function(examples):\n", |
| 150 | + " return tokenizer(examples[\"text\"], padding=\"max_length\", truncation=True)\n", |
| 151 | + "\n", |
| 152 | + " tokenized_datasets = dataset.map(tokenize_function, batched=True)\n", |
| 153 | + "\n", |
| 154 | + " #using a fraction of dataset but you can run with the full dataset\n", |
| 155 | + " small_train_dataset = tokenized_datasets[\"train\"].shuffle(seed=42).select(range(100))\n", |
| 156 | + " small_eval_dataset = tokenized_datasets[\"test\"].shuffle(seed=42).select(range(100))\n", |
| 157 | + "\n", |
| 158 | + " print(f\"len of train {small_train_dataset} and test {small_eval_dataset}\")\n", |
| 159 | + "\n", |
| 160 | + " ray_train_ds = ray.data.from_huggingface(small_train_dataset)\n", |
| 161 | + " ray_evaluation_ds = ray.data.from_huggingface(small_eval_dataset)\n", |
| 162 | + "\n", |
| 163 | + " def compute_metrics(eval_pred):\n", |
| 164 | + " metric = load_metric(\"accuracy\")\n", |
| 165 | + " logits, labels = eval_pred\n", |
| 166 | + " predictions = np.argmax(logits, axis=-1)\n", |
| 167 | + " return metric.compute(predictions=predictions, references=labels)\n", |
| 168 | + "\n", |
| 169 | + " def trainer_init_per_worker(train_dataset, eval_dataset, **config):\n", |
| 170 | + " model = AutoModelForSequenceClassification.from_pretrained(\"distilbert-base-uncased\", num_labels=2)\n", |
| 171 | + "\n", |
| 172 | + " training_args = TrainingArguments(\"/tmp/hf_imdb/test\", eval_steps=1, disable_tqdm=True, \n", |
| 173 | + " num_train_epochs=1, skip_memory_metrics=True,\n", |
| 174 | + " learning_rate=2e-5,\n", |
| 175 | + " per_device_train_batch_size=16,\n", |
| 176 | + " per_device_eval_batch_size=16, \n", |
| 177 | + " weight_decay=0.01,)\n", |
| 178 | + " return transformers.Trainer(\n", |
| 179 | + " model=model,\n", |
| 180 | + " args=training_args,\n", |
| 181 | + " train_dataset=train_dataset,\n", |
| 182 | + " eval_dataset=eval_dataset,\n", |
| 183 | + " compute_metrics=compute_metrics\n", |
| 184 | + " )\n", |
| 185 | + "\n", |
| 186 | + " scaling_config = ScalingConfig(num_workers=2, use_gpu=False) #num workers is the number of gpus\n", |
| 187 | + "\n", |
| 188 | + " # we are using the ray native HuggingFaceTrainer, but you can swap out to use non ray Huggingface Trainer. Both have the same method signature. \n", |
| 189 | + " # the ray native HFTrainer has built in support for scaling to multiple GPUs\n", |
| 190 | + " trainer = HuggingFaceTrainer(\n", |
| 191 | + " trainer_init_per_worker=trainer_init_per_worker,\n", |
| 192 | + " scaling_config=scaling_config,\n", |
| 193 | + " datasets={\"train\": ray_train_ds, \"evaluation\": ray_evaluation_ds},\n", |
| 194 | + " )\n", |
| 195 | + " result = trainer.fit()" |
| 196 | + ] |
| 197 | + }, |
| 198 | + { |
| 199 | + "cell_type": "code", |
| 200 | + "execution_count": null, |
| 201 | + "id": "c4458b86-8699-44b7-9785-d8ae8d0e29d4", |
| 202 | + "metadata": {}, |
| 203 | + "outputs": [], |
| 204 | + "source": [ |
| 205 | + "ray.get(train_fn.remote())\n" |
| 206 | + ] |
| 207 | + }, |
| 208 | + { |
| 209 | + "cell_type": "code", |
| 210 | + "execution_count": null, |
| 211 | + "id": "2a774454-6484-44a3-b8a0-6affd9f93d9b", |
| 212 | + "metadata": {}, |
| 213 | + "outputs": [], |
| 214 | + "source": [] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "code", |
| 218 | + "execution_count": null, |
| 219 | + "id": "a525ec28-c485-43f5-afc1-155de4ed4149", |
| 220 | + "metadata": {}, |
| 221 | + "outputs": [], |
| 222 | + "source": [ |
| 223 | + "ray.cancel(ref)\n", |
| 224 | + "ray.shutdown()" |
| 225 | + ] |
| 226 | + }, |
| 227 | + { |
| 228 | + "cell_type": "code", |
| 229 | + "execution_count": null, |
| 230 | + "id": "00fe92e0-abb8-47d5-8a6d-b49c78bd230c", |
| 231 | + "metadata": {}, |
| 232 | + "outputs": [], |
| 233 | + "source": [ |
| 234 | + "!oc delete -f test.yaml" |
| 235 | + ] |
| 236 | + }, |
| 237 | + { |
| 238 | + "cell_type": "code", |
| 239 | + "execution_count": null, |
| 240 | + "id": "f551aa4e-d405-49f9-90d3-9780e8f397df", |
| 241 | + "metadata": {}, |
| 242 | + "outputs": [], |
| 243 | + "source": [ |
| 244 | + "\n", |
| 245 | + "Go to ray dashboard and see the logs and stuff in there.\n", |
| 246 | + "\n", |
| 247 | + "Add a bit at the end for ray dashboard.\n" |
| 248 | + ] |
| 249 | + }, |
| 250 | + { |
| 251 | + "cell_type": "code", |
| 252 | + "execution_count": null, |
| 253 | + "id": "2f402fba-1659-4a4a-ae27-c3d8ca3f1e98", |
| 254 | + "metadata": {}, |
| 255 | + "outputs": [], |
| 256 | + "source": [] |
| 257 | + } |
| 258 | + ], |
| 259 | + "metadata": { |
| 260 | + "kernelspec": { |
| 261 | + "display_name": "Python 3", |
| 262 | + "language": "python", |
| 263 | + "name": "python3" |
| 264 | + }, |
| 265 | + "language_info": { |
| 266 | + "codemirror_mode": { |
| 267 | + "name": "ipython", |
| 268 | + "version": 3 |
| 269 | + }, |
| 270 | + "file_extension": ".py", |
| 271 | + "mimetype": "text/x-python", |
| 272 | + "name": "python", |
| 273 | + "nbconvert_exporter": "python", |
| 274 | + "pygments_lexer": "ipython3", |
| 275 | + "version": "3.8.13" |
| 276 | + } |
| 277 | + }, |
| 278 | + "nbformat": 4, |
| 279 | + "nbformat_minor": 5 |
| 280 | +} |
0 commit comments