Skip to content

Commit 64bb147

Browse files
juanjgalveznv-kriehl
authored andcommitted
Add initial nvmath-python distributed APIs notebook and infra
1 parent a24ffa7 commit 64bb147

3 files changed

Lines changed: 345 additions & 0 deletions

File tree

‎tutorials/nvmath-python/notebooks/07_ust.ipynb‎

100755100644
File mode changed.
Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "d4c3d2dd-afb0-4dfd-b071-7b500c6a46a1",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"%load_ext nbdistributed\n",
11+
"%run _mgmn_nb_setup.py"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"id": "c208c7b9-a8e6-494e-92bd-cb53ec9f8bc0",
17+
"metadata": {},
18+
"source": [
19+
"# Initializing nvmath.distributed with torch.distributed"
20+
]
21+
},
22+
{
23+
"cell_type": "markdown",
24+
"id": "d77e5b3c-f6fd-4726-85de-406135db021c",
25+
"metadata": {},
26+
"source": [
27+
"## 1) Initialize distributed workers with torch.distributed"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"id": "22012fc3-4782-4223-a058-a052c8984737",
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"assert not using_mpi\n",
38+
"\n",
39+
"# Initialize worker processes\n",
40+
"%dist_init --num-processes 2 --gpu-ids 0,1\n",
41+
"\n",
42+
"# disable distributed mode on cells without magic\n",
43+
"%dist_mode --disable\n",
44+
"\n",
45+
"using_torch_distributed = True"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": null,
51+
"id": "ee6f997f-5a6a-4dc4-8c85-6db836d1b0e9",
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"%dist_status"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"id": "f700e123-ed08-4a71-8af2-64897d3962b2",
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"%%mgmn\n",
66+
"\n",
67+
"print(\"hello world\")"
68+
]
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"id": "fd1e4476-bcfa-4672-b9cc-c5a7d708079a",
73+
"metadata": {},
74+
"source": [
75+
"## 2) Initialize nvmath.distributed runtime\n",
76+
"\n",
77+
"This code runs on every process."
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": null,
83+
"id": "0537eae1-5546-4da9-9945-6bcc876c9880",
84+
"metadata": {},
85+
"outputs": [],
86+
"source": [
87+
"%%mgmn\n",
88+
"\n",
89+
"import torch\n",
90+
"\n",
91+
"import nvmath.distributed\n",
92+
"from nvmath.distributed.process_group import TorchProcessGroup\n",
93+
"\n",
94+
"device_id = rank % torch.cuda.device_count()\n",
95+
"process_group = TorchProcessGroup(device_id=device_id)\n",
96+
"nvmath.distributed.initialize(device_id, process_group, backends=[\"nccl\"])\n",
97+
"print(\"nvmath.distributed initialized\")"
98+
]
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"id": "ef1c2a5c-6183-4d1b-b20d-d57f04821ee0",
103+
"metadata": {},
104+
"source": [
105+
"## Finalize distributed runtime"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": null,
111+
"id": "c127c59e-c24b-4f2f-acea-efe1acce3056",
112+
"metadata": {},
113+
"outputs": [],
114+
"source": [
115+
"%%mgmn\n",
116+
"\n",
117+
"import nvmath.distributed\n",
118+
"nvmath.distributed.finalize()"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": null,
124+
"id": "ca25d913-d766-4553-a0d0-9f4d6ac064f4",
125+
"metadata": {},
126+
"outputs": [],
127+
"source": [
128+
"%dist_shutdown\n",
129+
"\n",
130+
"using_torch_distributed = False"
131+
]
132+
},
133+
{
134+
"cell_type": "markdown",
135+
"id": "744fccac-421b-4d01-b13c-b1edf56d13df",
136+
"metadata": {},
137+
"source": [
138+
"# Initializing nvmath.distributed with MPI"
139+
]
140+
},
141+
{
142+
"cell_type": "markdown",
143+
"id": "b53cdd3b-c38a-4b54-9e5d-d4ea89f64819",
144+
"metadata": {},
145+
"source": [
146+
"## 1) Initialize distributed workers with ipyparallel and MPI"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": null,
152+
"id": "ec9c3ec3-f754-4b44-b742-bb3202034dc4",
153+
"metadata": {},
154+
"outputs": [],
155+
"source": [
156+
"import ipyparallel as ipp\n",
157+
"\n",
158+
"assert not using_torch_distributed\n",
159+
"\n",
160+
"cluster = ipp.Cluster(engines=\"mpi\", n=2)\n",
161+
"\n",
162+
"using_mpi = True\n",
163+
"\n",
164+
"# Start the cluster, block until ready, and connect to it\n",
165+
"rc = cluster.start_and_connect_sync()\n",
166+
"rc.activate()"
167+
]
168+
},
169+
{
170+
"cell_type": "markdown",
171+
"id": "f3ed45c7-33f1-425a-8325-25d13b5e5107",
172+
"metadata": {},
173+
"source": [
174+
"## 2) Initialize nvmath.distributed runtime\n",
175+
"\n",
176+
"This code runs on every process."
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": null,
182+
"id": "532d63e6-7393-4a4c-92ba-12653c114903",
183+
"metadata": {},
184+
"outputs": [],
185+
"source": [
186+
"%%mgmn\n",
187+
"\n",
188+
"from cuda.core import system\n",
189+
"from mpi4py import MPI\n",
190+
"\n",
191+
"import nvmath.distributed\n",
192+
"from nvmath.distributed.process_group import MPIProcessGroup\n",
193+
"\n",
194+
"comm = MPI.COMM_WORLD\n",
195+
"rank = comm.Get_rank()\n",
196+
"device_id = rank % system.get_num_devices()\n",
197+
"process_group = MPIProcessGroup(comm)\n",
198+
"nvmath.distributed.initialize(device_id, process_group, backends=[\"nccl\"])\n",
199+
"print(\"nvmath.distributed initialized\")"
200+
]
201+
},
202+
{
203+
"cell_type": "markdown",
204+
"id": "b6e0f19f-107e-42f2-a9f2-637089911ed9",
205+
"metadata": {},
206+
"source": [
207+
"## Finalize distributed runtime"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": null,
213+
"id": "5b4901ae-e436-4a00-b636-f439f2a49ded",
214+
"metadata": {},
215+
"outputs": [],
216+
"source": [
217+
"%%mgmn\n",
218+
"\n",
219+
"import nvmath.distributed\n",
220+
"nvmath.distributed.finalize()"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": null,
226+
"id": "21c5bc76-a36c-41b5-8259-ec1b018f47e5",
227+
"metadata": {},
228+
"outputs": [],
229+
"source": [
230+
"rc.shutdown(hub=True)\n",
231+
"\n",
232+
"using_mpi = False"
233+
]
234+
},
235+
{
236+
"cell_type": "markdown",
237+
"id": "eef889d8-ea0b-4628-9cac-f77865a09460",
238+
"metadata": {},
239+
"source": [
240+
"# Distributed matrix multiplication"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": null,
246+
"id": "5d99f7bd-a443-40d3-a94c-a664e2868c64",
247+
"metadata": {},
248+
"outputs": [],
249+
"source": [
250+
"%%mgmn\n",
251+
"\n",
252+
"import torch\n",
253+
"import numpy as np\n",
254+
"from nvmath.distributed.distribution import Slab\n",
255+
"from nvmath.distributed.linalg.advanced import matrix_qualifiers_dtype\n",
256+
"\n",
257+
"ctx = nvmath.distributed.get_context()\n",
258+
"process_group = ctx.process_group\n",
259+
"\n",
260+
"# The global problem size m, n, k\n",
261+
"m, n, k = 256, 512, 256\n",
262+
"\n",
263+
"row_wise_distribution = Slab.X\n",
264+
"col_wise_distribution = Slab.Y\n",
265+
"\n",
266+
"# Get my local shape\n",
267+
"a_shape = col_wise_distribution.shape(process_group.rank, (m, k))\n",
268+
"b_shape = col_wise_distribution.shape(process_group.rank, (n, k))\n",
269+
"a = torch.rand(*a_shape, device=f\"cuda:{ctx.device_id}\")\n",
270+
"b = torch.rand(*b_shape, device=f\"cuda:{ctx.device_id}\")\n",
271+
"\n",
272+
"# Get a transposed view to obtain column-major memory layout. Note that this\n",
273+
"# also changes the distribution of a and b.\n",
274+
"a = a.T # a is now (k, m) with row_wise_distribution\n",
275+
"b = b.T # b is now (k, n) with row_wise_distribution\n",
276+
"\n",
277+
"# Distribution of a, b and output.\n",
278+
"distributions = [row_wise_distribution, row_wise_distribution, col_wise_distribution]\n",
279+
"\n",
280+
"qualifiers = np.zeros((3,), dtype=matrix_qualifiers_dtype)\n",
281+
"qualifiers[0][\"is_transpose\"] = True # a is transposed\n",
282+
"\n",
283+
"# Use the stateful object as a context manager to automatically release resources.\n",
284+
"result = nvmath.distributed.linalg.advanced.matmul(a, b, distributions=distributions, qualifiers=qualifiers)\n"
285+
]
286+
},
287+
{
288+
"cell_type": "code",
289+
"execution_count": null,
290+
"id": "68af6e04-ee34-493c-b9d3-34c4b65d66d3",
291+
"metadata": {},
292+
"outputs": [],
293+
"source": [
294+
"%%mgmn\n",
295+
"\n",
296+
"result, result.shape"
297+
]
298+
}
299+
],
300+
"metadata": {
301+
"accelerator": "GPU",
302+
"colab": {
303+
"gpuType": "T4",
304+
"provenance": [],
305+
"toc_visible": true
306+
},
307+
"kernelspec": {
308+
"display_name": "Python 3 (ipykernel)",
309+
"language": "python",
310+
"name": "python3"
311+
},
312+
"language_info": {
313+
"codemirror_mode": {
314+
"name": "ipython",
315+
"version": 3
316+
},
317+
"file_extension": ".py",
318+
"mimetype": "text/x-python",
319+
"name": "python",
320+
"nbconvert_exporter": "python",
321+
"pygments_lexer": "ipython3",
322+
"version": "3.11.7"
323+
}
324+
},
325+
"nbformat": 4,
326+
"nbformat_minor": 5
327+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import Union, Literal # to suppress irrelevant warnings from nbdistributed
2+
3+
from IPython.core.magic import register_cell_magic, needs_local_scope
4+
from IPython import get_ipython
5+
6+
@register_cell_magic
7+
@needs_local_scope
8+
def mgmn(line, cell, local_ns=None):
9+
ipython = get_ipython()
10+
if local_ns and local_ns["using_mpi"]:
11+
ipython.run_cell_magic("px", "", cell)
12+
elif local_ns and local_ns["using_torch_distributed"]:
13+
ipython.run_cell_magic("distributed", "", cell)
14+
else:
15+
print("No worker processes running")
16+
17+
using_torch_distributed = False
18+
using_mpi = False

0 commit comments

Comments
 (0)