Knowledge Distillation in AutoMM¶
Pretrained foundation models are becoming increasingly large. However, these models are difficult to deploy due to limited resources available in deployment scenarios. To benefit from large models under this constraint, you transfer the knowledge from the large-scale teacher models to the student model, with knowledge distillation. In this way, the small student model can be practically deployed under real-world scenarios, while the performance will be better than training the student model from scratch thanks to the teacher.
In this tutorial, we introduce how to adopt MultiModalPredictor
for knowledge distillation. For the purpose of demonstration, we use the Question-answering NLI dataset,
which comprises 104,743 question, answer pairs sampled from question answering datasets. We will demonstrate how to use a large model to guide the learning and improve the performance of a small model in AutoGluon.
Load Dataset¶
The Question-answering NLI dataset contains
sentence pairs in English. In the label column, 0
means that the sentence is not related to the question and 1
means that the sentence is related to the question.
import datasets
from datasets import load_dataset
datasets.logging.disable_progress_bar()
dataset = load_dataset("glue", "qnli")
dataset['train']
Dataset({
features: ['question', 'sentence', 'label', 'idx'],
num_rows: 104743
})
from sklearn.model_selection import train_test_split
train_valid_df = dataset["train"].to_pandas()[["question", "sentence", "label"]].sample(1000, random_state=123)
train_df, valid_df = train_test_split(train_valid_df, test_size=0.2, random_state=123)
test_df = dataset["validation"].to_pandas()[["question", "sentence", "label"]].sample(1000, random_state=123)
Load the Teacher Model¶
In our example, we will directly load a teacher model with the google/bert_uncased_L-12_H-768_A-12 backbone that has been trained on QNLI and distill it into a student model with the google/bert_uncased_L-6_H-768_A-12 backbone.
!wget --quiet https://automl-mm-bench.s3.amazonaws.com/unit-tests/distillation_sample_teacher.zip -O distillation_sample_teacher.zip
!unzip -q -o distillation_sample_teacher.zip -d .
from autogluon.multimodal import MultiModalPredictor
teacher_predictor = MultiModalPredictor.load("ag_distillation_sample_teacher/")
/home/ci/autogluon/multimodal/src/autogluon/multimodal/data/templates.py:16: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.
import pkg_resources
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[6], line 3
1 from autogluon.multimodal import MultiModalPredictor
----> 3 teacher_predictor = MultiModalPredictor.load("ag_distillation_sample_teacher/")
File ~/autogluon/multimodal/src/autogluon/multimodal/predictor.py:849, in MultiModalPredictor.load(cls, path, resume, verbosity)
847 assets = json.load(fp)
848 learner_class = BaseLearner
--> 849 if assets["learner_class"] == "MatchingLearner":
850 learner_class = MatchingLearner
851 elif assets["learner_class"] == "EnsembleLearner":
KeyError: 'learner_class'
Distill to Student¶
Training the student model is straight forward. You may just add the teacher_predictor
argument when calling .fit()
.
Internally, the student will be trained by matching the prediction/feature map from the teacher. It can perform better than
directly finetuning the student.
student_predictor = MultiModalPredictor(label="label")
student_predictor.fit(
train_df,
tuning_data=valid_df,
teacher_predictor=teacher_predictor,
hyperparameters={
"model.hf_text.checkpoint_name": "google/bert_uncased_L-6_H-768_A-12",
"optim.max_epochs": 2,
}
)
print(student_predictor.evaluate(data=test_df))
More about Knowledge Distillation¶
To learn how to customize distillation and how it compares with direct finetuning, see the distillation examples and README in AutoMM Distillation Examples. Especially the multilingual distillation example with more details and customization.
Other Examples¶
You may go to AutoMM Examples to explore other examples about AutoMM.
Customization¶
To learn how to customize AutoMM, please refer to Customize AutoMM.