atsushi3110 commited on
Commit
b3cabe6
·
verified ·
1 Parent(s): edf8f1b

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +113 -0
README.md CHANGED
@@ -20,3 +20,116 @@ 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
+ # 推論に使用したcolab-notebook
25
+
26
+ https://colab.research.google.com/drive/195MaBVO4dsuO-iA3O7pjYoSUKOBvnQJ8#scrollTo=I5B5MOHuBy8b
27
+
28
+ # notebookをpythonに変換したコード
29
+ * HF_TOKENは削除している
30
+ *
31
+
32
+ ```python
33
+ # -*- coding: utf-8 -*-
34
+ """Model_Inference_Template_unsloth_20241127.ipynb
35
+
36
+ Automatically generated by Colab.
37
+
38
+ Original file is located at
39
+ https://colab.research.google.com/drive/195MaBVO4dsuO-iA3O7pjYoSUKOBvnQJ8
40
+
41
+ # 推論用コード
42
+ 本コードはunslothで学習したqLoRAのアダプタを用いてELYZA-tasks-100-TVの出力を得るためのコードです。
43
+ Hugging Faceにアダプタをアップロードしてあることが前提となります。
44
+ このコードはunslothライブラリを用いてモデルを読み込み、推論するためのコードとなります。
45
+ このコードで生成されたjsonlファイルは課題の成果として提出可能なフォーマットになっております。
46
+
47
+ ※本コードはGoogle Colabでの動作を想定しており、Omnicampusでの動作を想定しておりません。
48
+ Omnicampus向けのコードは別途用意しております。
49
+ """
50
+
51
+ from google.colab import drive
52
+ drive.mount('/content/drive')
53
+
54
+ # Commented out IPython magic to ensure Python compatibility.
55
+ # # 必要なライブラリをインストール
56
+ # %%capture
57
+ # !pip install unsloth
58
+ # !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
59
+ # !pip install -U torch
60
+ # !pip install -U peft
61
+
62
+ # 必要なライブラリを読み込み
63
+ from unsloth import FastLanguageModel
64
+ from peft import PeftModel
65
+ import torch
66
+ import json
67
+ from tqdm import tqdm
68
+ import re
69
+
70
+ !ls /content/drive/MyDrive/elyza-tasks-100-TV_0.jsonl
71
+
72
+ # ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
73
+ model_id = "llm-jp/llm-jp-3-13b"
74
+ adapter_id = "atsushi3110/ljp-13b-it"
75
+
76
+ # Hugging Face Token を指定。
77
+ # 下記の URL から Hugging Face Token を取得できますので下記の HF_TOKEN に入れてください。
78
+ # https://huggingface.co/settings/tokens
79
+ HF_TOKEN = "" #@param {type:"string"}
80
+
81
+ # unslothのFastLanguageModelで元のモデルをロード。
82
+ dtype = None # Noneにしておけば自動で設定
83
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
84
+
85
+ model, tokenizer = FastLanguageModel.from_pretrained(
86
+ model_name=model_id,
87
+ dtype=dtype,
88
+ load_in_4bit=load_in_4bit,
89
+ trust_remote_code=True,
90
+ )
91
+
92
+ # 元のモデルにLoRAのアダプタを統合。
93
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
94
+
95
+ # タスクとなるデータの読み込み。
96
+ # 事前にデータをアップロードしてください。
97
+ datasets = []
98
+ with open("./drive/MyDrive/lecture2024/elyza-tasks-100-TV_0.jsonl", "r") as f:
99
+ item = ""
100
+ for line in f:
101
+ line = line.strip()
102
+ item += line
103
+ if item.endswith("}"):
104
+ datasets.append(json.loads(item))
105
+ item = ""
106
+
107
+ # モデルを用いてタスクの推論。
108
+
109
+ # 推論するためにモデルのモードを変更
110
+ FastLanguageModel.for_inference(model)
111
+
112
+ results = []
113
+ for dt in tqdm(datasets):
114
+ input = dt["input"]
115
+
116
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
117
+
118
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
119
+
120
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
121
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
122
+
123
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
124
+
125
+ # 結果をjsonlで保存。
126
+
127
+ # ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
128
+ json_file_id = re.sub(".*/", "", adapter_id)
129
+ with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
130
+ for result in results:
131
+ json.dump(result, f, ensure_ascii=False)
132
+ f.write('\n')
133
+
134
+ ```
135
+