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

Pass context_fn to activation_checkpoint_params #951

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 18 additions & 1 deletion torchtnt/utils/prepare_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@

from dataclasses import asdict, dataclass
from functools import partial
from typing import Any, Callable, cast, Dict, Iterable, Optional, Union
from typing import (
Any,
Callable,
cast,
ContextManager,
Dict,
Iterable,
Optional,
Tuple,
Union,
)

import torch
import torch.distributed as dist
Expand Down Expand Up @@ -165,6 +175,8 @@ class ActivationCheckpointParams:
checkpoint_impl: CheckpointImpl
check_fn: Callable[[torch.nn.Module], bool] = lambda _: True
auto_wrap_policy: Optional[Callable[[torch.nn.Module, bool, int], bool]] = None
# pyre-fixme[24]: Generic type `Callable` expects 2 type parameters.
context_fn: Optional[Callable[[], Tuple[ContextManager, ContextManager]]] = None


def prepare_ddp(
Expand Down Expand Up @@ -357,9 +369,14 @@ def prepare_module(
checkpoint_impl = activation_checkpoint_params.checkpoint_impl
check_fn = activation_checkpoint_params.check_fn
auto_wrap_policy = activation_checkpoint_params.auto_wrap_policy
context_fn = activation_checkpoint_params.context_fn
additional_params = {}
if context_fn:
additional_params["context_fn"] = context_fn
custom_checkpoint_wrapper = partial(
checkpoint_wrapper,
checkpoint_impl=checkpoint_impl,
**additional_params,
)
apply_activation_checkpointing(
module,
Expand Down
Loading