thash-ai commited on
Commit
e85cf67
·
verified ·
1 Parent(s): f118c5a

Update README

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
+ ## ELYZA-tasks-100-TV コンペ用推論コード
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
+ # ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
35
+ model_id = "llm-jp/llm-jp-3-13b"
36
+ adapter_id = "thash-ai/llm-jp-3-13b-it-v5_lora"
37
+
38
+ # Hugging Face Token を指定。
39
+ # 下記の URL から Hugging Face Token を取得できますので下記の HF_TOKEN に入れてください。
40
+ # https://huggingface.co/settings/tokens
41
+ HF_TOKEN = "xxx"
42
+
43
+ # unslothのFastLanguageModelで元のモデルをロード。
44
+ dtype = None # Noneにしておけば自動で設定
45
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
46
+
47
+ model, tokenizer = FastLanguageModel.from_pretrained(
48
+ model_name=model_id,
49
+ dtype=dtype,
50
+ load_in_4bit=load_in_4bit,
51
+ trust_remote_code=True,
52
+ )
53
+
54
+ # 元のモデルにLoRAのアダプタを統合。
55
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
56
+
57
+ # タスクとなるデータの読み込み。
58
+ # 事前にデータをアップロードしてください。
59
+ datasets = []
60
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
61
+ item = ""
62
+ for line in f:
63
+ line = line.strip()
64
+ item += line
65
+ if item.endswith("}"):
66
+ datasets.append(json.loads(item))
67
+ item = ""
68
+
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### 回答\n')[-1]
84
+
85
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
86
+
87
+ # 結果をjsonlで保存。
88
+
89
+ # ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
90
+ json_file_id = re.sub(".*/", "", adapter_id)
91
+ with open(f"{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
92
+ for result in results:
93
+ json.dump(result, f, ensure_ascii=False)
94
+ f.write('\n')
95
+ ```