T5 serves primarily as code for reproducing the experiments in [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer][paper]. In the paper, we demonstrate how to achieve state-of-the-art results on multiple NLP tasks using a text-to-text transformer pre-trained on a large text corpus.
The bulk of the code in this repository is used for loading, preprocessing, mixing, and evaluating datasets. It also provides a way to fine-tune the pre-trained models released alongside the publication.
T5 can be used as a library for future model development by providing useful modules for training and fine-tuning (potentially huge) models on mixtures of text-to-text tasks.
t5.data is a package for defining Task objects that provide tf.data.Datasets.
Each Task is made up of:
Additionally, you may optionally provide:
The data source can be an arbitrary function that provides a tf.data.Dataset, but we also provide simpler wrappers for datasets available in [TensorFlow Datasets (TFDS)][tfds] (a TfdsTask) or stored as text files with one example per line (a TextLineTask).
The text preprocessor converts the examples in the source dataset into the appropriate format for a text-to-text model with fields for inputs and targets. For example, the predefined t5.data.preprocessors.translate preprocessor converts inputs in the form
{'de': 'Das ist gut.', 'en': 'That is good.'}
to the form
{'inputs': 'translate German to English: Das ist gut.', 'targets': 'That is good.'}
In additon to text preprocessing, you can also use one or more token preprocessors to modify the inputs post-tokenization. We implemented our unsupervised pre-training objectives using these token preprocessors.
We provide many predefined preprocessors in t5.data.preprocessors, but you may also define your own.
The SentencePiece model is used to tokenize the input strings and decode the output tokens. You can create your own model with the google/sentencepiece library, or use our default one at t5.data.DEFAULT_SPM_PATH.
The metric function returns a score given the target and prediction from the model. You may also define a postprocess function to convert the target and prediction text to another format before calling the metric. We provide some predefined metrics in t5.evaluation.metrics.
Finally, t5.data contains a Mixture class that can be instantiated to combine multiple Task datasets for multi-task training using various functions for specifying the mixture rates.
t5.evaluation contains two core components:
t5.models contains shims for connecting T5 Tasks and Mixtures to a model implementation for training, evaluation, and inference.
Currently the only available shim is to [Mesh TensorFlow Transformer][mtft] (MeshTF), which enables both data and model parallelism for training massive Transformer models. This also includes a binary for launching the model in MeshTF along with [gin configs][gin] for setting various hyperparameters.
The easiest way to try out T5 is with a free TPU in our Colab Tutorial.
Below we provide examples for how to pre-train, fine-tune, evaluate, and decode from a model from the command-line with our codebase. You can use these instructions to reproduce our results, fine-tune one of our released checkpoints with your own data and/or hyperparameters, or pre-train a model from scratch.
You may either use a new or pre-existing Task, or you may load examples from a preprocessed TSV file.
TaskDepending on your data source (see above), you will need to prepare your data appropriately.
TaskIf using a vanilla task, just make sure any file(s) loaded by your dataset_fn are accessible to the TPU (i.e., are in a GCS bucket), and you should be good to go!
TfdsTaskMost of our predefined Tasks use [TensorFlow Datasets (TFDS)][tfds] as their data source. When you run our training binary (see instructions below) with a TfdsTask, the dataset will automatically be downloaded and prepared on its first use. After preparation is complete, the dataset is cached to your local storage to avoid this overhead in future runs. If working in the cloud, we recommend you set the --t5_tfds_data_dir flag to point to a persistent storage location, such as a [GCS bucket][gcs]. This is a requirement when training on TPU.
Note:The [C4][c4] dataset we created for unsupervised pre-training is available in TensorFlow Datasets, but it requires a significant amount of bandwith for downloading the raw [Common Crawl][cc] scrapes and compute for its preparation. We suggest you take advantage of the [Apache Beam][beam] support in TFDS, which enables distributed preprocessing of the dataset and can be run on [Google Cloud Dataflow][gcd]. Otherwise, it is unlikely that you will be able to complete preprocessing in a human lifetime. Read more in the [TFDS Beam instructions][tfds_beam].
TextLineTaskA TextLineTask is useful when your data source is a text file (or files) with one example per line. You can then use a text preprocessor to convert each line into a dictionary of inputs and targets.
Make sure your files are accessible to the TPU (i.e., are in a GCS bucket), and you should be good to go!
Instead of defining a new Task, you may use a TSV file (or files) directly as your dataset where each line is formatted as <input>\t<target>.
However, there are a couple of caveats:
If you need any of these features, you must define a new Task, TfdsTask, or TextLineTask.
Similar to the above cases, your TSV file(s) must be accessible to the TPU (i.e., are in a GCS bucket).
To install the T5 package, simply run:
pip install t5[gcp]
You will first need to launch a Virtual Machine (VM) on Google Cloud. Details about launching the VM can be found at the Google Cloud Documentation.
In order to run training or eval on Cloud TPUs, you must set up the following variables based on your project, zone and GCS bucket appropriately. Please refer to the Cloud TPU Quickstart guide for more details.
export PROJECT=your_project_name
export ZONE=your_project_zone
export BUCKET=gs://yourbucket/
export TPU_NAME=t5-tpu
export DATA_DIR="${BUCKET}/your_data_dir"
export MODEL_DIR="${BUCKET}/your_model_dir"
Please use the following command to create a TPU device in the Cloud VM.
ctpu up --name=$TPU_NAME --project=$PROJECT --zone=$ZONE --tpu-size=v3-8 \
--tpu-only --tf-version=2.1 --noconf
In the command below, we train a model on the GLUE Benchmark MRPC task from scratch. You can change the MIXTURE_NAME gin parameter to use any of the tasks or mixtures provided in our package.
t5_mesh_transformer \
--tpu="${TPU_NAME}" \
--gcp_project="${PROJECT}" \
--tpu_zone="${ZONE}" \
--model_dir="${MODEL_DIR}" \
--t5_tfds_data_dir="${DATA_DIR}" \
--gin_file="dataset.gin" \
--gin_file="models/bi_v1.gin" \
--gin_param="utils.tpu_mesh_shape.model_parallelism = 1" \
--gin_param="utils.tpu_mesh_shape.tpu_topology = '2x2'" \
--gin_param="MIXTURE_NAME = 'glue_mrpc_v002'"
The full list of tasks and mixtures can be obtained by running:
python -c "import t5; print(t5.data.MixtureRegistry.names())"
You may also define additional tasks and mixtures in a new file and import it using the --module_import flag.
Alternatively, you could train with a TSV file where each line is formatted as <input>\t<target> (see above).
In order to fine-tune one of our pre-trained models, you need to pass the operative config of the pre-trained model to the training script. The operative config should be passed in as a gin_file flag. It specifies the model architecture and other hyperparameters. In addition, you need to specify the mixture to fine-tune on. For example, to fine-tune the T5-small model on the glue_mrpc_v002 mixture, please run:
t5_mesh_transformer \
--tpu="${TPU_NAME}" \
--gcp_project="${PROJECT}" \
--tpu_zone="${ZONE}" \
--model_dir="${MODEL_DIR}" \
--t5_tfds_data_dir="${DATA_DIR}" \
--gin_file="dataset.gin" \
--gin_param="utils.tpu_mesh_shape.model_parallelism = 1" \
--gin_param="utils.tpu_mesh_shape.tpu_topology = '2x2'" \
--gin_param="MIXTURE_NAME = 'glue_mrpc_v002'" \
--gin_file="gs://t5-data/pretrained_models/small/operative_config.gin"
The correct pre-trained checkpoint path is included in the operative config.
You may also define additional tasks and mixtures in a new file and import it using the --module_import flag.
Alternatively, you could fine-tune with a TSV file where each line is formatted as <input>\t<target> (see above). For example, you could try one of the paired translation datasets from WMT '19 News Commentary 14 training set
(e.g., English-French). When using a TSV file, you would replace the MIXTURE_NAME flag with:
--gin_param="utils.run.train_dataset_fn = @t5.models.mesh_transformer.tsv_dataset_fn"
--gin_param="tsv_dataset_fn.filename = 'gs:/path/to/tsv'"
To fine-tune with the same hyperparameters we used in the [paper][paper] (using a constant learning rate of 0.001), you can pass in this gin file which is included in the T5 package:
--gin_file="learning_rate_schedules/constant_0_001.gin"
The operative config for the pre-trained models are set so that there is effectively no limit on the number of train steps. If you'd like to train for a specific number of steps, you'll need to pass that in. Since the pre-trained model has already been trained for 1,000,000 steps, you should specify the total number of steps after pre-training and fine-tuning. For example, if you want to fine-tune for an additional 10,000 steps, you should pass
--gin_param="run.train_steps = 1010000"
You can also use a different batch size for fine-tuning. We set the batch size according to the total number of tokens in a batch. By default, a batch uses a sequence length of 512. To set the number of tokens in a batch, you should set
--gin_param = "tokens_per_batch=1048576"
In order to evaluate a model in the T5 framework, you need to use the eval.gin file, specify the model directory, decoding method, and which checkpoint step(s) to evaluate. So, to evaluate on the GLUE MRPC task using beam search on all checkpoints, use the following command:
t5_mesh_transformer \
--tpu="${TPU_NAME}" \
--gcp_project="${PROJECT}" \
--tpu_zone="${ZONE}" \
--model_dir="${MODEL_DIR}" \
--gin_file="${MODEL_DIR}/operative_config.gin" \
--t5_tfds_data_dir=${DATA_DIR} \
--gin_file="eval.gin" \
--gin_file="beam_search.gin" \
--gin_param="utils.tpu_mesh_shape.tpu_topology = '2x2'" \
--gin_param="MIXTURE_NAME = 'glue_mrpc_v002'" \
--gin_param="eval_checkpoint_step = 'all'"
To evaluate a specific checkpoint, simply set the eval_checkpoint_step parameter to appropriate checkpoint.
--gin_param="eval_checkpoint_step = 100000"
You can also use greedy_decode.gin or sample_decode.gin instead of beam_search.gin in the command above.
In order to produce predictions from a model in the T5 framework, you need to specify the model directory, decoding method, and which checkpoint step(s) to use for decoding. Assuming you have a text file of input sequences stored at /path/to/intputs.txt, an example command would be:
t5_mesh_transformer \
--tpu="${TPU_NAME}" \
--gcp_project="${PROJECT}" \
--tpu_zone="${ZONE}" \
--model_dir="${MODEL_DIR}" \
--gin_file="${MODEL_DIR}/operative_config.gin" \
--gin_file="infer.gin" \
--gin_file="sample_decode.gin" \
--gin_param="input_filename = '/path/to/inputs.txt'"\
--gin_param="output_filename = '/tmp/outputs.txt'"\
--gin_param="utils.tpu_mesh_shape.tpu_topology = '2x2'"\
--gin_param="infer_checkpoint_step = 'all'"
To predict with a specific checkpoint, simply set the infer_checkpoint_step parameter to appropriate checkpoint.
--gin_param="infer_checkpoint_step = 100000"
You can also use beam_search.gin or greedy_decode.gin instead of sample_decode.gin in the command above.
You may also want to export a SavedModel, which is useful for serving your trained model, (e.g., when deploying with [ML Engine](https://cloud.google.com/ml-engine/
$ claude mcp add text-to-text-transfer-transformer \
-- python -m otcore.mcp_server <graph>