diff --git a/src/twinkle/model/micro_batch.py b/src/twinkle/model/micro_batch.py index 2b8e09a1e..c378df3aa 100644 --- a/src/twinkle/model/micro_batch.py +++ b/src/twinkle/model/micro_batch.py @@ -54,7 +54,10 @@ def from_kwargs(cls, kwargs: dict[str, Any]) -> MicroBatchConfig | None: ) -def sequence_length(model_input: dict[str, Any]) -> int: +def sequence_length(model_input: dict[str, Any] | list | tuple) -> int: + """Return the token count of one encoded row or packed group.""" + if isinstance(model_input, (list, tuple)): + return sum(sequence_length(item) for item in model_input) input_ids = model_input["input_ids"] return int(input_ids.shape[-1]) if hasattr(input_ids, "shape") else len(input_ids) @@ -197,7 +200,7 @@ def _reorder_micro_batches( def plan_micro_batches( - inputs: list[dict[str, Any]], + inputs: list[dict[str, Any] | list | tuple], config: MicroBatchConfig, *, padding_free: bool, diff --git a/tests/model/test_micro_batch.py b/tests/model/test_micro_batch.py index c2e63c2e4..157faa4ac 100644 --- a/tests/model/test_micro_batch.py +++ b/tests/model/test_micro_batch.py @@ -31,6 +31,42 @@ def test_fixed_micro_batch_plan_keeps_full_batches_and_a_short_tail( assert [index for batch in plan for index in batch] == list(range(input_count)) +@pytest.fixture +def packed_inputs(): + return [ + [{'input_ids': [1]}, {'input_ids': [2]}], + ({'input_ids': [1, 2]}, {'input_ids': [3]}), + [{'input_ids': [1, 2, 3]}, {'input_ids': [4, 5, 6, 7]}], + [{'input_ids': [1]}], + ] + + +@pytest.mark.parametrize(('order', 'expected'), [ + ('none', [[0, 1], [2, 3]]), + ('descending', [[2, 3], [0, 1]]), + ('ends_small', [[2, 3], [0, 1]]), +]) +def test_fixed_micro_batch_plan_counts_packed_tokens_for_ordering(packed_inputs, order, expected): + config = MicroBatchConfig(micro_batch_size=2, micro_batch_order=order) + + assert [sequence_length(item) for item in packed_inputs] == [2, 3, 7, 1] + assert plan_micro_batches(packed_inputs, config, padding_free=True) == expected + + +@pytest.mark.parametrize('packing_algorithm', ['ffd', 'kk']) +def test_dynamic_micro_batch_plan_respects_packed_token_budget(packed_inputs, packing_algorithm): + config = MicroBatchConfig( + micro_batch_size=2, dynamic_batching=True, max_tokens_per_micro_batch=7, + packing_algorithm=packing_algorithm, + ) + + plan = plan_micro_batches(packed_inputs, config, padding_free=True) + + assert sorted(index for batch in plan for index in batch) == [0, 1, 2, 3] + assert [2] in plan + assert all(sum(sequence_length(packed_inputs[index]) for index in batch) <= 7 for batch in plan) + + @pytest.mark.parametrize('packing_algorithm', ['ffd', 'kk']) def test_dynamic_micro_batch_plan_preserves_samples_and_limits_cost(packing_algorithm): lengths = [10, 9, 8, 7, 4, 3, 2, 1]