|
| 1 | +from tests.unit.calibration.fixtures.test_local_h5_partitioning import ( |
| 2 | + load_partitioning_exports, |
| 3 | +) |
| 4 | + |
| 5 | + |
| 6 | +partitioning = load_partitioning_exports() |
| 7 | +flatten_chunks = partitioning["flatten_chunks"] |
| 8 | +partition_weighted_work_items = partitioning["partition_weighted_work_items"] |
| 9 | +work_item_key = partitioning["work_item_key"] |
| 10 | + |
| 11 | + |
| 12 | +def test_work_item_key_uses_existing_completion_shape(): |
| 13 | + item = {"type": "district", "id": "CA-12", "weight": 1} |
| 14 | + assert work_item_key(item) == "district:CA-12" |
| 15 | + |
| 16 | + |
| 17 | +def test_partition_filters_completed_items(): |
| 18 | + work_items = [ |
| 19 | + {"type": "state", "id": "CA", "weight": 3}, |
| 20 | + {"type": "district", "id": "CA-12", "weight": 1}, |
| 21 | + {"type": "city", "id": "NYC", "weight": 2}, |
| 22 | + ] |
| 23 | + |
| 24 | + chunks = partition_weighted_work_items( |
| 25 | + work_items, |
| 26 | + num_workers=2, |
| 27 | + completed={"district:CA-12"}, |
| 28 | + ) |
| 29 | + |
| 30 | + flattened = flatten_chunks(chunks) |
| 31 | + assert all(item["id"] != "CA-12" for item in flattened) |
| 32 | + assert {item["id"] for item in flattened} == {"CA", "NYC"} |
| 33 | + |
| 34 | + |
| 35 | +def test_partition_returns_empty_for_zero_workers_or_zero_remaining(): |
| 36 | + work_items = [{"type": "state", "id": "CA", "weight": 1}] |
| 37 | + |
| 38 | + assert partition_weighted_work_items(work_items, num_workers=0) == [] |
| 39 | + assert ( |
| 40 | + partition_weighted_work_items( |
| 41 | + work_items, |
| 42 | + num_workers=3, |
| 43 | + completed={"state:CA"}, |
| 44 | + ) |
| 45 | + == [] |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def test_partition_uses_no_more_workers_than_remaining_items(): |
| 50 | + work_items = [ |
| 51 | + {"type": "state", "id": "CA", "weight": 5}, |
| 52 | + {"type": "state", "id": "NY", "weight": 4}, |
| 53 | + ] |
| 54 | + |
| 55 | + chunks = partition_weighted_work_items(work_items, num_workers=10) |
| 56 | + |
| 57 | + assert len(chunks) == 2 |
| 58 | + assert all(len(chunk) == 1 for chunk in chunks) |
| 59 | + |
| 60 | + |
| 61 | +def test_partition_is_weight_balancing_and_deterministic_for_equal_weights(): |
| 62 | + work_items = [ |
| 63 | + {"type": "district", "id": "A", "weight": 5}, |
| 64 | + {"type": "district", "id": "B", "weight": 5}, |
| 65 | + {"type": "district", "id": "C", "weight": 2}, |
| 66 | + {"type": "district", "id": "D", "weight": 2}, |
| 67 | + ] |
| 68 | + |
| 69 | + chunks = partition_weighted_work_items(work_items, num_workers=2) |
| 70 | + |
| 71 | + ids_by_chunk = [[item["id"] for item in chunk] for chunk in chunks] |
| 72 | + loads = [sum(item["weight"] for item in chunk) for chunk in chunks] |
| 73 | + |
| 74 | + assert ids_by_chunk == [["A", "C"], ["B", "D"]] |
| 75 | + assert loads == [7, 7] |
0 commit comments