0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🦙 Unslothで作成したLLaMA 3.2ベースのファインチューニングモデルを使った高速推論ガイド(GoogleColab📒ノートブック付)

Posted at

📦 必要なライブラリのインストール

%%capture
!pip install unsloth
# 最新のUnslothナイトリービルドを取得
!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"

解説:
Unslothライブラリをインストールします。このライブラリを使用することで、LLaMAモデルのファインチューニングと推論を大幅に高速化できます。ナイトリービルドを使用することで、最新の機能と改善が利用可能です。

🔧 ライブラリのインポートと基本設定

from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from unsloth import FastLanguageModel
from unsloth.chat_templates import get_chat_template
import torch

# モデルの基本設定
max_seq_length = 512
dtype = None
load_in_4bit = True
model_id = "path/to/your/finetuned-llama-3.2"  # ファインチューニング済みのモデルパス

解説:

  • 必要なライブラリをインポート
  • モデルは4ビット量子化を使用して、メモリ効率を改善
  • model_idには、Unslothでファインチューニングしたモデルのパスを指定

🚀 モデルとトークナイザーの初期化

# モデルとトークナイザーのロード
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name=model_id,
    dtype=dtype,
    load_in_4bit=load_in_4bit,
    trust_remote_code=True,
)

# LLaMA 3.1のチャットテンプレートを使用
tokenizer = get_chat_template(
    tokenizer,
    chat_template="llama-3.1",  # LLaMA 3.1のテンプレートで問題なし
)

# 高速推論モードを有効化
FastLanguageModel.for_inference(model)  # 通常の2倍の速度

解説:

  1. ファインチューニング済みのモデルをロード
  2. LLaMA 3.1のチャットテンプレートを適用(3.2でも互換性あり)
  3. Unslothの高速推論モードを有効化

💬 データセットを使用した推論の実装

def generate_response(dataset_entry):
    """
    データセットのエントリーに対して応答を生成する関数
    """
    # メッセージの作成
    messages = [
        {"role": "user", "content": dataset_entry["conversations"][0]['content']},
    ]
    
    # チャットテンプレートの適用
    inputs = tokenizer.apply_chat_template(
        messages,
        tokenize=True,
        add_generation_prompt=True,  # 生成プロンプトの追加
        return_tensors="pt",
    ).to(model.device)
    
    # 応答の生成
    outputs = model.generate(
        input_ids=inputs,
        max_new_tokens=64,  # 生成するトークン数
        use_cache=True,     # キャッシュを使用して高速化
        temperature=1.5,    # より創造的な応答を生成
        min_p=0.1          # 出力の多様性を確保
    )
    
    return tokenizer.batch_decode(outputs)

解説:
この関数は:

  1. データセットのエントリーからユーザーの入力を抽出
  2. LLaMA 3.1形式のチャットテンプレートを適用
  3. 以下のパラメータで応答を生成:
    • max_new_tokens: 64(短めの応答を生成)
    • temperature: 1.5(創造性を高める)
    • min_p: 0.1(多様な応答を確保)

✅ 実行例

if __name__ == "__main__":
    # テストデータセット
    dataset = [
        {"conversations": [{"content": "量子コンピュータについて簡単に説明してください。"}]},
        {"conversations": [{"content": "機械学習の基本的な概念を教えてください。"}]},
        {"conversations": [{"content": "プログラミング初心者へのアドバイスをお願いします。"}]}
    ]
    
    # 2番目のデータセットエントリーで試してみる
    response = generate_response(dataset[2])
    
    print("入力:", dataset[2]["conversations"][0]['content'])
    print("応答:", response)

解説:
サンプルの実行方法を示しています:

  • テスト用のデータセットを定義
  • 選択したエントリーで応答を生成
  • 入力と生成された応答を表示

このコードを使用することで、UnslothでファインチューニングしたカスタムのデータセットでトレーニングしたLLaMA 3.2モデルを、高速に推論できます。LLaMA 3.1のトークナイザーを使用することで、新しいモデルでも安定した出力が得られます。必要に応じて生成パラメータを調整することで、モデルの応答特性をカスタマイズできます。

Google Colab📒ノートブック

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?