Spaces:
Runtime error
Runtime error
File size: 7,364 Bytes
fa6856c |
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 |
"""Example of using PPO to train a T5 model for translation.
Based on examples/summarize_daily_cnn/t5_summarize_daily_cnn.py"""
import json
import os
import sys
from typing import List
import torch
from datasets import load_dataset
from tqdm import tqdm
from transformers import AutoTokenizer
import trlx
from trlx.data.configs import (
ModelConfig,
OptimizerConfig,
SchedulerConfig,
TokenizerConfig,
TrainConfig,
TRLConfig,
)
from trlx.models.modeling_ppo import PPOConfig
try:
import comet
import evaluate
if comet.__version__ != "1.1.3":
raise ImportError
except ImportError:
raise ImportError(
"To run this example, please install `evaluate`, `nltk` and `comet==1.1.3` packages by "
"running `pip install evaluate unbabel-comet==1.1.3`"
)
default_config = TRLConfig(
train=TrainConfig(
seq_length=612,
epochs=100,
total_steps=100000,
batch_size=12,
checkpoint_interval=10000,
eval_interval=200,
pipeline="PromptPipeline",
trainer="AcceleratePPOTrainer",
tracker="wandb",
),
model=ModelConfig(
model_path="t5-large",
model_arch_type="seq2seq",
num_layers_unfrozen=-1,
),
tokenizer=TokenizerConfig(
tokenizer_path="t5-large",
padding_side="right",
truncation_side="right",
),
optimizer=OptimizerConfig(
name="adamw",
kwargs={
"lr": 2.0e-6,
"betas": [0.9, 0.999],
"eps": 1.0e-8,
"weight_decay": 1.0e-6,
},
),
scheduler=SchedulerConfig(
name="cosine_annealing",
kwargs={
"T_max": 10000,
"eta_min": 1.0e-6,
},
),
method=PPOConfig(
name="PPOConfig",
num_rollouts=256,
chunk_size=12,
ppo_epochs=4,
init_kl_coef=0.05,
target=6,
horizon=10000,
gamma=0.99,
lam=0.95,
cliprange=0.2,
cliprange_value=0.2,
vf_coef=1.0,
scale_reward=None,
ref_mean=None,
ref_std=None,
cliprange_reward=10,
gen_kwargs={
"max_new_tokens": 100,
},
gen_experience_kwargs={
"max_new_tokens": 100,
"do_sample": False,
"num_beams": 4,
"temperature": 1.0,
},
),
)
def main(hparams={}):
config = TRLConfig.update(default_config, hparams)
# COMET is the metric we are optimizng for
comet_metric = evaluate.load("comet", "wmt20-comet-da", progress_bar=False)
bleu_metric = evaluate.load("bleu")
chrf_metric = evaluate.load("chrf")
os.environ["TOKENIZERS_PARALLELISM"] = "false"
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
def reward_fn(samples: List[str], prompts: List[str], outputs: List[str]) -> List[float]:
original_sents = [translation_map[prompt.strip()] for prompt in prompts]
scores = comet_metric.compute(
predictions=[output.strip() for output in outputs],
references=[original["tgt"] for original in original_sents],
sources=[original["src"] for original in original_sents],
)["scores"]
# TODO: This is needed since there seems to be a bug in the comet metric
# that changes torch's determinism setting. Remove this once the bug is fixed.
torch.use_deterministic_algorithms(False, warn_only=True)
return scores
def metric_fn(samples: List[str], prompts: List[str], outputs: List[str]) -> List[float]:
"""Compute COMET, BLEU and CHRF for evaluation"""
original_sents = [translation_map[prompt.strip()] for prompt in prompts]
comet_score = comet_metric.compute(
predictions=[output.strip() for output in outputs],
references=[original["tgt"] for original in original_sents],
sources=[original["src"] for original in original_sents],
)["mean_score"]
bleu_score = bleu_metric.compute(
predictions=[output.strip() for output in outputs],
references=[original["tgt"] for original in original_sents],
)["bleu"]
chrf_score = chrf_metric.compute(
predictions=[output.strip() for output in outputs],
references=[original["tgt"] for original in original_sents],
)["score"]
# TODO: This is needed since there seems to be a bug in the comet metric
# that changes torch's determinism setting. Remove this once the bug is fixed.
# Same issue as in `reward_fn`
torch.use_deterministic_algorithms(False, warn_only=True)
# For corpus-level metrics, it's better to ignore the sentence-level scores
return {"bleu": bleu_score, "chrf": chrf_score, "comet": comet_score}
# The WMT16 is large so we can benefit with using it as a streaming dataset
train_dataset = load_dataset("wmt16", "de-en", split="train", streaming=True)
valid_dataset = load_dataset("wmt16", "de-en", split="validation", streaming=True)
src_lang = "en"
tgt_lang = "de"
PREFIX = "translate English to German: "
# take 20,000 samples from the training set as prompts for training
original_src_dataset = [sent_pair["translation"][src_lang] for sent_pair in train_dataset.take(20000)]
tgt_dataset = [sent_pair["translation"][tgt_lang] for sent_pair in train_dataset.take(20000)]
src_dataset = [PREFIX + src_sent for src_sent in original_src_dataset]
# take 1,000 samples from the validation set as prompts for evaluation
val_original_src_dataset = [sent_pair["translation"][src_lang] for sent_pair in valid_dataset.take(1000)]
val_tgt_dataset = [sent_pair["translation"][tgt_lang] for sent_pair in valid_dataset.take(1000)]
val_src_dataset = [PREFIX + src_sent for src_sent in val_original_src_dataset]
# make dictionary of prompts and labels to use for reward function
tokenizer = AutoTokenizer.from_pretrained(config.model.model_path)
tokenizer.padding_side = "left"
tokenizer.truncation_side = "right"
tokenizer.sep_token = "<sep>"
max_length = config.train.seq_length - config.method.gen_kwargs["max_new_tokens"]
translation_map = {}
for i in tqdm(range(len(original_src_dataset))):
key = tokenizer.decode(
tokenizer(src_dataset[i], truncation=True, max_length=max_length, add_special_tokens=False)["input_ids"],
skip_special_tokens=True,
) # get prompt like trlx's prompt
translation_map[key.strip()] = {"src": original_src_dataset[i], "tgt": tgt_dataset[i]}
for i in tqdm(range(len(val_original_src_dataset))):
key = tokenizer.decode(
tokenizer(val_src_dataset[i], truncation=True, max_length=max_length, add_special_tokens=False)[
"input_ids"
],
skip_special_tokens=True,
) # get prompt like trlx's prompt
translation_map[key.strip()] = {"src": val_original_src_dataset[i], "tgt": val_tgt_dataset[i]}
trlx.train(
reward_fn=reward_fn,
metric_fn=metric_fn,
prompts=src_dataset,
eval_prompts=val_src_dataset,
config=config,
)
if __name__ == "__main__":
hparams = {} if len(sys.argv) == 1 else json.loads(sys.argv[1])
main(hparams)
|