ikema commited on
Commit
7dc693c
·
verified ·
1 Parent(s): 06347ed

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +65 -0
README.md CHANGED
@@ -20,3 +20,68 @@ 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
+ # Usage
26
+
27
+ ```python
28
+ # ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
29
+ model_id = "llm-jp/llm-jp-3-13b"
30
+ adapter_id = "ikema/llm-jp-3-13b-it_24"
31
+
32
+
33
+ # unslothのFastLanguageModelで元のモデルをロード。
34
+ dtype = None # Noneにしておけば自動で設定
35
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
36
+
37
+ model, tokenizer = FastLanguageModel.from_pretrained(
38
+ model_name=model_id,
39
+ dtype=dtype,
40
+ load_in_4bit=load_in_4bit,
41
+ trust_remote_code=True,
42
+ )
43
+
44
+ # 元のモデルにLoRAのアダプタを統合。
45
+ model = PeftModel.from_pretrained(model, adapter_id)
46
+
47
+
48
+ # タスクとなるデータの読み込み。
49
+ # 事前にデータをアップロードしてください。
50
+ datasets = []
51
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
52
+ item = ""
53
+ for line in f:
54
+ line = line.strip()
55
+ item += line
56
+ if item.endswith("}"):
57
+ datasets.append(json.loads(item))
58
+ item = ""
59
+
60
+ # モデルを用いてタスクの推論。
61
+
62
+ # 推論するためにモデルのモードを変更
63
+ FastLanguageModel.for_inference(model)
64
+
65
+ results = []
66
+ for dt in tqdm(datasets):
67
+ input = dt["input"]
68
+
69
+ prompt = f"""### 指示\n{input}\n回答はなるべく短く答えを端的に出力してください。\n### 回答\n"""
70
+
71
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
72
+
73
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
74
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答\n')[-1]
75
+
76
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
77
+
78
+
79
+ # 結果をjsonlで保存。
80
+ # ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
81
+ json_file_id = re.sub(".*/", "", adapter_id)
82
+ with open(f"/content/{json_file_id}output_prompt.jsonl", 'w', encoding='utf-8') as f:
83
+ for result in results:
84
+ json.dump(result, f, ensure_ascii=False)
85
+ f.write('\n')
86
+
87
+ ```