-
Notifications
You must be signed in to change notification settings - Fork 55
/
tokenizer.py
140 lines (116 loc) · 3.39 KB
/
tokenizer.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
# Apache Software License 2.0
#
# Copyright (c) ZenML GmbH 2024. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from transformers import AutoTokenizer
def load_tokenizer(
base_model_id: str,
is_eval: bool = False,
use_fast: bool = True,
) -> AutoTokenizer:
"""Loads the tokenizer for the given base model id.
Args:
base_model_id: The base model id to use.
is_eval: Whether to load the tokenizer for evaluation.
use_fast: Whether to use the fast tokenizer.
Returns:
The tokenizer.
"""
if is_eval:
tokenizer = AutoTokenizer.from_pretrained(
base_model_id,
add_bos_token=True,
device_map="auto",
use_fast=use_fast,
trust_remote_code=True,
)
tokenizer.pad_token_id = 0
else:
tokenizer = AutoTokenizer.from_pretrained(
base_model_id,
model_max_length=512,
padding_side="left",
add_eos_token=True,
device_map="auto",
use_fast=use_fast,
trust_remote_code=True,
)
tokenizer.pad_token = tokenizer.eos_token
return tokenizer
def tokenize(
prompt: str,
tokenizer: AutoTokenizer,
) -> dict:
"""Tokenizes the prompt for single entry.
Args:
prompt: The prompt to tokenize.
tokenizer: The tokenizer to use.
Returns:
The tokenized prompt.
"""
result = tokenizer(
prompt,
truncation=True,
max_length=512,
padding="max_length",
)
result["labels"] = result["input_ids"].copy()
return result
def generate_and_tokenize_prompt(
data_point: dict,
tokenizer: AutoTokenizer,
system_prompt: str,
):
"""Generates and tokenizes the prompt for single entry.
To be used in map function of the dataset.
Args:
data_point: The data point to generate and tokenize.
tokenizer: The tokenizer to use.
system_prompt: The system prompt to use.
Returns:
The tokenized prompt.
"""
full_prompt = f"""{system_prompt}
### Target sentence:
{data_point["target"]}
### Meaning representation:
{data_point["meaning_representation"]}
"""
return tokenize(full_prompt, tokenizer)
def tokenize_for_eval(
data_points: dict,
tokenizer: AutoTokenizer,
system_prompt: str,
):
"""Tokenizes the prompts for evaluation.
This runs for the whole test dataset at once.
Args:
data_points: The data points to tokenize.
tokenizer: The tokenizer to use.
system_prompt: The system prompt to use.
Returns:
The tokenized prompt.
"""
eval_prompts = [
f"""{system_prompt}
### Target sentence:
{data_point}
### Meaning representation:
"""
for data_point in data_points["target"]
]
return tokenizer(eval_prompts, padding="longest", return_tensors="pt").to(
"cuda"
)