Skip to content

Commit

Permalink
add model and taskmodule Interface classes (#328)
Browse files Browse the repository at this point in the history
* add the following model interfaces: RequiresModelNameOrPath and RequiresNumClasses; make tokenizer_vocab_size parameter optional for TransformerTextClassificationModel

* add the ChangesTokenizerVocabSize taskmodule interfaces

* re-add parameter freeze_model (removing it breaks tests) and really implement it
  • Loading branch information
ArneBinder authored Sep 7, 2023
1 parent 4c39a61 commit 0bde3ae
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
6 changes: 6 additions & 0 deletions src/pytorch_ie/models/interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RequiresModelNameOrPath:
pass


class RequiresNumClasses:
pass
3 changes: 2 additions & 1 deletion src/pytorch_ie/models/transformer_seq2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing_extensions import TypeAlias

from pytorch_ie.core import PyTorchIEModel
from pytorch_ie.models.interface import RequiresModelNameOrPath

ModelInputType: TypeAlias = BatchEncoding
ModelOutputType: TypeAlias = Seq2SeqLMOutput
Expand All @@ -14,7 +15,7 @@


@PyTorchIEModel.register()
class TransformerSeq2SeqModel(PyTorchIEModel):
class TransformerSeq2SeqModel(PyTorchIEModel, RequiresModelNameOrPath):
def __init__(self, model_name_or_path: str, learning_rate: float = 1e-5, **kwargs) -> None:
super().__init__(**kwargs)

Expand Down
5 changes: 4 additions & 1 deletion src/pytorch_ie/models/transformer_span_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing_extensions import TypeAlias

from pytorch_ie.core import PyTorchIEModel
from pytorch_ie.models.interface import RequiresModelNameOrPath, RequiresNumClasses
from pytorch_ie.models.modules.mlp import MLP

ModelInputType: TypeAlias = BatchEncoding
Expand All @@ -29,7 +30,9 @@


@PyTorchIEModel.register()
class TransformerSpanClassificationModel(PyTorchIEModel):
class TransformerSpanClassificationModel(
PyTorchIEModel, RequiresModelNameOrPath, RequiresNumClasses
):
def __init__(
self,
model_name_or_path: str,
Expand Down
33 changes: 11 additions & 22 deletions src/pytorch_ie/models/transformer_text_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing_extensions import TypeAlias

from pytorch_ie.core import PyTorchIEModel
from pytorch_ie.models.interface import RequiresModelNameOrPath, RequiresNumClasses

ModelInputType: TypeAlias = MutableMapping[str, Any]
ModelOutputType: TypeAlias = Dict[str, Any]
Expand All @@ -25,12 +26,14 @@


@PyTorchIEModel.register()
class TransformerTextClassificationModel(PyTorchIEModel):
class TransformerTextClassificationModel(
PyTorchIEModel, RequiresModelNameOrPath, RequiresNumClasses
):
def __init__(
self,
model_name_or_path: str,
num_classes: int,
tokenizer_vocab_size: int,
tokenizer_vocab_size: Optional[int] = None,
ignore_index: Optional[int] = None,
learning_rate: float = 1e-5,
task_learning_rate: float = 1e-4,
Expand Down Expand Up @@ -58,11 +61,13 @@ def __init__(
self.model = AutoModel.from_config(config=config)
else:
self.model = AutoModel.from_pretrained(model_name_or_path, config=config)
self.model.resize_token_embeddings(tokenizer_vocab_size)

# if freeze_model:
# for param in self.model.parameters():
# param.requires_grad = False
if freeze_model:
for param in self.model.parameters():
param.requires_grad = False

if tokenizer_vocab_size is not None:
self.model.resize_token_embeddings(tokenizer_vocab_size)

classifier_dropout = (
config.classifier_dropout
Expand Down Expand Up @@ -131,19 +136,3 @@ def configure_optimizers(self):
return [optimizer], [{"scheduler": scheduler, "interval": "step"}]
else:
return optimizer

# param_optimizer = list(self.named_parameters())
# # TODO: this needs fixing (does not work models other than BERT)
# optimizer_grouped_parameters = [
# {"params": [p for n, p in param_optimizer if "bert" in n]},
# {
# "params": [p for n, p in param_optimizer if "bert" not in n],
# "lr": self.task_learning_rate,
# },
# ]
# optimizer = AdamW(optimizer_grouped_parameters, lr=self.learning_rate)
# scheduler = get_linear_schedule_with_warmup(
# optimizer, int(self.t_total * self.warmup_proportion), self.t_total
# )
# return [optimizer], [scheduler]
# return torch.optim.Adam(self.parameters(), lr=self.learning_rate)
5 changes: 4 additions & 1 deletion src/pytorch_ie/models/transformer_token_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing_extensions import TypeAlias

from pytorch_ie.core import PyTorchIEModel
from pytorch_ie.models.interface import RequiresModelNameOrPath, RequiresNumClasses

ModelInputType: TypeAlias = BatchEncoding
ModelOutputType: TypeAlias = Dict[str, Any]
Expand All @@ -23,7 +24,9 @@


@PyTorchIEModel.register()
class TransformerTokenClassificationModel(PyTorchIEModel):
class TransformerTokenClassificationModel(
PyTorchIEModel, RequiresModelNameOrPath, RequiresNumClasses
):
def __init__(
self,
model_name_or_path: str,
Expand Down
2 changes: 2 additions & 0 deletions src/pytorch_ie/taskmodules/interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ChangesTokenizerVocabSize:
pass
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from pytorch_ie.core import AnnotationList, Document, TaskEncoding, TaskModule
from pytorch_ie.documents import TextDocument
from pytorch_ie.models.transformer_text_classification import ModelOutputType, ModelStepInputType
from pytorch_ie.taskmodules.interface import ChangesTokenizerVocabSize
from pytorch_ie.utils.span import get_token_slice, is_contained_in
from pytorch_ie.utils.window import get_window_around_slice

Expand Down Expand Up @@ -109,7 +110,7 @@ def shift_token_span(self, value: int):


@TaskModule.register()
class TransformerRETextClassificationTaskModule(TaskModuleType):
class TransformerRETextClassificationTaskModule(TaskModuleType, ChangesTokenizerVocabSize):
"""Marker based relation extraction. This taskmodule prepares the input token ids in such a way
that before and after the candidate head and tail entities special marker tokens are inserted.
Then, the modified token ids can be simply passed into a transformer based text classifier
Expand Down

0 comments on commit 0bde3ae

Please sign in to comment.