Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

TextFieldTensor in multitask models #5331

Merged
merged 4 commits into from
Jul 26, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `TransformerTextField` can now take tensors of shape `(1, n)` like the tensors produced from a HuggingFace tokenizer.
- `tqdm` lock is now set inside `MultiProcessDataLoading` when new workers are spawned to avoid contention when writing output.
- `ConfigurationError` is now pickleable.
- Multitask models now support `TextFieldTensor` in heads, not just in the backbone

### Changed

Expand Down
13 changes: 11 additions & 2 deletions allennlp/models/multitask.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from overrides import overrides
import torch

from allennlp.data import Vocabulary
from allennlp.data import Vocabulary, TextFieldTensors
from allennlp.modules import Backbone
from allennlp.models.model import Model
from allennlp.models.heads import Head
Expand Down Expand Up @@ -111,7 +111,16 @@ def forward(self, **kwargs) -> Dict[str, torch.Tensor]: # type: ignore
task: torch.LongTensor(indices) for task, indices in task_indices_just_for_mypy.items()
}

def make_inputs_for_task(task: str, whole_batch_input: Union[torch.Tensor, List]):
def make_inputs_for_task(
task: str, whole_batch_input: Union[torch.Tensor, TextFieldTensors, List]
):
if isinstance(whole_batch_input, dict):
for k1, v1 in whole_batch_input.items():
for k2, v2 in v1.items():
whole_batch_input[k1][k2] = make_inputs_for_task(task, v2)

return whole_batch_input

if isinstance(whole_batch_input, torch.Tensor):
task_indices[task] = task_indices[task].to(whole_batch_input.device)
return torch.index_select(whole_batch_input, 0, task_indices[task])
Expand Down