|
9 | 9 |
|
10 | 10 | import unittest |
11 | 11 | from functools import partial, update_wrapper |
12 | | -from typing import Callable, Dict, Optional, Type |
| 12 | +from typing import Any, Callable, cast, Dict, List, Optional, Type |
| 13 | +from unittest.mock import patch |
13 | 14 |
|
14 | 15 | import torch |
| 16 | +import torchrec.metrics.rec_metric as rec_metric |
| 17 | +from torchrec.metrics.metrics_config import DefaultTaskInfo |
15 | 18 | from torchrec.metrics.ne import ( |
16 | 19 | compute_cross_entropy, |
17 | 20 | compute_logloss, |
18 | 21 | compute_ne, |
19 | 22 | NEMetric, |
20 | 23 | ) |
21 | | -from torchrec.metrics.rec_metric import RecComputeMode, RecMetric |
| 24 | +from torchrec.metrics.rec_metric import RecComputeMode, RecMetric, RecMetricComputation |
22 | 25 | from torchrec.metrics.test_utils import ( |
23 | 26 | metric_test_helper, |
24 | 27 | rec_metric_gpu_sync_test_launcher, |
@@ -115,6 +118,149 @@ class NEMetricTest(unittest.TestCase): |
115 | 118 | target_compute_mode: RecComputeMode = RecComputeMode.UNFUSED_TASKS_COMPUTATION |
116 | 119 | task_name: str = "ne" |
117 | 120 |
|
| 121 | + @staticmethod |
| 122 | + def _set_distinct_sync_states( |
| 123 | + computation: RecMetricComputation, |
| 124 | + ) -> Dict[str, torch.Tensor]: |
| 125 | + defaults = cast(Dict[str, Any], cast(Any, computation)._defaults) |
| 126 | + expected: Dict[str, torch.Tensor] = {} |
| 127 | + for value, name in enumerate(defaults, start=1): |
| 128 | + state = cast(torch.Tensor, getattr(computation, name)) |
| 129 | + state.fill_(value) |
| 130 | + expected[name] = state.clone() |
| 131 | + return expected |
| 132 | + |
| 133 | + def test_fixed_shape_sync_path(self) -> None: |
| 134 | + ne = NEMetric( |
| 135 | + world_size=1, |
| 136 | + my_rank=0, |
| 137 | + batch_size=1, |
| 138 | + tasks=[DefaultTaskInfo], |
| 139 | + ) |
| 140 | + computation = cast(RecMetricComputation, ne._metrics_computations[0]) |
| 141 | + |
| 142 | + with ( |
| 143 | + patch.object(torch._utils_internal, "justknobs_check", return_value=True), |
| 144 | + patch.object(torch.distributed, "get_world_size", return_value=1), |
| 145 | + patch.object(torch.distributed, "barrier") as barrier, |
| 146 | + patch.object(torch.distributed, "all_gather") as all_gather, |
| 147 | + ): |
| 148 | + computation.sync(distributed_available=lambda: True) |
| 149 | + |
| 150 | + barrier.assert_not_called() |
| 151 | + self.assertGreater(all_gather.call_count, 0) |
| 152 | + |
| 153 | + def test_fixed_shape_sync_killswitch_uses_variable_shape_path(self) -> None: |
| 154 | + ne = NEMetric( |
| 155 | + world_size=1, |
| 156 | + my_rank=0, |
| 157 | + batch_size=1, |
| 158 | + tasks=[DefaultTaskInfo], |
| 159 | + ) |
| 160 | + computation = cast(RecMetricComputation, ne._metrics_computations[0]) |
| 161 | + |
| 162 | + with ( |
| 163 | + patch.object(torch._utils_internal, "justknobs_check", return_value=False), |
| 164 | + patch.object(torch.distributed, "get_world_size", return_value=1), |
| 165 | + patch.object(torch.distributed, "barrier") as barrier, |
| 166 | + patch.object(torch.distributed, "all_gather"), |
| 167 | + ): |
| 168 | + computation.sync(distributed_available=lambda: True) |
| 169 | + |
| 170 | + barrier.assert_called() |
| 171 | + |
| 172 | + def test_runtime_shape_change_uses_variable_shape_path(self) -> None: |
| 173 | + ne = NEMetric( |
| 174 | + world_size=1, |
| 175 | + my_rank=0, |
| 176 | + batch_size=1, |
| 177 | + tasks=[DefaultTaskInfo], |
| 178 | + ) |
| 179 | + computation = cast(RecMetricComputation, ne._metrics_computations[0]) |
| 180 | + cast(Any, computation).cross_entropy_sum = torch.zeros(2, dtype=torch.double) |
| 181 | + |
| 182 | + with ( |
| 183 | + patch.object(torch._utils_internal, "justknobs_check", return_value=True), |
| 184 | + patch.object(torch.distributed, "get_world_size", return_value=1), |
| 185 | + patch.object(torch.distributed, "barrier") as barrier, |
| 186 | + patch.object(torch.distributed, "all_gather"), |
| 187 | + ): |
| 188 | + computation.sync(distributed_available=lambda: True) |
| 189 | + |
| 190 | + barrier.assert_called() |
| 191 | + |
| 192 | + def test_missing_state_defaults_disables_fixed_shape_sync(self) -> None: |
| 193 | + ne = NEMetric( |
| 194 | + world_size=1, |
| 195 | + my_rank=0, |
| 196 | + batch_size=1, |
| 197 | + tasks=[DefaultTaskInfo], |
| 198 | + ) |
| 199 | + computation = cast(RecMetricComputation, ne._metrics_computations[0]) |
| 200 | + defaults = cast(Any, computation)._defaults |
| 201 | + delattr(computation, "_defaults") |
| 202 | + try: |
| 203 | + self.assertFalse(computation._state_shapes_match_defaults()) |
| 204 | + finally: |
| 205 | + cast(Any, computation)._defaults = defaults |
| 206 | + |
| 207 | + def test_upstream_gather_without_fixed_shape_support_is_unchanged(self) -> None: |
| 208 | + ne = NEMetric( |
| 209 | + world_size=1, |
| 210 | + my_rank=0, |
| 211 | + batch_size=1, |
| 212 | + tasks=[DefaultTaskInfo], |
| 213 | + ) |
| 214 | + computation = cast(RecMetricComputation, ne._metrics_computations[0]) |
| 215 | + expected = self._set_distinct_sync_states(computation) |
| 216 | + synced_tensors: List[torch.Tensor] = [] |
| 217 | + |
| 218 | + def upstream_gather( |
| 219 | + tensor: torch.Tensor, group: Optional[Any] = None |
| 220 | + ) -> List[torch.Tensor]: |
| 221 | + synced_tensors.append(tensor) |
| 222 | + return [tensor] |
| 223 | + |
| 224 | + with patch.object(rec_metric, "gather_all_tensors", upstream_gather): |
| 225 | + computation.sync( |
| 226 | + dist_sync_fn=upstream_gather, |
| 227 | + distributed_available=lambda: True, |
| 228 | + ) |
| 229 | + |
| 230 | + self.assertEqual(len(synced_tensors), len(expected)) |
| 231 | + for actual, expected_tensor in zip(synced_tensors, expected.values()): |
| 232 | + torch.testing.assert_close(actual, expected_tensor) |
| 233 | + for name, expected_tensor in expected.items(): |
| 234 | + torch.testing.assert_close(getattr(computation, name), expected_tensor) |
| 235 | + |
| 236 | + def test_custom_sync_path_is_unchanged(self) -> None: |
| 237 | + ne = NEMetric( |
| 238 | + world_size=1, |
| 239 | + my_rank=0, |
| 240 | + batch_size=1, |
| 241 | + tasks=[DefaultTaskInfo], |
| 242 | + ) |
| 243 | + computation = cast(RecMetricComputation, ne._metrics_computations[0]) |
| 244 | + expected = self._set_distinct_sync_states(computation) |
| 245 | + synced_tensors: List[torch.Tensor] = [] |
| 246 | + |
| 247 | + def custom_sync( |
| 248 | + tensor: torch.Tensor, group: Optional[Any] = None |
| 249 | + ) -> List[torch.Tensor]: |
| 250 | + synced_tensors.append(tensor) |
| 251 | + return [tensor] |
| 252 | + |
| 253 | + computation.sync( |
| 254 | + dist_sync_fn=custom_sync, |
| 255 | + distributed_available=lambda: True, |
| 256 | + ) |
| 257 | + |
| 258 | + self.assertEqual(len(synced_tensors), len(expected)) |
| 259 | + for actual, expected_tensor in zip(synced_tensors, expected.values()): |
| 260 | + torch.testing.assert_close(actual, expected_tensor) |
| 261 | + for name, expected_tensor in expected.items(): |
| 262 | + torch.testing.assert_close(getattr(computation, name), expected_tensor) |
| 263 | + |
118 | 264 | def test_ne_unfused(self) -> None: |
119 | 265 | rec_metric_value_test_launcher( |
120 | 266 | target_clazz=NEMetric, |
|
0 commit comments