feat: add support for batch inference (#2027)

This commit is contained in:
Hervé BREDIN authored and GitHub committed 2026-06-29 15:20:39 +02:00
1 parent 5dd8d59148
commit 83a7c04623
2 files changed
+138 -55

No files matched your search

+17 -20
View File
@@ -4,7 +4,7 @@
# MIT License
#
# Copyright (c) 2024-2025 CNRS
# Copyright (c) 2025 pyannoteAI
# Copyright (c) 2025- pyannoteAI
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -97,6 +97,7 @@ def parse_device(device: Device) -> torch.device:
def get_diarization(prediction) -> Annotation:
# if result is an Annotation, assume it is speaker diarization
if isinstance(prediction, Annotation):
return prediction
@@ -603,7 +604,10 @@ def benchmark(
# load pretrained pipeline
pretrained_pipeline = Pipeline.from_pretrained(
pipeline, revision=revision, token=token, cache_dir=cache,
pipeline,
revision=revision,
token=token,
cache_dir=cache,
)
if pretrained_pipeline is None:
print(f"Could not load pretrained pipeline from {pipeline}.")
@@ -646,10 +650,6 @@ def benchmark(
if num_speakers == NumSpeakers.ORACLE:
benchmark_name += ".OracleNumSpeakers"
# used to store processing time and file duration
processing_time: dict[str, float] = dict()
playing_time: dict[str, float] = dict()
# used to store raw predictions in JSON format
serialized_predictions: dict[str, dict] = dict()
@@ -676,19 +676,14 @@ def benchmark(
if rttm_file.exists():
raise FileExistsError(f"{rttm_file} already exists.")
# iterate over all files in the specified subset
for file in track(files, disable=not progress):
# gather file metadata
uri: str = file["uri"]
playing_time[uri] = Audio().get_duration(file)
if hasattr(pretrained_pipeline, "apply_batch"):
iterator = pretrained_pipeline(files, progress=progress)
else:
iterator = track(pretrained_pipeline(files), disable=not progress)
tic: float = time.time()
# apply pretrained pipeline to file
prediction = pretrained_pipeline(file, **file.get("pipeline_kwargs", {}))
tac: float = time.time()
processing_time[uri] = tac - tic
tic: float = time.time()
for file, prediction in iterator:
uri = file["uri"]
# if prediction has a built-in serialize method, save serialized version
if hasattr(prediction, "serialize"):
@@ -731,6 +726,8 @@ def benchmark(
if optimize:
file["speaker_diarization"] = speaker_diarization
tac: float = time.time()
# save serialized predictions to disk (might contain more than just diarization results)
if serialized_predictions and not per_file:
with open(into / f"{benchmark_name}.json", "w") as f:
@@ -738,8 +735,8 @@ def benchmark(
# log processing time and capacity
processing = dict()
total_processing_time: float = sum(processing_time.values())
total_playing_time: float = sum(playing_time.values())
total_processing_time = tac - tic
total_playing_time = sum(Audio().get_duration(file) for file in files)
processing["seconds_per_hour"] = total_processing_time / (total_playing_time / 3600)
processing["times_faster_than_realtime"] = (
total_playing_time / total_processing_time
+121 -35
View File
@@ -22,13 +22,14 @@
# SOFTWARE.
from __future__ import annotations
import os
import warnings
from collections import OrderedDict
from collections.abc import Iterator
from functools import partial
from pathlib import Path
from typing import Callable, Dict, List, Optional
from typing import Any, Callable, Dict, List, Mapping, Optional
import torch
import torch.nn as nn
@@ -90,7 +91,9 @@ def expand_subfolders(
revision = parent_revision
if parent_subfolder:
subfolder = f"{parent_subfolder.rstrip('/')}/{subfolder.lstrip('/')}"
subfolder = (
f"{parent_subfolder.rstrip('/')}/{subfolder.lstrip('/')}"
)
config[key] = {
"checkpoint": model_id,
@@ -122,7 +125,9 @@ def expand_subfolders(
revision = parent_revision
if parent_subfolder:
subfolder = f"{parent_subfolder.rstrip('/')}/{subfolder.lstrip('/')}"
subfolder = (
f"{parent_subfolder.rstrip('/')}/{subfolder.lstrip('/')}"
)
config[idx] = {
"checkpoint": model_id,
@@ -435,45 +440,25 @@ class Pipeline(_Pipeline):
"""
raise NotImplementedError()
def __call__(self, file: AudioFile, preload: bool = False, **kwargs):
"""Validate file, (optionally) load it in memory, then process it
def prepare_one(self, file: AudioFile, preload: bool = False) -> Mapping:
"""Prepare one file for being processed by the pipeline
1. Validate file
2. Add pipeline preprocessors if any
3. Preload in memory if requested
Parameters
----------
file : AudioFile
File to process
preload : bool, optional
Whether to preload waveform before applying the pipeline.
kwargs : keyword arguments, optional
Additional keyword arguments passed to `self.apply(...)`
preload : bool
Whether to preload in memory
Returns
-------
output : Any
Whatever `self.apply(...)` returns
file : Mapping
Prepared file
"""
fix_reproducibility(getattr(self, "device", torch.device("cpu")))
if not self.instantiated:
# instantiate with default parameters when available
try:
default_parameters = self.default_parameters()
except NotImplementedError:
raise RuntimeError(
"A pipeline must be instantiated with `pipeline.instantiate(parameters)` before it can be applied."
)
try:
self.instantiate(default_parameters)
except ValueError:
raise RuntimeError(
"A pipeline must be instantiated with `pipeline.instantiate(parameters)` before it can be applied. "
"Tried to use parameters provided by `pipeline.default_parameters()` but those are not compatible. "
)
warnings.warn(
f"The pipeline has been automatically instantiated with {default_parameters}."
)
file = Audio.validate_file(file)
@@ -499,11 +484,110 @@ class Pipeline(_Pipeline):
# therefore we remove the `channel` key from the file
file.pop("channel", None)
return file
def __call__(
self,
file: AudioFile | list[AudioFile],
preload: bool = False,
**kwargs,
) -> Any | Iterator[tuple[str, Any]]:
"""Validate file(s), (optionally) load it/them in memory, then process it/them
Parameters
----------
file : AudioFile or list[AudioFile]
File to process, or a list of AudioFiles for batch processing.
All files in the list must have distinct URIs.
Files are processed in sequence unless the pipeline defines an
`apply_batch` method, which is called instead.
preload : bool, optional
Whether to preload waveform before applying the pipeline.
Ignored for batch input when `apply_batch` is available.
kwargs : keyword arguments, optional
Additional keyword arguments passed to `self.apply(...)` or
`self.apply_batch(...)`
Returns (for single file)
-------------------------
output : Any
Whatever `self.apply(...)` returns for a single file, or a dict
mapping each URI to its output for batch input.
Yields (for multiple files)
---------------------------
file : AudioFile
output : Any
"""
fix_reproducibility(getattr(self, "device", torch.device("cpu")))
if not self.instantiated:
# instantiate with default parameters when available
try:
default_parameters = self.default_parameters()
except NotImplementedError:
raise RuntimeError(
"A pipeline must be instantiated with `pipeline.instantiate(parameters)` before it can be applied."
)
try:
self.instantiate(default_parameters)
except ValueError:
raise RuntimeError(
"A pipeline must be instantiated with `pipeline.instantiate(parameters)` before it can be applied. "
"Tried to use parameters provided by `pipeline.default_parameters()` but those are not compatible. "
)
warnings.warn(
f"The pipeline has been automatically instantiated with {default_parameters}."
)
if isinstance(file, list):
# prepare every file
files = [self.prepare_one(f, preload=preload) for f in file]
# check for duplicate uris (and complain if any)
uris = [f["uri"] for f in files]
if len(uris) != len(set(uris)):
seen: set[str] = set()
duplicates = [u for u in uris if u in seen or seen.add(u)]
raise ValueError(
f"All files in a batch must have distinct URIs. "
f"Duplicate URIs: {duplicates}"
)
# if pipeline natively supports batch processing, use it
if hasattr(self, "apply_batch"):
# TODO: warn user that pipeline_kwargs might not be taken into account.
for f, prediction in self.apply_batch(files, **kwargs):
# send file duration to telemetry as well as
# requested number of speakers in case of diarization
track_pipeline_apply(self, f, **kwargs)
yield f, prediction
# otherwise process files sequentially
else:
for f in files:
prediction = self.apply(f, **f.get("pipeline_kwargs", {}), **kwargs)
# send file duration to telemetry as well as
# requested number of speakers in case of diarization
track_pipeline_apply(self, f, **kwargs)
yield f, prediction
return
file = self.prepare_one(file, preload=preload)
prediction = self.apply(file, **file.get("pipeline_kwargs", {}), **kwargs)
# send file duration to telemetry as well as
# requested number of speakers in case of diarization
track_pipeline_apply(self, file, **kwargs)
return self.apply(file, **kwargs)
return prediction
def to(self, device: torch.device) -> Pipeline:
"""Send pipeline to `device`"""
@@ -535,5 +619,7 @@ class Pipeline(_Pipeline):
return self.to(torch.device("cuda", device))
else:
if device.type != "cuda":
raise ValueError("Expected CUDA device. Use `Pipeline.to(device)` for other devices.")
raise ValueError(
"Expected CUDA device. Use `Pipeline.to(device)` for other devices."
)
return self.to(device)