-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add activation checkpointing to unet #8554
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
Open
ferreirafabio80
wants to merge
11
commits into
Project-MONAI:dev
Choose a base branch
from
ferreirafabio80:feat/add_activation_checkpointing_to_unet
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+21
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
de2b6bd
feat: add optional gradient checkpointing to unet
66edcb5
fix: small ruff issue
e66e357
Update monai/networks/nets/unet.py
ferreirafabio80 feefcaa
docs: update docstrings
e112457
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f673ca1
fix: avoid BatchNorm subblocks
69540ff
fix: revert batch norm changes
42ec757
refactor: creates a subclass of UNet and overrides the get connection…
a2e8474
chore: remove use checkpointing from doc string
4c4782e
fix: linting issues
515c659
feat: add activation checkpointing to down and up paths to be more ef…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,11 @@ | |
|
||
import warnings | ||
from collections.abc import Sequence | ||
from typing import cast | ||
|
||
import torch | ||
import torch.nn as nn | ||
from torch.utils.checkpoint import checkpoint | ||
|
||
from monai.networks.blocks.convolutions import Convolution, ResidualUnit | ||
from monai.networks.layers.factories import Act, Norm | ||
|
@@ -24,6 +26,17 @@ | |
__all__ = ["UNet", "Unet"] | ||
|
||
|
||
class _ActivationCheckpointWrapper(nn.Module): | ||
"""Apply activation checkpointing to the wrapped module during training.""" | ||
|
||
def __init__(self, module: nn.Module) -> None: | ||
super().__init__() | ||
self.module = module | ||
|
||
def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
return cast(torch.Tensor, checkpoint(self.module, x, use_reentrant=False)) | ||
Comment on lines
+29
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Add Google-style docstrings. Class and forward docstrings need Args/Returns sections per guidelines. Document the wrapped module, checkpoint guard details, and returned tensor. As per coding guidelines. 🤖 Prompt for AI Agents
|
||
|
||
|
||
class UNet(nn.Module): | ||
""" | ||
Enhanced version of UNet which has residual units implemented with the ResidualUnit class. | ||
|
@@ -298,4 +311,12 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: | |
return x | ||
|
||
|
||
class CheckpointUNet(UNet): | ||
def _get_connection_block(self, down_path: nn.Module, up_path: nn.Module, subblock: nn.Module) -> nn.Module: | ||
subblock = _ActivationCheckpointWrapper(subblock) | ||
down_path = _ActivationCheckpointWrapper(down_path) | ||
up_path = _ActivationCheckpointWrapper(up_path) | ||
return super()._get_connection_block(down_path, up_path, subblock) | ||
|
||
|
||
Unet = UNet |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
CheckpointUNet
to__all__
exports.CheckpointUNet
is a public class but not exported in__all__
.Apply this diff:
📝 Committable suggestion
🤖 Prompt for AI Agents