Skip to content

Commit 853a994

Browse files
FIX Call super().__post_init__() in CPTConfig and RandLoraConfig (#3365)
CPTConfig and RandLoraConfig override __post_init__ without calling super().__post_init__(), so PeftConfig.__post_init__ never runs for them. As a result the peft_version field is left as None (instead of being auto-filled) and the task_type validation in the base class is skipped. Every other tuner config that overrides __post_init__ calls super(); add the call (first, matching the convention) so these two behave the same and record peft_version in their saved adapter_config.json. Follow-up to adding the missing configs to ALL_CONFIG_CLASSES: - CPTConfig requires task_type=CAUSAL_LM to construct and only supports that task type, so pin it in mandatory_kwargs and skip it in the generic valid/invalid task_type tests (it's still covered by the round-trip tests, which is what would have caught the original __post_init__ omission). - OFTConfig's from_pretrained back-compat guard fires before the generic load path, so skip it in the four from_pretrained-based tests. All other matrix tests now exercise both configs.
1 parent 1ef3b4b commit 853a994

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/peft/tuners/cpt/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def __post_init__(self):
7272
"""
7373
Post-initialization hook to set additional attributes after the config is initialized.
7474
"""
75+
super().__post_init__()
7576
# CPT-specific static attributes
7677
self.is_prompt_learning = True # Indicates that CPT is a prompt-learning method.
7778
self.num_layers = None # Number of layers (optional, not always required).

src/peft/tuners/randlora/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ class RandLoraConfig(PeftConfig):
186186
)
187187

188188
def __post_init__(self):
189+
super().__post_init__()
189190
self.peft_type = PeftType.RANDLORA
190191
self.target_modules = (
191192
set(self.target_modules) if isinstance(self.target_modules, list) else self.target_modules

tests/test_config.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
C3AConfig,
3030
CartridgeConfig,
3131
CPTConfig,
32+
DeloraConfig,
3233
FourierFTConfig,
3334
FrodConfig,
3435
GraloraConfig,
@@ -53,13 +54,16 @@
5354
PromptEncoderConfig,
5455
PromptTuningConfig,
5556
PsoftConfig,
57+
PveraConfig,
58+
RandLoraConfig,
5659
RoadConfig,
5760
ShiraConfig,
5861
TaskType,
5962
TinyLoraConfig,
6063
TrainableTokensConfig,
6164
VBLoRAConfig,
6265
VeraConfig,
66+
WaveFTConfig,
6367
XLoraConfig,
6468
)
6569

@@ -106,6 +110,12 @@ class TestingCommitHashError(Exception):
106110
(VeraConfig, {}),
107111
(VBLoRAConfig, {}),
108112
(XLoraConfig, {"hidden_size": 32, "adapters": {}}),
113+
(CPTConfig, {"task_type": "CAUSAL_LM"}),
114+
(RandLoraConfig, {}),
115+
(DeloraConfig, {}),
116+
(OFTConfig, {}),
117+
(PveraConfig, {}),
118+
(WaveFTConfig, {}),
109119
)
110120

111121

@@ -132,13 +142,17 @@ def test_valid_task_type(self, config_class, mandatory_kwargs, valid_task_type):
132142
r"""
133143
Test if all configs work correctly for all valid task types
134144
"""
145+
if config_class is CPTConfig:
146+
pytest.skip("CPTConfig only supports the CAUSAL_LM task type (validated in its __post_init__)")
135147
config_class(task_type=valid_task_type, **mandatory_kwargs)
136148

137149
@pytest.mark.parametrize("config_class, mandatory_kwargs", ALL_CONFIG_CLASSES)
138150
def test_invalid_task_type(self, config_class, mandatory_kwargs):
139151
r"""
140152
Test if all configs correctly raise the defined error message for invalid task types.
141153
"""
154+
if config_class is CPTConfig:
155+
pytest.skip("CPTConfig validates task_type with a config-specific message in its __post_init__")
142156
invalid_task_type = "invalid-task-type"
143157
with pytest.raises(
144158
ValueError,
@@ -171,6 +185,8 @@ def test_from_pretrained(self, config_class, mandatory_kwargs):
171185
Test if the config is correctly loaded using:
172186
- from_pretrained
173187
"""
188+
if config_class is OFTConfig:
189+
pytest.skip("OFT's from_pretrained back-compat guard fires before the generic load path tested here")
174190
for model_name, revision in PEFT_MODELS_TO_TEST:
175191
# Test we can load config from delta
176192
config_class.from_pretrained(model_name, revision=revision)
@@ -221,6 +237,8 @@ def test_from_pretrained_cache_dir(self, config_class, mandatory_kwargs):
221237
r"""
222238
Test if the config is correctly loaded with extra kwargs
223239
"""
240+
if config_class is OFTConfig:
241+
pytest.skip("OFT's from_pretrained back-compat guard fires before the generic load path tested here")
224242
with tempfile.TemporaryDirectory() as tmp_dirname:
225243
for model_name, revision in PEFT_MODELS_TO_TEST:
226244
# Test we can load config from delta
@@ -239,6 +257,8 @@ def test_save_pretrained_with_runtime_config(self, config_class, mandatory_kwarg
239257
r"""
240258
Test if the config correctly removes runtime config when saving
241259
"""
260+
if config_class is OFTConfig:
261+
pytest.skip("OFT's from_pretrained back-compat guard fires before the generic load path tested here")
242262
with tempfile.TemporaryDirectory() as tmp_dirname:
243263
for model_name, revision in PEFT_MODELS_TO_TEST:
244264
cfg = config_class.from_pretrained(model_name, revision=revision)
@@ -482,6 +502,8 @@ def test_from_pretrained_sanity_check(self, config_class, mandatory_kwargs, tmp_
482502
"""Following up on the previous test about forward compatibility, we *don't* want any random json to be accepted as
483503
a PEFT config. There should be a minimum set of required keys.
484504
"""
505+
if config_class is OFTConfig:
506+
pytest.skip("OFT's from_pretrained back-compat guard fires before the generic load path tested here")
485507
non_peft_json = {"foo": "bar", "baz": 123}
486508
with open(tmp_path / "adapter_config.json", "w") as f:
487509
json.dump(non_peft_json, f)

0 commit comments

Comments
 (0)