Skip to content

Commit

Permalink
less call to detached
Browse files Browse the repository at this point in the history
  • Loading branch information
pommedeterresautee committed Sep 19, 2019
1 parent 64dabf7 commit 1df3b05
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions flair/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,9 @@ def _add_embeddings_internal(self, sentences: List[Sentence]) -> List[Sentence]:
text_sentences, start_marker, end_marker, self.chars_per_chunk
)

if not self.fine_tune:
all_hidden_states_in_lm = all_hidden_states_in_lm.detach()

# take first or last hidden states from language model as word representation
for i, sentence in enumerate(sentences):
sentence_text = sentence.to_tokenized_string()
Expand All @@ -1823,16 +1826,12 @@ def _add_embeddings_internal(self, sentences: List[Sentence]) -> List[Sentence]:

offset_backward -= len(token.text)

if not self.fine_tune:
embedding = embedding.detach()

# only clone if optimization mode is 'gpu'
if flair.embedding_storage_mode == "gpu":
embedding = embedding.clone()

token.set_embedding(self.name, embedding)

all_hidden_states_in_lm = all_hidden_states_in_lm.detach()
del all_hidden_states_in_lm

return sentences
Expand Down

0 comments on commit 1df3b05

Please sign in to comment.