yutakasawai commited on
Commit
c29bd2f
1 Parent(s): 6eb6687

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +73 -0
README.md CHANGED
@@ -9,6 +9,10 @@ tags:
9
  license: apache-2.0
10
  language:
11
  - en
 
 
 
 
12
  ---
13
 
14
  # Uploaded model
@@ -20,3 +24,72 @@ 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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  license: apache-2.0
10
  language:
11
  - en
12
+ - ja
13
+ datasets:
14
+ - DeL-TaiseiOzaki/Tengentoppa-sft-v1.0
15
+ - elyza/ELYZA-tasks-100
16
  ---
17
 
18
  # Uploaded model
 
24
  This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
25
 
26
  [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
27
+
28
+ ```python
29
+ # 必要なライブラリを読み込み
30
+ from unsloth import FastLanguageModel
31
+ from peft import PeftModel
32
+ import torch
33
+ import json
34
+ from tqdm import tqdm
35
+ import re
36
+
37
+ model_id = "llm-jp/llm-jp-3-13b"
38
+ adapter_id = "yutakasawai/llm-jp-3-13b-it1214_V1_lora"
39
+
40
+ HF_TOKEN = "" #@param {type:"string"}
41
+
42
+ # unslothのFastLanguageModelで元のモデルをロード。
43
+ dtype = None # Noneにしておけば自動で設定
44
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
45
+
46
+ model, tokenizer = FastLanguageModel.from_pretrained(
47
+ model_name=model_id,
48
+ dtype=dtype,
49
+ load_in_4bit=load_in_4bit,
50
+ trust_remote_code=True,
51
+ )
52
+
53
+ # 元のモデルにLoRAのアダプタを統合。
54
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
55
+
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 = 2048, 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
+
87
+ # 結果をjsonlで保存。
88
+
89
+ # ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
90
+ json_file_id = re.sub(".*/", "", adapter_id)
91
+ with open(f"/content/{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
+ ```