|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import os |
| 17 | +import sys |
| 18 | +import unittest |
| 19 | + |
| 20 | +import torch |
| 21 | +import torch.distributed as dist |
| 22 | +import torch.multiprocessing as mp |
| 23 | +import torch.nn as nn |
| 24 | +import torch.optim as optim |
| 25 | +from opacus.optimizers.ddpoptimizer_fast_gradient_clipping import ( |
| 26 | + DistributedDPOptimizerFastGradientClipping, |
| 27 | +) |
| 28 | +from opacus.utils.adaptive_clipping.adaptive_clipping_utils import ( |
| 29 | + PrivacyEngineAdaptiveClipping, |
| 30 | +) |
| 31 | +from torch.nn.parallel import DistributedDataParallel as DDP |
| 32 | +from torch.utils.data import DataLoader, TensorDataset |
| 33 | +from torch.utils.data.distributed import DistributedSampler |
| 34 | + |
| 35 | + |
| 36 | +def setup(rank, world_size): |
| 37 | + if sys.platform == "win32": |
| 38 | + raise ValueError("Windows platform is not supported for this test") |
| 39 | + else: |
| 40 | + os.environ["MASTER_ADDR"] = "localhost" |
| 41 | + os.environ["MASTER_PORT"] = "12355" |
| 42 | + |
| 43 | + # initialize the process group |
| 44 | + |
| 45 | + os.environ["RANK"] = str(rank) |
| 46 | + os.environ["WORLD_SIZE"] = str(world_size) |
| 47 | + torch.distributed.init_process_group( |
| 48 | + init_method="env://", |
| 49 | + backend="nccl", |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def cleanup(): |
| 54 | + dist.destroy_process_group() |
| 55 | + |
| 56 | + |
| 57 | +class ToyModel(nn.Module): |
| 58 | + def __init__(self): |
| 59 | + super(ToyModel, self).__init__() |
| 60 | + self.net1 = nn.Linear(10, 10) |
| 61 | + self.relu = nn.ReLU() |
| 62 | + self.net2 = nn.Linear(10, 5) |
| 63 | + |
| 64 | + def forward(self, x): |
| 65 | + return self.net2(self.relu(self.net1(x))) |
| 66 | + |
| 67 | + |
| 68 | +def demo_basic(rank, weight, world_size, dp): |
| 69 | + torch.manual_seed(world_size) |
| 70 | + batch_size = 32 |
| 71 | + setup(rank, world_size) |
| 72 | + |
| 73 | + # create model and move it to GPU with id rank |
| 74 | + model = ToyModel().to(rank) |
| 75 | + model.net1.weight.data.zero_() |
| 76 | + optimizer = optim.SGD(model.parameters(), lr=1) |
| 77 | + |
| 78 | + # create dataset |
| 79 | + labels = torch.randn(2 * batch_size, 5).to(rank) |
| 80 | + data = torch.randn(2 * batch_size, 10) |
| 81 | + dataset = TensorDataset(data, labels) |
| 82 | + |
| 83 | + criterion = nn.CrossEntropyLoss(reduction="mean") |
| 84 | + |
| 85 | + max_grad_norm = 1e8 |
| 86 | + |
| 87 | + ddp_model = DDP(model, device_ids=[rank]) |
| 88 | + |
| 89 | + privacy_engine = PrivacyEngineAdaptiveClipping() |
| 90 | + |
| 91 | + sampler = DistributedSampler( |
| 92 | + dataset, num_replicas=world_size, rank=rank, shuffle=False |
| 93 | + ) |
| 94 | + data_loader = DataLoader(dataset, batch_size=batch_size, sampler=sampler) |
| 95 | + |
| 96 | + if dp: |
| 97 | + ddp_model, optimizer, criterion, data_loader = privacy_engine.make_private( |
| 98 | + module=ddp_model, |
| 99 | + optimizer=optimizer, |
| 100 | + criterion=criterion, |
| 101 | + data_loader=data_loader, |
| 102 | + noise_multiplier=0, |
| 103 | + max_grad_norm=max_grad_norm, |
| 104 | + poisson_sampling=False, |
| 105 | + grad_sample_mode="ghost", |
| 106 | + target_unclipped_quantile=1.0, |
| 107 | + ) |
| 108 | + assert isinstance(optimizer, DistributedDPOptimizerFastGradientClipping) |
| 109 | + |
| 110 | + for x, y in data_loader: |
| 111 | + outputs = ddp_model(x.to(rank)) |
| 112 | + loss = criterion(outputs, y) |
| 113 | + optimizer.zero_grad() |
| 114 | + loss.backward() |
| 115 | + optimizer.step() |
| 116 | + break |
| 117 | + |
| 118 | + weight.copy_(model.net1.weight.data.cpu()) |
| 119 | + cleanup() |
| 120 | + |
| 121 | + |
| 122 | +def run_demo(demo_fn, weight, world_size, dp): |
| 123 | + mp.spawn( |
| 124 | + demo_fn, |
| 125 | + args=(weight, world_size, dp), |
| 126 | + nprocs=world_size, |
| 127 | + join=True, |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +class GradientComputationTestAdaptiveClipping(unittest.TestCase): |
| 132 | + def test_gradient_correct_adaptive(self) -> None: |
| 133 | + |
| 134 | + # Tests that gradient is the same with DP or without DP in the distributed setting |
| 135 | + n_gpus = torch.cuda.device_count() |
| 136 | + self.assertTrue( |
| 137 | + n_gpus >= 2, f"Need at least 2 gpus but was provided only {n_gpus}." |
| 138 | + ) |
| 139 | + |
| 140 | + weight_dp, weight_nodp = torch.ones(10, 10), torch.ones(10, 10) |
| 141 | + |
| 142 | + run_demo( |
| 143 | + demo_basic, |
| 144 | + weight_nodp, |
| 145 | + 2, |
| 146 | + dp=False, |
| 147 | + ) |
| 148 | + run_demo( |
| 149 | + demo_basic, |
| 150 | + weight_dp, |
| 151 | + 2, |
| 152 | + dp=True, |
| 153 | + ) |
| 154 | + |
| 155 | + self.assertTrue(torch.allclose(weight_dp, weight_nodp, atol=1e-5, rtol=1e-3)) |
0 commit comments