This repository was archived by the owner on Sep 10, 2025. It is now read-only.
Make T5 model torchscriptable #1876
Merged
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.
Description
Make minor design changes that allow for the T5 model to be scripted
Process
A few design changes had to be made for the model to become scriptable:
relative_attention_biasis no longer instantiated when a T5 layer is being instantiated. This is becauserelative_attention_biasthen needs to get passed down toT5MultiheadAttentionas an input argument, where the embedding is actually used, but Torchscript does not supportnn.Embeddinginput args. So instead,relative_attention_biasgets instantiated withT5MultiheadAttention. This completely avoids having to pass it in as an input argument.There seemed to be an issue with
all_outputs,all_sa_scores, andall_ca_scores(inT5Stack.forward) being tuples (probably because after every layer we "append" tensors to this object, and tuples aren't the best data structures for what we are effectively using as mutable objects). Changing their types toListcleared the torchscript errors.After correcting the above and a few other minor errors, we were then getting a vague error message of
torchscript RuntimeError: Unsupported value kind: Tensor. The error message did not provide any pointers to where in the code this issue was arising. It is unclear why exactly this worked, but breaking outT5LayerandT5Stackto beT5EncoderLayer,T5DecoderLayer,T5Encoder,T5Decoderseemed to resolve this issue.Testing
Updated pre-trained weights saved in S3/Manifold and modified integrations tests to test scripted version of models.
pytest test/prototype/integration_tests/test_models.py