python
Browse files
README.md
CHANGED
@@ -13,6 +13,7 @@ base_model:
|
|
13 |
## Uses
|
14 |
以下のコードで40分ほどでElyza-tasks-TV-100の推論が終了します。
|
15 |
|
|
|
16 |
#推論時のコード
|
17 |
|
18 |
!pip install -U bitsandbytes
|
@@ -46,63 +47,64 @@ bnb_config = BitsAndBytesConfig(
|
|
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 |
-
# 元のモデルにLoRAのアダプタを統合。
|
59 |
-
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
60 |
-
model.eval()
|
61 |
-
|
62 |
-
datasets = []
|
63 |
-
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
64 |
-
item = ""
|
65 |
-
for line in f:
|
66 |
-
line = line.strip()
|
67 |
-
item += line
|
68 |
-
if item.endswith("}"):
|
69 |
-
datasets.append(json.loads(item))
|
70 |
-
item = ""
|
71 |
-
|
72 |
-
|
73 |
-
results = []
|
74 |
-
for data in tqdm(datasets):
|
75 |
-
|
76 |
-
input = data["input"]
|
77 |
-
|
78 |
-
prompt = f"""### 指示
|
79 |
-
{input}
|
80 |
-
### 回答
|
81 |
-
"""
|
82 |
-
|
83 |
-
tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
|
84 |
-
attention_mask = torch.ones_like(tokenized_input)
|
85 |
-
with torch.no_grad():
|
86 |
-
outputs = model.generate(
|
87 |
-
tokenized_input,
|
88 |
-
attention_mask=attention_mask,
|
89 |
-
max_new_tokens=512,
|
90 |
-
do_sample=False,
|
91 |
-
repetition_penalty=1.2,
|
92 |
-
pad_token_id=tokenizer.eos_token_id
|
93 |
-
)[0]
|
94 |
-
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
|
95 |
-
|
96 |
-
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
97 |
-
|
98 |
-
import re
|
99 |
-
jsonl_id = re.sub(".*/", "", adapter_id)
|
100 |
-
with open(f"./{jsonl_id}-outputs.jsonl", 'w', encoding='utf-8') as f:
|
101 |
-
for result in results:
|
102 |
-
json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
|
103 |
-
f.write('\n')
|
104 |
|
105 |
-
#以上でjsonlファイルを得る。
|
|
|
106 |
|
107 |
### Training Data
|
108 |
|
|
|
13 |
## Uses
|
14 |
以下のコードで40分ほどでElyza-tasks-TV-100の推論が終了します。
|
15 |
|
16 |
+
```python:inference.py
|
17 |
#推論時のコード
|
18 |
|
19 |
!pip install -U bitsandbytes
|
|
|
47 |
)
|
48 |
|
49 |
# Load model
|
50 |
+
model = AutoModelForCausalLM.from_pretrained(
|
51 |
+
model_id,
|
52 |
+
quantization_config=bnb_config,
|
53 |
+
device_map="auto",
|
54 |
+
token = HF_TOKEN
|
55 |
+
)
|
56 |
|
57 |
# Load tokenizer
|
58 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True, token = HF_TOKEN)
|
59 |
+
# 元のモデルにLoRAのアダプタを統合。
|
60 |
+
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
61 |
+
model.eval()
|
62 |
+
|
63 |
+
datasets = []
|
64 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
65 |
+
item = ""
|
66 |
+
for line in f:
|
67 |
+
line = line.strip()
|
68 |
+
item += line
|
69 |
+
if item.endswith("}"):
|
70 |
+
datasets.append(json.loads(item))
|
71 |
+
item = ""
|
72 |
+
|
73 |
+
|
74 |
+
results = []
|
75 |
+
for data in tqdm(datasets):
|
76 |
+
|
77 |
+
input = data["input"]
|
78 |
+
|
79 |
+
prompt = f"""### 指示
|
80 |
+
{input}
|
81 |
+
### 回答
|
82 |
+
"""
|
83 |
+
|
84 |
+
tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
|
85 |
+
attention_mask = torch.ones_like(tokenized_input)
|
86 |
+
with torch.no_grad():
|
87 |
+
outputs = model.generate(
|
88 |
+
tokenized_input,
|
89 |
+
attention_mask=attention_mask,
|
90 |
+
max_new_tokens=512,
|
91 |
+
do_sample=False,
|
92 |
+
repetition_penalty=1.2,
|
93 |
+
pad_token_id=tokenizer.eos_token_id
|
94 |
+
)[0]
|
95 |
+
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
|
96 |
+
|
97 |
+
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
98 |
+
|
99 |
+
import re
|
100 |
+
jsonl_id = re.sub(".*/", "", adapter_id)
|
101 |
+
with open(f"./{jsonl_id}-outputs.jsonl", 'w', encoding='utf-8') as f:
|
102 |
+
for result in results:
|
103 |
+
json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
|
104 |
+
f.write('\n')
|
105 |
|
106 |
+
#以上でjsonlファイルを得る。
|
107 |
+
```
|
108 |
|
109 |
### Training Data
|
110 |
|