Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update lightning example to lightning 2.0 #603

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions examples/mnist_lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,8 @@ def configure_optimizers(self):
optimizer = optim.SGD(self.parameters(), lr=self.lr, momentum=0)

if self.enable_dp:
data_loader = (
# soon there will be a fancy way to access train dataloader,
# see https://github.com/PyTorchLightning/pytorch-lightning/issues/10430
self.trainer._data_connector._train_dataloader_source.dataloader()
)
self.trainer.fit_loop.setup_data()
data_loader = self.trainer.train_dataloader

# transform (model, optimizer, dataloader) to DP-versions
if hasattr(self, "dp"):
Expand Down
11 changes: 8 additions & 3 deletions opacus/utils/batch_memory_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
from typing import List

import numpy as np
from torch.utils.data import BatchSampler, DataLoader, Sampler

from opacus.optimizers import DPOptimizer
from opacus.utils.uniform_sampler import (
DistributedUniformWithReplacementSampler,
UniformWithReplacementSampler,
)
from torch.utils.data import BatchSampler, DataLoader, Sampler


class BatchSplittingSampler(Sampler[List[int]]):
Expand Down Expand Up @@ -71,13 +72,17 @@ def __iter__(self):
def __len__(self):
if isinstance(self.sampler, BatchSampler):
return int(
len(self.sampler) * (self.sampler.batch_size / self.max_batch_size)
np.ceil(
len(self.sampler) * (self.sampler.batch_size / self.max_batch_size)
)
)
elif isinstance(self.sampler, UniformWithReplacementSampler) or isinstance(
self.sampler, DistributedUniformWithReplacementSampler
):
expected_batch_size = self.sampler.sample_rate * self.sampler.num_samples
return int(len(self.sampler) * (expected_batch_size / self.max_batch_size))
return int(
np.ceil(len(self.sampler) * (expected_batch_size / self.max_batch_size))
)

return len(self.sampler)

Expand Down