MCPcopy Create free account
hub / github.com/AnswerDotAI/ModernBERT / test_packer_masking_randomly

Function test_packer_masking_randomly

tests/test_sequence_packer.py:251–354  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

249
250
251def test_packer_masking_randomly():
252 input_batch_size = 4
253 xds = [{"input_ids": seq} for seq in xs]
254 input_batches = list(batched(xds, input_batch_size))
255 d = {
256 "src_iterable": input_batches,
257 "src_batch_size": input_batch_size,
258 "src_max_seq_len": max_seq_len,
259 "out_batch_size": 5,
260 "out_pseq_len": 15,
261 "buffer_size": 5,
262 "pad_token_id": -1,
263 "mask_token_id": -2,
264 "ignore_token_id": -3,
265 "mask_prob": 0.5, # <== extensive masking and word randomization
266 "seed": 42,
267 "suppress_masking": False,
268 }
269
270 out_batches = list(GreedyBestFitSequencePacker(**d))
271
272 out_expected = [
273 {
274 "input_ids": tensor(
275 [
276 [0, 4, 0, 0, -2, 1, 1, 1, -2, 4, -2, 4, 4, 4, 6],
277 [-2, 2, -2, 2, 2, 2, -2, 2, 2, 2, -2, 3, -2, -2, 8],
278 [5, 5, -2, -2, 5, -2, -2, 6, -2, 6, 10, 6, 6, -2, -1],
279 [7, -2, -2, 10, -2, -2, -2, 10, 10, 10, 11, 11, 11, -2, -2],
280 [12, 12, 12, 12, 13, 13, 13, -2, -2, 13, -2, 6, 14, -2, -2],
281 ]
282 ),
283 "labels": tensor(
284 [
285 [-3, 0, -3, -3, 1, -3, -3, -3, 1, 4, 4, -3, -3, -3, 4],
286 [2, -3, 2, -3, -3, -3, 2, -3, -3, -3, 3, 3, 3, 8, -3],
287 [-3, -3, 5, 5, 5, 5, 6, 6, 6, -3, 6, -3, -3, -3, -3],
288 [-3, 7, 7, -3, 10, 10, 10, -3, -3, -3, -3, 11, -3, 11, 18],
289 [-3, 12, -3, -3, -3, -3, -3, 13, 13, 13, 13, 13, -3, 14, 14],
290 ]
291 ),
292 "cu_seqlens": [
293 tensor([0, 4, 9, 15], dtype=torch.int32),
294 tensor([0, 10, 13, 15], dtype=torch.int32),
295 tensor([0, 6, 13, 15], dtype=torch.int32),
296 tensor([0, 3, 10, 14, 15], dtype=torch.int32),
297 tensor([0, 4, 12, 15], dtype=torch.int32),
298 ],
299 "max_seqlen": [6, 10, 7, 7, 8],
300 "attention_mask": tensor(
301 [
302 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
303 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
304 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
305 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
306 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
307 ]
308 ),

Callers

nothing calls this directly

Calls 3

batchedFunction · 0.85
compare_structuresFunction · 0.85

Tested by

no test coverage detected