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

Support deprecated attribute usage #699

Merged
merged 5 commits into from
Aug 9, 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
2 changes: 1 addition & 1 deletion pl_bolts/models/rl/double_dqn_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def training_step(self, batch: Tuple[Tensor, Tensor], _) -> OrderedDict:
# calculates training loss
loss = double_dqn_loss(batch, self.net, self.target_net, self.gamma)

if self.trainer.use_dp or self.trainer.use_ddp2:
if self._use_dp_or_ddp2(self.trainer):
loss = loss.unsqueeze(0)

# Soft update of target network
Expand Down
11 changes: 9 additions & 2 deletions pl_bolts/models/rl/dqn_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pl_bolts.models.rl.common.gym_wrappers import make_environment
from pl_bolts.models.rl.common.memory import MultiStepBuffer
from pl_bolts.models.rl.common.networks import CNN
from pl_bolts.utils import _GYM_AVAILABLE
from pl_bolts.utils import _GYM_AVAILABLE, _PL_GREATER_EQUAL_1_4
from pl_bolts.utils.warnings import warn_missing_pkg

if _GYM_AVAILABLE:
Expand Down Expand Up @@ -272,7 +272,7 @@ def training_step(self, batch: Tuple[Tensor, Tensor], _) -> OrderedDict:
# calculates training loss
loss = dqn_loss(batch, self.net, self.target_net, self.gamma)

if self.trainer.use_dp or self.trainer.use_ddp2:
if self._use_dp_or_ddp2(self.trainer):
loss = loss.unsqueeze(0)

# Soft update of target network
Expand Down Expand Up @@ -404,6 +404,13 @@ def add_model_specific_args(arg_parser: argparse.ArgumentParser, ) -> argparse.A

return arg_parser

@staticmethod
def _use_dp_or_ddp2(trainer: Trainer) -> bool:
# for backwards compatibility
if _PL_GREATER_EQUAL_1_4:
return trainer.accelerator_connector.use_dp or trainer.accelerator_connector.use_ddp2
return trainer.use_dp or trainer.use_ddp2


def cli_main():
parser = argparse.ArgumentParser(add_help=False)
Expand Down
2 changes: 1 addition & 1 deletion pl_bolts/models/rl/per_dqn_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def training_step(self, batch, _) -> OrderedDict:
# calculates training loss
loss, batch_weights = per_dqn_loss(samples, weights, self.net, self.target_net, self.gamma)

if self.trainer.use_dp or self.trainer.use_ddp2:
if self._use_dp_or_ddp2(self.trainer):
loss = loss.unsqueeze(0)

# update priorities in buffer
Expand Down
15 changes: 11 additions & 4 deletions pl_bolts/models/self_supervised/moco/moco2_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
Moco2TrainImagenetTransforms,
Moco2TrainSTL10Transforms,
)
from pl_bolts.utils import _TORCHVISION_AVAILABLE
from pl_bolts.utils import _PL_GREATER_EQUAL_1_4, _TORCHVISION_AVAILABLE
from pl_bolts.utils.warnings import warn_missing_pkg

if _TORCHVISION_AVAILABLE:
Expand Down Expand Up @@ -150,7 +150,7 @@ def _momentum_update_key_encoder(self):
@torch.no_grad()
def _dequeue_and_enqueue(self, keys, queue_ptr, queue):
# gather keys before updating queue
if self.trainer.use_ddp or self.trainer.use_ddp2:
if self._use_ddp_or_ddp2(self.trainer):
keys = concat_all_gather(keys)

batch_size = keys.shape[0]
Expand Down Expand Up @@ -229,14 +229,14 @@ def forward(self, img_q, img_k, queue):
with torch.no_grad(): # no gradient to keys

# shuffle for making use of BN
if self.trainer.use_ddp or self.trainer.use_ddp2:
if self._use_ddp_or_ddp2(self.trainer):
img_k, idx_unshuffle = self._batch_shuffle_ddp(img_k)

k = self.encoder_k(img_k) # keys: NxC
k = nn.functional.normalize(k, dim=1)

# undo shuffle
if self.trainer.use_ddp or self.trainer.use_ddp2:
if self._use_ddp_or_ddp2(self.trainer):
k = self._batch_unshuffle_ddp(k, idx_unshuffle)

# compute logits
Expand Down Expand Up @@ -335,6 +335,13 @@ def add_model_specific_args(parent_parser):

return parser

@staticmethod
def _use_ddp_or_ddp2(trainer: Trainer) -> bool:
# for backwards compatibility
if _PL_GREATER_EQUAL_1_4:
return trainer.accelerator_connector.use_ddp or trainer.accelerator_connector.use_ddp2
return trainer.use_ddp or trainer.use_ddp2


# utils
@torch.no_grad()
Expand Down
1 change: 1 addition & 0 deletions pl_bolts/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ def _compare_version(package: str, op, version) -> bool:
_WANDB_AVAILABLE: bool = _module_available("wandb")
_MATPLOTLIB_AVAILABLE: bool = _module_available("matplotlib")
_TORCHVISION_LESS_THAN_0_9_1: bool = _compare_version("torchvision", operator.lt, "0.9.1")
_PL_GREATER_EQUAL_1_4 = _compare_version("pytorch_lightning", operator.ge, "1.4.0")

__all__ = ["BatchGradientVerification"]