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

[Utils] has_offloaded_params #3188

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kylesayrs
Copy link

@kylesayrs kylesayrs commented Oct 23, 2024

Purpose

  • Add a has_offloaded_params utility function which returns True iff there is an AlignDevicesHook with offloading enabled
  • Prepare for other utility functions to be implemented in future PRs

Changes

  • Implement utility function in accelerate.modeling
  • Replace repeat code use in accelerate.modeling and accelerate.accelerator with newly added function

Testing

test_has_offloaded_params.py
import torch

from accelerate.hooks import attach_align_device_hook
from accelerate.utils.modeling import has_offloaded_params
from accelerate.big_modeling import cpu_offload_with_hook

class ModelForTest(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.linear1 = torch.nn.Linear(3, 4)
        self.batchnorm = torch.nn.BatchNorm1d(4)
        self.linear2 = torch.nn.Linear(4, 5)

    def forward(self, x):
        return self.linear2(self.batchnorm(self.linear1(x)))
    
model = ModelForTest()
assert not has_offloaded_params(model.linear1)
assert not has_offloaded_params(model.batchnorm)
assert not has_offloaded_params(model.batchnorm)

model = ModelForTest()
model, hook = cpu_offload_with_hook(model, execution_device="cuda:0")
assert not has_offloaded_params(model.linear1)
assert not has_offloaded_params(model.batchnorm)
assert not has_offloaded_params(model.batchnorm)

model = ModelForTest()
attach_align_device_hook(model, offload=False)
assert not has_offloaded_params(model.linear1)
assert not has_offloaded_params(model.batchnorm)
assert not has_offloaded_params(model.batchnorm)

model = ModelForTest()
attach_align_device_hook(model, offload=True)
assert has_offloaded_params(model.linear1)
assert has_offloaded_params(model.batchnorm)
assert has_offloaded_params(model.batchnorm)

Who can review?

@SunMarc
@LysandreJik
@mgoin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant