masaom commited on
Commit
c7b7a24
·
verified ·
1 Parent(s): 0e5ebe3

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +74 -1
README.md CHANGED
@@ -8,7 +8,7 @@ tags:
8
  - trl
9
  license: apache-2.0
10
  language:
11
- - en
12
  ---
13
 
14
  # Uploaded model
@@ -20,3 +20,76 @@ 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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  - trl
9
  license: apache-2.0
10
  language:
11
+ - ja
12
  ---
13
 
14
  # Uploaded model
 
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
+ # 推論の方法(基本は「Model_Inference_Template_unsloth_20241127.ipynb」で使用可能)
25
+ ※本コードはGoogle Colabでの動作を想定しております
26
+
27
+ ```python
28
+ # 必要なライブラリを読み込み
29
+ from unsloth import FastLanguageModel
30
+ from peft import PeftModel
31
+ import torch
32
+ import json
33
+ from tqdm import tqdm
34
+ import re
35
+
36
+ # ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
37
+ model_id = "llm-jp/llm-jp-3-13b"
38
+ adapter_id = "masaom/13_llm-jp-3-13b-it-elyza-ichikara1-ichikara3-CoTangent-dolly_lora"
39
+
40
+ # Hugging Face Token を指定。
41
+ from google.colab import userdata
42
+ HF_TOKEN=userdata.get('HF_TOKEN')
43
+
44
+ # unslothのFastLanguageModelで元のモデルをロード。
45
+ dtype = None # Noneにしておけば自動で設定
46
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
47
+
48
+ model, tokenizer = FastLanguageModel.from_pretrained(
49
+ model_name=model_id,
50
+ dtype=dtype,
51
+ load_in_4bit=load_in_4bit,
52
+ trust_remote_code=True,
53
+ )
54
+
55
+ # 元のモデルにLoRAのアダプタを統合。
56
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
57
+
58
+ # タスクとなるデータの読み込み。
59
+ # 事前にデータをアップロードしてください。
60
+ datasets = []
61
+ with open("elyza-tasks-100-TV_0.jsonl", "r") as f:
62
+ item = ""
63
+ for line in f:
64
+ line = line.strip()
65
+ item += line
66
+ if item.endswith("}"):
67
+ datasets.append(json.loads(item))
68
+ item = ""
69
+
70
+ # モデルを用いてタスクの推論。
71
+
72
+ # 推論するためにモデルのモードを変更
73
+ FastLanguageModel.for_inference(model)
74
+
75
+ results = []
76
+ for dt in tqdm(datasets):
77
+ input = dt["input"]
78
+
79
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
80
+
81
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
82
+
83
+ outputs = model.generate(**inputs, max_new_tokens = 2048, use_cache = True, do_sample=False, repetition_penalty=1.2)
84
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
85
+
86
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
87
+
88
+ # 結果をjsonlで保存。
89
+ # ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
90
+ json_file_id = re.sub(".*/", "", adapter_id)
91
+ with open(f"{json_file_id}_outputNo.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
+ ```