horie1983 commited on
Commit
0b4bc2a
·
verified ·
1 Parent(s): e15fc05
Files changed (1) hide show
  1. README.md +86 -0
README.md CHANGED
@@ -20,3 +20,89 @@ 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
+ ```python
25
+
26
+ from transformers import (
27
+ AutoModelForCausalLM,
28
+ AutoTokenizer,
29
+ BitsAndBytesConfig,
30
+ )
31
+ from peft import PeftModel
32
+ import torch
33
+ from tqdm import tqdm
34
+ import json
35
+
36
+ HF_TOKEN = "myToken"
37
+
38
+ model_id = "models/models--llm-jp--llm-jp-3-13b/snapshots/cd3823f4c1fcbb0ad2e2af46036ab1b0ca13192a"
39
+ adapter_id = "horie1983/llm-jp-3-13b-it_lora"
40
+
41
+ # QLoRA config
42
+ bnb_config = BitsAndBytesConfig(
43
+ load_in_4bit=True,
44
+ bnb_4bit_quant_type="nf4",
45
+ bnb_4bit_compute_dtype=torch.bfloat16,
46
+ )
47
+
48
+ # Load model
49
+ model = AutoModelForCausalLM.from_pretrained(
50
+ model_id,
51
+ quantization_config=bnb_config,
52
+ device_map="auto",
53
+ token = HF_TOKEN
54
+ )
55
+
56
+ # Load tokenizer
57
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True, token = HF_TOKEN)
58
+
59
+ # 元のモデルにLoRAのアダプタを統合。
60
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
61
+
62
+ # データセットの読み込み。
63
+ # omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
64
+ datasets = []
65
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
66
+ item = ""
67
+ for line in f:
68
+ line = line.strip()
69
+ item += line
70
+ if item.endswith("}"):
71
+ datasets.append(json.loads(item))
72
+ item = ""
73
+
74
+ # llmjp
75
+ results = []
76
+ for data in tqdm(datasets):
77
+
78
+ input = data["input"]
79
+
80
+ prompt = f"""### 指示
81
+ {input}
82
+ ### 回答
83
+ """
84
+
85
+ tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
86
+ attention_mask = torch.ones_like(tokenized_input)
87
+ with torch.no_grad():
88
+ outputs = model.generate(
89
+ tokenized_input,
90
+ attention_mask=attention_mask,
91
+ max_new_tokens=100,
92
+ do_sample=False,
93
+ repetition_penalty=1.2,
94
+ pad_token_id=tokenizer.eos_token_id
95
+ )[0]
96
+ output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
97
+
98
+ results.append({"task_id": data["task_id"], "input": input, "output": output})
99
+
100
+
101
+ import re
102
+ jsonl_id = re.sub(".*/", "", adapter_id)
103
+ with open(f"./{jsonl_id}-outputs.jsonl", 'w', encoding='utf-8') as f:
104
+ for result in results:
105
+ json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
106
+ f.write('\n')
107
+
108
+ ```