Files
artcraft/models/tts/NeMo/tutorials/nlp/GLUE_Benchmark.ipynb
T

23 KiB

In [ ]:
"""
You can run either this notebook locally (if you have all the dependencies and a GPU) or on Google Colab.

Instructions for setting up Colab are as follows:
1. Open a new Python 3 notebook.
2. Import this notebook from GitHub (File -> Upload Notebook -> "GITHUB" tab -> copy/paste GitHub URL)
3. Connect to an instance with a GPU (Runtime -> Change runtime type -> select "GPU" for hardware accelerator)
4. Run this cell to set up dependencies.
"""
# If you're using Google Colab and not running locally, run this cell

# install NeMo
BRANCH = 'v1.0.2'
!python -m pip install git+https://github.com/NVIDIA/NeMo.git@$BRANCH#egg=nemo_toolkit[nlp]
In [ ]:
# If you're not using Colab, you might need to upgrade jupyter notebook to avoid the following error:
# 'ImportError: IProgress not found. Please update jupyter and ipywidgets.'

! pip install ipywidgets
! jupyter nbextension enable --py widgetsnbextension

# Please restart the kernel after running this cell
In [ ]:
from nemo.collections import nlp as nemo_nlp
from nemo.utils.exp_manager import exp_manager

import os
import wget 
import torch
import pytorch_lightning as pl
from omegaconf import OmegaConf

In this tutorial, we are going to describe how to finetune a BERT-like model based on BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding on GLUE: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding.

GLUE tasks

GLUE Benchmark includes 9 natural language understanding tasks:

Single-Sentence Tasks

  • CoLA - The Corpus of Linguistic Acceptability is a set of English sentences from published linguistics literature. The task is to predict whether a given sentence is grammatically correct or not.
  • SST-2 - The Stanford Sentiment Treebank consists of sentences from movie reviews and human annotations of their sentiment. The task is to predict the sentiment of a given sentence: positive or negative.

Similarity and Paraphrase tasks

  • MRPC - The Microsoft Research Paraphrase Corpus is a corpus of sentence pairs automatically extracted from online news sources, with human annotations for whether the sentences in the pair are semantically equivalent.
  • QQP - The Quora Question Pairs dataset is a collection of question pairs from the community question-answering website Quora. The task is to determine whether a pair of questions are semantically equivalent.
  • STS-B - The Semantic Textual Similarity Benchmark is a collection of sentence pairs drawn from news headlines, video, and image captions, and natural language inference data. The task is to determine how similar two sentences are.

Inference Tasks

  • MNLI - The Multi-Genre Natural Language Inference Corpus is a crowdsourced collection of sentence pairs with textual entailment annotations. Given a premise sentence and a hypothesis sentence, the task is to predict whether the premise entails the hypothesis (entailment), contradicts the hypothesis (contradiction), or neither (neutral). The task has the matched (in-domain) and mismatched (cross-domain) sections.
  • QNLI - The Stanford Question Answering Dataset is a question-answering dataset consisting of question-paragraph pairs, where one of the sentences in the paragraph (drawn from Wikipedia) contains the answer to the corresponding question. The task is to determine whether the context sentence contains the answer to the question.
  • RTE The Recognizing Textual Entailment (RTE) datasets come from a series of annual textual entailment challenges. The task is to determine whether the second sentence is the entailment of the first one or not.
  • WNLI - The Winograd Schema Challenge is a reading comprehension task in which a system must read a sentence with a pronoun and select the referent of that pronoun from a list of choices (Hector Levesque, Ernest Davis, and Leora Morgenstern. The winograd schema challenge. In Thirteenth International Conference on the Principles of Knowledge Representation and Reasoning. 2012).

All tasks are classification tasks, except for the STS-B task which is a regression task. All classification tasks are 2-class problems, except for the MNLI task which has 3-classes.

More details about GLUE benchmark could be found here.

Datasets

To proceed further, you need to download the GLUE data. For example, you can download this script using wget and then execute it by running:

python download_glue_data.py

use --tasks TASK if datasets for only selected GLUE tasks are needed

After running the above commands, you will have a folder glue_data with data folders for every GLUE task. For example, data for MRPC task would be under glue_data/MRPC.

This tutorial and examples/nlp/glue_benchmark/glue_benchmark.py work with all GLUE tasks without any modifications. For this tutorial, we are going to use MRPC task.

In [ ]:
# supported task names: ["cola", "sst-2", "mrpc", "sts-b", "qqp", "mnli", "qnli", "rte", "wnli"]
TASK = 'mrpc'
DATA_DIR = 'glue_data/MRPC'
WORK_DIR = "WORK_DIR"
MODEL_CONFIG = 'glue_benchmark_config.yaml'
In [ ]:
! ls -l $DATA_DIR

For each task, there are 3 files: train.tsv, dev.tsv, and test.tsv. Note, MNLI has 2 dev sets: matched and mismatched, evaluation on both dev sets will be done automatically.

In [ ]:
# let's take a look at the training data 
! head -n 5 {DATA_DIR}/train.tsv

Model configuration

Now, let's take a closer look at the model's configuration and learn to train the model.

GLUE model is comprised of the pretrained BERT model followed by a Sequence Regression module (for STS-B task) or Sequence classifier module (for the rest of the tasks).

The model is defined in a config file which declares multiple important sections. They are:

  • model: All arguments that are related to the Model - language model, a classifier, optimizer and schedulers, datasets and any other related information

  • trainer: Any argument to be passed to PyTorch Lightning

In [ ]:
# download the model's configuration file 
config_dir = WORK_DIR + '/configs/'
os.makedirs(config_dir, exist_ok=True)
if not os.path.exists(config_dir + MODEL_CONFIG):
    print('Downloading config file...')
    wget.download(f'https://raw.githubusercontent.com/NVIDIA/NeMo/{BRANCH}/examples/nlp/glue_benchmark/' + MODEL_CONFIG, config_dir)
else:
    print ('config file is already exists')
In [ ]:
# this line will print the entire config of the model
config_path = f'{WORK_DIR}/configs/{MODEL_CONFIG}'
print(config_path)
config = OmegaConf.load(config_path)
print(OmegaConf.to_yaml(config))

Model Training

Setting up Data within the config

Among other things, the config file contains dictionaries called dataset, train_ds and validation_ds. These are configurations used to setup the Dataset and DataLoaders of the corresponding config.

We assume that both training and evaluation files are located in the same directory, and use the default names mentioned during the data download step. So, to start model training, we simply need to specify model.dataset.data_dir, like we are going to do below.

Also notice that some config lines, including model.dataset.data_dir, have ??? in place of paths, this means that values for these fields are required to be specified by the user.

Let's now add the data directory path, task name and output directory for saving predictions to the config.

In [ ]:
config.model.task_name = TASK
config.model.output_dir = WORK_DIR
config.model.dataset.data_dir = DATA_DIR

Building the PyTorch Lightning Trainer

NeMo models are primarily PyTorch Lightning modules - and therefore are entirely compatible with the PyTorch Lightning ecosystem.

Let's first instantiate a Trainer object

In [ ]:
print("Trainer config - \n")
print(OmegaConf.to_yaml(config.trainer))
In [ ]:
# lets modify some trainer configs
# checks if we have GPU available and uses it
cuda = 1 if torch.cuda.is_available() else 0
config.trainer.gpus = cuda

config.trainer.precision = 16 if torch.cuda.is_available() else 32

# for mixed precision training, uncomment the line below (precision should be set to 16 and amp_level to O1):
# config.trainer.amp_level = O1

# remove distributed training flags
config.trainer.accelerator = None

# setup max number of steps to reduce training time for demonstration purposes of this tutorial
config.trainer.max_steps = 128

trainer = pl.Trainer(**config.trainer)

Setting up a NeMo Experiment

NeMo has an experiment manager that handles logging and checkpointing for us, so let's use it:

In [ ]:
exp_dir = exp_manager(trainer, config.get("exp_manager", None))

# the exp_dir provides a path to the current experiment for easy access
exp_dir = str(exp_dir)
exp_dir

Before initializing the model, we might want to modify some of the model configs. For example, we might want to modify the pretrained BERT model and use Megatron-LM BERT or AlBERT model:

In [ ]:
# get the list of supported BERT-like models, for the complete list of HugginFace models, see https://huggingface.co/models
print(nemo_nlp.modules.get_pretrained_lm_models_list(include_external=True))

# specify BERT-like model, you want to use, for example, "megatron-bert-345m-uncased" or 'bert-base-uncased'
PRETRAINED_BERT_MODEL = "albert-base-v1"
In [ ]:
# add the specified above model parameters to the config
config.model.language_model.pretrained_model_name = PRETRAINED_BERT_MODEL

Now, we are ready to initialize our model. During the model initialization call, the dataset and data loaders we'll be prepared for training and evaluation. Also, the pretrained BERT model will be downloaded, note it can take up to a few minutes depending on the size of the chosen BERT model.

In [ ]:
model = nemo_nlp.models.GLUEModel(cfg=config.model, trainer=trainer)

Monitoring training progress

Optionally, you can create a Tensorboard visualization to monitor training progress.

In [ ]:
try:
  from google import colab
  COLAB_ENV = True
except (ImportError, ModuleNotFoundError):
  COLAB_ENV = False

# Load the TensorBoard notebook extension
if COLAB_ENV:
  %load_ext tensorboard
  %tensorboard --logdir {exp_dir}
else:
  print("To use tensorboard, please use this notebook in a Google Colab environment.")

Note, it’s recommended to finetune the model on each task separately. Also, based on GLUE Benchmark FAQ#12, there are might be some differences in dev/test distributions for QQP task and in train/dev for WNLI task.

In [ ]:
# start model training
trainer.fit(model)

Training Script

If you have NeMo installed locally, you can also train the model with examples/nlp/glue_benchmark/glue_benchmark.py.

To run training script, use:

python glue_benchmark.py \ model.dataset.data_dir=PATH_TO_DATA_DIR \ model.task_name=TASK

Average results after 3 runs:

Task Metric ALBERT-large ALBERT-xlarge Megatron-345m BERT base paper BERT large paper
CoLA Matthew's correlation 54.94 61.72 64.56 52.1 60.5
SST-2 Accuracy 92.74 91.86 95.87 93.5 94.9
MRPC F1/Accuracy 92.05/88.97 91.87/88.61 92.36/89.46 88.9/- 89.3/-
STS-B Person/Spearman corr. 90.41/90.21 90.07/90.10 91.51/91.61 -/85.8 -/86.5
QQP F1/Accuracy 88.26/91.26 88.80/91.65 89.18/91.91 71.2/- 72.1/-
MNLI Matched /Mismatched acc. 86.69/86.81 88.66/88.73 89.86/89.81 84.6/83.4 86.7/85.9
QNLI Accuracy 92.68 93.66 94.33 90.5 92.7
RTE Accuracy 80.87 82.86 83.39 66.4 70.1

WNLI task was excluded from the experiments due to the problematic WNLI set. The dev sets were used for evaluation for ALBERT and Megatron models, and the test sets results for the BERT paper.

Hyperparameters used to get the results from the above table, could be found in the table below. Some tasks could be further finetuned to improve performance numbers, the tables are for a baseline reference only. Each cell in the table represents the following parameters: Number of GPUs used/ Batch Size/ Learning Rate/ Number of Epochs. For not specified parameters, please refer to the default parameters in the training script.

Task ALBERT-large ALBERT-xlarge Megatron-345m
CoLA 1 / 32 / 1e-5 / 3 1 / 32 / 1e-5 / 10 4 / 16 / 2e-5 / 12
SST-2 4 / 16 / 2e-5 / 5 4 / 16 / 2e-5 /12 4 / 16 / 2e-5 / 12
MRPC 1 / 32 / 1e-5 / 5 1 / 16 / 2e-5 / 5 1 / 16 / 2e-5 / 10
STS-B 1 / 16 / 2e-5 / 5 1 / 16 / 4e-5 / 12 4 / 16 / 3e-5 / 12
QQP 1 / 16 / 2e-5 / 5 4 / 16 / 1e-5 / 12 4 / 16 / 1e-5 / 12
MNLI 4 / 64 / 1e-5 / 5 4 / 32 / 1e-5 / 5 4 / 32 / 1e-5 / 5
QNLI 4 / 16 / 1e-5 / 5 4 / 16 / 1e-5 / 5 4 / 16 / 2e-5 / 5
RTE 1 / 16 / 1e-5 / 5 1 / 16 / 1e-5 / 12 4 / 16 / 3e-5 / 12