From bc1c9c20e3cadb2a60163fa73370161a806124f0 Mon Sep 17 00:00:00 2001 From: Wing Lian Date: Mon, 13 Jan 2025 10:44:11 -0500 Subject: [PATCH] assume empty lora dropout means 0.0 and add tests (#2243) * assume empty lora dropout means 0.0 and add tests * remove un-necessary arg * refactor based on pr feedback: * chore: lint --- .../config/models/input/v0_4_1/__init__.py | 7 ++ tests/test_lora.py | 69 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 tests/test_lora.py diff --git a/src/axolotl/utils/config/models/input/v0_4_1/__init__.py b/src/axolotl/utils/config/models/input/v0_4_1/__init__.py index bb88a0baa..19ce7b18c 100644 --- a/src/axolotl/utils/config/models/input/v0_4_1/__init__.py +++ b/src/axolotl/utils/config/models/input/v0_4_1/__init__.py @@ -367,6 +367,13 @@ class LoraConfig(BaseModel): loraplus_lr_embedding = float(loraplus_lr_embedding) return loraplus_lr_embedding + @model_validator(mode="before") + @classmethod + def validate_lora_dropout(cls, data): + if data.get("adapter") is not None and data.get("lora_dropout") is None: + data["lora_dropout"] = 0.0 + return data + class ReLoRAConfig(BaseModel): """ReLoRA configuration subset""" diff --git a/tests/test_lora.py b/tests/test_lora.py new file mode 100644 index 000000000..b917ff3f9 --- /dev/null +++ b/tests/test_lora.py @@ -0,0 +1,69 @@ +""" +tests for loading loras +""" +from axolotl.utils.config import normalize_config, validate_config +from axolotl.utils.dict import DictDefault +from axolotl.utils.models import load_model, load_tokenizer + +# pylint: disable=duplicate-code +minimal_config = DictDefault( + { + "base_model": "HuggingFaceTB/SmolLM2-135M", + "learning_rate": 0.000001, + "datasets": [ + { + "path": "mhenrichsen/alpaca_2k_test", + "type": "alpaca", + } + ], + "micro_batch_size": 1, + "gradient_accumulation_steps": 1, + } +) + + +class TestLoRALoad: + """ + Test class for loading LoRA weights + """ + + def test_load_lora_weights(self): + cfg = DictDefault( + { + "base_model": "HuggingFaceTB/SmolLM2-135M", + "adapter": "lora", + "lora_r": 8, + "lora_alpha": 16, + "lora_dropout": 0.0, + "lora_target_linear": True, + "micro_batch_size": 1, + "gradient_accumulation_steps": 1, + "sequence_len": 1024, + } + | minimal_config + ) + cfg = validate_config(cfg) + normalize_config(cfg) + tokenizer = load_tokenizer(cfg) + load_model(cfg, tokenizer) + + def test_load_lora_weights_empty_dropout(self): + cfg = DictDefault( + { + "base_model": "HuggingFaceTB/SmolLM2-135M", + "adapter": "lora", + "lora_r": 8, + "lora_alpha": 16, + "lora_dropout": None, + "lora_target_linear": True, + "micro_batch_size": 1, + "gradient_accumulation_steps": 1, + "sequence_len": 1024, + } + | minimal_config + ) + cfg = validate_config(cfg) + normalize_config(cfg) + assert cfg.lora_dropout == 0.0 + tokenizer = load_tokenizer(cfg) + load_model(cfg, tokenizer)