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

require python 3.7 + minor refactoring #277

Merged
merged 4 commits into from
Nov 25, 2021
Merged
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
16 changes: 8 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ version: 2.1
commands:

py_3_7_setup:
description: "Install and switch to Python 3.7.10; also install pip and pytest."
description: "Install and switch to Python 3.7.4; also install pip and pytest."
steps:
- run:
name: "Setup Python v3.7.10 environment"
name: "Setup Python v3.7.4 environment"
command: |
cd /opt/circleci/.pyenv && git pull && cd -
pyenv install -s 3.7.10
pyenv global 3.7.10
pyenv local 3.7.10
pyenv install -s 3.7.4
pyenv global 3.7.4
pyenv local 3.7.4
pyenv versions
echo "In venv: $(pyenv local) - $(python -V), $(pip -V)"
sudo "$(which python)" -m pip install --upgrade pip
Expand Down Expand Up @@ -259,7 +259,7 @@ jobs:

lint_py37_torch_release:
docker:
- image: cimg/python:3.7
- image: cimg/python:3.7.4
steps:
- checkout
- pip_dev_install
Expand All @@ -269,7 +269,7 @@ jobs:

unittest_py37_torch_release:
docker:
- image: cimg/python:3.7
- image: cimg/python:3.7.4
steps:
- checkout
- pip_dev_install
Expand Down Expand Up @@ -302,7 +302,7 @@ jobs:

integrationtest_py37_torch_release_cpu:
docker:
- image: cimg/python:3.7
- image: cimg/python:3.7.4
steps:
- checkout
- pip_dev_install
Expand Down
17 changes: 17 additions & 0 deletions opacus/accountants/accountant.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import abc
from typing import Callable

from opacus.optimizers import DPOptimizer


class IAccountant(abc.ABC):
Expand All @@ -22,3 +25,17 @@ def __len__(self) -> int:
@abc.abstractmethod
def mechanism(cls) -> str:
pass

def get_optimizer_hook_fn(
self, sample_rate: float
) -> Callable[[DPOptimizer], None]:
def hook_fn(optim: DPOptimizer):
# This works for Poisson for both single-node and distributed
# The reason is that the sample rate is the same in both cases (but in
# distributed mode, each node samples among a subset of the data)
self.step(
noise_multiplier=optim.noise_multiplier,
sample_rate=sample_rate * optim.accumulated_iterations,
)

return hook_fn
13 changes: 3 additions & 10 deletions opacus/privacy_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,9 @@ def make_private(
clipping=clipping,
)

def accountant_hook(optim: DPOptimizer):
# This works for Poisson for both single-node and distributed
# The reason is that the sample rate is the same in both cases (but in
# distributed mode, each node samples among a subset of the data)
self.accountant.step(
noise_multiplier=optim.noise_multiplier,
sample_rate=sample_rate * optim.accumulated_iterations,
)

optimizer.attach_step_hook(accountant_hook)
optimizer.attach_step_hook(
self.accountant.get_optimizer_hook_fn(sample_rate=sample_rate)
)

return module, optimizer, data_loader

Expand Down
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
from setuptools import find_packages, setup


# 3.6.8 is the final Windows binary release for 3.6.x
REQUIRED_MAJOR = 3
REQUIRED_MINOR = 6
REQUIRED_MICRO = 8

REQUIRED_MINOR = 7
REQUIRED_MICRO = 4

version = {}
with open("opacus/version.py") as fp:
Expand Down