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

[Bugfix] Add fix to preserve modifier order when passed as a list #26

Merged
merged 3 commits into from
Jul 22, 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
2 changes: 1 addition & 1 deletion src/llmcompressor/recipe/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ def create_recipe_string_from_modifiers(
}
}
}
recipe_str: str = yaml.dump(recipe_dict)
recipe_str: str = yaml.dump(recipe_dict, sort_keys=False)
return recipe_str


Expand Down
44 changes: 44 additions & 0 deletions tests/llmcompressor/recipe/test_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import pytest
import yaml

from llmcompressor.modifiers import Modifier
from llmcompressor.modifiers.obcq.base import SparseGPTModifier
from llmcompressor.recipe import Recipe
from llmcompressor.recipe.recipe import create_recipe_string_from_modifiers
from tests.llmcompressor.helpers import valid_recipe_strings


Expand Down Expand Up @@ -96,3 +98,45 @@ def test_recipe_can_be_created_from_modifier_instances():
):
assert isinstance(actual_modifier, type(expected_modifier))
assert actual_modifier.dict() == expected_modifier.dict()


class A_FirstDummyModifier(Modifier):
def model_dump(self):
return {}


class B_SecondDummyModifier(Modifier):
def model_dump(self):
return {}


def test_create_recipe_string_from_modifiers_with_default_group_name():
modifiers = [B_SecondDummyModifier(), A_FirstDummyModifier()]
expected_recipe_str = (
"DEFAULT_stage:\n"
" DEFAULT_modifiers:\n"
" B_SecondDummyModifier: {}\n"
" A_FirstDummyModifier: {}\n"
)
actual_recipe_str = create_recipe_string_from_modifiers(modifiers)
assert actual_recipe_str == expected_recipe_str


def test_create_recipe_string_from_modifiers_with_custom_group_name():
modifiers = [B_SecondDummyModifier(), A_FirstDummyModifier()]
group_name = "custom"
expected_recipe_str = (
"custom_stage:\n"
" DEFAULT_modifiers:\n"
" B_SecondDummyModifier: {}\n"
" A_FirstDummyModifier: {}\n"
)
actual_recipe_str = create_recipe_string_from_modifiers(modifiers, group_name)
assert actual_recipe_str == expected_recipe_str


def test_create_recipe_string_from_modifiers_with_empty_modifiers():
modifiers = []
expected_recipe_str = "DEFAULT_stage:\n" " DEFAULT_modifiers: {}\n"
actual_recipe_str = create_recipe_string_from_modifiers(modifiers)
assert actual_recipe_str == expected_recipe_str
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path

import pytest
from compressed_tensors.compressors.utils import tensor_follows_mask_structure
from compressed_tensors.utils import tensor_follows_mask_structure
from parameterized import parameterized_class

from llmcompressor.core import reset_session
Expand Down
Loading