forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_new_dataset_config.py
349 lines (290 loc) · 10.4 KB
/
test_new_dataset_config.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from typing import Optional
import random
import pytest
from unittest.mock import MagicMock
import ray
from ray import train
from ray.train import DataConfig, ScalingConfig
from ray.data import DataIterator
from ray.train.data_parallel_trainer import DataParallelTrainer
from ray.data._internal.execution.interfaces.execution_options import ExecutionOptions
from ray.tests.conftest import * # noqa
@pytest.fixture
def ray_start_4_cpus():
address_info = ray.init(num_cpus=4)
yield address_info
ray.shutdown()
class TestBasic(DataParallelTrainer):
def __init__(
self, num_workers: int, expect_ds: bool, expect_sizes: Optional[dict], **kwargs
):
def train_loop_per_worker():
data_shard = train.get_dataset_shard("train")
assert isinstance(data_shard, DataIterator), data_shard
for k, v in expect_sizes.items():
shard = train.get_dataset_shard(k)
if v == -1:
assert shard is None, shard
else:
count = 0
for batch in shard.iter_batches():
for arr in batch.values():
count += arr.size
assert count == v, shard
kwargs.pop("scaling_config", None)
super().__init__(
train_loop_per_worker=train_loop_per_worker,
scaling_config=ScalingConfig(num_workers=num_workers),
**kwargs,
)
def test_basic(ray_start_4_cpus):
ds = ray.data.range(10)
# Single worker basic case.
test = TestBasic(
1,
True,
{"train": 10, "test": 10},
datasets={"train": ds, "test": ds},
)
test.fit()
# Single worker, no test ds.
test = TestBasic(1, True, {"train": 10, "test": -1}, datasets={"train": ds})
test.fit()
# Two workers, train and test split.
test = TestBasic(
2, True, {"train": 5, "test": 5}, datasets={"train": ds, "test": ds}
)
test.fit()
# Two workers, both split.
test = TestBasic(
2,
True,
{"train": 5, "test": 5},
dataset_config=DataConfig(datasets_to_split=["train", "test"]),
datasets={"train": ds, "test": ds},
)
# Test get config.
assert isinstance(test.get_dataset_config(), DataConfig)
test.fit()
def test_split(ray_start_4_cpus):
ds = ray.data.range(10)
# Split all by default
test = TestBasic(
2,
True,
{"train": 5, "test": 5, "val": 5},
datasets={"train": ds, "test": ds, "val": ds},
)
test.fit()
# Test flag "all"
test = TestBasic(
2,
True,
{"train": 5, "test": 5},
datasets={"train": ds, "test": ds},
dataset_config=DataConfig(datasets_to_split="all"),
)
# Test split train only.
test = TestBasic(
2,
True,
{"train": 5, "test": 10},
datasets={"train": ds, "test": ds},
dataset_config=DataConfig(datasets_to_split=["train"]),
)
test.fit()
# Test invalid arguments
for datasets_to_split in ["train", ("train"), {}]:
with pytest.raises(TypeError, match="`datasets_to_split` should be.*"):
test = TestBasic(
2,
True,
{"train": 5, "test": 10},
datasets={"train": ds, "test": ds},
dataset_config=DataConfig(datasets_to_split=datasets_to_split),
)
# Test empty `datasets_to_split` list
test = TestBasic(
2,
True,
{"train": 10, "test": 10},
datasets={"train": ds, "test": ds},
dataset_config=DataConfig(datasets_to_split=[]),
)
test.fit()
@pytest.mark.skip(
reason="Incomplete implementation of _validate_dag causes other errors, so we "
"remove DAG validation for now; see https://github.com/ray-project/ray/pull/37829"
)
def test_configure_execution_options(ray_start_4_cpus):
ds = ray.data.range(10)
# Resource limit is too low and will trigger an error.
options = DataConfig.default_ingest_options()
options.resource_limits.cpu = 0
test = TestBasic(
1,
True,
{"train": 10, "test": 10},
datasets={"train": ds, "test": ds},
dataset_config=DataConfig(execution_options=options),
)
with pytest.raises(ray.train.base_trainer.TrainingFailedError):
test.fit()
def test_configure_execution_options_carryover_context(ray_start_4_cpus):
"""Tests that execution options in DataContext are carried over to DatConfig
automatically."""
ctx = ray.data.DataContext.get_current()
ctx.execution_options.preserve_order = True
ctx.execution_options.verbose_progress = True
data_config = DataConfig()
ingest_options = data_config.default_ingest_options()
assert ingest_options.preserve_order is True
assert ingest_options.verbose_progress is True
@pytest.mark.parametrize("enable_locality", [True, False])
def test_configure_locality(enable_locality):
options = DataConfig.default_ingest_options()
options.locality_with_output = enable_locality
data_config = DataConfig(execution_options=options)
mock_ds = MagicMock()
mock_ds.streaming_split = MagicMock()
mock_ds.copy = MagicMock(return_value=mock_ds)
world_size = 2
worker_handles = [MagicMock() for _ in range(world_size)]
worker_node_ids = ["node" + str(i) for i in range(world_size)]
data_config.configure(
datasets={"train": mock_ds},
world_size=world_size,
worker_handles=worker_handles,
worker_node_ids=worker_node_ids,
)
mock_ds.streaming_split.assert_called_once()
mock_ds.streaming_split.assert_called_with(
world_size,
equal=True,
locality_hints=worker_node_ids if enable_locality else None,
)
class CustomConfig(DataConfig):
def __init__(self):
pass
def configure(self, *args, **kwargs):
ds = ray.data.range(10)
return [
{"train": ds.iterator()},
{"train": ds.iterator()},
]
def test_custom_config_subclass(ray_start_4_cpus):
test = TestBasic(
1,
True,
{"train": 10},
dataset_config=CustomConfig(),
)
test.fit()
class TestRandom(DataParallelTrainer):
def __init__(self, num_workers: int, expect_random: bool, **kwargs):
def train_loop_per_worker():
data_shard = train.get_dataset_shard("train")
assert isinstance(data_shard, DataIterator), data_shard
epoch1 = list(data_shard.iter_rows())
epoch2 = list(data_shard.iter_rows())
print("Epochs", epoch1, "\n", epoch2)
if expect_random:
assert epoch1 != epoch2
else:
assert epoch1 == epoch2
kwargs.pop("scaling_config", None)
super().__init__(
train_loop_per_worker=train_loop_per_worker,
scaling_config=ScalingConfig(num_workers=num_workers),
**kwargs,
)
def test_per_epoch_preprocessing(ray_start_4_cpus):
ds = ray.data.range(100, parallelism=100).randomize_block_order()
test = TestRandom(2, True, datasets={"train": ds})
test.fit()
ds = ray.data.range(100, parallelism=100).random_shuffle()
test = TestRandom(2, True, datasets={"train": ds})
test.fit()
ds = ray.data.range(100, parallelism=100).map(
lambda x: {"id": x["id"] * random.random()}
)
test = TestRandom(2, True, datasets={"train": ds})
test.fit()
def test_materialized_preprocessing(ray_start_4_cpus):
# TODO(ekl) we should test all these configs with splitting enabled, but this
# requires implementing deterministic streaming split.
ds = ray.data.range(100, parallelism=100).randomize_block_order()
ds = ds.materialize()
test = TestRandom(
2,
False,
datasets={"train": ds},
dataset_config=DataConfig(datasets_to_split=[]),
)
test.fit()
ds = ray.data.range(100, parallelism=100).random_shuffle()
ds = ds.materialize()
test = TestRandom(
2,
False,
datasets={"train": ds},
dataset_config=DataConfig(datasets_to_split=[]),
)
test.fit()
ds = ray.data.range(100, parallelism=100).map(
lambda x: {"id": x["id"] * random.random()}
)
ds = ds.materialize()
test = TestRandom(
2,
False,
datasets={"train": ds},
dataset_config=DataConfig(datasets_to_split=[]),
)
test.fit()
def test_data_config_default_resource_limits(shutdown_only):
"""Test that DataConfig should exclude training resources from Data."""
cluster_cpus, cluster_gpus = 20, 10
num_workers = 2
# Resources used by training workers.
cpus_per_worker, gpus_per_worker = 2, 1
# Resources used by the trainer actor.
default_trainer_cpus, default_trainer_gpus = 1, 0
num_train_cpus = num_workers * cpus_per_worker + default_trainer_cpus
num_train_gpus = num_workers * gpus_per_worker + default_trainer_gpus
init_exclude_cpus = 2
init_exclude_gpus = 1
ray.init(num_cpus=cluster_cpus, num_gpus=cluster_gpus)
class MyTrainer(DataParallelTrainer):
def __init__(self, **kwargs):
def train_loop_fn():
train_ds = train.get_dataset_shard("train")
exclude_resources = (
train_ds._base_dataset.context.execution_options.exclude_resources
)
assert exclude_resources.cpu == num_train_cpus + init_exclude_cpus
assert exclude_resources.gpu == num_train_gpus + init_exclude_gpus
kwargs.pop("scaling_config", None)
execution_options = ExecutionOptions()
execution_options.exclude_resources.cpu = init_exclude_cpus
execution_options.exclude_resources.gpu = init_exclude_gpus
super().__init__(
train_loop_per_worker=train_loop_fn,
scaling_config=ScalingConfig(
num_workers=num_workers,
use_gpu=True,
resources_per_worker={
"CPU": cpus_per_worker,
"GPU": gpus_per_worker,
},
),
datasets={"train": ray.data.range(10)},
dataset_config=DataConfig(execution_options=execution_options),
**kwargs,
)
trainer = MyTrainer()
trainer.fit()
if __name__ == "__main__":
import sys
import pytest
sys.exit(pytest.main(["-v", "-x", __file__]))