llmcompressor.core.session
Compression session management for LLM compression workflows.
Provides the main CompressionSession class for managing compression workflows, including lifecycle management, event handling, callback registration, and state tracking.
Classes:
-
CompressionSession
–A session for compression that holds the lifecycle
CompressionSession
A session for compression that holds the lifecycle and state for the current compression session
Methods:
-
event
–Invoke an event for current CompressionSession.
-
finalize
–Finalize the session for compression. This will run the finalize method
-
get_serialized_recipe
–:return: serialized string of the current compiled recipe
-
initialize
–Initialize the session for compression. This will run the initialize method
-
log
–Log model and loss information for the current event type
-
reset
–Reset the session to its initial state
-
reset_stage
–Reset the session for starting a new stage, recipe and model stays intact
Attributes:
-
lifecycle
(CompressionLifecycle
) –Lifecycle is used to keep track of where we are in the compression
-
state
(State
) –State of the current compression session. State instance
Source code in llmcompressor/core/session.py
lifecycle property
Lifecycle is used to keep track of where we are in the compression process and what modifiers are active. It also Provides the ability to invoke events on the lifecycle.
Returns:
-
CompressionLifecycle
–the lifecycle for the session
state property
State of the current compression session. State instance is used to store all information such as the recipe, model optimizer, data, etc. that is needed for compression.
Returns:
-
State
–the current state of the session
event
event(
event_type: EventType,
batch_data: Optional[Any] = None,
loss: Optional[Any] = None,
**kwargs
) -> ModifiedState
Invoke an event for current CompressionSession.
Parameters:
-
event_type
EventType
) –the event type to invoke
-
batch_data
Optional[Any]
, default:None
) –the batch data to use for the event
-
loss
Optional[Any]
, default:None
) –the loss to use for the event if any
-
kwargs
additional kwargs to pass to the lifecycle's event method
Returns:
-
ModifiedState
–the modified state of the session after invoking the event
Source code in llmcompressor/core/session.py
finalize
Finalize the session for compression. This will run the finalize method for each modifier in the session's lifecycle. This will also set the session's state to the finalized state.
Parameters:
-
kwargs
additional kwargs to pass to the lifecycle's finalize method
Returns:
-
ModifiedState
–the modified state of the session after finalizing
Source code in llmcompressor/core/session.py
get_serialized_recipe
Returns:
-
Optional[str]
–serialized string of the current compiled recipe
Source code in llmcompressor/core/session.py
initialize
initialize(
recipe: Union[
str, List[str], Recipe, List[Recipe], None
] = None,
recipe_stage: Union[str, List[str], None] = None,
recipe_args: Union[Dict[str, Any], None] = None,
model: Optional[Any] = None,
teacher_model: Optional[Any] = None,
optimizer: Optional[Any] = None,
attach_optim_callbacks: bool = True,
train_data: Optional[Any] = None,
val_data: Optional[Any] = None,
test_data: Optional[Any] = None,
calib_data: Optional[Any] = None,
copy_data: bool = True,
start: Optional[float] = None,
steps_per_epoch: Optional[int] = None,
batches_per_step: Optional[int] = None,
loggers: Union[
None, LoggerManager, List[BaseLogger]
] = None,
**kwargs
) -> ModifiedState
Initialize the session for compression. This will run the initialize method for each modifier in the session's lifecycle. This will also set the session's state to the initialized state.
Parameters:
-
recipe
Union[str, List[str], Recipe, List[Recipe], None]
, default:None
) –the recipe to use for the compression, can be a path to a recipe file, a raw recipe string, a recipe object, or a list of recipe objects.
-
recipe_stage
Union[str, List[str], None]
, default:None
) –the stage to target for the compression
-
recipe_args
Union[Dict[str, Any], None]
, default:None
) –the args to use for overriding the recipe defaults
-
model
Optional[Any]
, default:None
) –the model to compress
-
teacher_model
Optional[Any]
, default:None
) –the teacher model to use for knowledge distillation
-
optimizer
Optional[Any]
, default:None
) –the optimizer to use for the compression
-
attach_optim_callbacks
bool
, default:True
) –True to attach the optimizer callbacks to the compression lifecycle, False otherwise
-
train_data
Optional[Any]
, default:None
) –the training data to use for the compression
-
val_data
Optional[Any]
, default:None
) –the validation data to use for the compression
-
test_data
Optional[Any]
, default:None
) –the testing data to use for the compression
-
calib_data
Optional[Any]
, default:None
) –the calibration data to use for the compression
-
copy_data
bool
, default:True
) –True to copy the data, False otherwise
-
start
Optional[float]
, default:None
) –the start epoch to use for the compression
-
steps_per_epoch
Optional[int]
, default:None
) –the number of steps per epoch to use for the compression
-
batches_per_step
Optional[int]
, default:None
) –the number of batches per step to use for compression
-
loggers
Union[None, LoggerManager, List[BaseLogger]]
, default:None
) –the metrics manager to setup logging important info and milestones to, also accepts a list of BaseLogger(s)
-
kwargs
additional kwargs to pass to the lifecycle's initialize method
Returns:
-
ModifiedState
–the modified state of the session after initializing
Source code in llmcompressor/core/session.py
log
Log model and loss information for the current event type
Parameters:
-
event_type
EventType
) –the event type to log for
-
loss
Optional[Any]
, default:None
) –the loss to log if any
Source code in llmcompressor/core/session.py
reset
reset_stage
Reset the session for starting a new stage, recipe and model stays intact