suzuna0831 commited on
Commit
e818971
·
verified ·
1 Parent(s): e10ba69

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +73 -0
README.md CHANGED
@@ -20,3 +20,76 @@ 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
+ # Usage
25
+
26
+ ```python
27
+ from unsloth import FastLanguageModel
28
+ from peft import PeftModel
29
+ import torch
30
+ import json
31
+ from tqdm import tqdm
32
+ import re
33
+
34
+ HF_TOKEN = your_huggingface_token
35
+
36
+ model_id = "llm-jp/llm-jp-3-13b"
37
+ adapter_id = "suzuna0831/llm-jp-3-13b-it_lora"
38
+
39
+
40
+ # unslothのFastLanguageModelで元のモデルをロード。
41
+ dtype = None # Noneにしておけば自動で設定
42
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
43
+
44
+ model, tokenizer = FastLanguageModel.from_pretrained(
45
+ model_name=model_id,
46
+ dtype=dtype,
47
+ load_in_4bit=load_in_4bit,
48
+ trust_remote_code=True,
49
+ )
50
+
51
+ # 元のモデルにLoRAのアダプタを統合。
52
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
53
+
54
+ # タスクとなるデータの読み込み。
55
+ # 事前にデータをアップロードしてください。
56
+ datasets = []
57
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
58
+ item = ""
59
+ for line in f:
60
+ line = line.strip()
61
+ item += line
62
+ if item.endswith("}"):
63
+ datasets.append(json.loads(item))
64
+ item = ""
65
+
66
+ # モデルを用いてタスクの推論。
67
+
68
+ # 推論するためにモデルのモードを変更
69
+ FastLanguageModel.for_inference(model)
70
+
71
+ results = []
72
+ for dt in tqdm(datasets):
73
+ input = dt["input"]
74
+
75
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
76
+
77
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
78
+
79
+ outputs = model.generate(**inputs, max_new_tokens = 2048, use_cache = True, do_sample=False, repetition_penalty=1.2)
80
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
81
+
82
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
83
+
84
+ # 結果をjsonlで保存。
85
+
86
+ # ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
87
+ json_file_id = re.sub(".*/", "", adapter_id)
88
+ with open(f"{output_dir}/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
89
+ for result in results:
90
+ json.dump(result, f, ensure_ascii=False)
91
+ f.write('\n')
92
+ ```
93
+
94
+
95
+