KD-Lib: A PyTorch library for Knowledge Distillation, Pruning and Quantization

Het Shah, Avishree Khare, Neelay Shah, Khizir SiddiquiarXiv 2020

Abstract: In recent years, the growing size of neural networks has led to a vast amount of research concerning compression techniques to mitigate the drawbacks of such large sizes. Most of these research works can be categorized into three broad families: Knowledge Distillation, Pruning, and Quantization. While there has been steady research in this domain, adoption and commercial usage of the proposed techniques has not quite progressed at the rate. We present KD-Lib, an open-source PyTorch based library, which contains state-of-the-art modular implementations of algorithms from the three families on top of multiple abstraction layers. KD-Lib is model and algorithm-agnostic, with extended support for hyperparameter tuning using Optuna and Tensorboard for logging and monitoring.

Introduction

Deep Neural Networks (DNNs) have achieved state-of-the-art performance across various domains, but their success comes at a cost: a massive number of parameters. These "heavy" models are often too computationally expensive or memory-intensive to deploy on edge devices, mobile phones, or in real-time commercial environments.

While the research community has proposed numerous compression techniques—specifically Knowledge Distillation (KD), Pruning, and Quantization—adopting these methods is often difficult. Implementations are frequently fragmented across different repositories, hard to understand, or specific to certain architectures. KD-Lib addresses this by providing an "umbrella framework" that brings these state-of-the-art algorithms together into a single, easy-to-use library.

Comparison of KD-Lib with other existing model compression libraries.

The KD-Lib Framework

KD-Lib introduces a modular architecture built on top of PyTorch, categorized into three main functional pillars:

  1. Knowledge Distillation: This involves a "teacher-student" paradigm where a large, pre-trained model transfers its "knowledge" to a smaller student model. KD-Lib supports 13 vision-based algorithms (like Deep Mutual Learning and Self-Training) and text-based distillation (e.g., BERT to LSTM).
  2. Pruning: This technique reduces model size by removing "unimportant" weights. The library currently implements pruning based on the Lottery Ticket Hypothesis, which suggests that dense networks contain sub-networks that can reach the same accuracy as the original when trained from scratch.
  3. Quantization: This reduces the precision of weights (e.g., from 32-bit floating point to 8-bit integers), effectively cutting the model size by 4x. The library supports Static, Dynamic, and Quantization Aware Training (QAT).

The library is designed for ease of use, featuring a "Distiller," "Pruner," and "Quantizer" object structure where core functions can often be executed in a single line of code. It also integrates Optuna for hyperparameter tuning and Tensorboard for logging and monitoring.

Evaluation and Results

The authors evaluated the library's implementations using standard benchmarks: CIFAR10 for Knowledge Distillation and Quantization, MNIST for Pruning, and IMDB for NLP tasks.

Accuracy of various Knowledge Distillation algorithms implemented in KD-Lib on the CIFAR10 dataset.

As shown in the table above, Knowledge Distillation provides significant performance boosts. On the CIFAR10 dataset, a baseline ResNet18 student achieved 57% accuracy. Using the Deep Mutual Learning (DML) algorithm via KD-Lib, the same model's accuracy jumped to 62%, benefiting from a ResNet34 teacher.

In quantization experiments, Static Quantization and QAT reduced the model size by 75% while only suffering a minor accuracy drop (from 0.72 to 0.70/0.71). Pruning tests using the Lottery Ticket Hypothesis on MNIST successfully pruned 19% of the model while maintaining an accuracy of 98.9%, demonstrating that significant portions of the network were redundant.

Conclusion and Future Work

KD-Lib bridges the gap between research and production by offering a model-agnostic, modular, and easy-to-use interface for model compression. By abstracting complex training loops into dedicated methods (like train_student or quantize), the library lowers the barrier to entry for engineers who aren't specialists in the field.

Future development of KD-Lib will focus on expanding the variety of supported algorithms, implementing distributed training for larger models, and adding features for model explainability and interpretability. While currently focused on Vision and Text, there is also potential to expand into domains like audio or graph-based neural networks.

Summary updated

BibTeX
@misc{shah2020kd,
  author = {Het Shah and Avishree Khare and Neelay Shah and Khizir Siddiqui},
  title = {{KD-Lib: A PyTorch library for Knowledge Distillation, Pruning and Quantization}},
  year = {2020},
  eprint = {2011.14691},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2011.14691},
}
Figure