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

[T5] T5 in ParlAI #3519

Merged
merged 26 commits into from
Mar 19, 2021
Merged

[T5] T5 in ParlAI #3519

merged 26 commits into from
Mar 19, 2021

Conversation

klshuster
Copy link
Contributor

Patch description

Port of HuggingFace's T5 model for ParlAI. Tagging people I thought might be interested, This PR serves a dual purpose:

  1. Get T5 in ParlAI
  2. Demonstrate an effective way of using HF models in ParlAI

The second point is relevant in that I tried writing a generalized version of a DictionaryAgent that wraps a HF tokenizer.

On that note - I am seeking feedback on whether this would be more appropriate in a utils file (e.g. parlai.utils.huggingface)

Testing steps
Extensive CI; HF had some nice integration tests I copied over, and I used similar integration tests as the ones used for BART.

$ pytest test_t5.py
=====starts =====

test_t5.py ........                                                                                                                                                            [100%]

====slowest 10 durations ====
38.64s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_t5_ft
33.27s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_t5_model_parallel
18.95s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_t5_gen
14.99s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_small
12.57s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_summarization
12.21s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_translation_en_to_fr
10.35s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_translation_en_to_ro
9.90s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_translation_en_to_de
0.05s teardown tests/nightly/gpu/test_t5.py::TestT5Model::test_translation_en_to_ro
0.03s teardown tests/nightly/gpu/test_t5.py::TestT5Model::test_t5_model_parallel
====8 passed, 1 warning in 155.14s (0:02:35) ====

@klshuster
Copy link
Contributor Author

does anyone know why circle can't find tests to run? did i mess something up?

tests/nightly/gpu/test_t5.py Outdated Show resolved Hide resolved
tests/nightly/gpu/test_t5.py Outdated Show resolved Hide resolved
Copy link
Contributor

@EricMichaelSmith EricMichaelSmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, seems reasonable at a quick glance, but will defer to others with more context for approval

parlai/agents/t5/modules.py Outdated Show resolved Hide resolved
@klshuster
Copy link
Contributor Author

I'm re-requesting reviews because the model now lives in parlai/agents/hugging_face; I refactored the HFDictionaryAgent as well, but verified with CI that gpt2 and dialogpt tests still pass:

$ pytest test_dialogpt.py
================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.9, pytest-6.2.1, py-1.10.0, pluggy-1.0.0.dev0
rootdir: /private/home/kshuster/ParlAI, configfile: pytest.ini
plugins: hydra-core-1.0.0, requests-mock-1.8.0, regressions-2.1.1, datadir-1.3.1
collected 4 items

test_dialogpt.py ....                                                                                                                                                         [100%]

====slowest 10 durations ====
708.84s call     tests/nightly/gpu/test_dialogpt.py::TestDialogptModel::test_dialogpt
62.30s call     tests/nightly/gpu/test_dialogpt.py::TestDialogptModel::test_batchsize
5.53s call     tests/nightly/gpu/test_dialogpt.py::TestDialogptModel::test_nospecialtok
0.01s call     tests/nightly/gpu/test_dialogpt.py::TestDialogptModel::test_start_token

(6 durations < 0.005s hidden.  Use -vv to show these durations.)
==== 4 passed, 6 warnings in 778.03s (0:12:58) ====

$ pytest test_gpt2.py
================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.9, pytest-6.2.1, py-1.10.0, pluggy-1.0.0.dev0
rootdir: /private/home/kshuster/ParlAI, configfile: pytest.ini
plugins: hydra-core-1.0.0, requests-mock-1.8.0, regressions-2.1.1, datadir-1.3.1
collected 5 items

test_gpt2.py .....                                                                                                                                                            [100%]

====slowest 10 durations ====
119.10s call     tests/nightly/gpu/test_gpt2.py::TestDistributed::test_distributed
116.12s call     tests/nightly/gpu/test_gpt2.py::TestGpt2::test_batchsize
12.81s call     tests/nightly/gpu/test_gpt2.py::TestHuggingFaceDict::test_custom_special_tokens
8.67s setup    tests/nightly/gpu/test_gpt2.py::TestGpt2::test_nospecialtok
6.02s call     tests/nightly/gpu/test_gpt2.py::TestGpt2::test_nospecialtok
0.01s call     tests/nightly/gpu/test_gpt2.py::TestGpt2::test_start_token

(4 durations < 0.005s hidden.  Use -vv to show these durations.)
====5 passed, 7 warnings in 263.99s (0:04:23) ====

@klshuster
Copy link
Contributor Author

$ pytest test_gpt2.py test_dialogpt.py test_t5.py
=====test session starts =====
platform linux -- Python 3.7.9, pytest-6.2.1, py-1.10.0, pluggy-1.0.0.dev0
rootdir: /private/home/kshuster/ParlAI, configfile: pytest.ini
plugins: hydra-core-1.0.0, requests-mock-1.8.0, regressions-2.1.1, datadir-1.3.1
collected 17 items

test_gpt2.py .....                                                                                                                                                            [ 29%]
test_dialogpt.py ....                                                                                                                                                         [ 52%]
test_t5.py ........                                                                                                                                                           [100%]

====slowest 10 durations ====
494.52s call     tests/nightly/gpu/test_dialogpt.py::TestDialogptModel::test_dialogpt
50.78s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_t5_ft
49.19s call     tests/nightly/gpu/test_dialogpt.py::TestDialogptModel::test_batchsize
40.02s call     tests/nightly/gpu/test_gpt2.py::TestGpt2::test_batchsize
32.69s call     tests/nightly/gpu/test_gpt2.py::TestDistributed::test_distributed
32.33s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_t5_model_parallel
19.26s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_t5_gen
12.63s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_summarization
12.50s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_small
11.41s call     tests/nightly/gpu/test_t5.py::TestT5Model::test_translation_en_to_fr
====17 passed, 8 warnings in 789.71s (0:13:09) ====

@klshuster
Copy link
Contributor Author

Running all gpu tests locally, I get:

======== short test summary info ========
FAILED tests/nightly/gpu/test_bert.py::TestBertModel::test_biencoder - ImportError: BERT rankers needs pytorch-pretrained-BERT installed.
FAILED tests/nightly/gpu/test_bert.py::TestBertModel::test_crossencoder - ImportError: BERT rankers needs pytorch-pretrained-BERT installed.
FAILED tests/nightly/gpu/test_style_gen.py::TestClassifierOnGenerator::test_simple - AssertionError
======== 3 failed, 72 passed, 356 warnings in 4435.44s (1:13:55) ========

we can ignore the first two; the third might be an issue with my downloaded model for that? (the assertion that failed was assert 'beam_size' in opt_from_disk

@klshuster
Copy link
Contributor Author

all tests pass!! PR is ready for review

Copy link
Contributor

@spencerp spencerp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me. Thanks for adding this!

Comment on lines 51 to 52
self.override_special_tokens(opt)
for i in range(self.tokenizer.vocab_size):
token = self.tokenizer._convert_id_to_token(i)
self.add_token(token)
self.freq[token] = 1
self._unk_token_idx = self.hf_tokenizer.unk_token_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these lines be swapped? Right now _unk_token_idx from override_special_tokens is overridden.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

except ImportError:
raise ImportError('Please run `pip install transformers`.')


version = float('.'.join(transformers.__version__.split('.')[:2]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; Can we identify this as a global more distinctly somehow? So that we don't end up mixing this up with some other variable named "version". Perhaps just all caps (VERSION)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i'll make it more explicit (HF_VERSION)

@klshuster
Copy link
Contributor Author

failing gpu test is flaky

@klshuster klshuster merged commit a8fe17c into master Mar 19, 2021
@klshuster klshuster deleted the t5 branch March 19, 2021 20:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants