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

Resolve instantiation problem with init_meta_context #10493

Merged
merged 29 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed `CombinedLoader` and `max_size_cycle` didn't receive a `DistributedSampler` ([#10374](https://github.com/PyTorchLightning/pytorch-lightning/issues/10374))


- Fix `isinstance` not working with `init_meta_context`, materialize model not being moved to the device ([#10493](https://github.com/PyTorchLightning/metrics/pull/10493))
tchaton marked this conversation as resolved.
Show resolved Hide resolved


-


Expand Down
6 changes: 5 additions & 1 deletion pytorch_lightning/core/mixins/device_dtype_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import torch
from torch.nn import Module

import pytorch_lightning as pl


class DeviceDtypeModuleMixin(Module):
__jit_unused_properties__ = ["device", "dtype"]
Expand Down Expand Up @@ -177,7 +179,9 @@ def __update_properties(
self, device: Optional[torch.device] = None, dtype: Optional[Union[str, torch.dtype]] = None
) -> None:
def apply_fn(module: Union["DeviceDtypeModuleMixin", Module]) -> None:
if not isinstance(module, DeviceDtypeModuleMixin):
# TODO: Find why `isinstance(module, DeviceDtypeModuleMixin)` doesn't
tchaton marked this conversation as resolved.
Show resolved Hide resolved
# work when using `init_meta_device`.
tchaton marked this conversation as resolved.
Show resolved Hide resolved
if not isinstance(module, (DeviceDtypeModuleMixin, pl.LightningModule)):
tchaton marked this conversation as resolved.
Show resolved Hide resolved
return
if device is not None:
module._device = device
Expand Down
24 changes: 22 additions & 2 deletions pytorch_lightning/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@
from pytorch_lightning.loops import PredictionLoop, TrainingBatchLoop, TrainingEpochLoop
from pytorch_lightning.loops.dataloader.evaluation_loop import EvaluationLoop
from pytorch_lightning.loops.fit_loop import FitLoop
from pytorch_lightning.plugins import DDPSpawnPlugin, ParallelPlugin, PLUGIN_INPUT, PrecisionPlugin, TrainingTypePlugin
from pytorch_lightning.plugins import (
DDPSpawnPlugin,
ParallelPlugin,
PLUGIN_INPUT,
PrecisionPlugin,
TPUSpawnPlugin,
TrainingTypePlugin,
)
from pytorch_lightning.profiler import (
AdvancedProfiler,
BaseProfiler,
Expand Down Expand Up @@ -1404,10 +1411,23 @@ def _call_setup_hook(self) -> None:

def _call_configure_sharded_model(self) -> None:
with self.accelerator.model_sharded_context():
materialize_module(self.lightning_module)
self._handle_meta_model()
self.call_hook("configure_sharded_model")
self.call_hook("on_configure_sharded_model")

def _handle_meta_model(self) -> None:
tchaton marked this conversation as resolved.
Show resolved Hide resolved
param = next(self.lightning_module.parameters())
if param.device.type != "meta":
tchaton marked this conversation as resolved.
Show resolved Hide resolved
return

if isinstance(self.training_type_plugin, (DDPSpawnPlugin, TPUSpawnPlugin)):
tchaton marked this conversation as resolved.
Show resolved Hide resolved
raise MisconfigurationException("LightningModule on meta device isn't supported with spawn.")

materialize_module(self.lightning_module)
self.lightning_module.trainer = proxy(self)
tchaton marked this conversation as resolved.
Show resolved Hide resolved
# TODO: Find a better place to move the newly materialized model to the device
self.training_type_plugin.model_to_device()
tchaton marked this conversation as resolved.
Show resolved Hide resolved
tchaton marked this conversation as resolved.
Show resolved Hide resolved

def _call_teardown_hook(self) -> None:
fn = self.state.fn._setup_fn

Expand Down
18 changes: 14 additions & 4 deletions pytorch_lightning/utilities/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from torch.nn import Module
from torch.nn.modules.container import ModuleDict, ModuleList, Sequential

import pytorch_lightning as pl
from pytorch_lightning.utilities import rank_zero_warn
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.imports import _TORCH_GREATER_EQUAL_1_10
Expand Down Expand Up @@ -191,7 +192,6 @@ def materialize_module(root_module: nn.Module) -> nn.Module:

# cache subclasses to optimize the search when resetting the meta device later on.
__STORAGE_META__ = {}

__CREATED_MODULES__ = set()


Expand Down Expand Up @@ -237,7 +237,7 @@ def _set_meta_device() -> None:

for subclass in get_all_subclasses(torch.nn.modules.module.Module):

if isinstance(subclass, (Sequential, ModuleList, ModuleDict)):
if subclass in (Sequential, ModuleList, ModuleDict, pl.LightningModule):
continue

# if a subclass has already been stored, we should use the cache
Expand Down Expand Up @@ -267,8 +267,10 @@ def materialize(cls, materialize_fn: Callable):

@staticmethod
def add_subclasses(subclass):
"""This is used to unrol the instantion tree while creating the modules."""
__CREATED_MODULES__.add(subclass)
"""This is used to unroll the instantion tree while creating the modules."""
# Don't store the LightningModule as skiped from the Meta process.
if subclass != pl.LightningModule:
__CREATED_MODULES__.add(subclass)
if subclass.__bases__[0] != torch.nn.modules.module.Module:
_MetaClass.add_subclasses(subclass.__bases__[0])

Expand Down Expand Up @@ -312,12 +314,20 @@ def search(mod: ModuleType) -> List[ModuleType]:
setattr(mod, subclass.__name__, _MetaClass)


def mock_isinstance(A, B, isinstance=None):
tchaton marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(B, type) and "_MetaClass" in B.__name__:
return isinstance(A, B.__bases__[0])
return isinstance(A, B)


@contextmanager
def init_meta_context() -> Generator:
rank_zero_warn(
"Be aware this feature is highly experimental and there are a number of weird edge cases "
"where it can internal assert and/or crash. A more stable version is to be expected from PyTorch 1.11."
)
_set_meta_device()
__builtins__["isinstance"] = partial(mock_isinstance, isinstance=isinstance)
yield
__builtins__["isinstance"] = isinstance.keywords["isinstance"]
_unset_meta_device()
1 change: 1 addition & 0 deletions tests/utilities/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def test_init_meta_context():

with init_meta_context():
m = nn.Linear(in_features=1, out_features=1)
assert isinstance(m, nn.Linear)
assert m.weight.device.type == "meta"
mlp = MLP(4)
assert mlp.layer[0].weight.device.type == "meta"
Expand Down