見出し画像

llama.cppでHuggingFaceモデルを4bit量子化😚【GoogleColab】/大塚

前回、llama.cppをColabで動かして、HuggingFaceのモデルをGGUFに変換しました。

今回はこちらの記事を参考に、llama.cppで4bitと2bitの量子化をGoogle Colabで試します。

ColabのCPUで動きました。ただ2bit量子化をする場合は、L4の使用をおすすめします。

モデルのダウンロード

huggingface_hubライブラリをインストールします。

!pip install huggingface_hub

Hugging Faceからモデルをダウンロードします。
model_idにダウンロードしたいモデル名を入力してください。
local_dirには保存先を指定します。

from huggingface_hub import snapshot_download
model_id="4piken/Llama-3-Gozaru-8B-Instruct"
snapshot_download(repo_id=model_id, local_dir="Llama-3-Gozaru-8B-Instruct",
                  local_dir_use_symlinks=False, revision="main")

※「HF_TOKEN」が必要とのエラーが出た場合、Google Colab左側メニューの鍵アイコンをクリックして、「HF_TOKEN」という名前を入力し、自分のHugging FaceページからAccess Tokenを発行し、コピペします。

モデルを変換する

llama.cppをクローンしてmakeします。5分ほどかかります。

# llama.cppのダウンロード
!git clone https://github.com/ggerganov/llama.cpp
%cd llama.cpp
!make

依存関係をダウンロードします。

!sed -i -e "1c numpy==1.25.0" /content/llama.cpp/requirements/requirements-convert-legacy-llama.txt
!sed -i -e "5c protobuf==3.20.3" /content/llama.cpp/requirements/requirements-convert-legacy-llama.txt
!sed -i -e "2c torch==2.3.0" /content/llama.cpp/requirements/requirements-convert-hf-to-gguf.txt
!sed -i -e "2c torch==2.3.0" /content/llama.cpp/requirements/requirements-convert-hf-to-gguf-update.txt

# 依存関係のインストール
!python3 -m pip install -r requirements.txt
!pip install tiktoken

モデルをggufに変換する。

# モデルをgguf(FP16)へ変換
!python /content/llama.cpp/convert-hf-to-gguf.py \
  --outfile /content/Llama-3-Gozaru-8B-Instruct-f16.gguf \
  --outtype f16 \
  /content/Llama-3-Gozaru-8B-Instruct

4bit(q4_k_m)に量子化する。※CPUだと20分ほどかかります。L4だと2分ほどです。
モデルサイズは、16GB→5GBになりました。

%cd /content/llama.cpp

# gguf(FP16)を4bit量子化(q4_k_m)
!./quantize /content/Llama-3-Gozaru-8B-Instruct-f16.gguf \
  /content/Llama-3-Gozaru-8B-Instruct.gguf-q4_k_m.gguf \
  q4_K_M

推論してみます。

%cd /content/llama.cpp

# 4bitモデルで推論
question = "犬に仏の性質はあるのでしょうか?"
usr_prompt = f"<|start_header_id|>system<|end_header_id|>必ず日本語で回答してください。<|eot_id|><|start_header_id|>user<|end_header_id|>{question}<|eot_id|><|start_header_id|>assistant<|end_header_id|>"
!./main -m /content/Llama-3-Gozaru-8B-Instruct.gguf-q4_k_m.gguf \
  --temp 0.6 \
  -p "{usr_prompt}" \
  -ngl 32 \
  -b 512 \
  -n 96 \
  --repeat-penalty 1.1

我、りんえもんは思う。 仏教では、仏様という言葉を使い、仏様を尊敬することが多くありますでござる。 しかし、仏様が犬に似ていることはあまり知られていないのでしょう。 仏様は人間に似ていると見なされていますでござる。 しかし、仏様は人間より小さいという特徴があり

2bit量子化

imatrixデータセットをダウンロードします。

from huggingface_hub import hf_hub_download

%cd /content/llama.cpp

REPO_ID = "TFMC/imatrix-dataset-for-japanese-llm"
FILENAME = "c4_en_ja_imatrix.txt"

hf_hub_download(repo_id=REPO_ID, filename=FILENAME, local_dir="/content/c4_en_ja_imatrix", repo_type="dataset")

ggufからimatrixに変換します。
※CPUだと3時間ぐらいかかりそうです。途中で諦めました。L4だと18分ほどで終わりました。

# ggufからimatrixへの変換
!./imatrix -m /content/Llama-3-Gozaru-8B-Instruct-f16.gguf \
  -f /content/c4_en_ja_imatrix/c4_en_ja_imatrix.txt \
  -o /content/Llama-3-Gozaru-8B-Instruct.imatrix \
  --chunks 32

imatrixを2bit量子化(iq2_xs)します。L4で20分かかりました。
モデルサイズは、16GB→2.6GBになりました。

# imatrixを2bit量子化(iq2_xs)
!./quantize \
  --imatrix /content/Llama-3-Gozaru-8B-Instruct.imatrix \
  /content/Llama-3-Gozaru-8B-Instruct-f16.gguf \
  /content/Llama-3-Gozaru-8B-Instruct_IQ2_XS.gguf \
  iq2_xs

2bitモデルで推論を試します。

# 2bitモデルで推論
question = "犬に仏の性質はあるのでしょうか?"
usr_prompt = f"<|start_header_id|>system<|end_header_id|>必ず日本語で回答してください。<|eot_id|><|start_header_id|>user<|end_header_id|>{question}<|eot_id|><|start_header_id|>assistant<|end_header_id|>"
!./main -m /content/Llama-3-Gozaru-8B-Instruct_IQ2_XS.gguf \
  --temp 0.6 \
  -p "{usr_prompt}" \
  -ngl 32 \
  -b 512 \
  -n 96 \
  --repeat-penalty 1.1

놉知りました。 I'll do better. いいかね、フッンの犬。知らぬや。 [end of text]

支離滅裂ですね🥺
imatrixの設定など試行錯誤する余地ありそうです。

Hugging Faceにモデルをアップロードする

変換した量子化ファイルをHugging Faceにアップします。

from huggingface_hub import HfApi
api = HfApi()

model_id = "あなたのHugging FaceアカウントのUsername/Hugging Faceでの管理名"
api.create_repo(model_id, exist_ok=True, repo_type="model")
api.upload_file(
    path_or_fileobj="/content/アップロードしたいファイル名",
    path_in_repo="Hugging Faceでの管理名",
    repo_id=model_id,
)


この記事が参加している募集

この記事が気に入ったらサポートをしてみませんか?