|
35 | 35 |
|
36 | 36 |
|
37 | 37 | if is_peft_available(): |
38 | | - from peft import LoraConfig, get_peft_model |
| 38 | + from peft import ( |
| 39 | + LoraConfig, |
| 40 | + PrefixTuningConfig, |
| 41 | + PromptEncoderConfig, |
| 42 | + PromptTuningConfig, |
| 43 | + TaskType, |
| 44 | + get_peft_model, |
| 45 | + ) |
39 | 46 |
|
40 | 47 |
|
41 | 48 | class TestDataCollatorForPreference(TrlTestCase): |
@@ -621,6 +628,56 @@ def test_train_with_peft_config_and_gradient_checkpointing_reentrant(self, use_r |
621 | 628 | elif "base_layer" not in n: # We expect the peft params to be different (except for the base layer) |
622 | 629 | assert not torch.equal(param, new_param), f"Parameter {n} has not changed." |
623 | 630 |
|
| 631 | + @pytest.mark.parametrize( |
| 632 | + "peft_type", |
| 633 | + [ |
| 634 | + "prompt_tuning", |
| 635 | + "prefix_tuning", |
| 636 | + "prompt_encoder", |
| 637 | + ], |
| 638 | + ) |
| 639 | + @require_peft |
| 640 | + def test_train_with_peft_config_prompt_tuning(self, peft_type): |
| 641 | + model_id = "trl-internal-testing/tiny-Qwen2ForSequenceClassification-2.5" |
| 642 | + model = AutoModelForSequenceClassification.from_pretrained(model_id, dtype="float32") |
| 643 | + base_param_names = [f"base_model.{n}" for n, _ in model.named_parameters()] |
| 644 | + |
| 645 | + dataset = load_dataset("trl-internal-testing/zen", "conversational_implicit_prompt_preference", split="train") |
| 646 | + |
| 647 | + # Initialize the trainer, p-tuning doesn't support gradient checkpointing |
| 648 | + # transformers 5.23 renamed `gradient_checkpointing*` to `activation_checkpointing*` (see #8134) |
| 649 | + if Version(transformers.__version__) >= Version("5.23.0"): |
| 650 | + checkpointing_args = {"activation_checkpointing": False} |
| 651 | + else: |
| 652 | + checkpointing_args = {"gradient_checkpointing": False} |
| 653 | + training_args = RewardConfig(output_dir=self.tmp_dir, report_to="none", **checkpointing_args) |
| 654 | + if peft_type == "prompt_tuning": |
| 655 | + peft_config = PromptTuningConfig(task_type=TaskType.SEQ_CLS, num_virtual_tokens=4) |
| 656 | + elif peft_type == "prefix_tuning": |
| 657 | + peft_config = PrefixTuningConfig(task_type=TaskType.SEQ_CLS, num_virtual_tokens=4) |
| 658 | + elif peft_type == "prompt_encoder": |
| 659 | + peft_config = PromptEncoderConfig(task_type=TaskType.SEQ_CLS, num_virtual_tokens=4) |
| 660 | + trainer = RewardTrainer( |
| 661 | + model=model_id, |
| 662 | + args=training_args, |
| 663 | + train_dataset=dataset, |
| 664 | + peft_config=peft_config, |
| 665 | + ) |
| 666 | + |
| 667 | + previous_trainable_params = {n: param.clone() for n, param in trainer.model.named_parameters()} |
| 668 | + |
| 669 | + trainer.train() |
| 670 | + |
| 671 | + assert trainer.state.log_history[-1]["train_loss"] is not None |
| 672 | + |
| 673 | + # Check that the peft params have changed and the base model params have not changed |
| 674 | + for n, param in previous_trainable_params.items(): |
| 675 | + new_param = trainer.model.get_parameter(n) |
| 676 | + if n in base_param_names: # We expect the base model params to be the same |
| 677 | + torch.testing.assert_close(param, new_param, msg=f"Parameter {n} has changed.") |
| 678 | + elif "original_module" not in n: # We expect the peft params to be different (except the frozen head) |
| 679 | + assert not torch.equal(param, new_param), f"Parameter {n} has not changed." |
| 680 | + |
624 | 681 | @pytest.mark.parametrize( |
625 | 682 | "chosen_column,rejected_column,expect_deprecation_warning", |
626 | 683 | [ |
|
0 commit comments