TakumiIwata commited on
Commit
19770c0
·
verified ·
1 Parent(s): 509f127

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +96 -4
README.md CHANGED
@@ -8,11 +8,9 @@ tags:
8
  - trl
9
  license: apache-2.0
10
  language:
11
- - en
12
  ---
13
-
14
- # Uploaded model
15
-
16
  - **Developed by:** TakumiIwata
17
  - **License:** apache-2.0
18
  - **Finetuned from model :** llm-jp/llm-jp-3-13b
@@ -21,3 +19,97 @@ language:
21
  This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
22
 
23
  [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  - trl
9
  license: apache-2.0
10
  language:
11
+ - ja
12
  ---
13
+ # モデル情報
 
 
14
  - **Developed by:** TakumiIwata
15
  - **License:** apache-2.0
16
  - **Finetuned from model :** llm-jp/llm-jp-3-13b
 
19
  This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
20
 
21
  [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
22
+
23
+ # 実行
24
+
25
+ - Colab環境での実行確認済み
26
+ - 本コードはunslothで学習したqLoRAのアダプタを用いてELYZA-tasks-100-TVの出力を得るためのコードです
27
+
28
+ ```
29
+ # 必要なライブラリをインストール
30
+ %%capture
31
+ !pip install unsloth
32
+ !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
33
+ !pip install -U torch
34
+ !pip install -U peft
35
+ ```
36
+
37
+ ```
38
+ # -*- coding: utf-8 -*-
39
+ """
40
+
41
+ # 推論用コード
42
+
43
+ このコードはunslothライブラリを用いてモデルを読み込み、推論するためのコードとなります。
44
+
45
+ """
46
+
47
+ # 必要なライブラリを読み込み
48
+ from unsloth import FastLanguageModel
49
+ from peft import PeftModel
50
+ import torch
51
+ import json
52
+ from tqdm import tqdm
53
+ import re
54
+
55
+ # ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
56
+ model_id = "llm-jp/llm-jp-3-13b"
57
+ adapter_id = "TakumiIwata/llm-jp-3-13b-it"
58
+ version = "bc52a00f4e0bf6b3b40867351594b2d69e8d2ac8"
59
+
60
+ # Hugging Face Token を指定。
61
+ # 下記の URL から Hugging Face Token を取得できますので下記の HF_TOKEN に入れてください。
62
+ # https://huggingface.co/settings/tokens
63
+ HF_TOKEN = "" #@param {type:"string"}
64
+
65
+ # unslothのFastLanguageModelで元のモデルをロード。
66
+ dtype = None # Noneにしておけば自動で設定
67
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
68
+
69
+ model, tokenizer = FastLanguageModel.from_pretrained(
70
+ model_name=model_id,
71
+ dtype=dtype,
72
+ load_in_4bit=load_in_4bit,
73
+ trust_remote_code=True,
74
+ )
75
+
76
+ # 元のモデルにLoRAのアダプタを統合。
77
+ model = PeftModel.from_pretrained(model, adapter_id, revision=version, token = HF_TOKEN)
78
+
79
+ # タスクとなるデータの読み込み。
80
+ # 事前にデータをアップロードしてください。
81
+ datasets = []
82
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
83
+ item = ""
84
+ for line in f:
85
+ line = line.strip()
86
+ item += line
87
+ if item.endswith("}"):
88
+ datasets.append(json.loads(item))
89
+ item = ""
90
+
91
+ # モデルを用いてタスクの推論。
92
+ # 推論するためにモデルのモードを変更
93
+ FastLanguageModel.for_inference(model)
94
+
95
+ results = []
96
+ for dt in tqdm(datasets):
97
+ input = dt["input"]
98
+
99
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
100
+
101
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
102
+
103
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
104
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
105
+
106
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
107
+
108
+ # 結果をjsonlで保存。
109
+ # ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
110
+ json_file_id = re.sub(".*/", "", adapter_id)
111
+ with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
112
+ for result in results:
113
+ json.dump(result, f, ensure_ascii=False)
114
+ f.write('\n')
115
+ ```