File size: 17,825 Bytes
1904ee8 |
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
import math
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple, Union
import accelerate
import torch
from datasets import Dataset
from torch.utils.data import DataLoader
from tqdm.auto import tqdm
from transformers import PreTrainedTokenizerBase, TrainerCallback
import wandb
from trl.trainer.utils import pad_to_length
@dataclass
class PromptAndTextCollator:
tokenizer: PreTrainedTokenizerBase
padding: Union[bool, str] = True
max_prompt_length: Optional[int] = None
max_length: Optional[int] = None
prompt_field: str = "prompt"
target_field: str = "label"
return_tensors: str = "pt"
def __call__(self, features: List[Dict[str, Any]]) -> Dict[str, Any]:
prompts = [feat[self.prompt_field] for feat in features]
texts = [feat[self.prompt_field] + " " + feat[self.target_field] for feat in features]
original_side = self.tokenizer.padding_side
self.tokenizer.padding_side = "left"
tokenized_batch = self.tokenizer(
prompts,
truncation=True,
padding=True,
max_length=self.max_prompt_length,
return_tensors=self.return_tensors,
)
tokenized_batch["prompt"] = prompts
self.tokenizer.padding_side = original_side
tokenized_texts = self.tokenizer(
texts,
truncation=True,
padding=True,
max_length=self.max_length,
return_tensors=self.return_tensors,
)
text_labels = tokenized_texts["input_ids"].clone()
if self.tokenizer.pad_token_id is not None:
text_labels[text_labels == self.tokenizer.pad_token_id] = -100
tokenized_batch.update(
{
"text_input_ids": tokenized_texts["input_ids"],
"text_attention_mask": tokenized_texts["attention_mask"],
"text_labels": text_labels,
}
)
return tokenized_batch
class GoldModelRewardCallback(TrainerCallback):
def __init__(
self,
args,
gold_model,
gold_eval_dataset,
tokenizer,
accelerator,
max_length,
max_prompt_length,
prompt_field,
target_field,
gold_load_and_unload=False,
log_n_samples_during_eval=0,
generation_config=None,
):
self.max_length = max_length
self.log_n_samples_during_eval = log_n_samples_during_eval
self.generation_config = generation_config
# data_collator = DataCollatorWithPadding(tokenizer)
data_collator = PromptAndTextCollator(
tokenizer,
max_prompt_length=max_prompt_length,
max_length=max_length,
prompt_field=prompt_field,
target_field=target_field,
)
dataloader_params = {
"batch_size": args.eval_batch_size,
"collate_fn": data_collator,
"num_workers": args.dataloader_num_workers,
"pin_memory": args.dataloader_pin_memory,
}
dataloader = DataLoader(gold_eval_dataset, **dataloader_params)
self.dataloader = accelerator.prepare(dataloader)
self.accelerator = accelerator
self.completed_step = -1
self.gold_model = gold_model
self.gold_load_and_unload = gold_load_and_unload
# keep model on gpu the whole time
if not self.gold_load_and_unload:
self.gold_model = self.accelerator.prepare(self.gold_model)
def on_evaluate(self, args, state, control, model, tokenizer, metrics, **kwargs):
samples_to_log = []
gold_reward_sum = 0.0
nll_sum = 0.0
total_samples = 0
sample_length_sum = 0.0
# load model onto gpu for inference then unload
if self.gold_load_and_unload:
self.gold_model = self.accelerator.prepare(self.gold_model)
if state.global_step == self.completed_step:
return
for inputs in tqdm(
self.dataloader, desc="Gold Eval", dynamic_ncols=True, disable=not state.is_local_process_zero
):
# get loss over true continuation i.e. ppl on dataset
with torch.no_grad():
nll_loss = model(
input_ids=inputs["text_input_ids"],
attention_mask=inputs["text_attention_mask"],
labels=inputs["text_labels"],
).loss
nll_loss = self.accelerator.gather_for_metrics(nll_loss)
# generate from model
policy_output_decoded, ref_output_decoded, policy_output_ids = self.get_batch_samples(
model,
tokenizer,
inputs["input_ids"],
inputs["attention_mask"],
return_ids=True,
)
# gold reward
policy_output_attention_mask = (policy_output_ids != tokenizer.pad_token_id).to(torch.int64)
with torch.no_grad():
gold_rewards = self.gold_model(
input_ids=policy_output_ids, attention_mask=policy_output_attention_mask
)[0]
gold_rewards = self.accelerator.gather_for_metrics(gold_rewards)
if state.is_local_process_zero:
nll_sum += nll_loss.sum().item()
gold_reward_sum += gold_rewards.sum().item()
total_samples += gold_rewards.size(0)
sample_length_sum += policy_output_attention_mask.sum().item()
# Sample and save to game log if requested (for one batch to save time)
for i, (prompt, pol, ref) in enumerate(
zip(inputs["prompt"], policy_output_decoded, ref_output_decoded)
):
if len(samples_to_log) < self.log_n_samples_during_eval:
samples_to_log.append([prompt, pol[len(prompt) :], ref[len(prompt) :]])
else:
break
if self.gold_load_and_unload:
self.gold_model = self.gold_model.to("cpu")
torch.cuda.empty_cache()
if state.is_world_process_zero:
gold_log = {
"eval/gold_rewards_mean": gold_reward_sum / total_samples,
"eval/perplexity": math.exp(nll_sum / total_samples),
"eval/gold_sample_length": sample_length_sum / total_samples,
}
for key, value in gold_log.items():
print(f"{key}: {value}")
if state.epoch:
gold_log["epoch"] = round(state.epoch, 2)
gold_log["step"] = state.global_step
if samples_to_log:
gold_log["gold_log"] = (
wandb.Table(
columns=["Prompt", "Policy", "Ref Model"],
rows=samples_to_log,
),
)
wandb.log(gold_log)
self.completed_step = state.global_step
def get_batch_samples(self, model, tokenizer, input_ids, attention_mask, return_ids=False) -> Tuple[str, str]:
"""Reduce inputs to unseen prompts, and maximum batch size if necessary
Generate samples from the model and reference model for the given batch of inputs."""
policy_output = model.generate(
input_ids=input_ids,
attention_mask=attention_mask,
generation_config=self.generation_config,
)
# if self.ref_model is None:
with self.accelerator.unwrap_model(model).disable_adapter():
reference_output = model.generate(
input_ids=input_ids,
attention_mask=attention_mask,
generation_config=self.generation_config,
)
# else:
# reference_output = self.ref_model.generate(
# **inputs,
# generation_config=self.generation_config,
# )
policy_output = pad_to_length(policy_output, self.max_length, tokenizer.pad_token_id)
policy_output_decoded = tokenizer.batch_decode(policy_output, skip_special_tokens=True)
reference_output = pad_to_length(reference_output, self.max_length, tokenizer.pad_token_id)
reference_output_decoded = tokenizer.batch_decode(reference_output, skip_special_tokens=True)
if return_ids:
return policy_output_decoded, reference_output_decoded, policy_output
else:
return policy_output_decoded, reference_output_decoded
class PerplexityCallback(TrainerCallback):
"""Like GoldModelReward in that you generate and get ppl on dataset
But you don't run eval with the gold model
Useful when gold model is very larger and you want to run inference later
"""
def __init__(
self,
args,
dataset,
tokenizer,
accelerator,
max_length,
max_prompt_length,
prompt_field,
target_field,
hub_model_id=None,
**kwargs,
):
self.max_length = max_length
# data_collator = DataCollatorWithPadding(tokenizer)
data_collator = PromptAndTextCollator(
tokenizer,
max_prompt_length=max_prompt_length,
max_length=max_length,
prompt_field=prompt_field,
target_field=target_field,
)
dataloader_params = {
"batch_size": args.eval_batch_size,
"collate_fn": data_collator,
"num_workers": args.dataloader_num_workers,
"pin_memory": args.dataloader_pin_memory,
}
dataloader = DataLoader(dataset, **dataloader_params)
self.dataloader = accelerator.prepare(dataloader)
self.accelerator = accelerator
self.completed_step = -1
self.hub_model_id = hub_model_id
def on_evaluate(self, args, state, control, model, tokenizer, metrics, **kwargs):
nll_sum = 0.0
total_samples = 0
if state.global_step == self.completed_step:
return
for inputs in tqdm(
self.dataloader, desc="PPL and Gen Eval", dynamic_ncols=True, disable=not state.is_local_process_zero
):
# get loss over true continuation i.e. ppl on dataset
with torch.no_grad():
nll_loss = model(
input_ids=inputs["text_input_ids"],
attention_mask=inputs["text_attention_mask"],
labels=inputs["text_labels"],
).loss
nll_loss = self.accelerator.gather_for_metrics(nll_loss)
if state.is_local_process_zero:
total_samples += nll_loss.size(0)
nll_sum += nll_loss.sum().item()
if state.is_world_process_zero:
# gather_for_metrics doesn't work for list of strings?
gold_log = {
"eval/perplexity": math.exp(nll_sum / total_samples),
}
for key, value in gold_log.items():
print(f"{key}: {value}")
if state.epoch:
gold_log["epoch"] = round(state.epoch, 2)
gold_log["step"] = state.global_step
wandb.log(gold_log)
if self.hub_model_id is not None:
model.push_to_hub(self.hub_model_id, revision=f"step{state.global_step}")
self.completed_step = state.global_step
class PerplexityGenCallback(TrainerCallback):
"""Like GoldModelReward in that you generate and get ppl on dataset
But you don't run eval with the gold model
Useful when gold model is very larger and you want to run inference later
"""
def __init__(
self,
args,
dataset,
tokenizer,
accelerator,
max_length,
max_prompt_length,
prompt_field,
target_field,
log_n_samples_during_eval=0,
generation_config=None,
hub_model_id="tmp",
):
self.max_length = max_length
self.log_n_samples_during_eval = log_n_samples_during_eval
self.generation_config = generation_config
# data_collator = DataCollatorWithPadding(tokenizer)
data_collator = PromptAndTextCollator(
tokenizer,
max_prompt_length=max_prompt_length,
max_length=max_length,
prompt_field=prompt_field,
target_field=target_field,
)
dataloader_params = {
"batch_size": args.eval_batch_size,
"collate_fn": data_collator,
"num_workers": args.dataloader_num_workers,
"pin_memory": args.dataloader_pin_memory,
}
dataloader = DataLoader(dataset, **dataloader_params)
self.dataloader = accelerator.prepare(dataloader)
self.accelerator = accelerator
self.completed_step = -1
self.hub_name = hub_model_id
def on_evaluate(self, args, state, control, model, tokenizer, metrics, **kwargs):
all_generations = []
all_prompts = []
nll_sum = 0.0
total_samples = 0
sample_length_sum = 0.0
if state.global_step == self.completed_step:
return
for inputs in tqdm(
self.dataloader, desc="PPL and Gen Eval", dynamic_ncols=True, disable=not state.is_local_process_zero
):
# get loss over true continuation i.e. ppl on dataset
with torch.no_grad():
nll_loss = model(
input_ids=inputs["text_input_ids"],
attention_mask=inputs["text_attention_mask"],
labels=inputs["text_labels"],
).loss
# generate from model
policy_output_ids = model.generate(
input_ids=inputs["input_ids"],
attention_mask=inputs["attention_mask"],
generation_config=self.generation_config,
)
policy_output_ids = pad_to_length(policy_output_ids, self.max_length, tokenizer.pad_token_id)
policy_output_attention_mask = (policy_output_ids != tokenizer.pad_token_id).to(torch.int64)
generation_sizes = policy_output_attention_mask.sum(dim=1)
(nll_loss, generation_ids, generation_sizes) = self.accelerator.gather_for_metrics(
(nll_loss, policy_output_ids, generation_sizes)
)
prompts = accelerate.utils.gather_object(inputs["prompt"])
if state.is_local_process_zero:
nll_sum += nll_loss.sum().item()
total_samples += generation_sizes.size(0)
sample_length_sum += generation_sizes.sum().item()
generation_strs = tokenizer.batch_decode(generation_ids, skip_special_tokens=True)
all_prompts.extend(prompts)
all_generations.extend(generation_strs)
if state.is_world_process_zero:
# gather_for_metrics doesn't work for list of strings?
gold_log = {
"eval/perplexity": math.exp(nll_sum / total_samples),
"eval/gold_sample_length": sample_length_sum / total_samples,
}
for key, value in gold_log.items():
print(f"{key}: {value}")
if state.epoch:
gold_log["epoch"] = round(state.epoch, 2)
gold_log["step"] = state.global_step
if self.log_n_samples_during_eval:
samples_to_log = [
[prompt, generation[len(prompt) :]]
for prompt, generation in zip(
all_prompts[: self.log_n_samples_during_eval],
all_generations[: self.log_n_samples_during_eval],
)
]
gold_log["gold_log"] = (
wandb.Table(
columns=["Prompt", "Policy"],
rows=samples_to_log,
),
)
wandb.log(gold_log)
generation_ds = Dataset.from_dict({"generations": all_generations})
generation_ds.push_to_hub(f"{self.hub_name}_generations", revision=str(state.global_step))
self.completed_step = state.global_step
def get_batch_samples(self, model, tokenizer, input_ids, attention_mask, return_ids=False) -> Tuple[str, str]:
"""Reduce inputs to unseen prompts, and maximum batch size if necessary
Generate samples from the model and reference model for the given batch of inputs."""
policy_output = model.generate(
input_ids=input_ids,
attention_mask=attention_mask,
generation_config=self.generation_config,
)
# if self.ref_model is None:
with self.accelerator.unwrap_model(model).disable_adapter():
reference_output = model.generate(
input_ids=input_ids,
attention_mask=attention_mask,
generation_config=self.generation_config,
)
# else:
# reference_output = self.ref_model.generate(
# **inputs,
# generation_config=self.generation_config,
# )
policy_output = pad_to_length(policy_output, self.max_length, tokenizer.pad_token_id)
policy_output_decoded = tokenizer.batch_decode(policy_output, skip_special_tokens=True)
reference_output = pad_to_length(reference_output, self.max_length, tokenizer.pad_token_id)
reference_output_decoded = tokenizer.batch_decode(reference_output, skip_special_tokens=True)
if return_ids:
return policy_output_decoded, reference_output_decoded, policy_output
else:
return policy_output_decoded, reference_output_decoded
|