DFRot: Achieving Outlier-Free and Massive Activation-Free for Rotated LLMs with Refined Rotation

December 10, 2024 ยท View on GitHub

Motivation

In this paper, we have answered why randomized orthogonal transforms better than randomized Hadamard transforms.

Preliminary

Understanding LLM's rotational invariance and Massive Activation will help you better understand our work:

  1. QuaRot: Outlier-Free 4-Bit Inference in Rotated LLMs
  2. Massive Activations in Large Language Models

Visualization

NR: No Rotation; RO: Randomized Orthogonal transforms;

RH: Randomized Hadamard transforms; E: Per Token Quantization Error.

Method4-4-44-4-164-8-164-4-44-4-164-8-164-4-44-4-164-8-164-4-44-4-164-8-16
LLaMA2-7BLLaMA2-7BLLaMA2-7BLLaMA2-13BLLaMA2-13BLLaMA2-13BLLaMA3-8BLLaMA3-8BLLaMA3-8BMistral-7B-v0.3Mistral-7B-v0.3Mistral-7B-v0.3
GPTQNaNNaNNaNInfInf6.01InfInf7.29InfInf8.39
(RO) QuaRot7.967.715.616.005.924.9910.5410.156.526.055.985.40
(RO) QuaRot.FP16()6.176.10-5.385.34-7.837.68-5.795.73-
(RH) QuaRot6.276.205.615.515.465.018.208.026.525.815.755.40
(RH) QuaRot.FP16()6.176.10-5.405.37-7.827.67-5.785.73-

Chain of Thoughts (CoT)

Run for LLaMA3-8B

1. Generate Calibration Dataset

python3 generate_calibration.py --model meta-llama/Meta-Llama-3-8B --nsamples 1 \\
--output_dir calibration --disable_qk_rotation

2. Optimize Rotational Matrix

python3 optimize_procrustes_alter.py --rotate_mode hadamard --data_principle alter --alpha 100

3. Run Results

python3 main.py --model meta-llama/Meta-Llama-3-8B  --rotate --w_bits 4 --a_bits 4 --k_bits 4 \\
--v_bits 4 --w_clip --v_groupsize 128 --k_groupsize 128 --a_asym --k_asym --v_asym \\
--rotate_mode orthogonal_procrustes --indices_path rms_norm_feature_hadamard_alter/100/LLaMA-3-8B-4.npy \\
--fp32_had --seed 0 --lm_eval

More Scripts

# Random, Hadamard, Procrustes Scripts
CUDA_VISIBLE_DEVICES=None python3 scripts/generate_scripts.py
# For QuaRot.FP16()
CUDA_VISIBLE_DEVICES=None python3 scripts/separate_scripts.py
# Calibration Dataset
CUDA_VISIBLE_DEVICES=None python3 scripts/generate_calibration.py
# Without Rotation
CUDA_VISIBLE_DEVICES=None python3 scripts/vanilla_scripts.py

Main Results on WikiText-2 PPL

MethodLLaMA2-7BLLaMA2-7BLLaMA2-13BLLaMA2-13BLLaMA3-8BLLaMA3-8BMistral-7B-v0.3Mistral-7B-v0.3
Baseline5.475.474.884.886.146.145.325.32
Extra Time+8min+8min+20min+20min+8min+8min+8min+8min
4-4-44-4-164-4-44-4-164-4-44-4-164-4-44-4-16
RTNNaNNaNInfInfInfInfInfInf
QuaRot-RTN9.048.696.316.2311.0610.476.386.29
DFRot-RTN7.687.476.216.129.679.356.366.27
GPTQNaNNaNInfInfInfInfInfInf
QuaRot-GPTQ6.276.205.515.478.208.025.815.75
DFRot-GPTQ6.216.145.475.397.957.815.815.76

Main Results on Zero-Shot tasks

ModelMethodW-A-KVPQWGHSA-eA-cLAAvg.
LLaMA2-7BFP1616-16-1679.1168.9875.9974.5446.4273.8869.82
QuaRot4-4-1676.0665.6773.0069.8242.2469.4266.03
4-4-476.3364.9672.6968.6041.6468.5865.47
DFRot4-4-1677.1565.8273.1769.7844.3770.6666.83
4-4-476.2264.9672.4170.7542.6669.9266.15
LLaMA2-13BFP1616-16-1680.5272.2279.3977.4849.1576.7572.58
QuaRot4-4-1677.9168.5175.9473.5746.2572.9769.19
4-4-478.7370.4075.8273.7446.3372.7369.63
DFRot4-4-1678.7369.3076.9972.6945.8275.4169.82
4-4-479.8268.4376.7072.6446.5975.3369.92
LLaMA3-8BFP1616-16-1680.7972.8579.1677.7853.3376.0373.32
QuaRot4-4-1674.9266.6173.3970.2944.5467.7166.24
4-4-475.1466.5472.3268.6442.4166.0465.18
DFRot4-4-1676.2268.0373.9270.4145.6568.8767.18
4-4-475.6866.7773.5670.2945.1468.9966.74
Mistral-7B-v0.3FP1616-16-1682.2673.8880.4178.2052.3075.3273.73
QuaRot4-4-1679.5469.3077.8175.5147.9573.7670.65
4-4-479.3869.0677.3674.5448.2973.5570.36
DFRot4-4-1679.8769.5378.2475.8848.4673.0170.83
4-4-480.3669.6178.0175.5547.9572.3970.65

Reference

If you find DFRot useful or relevant to your research, please cite our paper:

@article{xiang2024dfrot,
  title={DFRot: Achieving Outlier-Free and Massive Activation-Free for Rotated LLMs with Refined Rotation},
  author={Xiang, Jingyang and Zhang, Saiqian},
  journal={arXiv preprint arXiv:2412.00648},
  year={2024}
}