TOSHImommy commited on
Commit
809d782
·
verified ·
1 Parent(s): e5d137e

Update README.md

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