ystknsh commited on
Commit
6c7dfb7
·
verified ·
1 Parent(s): a471ee7

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +84 -1
README.md CHANGED
@@ -19,4 +19,87 @@ language:
19
 
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
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
+ # How to useo on Google Colab
25
+ ```python
26
+ # 必要なライブラリをインストール
27
+ %%capture
28
+ !pip install unsloth
29
+ !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
30
+ !pip install -U torch
31
+ !pip install -U peft
32
+ # 必要なライブラリを読み込み
33
+ from unsloth import FastLanguageModel
34
+ from peft import PeftModel
35
+ import torch
36
+ import json
37
+ from tqdm import tqdm
38
+ import re
39
+ ```
40
+ ```python
41
+ # ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
42
+ model_id = "llm-jp/llm-jp-3-13b"
43
+ adapter_id = "ystknsh/llm-jp-3-13b-it-maxlen4096_lora_submit"
44
+
45
+ # Hugging Face Token を指定。
46
+ # 下記の URL から Hugging Face Token を取得できますので下記の HF_TOKEN に入れてください。
47
+ # https://huggingface.co/settings/tokens
48
+ import os
49
+ from google.colab import userdata
50
+ HF_TOKEN = userdata.get("HF_TOKEN")
51
+
52
+ # unslothのFastLanguageModelで元のモデルをロード。
53
+ dtype = None # Noneにしておけば自動で設定
54
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
55
+
56
+ model, tokenizer = FastLanguageModel.from_pretrained(
57
+ model_name=model_id,
58
+ dtype=dtype,
59
+ load_in_4bit=load_in_4bit,
60
+ trust_remote_code=True,
61
+ )
62
+
63
+ # 元のモデルにLoRAのアダプタを統合。
64
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
65
+
66
+ # タスクとなるデータの読み込み。
67
+ # 事前にデータをアップロードしてください。
68
+ datasets = []
69
+ with open("{YOUR INPUT FILE PATH}.jsonl", "r") as f:
70
+ item = ""
71
+ for line in f:
72
+ line = line.strip()
73
+ item += line
74
+ if item.endswith("}"):
75
+ datasets.append(json.loads(item))
76
+ item = ""
77
+
78
+ # モデルを用いてタスクの推論。
79
+
80
+ # 推論するためにモデルのモードを変更
81
+ FastLanguageModel.for_inference(model)
82
+
83
+ results = []
84
+ for dt in tqdm(datasets):
85
+ input = dt["input"]
86
+
87
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
88
+
89
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
90
+
91
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
92
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
93
+
94
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
95
+
96
+ # 結果をjsonlで保存。
97
+
98
+ # ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
99
+ json_file_id = re.sub(".*/", "",adapter_id)
100
+ with open(f"{YOUR OUTPUT FILE PATH}.jsonl", 'w', encoding='utf-8') as f:
101
+ for result in results:
102
+ json.dump(result, f, ensure_ascii=False)
103
+ f.write('\n')
104
+ ```
105
+