Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/sagemaker/15_training_compiler/sagemaker-notebook.ipynb
Views: 2542
Hugging Face Transformers BERT fine-tuning using Amazon SageMaker and Training Compiler
Compile and fine-tune a Multi-Class Classification Transformers with Trainer
and emotion
dataset using Amazon SageMaker Training Compiler
Introduction
SageMaker Training Compiler Overview
SageMaker Training Compiler optimizes DL models to accelerate training by more efficiently using SageMaker machine learning (ML) GPU instances. SageMaker Training Compiler is available at no additional charge within SageMaker and can help reduce total billable time as it accelerates training.
SageMaker Training Compiler is integrated into the AWS Deep Learning Containers (DLCs). Using the SageMaker Training Compiler enabled AWS DLCs, you can compile and optimize training jobs on GPU instances with minimal changes to your code.
In this demo, we will use the Hugging Faces transformers
and datasets
library together with Amazon SageMaker and the new Amazon SageMaker Training Compiler to fine-tune a pre-trained transformer for multi-class text classification. In particular, the pre-trained model will be fine-tuned using the emotion
dataset. To get started, we need to set up the environment with a few prerequisite steps, for permissions, configurations, and so on.
NOTE: You can run this demo in Sagemaker Studio, your local machine or Sagemaker Notebook Instances
Development Environment and Permissions
Installation
Note: we only install the required libraries from Hugging Face and AWS. You also need PyTorch or Tensorflow, if you haven´t it installed
Permissions
If you are going to use Sagemaker in a local environment. You need access to an IAM Role with the required permissions for Sagemaker. You can find here more about it.
Preprocessing
We are using the datasets
library to download and preprocess the emotion
dataset. After preprocessing, the dataset will be uploaded to our sagemaker_session_bucket
to be used within our training job. The emotion dataset consists of 16000 training examples, 2000 validation examples, and 2000 testing examples.
Tokenization
Uploading data to sagemaker_session_bucket
After we processed the datasets
we are going to use the new FileSystem
integration to upload our dataset to S3.
Creating an Estimator and start a training job
The Amazon Training Compiler works best with Encoder Type models, like BERT
, RoBERTa
, ALBERT
, DistilBERT
.
The Model compilation using Amazon SageMaker Training compiler increases efficiency and lowers the memory footprint of your Transformers model, which allows larger batch sizes and more efficient and faster training.
We tested long classification tasks with BERT
, DistilBERT
and `RoBERTa and achieved up 33% higher batch sizes and 1.4x faster Training. For best performance, set batch size to a multiple of 8.
The longer your training job, the larger the benefit of using Amazon SageMaker Training Compiler. 30 minutes seems to be the sweet spot to offset model compilation time in the beginning of your training. Initial pre-training jobs are excellent candidates for using the new Amazon SageMaker Training Compiler.
Create a SageMakerEstimator
with the SageMakerTrainingCompiler
and the hyperparemters
, instance configuration and training script.
Start the training with the uploaded datsets on s3 with huggingface_estimator.fit()
.
Deploying the endpoint
To deploy our endpoint, we call deploy()
on our HuggingFace estimator object, passing in our desired number of instances and instance type.
Then, we use the returned predictor object to call the endpoint.
Finally, we delete the inference endpoint.