-
Notifications
You must be signed in to change notification settings - Fork 51
/
tokenize_dataset_rows.py
44 lines (35 loc) · 1.39 KB
/
tokenize_dataset_rows.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
import argparse
import json
from tqdm import tqdm
import datasets
import transformers
def preprocess(tokenizer, example, max_seq_length=512):
prompt = example["context"]
target = example["target"]
prompt_ids = tokenizer.encode(prompt, max_length=max_seq_length, truncation=True)
target_ids = tokenizer.encode(
target, max_length=max_seq_length, truncation=True, add_special_tokens=False
)
input_ids = prompt_ids + target_ids + [tokenizer.eos_token_id]
return {"input_ids": input_ids, "seq_len": len(prompt_ids)}
def read_jsonl(path, max_seq_length):
tokenizer = transformers.AutoTokenizer.from_pretrained(
"THUDM/chatglm-6b", trust_remote_code=True
)
with open(path, "r") as f:
for line in tqdm(f.readlines()):
example = json.loads(line)
yield preprocess(tokenizer, example, max_seq_length)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--jsonl_path", type=str, default="data/alpaca_data.jsonl")
parser.add_argument("--save_path", type=str, default="data/alpaca")
parser.add_argument("--max_seq_length", type=int, default=384)
args = parser.parse_args()
dataset = datasets.Dataset.from_generator(
lambda: read_jsonl(args.jsonl_path, args.max_seq_length)
)
dataset.save_to_disk(args.save_path)
print(dataset.to_pandas().head())
if __name__ == "__main__":
main()