-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_imr.py
More file actions
234 lines (190 loc) · 5.55 KB
/
Copy pathevaluate_imr.py
File metadata and controls
234 lines (190 loc) · 5.55 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Imports
import argparse
import torch
from transformers import AutoTokenizer, PreTrainedTokenizerFast
# BoardGPT
from boardGPT.datasets import load_othello_data_files, GameDataset
from boardGPT.models import GameGPT
from boardGPT.nn import GPTConfig, GPT
from boardGPT.utils import info, error, warning, train_log, eval_log
from boardGPT.validation import evaluate_IMR
def parse_args():
"""
Parse command line arguments for the training script.
Returns:
argparse.Namespace: Parsed command line arguments
"""
parser = argparse.ArgumentParser(
description='Evaluate IMR of a model on a dataset.'
)
parser.add_argument(
'--data-dir',
type=str,
required=True,
help='Path to the data directory. This directory must contain "train" and '
'"val" folders with bin files for each split.'
)
parser.add_argument(
'--num-iter',
type=int,
help='Number of evaluation iterations.'
)
parser.add_argument(
'--repo-id',
type=str,
default=None,
help='Path to model repository'
)
parser.add_argument(
'--batch-size',
type=int,
required=True,
help='Batch size for evaluation',
)
parser.add_argument(
'--num-workers',
type=int,
required=True,
help='Number of workers for data loading',
)
parser.add_argument(
'--seed',
type=int,
default=42,
help='Random seed for reproducibility',
)
parser.add_argument(
'--device',
type=str,
default='cuda',
help='Device'
)
return parser.parse_args()
# end def parse_args
def load_model(
repo_id: str,
device
):
"""
Initialize the model based on checkpoint arguments or from scratch.
Args:
repo_id (str): Repo ID
device (str): Device to use for the model
Returns:
tuple: (model, iter_num, best_val_loss, model_args) where model is the initialized GPT model,
iter_num is the starting iteration number, best_val_loss is the best
validation loss (used for checkpointing), and model_args are the model arguments.
"""
# Load model from repo
model, config = GameGPT.from_pretrained(repo_id=repo_id)
model.to(device)
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(repo_id, subfolder='tokenizer')
return model, config, tokenizer
# end load_model
def collate_fn(batch, tokenizer: PreTrainedTokenizerFast):
"""
Convert a batch of raw strings into padded tensors.
Args:
batch (list): A batch of raw strings.
tokenizer (PreTrainedTokenizerFast): Tokenizer object used to tokenize the batch.
max_length (int, optional): Maximum length of the padded tensors.
Returns:
tuple: padded tensors and padded lengths.
"""
enc = tokenizer(
batch,
return_tensors="pt"
)
# Sequence length
seq_len = enc["input_ids"].shape[-1] // 2
# Split into X and Y
X = enc["input_ids"][:, :seq_len]
Y = enc["input_ids"][:, seq_len:]
return X, Y
# end def collate_fn
def get_dataloader(
data_dir: str,
block_size: int,
batch_size: int,
num_workers: int,
tokenizer: PreTrainedTokenizerFast,
) -> torch.utils.data.DataLoader:
"""
Get dataloaders for training and validation.
Args:
data_dir (str): Path to the data directory
config (TrainingConfig): Configuration object containing 'data_dir' which points to a directory
with 'train' and 'val' folders containing bin files for each split
tokenizer (PreTrainedTokenizerFast): Tokenizer object used to tokenize the batch
"""
dataset = GameDataset(
data_dir=data_dir,
split="val",
block_size=block_size,
ood_perc=0.0,
num_samples=-1
)
# Create a dataloader
dataloader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=True,
shuffle=False,
drop_last=False,
collate_fn=lambda b: collate_fn(b, tokenizer)
)
return dataloader
# end def get_dataloader
def infinite_loader(
dataloader: torch.utils.data.DataLoader
):
"""
Infinite loader function for evaluating a batch of data.
"""
while True:
for batch in dataloader:
X, Y = batch
yield X, Y
# end for
# end while
# end def infinite_loader
def main():
"""
Main evaluation function.
"""
# Parse command line arguments
args = parse_args()
# Set random seed for reproducibility
torch.manual_seed(args.seed)
# Initialize the model
info(f"Initializing model")
model, config, tokenizer = load_model(
repo_id=args.repo_id,
device=args.device
)
# Create validation dataloader
val_dataloader = get_dataloader(
data_dir=args.data_dir,
block_size=config.block_size,
batch_size=args.batch_size,
num_workers=args.num_workers,
tokenizer=tokenizer
)
val_data_iter = infinite_loader(val_dataloader)
# Compute the IMR
info(f"Evaluating model")
im_rate = evaluate_IMR(
model=model,
iter=val_data_iter,
tokenizer=tokenizer,
num_samples=args.num_iter,
device=args.device
)
info(f"IM RATIO: {im_rate * 100:.4f}%")
# end def main
# Execute the main function if a script is run directly
if __name__ == "__main__":
main()
# end if