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

Commit

Permalink
Minor fixes for interpret code (#3260)
Browse files Browse the repository at this point in the history
* Minor fixes for interpret code

* pylint caught a bug!
  • Loading branch information
matt-gardner committed Sep 18, 2019
1 parent 05be16a commit 3e0bad4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 13 additions & 0 deletions allennlp/data/token_indexers/pretrained_transformer_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self,
elif model_name.endswith("-uncased") and not do_lowercase:
logger.warning("Your pretrained model appears to be uncased, "
"but your indexer is not lowercasing tokens.")
self._model_name = model_name
self.tokenizer = AutoTokenizer.from_pretrained(model_name, do_lower_case=do_lowercase)
self._namespace = namespace
self._added_to_vocabulary = False
Expand Down Expand Up @@ -91,3 +92,15 @@ def as_padded_tensor(self,
desired_num_tokens[key],
default_value=lambda: self._padding_value))
for key, val in tokens.items()}

def __eq__(self, other):
if isinstance(other, PretrainedTransformerIndexer):
for key in self.__dict__:
if key == 'tokenizer':
# This is a reference to a function in the huggingface code, which we can't
# really modify to make this clean. So we special-case it.
continue
if self.__dict__[key] != other.__dict__[key]:
return False
return True
return NotImplemented
7 changes: 5 additions & 2 deletions allennlp/nn/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,11 +1484,14 @@ def find_embedding_layer(model: torch.nn.Module) -> torch.nn.Module:
"""
# We'll look for a few special cases in a first pass, then fall back to just finding a
# TextFieldEmbedder in a second pass if we didn't find a special case.
from pytorch_pretrained_bert.modeling import BertEmbeddings
from pytorch_pretrained_bert.modeling import BertEmbeddings as BertEmbeddingsOld
from pytorch_transformers.modeling_gpt2 import GPT2Model
from pytorch_transformers.modeling_bert import BertEmbeddings as BertEmbeddingsNew
from allennlp.modules.text_field_embedders.text_field_embedder import TextFieldEmbedder
for module in model.modules():
if isinstance(module, BertEmbeddings):
if isinstance(module, BertEmbeddingsOld):
return module.word_embeddings
if isinstance(module, BertEmbeddingsNew):
return module.word_embeddings
if isinstance(module, GPT2Model):
return module.wte
Expand Down

0 comments on commit 3e0bad4

Please sign in to comment.