|
| 1 | +import pytest |
| 2 | + |
| 3 | +from speculators.data_generation.offline import ( |
| 4 | + get_existing_hidden_state_indices, |
| 5 | + get_indices_to_process, |
| 6 | +) |
| 7 | + |
| 8 | + |
| 9 | +# ===== get_indices_to_process Tests ===== |
| 10 | + |
| 11 | + |
| 12 | +class TestGetIndicesToProcess: |
| 13 | + def test_single_node_no_max_samples(self): |
| 14 | + result = get_indices_to_process(10, None, [], world_size=1, rank=0) |
| 15 | + assert result == list(range(10)) |
| 16 | + |
| 17 | + def test_single_node_with_max_samples(self): |
| 18 | + result = get_indices_to_process(10, 5, [], world_size=1, rank=0) |
| 19 | + assert result == [0, 1, 2, 3, 4] |
| 20 | + |
| 21 | + def test_single_node_max_samples_exceeds_num_samples(self): |
| 22 | + result = get_indices_to_process(5, 10, [], world_size=1, rank=0) |
| 23 | + assert result == list(range(5)) |
| 24 | + |
| 25 | + def test_single_node_with_existing(self): |
| 26 | + result = get_indices_to_process(10, None, [2, 5, 7], world_size=1, rank=0) |
| 27 | + assert result == [0, 1, 3, 4, 6, 8, 9] |
| 28 | + |
| 29 | + def test_all_samples_already_processed(self): |
| 30 | + result = get_indices_to_process(5, None, list(range(5)), world_size=1, rank=0) |
| 31 | + assert result == [] |
| 32 | + |
| 33 | + def test_multi_node_even_split(self): |
| 34 | + r0 = get_indices_to_process(10, None, [], world_size=2, rank=0) |
| 35 | + r1 = get_indices_to_process(10, None, [], world_size=2, rank=1) |
| 36 | + assert r0 == [0, 1, 2, 3, 4] |
| 37 | + assert r1 == [5, 6, 7, 8, 9] |
| 38 | + |
| 39 | + def test_multi_node_uneven_split(self): |
| 40 | + r0 = get_indices_to_process(10, None, [], world_size=3, rank=0) |
| 41 | + r1 = get_indices_to_process(10, None, [], world_size=3, rank=1) |
| 42 | + r2 = get_indices_to_process(10, None, [], world_size=3, rank=2) |
| 43 | + assert r0 == [0, 1, 2, 3] |
| 44 | + assert r1 == [4, 5, 6] |
| 45 | + assert r2 == [7, 8, 9] |
| 46 | + |
| 47 | + def test_multi_node_no_overlap_and_full_coverage(self): |
| 48 | + num_samples = 17 |
| 49 | + world_size = 4 |
| 50 | + all_indices = [] |
| 51 | + for rank in range(world_size): |
| 52 | + chunk = get_indices_to_process( |
| 53 | + num_samples, None, [], world_size=world_size, rank=rank |
| 54 | + ) |
| 55 | + all_indices.extend(chunk) |
| 56 | + assert sorted(all_indices) == list(range(num_samples)) |
| 57 | + assert len(all_indices) == len(set(all_indices)) |
| 58 | + |
| 59 | + def test_multi_node_with_max_samples(self): |
| 60 | + r0 = get_indices_to_process(100, 10, [], world_size=2, rank=0) |
| 61 | + r1 = get_indices_to_process(100, 10, [], world_size=2, rank=1) |
| 62 | + assert r0 == [0, 1, 2, 3, 4] |
| 63 | + assert r1 == [5, 6, 7, 8, 9] |
| 64 | + |
| 65 | + def test_multi_node_with_existing(self): |
| 66 | + result = get_indices_to_process(10, None, [1, 3], world_size=2, rank=0) |
| 67 | + assert result == [0, 2, 4] |
| 68 | + |
| 69 | + def test_multi_node_rank_fully_processed(self): |
| 70 | + result = get_indices_to_process( |
| 71 | + 10, None, [0, 1, 2, 3, 4], world_size=2, rank=0 |
| 72 | + ) |
| 73 | + assert result == [] |
| 74 | + |
| 75 | + def test_existing_exceeds_num_samples(self): |
| 76 | + result = get_indices_to_process(5, None, list(range(10)), world_size=1, rank=0) |
| 77 | + assert result == [] |
| 78 | + |
| 79 | + |
| 80 | +# ===== get_existing_hidden_state_indices Tests ===== |
| 81 | + |
| 82 | + |
| 83 | +class TestGetExistingHiddenStateIndices: |
| 84 | + def test_nonexistent_directory(self, tmp_path): |
| 85 | + result = get_existing_hidden_state_indices(tmp_path / "nonexistent") |
| 86 | + assert result == [] |
| 87 | + |
| 88 | + def test_empty_directory(self, tmp_path): |
| 89 | + result = get_existing_hidden_state_indices(tmp_path) |
| 90 | + assert result == [] |
| 91 | + |
| 92 | + def test_finds_safetensor_files(self, tmp_path): |
| 93 | + (tmp_path / "hs_0.safetensors").touch() |
| 94 | + (tmp_path / "hs_3.safetensors").touch() |
| 95 | + (tmp_path / "hs_7.safetensors").touch() |
| 96 | + result = get_existing_hidden_state_indices(tmp_path) |
| 97 | + assert result == [0, 3, 7] |
| 98 | + |
| 99 | + def test_ignores_non_numeric_suffixes(self, tmp_path): |
| 100 | + (tmp_path / "hs_0.safetensors").touch() |
| 101 | + (tmp_path / "hs_abc.safetensors").touch() |
| 102 | + (tmp_path / "hs_.safetensors").touch() |
| 103 | + result = get_existing_hidden_state_indices(tmp_path) |
| 104 | + assert result == [0] |
| 105 | + |
| 106 | + def test_ignores_unrelated_files(self, tmp_path): |
| 107 | + (tmp_path / "hs_0.safetensors").touch() |
| 108 | + (tmp_path / "other_file.txt").touch() |
| 109 | + (tmp_path / "hs_1.pt").touch() |
| 110 | + result = get_existing_hidden_state_indices(tmp_path) |
| 111 | + assert result == [0] |
| 112 | + |
| 113 | + def test_results_are_sorted(self, tmp_path): |
| 114 | + for i in [9, 2, 5, 0]: |
| 115 | + (tmp_path / f"hs_{i}.safetensors").touch() |
| 116 | + result = get_existing_hidden_state_indices(tmp_path) |
| 117 | + assert result == [0, 2, 5, 9] |
0 commit comments