Skip to content

Commit a8bd8dd

Browse files
committed
Add config_utils tests
Signed-off-by: Angel Luu <angel.luu@us.ibm.com>
1 parent 537215f commit a8bd8dd

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

tests/utils/test_config_utils.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Copyright The FMS HF Tuning Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# SPDX-License-Identifier: Apache-2.0
16+
# https://spdx.dev/learn/handling-license-info/
17+
18+
# Standard
19+
import base64
20+
import os
21+
import pickle
22+
23+
# Third Party
24+
import pytest
25+
from peft import LoraConfig, PromptTuningConfig
26+
27+
28+
# Local
29+
from tuning.utils import config_utils
30+
from tuning.config import peft_config
31+
from tests.build.test_utils import HAPPY_PATH_DUMMY_CONFIG_PATH
32+
33+
def test_get_hf_peft_config_returns_None_for_FT():
34+
expected_config = None
35+
assert expected_config == config_utils.get_hf_peft_config("", None, "")
36+
37+
def test_get_hf_peft_config_returns_Lora_config_correctly():
38+
# Test that when a value is not defined, the default values are used
39+
# Default values: r=8, lora_alpha=32, lora_dropout=0.05, target_modules=["q_proj", "v_proj"]
40+
tuning_config = peft_config.LoraConfig(r=3, lora_alpha=3)
41+
42+
config = config_utils.get_hf_peft_config("CAUSAL_LM", tuning_config, "")
43+
assert isinstance(config, LoraConfig)
44+
assert config.task_type == "CAUSAL_LM"
45+
assert config.r == 3
46+
assert config.lora_alpha == 3
47+
assert config.lora_dropout == 0.05 # default value from peft_config.LoraConfig
48+
assert config.target_modules == {'q_proj', 'v_proj'} # default value from peft_config.LoraConfig
49+
50+
# Test that when target_modules is ["all-linear"], we convert it to str type "all-linear"
51+
tuning_config = peft_config.LoraConfig(r=234, target_modules=["all-linear"])
52+
53+
config = config_utils.get_hf_peft_config("CAUSAL_LM", tuning_config, "")
54+
assert isinstance(config, LoraConfig)
55+
assert config.r == 234
56+
assert config.target_modules == "all-linear"
57+
assert config.lora_dropout == 0.05 # default value from peft_config.LoraConfig
58+
59+
def test_get_hf_peft_config_returns_PT_config_correctly():
60+
# Test that the prompt tuning config is set properly for each field
61+
# when a value is not defined, the default values are used
62+
# Default values:
63+
# prompt_tuning_init="TEXT",
64+
# prompt_tuning_init_text="Classify if the tweet is a complaint or not:"
65+
tuning_config = peft_config.PromptTuningConfig(num_virtual_tokens=12)
66+
67+
config = config_utils.get_hf_peft_config("CAUSAL_LM", tuning_config, "foo/bar/path")
68+
assert isinstance(config, PromptTuningConfig)
69+
assert config.task_type == "CAUSAL_LM"
70+
assert config.prompt_tuning_init == "TEXT"
71+
assert config.num_virtual_tokens == 12
72+
assert config.prompt_tuning_init_text == "Classify if the tweet is a complaint or not:"
73+
assert config.tokenizer_name_or_path == "foo/bar/path"
74+
75+
# Test that tokenizer path is allowed to be None only when prompt_tuning_init is not TEXT
76+
tuning_config = peft_config.PromptTuningConfig(prompt_tuning_init="RANDOM")
77+
config = config_utils.get_hf_peft_config(None, tuning_config, None)
78+
assert isinstance(config, PromptTuningConfig)
79+
assert config.tokenizer_name_or_path is None
80+
81+
tuning_config = peft_config.PromptTuningConfig(prompt_tuning_init="TEXT")
82+
with pytest.raises(ValueError) as err:
83+
config_utils.get_hf_peft_config(None, tuning_config, None)
84+
assert "tokenizer_name_or_path can't be None" in err.value
85+
86+
87+
def test_create_tuning_config():
88+
# Test that LoraConfig is created for peft_method Lora
89+
# and fields are set properly
90+
tune_config = config_utils.create_tuning_config("lora", foo= "x", r= 234)
91+
assert isinstance(tune_config, peft_config.LoraConfig)
92+
assert tune_config.r == 234
93+
assert tune_config.lora_alpha == 32
94+
assert tune_config.lora_dropout == 0.05
95+
96+
# Test that PromptTuningConfig is created for peft_method pt
97+
# and fields are set properly
98+
tune_config = config_utils.create_tuning_config("pt", foo= "x", prompt_tuning_init= "RANDOM")
99+
assert isinstance(tune_config, peft_config.PromptTuningConfig)
100+
assert tune_config.prompt_tuning_init == "RANDOM"
101+
102+
# Test that None is created for peft_method "None" or None
103+
# and fields are set properly
104+
tune_config = config_utils.create_tuning_config("None", foo= "x")
105+
assert tune_config is None
106+
107+
tune_config = config_utils.create_tuning_config(None, foo= "x")
108+
assert tune_config is None
109+
110+
# Test that this function does not recognize any other peft_method
111+
with pytest.raises(AssertionError) as err:
112+
tune_config = config_utils.create_tuning_config("hello", foo = "x")
113+
assert err.value == "peft config hello not defined in peft.py"
114+
115+
def test_update_config_can_handle_dot_for_nested_field():
116+
# Test update_config allows nested field
117+
config = peft_config.LoraConfig(r = 5)
118+
assert config.lora_alpha == 32 # default value is 32
119+
120+
# update lora_alpha to 98
121+
kwargs = {'LoraConfig.lora_alpha': 98}
122+
config_utils.update_config(config, **kwargs)
123+
assert config.lora_alpha == 98
124+
125+
# update an unknown field
126+
kwargs = {'LoraConfig.foobar': 98}
127+
config_utils.update_config(config, **kwargs) # nothing happens
128+
129+
def test_update_config_can_handle_multiple_config_updates():
130+
# update a tuple of configs
131+
config = (peft_config.LoraConfig(r = 5), peft_config.LoraConfig(r = 7))
132+
kwargs = {'r': 98}
133+
config_utils.update_config(config, **kwargs)
134+
assert config[0].r == 98
135+
assert config[1].r == 98
136+
137+
def test_get_json_config_can_load_from_path_or_envvar():
138+
# Load from path
139+
if "SFT_TRAINER_CONFIG_JSON_ENV_VAR" in os.environ:
140+
del os.environ['SFT_TRAINER_CONFIG_JSON_ENV_VAR']
141+
os.environ["SFT_TRAINER_CONFIG_JSON_PATH"] = HAPPY_PATH_DUMMY_CONFIG_PATH
142+
143+
job_config = config_utils.get_json_config()
144+
assert job_config is not None
145+
assert job_config["model_name_or_path"] == "bigscience/bloom-560m"
146+
147+
# Load from envvar
148+
config_json = {'model_name_or_path': 'foobar'}
149+
message_bytes = pickle.dumps(config_json)
150+
base64_bytes = base64.b64encode(message_bytes)
151+
encoded_json = base64_bytes.decode("ascii")
152+
os.environ["SFT_TRAINER_CONFIG_JSON_ENV_VAR"] = encoded_json
153+
154+
job_config = config_utils.get_json_config()
155+
assert job_config is not None
156+
assert job_config["model_name_or_path"] == "foobar"

0 commit comments

Comments
 (0)