|
8 | 8 | # pyre-strict
|
9 | 9 |
|
10 | 10 | import unittest
|
| 11 | +from typing import Dict, List, Union |
11 | 12 | from unittest.mock import MagicMock, patch
|
12 | 13 |
|
13 | 14 | import torch
|
14 | 15 | from torch.autograd import Variable
|
| 16 | +from torch.distributed import ProcessGroup |
| 17 | +from torch.distributed.tensor import distribute_tensor, DTensor, init_device_mesh, Shard |
| 18 | +from torch.testing._internal.common_utils import ( |
| 19 | + instantiate_parametrized_tests, |
| 20 | + parametrize, |
| 21 | +) |
| 22 | +from torch.testing._internal.distributed._tensor.common_dtensor import ( |
| 23 | + DTensorTestBase, |
| 24 | + with_comms, |
| 25 | +) |
15 | 26 | from torchrec.optim.clipping import GradientClipping, GradientClippingOptimizer
|
16 | 27 | from torchrec.optim.test_utils import DummyKeyedOptimizer
|
17 | 28 |
|
@@ -229,3 +240,99 @@ def test_clip_no_gradients_norm_meta_device(
|
229 | 240 | gradient_clipping_optimizer.step()
|
230 | 241 |
|
231 | 242 | mock_clip_grad_norm.assert_not_called()
|
| 243 | + |
| 244 | + |
| 245 | +@unittest.skipIf(not torch.cuda.is_available(), "Skip when CUDA is not available") |
| 246 | +@instantiate_parametrized_tests |
| 247 | +class TestGradientClippingDTensor(DTensorTestBase): |
| 248 | + def _get_params_to_pg( |
| 249 | + self, params: List[DTensor] |
| 250 | + ) -> Dict[DTensor, List[ProcessGroup]]: |
| 251 | + return {param: [param.device_mesh.get_group()] for param in params} |
| 252 | + |
| 253 | + @with_comms |
| 254 | + @parametrize("norm_type", ("inf",)) |
| 255 | + def test_dtensor_clip_all_gradients_norm( |
| 256 | + self, norm_type: Union[float, str] |
| 257 | + ) -> None: |
| 258 | + """ |
| 259 | + Test to ensure that the gradient clipping optimizer clips gradients |
| 260 | + correctly with mixed DTensor and tensor by comparing gradients to its |
| 261 | + torch.tensor counterpart. |
| 262 | +
|
| 263 | + Note that clipping for DTensor may require communication. |
| 264 | + """ |
| 265 | + |
| 266 | + # create gradient clipping optimizer containing no dtensor for reference |
| 267 | + ref_param_1 = torch.nn.Parameter( |
| 268 | + torch.tensor([1.0, 2.0, 3.0], device=self.device_type) |
| 269 | + ) |
| 270 | + ref_param_2 = torch.nn.Parameter( |
| 271 | + torch.tensor([4.0, 5.0, 6.0], device=self.device_type) |
| 272 | + ) |
| 273 | + ref_keyed_optimizer = DummyKeyedOptimizer( |
| 274 | + {"param_1": ref_param_1, "param_2": ref_param_2}, |
| 275 | + {}, |
| 276 | + [{"params": [ref_param_1, ref_param_2]}], |
| 277 | + ) |
| 278 | + ref_gradient_clipping_optimizer = GradientClippingOptimizer( |
| 279 | + optimizer=ref_keyed_optimizer, |
| 280 | + clipping=GradientClipping.NORM, |
| 281 | + max_gradient=10.0, |
| 282 | + norm_type=norm_type, |
| 283 | + ) |
| 284 | + ref_gradient_clipping_optimizer.zero_grad() |
| 285 | + ref_param_1.grad = torch.tensor([12.0, 15.0, 18.0], device=self.device_type) |
| 286 | + ref_param_2.grad = torch.tensor([20.0, 30.0, 15.0], device=self.device_type) |
| 287 | + ref_gradient_clipping_optimizer.step() |
| 288 | + |
| 289 | + # create gradient clipping optimizer containing both DTensor and tensor |
| 290 | + device_mesh = init_device_mesh(self.device_type, (self.world_size,)) |
| 291 | + param_1 = distribute_tensor( |
| 292 | + torch.tensor([1.0, 2.0, 3.0], requires_grad=True, device=self.device_type), |
| 293 | + device_mesh, |
| 294 | + [Shard(0)], |
| 295 | + ) |
| 296 | + param_2 = torch.tensor( |
| 297 | + [4.0, 5.0, 6.0], requires_grad=True, device=self.device_type |
| 298 | + ) |
| 299 | + param_to_pgs = self._get_params_to_pg([param_1]) |
| 300 | + keyed_optimizer = DummyKeyedOptimizer( |
| 301 | + {"dtensor_param_1": param_1, "dtensor_param_2": param_2}, |
| 302 | + {}, |
| 303 | + [{"params": [param_1, param_2]}], |
| 304 | + ) |
| 305 | + gradient_clipping_optimizer = GradientClippingOptimizer( |
| 306 | + optimizer=keyed_optimizer, |
| 307 | + clipping=GradientClipping.NORM, |
| 308 | + max_gradient=10.0, |
| 309 | + norm_type=norm_type, |
| 310 | + enable_global_grad_clip=True, |
| 311 | + param_to_pgs=param_to_pgs, # pyre-ignore[6] |
| 312 | + ) |
| 313 | + gradient_clipping_optimizer.zero_grad() |
| 314 | + param_1.grad = distribute_tensor( |
| 315 | + torch.tensor([12.0, 15.0, 18.0], device=self.device_type), |
| 316 | + device_mesh, |
| 317 | + [Shard(0)], |
| 318 | + ) |
| 319 | + param_2.grad = torch.tensor([20.0, 30.0, 15.0], device=self.device_type) |
| 320 | + gradient_clipping_optimizer.step() |
| 321 | + |
| 322 | + for param_group, ref_param_group in zip( |
| 323 | + gradient_clipping_optimizer.param_groups, |
| 324 | + ref_gradient_clipping_optimizer.param_groups, |
| 325 | + ): |
| 326 | + for param, ref_param in zip( |
| 327 | + param_group["params"], ref_param_group["params"] |
| 328 | + ): |
| 329 | + param_grad = ( |
| 330 | + param.grad.full_tensor() # pyre-ignore[16] |
| 331 | + if isinstance(param, DTensor) |
| 332 | + else param.grad |
| 333 | + ) |
| 334 | + self.assertEqual( |
| 335 | + param_grad, |
| 336 | + ref_param.grad, |
| 337 | + f"Expect gradient to be the same. However, found {param_grad=}, {ref_param.grad=}", |
| 338 | + ) |
0 commit comments