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

chore: rename use_gpu to disable_gpu in TorchAlgo #241

Merged
merged 8 commits into from
Aug 14, 2024
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
1 change: 1 addition & 0 deletions changes/241.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BREAKING: change `use_gpu` to `diasble_gpu` in all `TorchAlgo`. The device is set to `cpu` is no GPU are available or if `disable_gpu` is set to `True`. You must inverse the boolean in your code to keep the same behaviour (`diasble_gpu == not use_gpu`).
10 changes: 5 additions & 5 deletions substrafl/algorithms/pytorch/torch_base_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(
optimizer: Optional[torch.optim.Optimizer] = None,
scheduler: Optional[torch.optim.lr_scheduler._LRScheduler] = None,
seed: Optional[int] = None,
use_gpu: bool = True,
disable_gpu: bool = False,
*args,
**kwargs,
):
Expand All @@ -63,7 +63,7 @@ def __init__(
np.random.seed(seed)
torch.manual_seed(seed)

self._device = self._get_torch_device(use_gpu=use_gpu)
self._device = self._get_torch_device(disable_gpu=disable_gpu)

self._model = model.to(self._device)
self._optimizer = optimizer
Expand Down Expand Up @@ -212,18 +212,18 @@ def _local_train(
if self._scheduler is not None:
self._scheduler.step()

def _get_torch_device(self, use_gpu: bool) -> torch.device:
def _get_torch_device(self, disable_gpu: bool) -> torch.device:
"""Get the torch device, CPU or GPU, depending
on availability and user input.

Args:
use_gpu (bool): whether to use GPUs if available or not.
disable_gpu (bool): whether to use GPUs if available or not.

Returns:
torch.device: Torch device
"""
device = torch.device("cpu")
if use_gpu and torch.cuda.is_available():
if not disable_gpu and torch.cuda.is_available():
device = torch.device("cuda")
return device

Expand Down
7 changes: 4 additions & 3 deletions substrafl/algorithms/pytorch/torch_fed_avg_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def __init__(
scheduler: Optional[torch.optim.lr_scheduler._LRScheduler] = None,
with_batch_norm_parameters: bool = False,
seed: Optional[int] = None,
use_gpu: bool = True,
disable_gpu: bool = False,
*args,
**kwargs,
):
Expand Down Expand Up @@ -125,7 +125,8 @@ def __init__(
with_batch_norm_parameters (bool): Whether to include the batch norm layer parameters in the fed avg
strategy. Defaults to False.
seed (typing.Optional[int]): Seed set at the algo initialization on each organization. Defaults to None.
use_gpu (bool): Whether to use the GPUs if they are available. Defaults to True.
disable_gpu (bool): Force disabling GPU usage. If False, GPU will used if available, else CPU will be used.
Defaults to False.
"""
super().__init__(
*args,
Expand All @@ -136,7 +137,7 @@ def __init__(
dataset=dataset,
scheduler=scheduler,
seed=seed,
use_gpu=use_gpu,
disable_gpu=disable_gpu,
**kwargs,
)
self._with_batch_norm_parameters = with_batch_norm_parameters
Expand Down
6 changes: 3 additions & 3 deletions substrafl/algorithms/pytorch/torch_fed_pca_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(
out_features: int,
batch_size: Optional[int] = None,
seed: int = 1,
use_gpu: bool = True,
disable_gpu: bool = False,
*args,
**kwargs,
):
Expand All @@ -101,7 +101,7 @@ def __init__(
out_features (int): dimension to keep after PCA
batch_size (Optional[int]): mini-batch size
seed (int): random generator seed. The seed is mandatory. Default to 1.
use_gpu (bool): whether to use GPU or not. Default to True.
disable_gpu (bool): force to disable GPUs usage. Defaults to False.
"""
self.in_features = in_features
self.out_features = out_features
Expand All @@ -122,7 +122,7 @@ def __init__(
index_generator=None,
dataset=dataset,
seed=self._seed,
use_gpu=use_gpu,
disable_gpu=disable_gpu,
**kwargs,
)

Expand Down
7 changes: 4 additions & 3 deletions substrafl/algorithms/pytorch/torch_newton_raphson_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(
l2_coeff: float = 0,
with_batch_norm_parameters: bool = False,
seed: Optional[int] = None,
use_gpu: bool = True,
disable_gpu: bool = False,
*args,
**kwargs,
):
Expand Down Expand Up @@ -80,7 +80,8 @@ def __init__(
with_batch_norm_parameters (bool): Whether to include the batch norm layer parameters in the Newton-Raphson
strategy. Defaults to False.
seed (typing.Optional[int]): Seed set at the algo initialization on each organization. Defaults to None.
use_gpu (bool): Whether to use the GPUs if they are available. Defaults to True.
disable_gpu (bool): Force disabling GPU usage. If False, GPU will used if available, else CPU will be used.
Defaults to False.
"""
assert "optimizer" not in kwargs, "Newton Raphson strategy does not uses optimizers"

Expand All @@ -91,7 +92,7 @@ def __init__(
optimizer=None,
index_generator=None,
dataset=dataset,
use_gpu=use_gpu,
disable_gpu=disable_gpu,
seed=seed,
**kwargs,
)
Expand Down
7 changes: 4 additions & 3 deletions substrafl/algorithms/pytorch/torch_scaffold_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def __init__(
with_batch_norm_parameters: bool = False,
c_update_rule: CUpdateRule = CUpdateRule.FAST,
seed: Optional[int] = None,
use_gpu: bool = True,
disable_gpu: bool = False,
*args,
**kwargs,
):
Expand Down Expand Up @@ -153,7 +153,8 @@ def __init__(
client control variate.
Defaults to CUpdateRule.FAST.
seed (typing.Optional[int]): Seed set at the algo initialization on each organization. Defaults to None.
use_gpu (bool): Whether to use the GPUs if they are available. Defaults to True.
disable_gpu (bool): Force disabling GPU usage. If False, GPU will used if available, else CPU will be used.
Defaults to False.
Raises:
:ref:`~substrafl.exceptions.NumUpdatesValueError`: If `num_updates` is inferior or equal to zero.
"""
Expand All @@ -165,7 +166,7 @@ def __init__(
index_generator=index_generator,
dataset=dataset,
scheduler=scheduler,
use_gpu=use_gpu,
disable_gpu=disable_gpu,
seed=seed,
**kwargs,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(
dataset: torch.utils.data.Dataset,
scheduler: Optional[torch.optim.lr_scheduler._LRScheduler] = None,
seed: Optional[int] = None,
use_gpu: bool = True,
disable_gpu: bool = False,
*args,
**kwargs,
):
Expand Down Expand Up @@ -121,7 +121,8 @@ def __init__(
scheduler (torch.optim.lr_scheduler._LRScheduler, Optional): A torch scheduler that will be called at every
batch. If None, no scheduler will be used. Defaults to None.
seed (typing.Optional[int]): Seed set at the algo initialization on each organization. Defaults to None.
use_gpu (bool): Whether to use the GPUs if they are available. Defaults to True.
disable_gpu (bool): Force disabling GPU usage. If False, GPU will used if available, else CPU will be used.
Defaults to False.
"""
super().__init__(
*args,
Expand All @@ -132,7 +133,7 @@ def __init__(
dataset=dataset,
scheduler=scheduler,
seed=seed,
use_gpu=use_gpu,
disable_gpu=disable_gpu,
**kwargs,
)

Expand Down
24 changes: 12 additions & 12 deletions tests/algorithms/pytorch/test_base_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def __init__(self):
optimizer=torch.optim.SGD(perceptron.parameters(), lr=0.1),
index_generator=nig,
seed=test_seed,
use_gpu=False,
)

@property
Expand Down Expand Up @@ -161,8 +160,8 @@ def train(self, data_from_opener, shared_state):
return MyAlgo


@pytest.fixture(params=[pytest.param(True, marks=pytest.mark.gpu), False])
def use_gpu(request):
@pytest.fixture(params=[pytest.param(False, marks=pytest.mark.gpu), True])
def disable_gpu(request):
return request.param


Expand All @@ -174,7 +173,7 @@ def use_gpu(request):
(TorchNewtonRaphsonAlgo, NewtonRaphson),
]
)
def dummy_gpu(request, torch_linear_model, use_gpu, numpy_torch_dataset):
def dummy_gpu(request, torch_linear_model, disable_gpu, numpy_torch_dataset):
nig = NpIndexGenerator(
batch_size=1,
num_updates=1,
Expand All @@ -189,7 +188,7 @@ def __init__(self):
criterion=torch.nn.MSELoss(),
dataset=numpy_torch_dataset,
batch_size=1,
use_gpu=use_gpu,
disable_gpu=disable_gpu,
)
else:
super().__init__(
Expand All @@ -198,18 +197,18 @@ def __init__(self):
criterion=torch.nn.MSELoss(),
dataset=numpy_torch_dataset,
index_generator=nig,
use_gpu=use_gpu,
disable_gpu=disable_gpu,
)
if use_gpu:
assert self._device == torch.device("cuda")
else:
if disable_gpu or not torch.cuda.is_available():
assert self._device == torch.device("cpu")
else:
SdgJlbl marked this conversation as resolved.
Show resolved Hide resolved
assert self._device == torch.device("cuda")

@property
def strategies(self):
return list(StrategyName)

return MyAlgo, request.param[1], use_gpu
return MyAlgo, request.param[1], disable_gpu


def test_base_algo_custom_init_arg_default_value(session_dir, dummy_algo_custom_init_arg):
Expand Down Expand Up @@ -469,7 +468,7 @@ def test_gpu(
aggregation_node,
):
num_rounds = 2
algo_class, strategy_class, use_gpu = dummy_gpu
algo_class, strategy_class, disable_gpu = dummy_gpu
my_algo = algo_class()
algo_deps = Dependency(
pypi_dependencies=["torch==2.2.1", "numpy==1.26.4", "pytest"],
Expand Down Expand Up @@ -499,7 +498,8 @@ def test_gpu(
dependencies=algo_deps,
experiment_folder=session_dir / "experiment_folder",
clean_models=False,
name=f'Testing the GPU - strategy {strategy_class.__name__}, running on {"cuda" if use_gpu else "cpu"}',
name=f'Testing the GPU - strategy {strategy_class.__name__}, running on \
{"cpu" if (disable_gpu or not torch.cuda.is_available()) else "cuda"}',
)

# Wait for the compute plan to be finished
Expand Down
1 change: 0 additions & 1 deletion tests/algorithms/pytorch/test_fed_avg.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def __init__(
model=perceptron,
index_generator=nig,
dataset=numpy_torch_dataset,
use_gpu=False,
)

return MyAlgo
Expand Down
1 change: 0 additions & 1 deletion tests/algorithms/pytorch/test_fed_pca_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def __init__(self, batch_size=1):
batch_size=batch_size,
dataset=numpy_torch_dataset,
seed=seed,
use_gpu=False,
)

return MyAlgo
Expand Down
2 changes: 0 additions & 2 deletions tests/algorithms/pytorch/test_newton_raphson.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ def __init__(self):
batch_size=BATCH_SIZE,
dataset=numpy_torch_dataset,
l2_coeff=0,
use_gpu=False,
)

my_algo = MyAlgo()
Expand Down Expand Up @@ -435,7 +434,6 @@ def __init__(self):
batch_size=BATCH_SIZE,
dataset=numpy_torch_dataset,
l2_coeff=0,
use_gpu=False,
)

my_algo = MyAlgo()
Expand Down
1 change: 0 additions & 1 deletion tests/algorithms/pytorch/test_scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def __init__(
index_generator=nig,
dataset=numpy_torch_dataset,
scheduler=scheduler,
use_gpu=False,
)

return MyAlgo
Expand Down
2 changes: 0 additions & 2 deletions tests/algorithms/pytorch/test_single_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def __init__(
model=perceptron,
index_generator=nig,
dataset=numpy_torch_dataset,
use_gpu=False,
)

my_algo = MySingleOrganizationAlgo()
Expand Down Expand Up @@ -107,7 +106,6 @@ def __init__(
model=perceptron,
index_generator=nig,
dataset=numpy_torch_dataset,
use_gpu=False,
)

my_algo = MySingleOrganizationAlgo()
Expand Down
Loading