Skip to content

Commit 744166a

Browse files
authored
fix(batching): keep requested batches for zero-byte inputs (#749)
1 parent 2cc0849 commit 744166a

2 files changed

Lines changed: 39 additions & 18 deletions

File tree

‎src/hflow/batching.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def plan_batches(
5353
"""Pack ``item_sizes`` (uri -> size in bytes) into near-equal-byte batches.
5454
5555
Pass exactly one of ``batch_count`` or ``target_batch_bytes``.
56+
Zero-byte inputs still occupy a batch: fixed-count plans return
57+
``min(batch_count, len(item_sizes))`` nonempty batches.
5658
An optional item cap keeps each input whole. Fixed-count plans that cannot
5759
fit every item under that cap fail; capacity plans open additional batches.
5860
Batches come back largest-first; ``start_delay_s`` is ``index *
@@ -84,17 +86,18 @@ def plan_batches(
8486
batches: list[tuple[list[str], int]]
8587
if batch_count is not None:
8688
# Least-loaded assignment: push each item onto the currently-lightest
87-
# batch. Heap entries are (total_bytes, batch_index) so byte ties
88-
# break on index, deterministically.
89+
# batch. Empty batches win zero-byte ties, so every requested
90+
# batch receives an item before a zero-byte batch receives another.
91+
# Remaining ties break on index, deterministically.
8992
batches = [([], 0) for _ in range(min(batch_count, len(descending_items)))]
90-
heap = [(0, index) for index in range(len(batches))]
93+
heap = [(0, False, index) for index in range(len(batches))]
9194
heapq.heapify(heap)
9295
for uri, size_bytes in descending_items:
93-
total, index = heapq.heappop(heap)
96+
total, _, index = heapq.heappop(heap)
9497
batches[index][0].append(uri)
9598
batches[index] = (batches[index][0], total + size_bytes)
9699
if maximum_items_per_batch is None or len(batches[index][0]) < maximum_items_per_batch:
97-
heapq.heappush(heap, (total + size_bytes, index))
100+
heapq.heappush(heap, (total + size_bytes, True, index))
98101
else:
99102
assert target_batch_bytes is not None
100103
# First-fit-decreasing: first open batch with room, else a new one.

‎tests/test_batching.py‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,35 @@
99
from hflow.batching import PlannedBatch, plan_batches, plan_batches_from_files
1010

1111

12-
def test_fixed_count_batches_are_near_equal_bytes() -> None:
12+
@pytest.mark.parametrize(
13+
("sizes", "item_cap"),
14+
[
15+
pytest.param([900, 850, 500, 400, 300, 250, 200, 150, 100, 50], None, id="positive"),
16+
pytest.param([0] * 7, None, id="all-zero"),
17+
pytest.param([0] * 4, 3, id="all-zero-with-cap"),
18+
pytest.param([900, 0, 0, 0], None, id="mixed-zero"),
19+
pytest.param([0, 0], None, id="fewer-items-than-batches"),
20+
],
21+
)
22+
def test_fixed_count_batches_are_near_equal_bytes(sizes: list[int], item_cap: int | None) -> None:
1323
# A naive round-robin unbalances these widely varying sizes; least-loaded
14-
# keeps the spread within one item.
15-
item_sizes = {
16-
f"ep{index:02d}": size
17-
for index, size in enumerate([900, 850, 500, 400, 300, 250, 200, 150, 100, 50])
18-
}
19-
batches = plan_batches(item_sizes, batch_count=3)
20-
assert len(batches) == 3
24+
# keeps the spread within one item. Zero bytes must not erase requested batches.
25+
item_sizes = {f"ep{index:02d}": size for index, size in enumerate(sizes)}
26+
batches = plan_batches(item_sizes, batch_count=3, maximum_items_per_batch=item_cap)
27+
assert len(batches) == min(3, len(item_sizes))
2128
totals = [batch.total_bytes for batch in batches]
2229
assert sum(totals) == sum(item_sizes.values())
2330
assert max(totals) - min(totals) <= max(item_sizes.values())
24-
assert {item for batch in batches for item in batch.items} == set(item_sizes)
31+
assigned = [item for batch in batches for item in batch.items]
32+
assert sorted(assigned) == sorted(item_sizes)
33+
assert all(batch.items for batch in batches)
34+
if item_cap is not None:
35+
assert all(len(batch.items) <= item_cap for batch in batches)
36+
assert batches == plan_batches(
37+
dict(reversed(list(item_sizes.items()))),
38+
batch_count=3,
39+
maximum_items_per_batch=item_cap,
40+
)
2541

2642

2743
def test_plan_is_deterministic() -> None:
@@ -156,13 +172,15 @@ def test_plan_from_files_forwards_stagger_validation(tmp_path: Path) -> None:
156172
plan_batches_from_files([item], batch_count=1, stagger_interval_s=float("nan"))
157173

158174

159-
def test_plan_from_real_files(tmp_path: Path) -> None:
175+
@pytest.mark.parametrize("sizes", [(100, 10_000), (0, 0)], ids=["positive", "zero-byte"])
176+
def test_plan_from_real_files(tmp_path: Path, sizes: tuple[int, int]) -> None:
160177
small = tmp_path / "small.mcap"
161-
small.write_bytes(b"x" * 100)
178+
small.write_bytes(b"x" * sizes[0])
162179
large = tmp_path / "large.mcap"
163-
large.write_bytes(b"x" * 10_000)
180+
large.write_bytes(b"x" * sizes[1])
164181
batches = plan_batches_from_files([small, large], batch_count=2)
165-
assert batches[0].total_bytes == 10_000
182+
assert len(batches) == 2
183+
assert batches[0].total_bytes == sizes[1]
166184
assert batches[0].items == (str(large),)
167185

168186

0 commit comments

Comments
 (0)