-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdatautils.py
189 lines (162 loc) · 6.13 KB
/
datautils.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
import numpy as np
import torch
from datasets import load_dataset
import random
import io
import json
"""
doc https://huggingface.co/docs/datasets/loading
doc https://huggingface.co/docs/datasets/process
doc https://huggingface.co/blog/llama2#how-to-prompt-llama-2
"""
def set_seed(seed):
np.random.seed(seed)
torch.random.manual_seed(seed)
def sample_train_loaders(name, tokenizer, nsamples=128, seed=0, seqlen=2048):
set_seed(seed)
if "wikitext2" in name:
traindata = load_dataset(
"wikitext",
"wikitext-2-raw-v1",
split="train",
)
traindata = "\n\n".join(traindata["text"])
elif "c4" in name:
traindata = load_dataset(
"allenai/c4",
"allenai--c4",
data_files={"train": "en/c4-train.00000-of-01024.json.gz"},
split="train",
)
traindata = "\n\n".join(traindata["text"])
else:
raise NotImplementedError
trainloader = []
for _ in range(nsamples):
i = random.randint(0, len(traindata) - seqlen * 2 - 1)
j = i + seqlen * 2
# breakpoint()
trainenc = tokenizer(traindata[i:j], return_tensors="pt")
inp = trainenc.input_ids[:, :seqlen]
trainloader.append(inp)
return trainloader
def get_redpajama_train(tokenizer, percent=10, seed=3, batch_size=128, max_length=2048):
def tokenization(example):
return tokenizer(example["text"], truncation=True, max_length=max_length)
if percent != 100:
split = f"train[:{int(850000*percent/100)}]"
else:
split = "train"
dataset = load_dataset("togethercomputer/RedPajama-Data-1T-Sample", split=split)
processed_dataset = dataset.map(tokenization, batched=True, batch_size=batch_size, num_proc=os.cpu_count())
return processed_dataset
def get_english_quote(dataset_name, tokenizer):
data = load_dataset(dataset_name)
data = data.map(lambda samples: tokenizer(samples["quote"]), batched=True)
return data["train"]
def get_qat_dataset(name, tokenizer, data_percent):
if name == "red_pajama":
data = get_redpajama_train(tokenizer, data_percent)
elif name == "Abirate/english_quotes":
data = get_english_quote(name, tokenizer)
else:
raise NotImplementedError
data = data.shuffle()
return data
llama_chat_format = """<s>[INST] <<SYS>>
"Below is an instruction that describes a task. Write a response that appropriately completes the request."
<</SYS>>
{{ instruction }} [/INST] {{ response }} </s>
"""
def _make_r_io_base(f, mode: str):
if not isinstance(f, io.IOBase):
f = open(f, mode=mode)
return f
def jload(f, mode="r"):
"""Load a .json file into a dictionary."""
f = _make_r_io_base(f, mode)
jdict = json.load(f)
f.close()
return jdict
def get_calib_data(name, tokenizer, model_id, nsamples, seqlen=2048, seed=3, use_bos=False):
print(f" get_ptq_calib_data {name}, nsamples={nsamples}, seqlen={seqlen}, {seed}")
cache_file = f"cache/{name}_{model_id.replace('/','_')}_{nsamples}_{seqlen}_{seed}_bos{use_bos}.pt"
print(f"cache_file={cache_file}")
if not os.path.exists("cache"):
os.makedirs("cache")
if os.path.exists(cache_file):
traindataset = torch.load(cache_file)
return traindataset
if name == "c4":
traindata = load_dataset(
"allenai/c4", data_files={"train": "en/c4-train.00000-of-01024.json.gz"}, split="train"
)
tot_text = "\n\n".join(traindata["text"])
elif name == "wikitext2":
traindata = load_dataset("wikitext", "wikitext-2-raw-v1", split="train")
tot_text = "\n\n".join(traindata["text"])
elif name == "ptb":
traindata = load_dataset("ptb_text_only", "penn_treebank", split="train")
tot_text = "\n\n".join(traindata["sentence"])
elif name == "alpaca":
# this is for chat models
data_path = "data/alpaca_data.json"
list_data_dict = jload(data_path)
traindataset = []
selected_data_dict = random.sample(list_data_dict, nsamples)
for example in selected_data_dict:
if example.get("input", "") == "":
s = llama_chat_format.format(instruction=example["instruction"], response=example["output"])
trainenc = tokenizer(s, return_tensors="pt")
inp = trainenc.input_ids[:, :seqlen]
attention_mask = torch.ones_like(inp)
traindataset.append({"input_ids": inp, "attention_mask": attention_mask})
return traindataset
elif name == "selfgen":
raise NotImplementedError
else:
raise NotImplementedError
print(f"tot_text={len(tot_text)}")
traindataset = []
for _ in range(nsamples):
i = random.randint(0, len(tot_text) - seqlen - 1)
j = i + seqlen * 10
txt = tot_text[i:j]
ind = txt.find(".")
txt = txt[ind + 1 :].strip()
if use_bos:
txt = tokenizer.bos_token + txt
trainenc = tokenizer(txt, return_tensors="pt")
inp = trainenc.input_ids[:, :seqlen]
attention_mask = torch.ones_like(inp)
traindataset.append({"input_ids": inp, "attention_mask": attention_mask})
torch.save(traindataset, cache_file)
return traindataset
def get_eval_loaders(name, tokenizer):
if "wikitext2" in name:
testdata = load_dataset(
"wikitext",
"wikitext-2-raw-v1",
split="test",
)
testenc = tokenizer("\n\n".join(testdata["text"]), return_tensors="pt")
return testenc
if "ptb" in name:
valdata = load_dataset(
"ptb_text_only",
"penn_treebank",
split="validation",
)
testenc = tokenizer("\n\n".join(valdata["sentence"]), return_tensors="pt")
return testenc
if "c4" in name:
testdata = load_dataset(
"allenai/c4",
"allenai--c4",
data_files={"validation": "en/c4-validation.00000-of-00008.json.gz"},
split="validation",
)
testenc = tokenizer("\n\n".join(testdata["text"]), return_tensors="pt")
return testenc
raise NotImplementedError