llmcompressor.metrics
Metrics logging and monitoring framework for compression workflows.
Provides comprehensive metrics collection, logging, and monitoring capabilities for model compression operations. Includes base loggers, frequency management, and specialized metrics tracking for training and inference performance during compression.
Modules:
Classes:
-
BaseLogger
–Base class that all modifier loggers must implement.
-
LambdaLogger
–Logger that handles calling back to a lambda function with any logs.
-
LoggerManager
–Wrapper around loggers that handles log scheduling and handing off logs to intended
-
PythonLogger
–Modifier metrics that handles printing values into a python metrics instance.
-
SparsificationGroupLogger
–Modifier metrics that handles outputting values to other supported systems.
-
TensorBoardLogger
–Modifier metrics that handles outputting values into a TensorBoard log directory
-
WANDBLogger
–Modifier metrics that handles outputting values to Weights and Biases.
BaseLogger
Bases: ABC
Base class that all modifier loggers must implement.
Parameters:
-
name
str
) –name given to the metrics, used for identification
-
enabled
bool
, default:True
) –True to log, False otherwise
Methods:
-
log_hyperparams
–:param params: Each key-value pair in the dictionary is the name of the
-
log_scalar
–:param tag: identifying tag to log the value with
-
log_scalars
–:param tag: identifying tag to log the values with
-
log_string
–:param tag: identifying tag to log the values with
-
save
–:param file_path: path to a file to be saved
Attributes:
-
enabled
(bool
) –:return: True to log, False otherwise
-
name
(str
) –:return: name given to the metrics, used for identification
Source code in llmcompressor/metrics/logger.py
log_hyperparams
Parameters:
-
params
Dict[str, float]
) –Each key-value pair in the dictionary is the name of the hyper parameter and it's corresponding value.
Returns:
-
bool
–True if logged, False otherwise.
Source code in llmcompressor/metrics/logger.py
log_scalar
log_scalar(
tag: str,
value: float,
step: Optional[int] = None,
wall_time: Optional[float] = None,
**kwargs
) -> bool
Parameters:
-
tag
str
) –identifying tag to log the value with
-
value
float
) –value to save
-
step
Optional[int]
, default:None
) –global step for when the value was taken
-
wall_time
Optional[float]
, default:None
) –global wall time for when the value was taken
-
kwargs
additional logging arguments to support Python and custom loggers
Returns:
-
bool
–True if logged, False otherwise.
Source code in llmcompressor/metrics/logger.py
log_scalars
log_scalars(
tag: str,
values: Dict[str, float],
step: Optional[int] = None,
wall_time: Optional[float] = None,
**kwargs
) -> bool
Parameters:
-
tag
str
) –identifying tag to log the values with
-
values
Dict[str, float]
) –values to save
-
step
Optional[int]
, default:None
) –global step for when the values were taken
-
wall_time
Optional[float]
, default:None
) –global wall time for when the values were taken
-
kwargs
additional logging arguments to support Python and custom loggers
Returns:
-
bool
–True if logged, False otherwise.
Source code in llmcompressor/metrics/logger.py
log_string
log_string(
tag: str,
string: str,
step: Optional[int] = None,
wall_time: Optional[float] = None,
**kwargs
) -> bool
Parameters:
-
tag
str
) –identifying tag to log the values with
-
values
values to save
-
step
Optional[int]
, default:None
) –global step for when the values were taken
-
wall_time
Optional[float]
, default:None
) –global wall time for when the values were taken
-
kwargs
additional logging arguments to support Python and custom loggers
Returns:
-
bool
–True if logged, False otherwise.
Source code in llmcompressor/metrics/logger.py
save
Parameters:
-
file_path
str
) –path to a file to be saved
-
kwargs
additional arguments that a specific metrics might use
Returns:
-
bool
–True if saved, False otherwise
Source code in llmcompressor/metrics/logger.py
LambdaLogger
LambdaLogger(
lambda_func: Callable[
[
Optional[str],
Optional[Union[float, str]],
Optional[Dict[str, float]],
Optional[int],
Optional[float],
Optional[int],
],
bool,
],
name: str = "lambda",
enabled: bool = True,
)
Bases: BaseLogger
Logger that handles calling back to a lambda function with any logs.
Parameters:
-
lambda_func
Callable[[Optional[str], Optional[Union[float, str]], Optional[Dict[str, float]], Optional[int], Optional[float], Optional[int]], bool]
) –the lambda function to call back into with any logs. The expected call sequence is (tag, value, values, step, wall_time) -> bool The return type is True if logged and False otherwise.
-
name
str
, default:'lambda'
) –name given to the metrics, used for identification; defaults to lambda
-
enabled
bool
, default:True
) –True to log, False otherwise
Methods:
-
log_hyperparams
–:param params: Each key-value pair in the dictionary is the name of the
-
log_scalar
–:param tag: identifying tag to log the value with
-
log_scalars
–:param tag: identifying tag to log the values with
Attributes:
-
lambda_func
(Callable[[Optional[str], Optional[Union[float, str]], Optional[Dict[str, float]], Optional[int], Optional[float], Optional[int]], bool]
) –:return: the lambda function to call back into with any logs.
Source code in llmcompressor/metrics/logger.py
lambda_func property
lambda_func: Callable[
[
Optional[str],
Optional[Union[float, str]],
Optional[Dict[str, float]],
Optional[int],
Optional[float],
Optional[int],
],
bool,
]
Returns:
-
Callable[[Optional[str], Optional[Union[float, str]], Optional[Dict[str, float]], Optional[int], Optional[float], Optional[int]], bool]
–the lambda function to call back into with any logs. The expected call sequence is (tag, value, values, step, wall_time)
log_hyperparams
Parameters:
-
params
Dict
) –Each key-value pair in the dictionary is the name of the hyper parameter and it's corresponding value.
-
level
Optional[Union[int, str]]
, default:None
) –minimum severity level for the log message
Returns:
-
bool
–True if logged, False otherwise.
Source code in llmcompressor/metrics/logger.py
log_scalar
log_scalar(
tag: str,
value: float,
step: Optional[int] = None,
wall_time: Optional[float] = None,
level: Optional[Union[int, str]] = None,
) -> bool
Parameters:
-
tag
str
) –identifying tag to log the value with
-
value
float
) –value to save
-
step
Optional[int]
, default:None
) –global step for when the value was taken
-
wall_time
Optional[float]
, default:None
) –global wall time for when the value was taken, defaults to time.time()
-
level
Optional[Union[int, str]]
, default:None
) –minimum severity level for the log message
-
kwargs
additional logging arguments to support Python and custom loggers
Returns:
-
bool
–True if logged, False otherwise.
Source code in llmcompressor/metrics/logger.py
log_scalars
log_scalars(
tag: str,
values: Dict[str, float],
step: Optional[int] = None,
wall_time: Optional[float] = None,
level: Optional[Union[int, str]] = None,
) -> bool
Parameters:
-
tag
str
) –identifying tag to log the values with
-
values
Dict[str, float]
) –values to save
-
step
Optional[int]
, default:None
) –global step for when the values were taken
-
wall_time
Optional[float]
, default:None
) –global wall time for when the values were taken, defaults to time.time()
-
level
Optional[Union[int, str]]
, default:None
) –minimum severity level for the log message
-
kwargs
additional logging arguments to support Python and custom loggers
Returns:
-
bool
–True if logged, False otherwise.
Source code in llmcompressor/metrics/logger.py
LoggerManager
LoggerManager(
loggers: Optional[List[BaseLogger]] = None,
log_frequency: Union[float, None] = 0.1,
log_python: bool = True,
name: str = "manager",
mode: LoggingModeType = "exact",
frequency_type: FrequencyType = "epoch",
)
Bases: ABC
Wrapper around loggers that handles log scheduling and handing off logs to intended loggers.
Parameters:
-
loggers
Optional[List[BaseLogger]]
, default:None
) –list of loggers assigned to this manager
-
log_frequency
Union[float, None]
, default:0.1
) –number of stes or fraction of steps to wait between logs
-
mode
LoggingModeType
, default:'exact'
) –The logging mode to use, either "on_change" or "exact", "on_change" will log when the model has been updated since the last log, "exact" will log at the given frequency regardless of model updates. Defaults to "exact"
-
frequency_type
FrequencyType
, default:'epoch'
) –The frequency type to use, either "epoch", "step", or "batch" controls what the frequency manager is tracking, e.g. if the frequency type is "epoch", then the frequency manager will track the number of epochs that have passed since the last log, if the frequency type is "step", then the frequency manager will track the number of optimizer steps
Methods:
-
add_logger
–add a BaseLogger implementation to the loggers of this manager
-
log_hyperparams
–(Note: this method is deprecated and will be removed in a future version,
-
log_ready
–Check if there is a metrics that is ready to accept a log
-
log_scalar
–(Note: this method is deprecated and will be removed in a future version,
-
log_scalars
–(Note: this method is deprecated and will be removed in a future version,
-
log_string
–(Note: this method is deprecated and will be removed in a future version,
-
log_written
–Update the frequency manager with the last log step written
-
model_updated
–Update the frequency manager with the last model update step
-
save
–:param file_path: path to a file to be saved
-
time
–Context manager to log the time it takes to run the block of code
Attributes:
-
log_frequency
(Union[str, float, None]
) –:return: number of epochs or fraction of epochs to wait between logs
-
loggers
(List[BaseLogger]
) –:return: list of loggers assigned to this manager
-
name
(str
) –:return: name given to the metrics, used for identification
-
wandb
(Optional[ModuleType]
) –:return: wandb module if initialized
Source code in llmcompressor/metrics/logger.py
log_frequency property
writable
Returns:
-
Union[str, float, None]
–number of epochs or fraction of epochs to wait between logs
loggers property
writable
Returns:
-
List[BaseLogger]
–list of loggers assigned to this manager
wandb property
Returns:
-
Optional[ModuleType]
–wandb module if initialized
add_logger
add a BaseLogger implementation to the loggers of this manager
Parameters:
-
logger
BaseLogger
) –metrics object to add
Source code in llmcompressor/metrics/logger.py
log_hyperparams
log_hyperparams(
params: Dict,
log_types: Union[str, List[str]] = ALL_TOKEN,
level: Optional[Union[int, str]] = None,
)
(Note: this method is deprecated and will be removed in a future version, use LoggerManager().metric.log_hyperparams instead)
Parameters:
-
params
Dict
) –Each key-value pair in the dictionary is the name of the hyper parameter and it's corresponding value.
Source code in llmcompressor/metrics/logger.py
log_ready
Check if there is a metrics that is ready to accept a log
Parameters:
-
current_log_step
current step log is requested at
-
last_log_step
last time a log was recorder for this object. (Deprecated)
-
check_model_update
bool
, default:False
) –if True, will check if the model has been updated, if False, will only check the log frequency
Returns:
- –
True if a metrics is ready to accept a log.
Source code in llmcompressor/metrics/logger.py
log_scalar
log_scalar(
tag: str,
value: float,
step: Optional[int] = None,
wall_time: Optional[float] = None,
log_types: Union[str, List[str]] = ALL_TOKEN,
level: Optional[Union[int, str]] = None,
)
(Note: this method is deprecated and will be removed in a future version, use LoggerManager().metric.log_scalar instead)
Parameters:
-
tag
str
) –identifying tag to log the value with
-
value
float
) –value to save
-
step
Optional[int]
, default:None
) –global step for when the value was taken
-
wall_time
Optional[float]
, default:None
) –global wall time for when the value was taken
-
level
Optional[Union[int, str]]
, default:None
) –minimum severity level for the log message
-
kwargs
additional logging arguments to support Python and custom loggers
Returns:
- –
True if logged, False otherwise.
Source code in llmcompressor/metrics/logger.py
log_scalars
log_scalars(
tag: str,
values: Dict[str, float],
step: Optional[int] = None,
wall_time: Optional[float] = None,
log_types: Union[str, List[str]] = ALL_TOKEN,
level: Optional[Union[int, str]] = None,
)
(Note: this method is deprecated and will be removed in a future version, use LoggerManager().metric.log_scalars instead)
Parameters:
-
tag
str
) –identifying tag to log the values with
-
values
Dict[str, float]
) –values to save
-
step
Optional[int]
, default:None
) –global step for when the values were taken
-
wall_time
Optional[float]
, default:None
) –global wall time for when the values were taken
-
level
Optional[Union[int, str]]
, default:None
) –minimum severity level for the log message
-
kwargs
additional logging arguments to support Python and custom loggers
Returns:
- –
True if logged, False otherwise.
Source code in llmcompressor/metrics/logger.py
log_string
log_string(
tag: str,
string: str,
step: Optional[int] = None,
wall_time: Optional[float] = None,
log_types: Union[str, List[str]] = ALL_TOKEN,
level: Optional[Union[int, str]] = None,
)
(Note: this method is deprecated and will be removed in a future version, use LoggerManager().system.log_string instead)
Parameters:
-
tag
str
) –identifying tag to log the values with
-
values
values to save
-
step
Optional[int]
, default:None
) –global step for when the values were taken
-
wall_time
Optional[float]
, default:None
) –global wall time for when the values were taken
-
kwargs
additional logging arguments to support Python and custom loggers
-
level
Optional[Union[int, str]]
, default:None
) –minimum severity level for the log message
Returns:
- –
True if logged, False otherwise.
Source code in llmcompressor/metrics/logger.py
log_written
Update the frequency manager with the last log step written
Parameters:
-
step
LogStepType
) –step that was last logged
model_updated
Update the frequency manager with the last model update step
Parameters:
-
step
LogStepType
) –step that was last logged
save
Parameters:
-
file_path
str
) –path to a file to be saved
-
kwargs
additional arguments that a specific metrics might use
Source code in llmcompressor/metrics/logger.py
time
Context manager to log the time it takes to run the block of code
Usage:
with LoggerManager().time("my_block"): time.sleep(1)
Parameters:
-
tag
Optional[str]
, default:None
) –identifying tag to log the values with
Source code in llmcompressor/metrics/logger.py
PythonLogger
Bases: LambdaLogger
Modifier metrics that handles printing values into a python metrics instance.
Parameters:
-
name
str
, default:'python'
) –name given to the metrics, used for identification; defaults to python
-
enabled
bool
, default:True
) –True to log, False otherwise
Methods:
-
log_string
–:param tag: identifying tag to log the values with
Source code in llmcompressor/metrics/logger.py
log_string
log_string(
tag: Optional[str],
string: Optional[str],
step: Optional[int],
wall_time: Optional[float] = None,
level: Optional[Union[int, str]] = None,
) -> bool
Parameters:
-
tag
Optional[str]
) –identifying tag to log the values with
-
string
Optional[str]
) –string to log
-
step
Optional[int]
) –global step for when the values were taken
-
wall_time
Optional[float]
, default:None
) –global wall time for when the values were taken, defaults to time.time()
-
level
Optional[Union[int, str]]
, default:None
) –minimum severity level for the log message
Returns:
-
bool
–True if logged, False otherwise.
Source code in llmcompressor/metrics/logger.py
SparsificationGroupLogger
SparsificationGroupLogger(
lambda_func: Optional[
Callable[
[
Optional[str],
Optional[float],
Optional[Dict[str, float]],
Optional[int],
Optional[float],
],
bool,
]
] = None,
python: bool = False,
python_log_level: Optional[Union[int, str]] = "INFO",
tensorboard: Optional[
Union[bool, str, SummaryWriter]
] = None,
wandb_: Optional[Union[bool, Dict]] = None,
name: str = "sparsification",
enabled: bool = True,
)
Bases: BaseLogger
Modifier metrics that handles outputting values to other supported systems. Supported ones include: - Python logging - Tensorboard - Weights and Biases - Lambda callback
All are optional and can be bulk disabled and enabled by this root.
Parameters:
-
lambda_func
Optional[Callable[[Optional[str], Optional[float], Optional[Dict[str, float]], Optional[int], Optional[float]], bool]]
, default:None
) –an optional lambda function to call back into with any logs. The expected call sequence is (tag, value, values, step, wall_time) -> bool The return type is True if logged and False otherwise.
-
python
bool
, default:False
) –an bool argument for logging to a python metrics. True to create a metrics instance, or False to not log anything
-
python_log_level
Optional[Union[int, str]]
, default:'INFO'
) –if python, the level to log any incoming data at on the loguru.logger instance
-
tensorboard
Optional[Union[bool, str, SummaryWriter]]
, default:None
) –an optional argument for logging to a tensorboard writer. May be a SummaryWriter instance to log to, a string representing the directory to create a new SummaryWriter to log to, True to create a new SummaryWriter, or non truthy to not log anything (False, None)
-
wandb_
Optional[Union[bool, Dict]]
, default:None
) –an optional argument for logging to wandb. May be a dictionary to pass to the init call for wandb, True to log to wandb (will not call init), or non truthy to not log anything (False, None)
-
name
str
, default:'sparsification'
) –name given to the metrics, used for identification; defaults to sparsification
-
enabled
bool
, default:True
) –True to log, False otherwise
Methods:
-
enabled
–:param value: True to log, False otherwise
-
log_hyperparams
–:param params: Each key-value pair in the dictionary is the name of the
-
log_scalar
–:param tag: identifying tag to log the value with
-
log_scalars
–:param tag: identifying tag to log the values with
Attributes:
-
loggers
(List[BaseLogger]
) –:return: the created metrics sub instances for this metrics
Source code in llmcompressor/metrics/logger.py
loggers property
Returns:
-
List[BaseLogger]
–the created metrics sub instances for this metrics
enabled
Parameters:
-
value
bool
) –True to log, False otherwise
log_hyperparams
Parameters:
-
params
Dict
) –Each key-value pair in the dictionary is the name of the hyper parameter and it's corresponding value.
Source code in llmcompressor/metrics/logger.py
log_scalar
log_scalar(
tag: str,
value: float,
step: Optional[int] = None,
wall_time: Optional[float] = None,
level: Optional[Union[int, str]] = None,
)
Parameters:
-
tag
str
) –identifying tag to log the value with
-
value
float
) –value to save
-
step
Optional[int]
, default:None
) –global step for when the value was taken
-
wall_time
Optional[float]
, default:None
) –global wall time for when the value was taken, defaults to time.time()
-
level
Optional[Union[int, str]]
, default:None
) –minimum severity level for the log message
Source code in llmcompressor/metrics/logger.py
log_scalars
log_scalars(
tag: str,
values: Dict[str, float],
step: Optional[int] = None,
wall_time: Optional[float] = None,
level: Optional[Union[int, str]] = None,
)
Parameters:
-
tag
str
) –identifying tag to log the values with
-
values
Dict[str, float]
) –values to save
-
step
Optional[int]
, default:None
) –global step for when the values were taken
-
wall_time
Optional[float]
, default:None
) –global wall time for when the values were taken, defaults to time.time()
-
level
Optional[Union[int, str]]
, default:None
) –minimum severity level for the log message
Source code in llmcompressor/metrics/logger.py
TensorBoardLogger
TensorBoardLogger(
log_path: str = None,
writer: SummaryWriter = None,
name: str = "tensorboard",
enabled: bool = True,
)
Bases: LambdaLogger
Modifier metrics that handles outputting values into a TensorBoard log directory for viewing in TensorBoard.
Parameters:
-
log_path
str
, default:None
) –the path to create a SummaryWriter at. writer must be None to use if not supplied (and writer is None), will create a TensorBoard dir in cwd
-
writer
SummaryWriter
, default:None
) –the writer to log results to, if none is given creates a new one at the log_path
-
name
str
, default:'tensorboard'
) –name given to the metrics, used for identification; defaults to tensorboard
-
enabled
bool
, default:True
) –True to log, False otherwise
Methods:
-
available
–:return: True if tensorboard is available and installed, False, otherwise
Attributes:
-
writer
(SummaryWriter
) –:return: the writer to log results to,
Source code in llmcompressor/metrics/logger.py
writer property
Returns:
-
SummaryWriter
–the writer to log results to, if none is given creates a new one at the log_path
available staticmethod
Returns:
-
bool
–True if tensorboard is available and installed, False, otherwise
WANDBLogger
WANDBLogger(
init_kwargs: Optional[Dict] = None,
name: str = "wandb",
enabled: bool = True,
wandb_err: Optional[Exception] = wandb_err,
)
Bases: LambdaLogger
Modifier metrics that handles outputting values to Weights and Biases.
Parameters:
-
init_kwargs
Optional[Dict]
, default:None
) –the args to call into wandb.init with; ex: wandb.init(**init_kwargs). If not supplied, then init will not be called
-
name
str
, default:'wandb'
) –name given to the metrics, used for identification; defaults to wandb
-
enabled
bool
, default:True
) –True to log, False otherwise
Methods:
-
available
–:return: True if wandb is available and installed, False, otherwise
-
save
–:param file_path: path to a file to be saved