-
Notifications
You must be signed in to change notification settings - Fork 33.9k
Update past_key_values in GPT-2
#9596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
patrickvonplaten
merged 14 commits into
huggingface:master
from
forest1988:forest1988-fix-gpt2-past_key_values
Jan 19, 2021
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4f21a34
Update past_key_values in gpt2 (#9391)
forest1988 f8a3ad0
Update generation_utils, and rename some items
forest1988 3fb461d
Update modeling_gpt2 to avoid an error in gradient_checkpointing
forest1988 d04b10c
Remove 'reorder_cache' from util and add variations to XLNet, Transfo…
forest1988 09e6e59
Change the location of '_reorder_cache' in modeling files
forest1988 2654400
Add '_reorder_cache' in modeling_ctrl
forest1988 06ce793
Fix a bug of my last commit in CTRL
forest1988 445a96b
Add '_reorder_cache' to GPT2DoubleHeadsModel
forest1988 eb5b936
Manage 'use_cache' in config of test_modeling_gpt2
forest1988 85c0c1d
Clean up the doc string
forest1988 d035d68
Update src/transformers/models/gpt2/modeling_gpt2.py
forest1988 e932434
Fix the doc string (GPT-2, CTRL)
forest1988 35da6f3
Fix conflicts in modeling_gpt2
forest1988 0850299
improve gradient_checkpointing_behavior
patrickvonplaten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
|
|
||
| import os | ||
| from dataclasses import dataclass | ||
| from typing import List, Optional, Tuple | ||
| from typing import Optional, Tuple | ||
|
|
||
| import torch | ||
| import torch.nn as nn | ||
|
|
@@ -232,7 +232,7 @@ def forward( | |
| value = torch.cat((past_value, value), dim=-2) | ||
|
|
||
| if use_cache is True: | ||
| present = torch.stack((key.transpose(-2, -1), value)) # transpose to have same shapes for stacking | ||
| present = (key.transpose(-2, -1), value) # transpose to have same shapes | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the reason for the recent failure of the slow test:
Can you fix the onnx part easily? @mfuntowicz @Narsil |
||
| else: | ||
| present = None | ||
|
|
||
|
|
@@ -369,9 +369,9 @@ class GPT2DoubleHeadsModelOutput(ModelOutput): | |
| Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). | ||
| mc_logits (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, num_choices)`): | ||
| Prediction scores of the multiple choice classification head (scores for each choice before SoftMax). | ||
| past_key_values (:obj:`List[torch.FloatTensor]`, `optional`, returned when ``use_cache=True`` is passed or when ``config.use_cache=True``): | ||
| List of :obj:`torch.FloatTensor` of length :obj:`config.n_layers`, with each tensor of shape :obj:`(2, | ||
| batch_size, num_heads, sequence_length, embed_size_per_head)`). | ||
| past_key_values (:obj:`Tuple[Tuple[torch.Tensor]]`, `optional`, returned when ``use_cache=True`` is passed or when ``config.use_cache=True``): | ||
|
patrickvonplaten marked this conversation as resolved.
|
||
| :obj:`Tuple[Tuple[torch.Tensor]]` of length :obj:`config.n_layers`, with each tuple having 2 tuples each of | ||
| which has a tensor of shape :obj:`(batch_size, num_heads, sequence_length, embed_size_per_head)`). | ||
|
forest1988 marked this conversation as resolved.
Outdated
|
||
|
|
||
| Contains pre-computed hidden-states (key and values in the attention blocks) that can be used (see | ||
| :obj:`past_key_values` input) to speed up sequential decoding. | ||
|
|
@@ -392,7 +392,7 @@ class GPT2DoubleHeadsModelOutput(ModelOutput): | |
| mc_loss: Optional[torch.FloatTensor] = None | ||
| logits: torch.FloatTensor = None | ||
| mc_logits: torch.FloatTensor = None | ||
| past_key_values: Optional[List[torch.FloatTensor]] = None | ||
| past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None | ||
| hidden_states: Optional[Tuple[torch.FloatTensor]] = None | ||
| attentions: Optional[Tuple[torch.FloatTensor]] = None | ||
|
|
||
|
|
@@ -418,7 +418,7 @@ class GPT2DoubleHeadsModelOutput(ModelOutput): | |
| Args: | ||
| input_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, input_ids_length)`): | ||
| :obj:`input_ids_length` = ``sequence_length`` if :obj:`past_key_values` is ``None`` else | ||
| ``past_key_values[0].shape[-2]`` (``sequence_length`` of input past key value states). Indices of input | ||
| ``past_key_values[0][0].shape[-2]`` (``sequence_length`` of input past key value states). Indices of input | ||
| sequence tokens in the vocabulary. | ||
|
|
||
| If :obj:`past_key_values` is used, only ``input_ids`` that do not have their past calculated should be | ||
|
|
@@ -429,7 +429,7 @@ class GPT2DoubleHeadsModelOutput(ModelOutput): | |
| details. | ||
|
|
||
| `What are input IDs? <../glossary.html#input-ids>`__ | ||
| past_key_values (:obj:`List[torch.FloatTensor]` of length :obj:`config.n_layers`): | ||
| past_key_values (:obj:`Tuple[Tuple[torch.Tensor]]` of length :obj:`config.n_layers`): | ||
| Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see | ||
| :obj:`past_key_values` output below). Can be used to speed up sequential decoding. The ``input_ids`` which | ||
| have their past given to this model should not be passed as ``input_ids`` as they have already been | ||
|
|
@@ -639,7 +639,7 @@ def forward( | |
|
|
||
| if past_key_values is None: | ||
| past_length = 0 | ||
| past_key_values = [None] * len(self.h) | ||
| past_key_values = tuple([None] * len(self.h)) | ||
| else: | ||
| past_length = past_key_values[0][0].size(-2) | ||
| if position_ids is None: | ||
|
|
@@ -707,7 +707,7 @@ def forward( | |
| torch.cuda.set_device(hidden_states.device) | ||
| # Ensure layer_past is on same device as hidden_states (might not be correct) | ||
| if layer_past is not None: | ||
| layer_past = layer_past.to(hidden_states.device) | ||
| layer_past = tuple(past_state.to(hidden_states.device) for past_state in layer_past) | ||
|
patrickvonplaten marked this conversation as resolved.
|
||
| # Ensure that attention_mask is always on the same device as hidden_states | ||
| if attention_mask is not None: | ||
| attention_mask = attention_mask.to(hidden_states.device) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.