Skip to content

Commit bcac9f5

Browse files
committed
fix
Signed-off-by: Max Pumperla <max.pumperla@googlemail.com>
1 parent c7bbc74 commit bcac9f5

File tree

5 files changed

+204
-12
lines changed

5 files changed

+204
-12
lines changed

_toc.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ parts:
88
- file: courses/00_Developer_Intro_to_Ray/output/00_Intro_Ray_Core_Basics_03.ipynb
99
- file: courses/00_Developer_Intro_to_Ray/output/00_Intro_Ray_Core_Basics_04.ipynb
1010
- file: courses/00_Developer_Intro_to_Ray/output/00_Intro_Ray_Core_Basics_05.ipynb
11-
- file: courses/00_Developer_Intro_to_Ray/output/00_Intro_Ray_Core_Basics_06.ipynb
1211
- file: courses/00_Developer_Intro_to_Ray/output/00a_Intro_Ray_Core_Advancement_01.ipynb
1312
- file: courses/00_Developer_Intro_to_Ray/output/00a_Intro_Ray_Core_Advancement_02.ipynb
1413
- file: courses/00_Developer_Intro_to_Ray/output/00a_Intro_Ray_Core_Advancement_03.ipynb

courses/00_Developer_Intro_to_Ray/00_Intro_Ray_Core_Basics.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@
246246
"id": "cc911f58",
247247
"metadata": {},
248248
"source": [
249-
"## 3. Getting Results"
249+
"### 3. Getting Results"
250250
]
251251
},
252252
{

courses/00_Developer_Intro_to_Ray/output/00_Intro_Ray_Core_Basics_04.ipynb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,34 @@
6969
"ref = remote_add.remote(1, 2)\n",
7070
"ref"
7171
]
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"id": "cc911f58",
76+
"metadata": {},
77+
"source": [
78+
"### 3. Getting Results"
79+
]
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"id": "7928ca98-dc51-4ecf-b757-92996dd0c69a",
84+
"metadata": {},
85+
"source": [
86+
"If we want to wait (block) and retrieve the corresponding object, we can use `ray.get`"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"id": "a564c830-d30d-4d4c-adb5-ee12adee605b",
93+
"metadata": {
94+
"tags": []
95+
},
96+
"outputs": [],
97+
"source": [
98+
"ray.get(ref)"
99+
]
72100
}
73101
],
74102
"metadata": {

courses/00_Developer_Intro_to_Ray/output/00_Intro_Ray_Core_Basics_05.ipynb

Lines changed: 175 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,196 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "cc911f58",
5+
"id": "d550f58a",
66
"metadata": {},
77
"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"
914
]
1015
},
1116
{
1217
"cell_type": "markdown",
13-
"id": "7928ca98-dc51-4ecf-b757-92996dd0c69a",
18+
"id": "4da412f5-133a-441b-8734-b96f56389f05",
1419
"metadata": {},
1520
"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>"
1742
]
1843
},
1944
{
2045
"cell_type": "code",
2146
"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": {},
26148
"outputs": [],
27149
"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"
29195
]
30196
}
31197
],

index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ This collection contains multiple courses on Ray and Anyscale:
99
- [00 Intro Ray Core Basics 03](courses/00_Developer_Intro_to_Ray/output/00_Intro_Ray_Core_Basics_03.ipynb)
1010
- [00 Intro Ray Core Basics 04](courses/00_Developer_Intro_to_Ray/output/00_Intro_Ray_Core_Basics_04.ipynb)
1111
- [00 Intro Ray Core Basics 05](courses/00_Developer_Intro_to_Ray/output/00_Intro_Ray_Core_Basics_05.ipynb)
12-
- [00 Intro Ray Core Basics 06](courses/00_Developer_Intro_to_Ray/output/00_Intro_Ray_Core_Basics_06.ipynb)
1312
- [00a Intro Ray Core Advancement 01](courses/00_Developer_Intro_to_Ray/output/00a_Intro_Ray_Core_Advancement_01.ipynb)
1413
- [00a Intro Ray Core Advancement 02](courses/00_Developer_Intro_to_Ray/output/00a_Intro_Ray_Core_Advancement_02.ipynb)
1514
- [00a Intro Ray Core Advancement 03](courses/00_Developer_Intro_to_Ray/output/00a_Intro_Ray_Core_Advancement_03.ipynb)

0 commit comments

Comments
 (0)