|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "markdown", |
5 | | - "id": "cc911f58", |
| 5 | + "id": "d550f58a", |
6 | 6 | "metadata": {}, |
7 | 7 | "source": [ |
8 | | - "## 3. Getting Results" |
| 8 | + "## 4. Putting It All Together\n", |
| 9 | + "\n", |
| 10 | + "Here are the three steps:\n", |
| 11 | + "1. Create the remote function\n", |
| 12 | + "2. Execute it remotely\n", |
| 13 | + "3. Get the result when needed\n" |
9 | 14 | ] |
10 | 15 | }, |
11 | 16 | { |
12 | 17 | "cell_type": "markdown", |
13 | | - "id": "7928ca98-dc51-4ecf-b757-92996dd0c69a", |
| 18 | + "id": "4da412f5-133a-441b-8734-b96f56389f05", |
14 | 19 | "metadata": {}, |
15 | 20 | "source": [ |
16 | | - "If we want to wait (block) and retrieve the corresponding object, we can use `ray.get`" |
| 21 | + "<div class=\"alert alert-block alert-info\">\n", |
| 22 | + " \n", |
| 23 | + "__Activity: define and invoke a Ray task__\n", |
| 24 | + "\n", |
| 25 | + "Define a remote function `sqrt_add` that accepts two arguments and performs the following steps:\n", |
| 26 | + "1. computes the square-root of the first\n", |
| 27 | + "2. adds the second\n", |
| 28 | + "3. returns the result\n", |
| 29 | + "\n", |
| 30 | + "Execute it with 2 different sets of parameters and collect the results\n", |
| 31 | + "\n", |
| 32 | + "```python\n", |
| 33 | + "# Hint: define the below as a remote function\n", |
| 34 | + "def sqrt_add(a, b):\n", |
| 35 | + " ... \n", |
| 36 | + "\n", |
| 37 | + "# Hint: invoke it as a remote task and collect the results\n", |
| 38 | + "```\n", |
| 39 | + "\n", |
| 40 | + "\n", |
| 41 | + "</div>" |
17 | 42 | ] |
18 | 43 | }, |
19 | 44 | { |
20 | 45 | "cell_type": "code", |
21 | 46 | "execution_count": null, |
22 | | - "id": "a564c830-d30d-4d4c-adb5-ee12adee605b", |
23 | | - "metadata": { |
24 | | - "tags": [] |
25 | | - }, |
| 47 | + "id": "ace32382", |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "# Write your solution here" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "id": "34fe7b54", |
| 57 | + "metadata": {}, |
| 58 | + "source": [ |
| 59 | + "<div class=\"alert alert-block alert-info\">\n", |
| 60 | + "\n", |
| 61 | + "<details>\n", |
| 62 | + "\n", |
| 63 | + "<summary> Click to see solution </summary>\n", |
| 64 | + "\n", |
| 65 | + "```python\n", |
| 66 | + "import math\n", |
| 67 | + "\n", |
| 68 | + "@ray.remote\n", |
| 69 | + "def sqrt_add(a, b):\n", |
| 70 | + " return math.sqrt(a) + b\n", |
| 71 | + "\n", |
| 72 | + "ray.get([sqrt_add.remote(2, 3), sqrt_add.remote(5, 4)])\n", |
| 73 | + "```\n", |
| 74 | + "\n", |
| 75 | + "</details>\n", |
| 76 | + "\n", |
| 77 | + "</div>\n" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "markdown", |
| 82 | + "id": "1ae13f94-7307-4a43-ad55-bfa8df9c6cdb", |
| 83 | + "metadata": {}, |
| 84 | + "source": [ |
| 85 | + "### 4.1. Note about Ray ID Specification" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "markdown", |
| 90 | + "id": "a6948957", |
| 91 | + "metadata": {}, |
| 92 | + "source": [ |
| 93 | + "IDs for tasks and objects are build according to the [ID specification in Ray](https://github.com/ray-project/ray/blob/master/src/ray/design_docs/id_specification.md)." |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "markdown", |
| 98 | + "id": "a84c4e70", |
| 99 | + "metadata": {}, |
| 100 | + "source": [ |
| 101 | + "### 4.2. Anti-pattern: Calling ray.get in a loop harms parallelism" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "markdown", |
| 106 | + "id": "09dbf506-7cd4-4408-b4b6-91b1defeef9e", |
| 107 | + "metadata": {}, |
| 108 | + "source": [ |
| 109 | + "|<img src=\"https://assets-training.s3.us-west-2.amazonaws.com/ray-core/ray-core/ray-get-in-a-loop.png\" width=\"70%\" loading=\"lazy\">|\n", |
| 110 | + "|:--|\n", |
| 111 | + "|ray.get() is a blocking call. Avoid calling it on every item (left panel). Calling only on the final result improves performance (right panel).|" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "markdown", |
| 116 | + "id": "daeed79b-fb6f-45c3-a3c1-2f501ff56241", |
| 117 | + "metadata": {}, |
| 118 | + "source": [ |
| 119 | + "When trying to collect results for multiple remote function invocations (tasks), don't block and wait for each one individually. Let's consider this remote function:" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": null, |
| 125 | + "id": "4322b5b1", |
| 126 | + "metadata": {}, |
| 127 | + "outputs": [], |
| 128 | + "source": [ |
| 129 | + "@ray.remote\n", |
| 130 | + "def expensive_square(x):\n", |
| 131 | + " time.sleep(5)\n", |
| 132 | + " return x**2" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "id": "0405e415-06da-462f-8d5c-f8b6f0a460c2", |
| 138 | + "metadata": {}, |
| 139 | + "source": [ |
| 140 | + "This implementation will block for each item in the loop:" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": null, |
| 146 | + "id": "907ed0b7", |
| 147 | + "metadata": {}, |
26 | 148 | "outputs": [], |
27 | 149 | "source": [ |
28 | | - "ray.get(ref)" |
| 150 | + "results = []\n", |
| 151 | + "for item in range(4):\n", |
| 152 | + " output = ray.get(expensive_square.remote(item))\n", |
| 153 | + " results.append(output)\n", |
| 154 | + "results" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "markdown", |
| 159 | + "id": "0abdcf63", |
| 160 | + "metadata": {}, |
| 161 | + "source": [ |
| 162 | + "Schedule all remote calls, which are then processed in parallel. After scheduling the work, we can then request all the results at once." |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "code", |
| 167 | + "execution_count": null, |
| 168 | + "id": "d7e498de", |
| 169 | + "metadata": {}, |
| 170 | + "outputs": [], |
| 171 | + "source": [ |
| 172 | + "refs = []\n", |
| 173 | + "for j in range(4):\n", |
| 174 | + " refs.append(expensive_square.remote(j))\n", |
| 175 | + "results = ray.get(refs)\n", |
| 176 | + "results" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "markdown", |
| 181 | + "id": "837090c0-cd19-4678-bca8-99270a9a20f7", |
| 182 | + "metadata": {}, |
| 183 | + "source": [ |
| 184 | + "<div class=\"alert alert-info\">\n", |
| 185 | + "Read more about this <strong><a href=\"https://docs.ray.io/en/latest/ray-core/patterns/ray-get-loop.html\" target=\"_blank\">anti-pattern</a></strong>.\n", |
| 186 | + "</div>" |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "markdown", |
| 191 | + "id": "642e3863", |
| 192 | + "metadata": {}, |
| 193 | + "source": [ |
| 194 | + "<!-- TODO: add Patterns/antipatterns based on above learnings-->\n" |
29 | 195 | ] |
30 | 196 | } |
31 | 197 | ], |
|
0 commit comments