Skip to content

Commit c7225a2

Browse files
iden-kalemajfacebook-github-bot
authored andcommittedFeb 3, 2025·
Fix failing Github tests (#726)
Summary: Pull Request resolved: #726 (1) CIFAR10 cuda tests were failing with the following message. [Example](https://github.com/pytorch/opacus/actions/runs/13079011269/job/36498002040#step:7:757). > _pickle.UnpicklingError: Weights only load failed [...] In PyTorch 2.6, we changed the default value of the `weights_only` argument in `torch.load` from `False` to `True`. Re-running `torch.load` with `weights_only` set to `False` will likely succeed, but it can result in arbitrary code execution. Do it only if you got the file from a trusted source. Specified weights_only=False in torch.load() and tests are passing. (2) A similar issue was happening with multiple unit tests due to the change in behavior of `weights_only`. [Example](https://github.com/pytorch/opacus/actions/runs/13080274224/job/36502044189). Changed the `weights_only` setting where necessary. (3) Black was requesting addition of commas when listing mutliple variable types. (Locally there were no issues, appeared only GithubActions). [Example](https://github.com/pytorch/opacus/actions/runs/13080274224/job/36502043210). Reviewed By: EnayatUllah Differential Revision: D68973109 fbshipit-source-id: 7fbe9702dcfc72b40fe95bb8a749d2163525231b
1 parent 57f0349 commit c7225a2

File tree

5 files changed

+7
-7
lines changed

5 files changed

+7
-7
lines changed
 

‎.github/workflows/ci_gpu.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ jobs:
8484
mkdir -p runs/cifar10/test-reports
8585
pip install tensorboard
8686
python examples/cifar10.py --lr 0.1 --sigma 1.5 -c 10 --batch-size 2000 --epochs 10 --data-root runs/cifar10/data --log-dir runs/cifar10/logs --device cuda
87-
python -c "import torch; model = torch.load('model_best.pth.tar'); exit(0) if (model['best_acc1']>0.4 and model['best_acc1']<0.49) else exit(1)"
87+
python -c "import torch; model = torch.load('model_best.pth.tar', weights_only=False); exit(0) if (model['best_acc1']>0.4 and model['best_acc1']<0.49) else exit(1)"
8888
python examples/cifar10.py --lr 0.1 --sigma 1.5 -c 10 --batch-size 2000 --epochs 10 --data-root runs/cifar10/data --log-dir runs/cifar10/logs --device cuda --grad_sample_mode no_op
89-
python -c "import torch; model = torch.load('model_best.pth.tar'); exit(0) if (model['best_acc1']>0.4 and model['best_acc1']<0.49) else exit(1)"
89+
python -c "import torch; model = torch.load('model_best.pth.tar', weights_only=False); exit(0) if (model['best_acc1']>0.4 and model['best_acc1']<0.49) else exit(1)"
9090
9191
- name: Store CIFAR10 test results
9292
uses: actions/upload-artifact@v4

‎benchmarks/tests/test_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def pickle_data_and_config(
194194
],
195195
)
196196
def test_save_results(
197-
pickle_data_and_config: Tuple[Dict[str, Any], Dict[str, Any]]
197+
pickle_data_and_config: Tuple[Dict[str, Any], Dict[str, Any]],
198198
) -> None:
199199
"""Tests saving benchmark results.
200200

‎opacus/grad_sample/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
def register_grad_sampler(
30-
target_class_or_classes: Union[Type[nn.Module], Sequence[Type[nn.Module]]]
30+
target_class_or_classes: Union[Type[nn.Module], Sequence[Type[nn.Module]]],
3131
):
3232
"""
3333
Registers the decorated function as the ``grad_sampler`` of ``target_class_or_classes``, which is
@@ -56,7 +56,7 @@ def decorator(f):
5656

5757

5858
def register_norm_sampler(
59-
target_class_or_classes: Union[Type[nn.Module], Sequence[Type[nn.Module]]]
59+
target_class_or_classes: Union[Type[nn.Module], Sequence[Type[nn.Module]]],
6060
):
6161
"""
6262
Registers the decorated function as the ``norm_sampler`` of ``target_class_or_classes``, which is

‎opacus/privacy_engine.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ def load_checkpoint(
603603
module_load_dict_kwargs: Optional[Dict[str, Any]] = None,
604604
torch_load_kwargs: Optional[Dict[str, Any]] = None,
605605
) -> Dict:
606-
checkpoint = torch.load(path, **(torch_load_kwargs or {}))
606+
checkpoint = torch.load(path, **(torch_load_kwargs or {}), weights_only=False)
607607
module.load_state_dict(
608608
checkpoint["module_state_dict"], **(module_load_dict_kwargs or {})
609609
)

‎opacus/utils/per_sample_gradients_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def clone_module(module: nn.Module) -> nn.Module:
3939
with io.BytesIO() as bytesio:
4040
torch.save(module, bytesio)
4141
bytesio.seek(0)
42-
module_copy = torch.load(bytesio)
42+
module_copy = torch.load(bytesio, weights_only=False)
4343
return module_copy
4444

4545

0 commit comments

Comments
 (0)