8000 minor memory improvements in _joint_likelihood() of ConditionalRandomField with advanced indexing by LauraRuis · Pull Request #1686 · allenai/allennlp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

minor memory improvements in _joint_likelihood() of ConditionalRandomField with advanced indexing #1686

Merged
merged 3 commits into from
Aug 29, 2018

Conversation

LauraRuis
Copy link
Contributor

Minor memory improvements possible in the _joint_likelihood() function of the ConditionalRandomField class by utilizing the advanced indexing feature of PyTorch (https://github.com/pytorch/pytorch/releases/tag/v0.2.0).

Elliminates the need to expand transitions matrix by batch size (line 258 - 260) and to expand the tensor containing indices to the last tags by batch size (line 289).

Copy link
Contributor
@matt-gardner matt-gardner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for doing this! There are just a couple of minor issues that would be good to fix before merging.

@@ -286,10 +273,7 @@ def _joint_likelihood(self,
# Transition from last state to "stop" state. To start with, we need to find the last tag
# for each instance.
last_tag_index = mask.sum(0).long() - 1
last_tags = tags.gather(0, last_tag_index.view(1, batch_size).expand(sequence_length, batch_size))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just switch the .expand() here to a .squeeze(0), and still remove the three lines below? .squeeze() is generally preferable to .view(-1), as it's easier to reason about (unless there's some efficiency reason to prefer .view(-1) that I don't know about...?).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that makes more sense! I added your comments :)

@@ -255,26 +255,13 @@ def _joint_likelihood(self,
else:
score = 0.0

# Broadcast the transition scores to one per batch element
broadcast_transitions = self.transitions.view(1, num_tags, num_tags).expand(batch_size, num_tags, num_tags)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've removed the need for num_tags, so you need to remove the variable definition above on line 245 (batch_size, sequence_length, _ = logits.size()).

@matt-gardner matt-gardner merged commit d1f6748 into allenai:master Aug 29, 2018
gabrielStanovsky pushed a commit to gabrielStanovsky/allennlp that referenced this pull request Sep 7, 2018
…Field with advanced indexing (allenai#1686)

* optimize memory/speed _joint_likelihood() function with new PyTorch features

* remove redundant var and change .view(-1) to .squeeze()

* Use tags.gather(...) instead of torch.gather(tags, ...)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0