Home

Awesome

Outlier Weighed Layerwise Sparsity (OWL): A Missing Secret Sauce for Pruning LLMs to High Sparsity [ICML 2024]

Official PyTorch implementation of OWL: A Missing Secret Sauce for Pruning LLMs to High Sparsity

Lu Yin, You Wu, Zhenyu Zhang, Cheng-Yu Hsieh, Yaqing Wang, Yiling Jia, Mykola Pechenizkiy, Yi Liang, Zhangyang Wang, Shiwei Liu

University of Texas at Austin, Eindhoven University of Technology, Google Research, NY. University of Washington.

The code can be contacted at l.yin@tue.nl.

<p align="center"> <img src="./Images/Layer_wise_sparsity.png" width="700" height="200"> </p>

OWL layerwise sparsity and Uniform layerwise sparsity at 70% sparsity. The bar chart in background corresponds to the Layerwise Outlier Distribution.

Update

Table of contents

TL;DR


Inspiring by the strong correlation to the emergent outliers in the feature dimensions in LLMs, we propose effective layer-wise sparsity ratios for LLM pruning, achieving significant improvement.

Abstract


Large Language Models (LLMs), renowned for their remarkable performance across diverse domains, present a challenge due to their colossal model size when it comes to practical deployment. In response to this challenge, efforts have been directed toward the application of traditional network pruning techniques to LLMs, uncovering a massive number of parameters can be pruned without hurting performance. Building upon insights gained from pre-LLM models, particularly BERT-level language models, prevailing LLM pruning strategies have consistently adhered to the practice of uniformly pruning all layers at equivalent sparsity levels, resulting in robust performance. However, this observation stands in contrast to the prevailing trends observed in the field of vision models, where non-uniform layerwise sparsity typically yields substantially improved results. To elucidate the underlying reasons for this disparity, we conduct a comprehensive analysis of the distribution of token features within LLMs. In doing so, we discover a strong correlation with the emergence of outliers, defined as features exhibiting significantly greater magnitudes compared to their counterparts in feature dimensions. Inspired by this finding, we introduce a novel LLM pruning methodology that incorporates a tailored set of non-uniform layerwise sparsity ratios specifically designed for LLM pruning, termed as Outlier Weighed Layerwise sparsity (OWL). The sparsity ratio of OWL is directly proportional to the outlier ratio observed within each layer, facilitating a more effective alignment between layerwise weight sparsity and outlier ratios. Our empirical evaluation, conducted across the LLaMA-V1 family and OPT, spanning various benchmarks, demonstrates the distinct advantages offered by OWL over previous methods. For instance, our approach exhibits a remarkable performance gain, surpassing the state-of-the-art Wanda and SparseGPT by 61.22 and 6.80 perplexity at a high sparsity level of 70%, respectively. Code is submitted.

Results


<p align="center"> <img src="./Images/ppl.png" width="700" height="200"> </p> <p style="text-align: center;"><i>WikiText validation perplexity of OWL applied to SparseGPT and Wanda</i></p>
MethodLayerwise SparsityWeight UpdateLLaMA-V1 7BLLaMA-V1 13BLLaMA-V1 30BLLaMA-V1 65BOPT 6.7B
Dense--5.685.094.104.7710.13
MagnitudeUniform48419.1284539.45977.7346.89290985.03
WandaUniform85.7755.9017.3715.23162.92
OWL w. WandaNon-Uniform24.5517.1710.758.6140.22
SparseGPTUniform✔️26.3019.2412.5610.4520.29
OWL w. SparseGPTNon-Uniform✔️19.4914.5510.288.2822.48
<p style="text-align: center;"><i>WikiText validation perplexity of pruning methods for LLaMA-V1 family and OPT-6.7B at 70% sparsity. The best performance method is indicated in <b>bold </b>, and the gain in perplexity achieved by OWL is highlighted in blue.</i></p>

Installation


Installation instructions can be found in INSTALL.md.

Usage


We provide a quick overview of the arguments:


Script example of pruning llama-7b using OWL-wanda

python   main.py    \
--model_name_or_path decapoda-research/llama-7b-hf     \
--Lamda 0.08 \
--Hyper_m 5 \
--model decapoda-research/llama-7b-hf     \
--prune_method wanda_owl     \
--sparsity_ratio 0.7 \
--sparsity_type unstructured \
--save save_test/

Script example of pruning llama-7b using OWL-wanda [n:m sparsity]

python   main.py    \
--model_name_or_path decapoda-research/llama-7b-hf     \
--Lamda 2 \
--Hyper_m 6 \
--model decapoda-research/llama-7b-hf     \
--prune_method wanda_owl_structure      \
--sparsity_ratio 0.5 \
--sparsity_type "5:8"  \
--save save_test/

Script example of pruning llama-7b using OWL-SparseGPT

python   main.py    \
--model_name_or_path decapoda-research/llama-7b-hf     \
--Lamda 0.08 \
--Hyper_m 5 \
--model decapoda-research/llama-7b-hf     \
--prune_method sparsegpt_owl     \
--sparsity_ratio 0.7 \
--sparsity_type unstructured \
--save save_test/

Script example of pruning llama-7b using OWL-magnitude

python   main.py    \
--model_name_or_path decapoda-research/llama-7b-hf     \
--Lamda 0.08 \
--Hyper_m 5 \
--model decapoda-research/llama-7b-hf     \
--prune_method magnitude_owl      \
--sparsity_ratio 0.7 \
--sparsity_type unstructured \
--save save_test/

Zero-shot evaluation

Step1: generation data:

bash scripts/data_genration/run.sh 

Step2: obtain prediction:

bash benchmark/run.sh 

Step3: evaluation results:

bash benchmark/eval.sh

CPU-Acceleration

Install sparseml and deepsparse

Step1: create sparse model checkpoint using OWL, and save to model_path

Step2: export checkpoint to ONNX format

sparseml.export --task text-generation model_path

Step3: evaluate using deepsparse

deepsparse.benchmark model_path/deployment/model.onnx --sequence_length 2048

Lora finetuning on C4

Step1: lora ft checkpoint from "your_sparse_model_path", and save to "lora_ft_model_path"

CUDA_VISIBLE_DEVICES=0,1 python finetune_lm.py
--model_name_or_path "your_sparse_model_path"
--config_name "your_sparse_model_path"
--dataset_name c4
--num_train_epochs 1
--block_size 1024
--per_device_train_batch_size 2
--per_device_eval_batch_size 8
--do_train
--do_eval
--max_train_samples 240000
--max_eval_samples 128
--learning_rate 1e-4
--overwrite_output_dir
--output_dir "lora_ft_model_path"

Step2: evaluate the ppl

CUDA_VISIBLE_DEVICES=0,1 python evaluate_ppl.py
--model "your_sparse_model_path"
--lora_weights "lora_ft_model_path"

Acknowledgement

This repository is build upon the Wanda and SparseGPT repositories. The cpu acceleration is based on sparseml and deepsparse repositories.

Citation

if you find this repo is helpful, please cite

@article{yin2023outlier,
  title={Outlier weighed layerwise sparsity (owl): A missing secret sauce for pruning llms to high sparsity},
  author={Yin, Lu and Wu, You and Zhang, Zhenyu and Hsieh, Cheng-Yu and Wang, Yaqing and Jia, Yiling and Pechenizkiy, Mykola and Liang, Yi and Wang, Zhangyang and Liu, Shiwei},
  journal={arXiv preprint arXiv:2310.05175},
  year={2023}
}