{ "cells": [ { "cell_type": "markdown", "id": "a514e779", "metadata": {}, "source": [ "# Knowledge Distillation in AutoMM\n", "\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/autogluon/autogluon/blob/master/docs/tutorials/multimodal/advanced_topics/model_distillation.ipynb)\n", "[![Open In SageMaker Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/autogluon/autogluon/blob/master/docs/tutorials/multimodal/advanced_topics/model_distillation.ipynb)\n", "\n", "\n", "\n", "Pretrained foundation models are becoming increasingly large. However, these models are difficult to deploy due to \n", "limited resources available in deployment scenarios. To benefit from large models under this constraint, \n", "you transfer the knowledge from the large-scale teacher models to the student model, with knowledge distillation.\n", "In this way, the small student model can be practically deployed under real-world scenarios,\n", "while the performance will be better than training the student model from scratch thanks to the teacher.\n", "\n", "In this tutorial, we introduce how to adopt `MultiModalPredictor` for knowledge distillation. For the purpose of demonstration, we use the [Question-answering NLI](https://paperswithcode.com/dataset/qnli) dataset, \n", "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.\n", "\n", "## Load Dataset\n", "\n", "The [Question-answering NLI](https://paperswithcode.com/dataset/qnli) dataset contains \n", "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." ] }, { "cell_type": "code", "execution_count": null, "id": "aa00faab-252f-44c9-b8f7-57131aa8251c", "metadata": { "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "!pip install autogluon.multimodal\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f5bef2cc", "metadata": {}, "outputs": [], "source": [ "import datasets\n", "from datasets import load_dataset\n", "\n", "datasets.logging.disable_progress_bar()\n", "\n", "dataset = load_dataset(\"glue\", \"qnli\")" ] }, { "cell_type": "code", "execution_count": null, "id": "fe96574b", "metadata": {}, "outputs": [], "source": [ "dataset['train']" ] }, { "cell_type": "code", "execution_count": null, "id": "00a28e08", "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "\n", "train_valid_df = dataset[\"train\"].to_pandas()[[\"question\", \"sentence\", \"label\"]].sample(1000, random_state=123)\n", "train_df, valid_df = train_test_split(train_valid_df, test_size=0.2, random_state=123)\n", "test_df = dataset[\"validation\"].to_pandas()[[\"question\", \"sentence\", \"label\"]].sample(1000, random_state=123)" ] }, { "cell_type": "markdown", "id": "0c54ec47", "metadata": {}, "source": [ "## Load the Teacher Model\n", "\n", "In our example, we will directly load a teacher model with the [google/bert_uncased_L-12_H-768_A-12](https://huggingface.co/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](https://huggingface.co/google/bert_uncased_L-6_H-768_A-12) backbone." ] }, { "cell_type": "code", "execution_count": null, "id": "321edca7", "metadata": {}, "outputs": [], "source": [ "!wget --quiet https://automl-mm-bench.s3.amazonaws.com/unit-tests/distillation_sample_teacher.zip -O distillation_sample_teacher.zip\n", "!unzip -q -o distillation_sample_teacher.zip -d ." ] }, { "cell_type": "code", "execution_count": null, "id": "631e2b7a", "metadata": {}, "outputs": [], "source": [ "from autogluon.multimodal import MultiModalPredictor\n", "\n", "teacher_predictor = MultiModalPredictor.load(\"ag_distillation_sample_teacher/\")" ] }, { "cell_type": "markdown", "id": "6b67092f", "metadata": {}, "source": [ "## Distill to Student\n", "\n", "Training the student model is straight forward. You may just add the `teacher_predictor` argument when calling `.fit()`. \n", "Internally, the student will be trained by matching the prediction/feature map from the teacher. It can perform better than \n", "directly finetuning the student." ] }, { "cell_type": "code", "execution_count": null, "id": "fa0f0cd0", "metadata": {}, "outputs": [], "source": [ "student_predictor = MultiModalPredictor(label=\"label\")\n", "student_predictor.fit(\n", " train_df,\n", " tuning_data=valid_df,\n", " teacher_predictor=teacher_predictor,\n", " hyperparameters={\n", " \"model.hf_text.checkpoint_name\": \"google/bert_uncased_L-6_H-768_A-12\",\n", " \"optim.max_epochs\": 2,\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "9c178b00", "metadata": {}, "outputs": [], "source": [ "print(student_predictor.evaluate(data=test_df))" ] }, { "cell_type": "markdown", "id": "6d61593c", "metadata": {}, "source": [ "## More about Knowledge Distillation\n", "\n", "To learn how to customize distillation and how it compares with direct finetuning, see the distillation examples \n", "and README in [AutoMM Distillation Examples](https://github.com/autogluon/autogluon/tree/master/examples/automm/distillation).\n", "Especially the [multilingual distillation example](https://github.com/autogluon/autogluon/tree/master/examples/automm/distillation/automm_distillation_pawsx.py) with more details and customization.\n", "\n", "## Other Examples\n", "\n", "You may go to [AutoMM Examples](https://github.com/autogluon/autogluon/tree/master/examples/automm) to explore other examples about AutoMM.\n", "\n", "## Customization\n", "To learn how to customize AutoMM, please refer to [Customize AutoMM](customization.ipynb)." ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython" } }, "nbformat": 4, "nbformat_minor": 5 }