|
--- |
|
base_model: llm-jp/llm-jp-3-13b |
|
tags: |
|
- text-generation-inference |
|
- transformers |
|
- unsloth |
|
- llama |
|
- trl |
|
language: |
|
- en |
|
--- |
|
|
|
# Uploaded model |
|
|
|
- **Developed by:** ShotaMatsumoto |
|
- **License:** CC BY-NC-SA |
|
- **Finetuned from model :** llm-jp/llm-jp-3-13b |
|
|
|
# 概要 |
|
「llm-jp/llm-jp-3-13bモデルにichikara-instructionデータを用いてファインチューニングを行ったモデルです。 |
|
松尾研究室の講義のコンペ用としてモデル作成しました。 |
|
|
|
https://weblab.t.u-tokyo.ac.jp/lecture/course-list/large-language-model/ |
|
# データセットは以下を使いました。 |
|
- ichikara-instruction-003-001-1.json |
|
- ichikara-instruction-003-001-2.1.json |
|
- ichikara-instruction-003-001-2.2.json |
|
- ichikara-instruction-003-001-5.1.json |
|
- ichikara-instruction-003-001-5.2.json |
|
- ichikara-instruction-003-003-1.json |
|
# 注意 |
|
ichikara-instructionデータは、CC BY-NC-SAライセンス(表示-非営利-継承)で公開されています。このライセンスの下では、非営利目的での利用が許可されていますが、商用利用は認められていません。 |
|
詳しくはこちらのホームページで確認してください。https://llm-jp.nii.ac.jp/blog/2024/04/30/v2.0-release.html |
|
|
|
# 推論方法 |
|
事前にadapter_model.safetensorsをダウンロードしてください。 |
|
```python |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
from peft import PeftModel |
|
import torch |
|
|
|
#ベースモデル ID とアダプタファイルパス |
|
base_model_id = "llm-jp/llm-jp-3-13b" |
|
adapter_model_path = ""/path/to/adapter_model.safetensors"" |
|
|
|
#デバイス設定 |
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
#トークナイザーとベースモデルのロード |
|
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True) |
|
base_model = AutoModelForCausalLM.from_pretrained(base_model_id, torch_dtype=torch.float16).to(device) |
|
|
|
#アダプタの読み込み |
|
model = PeftModel.from_pretrained(base_model, adapter_model_path).to(device) |
|
|
|
#推論関数 |
|
def generate_text(prompt, max_length=256, temperature=0.7): |
|
inputs = tokenizer(prompt, return_tensors="pt").to(device) |
|
outputs = model.generate( |
|
inputs["input_ids"], |
|
max_length=max_length, |
|
temperature=temperature, |
|
do_sample=True, |
|
top_k=50, |
|
top_p=0.9 |
|
) |
|
return tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
#テストプロンプト |
|
prompt = "日本の経済について説明してください。" |
|
print("Generating text...") |
|
generated_text = generate_text(prompt) |
|
print("\nGenerated Text:") |
|
print(generated_text) |
|
``` |
|
|
|
# 量子化 |
|
```python |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
from peft import PeftModel |
|
import torch |
|
from transformers import BitsAndBytesConfig |
|
|
|
#ベースモデル ID とアダプタファイルパス |
|
base_model_id = "llm-jp/llm-jp-3-13b" |
|
adapter_model_path = "path/to/" |
|
|
|
#デバイス設定 |
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
#量子化の設定 |
|
bnb_config = BitsAndBytesConfig( |
|
load_in_4bit=True, # 4-bit 量子化を有効化 |
|
bnb_4bit_use_double_quant=True, |
|
bnb_4bit_quant_type="nf4", # 量子化スキーム |
|
bnb_4bit_compute_dtype=torch.float16, # 推論時の計算精度 |
|
) |
|
|
|
#トークナイザーのロード |
|
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True) |
|
|
|
ベースモデルのロード(量子化設定を使用) |
|
base_model = AutoModelForCausalLM.from_pretrained( |
|
base_model_id, |
|
quantization_config=bnb_config, |
|
device_map="auto", # 自動的に GPU に割り当て |
|
) |
|
#アダプタの読み込み |
|
model = PeftModel.from_pretrained(base_model, adapter_model_path).to(device) |
|
|
|
`pad_token_id` の設定(トークナイザーから取得) |
|
model.config.pad_token_id = tokenizer.pad_token_id |
|
|
|
#推論関数 |
|
def generate_text(prompt, max_length=256, temperature=0.7): |
|
# トークナイズして `attention_mask` を設定し、max_length を適用 |
|
inputs = tokenizer( |
|
prompt, |
|
return_tensors="pt", |
|
padding=True, |
|
truncation=True, |
|
max_length=max_length # 最大トークン数を制限 |
|
).to(device) |
|
|
|
outputs = model.generate( |
|
inputs["input_ids"], |
|
attention_mask=inputs["attention_mask"], |
|
max_length=max_length, |
|
temperature=temperature, |
|
do_sample=True, |
|
top_k=50, |
|
top_p=0.9, |
|
pad_token_id=tokenizer.pad_token_id # 安全な動作のため明示的に指定 |
|
) |
|
return tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
#テストプロンプト |
|
prompt = "日本の経済について説明してください。" |
|
print("Generating text...") |
|
generated_text = generate_text(prompt, max_length=256) # 最大長さを明示的に指定 |
|
print("\nGenerated Text:") |
|
print(generated_text) |
|
``` |
|
|
|
# jsonlファイルの出力方法は以下の通りです。 |
|
```python |
|
import json |
|
from google.colab import files |
|
|
|
file_name = "elyza-tasks-results.jsonl" |
|
|
|
# JSON形式で保存 |
|
with open(file_name, "w", encoding="utf-8") as f: |
|
json.dump(results, f, ensure_ascii=False) |
|
|
|
# 内容確認 |
|
with open(file_name, 'w', encoding='utf-8') as f: |
|
for result in results: |
|
json.dump(result, f, ensure_ascii=False) |
|
f.write('\n') |
|
# ファイルのダウンロード |
|
files.download(file_name) |
|
``` |
|
This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library. |
|
|
|
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth) |