Seigo55 commited on
Commit
79d7706
·
verified ·
1 Parent(s): 1ed5016

Update Read Me.

Browse files
Files changed (1) hide show
  1. README.md +71 -0
README.md CHANGED
@@ -20,3 +20,74 @@ tags:
20
  This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
21
 
22
  [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
21
 
22
  [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
23
+
24
+ # Sample Use
25
+ 本コードはunslothで学習したqLoRAのアダプタを用いてELYZA-tasks-100-TVの出力を得るためのコードです。
26
+ # 必要なライブラリをインストール
27
+ %%capture
28
+ !pip install unsloth
29
+ !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
30
+ !pip install -U torch
31
+ !pip install -U peft
32
+ # 必要なライブラリを読み込み
33
+ from unsloth import FastLanguageModel
34
+ from peft import PeftModel
35
+ import torch
36
+ import json
37
+ from tqdm import tqdm
38
+ import re
39
+ # ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
40
+ model_id = "llm-jp/llm-jp-3-13b"
41
+ adapter_id = "Seigo55/llm-jp-3-13b-finetune-2-LoRA"
42
+ # Hugging Face Token を指定。
43
+ # 下記の URL から Hugging Face Token を取得できますので下記の HF_TOKEN に入れてください。
44
+ # https://huggingface.co/settings/tokens
45
+ HF_TOKEN = "<Your Token>" #@param {type:"string"}
46
+ # unslothのFastLanguageModelで元のモデルをロード。
47
+ dtype = None # Noneにしておけば自動で設定
48
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
49
+
50
+ model, tokenizer = FastLanguageModel.from_pretrained(
51
+ model_name=model_id,
52
+ dtype=dtype,
53
+ load_in_4bit=load_in_4bit,
54
+ trust_remote_code=True,
55
+ )
56
+ # 元のモデルにLoRAのアダプタを統合。
57
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
58
+ # タスクとなるデータの読み込み。
59
+ # 事前にデータをアップロードしてください。
60
+ datasets = []
61
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
62
+ item = ""
63
+ for line in f:
64
+ line = line.strip()
65
+ item += line
66
+ if item.endswith("}"):
67
+ datasets.append(json.loads(item))
68
+ item = ""
69
+ # モデルを用いてタスクの推論。
70
+
71
+ # 推論するためにモデルのモードを変更
72
+ FastLanguageModel.for_inference(model)
73
+
74
+ results = []
75
+ for dt in tqdm(datasets):
76
+ input = dt["input"]
77
+
78
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
79
+
80
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
81
+
82
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
83
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
84
+
85
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
86
+ # 結果をjsonlで保存。
87
+
88
+ # ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
89
+ json_file_id = re.sub(".*/", "", adapter_id)
90
+ with open(f"./content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
91
+ for result in results:
92
+ json.dump(result, f, ensure_ascii=False)
93
+ f.write('\n')