sncffcns commited on
Commit
10478b0
·
verified ·
1 Parent(s): a8bf8c3

Update Readme.md

Browse files
Files changed (1) hide show
  1. README.md +66 -58
README.md CHANGED
@@ -22,80 +22,88 @@ language:
22
  base_model:
23
  - llm-jp/llm-jp-3-13b
24
  ---
25
- # 実行手順
26
- 以下の手順に従うことで、Hugging Face上のモデル(llm-jp/llm-jp-3-13b + /sncffcns/llm-jp-3-13b-it-20241127_lora)を用いて入力データ(elyza-tasks-100-TV_0.jsonl)を推論し、
27
- その結果を{adapter_id}-outputs.jsonlというファイルに出力することができる。
28
 
29
- # ライブラリのインストールを行う
30
- !pip install unsloth
31
- !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
32
- !pip install -U torch
33
- !pip install -U peft
 
 
34
 
35
- # 必要なライブラリの読み込みを行う
36
- from unsloth import FastLanguageModel
37
- from peft import PeftModel
38
- import torch
39
- import json
40
- from tqdm import tqdm
41
- import re
 
 
42
 
43
  # ベースとなるモデルと学習したLoRAのアダプタを設定する(Hugging FaceのIDを指定)。
44
- model_id = "llm-jp/llm-jp-3-13b"
45
- adapter_id = "sncffcns/llm-jp-3-13b-it-20241127_lora"
 
46
 
47
- # Hugging Face Token を指定する
48
- from google.colab import userdata
49
- HF_TOKEN = userdata.get('HF_TOKEN_WRITE')
50
 
51
- # unslothのFastLanguageModelで元のモデルをロード。
52
- dtype = None # Noneにしておけば自動で設定
53
- load_in_4bit = True # 今回は13Bモデルを扱うためTrue
54
 
55
- model, tokenizer = FastLanguageModel.from_pretrained(
56
- model_name=model_id,
57
- dtype=dtype,
58
- load_in_4bit=load_in_4bit,
59
- trust_remote_code=True,
60
- )
61
 
62
- # 元のモデルにLoRAのアダプタを統合する
63
- model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
 
 
 
 
64
 
65
- # タスクとなるデータの読み込み。
 
 
 
66
  # ./elyza-tasks-100-TV_0.jsonlというファイルからデータセットをロードする
67
- datasets = []
68
- with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
69
- item = ""
70
- for line in f:
71
- line = line.strip()
72
- item += line
73
- if item.endswith("}"):
74
- datasets.append(json.loads(item))
75
- item = ""
76
-
77
- # モデルを用いてタスクの推論を行う
78
- # 推論するためにモデルのモードを変更する
79
- FastLanguageModel.for_inference(model)
80
 
81
- results = []
82
- for dt in tqdm(datasets):
83
- input = dt["input"]
84
 
85
- prompt = f"""### 指示\n{input}\n### 回答\n"""
 
 
86
 
87
- inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
88
 
89
- outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
90
- prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
91
 
92
- results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
 
93
 
 
 
94
  # 結果をjsonlで保存する
95
  # adapter_idをベースにしたファイル名でJSONL形式の出力ファイルを保存する
96
- json_file_id = re.sub(".*/", "", adapter_id)
97
- with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
98
- for result in results:
99
- json.dump(result, f, ensure_ascii=False)
100
- f.write('\n')
 
 
101
  # 以上の手順で、{adapter_id}-outputs.jsonlというファイル名で推論結果が作成される
 
22
  base_model:
23
  - llm-jp/llm-jp-3-13b
24
  ---
25
+ ## 実行手順
26
+ 以下の手順に従うことで、Hugging Face上のモデル(llm-jp/llm-jp-3-13b + /sncffcns/llm-jp-3-13b-it-20241127_lora)を用いて入力データ(elyza-tasks-100-TV_0.jsonl)を推論し、その結果を{adapter_id}-outputs.jsonlというファイルに出力することができる。
 
27
 
28
+ ## ライブラリのインストールを行う
29
+ ```bash
30
+ !pip install unsloth
31
+ !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
32
+ !pip install -U torch
33
+ !pip install -U peft
34
+ ```
35
 
36
+ ## 必要なライブラリの読み込みを行う
37
+ ```python
38
+ from unsloth import FastLanguageModel
39
+ from peft import PeftModel
40
+ import torch
41
+ import json
42
+ from tqdm import tqdm
43
+ import re
44
+ ```
45
 
46
  # ベースとなるモデルと学習したLoRAのアダプタを設定する(Hugging FaceのIDを指定)。
47
+ ```python
48
+ model_id = "llm-jp/llm-jp-3-13b"
49
+ adapter_id = "sncffcns/llm-jp-3-13b-it-20241127_lora"
50
 
 
 
 
51
 
52
+ # Hugging Face Token を指定する
53
+ from google.colab import userdata
54
+ HF_TOKEN = userdata.get('HF_TOKEN_WRITE')
55
 
56
+ # unslothのFastLanguageModelで元のモデルをロード。
57
+ dtype = None # Noneにしておけば自動で設定
58
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
 
 
 
59
 
60
+ model, tokenizer = FastLanguageModel.from_pretrained(
61
+ model_name=model_id,
62
+ dtype=dtype,
63
+ load_in_4bit=load_in_4bit,
64
+ trust_remote_code=True,
65
+ )
66
 
67
+ # 元のモデルにLoRAのアダプタを統合する
68
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
69
+ ```
70
+ ## タスクとなるデータの読み込み。
71
  # ./elyza-tasks-100-TV_0.jsonlというファイルからデータセットをロードする
72
+ ```python
73
+ datasets = []
74
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
75
+ item = ""
76
+ for line in f:
77
+ line = line.strip()
78
+ item += line
79
+ if item.endswith("}"):
80
+ datasets.append(json.loads(item))
81
+ item = ""
 
 
 
82
 
83
+ # モデルを用いてタスクの推論を行う
84
+ # 推論するためにモデルのモードを変更する
85
+ FastLanguageModel.for_inference(model)
86
 
87
+ results = []
88
+ for dt in tqdm(datasets):
89
+ input = dt["input"]
90
 
91
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
92
 
93
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
 
94
 
95
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
96
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
97
 
98
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
99
+ ```
100
  # 結果をjsonlで保存する
101
  # adapter_idをベースにしたファイル名でJSONL形式の出力ファイルを保存する
102
+ ```python
103
+ json_file_id = re.sub(".*/", "", adapter_id)
104
+ with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
105
+ for result in results:
106
+ json.dump(result, f, ensure_ascii=False)
107
+ f.write('\n')
108
+ ```
109
  # 以上の手順で、{adapter_id}-outputs.jsonlというファイル名で推論結果が作成される