Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Version Bumps] May 7, 2024 #165

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions llmtune/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ lora:
r: 32
lora_alpha: 64
lora_dropout: 0.1
target_modules:
- q_proj
- v_proj
- k_proj
- o_proj
- up_proj
- down_proj
- gate_proj
target_modules: "all-linear"
# to target specific modules
# target_modules:
# - q_proj
# - v_proj
# - k_proj
# - o_proj
# - up_proj
# - down_proj
# - gate_proj

# Training -------------------
training:
Expand Down
4 changes: 3 additions & 1 deletion llmtune/pydantic_models/config_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ class LoraConfig(BaseModel):
lora_alpha: Optional[int] = Field(16, description="The alpha parameter for Lora scaling")
bias: Optional[str] = Field("none", description="Bias type for Lora. Can be 'none', 'all' or 'lora_only'")
lora_dropout: Optional[float] = Field(0.1, description="The dropout probability for Lora layers")
target_modules: Optional[List[str]] = Field(None, description="The names of the modules to apply Lora to")
target_modules: Optional[Union[List[str], Literal["all-linear"]]] = Field(
"all-linear", description="The names of the modules to apply Lora to"
)
fan_in_fan_out: Optional[bool] = Field(
False,
description="Flag to indicate if the layer to replace stores weight like (fan_in, fan_out)",
Expand Down
1,708 changes: 863 additions & 845 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ folders = [

[tool.poetry.dependencies]
python = ">=3.9, <=3.12"
transformers = "~4.37.2"
transformers = "~4.40.2"
datasets = "^2.17.0"
peft = "^0.8.2"
pandas = "^2.2.0"
Expand All @@ -44,7 +44,7 @@ einops = "^0.7.0"
bitsandbytes = "^0.42.0"
nltk = "^3.8.1"
accelerate = "^0.27.0"
trl = "~0.7.10"
trl = "~0.8.6"
rouge-score = "^0.1.2"
absl-py = "^2.1.0"
py7zr = "^0.20.8"
Expand Down
18 changes: 6 additions & 12 deletions tests/data/test_ingestor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from unittest.mock import MagicMock, mock_open

import pytest
from unittest.mock import patch, MagicMock, mock_open
from datasets import Dataset

from llmtune.data.ingestor import (
CsvIngestor,
Expand All @@ -9,8 +11,6 @@
get_ingestor,
)

from datasets import Dataset


def test_get_ingestor():
assert isinstance(get_ingestor("json")(""), JsonIngestor)
Expand All @@ -31,9 +31,7 @@ def test_json_ingestor_to_dataset(mocker):


def test_jsonl_ingestor_to_dataset(mocker):
mock_generator = mocker.patch(
"llmtune.data.ingestor.JsonlIngestor._jsonl_generator"
)
mock_generator = mocker.patch("llmtune.data.ingestor.JsonlIngestor._jsonl_generator")
mock_dataset = mocker.patch("llmtune.data.ingestor.Dataset")
JsonlIngestor("").to_dataset()

Expand All @@ -52,9 +50,7 @@ def test_huggingface_to_dataset(mocker):
# Setup
path = "some_path"
ingestor = HuggingfaceIngestor(path)
mock_concatenate_datasets = mocker.patch(
"llmtune.data.ingestor.concatenate_datasets"
)
mock_concatenate_datasets = mocker.patch("llmtune.data.ingestor.concatenate_datasets")
mock_load_dataset = mocker.patch("llmtune.data.ingestor.load_dataset")
mock_dataset = mocker.patch("llmtune.data.ingestor.Dataset")

Expand Down Expand Up @@ -108,9 +104,7 @@ def test_jsonl_ingestor_generator(file_content, expected_output, mocker):
mocker.patch("builtins.open", mock_open(read_data=file_content))
mocker.patch(
"ijson.items",
side_effect=lambda f, prefix, multiple_values: (
iter(expected_output) if multiple_values else iter([])
),
side_effect=lambda f, prefix, multiple_values: (iter(expected_output) if multiple_values else iter([])),
)
ingestor = JsonlIngestor("dummy_path.jsonl")

Expand Down
24 changes: 6 additions & 18 deletions tests/finetune/test_finetune_lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@ def test_lora_finetune_initialization(mocker):
)

# Initialize LoRAFinetune with the sample configuration
lora_finetune = LoRAFinetune(
config=get_sample_config(), directory_helper=MagicMock()
)
lora_finetune = LoRAFinetune(config=get_sample_config(), directory_helper=MagicMock())
# Assertions to ensure that LoRAFinetune is initialized as expected
mock_lora_config.assert_called_once_with(**get_sample_config().lora.model_dump())

assert (
lora_finetune.config == get_sample_config()
), "Configuration should match the input configuration"
assert lora_finetune.config == get_sample_config(), "Configuration should match the input configuration"


def test_model_and_tokenizer_loading(mocker):
Expand All @@ -37,9 +33,7 @@ def test_model_and_tokenizer_loading(mocker):
"llmtune.finetune.lora.AutoModelForCausalLM.from_pretrained",
return_value=MagicMock(),
)
mock_tokenizer = mocker.patch(
"llmtune.finetune.lora.AutoTokenizer.from_pretrained", return_value=MagicMock()
)
mock_tokenizer = mocker.patch("llmtune.finetune.lora.AutoTokenizer.from_pretrained", return_value=MagicMock())
mock_inject_lora = mocker.patch(
"llmtune.finetune.lora.LoRAFinetune._inject_lora",
return_value=None, # _inject_lora doesn't return a value
Expand Down Expand Up @@ -89,18 +83,14 @@ def test_model_finetune(mocker):
"llmtune.finetune.lora.AutoModelForCausalLM.from_pretrained",
return_value=MagicMock(),
)
mocker.patch(
"llmtune.finetune.lora.AutoTokenizer.from_pretrained", return_value=MagicMock()
)
mocker.patch("llmtune.finetune.lora.AutoTokenizer.from_pretrained", return_value=MagicMock())
mocker.patch(
"llmtune.finetune.lora.LoRAFinetune._inject_lora",
return_value=None, # _inject_lora doesn't return a value
)

mock_trainer = mocker.MagicMock()
mock_sft_trainer = mocker.patch(
"llmtune.finetune.lora.SFTTrainer", return_value=mock_trainer
)
mock_sft_trainer = mocker.patch("llmtune.finetune.lora.SFTTrainer", return_value=mock_trainer)

directory_helper = MagicMock()

Expand Down Expand Up @@ -144,9 +134,7 @@ def test_save_model(mocker):
mocker.patch("llmtune.finetune.lora.AutoModelForCausalLM.from_pretrained")

mock_tok = mocker.MagicMock()
mocker.patch(
"llmtune.finetune.lora.AutoTokenizer.from_pretrained", return_value=mock_tok
)
mocker.patch("llmtune.finetune.lora.AutoTokenizer.from_pretrained", return_value=mock_tok)
mocker.patch(
"llmtune.finetune.lora.LoRAFinetune._inject_lora",
return_value=None,
Expand Down
36 changes: 10 additions & 26 deletions tests/inference/test_inference_lora.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
import pytest
from unittest.mock import MagicMock

from datasets import Dataset
from transformers import BitsAndBytesConfig

from llmtune.inference.lora import LoRAInference
from llmtune.utils.save_utils import DirectoryHelper
from test_utils.test_config import get_sample_config # Adjust import path as needed

from transformers import BitsAndBytesConfig


def test_lora_inference_initialization(mocker):
# Mock dependencies
mock_model = mocker.patch(
"llmtune.inference.lora.AutoPeftModelForCausalLM.from_pretrained",
return_value=MagicMock(),
)
mock_tokenizer = mocker.patch(
"llmtune.inference.lora.AutoTokenizer.from_pretrained", return_value=MagicMock()
)
mock_tokenizer = mocker.patch("llmtune.inference.lora.AutoTokenizer.from_pretrained", return_value=MagicMock())

# Mock configuration and directory helper
config = get_sample_config()
dir_helper = MagicMock(
save_paths=MagicMock(results="results_dir", weights="weights_dir")
)
dir_helper = MagicMock(save_paths=MagicMock(results="results_dir", weights="weights_dir"))
test_dataset = Dataset.from_dict(
{
"formatted_prompt": ["prompt1", "prompt2"],
"label_column_name": ["label1", "label2"],
}
)

inference = LoRAInference(
_ = LoRAInference(
test_dataset=test_dataset,
label_column_name="label_column_name",
config=config,
Expand All @@ -45,34 +39,24 @@ def test_lora_inference_initialization(mocker):
device_map=config.model.device_map,
attn_implementation=config.model.attn_implementation,
)
mock_tokenizer.assert_called_once_with(
"weights_dir", device_map=config.model.device_map
)
mock_tokenizer.assert_called_once_with("weights_dir", device_map=config.model.device_map)


def test_infer_all(mocker):
mocker.patch(
"llmtune.inference.lora.AutoPeftModelForCausalLM.from_pretrained",
return_value=MagicMock(),
)
mocker.patch(
"llmtune.inference.lora.AutoTokenizer.from_pretrained", return_value=MagicMock()
)
mocker.patch("llmtune.inference.lora.AutoTokenizer.from_pretrained", return_value=MagicMock())
mocker.patch("os.makedirs")
mock_open = mocker.patch("builtins.open", mocker.mock_open())
mock_csv_writer = mocker.patch("csv.writer")

mock_infer_one = mocker.patch.object(
LoRAInference, "infer_one", return_value="predicted"
)
mock_infer_one = mocker.patch.object(LoRAInference, "infer_one", return_value="predicted")

config = get_sample_config()
dir_helper = MagicMock(
save_paths=MagicMock(results="results_dir", weights="weights_dir")
)
test_dataset = Dataset.from_dict(
{"formatted_prompt": ["prompt1"], "label_column_name": ["label1"]}
)
dir_helper = MagicMock(save_paths=MagicMock(results="results_dir", weights="weights_dir"))
test_dataset = Dataset.from_dict({"formatted_prompt": ["prompt1"], "label_column_name": ["label1"]})

inference = LoRAInference(
test_dataset=test_dataset,
Expand Down
4 changes: 2 additions & 2 deletions tests/qa/test_generics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from unittest.mock import Mock
from pandas import DataFrame
from llmtune.qa.generics import LLMQaTest, QaTestRegistry, LLMTestSuite

from llmtune.qa.generics import LLMQaTest, LLMTestSuite


@pytest.fixture
Expand Down
12 changes: 6 additions & 6 deletions tests/qa/test_qa_tests.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import pytest

from llmtune.qa.qa_tests import (
QaTestRegistry,
LengthTest,
JaccardSimilarityTest,
AdjectivePercent,
DotProductSimilarityTest,
JaccardSimilarityTest,
LengthTest,
NounPercent,
RougeScoreTest,
WordOverlapTest,
VerbPercent,
AdjectivePercent,
NounPercent,
WordOverlapTest,
)


Expand Down
6 changes: 3 additions & 3 deletions tests/test_ablation_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pytest
from pydantic import BaseModel

from llmtune.utils.ablation_utils import (
get_types_from_dict,
generate_permutations,
get_annotation,
get_model_field_type,
validate_and_get_ablations,
get_types_from_dict,
patch_with_permutation,
generate_permutations,
)


Expand Down
9 changes: 2 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import pytest
from unittest.mock import MagicMock, patch
from pathlib import Path
from unittest.mock import patch

from pydantic import ValidationError
from typer.testing import CliRunner

from llmtune.cli.toolkit import app, cli, run_one_experiment
from llmtune.pydantic_models.config_model import Config
from llmtune.cli.toolkit import app, cli

from test_utils.test_config import get_sample_config

runner = CliRunner()

Expand Down