-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathtest_explicit_comms.py
293 lines (241 loc) · 9.41 KB
/
test_explicit_comms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import multiprocessing as mp
import numpy as np
import pandas as pd
import pytest
import dask
from dask import dataframe as dd
from dask.dataframe.shuffle import partitioning_index
from distributed import Client
from distributed.deploy.local import LocalCluster
import dask_cuda
from dask_cuda.explicit_comms import comms
from dask_cuda.explicit_comms.dataframe.merge import merge as explicit_comms_merge
from dask_cuda.explicit_comms.dataframe.shuffle import shuffle as explicit_comms_shuffle
mp = mp.get_context("spawn")
ucp = pytest.importorskip("ucp")
# Notice, all of the following tests is executed in a new process such
# that UCX options of the different tests doesn't conflict.
async def my_rank(state):
return state["rank"]
def _test_local_cluster(protocol):
with LocalCluster(
protocol=protocol,
dashboard_address=None,
n_workers=4,
threads_per_worker=1,
processes=True,
) as cluster:
with Client(cluster) as client:
c = comms.CommsContext(client)
assert sum(c.run(my_rank)) == sum(range(4))
@pytest.mark.parametrize("protocol", ["tcp", "ucx"])
def test_local_cluster(protocol):
p = mp.Process(target=_test_local_cluster, args=(protocol,))
p.start()
p.join()
assert not p.exitcode
def _test_dataframe_merge(backend, protocol, n_workers):
if backend == "cudf":
cudf = pytest.importorskip("cudf")
from cudf.tests.utils import assert_eq
else:
from dask.dataframe.utils import assert_eq
dask.config.update(
dask.config.global_config,
{"ucx": {"TLS": "tcp,sockcm,cuda_copy",},},
priority="new",
)
with LocalCluster(
protocol=protocol,
dashboard_address=None,
n_workers=n_workers,
threads_per_worker=1,
processes=True,
) as cluster:
with Client(cluster):
nrows = n_workers * 10
# Let's make some dataframes that we can join on the "key" column
df1 = pd.DataFrame({"key": np.arange(nrows), "payload1": np.arange(nrows)})
key = np.arange(nrows)
np.random.shuffle(key)
df2 = pd.DataFrame(
{"key": key[nrows // 3 :], "payload2": np.arange(nrows)[nrows // 3 :]}
)
expected = df1.merge(df2).set_index("key")
if backend == "cudf":
df1 = cudf.DataFrame.from_pandas(df1)
df2 = cudf.DataFrame.from_pandas(df2)
ddf1 = dd.from_pandas(df1, npartitions=n_workers + 1)
ddf2 = dd.from_pandas(
df2, npartitions=n_workers - 1 if n_workers > 1 else 1
)
ddf3 = explicit_comms_merge(ddf1, ddf2, on="key").set_index("key")
got = ddf3.compute()
if backend == "cudf":
assert_eq(got, expected)
else:
pd.testing.assert_frame_equal(got, expected)
@pytest.mark.parametrize("nworkers", [1, 2, 4])
@pytest.mark.parametrize("backend", ["pandas", "cudf"])
@pytest.mark.parametrize("protocol", ["tcp", "ucx"])
def test_dataframe_merge(backend, protocol, nworkers):
if backend == "cudf":
pytest.importorskip("cudf")
p = mp.Process(target=_test_dataframe_merge, args=(backend, protocol, nworkers))
p.start()
p.join()
assert not p.exitcode
def _test_dataframe_merge_empty_partitions(nrows, npartitions):
with LocalCluster(
protocol="tcp",
dashboard_address=None,
n_workers=npartitions,
threads_per_worker=1,
processes=True,
) as cluster:
with Client(cluster):
df1 = pd.DataFrame({"key": np.arange(nrows), "payload1": np.arange(nrows)})
key = np.arange(nrows)
np.random.shuffle(key)
df2 = pd.DataFrame({"key": key, "payload2": np.arange(nrows)})
expected = df1.merge(df2).set_index("key")
ddf1 = dd.from_pandas(df1, npartitions=npartitions)
ddf2 = dd.from_pandas(df2, npartitions=npartitions)
ddf3 = explicit_comms_merge(ddf1, ddf2, on=["key"]).set_index("key")
got = ddf3.compute()
pd.testing.assert_frame_equal(got, expected)
def test_dataframe_merge_empty_partitions():
# Notice, we use more partitions than rows
p = mp.Process(target=_test_dataframe_merge_empty_partitions, args=(2, 4))
p.start()
p.join()
assert not p.exitcode
def check_partitions(df, npartitions):
"""Check that all values in `df` hashes to the same"""
hashes = partitioning_index(df, npartitions)
if len(hashes) > 0:
return len(hashes.unique()) == 1
else:
return True
def _test_dataframe_shuffle(backend, protocol, n_workers):
if backend == "cudf":
cudf = pytest.importorskip("cudf")
from cudf.tests.utils import assert_eq
else:
from dask.dataframe.utils import assert_eq
dask.config.update(
dask.config.global_config,
{"ucx": {"TLS": "tcp,sockcm,cuda_copy",},},
priority="new",
)
with LocalCluster(
protocol=protocol,
dashboard_address=None,
n_workers=n_workers,
threads_per_worker=1,
processes=True,
) as cluster:
with Client(cluster) as client:
all_workers = list(client.get_worker_logs().keys())
comms.default_comms()
np.random.seed(42)
df = pd.DataFrame({"key": np.random.random(100)})
if backend == "cudf":
df = cudf.DataFrame.from_pandas(df)
for input_nparts in range(1, 5):
for output_nparts in range(1, 5):
ddf = dd.from_pandas(df.copy(), npartitions=input_nparts).persist(
workers=all_workers
)
ddf = explicit_comms_shuffle(
ddf, ["key"], npartitions=output_nparts
).persist()
assert ddf.npartitions == output_nparts
# Check that each partition of `ddf` hashes to the same value
result = ddf.map_partitions(
check_partitions, output_nparts
).compute()
assert all(result.to_list())
# Check the values of `ddf` (ignoring the row order)
expected = df.sort_values("key")
got = ddf.compute().sort_values("key")
if backend == "cudf":
assert_eq(got, expected)
else:
pd.testing.assert_frame_equal(got, expected)
@pytest.mark.parametrize("nworkers", [1, 2, 3])
@pytest.mark.parametrize("backend", ["pandas", "cudf"])
@pytest.mark.parametrize("protocol", ["tcp", "ucx"])
def test_dataframe_shuffle(backend, protocol, nworkers):
if backend == "cudf":
pytest.importorskip("cudf")
p = mp.Process(target=_test_dataframe_shuffle, args=(backend, protocol, nworkers))
p.start()
p.join()
assert not p.exitcode
def _test_dask_use_explicit_comms():
def check_shuffle(in_cluster):
"""Check if shuffle use explicit-comms by search for keys named "shuffle"
The explicit-comms implemention of shuffle doesn't produce any keys
named "shuffle"
"""
ddf = dd.from_pandas(pd.DataFrame({"key": np.arange(10)}), npartitions=2)
with dask.config.set(explicit_comms=False):
res = ddf.shuffle(on="key", npartitions=4, shuffle="tasks")
assert any(["shuffle" in str(key) for key in res.dask])
with dask.config.set(explicit_comms=True):
res = ddf.shuffle(on="key", npartitions=4, shuffle="tasks")
if in_cluster:
assert all(["shuffle" not in str(key) for key in res.dask])
else: # If not in cluster, we cannot use explicit comms
assert any(["shuffle" in str(key) for key in res.dask])
with LocalCluster(
protocol="tcp",
dashboard_address=None,
n_workers=2,
threads_per_worker=1,
processes=True,
) as cluster:
with Client(cluster):
check_shuffle(True)
check_shuffle(False)
def test_dask_use_explicit_comms():
p = mp.Process(target=_test_dask_use_explicit_comms)
p.start()
p.join()
assert not p.exitcode
def _test_jit_unspill(protocol):
import cudf
from cudf.tests.utils import assert_eq
dask.config.update(
dask.config.global_config,
{"ucx": {"TLS": "tcp,sockcm,cuda_copy",},},
priority="new",
)
with dask_cuda.LocalCUDACluster(
protocol=protocol,
dashboard_address=None,
n_workers=1,
threads_per_worker=1,
processes=True,
jit_unspill=True,
device_memory_limit="1B",
) as cluster:
with Client(cluster):
np.random.seed(42)
df = cudf.DataFrame.from_pandas(
pd.DataFrame({"key": np.random.random(100)})
)
ddf = dd.from_pandas(df.copy(), npartitions=4)
ddf = explicit_comms_shuffle(ddf, ["key"])
# Check the values of `ddf` (ignoring the row order)
expected = df.sort_values("key")
got = ddf.compute().sort_values("key")
assert_eq(got, expected)
@pytest.mark.parametrize("protocol", ["tcp", "ucx"])
def test_jit_unspill(protocol):
pytest.importorskip("cudf")
p = mp.Process(target=_test_jit_unspill, args=(protocol,))
p.start()
p.join()
assert not p.exitcode