-
Notifications
You must be signed in to change notification settings - Fork 812
Add test to compare encoder inference on input with and without padding #1770
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import torch | ||
|
||
from ..common.parameterized_utils import nested_params | ||
from ..common.torchtext_test_case import TorchtextTestCase | ||
|
||
|
||
class TestTransformers(TorchtextTestCase): | ||
@nested_params( | ||
[True, False], | ||
[True, False], | ||
) | ||
def test_padded_input_inference(self, with_no_grad, return_all_layers): | ||
"""test transformerencoder inference same with and without padding""" | ||
from torchtext.models import RobertaEncoderConf, RobertaModel | ||
|
||
def encoder_inference(encoder, input_lst, with_no_grad): | ||
if with_no_grad: | ||
with torch.no_grad(): | ||
res = [encoder(eval_input) for eval_input in input_lst] | ||
else: | ||
res = [encoder(eval_input) for eval_input in input_lst] | ||
return res | ||
|
||
# Roberta config except for less layers (2 instead of 12) | ||
pad_idx = 1 | ||
encoder_conf = RobertaEncoderConf( | ||
vocab_size=250002, | ||
embedding_dim=768, | ||
ffn_dimension=3072, | ||
padding_idx=pad_idx, | ||
max_seq_len=514, | ||
num_attention_heads=12, | ||
num_encoder_layers=2, | ||
dropout=0.1, | ||
scaling=None, | ||
normalize_before=False, | ||
) | ||
model = RobertaModel(encoder_conf) | ||
model = model.eval() | ||
# TODO: make return_all_layers a property of RobertaEncoderConf so it can be passed as arg | ||
model.encoder.transformer.return_all_layers = return_all_layers | ||
|
||
# result from converting string "some text" to tensor using xlmr_base embeddings | ||
input_no_pad = torch.Tensor([[0, 3060, 7986, 2]]).to(torch.int) | ||
data_len = input_no_pad.shape[1] # sequence length of non-pad data | ||
# add two padding tokens to input_no_pad | ||
input_pad = torch.Tensor([[0, 3060, 7986, 2, pad_idx, pad_idx]]).to(torch.int) | ||
input_lst = [input_no_pad, input_pad] | ||
|
||
output_no_pad, output_pad = encoder_inference(model, input_lst, with_no_grad) | ||
torch.testing.assert_close(output_no_pad, output_pad[:, :data_len, :]) |
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.
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.
Currently, return_all_layers cannot be set from RobertaEncoderConf because it's not a property of the config. Thus we need to manually set it below. Not a big deal right now, but something to consider changing.