Chasottco commited on
Commit
363e709
1 Parent(s): c841151

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +70 -0
README.md CHANGED
@@ -20,3 +20,73 @@ 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
+ #Google Colabでの動作を想定
26
+
27
+ # 必要なライブラリをインストール
28
+ %%capture
29
+ !pip install unsloth
30
+ !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
31
+ !pip install -U torch
32
+ !pip install -U peft
33
+
34
+ # 必要なライブラリを読み込み
35
+ from unsloth import FastLanguageModel
36
+ from peft import PeftModel
37
+ import torch
38
+ import json
39
+ from tqdm import tqdm
40
+ import re
41
+
42
+ # ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)
43
+ model_id = "llm-jp/llm-jp-3-13b"
44
+ adapter_id = "Chasottco/llm-jp-3-13b-it-Chasottco"
45
+
46
+ # Hugging Face Token を指定
47
+ HF_TOKEN = ""
48
+
49
+ # unslothのFastLanguageModelで元のモデルをロード。
50
+ dtype = None # Noneにしておけば自動で設定
51
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
52
+
53
+ model, tokenizer = FastLanguageModel.from_pretrained(
54
+ model_name=model_id,
55
+ dtype=dtype,
56
+ load_in_4bit=load_in_4bit,
57
+ trust_remote_code=True,
58
+ )
59
+
60
+ # 元のモデルにLoRAのアダプタを統合。
61
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
62
+
63
+ # google drive mount(事前にデータをアップロード)
64
+ from google.colab import drive
65
+ drive.mount('/content/drive')
66
+
67
+ # タスクとなるデータの読み込み。
68
+ datasets = []
69
+ with open("/content/drive/MyDrive/2024松尾研LLM/elyza-tasks-100-TV_0.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
+ 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})