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

Commit 57908bb

Browse files
tpanzaAnthony P Panzadirkgr
authored
allow multiple token embedders, but only use first with non-empty type (#129)
* allow multiple token embedders, but only use first with non-empty type * blacken the modified code * update CHANGELOG * Throw an exception if we already know the model isn't going to work. * Checks the config by parsing it Co-authored-by: Anthony P Panza <anthony.p.panza@boeing.com> Co-authored-by: Dirk Groeneveld <dirkg@allenai.org>
1 parent 5ffc207 commit 57908bb

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Fixed
1313

14+
- Updated `LanguageModelTokenEmbedder` to allow allow multiple token embedders, but only use first with non-empty type
1415
- Fixed evaluation of metrics when using distributed setting.
1516
- Fixed a bug introduced in 1.0 where the SRL model did not reproduce the original result.
1617

allennlp_models/lm/modules/token_embedders/language_model.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
from typing import Dict, Tuple, TYPE_CHECKING
33

44
import torch
5+
from allennlp.common import Params
56

67
from allennlp.common.checks import ConfigurationError
78
from allennlp.data import TokenIndexer, Token
9+
from allennlp.modules import TextFieldEmbedder
810
from allennlp.modules.scalar_mix import ScalarMix
11+
from allennlp.modules.text_field_embedders import BasicTextFieldEmbedder
12+
from allennlp.modules.token_embedders import EmptyEmbedder
913
from allennlp.modules.token_embedders.token_embedder import TokenEmbedder
1014
from allennlp.nn.util import (
1115
remove_sentence_boundaries,
@@ -76,15 +80,29 @@ def __init__(
7680

7781
# Extract the name of the tokens that the LM was trained on.
7882
text_field_embedder = dict_config["model"]["text_field_embedder"]
79-
token_names = list(text_field_embedder["token_embedders"].keys())
80-
if len(token_names) != 1:
81-
# We don't currently support embedding with language models trained with multiple
82-
# embedded indices.
83-
#
84-
# Note: We only care about embedded indices. This does not include "tokens" which
85-
# is just used to compute the loss in LanguageModel.
86-
raise ConfigurationError(f"LM from {archive_file} trained with multiple embedders!")
87-
self._token_name = token_names[0]
83+
text_field_embedder = TextFieldEmbedder.from_params(Params(text_field_embedder))
84+
if not isinstance(text_field_embedder, BasicTextFieldEmbedder):
85+
raise ConfigurationError(
86+
f"Language model from {archive_file} uses a non-standard TextFieldEmbedder!"
87+
)
88+
non_empty_embedders = [
89+
name
90+
for name, token_embedder in text_field_embedder._token_embedders.items()
91+
if not isinstance(token_embedder, EmptyEmbedder)
92+
]
93+
94+
if len(non_empty_embedders) == 0:
95+
# Only empty embedders were contained in the language model
96+
# We need at least one non-empty embedder in the language model
97+
raise ConfigurationError(
98+
f"Language model from {archive_file} trained with only empty embedders!"
99+
)
100+
elif len(non_empty_embedders) > 1:
101+
raise ConfigurationError(
102+
f"Language model from {archive_file} trained with multiple non-empty embedders!"
103+
)
104+
105+
self._token_name = non_empty_embedders[0]
88106

89107
# TODO(brendanr): Find a way to remove this hack. The issue fundamentally is that the
90108
# BasicTextFieldEmbedder concatenates multiple embedded representations. When a

0 commit comments

Comments
 (0)