Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

[BART] Do not add start/end tokens multiple times #3714

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
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
18 changes: 12 additions & 6 deletions parlai/agents/bart/bart.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,19 @@ def _set_text_vec(
if 'text' not in obs or 'text_vec' not in obs:
return obs
vec = obs['text_vec']
if truncate is not None:
vec = torch.LongTensor( # type: ignore
self._check_truncate(obs['text_vec'], truncate - 2, True)

# add start/end tokens
if 'added_start_end_tokens' not in obs:
if truncate is not None:
vec = torch.LongTensor( # type: ignore
self._check_truncate(obs['text_vec'], truncate - 2, True)
)
obs.force_set(
'text_vec',
self._add_start_end_tokens(vec, add_start=True, add_end=True),
)
obs.force_set(
'text_vec', self._add_start_end_tokens(vec, add_start=True, add_end=True)
)
obs['added_start_end_tokens'] = True

return obs

def _get_initial_decoder_input(
Expand Down
21 changes: 21 additions & 0 deletions tests/nightly/gpu/test_bart.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ def test_bart_gen(self):

self.assertEqual(act['text'], text)

def test_bart_cache_text_vec(self):
"""
Test BART text vec caching
"""
opt = ParlaiParser(True, True).parse_args(['--model', 'bart'])
bart = create_agent(opt)

# obs 1
text = "Don't have a cow, Man!"
in_obs = {'text': text, 'episode_done': True}
out_obs = bart.observe(in_obs)
cached_text_vec = out_obs['text_vec']
_ = bart.act()

# obs 2
in_obs = {'text_vec': cached_text_vec, 'episode_done': True}
out_obs = bart.observe(in_obs)
cached_text_vec_2 = out_obs['text_vec']

self.assertEqual(cached_text_vec.tolist(), cached_text_vec_2.tolist())

@testing_utils.retry(ntries=3, log_retry=True)
def test_bart_ft(self):
"""
Expand Down