yu3733 commited on
Commit
4c27529
·
verified ·
1 Parent(s): 90763e8

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +94 -0
README.md CHANGED
@@ -20,3 +20,97 @@ 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
+
26
+ !pip install -U bitsandbytes
27
+ !pip install -U transformers
28
+ !pip install -U accelerate
29
+ !pip install -U datasets
30
+
31
+ # notebookでインタラクティブな表示を可能とする(ただし、うまく動かない場合あり)
32
+ !pip install ipywidgets --upgrade
33
+
34
+
35
+
36
+ from transformers import (
37
+ AutoModelForCausalLM,
38
+ AutoTokenizer,
39
+ BitsAndBytesConfig,
40
+ )
41
+ import torch
42
+ from tqdm import tqdm
43
+ import json
44
+
45
+
46
+ # Hugging Faceで取得したTokenをこちらに貼る。
47
+ HF_TOKEN = ""
48
+
49
+
50
+ # QLoRA config
51
+ bnb_config = BitsAndBytesConfig(
52
+ load_in_4bit=True,
53
+ bnb_4bit_quant_type="nf4",
54
+ bnb_4bit_compute_dtype=torch.bfloat16,
55
+ bnb_4bit_use_double_quant=False,
56
+ )
57
+
58
+
59
+ # Load model
60
+ model = AutoModelForCausalLM.from_pretrained(
61
+ model_name,
62
+ quantization_config=bnb_config,
63
+ device_map="auto",
64
+ token = HF_TOKEN
65
+ )
66
+
67
+ # Load tokenizer
68
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, token = HF_TOKEN)
69
+
70
+
71
+
72
+ # データセットの読み込み。
73
+ # omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
74
+ datasets = []
75
+ with open("./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
+
84
+ # llmjp
85
+ results = []
86
+ for data in tqdm(datasets):
87
+
88
+ input = data["input"]
89
+
90
+ prompt = f"""### 指示
91
+ {input}
92
+ ### 回答:
93
+ """
94
+
95
+ tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
96
+ with torch.no_grad():
97
+ outputs = model.generate(
98
+ tokenized_input,
99
+ max_new_tokens=100,
100
+ do_sample=False,
101
+ repetition_penalty=1.2
102
+ )[0]
103
+ output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
104
+
105
+ results.append({"task_id": data["task_id"], "input": input, "output": output})
106
+
107
+
108
+ # こちらで生成されたjsolを提出してください。
109
+ # 本コードではinputとeval_aspectも含んでいますが、なくても問題ありません。
110
+ # 必須なのはtask_idとoutputとなります。
111
+ import re
112
+ model_name = re.sub(".*/", "", model_name)
113
+ with open(f"./{model_name}-outputs.jsonl", 'w', encoding='utf-8') as f:
114
+ for result in results:
115
+ json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
116
+ f.write('\n')