-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
Adding gradient checkpointing to GPT2 #7446
Merged
LysandreJik
merged 6 commits into
huggingface:master
from
TevenLeScao:gpt2_checkpointing
Sep 29, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
64989b5
GPT2 gradient checkpointing
TevenLeScao ae19b18
find_unused_parameters removed if checkpointing
TevenLeScao 85054e0
find_unused_parameters removed if checkpointing
TevenLeScao 188dff8
Update src/transformers/configuration_gpt2.py
TevenLeScao de111f9
Added a test for generation with checkpointing
TevenLeScao 6139c24
Update src/transformers/configuration_gpt2.py
TevenLeScao 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 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 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 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 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 |
---|---|---|
|
@@ -88,7 +88,7 @@ def __init__( | |
self.bos_token_id = vocab_size - 1 | ||
self.eos_token_id = vocab_size - 1 | ||
|
||
def prepare_config_and_inputs(self): | ||
def prepare_config_and_inputs(self, gradient_checkpointing=False): | ||
input_ids = ids_tensor([self.batch_size, self.seq_length], self.vocab_size) | ||
|
||
input_mask = None | ||
|
@@ -127,6 +127,7 @@ def prepare_config_and_inputs(self): | |
bos_token_id=self.bos_token_id, | ||
eos_token_id=self.eos_token_id, | ||
return_dict=True, | ||
gradient_checkpointing=gradient_checkpointing, | ||
) | ||
|
||
head_mask = ids_tensor([self.num_hidden_layers, self.num_attention_heads], 2) | ||
|
@@ -269,6 +270,15 @@ def create_and_check_lm_head_model(self, config, input_ids, input_mask, head_mas | |
self.parent.assertEqual(result.loss.shape, ()) | ||
self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size)) | ||
|
||
def create_and_check_forward_and_backwards(self, config, input_ids, input_mask, head_mask, token_type_ids, *args): | ||
model = GPT2LMHeadModel(config) | ||
model.to(torch_device) | ||
|
||
result = model(input_ids, token_type_ids=token_type_ids, labels=input_ids) | ||
self.parent.assertEqual(result.loss.shape, ()) | ||
self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size)) | ||
result.loss.backward() | ||
|
||
def create_and_check_double_lm_head_model( | ||
self, config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, *args | ||
): | ||
|
@@ -355,6 +365,10 @@ def test_gpt2_double_lm_head_model(self): | |
config_and_inputs = self.model_tester.prepare_config_and_inputs() | ||
self.model_tester.create_and_check_double_lm_head_model(*config_and_inputs) | ||
|
||
def test_gpt2_gradient_checkpointing(self): | ||
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. Awesome that you add a test! |
||
config_and_inputs = self.model_tester.prepare_config_and_inputs(gradient_checkpointing=True) | ||
self.model_tester.create_and_check_forward_and_backwards(*config_and_inputs) | ||
|
||
@slow | ||
def test_model_from_pretrained(self): | ||
for model_name in GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: | ||
|
@@ -366,33 +380,34 @@ def test_model_from_pretrained(self): | |
class GPT2ModelLanguageGenerationTest(unittest.TestCase): | ||
@slow | ||
def test_lm_generate_gpt2(self): | ||
model = GPT2LMHeadModel.from_pretrained("gpt2") | ||
model.to(torch_device) | ||
input_ids = torch.tensor([[464, 3290]], dtype=torch.long, device=torch_device) # The dog | ||
expected_output_ids = [ | ||
464, | ||
3290, | ||
373, | ||
1043, | ||
287, | ||
257, | ||
2214, | ||
1474, | ||
262, | ||
16246, | ||
286, | ||
2688, | ||
290, | ||
2688, | ||
27262, | ||
13, | ||
198, | ||
198, | ||
464, | ||
3290, | ||
] # The dog was found in a field near the intersection of West and West Streets.\n\nThe dog | ||
output_ids = model.generate(input_ids, do_sample=False) | ||
self.assertListEqual(output_ids[0].tolist(), expected_output_ids) | ||
for checkpointing in [True, False]: | ||
model = GPT2LMHeadModel.from_pretrained("gpt2", gradient_checkpointing=checkpointing) | ||
model.to(torch_device) | ||
input_ids = torch.tensor([[464, 3290]], dtype=torch.long, device=torch_device) # The dog | ||
expected_output_ids = [ | ||
464, | ||
3290, | ||
373, | ||
1043, | ||
287, | ||
257, | ||
2214, | ||
1474, | ||
262, | ||
16246, | ||
286, | ||
2688, | ||
290, | ||
2688, | ||
27262, | ||
13, | ||
198, | ||
198, | ||
464, | ||
3290, | ||
] # The dog was found in a field near the intersection of West and West Streets.\n\nThe dog | ||
output_ids = model.generate(input_ids, do_sample=False) | ||
self.assertListEqual(output_ids[0].tolist(), expected_output_ids) | ||
|
||
@slow | ||
def test_lm_generate_distilgpt2(self): | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
if self.config.gradient_checkpointing:
is nicerThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most model
config
s don't actually have this attribute, only the ones that support checkpointing (AFAIK, Bert and Longformer for now) so it's less risky to do things this way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in
modeling_gpt2.py
which only works withconfiguration_gpt2.py
. So if you addgradient_checkpointing
to the config with default =False
I don't see why this would be risky