holy0516 commited on
Commit
6c3ecdb
·
verified ·
1 Parent(s): dce7bbf

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +81 -0
README.md CHANGED
@@ -20,3 +20,84 @@ 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
+ # 出力までの流れ
25
+ - 1. 必要なライブラリのインストール・インポート
26
+ 2. ベースモデルの読み込みとLoRAアダプタの指定
27
+ 3. Hugging Face Tokenの指定
28
+ 4. 元モデルのロード
29
+ 5. LoRAアダプタの結合
30
+ 6. タスクの読み込み
31
+ 7. 推論
32
+ 8. 出力
33
+
34
+ # コード
35
+ ## 1. 必要なライブラリのインストール・インポート
36
+
37
+ !pip uninstall unsloth -y
38
+ !pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
39
+ !pip install --upgrade torch
40
+ !pip install --upgrade xformers
41
+
42
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
43
+ from unsloth import FastLanguageModel
44
+ import torch
45
+ from tqdm import tqdm
46
+ import json
47
+
48
+ ## 2. ベースモデルの読み込みとLoRAアダプタの指定
49
+
50
+ model_id = "llm-jp/llm-jp-3-13b"
51
+ adapter_id = "holy0516/llm-jp-3-13b-it-r1_elyza100-r4"
52
+
53
+ ## 3. Hugging Face Tokenの指定
54
+ (略)
55
+
56
+ ## 4. 元モデルのロード
57
+
58
+ dtype = None # Noneにしておけば自動で設定
59
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
60
+
61
+ model, tokenizer = FastLanguageModel.from_pretrained(
62
+ model_name=model_id,
63
+ dtype=dtype,
64
+ load_in_4bit=load_in_4bit,
65
+ trust_remote_code=True,
66
+ )
67
+
68
+ # 5. LoRAのアダプタの統合
69
+
70
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
71
+
72
+ # 6. タスクの読み込み
73
+
74
+ datasets = []
75
+ with open("/content/elyza-tasks-100-TV_0.jsonl", "r") as f:
76
+ item = ""
77
+ for line in f:
78
+ line = line.strip()
79
+ item += line
80
+ if item.endswith("}"):
81
+ datasets.append(json.loads(item))
82
+ item = ""
83
+ # 7. 推論
84
+
85
+ FastLanguageModel.for_inference(model)
86
+ results = []
87
+ for dt in tqdm(datasets):
88
+ input = dt["input"]
89
+
90
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
91
+
92
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
93
+
94
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
95
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
96
+
97
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
98
+
99
+ # 8. 出力結果の保存
100
+ with open(f"output.jsonl", 'w', encoding='utf-8') as f:
101
+ for result in results:
102
+ json.dump(result, f, ensure_ascii=False)
103
+ f.write('\n')