kmstat19 commited on
Commit
f3a853f
·
verified ·
1 Parent(s): 461b2e0

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +84 -0
README.md CHANGED
@@ -20,3 +20,87 @@ language:
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
+
26
+ 以下コードはunslothでELYZA-tasks-100-TVの出力を得るためのコードです:
27
+
28
+
29
+ ```python
30
+
31
+ # 必要なライブラリを読み込み
32
+ from unsloth import FastLanguageModel
33
+ from peft import PeftModel
34
+ import torch
35
+ import json
36
+ from tqdm import tqdm
37
+ import re
38
+
39
+ # ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
40
+ model_id = "llm-jp/llm-jp-3-13b"
41
+ adapter_id = "kmstat19/llm-jp-3-13b-it_lora"
42
+
43
+
44
+ # Hugging Face Token を指定。
45
+ # 下記の URL から Hugging Face Token を取得できますので下記の HF_TOKEN に入れてください。
46
+ # https://huggingface.co/settings/tokens
47
+ HF_TOKEN = "" #@param {type:"string"}
48
+
49
+
50
+ # unslothのFastLanguageModelで元のモデルをロード。
51
+ dtype = None # Noneにしておけば自動で設定
52
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
53
+
54
+ model, tokenizer = FastLanguageModel.from_pretrained(
55
+ model_name=model_id,
56
+ dtype=dtype,
57
+ load_in_4bit=load_in_4bit,
58
+ trust_remote_code=True,
59
+ )
60
+
61
+ # 元のモデルにLoRAのアダプタを統合。
62
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
63
+
64
+ # タスクとなるデータの読み込み。
65
+ # 事前にデータをアップロードしてください。
66
+ datasets = []
67
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
68
+ item = ""
69
+ for line in f:
70
+ line = line.strip()
71
+ item += line
72
+ if item.endswith("}"):
73
+ datasets.append(json.loads(item))
74
+ item = ""
75
+
76
+
77
+
78
+
79
+ # モデルを用いてタスクの推論。
80
+ # 推論するためにモデルのモードを変更
81
+ FastLanguageModel.for_inference(model)
82
+
83
+ results = []
84
+ for dt in tqdm(datasets):
85
+ input = dt["input"]
86
+
87
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
88
+
89
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
90
+
91
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
92
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
93
+
94
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
95
+
96
+
97
+
98
+ # 結果をjsonlで保存。
99
+ json_file_id = re.sub(".*/", "", adapter_id)
100
+ with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
101
+ for result in results:
102
+ json.dump(result, f, ensure_ascii=False)
103
+ f.write('\n')
104
+
105
+ ```
106
+