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

Steps cannot take Model as input #269

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions tango/integrations/torch/model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# from typing import Any, Dict

import torch

from tango.common.registrable import Registrable
Expand All @@ -10,3 +12,6 @@ class Model(torch.nn.Module, Registrable):
Its :meth:`~torch.nn.Module.forward()` method should return a :class:`dict` that
includes the ``loss`` during training and any tracked metrics during validation.
"""

# def _to_params(self) -> Dict[str, Any]:
# return {}
52 changes: 52 additions & 0 deletions tests/integrations/torch/model_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest
from torch import nn

from tango.common.testing import TangoTestCase
from tango.integrations.torch import Model
from tango.step import Step


class FeedForward(nn.Module):
def __init__(self):
super().__init__()
self.linear = nn.Linear(4, 4)
self.activation = nn.ReLU()

def forward(self, x):
return self.activation(self.linear(x))


@Model.register("simple_regression_model", exist_ok=True)
class SimpleRegressionModel(Model):
def __init__(self):
super().__init__()
self.blocks = nn.Sequential(*[FeedForward() for _ in range(3)])
self.regression_head = nn.Linear(4, 1)
self.loss_fcn = nn.MSELoss()

def forward(self, x, y):
output = self.blocks(x)
output = self.regression_head(output)
loss = self.loss_fcn(output, y)
return {"loss": loss}


@Step.register("step-that-takes-model-as-input")
class StepThatTakesModelAsInput(Step):
def run(self, model: Model) -> Model: # type: ignore
return model


class TestModelAsStepInput(TangoTestCase):
def test_step_that_takes_model_as_input(self):
config = {
"steps": {
"model": {
"type": "step-that-takes-model-as-input",
"model": {"type": "simple_regression_model"},
}
}
}

with pytest.raises(NotImplementedError):
self.run(config)