From 6ef48036f588752da8cddca3359b507030de93ec Mon Sep 17 00:00:00 2001 From: werwrewe <83645560+werwrewe@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:13:19 +0800 Subject: [PATCH 1/2] fix(model): preserve fixed micro-batching for packed inputs --- src/twinkle/model/micro_batch.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/twinkle/model/micro_batch.py b/src/twinkle/model/micro_batch.py index 2b8e09a1e..52360813c 100644 --- a/src/twinkle/model/micro_batch.py +++ b/src/twinkle/model/micro_batch.py @@ -212,6 +212,8 @@ def plan_micro_batches( """ if not inputs: raise ValueError("cannot plan micro-batches for empty inputs") + if not config.dynamic_batching and config.micro_batch_order == "none": + return _fixed_plan(len(inputs), config.micro_batch_size) lengths = [sequence_length(model_input) for model_input in inputs] return plan_micro_batches_for_lengths( lengths, config, padding_free=padding_free, min_micro_batches=min_micro_batches From e7a9c583467cdb2c0514a63cdc9915178fbaa88d Mon Sep 17 00:00:00 2001 From: werwrewe <83645560+werwrewe@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:55:27 +0800 Subject: [PATCH 2/2] fix(model): count packed input tokens for micro-batch planning --- src/twinkle/model/micro_batch.py | 9 ++++---- tests/model/test_micro_batch.py | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/twinkle/model/micro_batch.py b/src/twinkle/model/micro_batch.py index 52360813c..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, @@ -212,8 +215,6 @@ def plan_micro_batches( """ if not inputs: raise ValueError("cannot plan micro-batches for empty inputs") - if not config.dynamic_batching and config.micro_batch_order == "none": - return _fixed_plan(len(inputs), config.micro_batch_size) lengths = [sequence_length(model_input) for model_input in inputs] return plan_micro_batches_for_lengths( lengths, config, padding_free=padding_free, min_micro_batches=min_micro_batches 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]