-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathtest_shuffle.py
186 lines (158 loc) · 7.22 KB
/
test_shuffle.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
import dask.dataframe as dd
import pandas as pd
from dask.distributed import Client, LocalCluster
import nemo_curator as nc
from nemo_curator.datasets import DocumentDataset
def list_to_dataset(documents, col_name="text", npartitions=2):
data = {col_name: documents}
pdf = pd.DataFrame(data)
return DocumentDataset(dd.from_pandas(pdf, npartitions=npartitions))
def all_equal(left_dataset, right_dataset):
left_result = left_dataset.df.compute()
right_result = right_dataset.df.compute()
l_cols = set(left_result.columns)
r_cols = set(right_result.columns)
assert l_cols == r_cols
for col in left_result.columns:
left = left_result[col].reset_index(drop=True)
right = right_result[col].reset_index(drop=True)
assert all(left == right), f"Mismatch in {col} column.\n{left}\n{right}\n"
class TestShuffleNondeterministic:
def test_shuffle(self):
# Single threaded Dask is the only way to guarantee shuffle determinism
# Docs: https://docs.dask.org/en/latest/generated/dask.dataframe.DataFrame.shuffle.html
with LocalCluster(n_workers=1, threads_per_worker=1) as cluster:
with Client(cluster):
original_dataset = list_to_dataset(
["one", "two", "three", "four", "five"]
)
expected_dataset = list_to_dataset(
["two", "five", "three", "one", "four"]
)
shuffle = nc.Shuffle(seed=42)
result_dataset = shuffle.shuffle_nondeterministic(original_dataset)
all_equal(expected_dataset, result_dataset)
def test_new_partitions(self):
with LocalCluster(n_workers=1, threads_per_worker=1) as cluster:
with Client(cluster):
original_dataset = list_to_dataset(
["one", "two", "three", "four", "five"], npartitions=3
)
expected_dataset = list_to_dataset(
["two", "five", "three", "one", "four"], npartitions=3
)
shuffle = nc.Shuffle(seed=42, npartitions=2)
result_dataset = shuffle.shuffle_nondeterministic(original_dataset)
all_equal(expected_dataset, result_dataset)
def test_filename(self):
with LocalCluster(n_workers=1, threads_per_worker=1) as cluster:
with Client(cluster):
original_dataset = list_to_dataset(
["one", "two", "three", "four", "five"], npartitions=1
)
original_dataset.df["filename"] = "original.jsonl"
expected_data = {
"text": ["one", "two", "three", "five", "four"],
"filename": [
"file_0000000000.jsonl",
"file_0000000000.jsonl",
"file_0000000000.jsonl",
"file_0000000001.jsonl",
"file_0000000001.jsonl",
],
}
pdf = pd.DataFrame(expected_data)
expected_dataset = DocumentDataset(dd.from_pandas(pdf, npartitions=2))
shuffle = nc.Shuffle(seed=42, npartitions=2)
result_dataset = shuffle.shuffle_nondeterministic(original_dataset)
all_equal(expected_dataset, result_dataset)
def test_custom_filenames(self):
with LocalCluster(n_workers=1, threads_per_worker=1) as cluster:
with Client(cluster):
original_dataset = list_to_dataset(
["one", "two", "three", "four", "five"], npartitions=1
)
original_dataset.df["filename"] = "original.jsonl"
expected_data = {
"text": ["one", "two", "three", "five", "four"],
"filename": [
"my_0.test",
"my_0.test",
"my_0.test",
"my_1.test",
"my_1.test",
],
}
pdf = pd.DataFrame(expected_data)
expected_dataset = DocumentDataset(dd.from_pandas(pdf, npartitions=2))
def filename_fn(x):
return f"my_{x}.test"
shuffle = nc.Shuffle(
seed=42, npartitions=2, partition_to_filename=filename_fn
)
result_dataset = shuffle.shuffle_nondeterministic(original_dataset)
all_equal(expected_dataset, result_dataset)
def test_shuffle_no_seed(self):
original_dataset = list_to_dataset(["one", "two", "three", "four", "five"])
shuffle = nc.Shuffle()
result_dataset = shuffle(original_dataset)
assert len(result_dataset.df.compute()) == 5
class TestShuffleDeterministic:
def test_shuffle(self):
original_dataset = list_to_dataset(["one", "two", "three", "four", "five"])
expected_dataset = list_to_dataset(["five", "four", "three", "one", "two"])
shuffle = nc.Shuffle(seed=42)
result_dataset = shuffle(original_dataset)
all_equal(expected_dataset, result_dataset)
def test_new_partitions(self):
original_dataset = list_to_dataset(
["one", "two", "three", "four", "five"], npartitions=3
)
expected_dataset = list_to_dataset(
["four", "three", "five", "one", "two"], npartitions=3
)
shuffle = nc.Shuffle(seed=42, npartitions=2)
result_dataset = shuffle(original_dataset)
all_equal(expected_dataset, result_dataset)
def test_filename(self):
original_dataset = list_to_dataset(
["one", "two", "three", "four", "five"], npartitions=1
)
original_dataset.df["filename"] = "original.jsonl"
expected_data = {
"text": ["four", "five", "three", "one", "two"],
"filename": [
"file_0000000000.jsonl",
"file_0000000001.jsonl",
"file_0000000001.jsonl",
"file_0000000001.jsonl",
"file_0000000001.jsonl",
],
}
pdf = pd.DataFrame(expected_data)
expected_dataset = DocumentDataset(dd.from_pandas(pdf, npartitions=2))
shuffle = nc.Shuffle(seed=42, npartitions=2)
result_dataset = shuffle(original_dataset)
all_equal(expected_dataset, result_dataset)
def test_custom_filenames(self):
original_dataset = list_to_dataset(
["one", "two", "three", "four", "five"], npartitions=1
)
original_dataset.df["filename"] = "original.jsonl"
expected_data = {
"text": ["four", "five", "three", "one", "two"],
"filename": [
"my_0.test",
"my_1.test",
"my_1.test",
"my_1.test",
"my_1.test",
],
}
pdf = pd.DataFrame(expected_data)
expected_dataset = DocumentDataset(dd.from_pandas(pdf, npartitions=2))
def filename_fn(x):
return f"my_{x}.test"
shuffle = nc.Shuffle(seed=42, npartitions=2, partition_to_filename=filename_fn)
result_dataset = shuffle(original_dataset)
all_equal(expected_dataset, result_dataset)