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

Fix AC order for FLAVA example #434

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 19 additions & 20 deletions examples/flava/native/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ def __init__(self, config: DictConfig):
else torch.float16
)

self.scaler = ShardedGradScaler() if config.training.enable_amp else None
self.scaler = (
ShardedGradScaler()
if config.training.enable_amp and self.half_dtype == torch.float16
else None
)

def log(
self,
Expand All @@ -144,25 +148,6 @@ def create_model(self) -> torch.nn.Module:
f"size: {get_model_size_gb(model):.3} GB"
)

if self.config.training.activation_checkpointing:
check_fn = lambda submodule: isinstance(submodule, TransformerEncoderLayer)
checkpoint_impl = CheckpointImpl.REENTRANT

# DDP gradient hooks have compatibility issues with REENTRANT autograd
if strategy == "ddp":
checkpoint_impl = CheckpointImpl.NO_REENTRANT

checkpoint_wrapper_fn = partial(
checkpoint_wrapper,
offload_to_cpu=False,
checkpoint_impl=checkpoint_impl,
)
apply_activation_checkpointing(
model,
checkpoint_wrapper_fn=checkpoint_wrapper_fn,
check_fn=check_fn,
)

if strategy == "ddp":
# TODO do we have to do this in FSDP too? see https://github.com/pytorch/pytorch/issues/75478
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
Expand Down Expand Up @@ -206,6 +191,20 @@ def create_model(self) -> torch.nn.Module:

print0(f"after FSDP {torch.cuda.memory_allocated()/1024**3:.3} GB")

if self.config.training.activation_checkpointing:
check_fn = lambda submodule: isinstance(submodule, TransformerEncoderLayer)
checkpoint_impl = CheckpointImpl.NO_REENTRANT

checkpoint_wrapper_fn = partial(
checkpoint_wrapper,
checkpoint_impl=checkpoint_impl,
)
apply_activation_checkpointing(
model,
checkpoint_wrapper_fn=checkpoint_wrapper_fn,
check_fn=check_fn,
)

else:
raise ValueError(f"unknown strategy: {strategy}")

Expand Down
Loading