Skip to content

[cherry-pick] Add test to compare encoder inference on input with and without padding (#1770) #1772

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 1 commit into from
Jun 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions test/models/test_transformers.py
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, :])