This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1/2] Use any task as an embedder for any layer (#1396)
Co-authored-by: Kushashwa Ravi Shrimali <kushashwaravishrimali@gmail.com>
- Loading branch information
1 parent
3e4c8bb
commit 140c5f6
Showing
5 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Copyright The PyTorch Lightning team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import Any | ||
|
||
from pytorch_lightning import LightningModule | ||
|
||
from flash.core.model import Task | ||
|
||
|
||
class StopForward(Exception): | ||
pass | ||
|
||
|
||
class Embedder(Task): | ||
def __init__(self, model: LightningModule, layer: str): | ||
super().__init__() | ||
|
||
self.model = model | ||
self.layer = layer | ||
|
||
self._module, self._hook = self._make_hook() | ||
self._handle = None | ||
self._out = None | ||
|
||
def _make_hook(self): | ||
def hook(_, __, output): | ||
self._out = output | ||
raise StopForward | ||
|
||
available_layers = {"output", ""} | ||
|
||
if self.layer in available_layers: | ||
return None, None | ||
|
||
for name, module in self.model.named_modules(): | ||
available_layers.add(name) | ||
if name == self.layer: | ||
return module, hook | ||
|
||
raise ValueError( | ||
"The requested layer is not available in `model.named_modules`. The available layers are: " | ||
f"{', '.join(available_layers)}." | ||
) | ||
|
||
def _register_hook(self): | ||
if self._module is not None: | ||
self._handle = self._module.register_forward_hook(self._hook) | ||
|
||
def _remove_hook(self): | ||
if self._handle is not None: | ||
self._handle.remove() | ||
self._handle = None | ||
|
||
def training_step(self, batch: Any, batch_idx: int) -> Any: | ||
raise NotImplementedError("Training an `Embedder` is not supported.") | ||
|
||
def validation_step(self, batch: Any, batch_idx: int) -> Any: | ||
raise NotImplementedError("Validating an `Embedder` is not supported.") | ||
|
||
def test_step(self, batch: Any, batch_idx: int) -> Any: | ||
raise NotImplementedError("Testing an `Embedder` is not supported.") | ||
|
||
def forward(self, batch: Any) -> Any: | ||
try: | ||
self._register_hook() | ||
return self.model.predict_step(batch, 0, dataloader_idx=0) | ||
except StopForward: | ||
return self._out | ||
finally: | ||
self._remove_hook() | ||
self._out = None | ||
|
||
def predict_step(self, batch: Any, batch_idx: int, dataloader_idx: int = 0) -> Any: | ||
return self(batch) |
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Copyright The PyTorch Lightning team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import time | ||
|
||
import pytest | ||
import torch | ||
from pytorch_lightning import LightningModule | ||
from torch import nn | ||
|
||
from flash.core.utilities.embedder import Embedder | ||
from flash.core.utilities.imports import _CORE_TESTING | ||
|
||
|
||
class EmbedderTestModel(LightningModule): | ||
def __init__(self, backbone): | ||
super().__init__() | ||
|
||
self.backbone = backbone | ||
|
||
def predict_step(self, batch, batch_idx: int, dataloader_idx: int = 0): | ||
return self.backbone(batch) | ||
|
||
|
||
class NLayerModel(EmbedderTestModel): | ||
def __init__(self, n_layers): | ||
super().__init__(nn.Sequential(*[nn.Linear(1000, 1000) for _ in range(n_layers)])) | ||
|
||
|
||
@pytest.mark.skipif(not _CORE_TESTING, reason="Not testing core.") | ||
@pytest.mark.parametrize("layer, size", [("backbone.1", 30), ("output", 40), ("", 40)]) | ||
def test_embedder(layer, size): | ||
"""Tests that the embedder ``predict_step`` correctly returns the output from the requested layer.""" | ||
model = EmbedderTestModel( | ||
nn.Sequential( | ||
nn.Linear(10, 20), | ||
nn.Linear(20, 30), | ||
nn.Linear(30, 40), | ||
) | ||
) | ||
|
||
embedder = Embedder(model, layer) | ||
|
||
assert embedder.predict_step(torch.rand(10, 10), 0, 0).size(1) == size | ||
assert embedder(torch.rand(10, 10)).size(1) == size | ||
|
||
|
||
@pytest.mark.skipif(not _CORE_TESTING, reason="Not testing core.") | ||
def test_embedder_scaling_overhead(): | ||
"""Tests that embedding to the 3rd layer of a 200 layer model takes less than double the time of embedding to. | ||
the same layer of a 3 layer model and therefore in the order of 10s - 100s of times faster than executing the full | ||
200 layer model. | ||
Note that this bound is intentionally high in an effort to reduce the flakiness of the test. | ||
""" | ||
shallow_embedder = Embedder(NLayerModel(3), "backbone.2") | ||
|
||
start = time.perf_counter() | ||
shallow_embedder.predict_step(torch.rand(10, 1000), 0, 0) | ||
end = time.perf_counter() | ||
|
||
shallow_time = end - start | ||
|
||
deep_embedder = Embedder(NLayerModel(200), "backbone.2") | ||
|
||
start = time.perf_counter() | ||
deep_embedder.predict_step(torch.rand(10, 1000), 0, 0) | ||
end = time.perf_counter() | ||
|
||
deep_time = end - start | ||
|
||
assert (abs(deep_time - shallow_time) / shallow_time) < 1 | ||
|
||
|
||
@pytest.mark.skipif(not _CORE_TESTING, reason="Not testing core.") | ||
def test_embedder_raising_overhead(): | ||
"""Tests that embedding to the output layer of a 3 layer model takes less than 10ms more than the time taken to | ||
execute the model without the embedder. | ||
Note that this bound is intentionally high in an effort to reduce the flakiness of the test. | ||
""" | ||
model = NLayerModel(10) | ||
embedder = Embedder(model, "output") | ||
|
||
start = time.perf_counter() | ||
model.predict_step(torch.rand(10, 1000), 0, 0) | ||
end = time.perf_counter() | ||
|
||
model_time = end - start | ||
|
||
start = time.perf_counter() | ||
embedder.predict_step(torch.rand(10, 1000), 0, 0) | ||
end = time.perf_counter() | ||
|
||
embedder_time = end - start | ||
|
||
assert abs(embedder_time - model_time) < 0.01 |