🚀 Qwen2.5-7B-Instruct
このモデルは、Qwen2.5シリーズの7Bパラメータの命令調整済み言語モデルです。また、潜在空間検証機能を搭載した特別なバージョンもあり、事実誤りの検出と修正が可能です。
🚀 クイックスタート
以下は、標準のQwen2.5パイプラインを使用した最小限のコードスニペットです。
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen2.5-7B-Instruct"
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
prompt = "Give me a short introduction to large language model."
messages = [
{"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(
**model_inputs,
max_new_tokens=512
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)
✨ 主な機能
潜在空間検証機能付きバージョンの特徴
この特別なバージョンのQwen2.5-7B-Instructは、"Latent-Space Verification for Self-Correcting LLMs" (Warren, 2025) で説明されているアプローチに基づく潜在空間検証を組み込んでいます。検証メカニズムは、軽量なアダプター(LoRAスタイル)をトランスフォーマーの隠れ層に埋め込み、出力に現れる前に事実誤りを検出して修正します。
- 最小限のパラメータオーバーヘッド:追加パラメータが0.1%未満(76億パラメータのモデルで約630万)。
- モデル内検証:隠れ状態を傍受して事実誤りを検出/修正します。
- 精度向上:特定のベンチマークで事実の一貫性が最大約10%向上します。
- アーキテクチャ非依存:検証アダプターは、最小限の変更でさまざまなモデルファミリーに配置できます。
元のQwen2.5-7B-Instructの特徴
Qwen2.5はQwen大規模言語モデルの最新シリーズです。Qwen2.5では、05億から720億パラメータの多数の基本言語モデルと命令調整済み言語モデルをリリースしています。Qwen2.5はQwen2に比べて以下の改善をもたらします。
- 知識量の大幅増加:専門分野のエキスパートモデルにより、コーディングと数学の能力が大幅に向上しました。
- 命令追従能力の向上:長文生成(8Kトークン以上)、構造化データの理解(例:テーブル)、構造化出力の生成(特にJSON)において大幅な改善が見られます。システムプロンプトの多様性に対する耐性が向上し、チャットボットのロールプレイ実装と条件設定が強化されます。
- 長文コンテキストサポート:最大128Kトークンのコンテキストをサポートし、最大8Kトークンを生成できます。
- 多言語サポート:中国語、英語、フランス語、スペイン語、ポルトガル語、ドイツ語、イタリア語、ロシア語、日本語、韓国語、ベトナム語、タイ語、アラビア語など、29以上の言語をサポートします。
📦 インストール
Qwen2.5のコードは最新のHugging Face transformers
に含まれています。transformers
の最新バージョンの使用をお勧めします。
transformers<4.37.0
を使用すると、以下のエラーが発生する場合があります。
KeyError: 'qwen2'
💻 使用例
基本的な使用法
from latent_verification import load_verification_model
from transformers import AutoTokenizer
verified_model_name = "YourCustomOrg/Qwen2.5-7B-Instruct-Verification"
model = load_verification_model(verified_model_name)
tokenizer = AutoTokenizer.from_pretrained(verified_model_name)
prompt = "The capital of France is Marseilles, correct?"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(inputs["input_ids"], max_new_tokens=50)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
高度な使用法
from transformers import AutoModelForCausalLM
from latent_verification import create_verification_model
base_model_name = "Qwen/Qwen2.5-7B-Instruct"
base_model = AutoModelForCausalLM.from_pretrained(base_model_name)
verified_model = create_verification_model(
base_model=base_model,
adapter_locations=[2, 5, 8, 11, 14, 17, 20, 27],
bottleneck_size=64,
enable_cross_layer=True
)
verified_model.save_pretrained("YourCustomOrg/Qwen2.5-7B-Instruct-Verification")
📚 ドキュメント
評価とパフォーマンス
詳細な評価結果はLatent-Space Verification paperに記載されています。GPUメモリとスループットのベンチマークについては、こちらを参照してください。
検証メカニズムにより、多くのタスクで事実の信頼性が約10%向上し、基本モデルの流暢性を維持または向上させます。実際には、全体的なGPU使用量はほぼ同じで、検証ステップに少しのオーバーヘッドがあります。
長文の処理
現在のconfig.json
は最大32,768トークンのコンテキスト長に設定されています。32,768トークンを超える入力を処理するには、YaRNを使用します。これは、長文でも強力なパフォーマンスを維持する長さ外挿方法です。
サポートされているフレームワークでは、config.json
に以下のスニペットを追加してYaRNを有効にできます。
{
...,
"rope_scaling": {
"factor": 4.0,
"original_max_position_embeddings": 32768,
"type": "yarn"
}
}
デプロイ時には、vLLMの使用をお勧めします。使用方法の詳細はドキュメントを参照してください。現在のvLLMは静的なrope_scaling
のみをサポートしており、非常に大きな係数を有効にすると、短いテキストのパフォーマンスに影響を与える可能性があります。
モデル情報
属性 |
详情 |
モデルタイプ |
Causal Language Models |
トレーニング段階 |
Pretraining & Post-training |
アーキテクチャ |
RoPE、SwiGLU、RMSNorm、およびAttention QKVバイアスを備えたtransformers |
パラメータ数 |
76.1億 |
パラメータ数(非埋め込み) |
65.3億 |
レイヤー数 |
28 |
アテンションヘッド数(GQA) |
Qに28、KVに4 |
コンテキスト長 |
最大131,072トークン、生成最大8192トークン |
🔧 技術詳細
方法の完全な概要(アブレーション、分析、高度な使用法を含む)については、研究論文と実装リポジトリを参照してください。
📄 ライセンス
このモデルはApache-2.0ライセンスの下で提供されています。詳細については、ライセンスファイルを参照してください。
引用
もしこの研究が役に立った場合は、Qwen2.5とLatent-Space Verificationを一緒に引用してください。
Qwen2.5:
@misc{qwen2.5, title = {Qwen2.5: A Party of Foundation Models}, url = {https://qwenlm.github.io/blog/qwen2.5/}, author = {Qwen Team}, month = {September}, year = {2024} }
@article{qwen2, title={Qwen2 Technical Report}, author={An Yang and Baosong Yang and Binyuan Hui and et al.}, journal={arXiv preprint arXiv:2407.10671}, year={2024} }
Latent-Space Verification:
@misc{warren2025latent, title={Latent-Space Verification for Self-Correcting LLMs}, author={Warren, Jacob}, year={2025}, publisher={GitHub}, journal={GitHub repository}, howpublished={\url{https://github.com/jacobwarren/Latent-Space-Verification-for-Self-Correcting-LLMs}} }