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 1 commit
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
Next Next commit
drop setUp class in reward tester
  • Loading branch information
qgallouedec committed Aug 2, 2024
commit 85cefe29a35c2a06431307a8c58bc9052651d569
55 changes: 34 additions & 21 deletions tests/test_reward_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 26,16 @@


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 78,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 105,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 Down Expand Up @@ -158,9 160,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,6 198,10 @@ 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,
Expand Down Expand Up @@ -235,9 241,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 @@ -253,13 259,17 @@ def test_reward_trainer_assert_value_error(self):

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 Down Expand Up @@ -293,24 303,27 @@ 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(
outputs["rewards_chosen"] - outputs["rewards_rejected"] - batch["margin"]
).mean()

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 Down Expand Up @@ -353,9 366,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