Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop setUpClass in reward tester #1895

Merged
merged 5 commits into from
Aug 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 43 additions & 19 deletions tests/test_reward_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,17 @@


class RewardTrainerTester(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.model_id = "trl-internal-testing/dummy-GPT2-correct-vocab"
cls.model = AutoModelForSequenceClassification.from_pretrained(cls.model_id)
cls.tokenizer = AutoTokenizer.from_pretrained(cls.model_id)
cls.tokenizer.pad_token = cls.tokenizer.eos_token

def test_accuracy_metrics(self):
dummy_eval_predictions = EvalPrediction(torch.FloatTensor([[0.1, 0.9], [0.9, 0.1]]), torch.LongTensor([0, 0]))
accuracy = compute_accuracy(dummy_eval_predictions)
assert accuracy["accuracy"] == 0.5

def test_reward_trainer(self):
model_id = "trl-internal-testing/dummy-GPT2-correct-vocab"
model = AutoModelForSequenceClassification.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token = tokenizer.eos_token

with tempfile.TemporaryDirectory() as tmp_dir:
training_args = RewardConfig(
output_dir=tmp_dir,
Expand Down Expand Up @@ -81,9 +79,9 @@ def test_reward_trainer(self):
dummy_dataset = Dataset.from_dict(dummy_dataset_dict)

trainer = RewardTrainer(
model=self.model,
model=model,
args=training_args,
tokenizer=self.tokenizer,
tokenizer=tokenizer,
train_dataset=dummy_dataset,
eval_dataset=dummy_dataset,
)
Expand All @@ -108,6 +106,11 @@ def test_reward_trainer(self):
def test_reward_trainer_peft(self):
from peft import LoraConfig, TaskType

model_id = "trl-internal-testing/dummy-GPT2-correct-vocab"
model = AutoModelForSequenceClassification.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token = tokenizer.eos_token

peft_config = LoraConfig(
task_type=TaskType.SEQ_CLS,
inference_mode=False,
Expand All @@ -125,6 +128,7 @@ def test_reward_trainer_peft(self):
gradient_accumulation_steps=2,
learning_rate=9e-1,
eval_strategy="steps",
report_to="none",
)

# fmt: off
Expand Down Expand Up @@ -158,9 +162,9 @@ def test_reward_trainer_peft(self):
dummy_dataset = Dataset.from_dict(dummy_dataset_dict)

trainer = RewardTrainer(
model=self.model,
model=model,
args=training_args,
tokenizer=self.tokenizer,
tokenizer=tokenizer,
train_dataset=dummy_dataset,
eval_dataset=dummy_dataset,
peft_config=peft_config,
Expand Down Expand Up @@ -196,12 +200,18 @@ def test_reward_trainer_peft(self):
assert preds.predictions.shape == (4, 2)

def test_reward_trainer_assert_value_error(self):
model_id = "trl-internal-testing/dummy-GPT2-correct-vocab"
model = AutoModelForSequenceClassification.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token = tokenizer.eos_token

with tempfile.TemporaryDirectory() as tmp_dir:
training_args = RewardConfig(
output_dir=tmp_dir,
per_device_train_batch_size=2,
max_steps=1,
remove_unused_columns=False,
report_to="none",
)

# fmt: off
Expand Down Expand Up @@ -235,9 +245,9 @@ def test_reward_trainer_assert_value_error(self):
dummy_dataset = Dataset.from_dict(dummy_dataset_dict)

trainer = RewardTrainer(
model=self.model,
model=model,
args=training_args,
tokenizer=self.tokenizer,
tokenizer=tokenizer,
train_dataset=dummy_dataset,
)

Expand All @@ -249,17 +259,23 @@ def test_reward_trainer_assert_value_error(self):
per_device_train_batch_size=2,
max_steps=1,
remove_unused_columns=True,
report_to="none",
)

with self.assertWarns(UserWarning):
trainer = RewardTrainer(
model=self.model,
model=model,
args=training_args,
tokenizer=self.tokenizer,
tokenizer=tokenizer,
train_dataset=dummy_dataset,
)

def test_reward_trainer_margin(self):
model_id = "trl-internal-testing/dummy-GPT2-correct-vocab"
model = AutoModelForSequenceClassification.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token = tokenizer.eos_token

with tempfile.TemporaryDirectory() as tmp_dir:
training_args = RewardConfig(
output_dir=tmp_dir,
Expand All @@ -269,6 +285,7 @@ def test_reward_trainer_margin(self):
gradient_accumulation_steps=4,
learning_rate=9e-1,
eval_strategy="steps",
report_to="none",
)

# fmt: off
Expand All @@ -293,15 +310,16 @@ def test_reward_trainer_margin(self):
dummy_dataset = Dataset.from_dict(dummy_dataset_dict)

trainer = RewardTrainer(
model=self.model,
model=model,
args=training_args,
tokenizer=self.tokenizer,
tokenizer=tokenizer,
train_dataset=dummy_dataset,
eval_dataset=dummy_dataset,
)

batch = [dummy_dataset[0]]
batch = trainer.data_collator(batch)
batch = {k: v.to(trainer.model.device) if isinstance(v, torch.Tensor) else v for k, v in batch.items()}
loss, outputs = trainer.compute_loss(trainer.model, batch, return_outputs=True)

l_val = -torch.nn.functional.logsigmoid(
Expand All @@ -311,6 +329,11 @@ def test_reward_trainer_margin(self):
assert abs(loss - l_val) < 1e-6

def test_reward_trainer_tags(self):
model_id = "trl-internal-testing/dummy-GPT2-correct-vocab"
model = AutoModelForSequenceClassification.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token = tokenizer.eos_token

with tempfile.TemporaryDirectory() as tmp_dir:
training_args = RewardConfig(
output_dir=tmp_dir,
Expand All @@ -320,6 +343,7 @@ def test_reward_trainer_tags(self):
gradient_accumulation_steps=4,
learning_rate=9e-1,
eval_strategy="steps",
report_to="none",
)

# fmt: off
Expand Down Expand Up @@ -353,9 +377,9 @@ def test_reward_trainer_tags(self):
dummy_dataset = Dataset.from_dict(dummy_dataset_dict)

trainer = RewardTrainer(
model=self.model,
model=model,
args=training_args,
tokenizer=self.tokenizer,
tokenizer=tokenizer,
train_dataset=dummy_dataset,
eval_dataset=dummy_dataset,
)
Expand Down
Loading