Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ニューラルネットワークの量子化手法の紹介

Avatar for Ryosuke Kamesawa Ryosuke Kamesawa
September 14, 2023

 ニューラルネットワークの量子化手法の紹介

ニューラルネットワークにおける量子化とLLMや混合精度などの最近のトピックに関して紹介します。

Avatar for Ryosuke Kamesawa

Ryosuke Kamesawa

September 14, 2023
Tweet

More Decks by Ryosuke Kamesawa

Other Decks in Technology

Transcript

  1. AI 亀澤諒亮 Kamesawa Ryosuke - AI創薬 @DeNA (2018-2019) - DRIVE

    CHART @GO Inc. (2019-) - AIモデルのデバイス組み込み - MLOps - モデル高速化、軽量化 自己紹介 2
  2. AI 5 量子化誤差によって推論精度が劣化するデメリット ニューラルネットの量子化とは s: scale, z: zero point, q

    min , q max : 量子化された値の値域 (unquantized float value) (quantized int value)
  3. AI 6 何を - Weight - Activation 何に - IntN

    (N=2,3,4,8,16, etc.) - Ternary / Binary - 混合精度 量子化のバリエーション どの粒度で - Layer-wise - Channel-wise - Token-wise どうやって - 学習後量子化 (PTQ, Post Training Quantization) - 量子化を意識した学習 (QAT, Quantization-Aware Training)
  4. AI 7 - Weight only - 演算時 - Floatに戻して計算 [Zhou+

    '17] - 計算は効率化されない - 量子化したまま [Lin+ '23] - ハードウェアの対応やカーネルの実装が必要 - Weight + Activation - 全て整数演算で完結できる[Jacob+ '18] 何を量子化するか
  5. AI 8 - IntN - N=8 (byte) が最も一般的でどのHWでも利用可能 - N<8の場合は高速化を目的とするならHWに依存

    - 圧縮だけが目的ならソフトウェアでやればよい - Ternary (-1, 0, +1) / Binary (-1, 1) - 量子化の極端なケース - Int4, Int8と比べると全く別のアプローチが必要 [Lin+ '17] - 混合精度 (mixed precision) - レイヤーごとに適切なフォーマット(ビット幅)を選択 何に量子化するか
  6. AI 9 同じ量子化パラメータ(スケール、ゼロ点)を適用する範囲 - Layer-wise - ネットワークのレイヤーごと - Channel-wise -

    チャンネルごと - チャネル数倍量子化パラメータは増えるが 正確に量子化できる - Token-wise - トークンごとに動的に量子化パラメータを計算 - Transformer系のactivationで使われる どの粒度で量子化するか
  7. AI 10 - 学習後量子化 (PTQ, Post Training Quantization) - 学習済みのモデルに対して量子化を行う

    - 量子化パラメータ(スケールとゼロ点)の計算 - データ (weight, activation) の分布から適切な値を決定する - スケールが小さい→表現できる範囲が小さいが範囲内では正確 - スケールが大きい→表現できる範囲は大きいが範囲内でも誤差が大きい - e.g. Min/Max [Krishnamoorthi '18] , L2 loss minimization [Nagel+ '21] どうやって量子化するか
  8. AI 11 - 学習後量子化 (PTQ, Post Training Quantization) - static

    or dynamic quantization - static: activationの量子化パラメータを事前に決定(calibration) - 主にCNNで使われる - 比較的外れ値が少ない - dynamic: activationの量子化パラメータをforward時に決定 - transformerで使われる - 外れ値の影響が大きい - staticな場合は事前にcalibration dataを用意して activationの分布を取得する - Data-free な手法もある [Qian+ '23] [Liu+ '23] [Nagel+ '19] どうやって量子化するか
  9. AI 12 - 量子化を意識した学習 (QAT, Quantization-Aware Training) - backpropを通して重みの量子化まで行う -

    通常の学習後のfine-tuningとして行うのが一般的 - Quantization simulation (straight through estimator) - forward時に量子化シミュレーションを行い、backwardはそのまま どうやって量子化するか [Gholami+ '22]
  10. AI 15 - PTQ (static, dynamic) - TensorFlowからTFLiteに変換する過程で量子化 - QAT

    - TensorFlow (keras)で量子化シミュレーションしたモデルで fine-tune後にTFLiteに変換 - EdgeTPU, HexagonDSPに対応 - static quantization が必須 TensorFlow Lite Quantization
  11. AI 16 - PTQ (static, dynamic) / QAT (static) をサポート

    - 2種類の仕組み - Eager Mode Quantization - 量子化Opを自分でネットワークに組み込む必要がある - FX Graph Mode Quantization - 量子化Opの自動挿入、自動Op Fusion - prototypeなのでAPIはすぐ変わるかも? - ONNXへのexportはサポートされていないので 外部ツールに頼る必要がある - Floatに戻してexport後、再量子化 PyTorch Quantization
  12. AI 17 Qualcommが開発しているモデル軽量化ツールキット - モバイルへのデプロイを前提とした量子化 - static (full-interger) quantization -

    PyTorch, TensorFlow上でのPTQ/QAT - 精度劣化を改善する手法が実装 - Cross Layer Equalization [Nagel+ '19] - PTQでのchannel間での分布の違いを減らす - AdaRound [Nagel+ '20] - PTQでFloatからIntへの丸めの方向を最適化 (最近接への丸めが最適とは限らない) AIMET Quantization
  13. AI 20 - Dynamic PTQが多い - メモリアクセスがボトルネック - モデルサイズも大きいのでQATが難しい -

    Weightは3, 4bit程度まで量子化可能 [Frantar+ '22] - 外れ値の影響が大きい [Dettmers+ '22] - 外れ値だけfloat [Dettmers+ '22] - activationをchannelごとにスケール、シフト [Wei+ '23] [Lin+ '23] [Xiao+ '23] - 量子化でfine-tuneも効率化 [Dettmers+ '23] [Kim+ '23] - 量子化に対応した実装も出てきている - https://github.com/ggerganov/llama.cpp LLMへの応用
  14. AI 21 手法の自由度にHWが追いついてきつつある → 混合精度で精度劣化と圧縮率のトレードオフを探る - Sensitivity analysis+ 貪欲(二部探索) [Schaefer+

    '23] [Pandey+ '23] - 各レイヤーで量子化フォーマットを変えたときの影響を測定 - 強化学習で組み合わせを探索 [Wang+ '19] - 学習時間が膨大 - 量子化精度(ビット幅)自体を微分可能に [Koryakovskiy+, '23] [Wu+ '18] 混合精度
  15. AI 23 ▪ Jacob, Benoit, et al. "Quantization and training

    of neural networks for efficient integer-arithmetic-only inference." Proceedings of the IEEE conference on computer vision and pattern recognition. 2018. ▪ Zhou, Aojun, et al. "Incremental network quantization: Towards lossless cnns with low-precision weights." arXiv preprint arXiv:1702.03044 (2017). ▪ Lin, Ji, et al. "AWQ: Activation-aware Weight Quantization for LLM Compression and Acceleration." arXiv preprint arXiv:2306.00978 (2023). ▪ Krishnamoorthi, Raghuraman. "Quantizing deep convolutional networks for efficient inference: A whitepaper." arXiv preprint arXiv:1806.08342 (2018). ▪ Nagel, Markus, et al. "A white paper on neural network quantization." arXiv preprint arXiv:2106.08295 (2021). ▪ Lin, Xiaofan, Cong Zhao, and Wei Pan. "Towards accurate binary convolutional neural network." Advances in neural information processing systems 30 (2017). ▪ Gholami, Amir, et al. "A survey of quantization methods for efficient neural network inference." Low-Power Computer Vision. Chapman and Hall/CRC, 2022. 291-326. ▪ Nagel, Markus, et al. "Data-free quantization through weight equalization and bias correction." Proceedings of the IEEE/CVF International Conference on Computer Vision. 2019. ▪ Nagel, Markus, et al. "Up or down? adaptive rounding for post-training quantization." International Conference on Machine Learning. PMLR, 2020. ▪ Dettmers, Tim, et al. "Llm. int8 (): 8-bit matrix multiplication for transformers at scale." arXiv preprint arXiv:2208.07339 (2022). ▪ Wei, Xiuying, et al. "Outlier Suppression+: Accurate quantization of large language models by equivalent and optimal shifting and scaling." arXiv preprint arXiv:2304.09145 (2023). ▪ Lin, Ji, et al. "AWQ: Activation-aware Weight Quantization for LLM Compression and Acceleration." arXiv preprint arXiv:2306.00978 (2023). ▪ Xiao, Guangxuan, et al. "Smoothquant: Accurate and efficient post-training quantization for large language models." International Conference on Machine Learning. PMLR, 2023. 参考文献
  16. AI 24 ▪ Frantar, Elias, et al. "Gptq: Accurate post-training

    quantization for generative pre-trained transformers." arXiv preprint arXiv:2210.17323 (2022). ▪ Kim, Jeonghoon, et al. "Memory-Efficient Fine-Tuning of Compressed Large Language Models via sub-4-bit Integer Quantization." arXiv preprint arXiv:2305.14152 (2023). ▪ Schaefer, Clemens JS, et al. "Mixed Precision Post Training Quantization of Neural Networks with Sensitivity Guided Search." arXiv preprint arXiv:2302.01382 (2023). ▪ Pandey, Nilesh Prasad, et al. "A Practical Mixed Precision Algorithm for Post-Training Quantization." arXiv preprint arXiv:2302.05397 (2023). ▪ Wang, Kuan, et al. "Haq: Hardware-aware automated quantization with mixed precision." Proceedings of the IEEE/CVF conference on computer vision and pattern recognition. 2019. ▪ Koryakovskiy, Ivan, et al. "One-Shot Model for Mixed-Precision Quantization." Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition. 2023. ▪ Wu, Bichen, et al. "Mixed precision quantization of convnets via differentiable neural architecture search." arXiv preprint arXiv:1812.00090 (2018). ▪ Qian, Biao, et al. "Adaptive Data-Free Quantization." Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition. 2023. ▪ Liu, Zechun, et al. "LLM-QAT: Data-Free Quantization Aware Training for Large Language Models." arXiv preprint arXiv:2305.17888 (2023). 参考文献