eyepyon's picture
Upload README.md with huggingface_hub
a6c99cc verified
metadata
license: apache-2.0
base_model: google/gemma-2-9b-it
tags:
  - fine-tuned
  - gemma
  - lora
  - japanese
  - qa
library_name: transformers
pipeline_tag: text-generation

eyepyon/rcgemma2_9b_it-finetuned

このモデルは、google/gemma-2-9b-it をベースにLoRAでファインチューニングされたモデルです。

🔧 モデル情報

  • ベースモデル: google/gemma-2-9b-it
  • ファインチューニング手法: LoRA (Low-Rank Adaptation)
  • アテンション実装: eager (Gemma推奨)
  • 量子化: 4ビット (QLoRA)
  • 対応言語: 日本語
  • タスク: 質問応答

🚀 使用方法

from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel

# ベースモデルとトークナイザーを読み込み
base_model = AutoModelForCausalLM.from_pretrained(
    "google/gemma-2-9b-it",
    torch_dtype="auto",
    device_map="auto",
    attn_implementation="eager",  # Gemma推奨
    trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-9b-it")

# LoRAアダプターを適用
model = PeftModel.from_pretrained(base_model, "eyepyon/rcgemma2_9b_it-finetuned")

# 推論
def generate_response(context, question):
    input_text = f"### コンテキスト:\n{context}\n\n### 質問:\n{question}\n\n### 回答:\n"
    inputs = tokenizer(input_text, return_tensors="pt")
    
    # GPUに移動
    if torch.cuda.is_available():
        inputs = {k: v.to(model.device) for k, v in inputs.items()}
    
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_length=512,
            do_sample=True,
            temperature=0.7,
            top_p=0.9,
            pad_token_id=tokenizer.eos_token_id
        )
    
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    if "### 回答:" in response:
        response = response.split("### 回答:")[-1].strip()
    return response

# 使用例
context = "人工知能は機械学習技術を使用してデータから学習します。"
question = "機械学習の特徴は何ですか?"
answer = generate_response(context, question)
print(answer)

📊 入力フォーマット

### コンテキスト:
[背景情報やコンテキスト]

### 質問:
[ユーザーの質問]

### 回答:
[期待される回答]

⚙️ トレーニング設定

  • LoRAランク: 8
  • LoRA Alpha: 16
  • ターゲットモジュール: q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj
  • 学習率: 2e-4
  • バッチサイズ: 1 × 4 (gradient accumulation)
  • グラディエントチェックポイント: use_reentrant=False

⚠️ 重要な注意事項

Gemma特有の設定

  • eager attention必須: attn_implementation="eager"を使用してください
  • use_cache=False: グラディエントチェックポイントとの互換性のため
  • use_reentrant=False: 新しい推奨設定

制限事項

  • 主に日本語での質問応答に最適化
  • 生成される回答の事実確認が必要
  • 特定のドメイン知識でのファインチューニング

📄 ライセンス

Apache 2.0 License

🔗 関連リンク


最終更新: 2025年05月25日