Skip to content

Commit 2d90569

Browse files
FIX: CPT should not be tested with sequence classification (#2507)
PR #2481 added sequence classification tests to PEFT. The test matrix included CPT. However, CPT only supports the task type CAUSAL_LM. These tests still passed but now started failing with: > AttributeError: object has no attribute 'prepare_inputs_for_generation' This is probably a change in transformers but the since causal LM was never meant to work, the actual fix is to remove CPT from the seq cls test matrix. Since CPT automatically changes the task type to CAUSAL_LM, this mistake can be hard to spot. Therefore, this PR also adds a warning if users pass the wrong task type. In the future, this will raise an error.
1 parent 2e39c89 commit 2d90569

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/peft/tuners/cpt/config.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import warnings
1516
from dataclasses import dataclass, field
1617
from typing import Literal, Optional
1718

1819
from peft.config import PromptLearningConfig
19-
from peft.utils import PeftType
20+
from peft.utils import PeftType, TaskType
2021

2122

2223
@dataclass
@@ -79,7 +80,14 @@ def __post_init__(self):
7980
self.num_attention_heads = None # Number of attention heads (if applicable).
8081
self.num_transformer_submodules = 1 # Number of transformer submodules used.
8182
self.peft_type = PeftType.CPT # Specifies that the PEFT type is CPT.
82-
self.task_type = "CAUSAL_LM" # Ensures task type is causal language modeling.
83+
if self.task_type != TaskType.CAUSAL_LM:
84+
# TODO: adjust this to raise an error with PEFT v0.18.0
85+
warnings.warn(
86+
f"{self.__class__.__name__} only supports task_type = {TaskType.CAUSAL_LM.value}, "
87+
"setting it automatically. This will raise an error starting from PEFT v0.18.0.",
88+
FutureWarning,
89+
)
90+
self.task_type = TaskType.CAUSAL_LM # Ensures task type is causal language modeling.
8391

8492
if self.cpt_token_ids is None:
8593
self.cpt_token_ids = [0]

tests/test_cpt.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
TrainingArguments,
2828
)
2929

30-
from peft import CPTConfig, get_peft_model
30+
from peft import CPTConfig, TaskType, get_peft_model
3131

3232

3333
TEMPLATE = {"input": "input: {}", "intra_seperator": " ", "output": "output: {}", "inter_seperator": "\n"}
@@ -227,6 +227,14 @@ def test_model_initialization_random(global_tokenizer, config_random):
227227
assert model is not None, "PEFT model initialization failed"
228228

229229

230+
def test_model_initialization_wrong_task_type_warns():
231+
# TODO: adjust this test to check for an error with PEFT v0.18.0
232+
msg = "CPTConfig only supports task_type = CAUSAL_LM, setting it automatically"
233+
with pytest.warns(FutureWarning, match=msg):
234+
config = CPTConfig(task_type=TaskType.SEQ_CLS)
235+
assert config.task_type == TaskType.CAUSAL_LM
236+
237+
230238
def test_model_training_random(sst_data, global_tokenizer, collator, config_random):
231239
"""Perform a short training run to verify the model and data integration."""
232240

tests/test_seq_classifier.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
AdaLoraConfig,
2020
BOFTConfig,
2121
BoneConfig,
22-
CPTConfig,
2322
FourierFTConfig,
2423
HRAConfig,
2524
IA3Config,
@@ -67,15 +66,6 @@
6766
"r": 2,
6867
},
6968
),
70-
(
71-
CPTConfig,
72-
{
73-
"task_type": "SEQ_CLS",
74-
"cpt_token_ids": [0, 1, 2, 3, 4, 5, 6, 7], # Example token IDs for testing
75-
"cpt_mask": [1, 1, 1, 1, 1, 1, 1, 1],
76-
"cpt_tokens_type_mask": [1, 2, 2, 2, 3, 3, 4, 4],
77-
},
78-
),
7969
(
8070
FourierFTConfig,
8171
{

0 commit comments

Comments
 (0)