-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-cli-sft.py
More file actions
93 lines (74 loc) · 2.5 KB
/
generate-cli-sft.py
File metadata and controls
93 lines (74 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import json
import time
from peft import PeftModel, LoraModel
import os
def save_to_json(data, filename):
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "w") as f:
json.dump(data, f, indent=4)
model_name = "meta-llama/CodeLlama-13b-Instruct-hf"
model = AutoModelForCausalLM.from_pretrained(
model_name,
load_in_8bit=False,
device_map="auto",
torch_dtype=torch.bfloat16,
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.add_special_tokens({"pad_token": "<PAD>"})
model.resize_token_embeddings(len(tokenizer))
peft_model_id = "cli-meta-13b-sft-task1-lora16-epoch5"
peft_model: LoraModel = PeftModel.from_pretrained(
model,
f"./checkpoint/{peft_model_id}/final/",
offload_folder="lora_results/lora_7/temp",
)
with open("./prepare-data/Text2Chart-31-test.json", "r") as file:
test_set = json.load(file)
dataset = []
total_num = len(test_set)
# Print elapsed hour/min/sec
start = time.time()
def print_elapsed_time(start):
elapsed = time.time() - start
# format as 00:00:00
print(f"=== Elapsed time: {time.strftime('%H:%M:%S', time.gmtime(elapsed))} ===")
for index, item in enumerate(test_set):
input_prompt = f"""<s>[INST] <<SYS>>
You are good at generating complete python code from the given chart description.
<</SYS>>
Your task is to generate a complete python code for the given description. Make sure to include all necessary libraries.
Description:
{item['description']}
Please generate the corresponding code that generates the plot that has the above description.
[/INST]
“””Python
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
"""
input_tokens = tokenizer(input_prompt, return_tensors="pt")["input_ids"].to("cuda")
generated_output = peft_model.generate(
input_ids=input_tokens,
do_sample=True,
top_k=10,
temperature=0.1,
top_p=0.95,
num_return_sequences=1,
eos_token_id=tokenizer.eos_token_id,
max_length=2048,
)
value = tokenizer.decode(generated_output[0], skip_special_tokens=True)
print(value)
print(f"=== Step: ({index} / {total_num}) ===")
print_elapsed_time(start)
dataset.append(
{
"id": item["id"],
"description": item["description"],
"generated_output": value,
"code": item["code"],
}
)
save_to_json(dataset, f"output/{peft_model_id}.json")