Views
No views yet
1# 必要なライブラリをインストール
2!pip install unsloth
3!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
4!pip install -U torch
5!pip install -U peft
6
7# 必要なライブラリを読み込み
8from unsloth import FastLanguageModel
9from peft import PeftModel
10import torch
11import json
12from tqdm import tqdm
13
14# ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
15model_id = "llm-jp/llm-jp-3-13b"
16adapter_id = "kittokito/llm-jp-3-13b-it-202412170007"
17
18from google.colab import userdata
19HF_TOKEN=userdata.get('HF_TOKEN')
20
21# unslothのFastLanguageModelで元のモデルをロード。
22dtype = None # Noneにしておけば自動で設定
23load_in_4bit = True # 4bit量子化でモデルのパラメーターをダウンロード
24
25model, tokenizer = FastLanguageModel.from_pretrained(
26 model_name=model_id,
27 dtype=dtype,
28 load_in_4bit=load_in_4bit,
29 trust_remote_code=True,
30)
31
32# 元のモデルにLoRAのアダプタを統合。
33model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
34
35# タスクとなるデータの読み込み。
36# 事前にデータをアップロードしてください。
37datasets = []
38with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
39 item = ""
40 for line in f:
41 line = line.strip()
42 item += line
43 if item.endswith("}"):
44 datasets.append(json.loads(item))
45 item = ""
46
47# モデルを用いてタスクの推論。
48
49# 推論するためにモデルのモードを変更
50FastLanguageModel.for_inference(model)
51
52results = []
53for dt in tqdm(datasets):
54 input = dt["input"]
55
56 prompt = f"""### 指示\n{input}\n### 回答\n"""
57
58 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
59
60 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
61 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
62
63 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
64
65# 結果をjsonlで保存。
66with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
67 for result in results:
68 json.dump(result, f, ensure_ascii=False)
69 f.write('\n')
70| Language | Dataset | Description |
|---|---|---|
| Japanese | ichikara-instruction-003-001-1.json | A manually constructed instruction dataset. |
| Elyza-tasks-100 | A model evaluation dataset consisting of 100 Japanese tasks that include complex instructions and advanced reasoning. | |
| Synthesized data from Elyza-tasks-100 | Synthesized data from Elyza-tasks-100 by using LLM (Mixtral-8x22B). |