feat(cli): add CLI to strip checkpoints to their bare minimum (#1858)

This commit is contained in:
Hervé BREDIN authored and GitHub committed 2025-06-13 17:11:11 +02:00
1 parent 9178d88b87
commit 78924e2fa2
2 files changed
+56 -3

No files matched your search

+3 -2
View File
@@ -54,8 +54,9 @@ Models can now be stored alongside their pipelines in the same repository, strea
- feat(utils): add `FilterByNumberOfSpeakers` protocol files filter
- feat(core): add `Calibration` class to calibrate logits/distances into probabilities
- feat(metric): add `DetectionErrorRate`, `SegmentationErrorRate`, `DiarizationPrecision`, and `DiarizationRecall` metrics
- feat(cli): add CLI to download, apply, benchmark, or optimize pipelines
- feat(cli): add CLI to download, apply, benchmark, and optimize pipelines
- feat(cli): add CLI to strip checkpoints to their bare inference minimum
### Improvements
- improve(model): improve WavLM (un)freezing support for `SSeRiouSS` architecture ([@clement-pages](https://github.com/clement-pages/))
+53 -1
View File
@@ -40,7 +40,7 @@ import pyannote.database
import torch
import typer
import yaml
from pyannote.audio import Audio, Pipeline
from pyannote.audio import Audio, Pipeline, Model
from pyannote.core import Annotation
from pyannote.metrics.base import BaseMetric
from pyannote.metrics.diarization import DiarizationErrorRate, JaccardErrorRate
@@ -759,5 +759,57 @@ def benchmark(
file["best_speaker_diarization"].write_rttm(rttm)
@app.command("strip")
def strip(
checkpoint: Annotated[
Path,
typer.Argument(
help="Path to pyannote.audio model checkpoint",
exists=True,
dir_okay=False,
file_okay=True,
resolve_path=True,
),
],
into: Annotated[
Path,
typer.Argument(
help="Path to the stripped checkpoint",
exists=False,
dir_okay=False,
file_okay=True,
writable=True,
resolve_path=True,
),
],
):
"""
Strip a pretrained CHECKPOINT to only keep the parts needed for inference.
"""
keys = [
"pytorch-lightning_version", # * pytorch-lightning needs
"hparams_name", # those values to initialize
"hyper_parameters", # the model architecture
"state_dict", # * actual weights
"pyannote.audio", # * pyannote.audio dependencies
]
old_checkpoint = torch.load(
checkpoint, map_location=torch.device("cpu"), weights_only=False
)
new_checkpoint = {
key: value for key, value in old_checkpoint.items() if key in keys
}
torch.save(new_checkpoint, into)
# check that the stripped checkpoint can be loaded again
try:
_ = Model.from_pretrained(into)
except Exception as e:
sys.exit(
f"Something went wrong while stripping the checkpoint as it could not be reloaded: {e}"
)
if __name__ == "__main__":
app()