[cad161]: / tests / utils / test_batching.py

Download this file

333 lines (256 with data), 10.4 kB

  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
import pytest
from edsnlp.utils.batching import (
DATASET_END_SENTINEL,
BatchSizeArg,
FragmentEndSentinel,
StreamSentinel,
batchify,
batchify_by_dataset,
batchify_by_fragment,
batchify_by_length_sum,
batchify_by_padded,
stat_batchify,
)
class MockStreamSentinel(StreamSentinel):
pass
# Tests for BatchSizeArg
def test_batch_size_arg_validate():
# Valid inputs
assert BatchSizeArg.validate("10 samples") == (10, "samples")
assert BatchSizeArg.validate("20 words") == (20, "words")
assert BatchSizeArg.validate(15) == (15, "docs")
assert BatchSizeArg.validate("docs") == (None, "docs")
assert BatchSizeArg.validate("tokens") == (None, "tokens")
assert BatchSizeArg.validate("25") == (25, "docs")
# Invalid inputs
with pytest.raises(Exception):
BatchSizeArg.validate("invalid input")
with pytest.raises(Exception):
BatchSizeArg.validate("10 invalid input")
with pytest.raises(Exception):
BatchSizeArg.validate("invalid input 10")
# Tests for batchify function
def test_batchify_simple():
data = [1, 2, 3, 4, 5]
batches = list(batchify(data, batch_size=2))
assert batches == [[1, 2], [3, 4], [5]]
def test_batchify_drop_last():
data = [1, 2, 3, 4, 5]
batches = list(batchify(data, batch_size=2, drop_last=True))
assert batches == [[1, 2], [3, 4]]
def test_batchify_sentinel_drop():
data = [1, 2, MockStreamSentinel(), 3, 4]
batches = list(batchify(data, batch_size=2, sentinel_mode="drop"))
assert batches == [[1, 2], [3, 4]]
def test_batchify_sentinel_keep():
sentinel = MockStreamSentinel()
data = [1, 2, sentinel, 3, 4]
batches = list(batchify(data, batch_size=2, sentinel_mode="keep"))
assert batches == [[1, 2, sentinel], [3, 4]]
def test_batchify_sentinel_split():
sentinel = MockStreamSentinel()
data = [1, 2, sentinel, 3, 4]
batches = list(batchify(data, batch_size=2, sentinel_mode="split"))
assert batches == [[1, 2], sentinel, [3, 4]]
# Tests for batchify_by_length_sum
def test_batchify_by_length_sum_simple():
data = ["a", "bb", "ccc", "dddd", "eeeee"]
batches = list(batchify_by_length_sum(data, batch_size=5))
assert batches == [["a", "bb"], ["ccc"], ["dddd"], ["eeeee"]]
def test_batchify_by_length_sum_drop_last():
data = ["a", "bb", "ccc", "dddd", "eeeee"]
batches = list(batchify_by_length_sum(data, batch_size=5, drop_last=True))
assert batches == [["a", "bb"], ["ccc"], ["dddd"]]
# Tests for batchify_by_length_sum
def test_batchify_by_length_sum_split():
sentinel = MockStreamSentinel()
data = ["aa", "bb", sentinel, "ccc", "dddd", "eeeee"]
batches = list(batchify_by_length_sum(data, batch_size=7, sentinel_mode="split"))
assert batches == [["aa", "bb"], sentinel, ["ccc", "dddd"], ["eeeee"]]
# Tests for batchify_by_length_sum
def test_batchify_by_length_sum_keep():
sentinel = MockStreamSentinel()
data = ["aa", "bb", sentinel, "ccc", "dddd", "eeeee"]
batches = list(batchify_by_length_sum(data, batch_size=7, sentinel_mode="keep"))
assert batches == [["aa", "bb", sentinel, "ccc"], ["dddd"], ["eeeee"]]
# Tests for batchify_by_padded
def test_batchify_by_padded_simple():
data = ["a", "bb", "ccc", "dddd"]
batches = list(batchify_by_padded(data, batch_size=6))
assert batches == [["a", "bb"], ["ccc"], ["dddd"]]
def test_batchify_by_padded_drop_last():
data = ["a", "bb", "ccc", "dddd"]
batches = list(batchify_by_padded(data, batch_size=6, drop_last=True))
assert batches == [["a", "bb"], ["ccc"]]
def test_batchify_by_padded_sentinel_keep():
sentinel = MockStreamSentinel()
data = ["a", sentinel, "bb", "ccc"]
batches = list(batchify_by_padded(data, batch_size=6, sentinel_mode="keep"))
assert batches == [["a", sentinel, "bb"], ["ccc"]]
def test_batchify_by_padded_sentinel_split():
sentinel = MockStreamSentinel()
data = ["a", sentinel, "bb", "ccc"]
batches = list(batchify_by_padded(data, batch_size=5, sentinel_mode="split"))
assert batches == [["a"], sentinel, ["bb"], ["ccc"]]
# Tests for batchify_by_dataset
def test_batchify_by_dataset_simple():
data = [
"item1",
"item2",
DATASET_END_SENTINEL,
"item3",
DATASET_END_SENTINEL,
"item4",
"item5",
]
batches = list(batchify_by_dataset(data))
assert batches == [
["item1", "item2"],
DATASET_END_SENTINEL,
["item3"],
DATASET_END_SENTINEL,
["item4", "item5"],
]
def test_batchify_by_dataset_sentinel_split():
sentinel = MockStreamSentinel()
data = ["item1", sentinel, "item2", DATASET_END_SENTINEL, "item3"]
batches = list(batchify_by_dataset(data, sentinel_mode="split"))
assert batches == [["item1"], sentinel, ["item2"], DATASET_END_SENTINEL, ["item3"]]
def test_batchify_by_dataset_sentinel_keep():
sentinel = MockStreamSentinel()
data = ["item1", sentinel, "item2", DATASET_END_SENTINEL, "item3"]
batches = list(batchify_by_dataset(data, sentinel_mode="keep"))
assert batches == [["item1", sentinel, "item2"], DATASET_END_SENTINEL, ["item3"]]
def test_batchify_by_dataset_sentinel_drop():
sentinel = MockStreamSentinel()
data = ["item1", sentinel, "item2", DATASET_END_SENTINEL, "item3"]
batches = list(batchify_by_dataset(data, sentinel_mode="drop"))
assert batches == [["item1", "item2"], DATASET_END_SENTINEL, ["item3"]]
def test_batchify_by_dataset_drop_last():
data = ["item1", "item2", DATASET_END_SENTINEL, "item3"]
batches = list(batchify_by_dataset(data, drop_last=True))
assert batches == [["item1", "item2"], DATASET_END_SENTINEL]
# Tests for batchify_by_fragment
def test_batchify_by_fragment_simple():
fragment_end_1 = FragmentEndSentinel("fragment1")
fragment_end_2 = FragmentEndSentinel("fragment2")
data = ["item1", "item2", fragment_end_1, "item3", fragment_end_2, "item4"]
batches = list(batchify_by_fragment(data))
assert batches == [
["item1", "item2"],
fragment_end_1,
["item3"],
fragment_end_2,
["item4"],
]
def test_batchify_by_fragment_sentinel_split():
sentinel = MockStreamSentinel()
fragment_end = FragmentEndSentinel("fragment")
data = ["item1", sentinel, "item2", fragment_end]
batches = list(batchify_by_fragment(data, sentinel_mode="split"))
assert batches == [["item1"], sentinel, ["item2"], fragment_end]
def test_batchify_by_fragment_sentinel_keep():
sentinel = MockStreamSentinel()
fragment_end = FragmentEndSentinel("fragment")
data = ["item1", sentinel, "item2", fragment_end]
batches = list(batchify_by_fragment(data, sentinel_mode="keep"))
assert batches == [["item1", sentinel, "item2"], fragment_end]
def test_batchify_by_fragment_sentinel_drop():
sentinel = MockStreamSentinel()
fragment_end = FragmentEndSentinel("fragment")
data = ["item1", sentinel, "item2", fragment_end]
batches = list(batchify_by_fragment(data, sentinel_mode="drop"))
assert batches == [["item1", "item2"], fragment_end]
def test_batchify_by_fragment_drop_last():
fragment_end = FragmentEndSentinel("fragment")
data = ["item1", "item2", fragment_end]
batches = list(batchify_by_fragment(data, sentinel_mode="split", drop_last=True))
assert batches == [["item1", "item2"], fragment_end]
# Tests for stat_batchify
def test_stat_batchify_simple():
data = [
{"/stats/length": 2, "text": "aa"},
{"/stats/length": 3, "text": "bbb"},
{"/stats/length": 4, "text": "cccc"},
{"/stats/length": 2, "text": "dd"},
]
batch_fn = stat_batchify("length")
batches = list(batch_fn(data, batch_size=5))
assert batches == [
[data[0], data[1]], # Total length: 5
[data[2]], # Total length: 4
[data[3]], # Total length: 2
]
def test_stat_batchify_invalid_key():
data = [{"text": "aaa"}]
batch_fn = stat_batchify("length")
with pytest.raises(ValueError):
list(batch_fn(data, batch_size=5))
def test_stat_batchify_sentinel_split():
sentinel = MockStreamSentinel()
data = [
{"/stats/length": 2, "text": "aa"},
sentinel,
{"/stats/length": 3, "text": "bbb"},
]
batch_fn = stat_batchify("length")
batches = list(batch_fn(data, batch_size=5, sentinel_mode="split"))
assert batches == [
[data[0]],
sentinel,
[data[2]],
]
def test_stat_batchify_sentinel_keep():
sentinel = MockStreamSentinel()
data = [
{"/stats/length": 2, "text": "aa"},
sentinel,
{"/stats/length": 4, "text": "bbbb"},
]
batch_fn = stat_batchify("length")
batches = list(batch_fn(data, batch_size=5, sentinel_mode="keep"))
assert batches == [
[data[0], sentinel],
[data[2]],
]
def test_stat_batchify_drop_last():
data = [
{"/stats/length": 2, "text": "aa"},
{"/stats/length": 3, "text": "bbb"},
{"/stats/length": 4, "text": "cccc"},
]
batch_fn = stat_batchify("length")
batches = list(batch_fn(data, batch_size=6, drop_last=True))
assert batches == [
[data[0], data[1]], # Total length: 5
] # Last batch is dropped because total length is 4
# Additional tests to ensure full coverage
def test_batchify_empty_iterable():
data = []
batches = list(batchify(data, batch_size=2))
assert batches == []
def test_batchify_by_length_sum_empty_iterable():
data = []
batches = list(batchify_by_length_sum(data, batch_size=5))
assert batches == []
def test_batchify_by_padded_empty_iterable():
data = []
batches = list(batchify_by_padded(data, batch_size=6))
assert batches == []
def test_batchify_by_dataset_empty_iterable():
data = []
batches = list(batchify_by_dataset(data))
assert batches == []
def test_batchify_by_fragment_empty_iterable():
data = []
batches = list(batchify_by_fragment(data))
assert batches == []
def test_stat_batchify_empty_iterable():
data = []
batch_fn = stat_batchify("length")
batches = list(batch_fn(data, batch_size=5))
assert batches == []
def test_batchify_invalid_sentinel_mode():
data = [1, 2, 3]
with pytest.raises(AssertionError):
list(batchify(data, batch_size=2, sentinel_mode="invalid_mode"))