Skip to content

Commit 54eb85c

Browse files
authored
Merge branch 'main' into kylesayrs/remove-leave_enabled
2 parents 2bd0f6e + 29ddedb commit 54eb85c

File tree

24 files changed

+192
-471
lines changed

24 files changed

+192
-471
lines changed

.github/workflows/test-check-transformers.yaml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,41 @@ env:
1515
CLEARML_API_SECRET_KEY: ${{ secrets.CLEARML_API_SECRET_KEY }}
1616

1717
jobs:
18+
detect-changes:
19+
runs-on: ubuntu-latest
20+
21+
outputs:
22+
changes-present: ${{ steps.changed-files.outputs.any_modified }}
23+
24+
steps:
25+
- name: Checkout
26+
uses: actions/checkout@v4
27+
with:
28+
fetch-depth: 0
29+
- name: Get changed files
30+
id: changed-files
31+
uses: tj-actions/changed-files@v45
32+
with:
33+
files: |
34+
**
35+
!examples/**
36+
!tests/e2e/**
37+
!tests/lmeval/**
38+
!tests/examples/**
39+
!**/*.md
40+
!.github/**
41+
.github/workflows/test-check-transformers.yaml
42+
43+
- name: Log relevant output
44+
run: |
45+
echo "changes-present: ${{ steps.changed-files.outputs.any_modified }}"
46+
echo "all modified files: ${{ steps.changed-files.outputs.all_modified_files }}"
47+
shell: bash
48+
1849
transformers-tests:
50+
needs: [detect-changes]
1951
runs-on: gcp-k8s-vllm-l4-solo
20-
if: contains(github.event.pull_request.labels.*.name, 'ready') || github.event_name == 'push'
52+
if: (contains(github.event.pull_request.labels.*.name, 'ready') || github.event_name == 'push') && needs.detect-changes.outputs.changes-present == 'true'
2153
steps:
2254
- uses: actions/setup-python@v5
2355
with:

src/llmcompressor/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
create_session,
4141
finalize,
4242
initialize,
43-
pre_initialize_structure,
4443
reset_session,
4544
)
4645
from llmcompressor.entrypoints import Oneshot, oneshot

src/llmcompressor/core/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
create_session,
1616
finalize,
1717
initialize,
18-
pre_initialize_structure,
1918
reset_session,
2019
)
2120
from llmcompressor.core.state import Data, Hardware, ModifiedState, State
@@ -36,7 +35,6 @@
3635
"create_session",
3736
"active_session",
3837
"reset_session",
39-
"pre_initialize_structure",
4038
"initialize",
4139
"finalize",
4240
"apply",

src/llmcompressor/core/events/event.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class EventType(Enum):
2727
The purpose of each EventType is to trigger the corresponding
2828
modifier callback during training or post training pipelines.
2929
30-
:param PRE_INIT: Event type for pre-initialization.
3130
:param INITIALIZE: Event type for initialization.
3231
:param FINALIZE: Event type for finalization.
3332
:param BATCH_START: Event type for the start of a batch.
@@ -38,7 +37,6 @@ class EventType(Enum):
3837
"""
3938

4039
# training lifecycle
41-
PRE_INIT = "pre_init"
4240
INITIALIZE = "initialize"
4341
FINALIZE = "finalize"
4442

@@ -51,35 +49,6 @@ class EventType(Enum):
5149
OPTIM_PRE_STEP = "optim_pre_step"
5250
OPTIM_POST_STEP = "optim_post_step"
5351

54-
def order(self) -> int:
55-
"""
56-
Returns the priority order of the current EventType.
57-
Lower values have higher priority.
58-
59-
:raises ValueError: if the event type is invalid.
60-
:return: The order of the event type, lower has higher priority.
61-
:rtype: int
62-
"""
63-
if self == EventType.PRE_INIT:
64-
return 0
65-
elif self == EventType.INITIALIZE:
66-
return 10
67-
elif self == EventType.FINALIZE:
68-
return 20
69-
elif self == EventType.BATCH_START:
70-
return 100
71-
elif self == EventType.LOSS_CALCULATED:
72-
return 110
73-
elif self == EventType.OPTIM_PRE_STEP:
74-
return 120
75-
elif self == EventType.OPTIM_POST_STEP:
76-
return 130
77-
elif self == EventType.BATCH_END:
78-
return 140
79-
else:
80-
logger.error("Invalid event type: {}", self)
81-
raise ValueError(f"Invalid event type {self}")
82-
8352

8453
@dataclass
8554
class Event:

src/llmcompressor/core/lifecycle.py

Lines changed: 24 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
)
1919
from llmcompressor.core.state import State
2020
from llmcompressor.modifiers import StageModifiers
21-
from llmcompressor.recipe import RecipeContainer
21+
from llmcompressor.recipe import (
22+
RecipeArgsInput,
23+
RecipeContainer,
24+
RecipeInput,
25+
RecipeStageInput,
26+
)
2227

2328
__all__ = ["CompressionLifecycle"]
2429

@@ -38,7 +43,7 @@ class CompressionLifecycle:
3843
:type event_lifecycle: Optional[EventLifecycle]
3944
"""
4045

41-
state: Optional[State] = None
46+
state: State = field(default_factory=State)
4247
recipe_container: RecipeContainer = field(default_factory=RecipeContainer)
4348
modifiers: List[StageModifiers] = field(default_factory=list)
4449
event_lifecycle: Optional[EventLifecycle] = None
@@ -62,63 +67,35 @@ def reset(self):
6267
except Exception as e:
6368
logger.warning(f"Exception during finalizing modifier: {e}")
6469

65-
self.state = None
66-
self.recipe_container = RecipeContainer()
67-
self.modifiers = []
68-
self.event_lifecycle = None
69-
70-
self.initialized_ = False
71-
self.finalized = False
70+
self.__init__()
7271
logger.info("Compression lifecycle reset")
7372

74-
def pre_initialize_structure(self, **kwargs) -> List[Any]:
75-
"""
76-
Pre-initialize the structure of the compression lifecycle.
77-
78-
:param kwargs: Additional arguments to update the state with
79-
:return: List of data returned from pre-initialization of modifiers
80-
:rtype: List[Any]
81-
"""
82-
logger.debug("Pre-initializing structure")
83-
self._check_create_state()
84-
extras = self.state.update(**kwargs)
85-
extras = self.recipe_container.update(**extras)
86-
87-
self._check_compile_recipe()
88-
mod_data = []
89-
for mod in self.modifiers:
90-
data = mod.pre_initialize_structure(state=self.state, **extras)
91-
logger.debug("Pre-initialized modifier: {}", mod)
92-
if data is not None:
93-
mod_data.append(data)
94-
95-
applied_stage_names = [mod.unique_id for mod in self.modifiers if mod.applied]
96-
self.recipe_container.update_applied_stages(applied_stage_names)
97-
logger.info(
98-
"Compression lifecycle structure pre-initialized for {} modifiers",
99-
len(self.modifiers),
100-
)
101-
102-
return mod_data
103-
104-
def initialize(self, **kwargs) -> List[Any]:
73+
def initialize(
74+
self,
75+
recipe: Optional[RecipeInput] = None,
76+
recipe_stage: Optional[RecipeStageInput] = None,
77+
recipe_args: Optional[RecipeArgsInput] = None,
78+
**kwargs,
79+
) -> List[Any]:
10580
"""
10681
Initialize the compression lifecycle.
10782
10883
:param kwargs: Additional arguments to update the state with
10984
:return: List of data returned from initialization of modifiers
11085
:rtype: List[Any]
11186
"""
112-
logger.debug("Initializing compression lifecycle")
113-
self._check_create_state()
114-
extras = self.state.update(**kwargs)
115-
extras = self.recipe_container.update(**extras)
87+
self.state.update(**kwargs)
88+
if self.initialized_: # TODO: do not initialize twice
89+
return
11690

117-
self._check_compile_recipe()
91+
logger.debug("Initializing compression lifecycle")
92+
self.recipe_container.append(recipe, recipe_stage, recipe_args)
93+
self.modifiers = self.recipe_container.get_modifiers()
11894
self._set_model_layer_prefix()
95+
11996
mod_data = []
12097
for mod in self.modifiers:
121-
data = mod.initialize(state=self.state, **extras)
98+
data = mod.initialize(state=self.state, **kwargs)
12299
logger.debug("Initialized modifier: {}", mod)
123100
if data is not None:
124101
mod_data.append(data)
@@ -185,7 +162,7 @@ def event(self, event_type: EventType, **kwargs) -> List[Any]:
185162
logger.error("Cannot invoke event after finalizing")
186163
raise ValueError("Cannot invoke event after finalizing")
187164

188-
if event_type in [EventType.PRE_INIT, EventType.INITIALIZE, EventType.FINALIZE]:
165+
if event_type in [EventType.INITIALIZE, EventType.FINALIZE]:
189166
logger.error(
190167
"Cannot invoke {} event. Use the corresponding method instead.",
191168
event_type,
@@ -223,30 +200,6 @@ def event(self, event_type: EventType, **kwargs) -> List[Any]:
223200

224201
return mod_data
225202

226-
def _check_create_state(self):
227-
if self.state is not None:
228-
return
229-
230-
logger.debug("Creating new State instance for compression lifecycle")
231-
self.state = State()
232-
logger.info("State created for compression lifecycle")
233-
234-
def _check_compile_recipe(self):
235-
if not self.recipe_container.check_compile_recipe():
236-
return
237-
238-
logger.debug(
239-
"Compiling recipe and creating modifiers for compression lifecycle"
240-
)
241-
self.modifiers = self.recipe_container.compiled_recipe.create_modifier()
242-
for mod in self.modifiers:
243-
if mod.unique_id in self.recipe_container.applied_stages:
244-
mod.applied = True
245-
logger.info(
246-
"Recipe compiled and {} modifiers created",
247-
len(self.modifiers),
248-
)
249-
250203
def _check_setup_event_lifecycle(self, event_type: EventType):
251204
if self.event_lifecycle is not None:
252205
return

src/llmcompressor/core/session.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -65,45 +65,6 @@ def state(self) -> State:
6565
"""
6666
return self._lifecycle.state
6767

68-
def pre_initialize_structure(
69-
self,
70-
model: Any,
71-
recipe: Union[str, List[str], Recipe, List[Recipe], None] = None,
72-
recipe_stage: Union[str, List[str], None] = None,
73-
recipe_args: Union[Dict[str, Any], List[Dict[str, Any]], None] = None,
74-
**kwargs,
75-
) -> ModifiedState:
76-
"""
77-
A method to pre-initialize the structure of the model for compression.
78-
This will run the pre-initialize structure method for each modifier in the
79-
session's lifecycle. This will also set the session's state to the
80-
pre-initialized state. Takes care of cases when the model(s) structure
81-
has been previously modified by a modifier.
82-
83-
:param model: the model to pre-initialize the structure for
84-
:param recipe: the recipe to use for the compression, can be a path to a
85-
recipe file, a raw recipe string, a recipe object, or a list
86-
of recipe objects.
87-
:param recipe_stage: the stage to use for the compression
88-
:param recipe_args: the args to use for overriding the recipe defaults
89-
:return: A ModifiedState instance holding the modified model and modifier_data
90-
after pre-initializing the structure
91-
"""
92-
mod_data = self._lifecycle.pre_initialize_structure(
93-
model=model,
94-
recipe=recipe,
95-
recipe_stage=recipe_stage,
96-
recipe_args=recipe_args,
97-
**kwargs,
98-
)
99-
100-
return ModifiedState(
101-
model=self.state.model,
102-
optimizer=None,
103-
loss=None,
104-
modifier_data=mod_data,
105-
)
106-
10768
def initialize(
10869
self,
10970
recipe: Union[str, List[str], "Recipe", List["Recipe"], None] = None,

src/llmcompressor/core/session_functions.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"create_session",
1212
"active_session",
1313
"reset_session",
14-
"pre_initialize_structure",
1514
"initialize",
1615
"finalize",
1716
"callbacks",
@@ -59,16 +58,6 @@ def reset_session():
5958
session._lifecycle.reset()
6059

6160

62-
def pre_initialize_structure(**kwargs):
63-
"""
64-
A method to pre-initialize the structure of the model for the active session
65-
66-
:param kwargs: the kwargs to pass to the active session's pre-initialize-structure
67-
method
68-
"""
69-
active_session().pre_initialize_structure(**kwargs)
70-
71-
7261
def initialize(
7362
recipe: Union[str, List[str], "Recipe", List["Recipe"], None] = None,
7463
recipe_stage: Union[str, List[str], None] = None,
@@ -156,7 +145,7 @@ def event(cls, event_type: EventType, **kwargs) -> ModifiedState:
156145
:param kwargs: additional kwargs to pass to the current session's event method
157146
:return: the modified state of the active session after invoking the event
158147
"""
159-
if event_type in [EventType.PRE_INIT, EventType.INITIALIZE, EventType.FINALIZE]:
148+
if event_type in [EventType.INITIALIZE, EventType.FINALIZE]:
160149
raise ValueError(
161150
f"Cannot invoke {event_type} event. "
162151
f"Use the corresponding method instead."

src/llmcompressor/modifiers/interface.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ class ModifierInterface(ABC):
1111
Defines the contract that all modifiers must implement
1212
"""
1313

14-
@property
15-
@abstractmethod
16-
def initialized_structure(self) -> bool:
17-
"""
18-
:return: True if the modifier structure has been
19-
applied to the model
20-
"""
21-
raise NotImplementedError()
22-
2314
@property
2415
@abstractmethod
2516
def initialized(self) -> bool:
@@ -58,16 +49,6 @@ def calculate_end(self) -> float:
5849
"""
5950
raise NotImplementedError()
6051

61-
@abstractmethod
62-
def pre_initialize_structure(self, state: State, **kwargs):
63-
"""
64-
Apply the modifier structure to the model
65-
66-
:param state: The current state of the model
67-
:param kwargs: Additional arguments for the modifier
68-
"""
69-
raise NotImplementedError()
70-
7152
@abstractmethod
7253
def initialize(self, state: State, **kwargs):
7354
"""

0 commit comments

Comments
 (0)