Skip to content

[Examples] [Bugfix] skip sparsity stats when saving checkpoints #1528

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

Merged
merged 2 commits into from
Jun 10, 2025
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
17 changes: 11 additions & 6 deletions src/llmcompressor/pytorch/model_load/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ def save_checkpoint(
get_model_compressor, # avoid circular import
)

# used for decompression
# unfortunately, if skip_sparsity_compression_stats==True, sparsity stats
# are computed twice. In the future, track sparsity from recipe or
# share recipe between compression and decompression
compressor = get_model_compressor(
model=model,
save_compressed=save_compressed,
skip_sparsity_compression_stats=skip_sparsity_compression_stats,
)

# saving the model also saves the recipe
model.save_pretrained(
save_path,
Expand All @@ -55,13 +65,8 @@ def save_checkpoint(
if processor is not None:
processor.save_pretrained(save_path)

# saving the model modifies the model strcuture
# decompression: saving the model modifies the model strcuture
# as this is only a checkpoint, decompress model to enable future training/oneshot
compressor = get_model_compressor(
model=model,
save_compressed=save_compressed,
skip_sparsity_compression_stats=skip_sparsity_compression_stats,
)
if compressor is not None:
compressor.decompress_model(model)

Expand Down
4 changes: 3 additions & 1 deletion src/llmcompressor/transformers/finetune/session_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def save_model(
self,
output_dir: str,
_internal_call: bool = False,
skip_sparsity_compression_stats: Optional[bool] = False,
skip_sparsity_compression_stats: Optional[bool] = True,
):
"""
Override of the save_model function and expects it to exist in the parent.
Expand All @@ -388,6 +388,8 @@ def save_model(
self.model.prepare_for_save() # TODO: move to finalize

# save checkpoint
# note that skip_sparsity_compression_stats
# is True by default to avoid high runtime cost
self.save_state()
if self.accelerator.is_main_process:
processor = getattr(self, "processing_class", self.tokenizer)
Expand Down