naomiyoshioka commited on
Commit
f42414f
·
verified ·
1 Parent(s): 559afdd

Upload README (2).md

Browse files
Files changed (1) hide show
  1. README (2).md +99 -0
README (2).md ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 必要なライブラリをインストール
2
+ !pip install unsloth
3
+ !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
4
+ !pip install -U torch
5
+ !pip install -U peft
6
+
7
+ # 必要なライブラリを読み込み
8
+ from unsloth import FastLanguageModel
9
+ from peft import PeftModel
10
+ import torch
11
+ import json
12
+ from tqdm import tqdm
13
+ import re
14
+
15
+ # ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
16
+ model_id = "llm-jp/llm-jp-3-13b"
17
+ adapter_id = "karaage0703/llm-jp-3-13b-it-20241205_018"
18
+
19
+ from google.colab import userdata
20
+ HF_TOKEN=userdata.get('HF_TOKEN')
21
+
22
+ # unslothのFastLanguageModelで元のモデルをロード。
23
+ dtype = None # Noneにしておけば自動で設定
24
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
25
+
26
+ model, tokenizer = FastLanguageModel.from_pretrained(
27
+ model_name=model_id,
28
+ dtype=dtype,
29
+ load_in_4bit=load_in_4bit,
30
+ trust_remote_code=True,
31
+ )
32
+
33
+ # 元のモデルにLoRAのアダプタを統合。
34
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
35
+
36
+ # タスクとなるデータの読み込み。
37
+ # 事前にデータをアップロードしてください。
38
+ datasets = []
39
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
40
+ item = ""
41
+ for line in f:
42
+ line = line.strip()
43
+ item += line
44
+ if item.endswith("}"):
45
+ datasets.append(json.loads(item))
46
+ item = ""
47
+
48
+ # モデルを用いてタスクの推論。
49
+
50
+ # 推論するためにモデルのモードを変更
51
+ FastLanguageModel.for_inference(model)
52
+
53
+ results = []
54
+ for dt in tqdm(datasets):
55
+ input = dt["input"]
56
+
57
+ prompt = f"""### 指示\n{input} 簡潔に回答してください \n### 回答\n"""
58
+
59
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
60
+
61
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
62
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
63
+ prediction = re.sub(r"[*#]", "", prediction)
64
+
65
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
66
+
67
+ # 結果をjsonlで保存。
68
+ json_file_id = re.sub(".*/", "", adapter_id)
69
+ with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
70
+ for result in results:
71
+ json.dump(result, f, ensure_ascii=False)
72
+ f.write('\n')
73
+
74
+
75
+
76
+
77
+
78
+ ---
79
+ base_model: llm-jp/llm-jp-3-13b
80
+ tags:
81
+ - text-generation-inference
82
+ - transformers
83
+ - unsloth
84
+ - llama
85
+ - trl
86
+ license: apache-2.0
87
+ language:
88
+ - en
89
+ ---
90
+
91
+ # Uploaded model
92
+
93
+ - **Developed by:** naomiyoshioka
94
+ - **License:** apache-2.0
95
+ - **Finetuned from model :** llm-jp/llm-jp-3-13b
96
+
97
+ This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
98
+
99
+ [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)