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# 必要なライブラリを読み込み
7from unsloth import FastLanguageModel
8from peft import PeftModel
9import torch
10import json
11from tqdm import tqdm
12import re
13# ベースとなるモデル。
14model_id = "llm-jp/llm-jp-3-13b"
15from google.colab import userdata
16HF_TOKEN=userdata.get('HF_TOKEN')
17# unslothのFastLanguageModelで元のモデルをロード。
18dtype = None # Noneにしておけば自動で設定
19load_in_4bit = True # 今回は13Bモデルを扱うためTrue
20model, tokenizer = FastLanguageModel.from_pretrained(
21 model_name=model_id,
22 dtype=dtype,
23 load_in_4bit=load_in_4bit,
24 trust_remote_code=True,
25)
26# SFT用のモデルを用意。
27model = FastLanguageModel.get_peft_model(
28 model,
29 r = 32,
30 target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
31 "gate_proj", "up_proj", "down_proj",],
32 lora_alpha = 32,
33 lora_dropout = 0.05,
34 bias = "none",
35 use_gradient_checkpointing = "unsloth",
36 random_state = 3407,
37 use_rslora = False,
38 loftq_config = None,
39 max_seq_length = max_seq_length,
40)
41# タスクとなるデータの読み込み。
42# 事前にデータをアップロードしてください。
43datasets = []
44with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
45 item = ""
46 for line in f:
47 line = line.strip()
48 item += line
49 if item.endswith("}"):
50 datasets.append(json.loads(item))
51 item = ""
52# モデルを用いてタスクの推論。
53# 推論するためにモデルのモードを変更
54FastLanguageModel.for_inference(model)
55results = []
56for dt in tqdm(datasets):
57 input = dt["input"]
58 prompt = f"""### 指示\n{input} 簡潔に回答してください \n### 回答\n"""
59 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
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 prediction = re.sub(r"[*#]", "", prediction)
63 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
64# 結果をjsonlで保存。
65json_file_id = re.sub(".*/", "", adapter_id)
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')| Language | Dataset | description |
|---|---|---|
| Japanese | ichikara-instruction-003-001-1.json | A manually constructed instruction dataset |
| Synthesized data from Elyza-tasks-100 | Synthesize data from Elyza-tasks-100 by using LLM(Tanuki-8x8B) |