Open-source inference server and production cluster for all the models your agent needs.
Python bgecolbertdata-pipelinedeep-learningembeddingsinferenceinference-serverinformation-retrievalllmmlmlopsnatural-language-processingnlppythonrerankingretrievalretrieval-augmented-generationsemantic-searchspladevector-search
feat(server): add Parakeet-TDT speech-to-text (nvidia/parakeet-tdt-0.6b-v3) (#608)
Shai Shmuel7e2a1d31e48b
diff --git a/packages/sie_server/bundles/transformers5.yaml b/packages/sie_server/bundles/transformers5.yamlindex 9bd203bb9..47cd70a20 100644--- a/packages/sie_server/bundles/transformers5.yaml+++ b/packages/sie_server/bundles/transformers5.yaml@@ -13,6 +13,8 @@ adapters:- sie_server.adapters.st_sparse_vision.adapter# TopK-Embed-V1 multi-vector text + page-image models (Qwen3.5 backbone, transformers>=5.2).- sie_server.adapters.topk_embed.adapter+# Parakeet-TDT speech-to-text (ParakeetForTDT).+- sie_server.adapters.parakeet.adapterdeps:# Transformers 5.x for models requiring newer architecture support# (e.g., LightOnOCR-2-1B needs LightOnOcrForConditionalGeneration,@@ -47,6 +49,9 @@ deps:opf:url: git+https://github.com/openai/privacy-filter@f7f00ca7fb869683eb732c010299d901457f19c3tiktoken: '==0.12.0'+ # Parakeet's feature extractor builds its mel filter bank with librosa and+ # refuses to load without it. Exact: the version Parakeet-TDT was validated with.+ librosa: '==1.0.0'# TopK-Embed-V1 packed path (adapters/topk_embed/packed.py): the Gated# DeltaNet chunked delta rule and causal conv (flash-linear-attention's# Triton kernels) and FlashAttention's varlen attention. CUDA only; withoutdiff --git a/packages/sie_server/models/nvidia__parakeet-tdt-0.6b-v3.yaml b/packages/sie_server/models/nvidia__parakeet-tdt-0.6b-v3.yamlnew file mode 100644index 000000000..582fb51d2--- /dev/null+++ b/packages/sie_server/models/nvidia__parakeet-tdt-0.6b-v3.yaml@@ -0,0 +1,25 @@+sie_id: nvidia/parakeet-tdt-0.6b-v3+hf_id: nvidia/parakeet-tdt-0.6b-v3+hf_revision: 541d1f99c6b0c3cd0b11a95167540bb8edefd82b+inputs:+ text: false+ image: false+ audio: true+ video: false+tasks:+ encode: null+ score: null+ extract: {}+profiles:+ default:+ # Audio milliseconds per extract call: one 12-minute request (the longest+ # SIE accepts) or several shorter ones. The adapter also caps each forward+ # pass at 12 minutes of padded audio (max_padded_batch_ms), so no batch+ # needs more encoder memory than one 12-minute recording, about 6.3 GiB+ # allocated in bfloat16 on an L4.+ max_batch_tokens: 720000+ compute_precision: bfloat16+ adapter_path: sie_server.adapters.parakeet.adapter:ParakeetTDTAdapter+ adapter_options:+ loadtime: {}+ runtime: {}diff --git a/packages/sie_server/src/sie_server/adapters/parakeet/__init__.py b/packages/sie_server/src/sie_server/adapters/parakeet/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/packages/sie_server/src/sie_server/adapters/parakeet/adapter.py b/packages/sie_server/src/sie_server/adapters/parakeet/adapter.pynew file mode 100644index 000000000..8c58db10c--- /dev/null+++ b/packages/sie_server/src/sie_server/adapters/parakeet/adapter.py@@ -0,0 +1,494 @@+"""Parakeet-TDT speech-to-text adapter implemented on the native extract primitive.++``nvidia/parakeet-tdt-0.6b-v3`` transcribes 25 European languages with+punctuation and capitalization. It identifies the spoken language itself and+cannot be steered to one, so a ``language`` option inside its set is accepted+and has no effect, and the response reports ``language: null``.++Decoding is the batched greedy TDT loop in :mod:`.decoding` rather than+``generate()``, whose TDT path has no per-frame symbol limit and can repeat a+token until the output buffer is full.++A request's audio is decoded in one pass, however long. Rows are grouped by+length so that a forward pass never holds more than ``max_padded_batch_ms`` of+padded audio: encoder self-attention grows with the square of a row's length,+so with that budget no batch needs more memory than a single recording of the+budget's length.++The model occasionally returns nothing for a short, tightly cut clip that+contains speech. Each empty row of at most ``_EMPTY_RETRY_MAX_S`` (30 s) is+decoded again on its own, first with 0.25 s of silence appended and then, if+still empty, with 0.25 s on both sides. Padding is never added to the batched+pass, where it lowers accuracy. A longer empty row is returned as it is: every+empty row the padding recovered in testing was under 12 s, and a long+recording that decodes to nothing (silence, music) would otherwise run+through the encoder three times.+"""++from __future__ import annotations++import logging+import re+from dataclasses import dataclass, field+from pathlib import Path+from typing import TYPE_CHECKING, Any, ClassVar++import numpy as np+import torch+from packaging.version import Version++from sie_server.adapters._base_adapter import BaseAdapter+from sie_server.adapters._spec import AdapterSpec+from sie_server.adapters._types import ComputePrecision+from sie_server.adapters.parakeet.decoding import TdtHypothesis, greedy_tdt_decode+from sie_server.core.inference_output import EncodeOutput, ExtractOutput+from sie_server.core.prepared import AudioPayload+from sie_server.core.preprocessor.audio import AudioPreprocessor++if TYPE_CHECKING:+ from collections.abc import Sequence++ from sie_server.types.inputs import Item++logger = logging.getLogger(__name__)++# ISO 639-1 codes of the languages the checkpoint was trained to transcribe.+SUPPORTED_LANGUAGES = frozenset(+ {+ "bg",+ "cs",+ "da",+ "de",+ "el",+ "en",+ "es",+ "et",+ "fi",+ "fr",+ "hr",+ "hu",+ "it",+ "lt",+ "lv",+ "mt",+ "nl",+ "pl",+ "pt",+ "ro",+ "ru",+ "sk",+ "sl",+ "sv",+ "uk",+ }+)+_ERR_ENCODE_NOT_SUPPORTED = "ParakeetTDTAdapter does not support encode(). Use extract() instead."+# transformers releases before 5.18 mask padded encoder frames with -inf. Under+# eager attention a fully padded query then softmaxes to NaN, which the next+# layer spreads to every frame of its row (huggingface/transformers#49070).+# The default SDPA attention avoids this only on backends that zero fully+# masked rows, and PyTorch picks the backend per device, so 5.18 is required.+_MIN_TRANSFORMERS_VERSION = Version("5.18")+_RUNTIME_OPTIONS = frozenset({"language", "temperature", "timestamp_granularities"})+_TIMESTAMP_GRANULARITIES = frozenset({"segment", "word"})+# Seconds of silence (leading, trailing) for the re-decodes of an empty row, in order.+_EMPTY_RETRY_PADDING_S = ((0.0, 0.25), (0.25, 0.25))+# Longest empty row, in seconds, that is re-decoded with padding.+_EMPTY_RETRY_MAX_S = 30.0+# A segment ends at a word ending in sentence-final punctuation (. ! ? or an+# ellipsis, optionally followed by closing quotes or brackets), or once it spans+# this many seconds.+_SENTENCE_END_RE = re.compile(r"[.!?\u2026][\"'\u00bb\u201d\u2019)\]]*$")+_MAX_SEGMENT_S = 30.0+_DEFAULT_MAX_PADDED_BATCH_MS = 12 * 60 * 1_000+++@dataclass(slots=True)+class _Transcript:+ text: str = ""+ hypothesis: TdtHypothesis = field(default_factory=TdtHypothesis)+ # Seconds of silence prepended before decoding; subtracted from timestamps.+ offset_s: float = 0.0+++class ParakeetTDTAdapter(BaseAdapter):+ """Batched transcription for Parakeet-TDT checkpoints (``ParakeetForTDT``)."""++ spec: ClassVar[AdapterSpec] = AdapterSpec(+ inputs=("audio",),+ outputs=("json",),+ unload_fields=("_model", "_processor", "_preprocessor"),+ default_preprocessor="audio",+ )++ def __init__(+ self,+ model_name_or_path: str | Path,+ *,+ compute_precision: ComputePrecision = "bfloat16",+ revision: str | None = None,+ max_padded_batch_ms: int = _DEFAULT_MAX_PADDED_BATCH_MS,+ **kwargs: Any,+ ) -> None:+ del kwargs+ if (+ isinstance(max_padded_batch_ms, bool)+ or not isinstance(max_padded_batch_ms, int)+ or max_padded_batch_ms <= 0+ ):+ msg = "max_padded_batch_ms must be a positive integer"+ raise ValueError(msg)+ self._model_name_or_path = str(model_name_or_path)+ self._compute_precision = compute_precision+ self._revision = revision+ self._max_padded_batch_ms = max_padded_batch_ms+ self._model: Any = None+ self._processor: Any = None+ self._preprocessor: AudioPreprocessor | None = None+ self._device: str | None = None+ self._dtype = torch.float32+ self._sample_rate = 16_000+ self._min_samples = 320+ self._blank_id = 0+ self._vocab_size = 0+ self._durations: tuple[int, ...] = ()++ def load(self, device: str) -> None:+ import transformers+ from transformers import (+ AutoModelForTDT, # ty: ignore[unresolved-import]+ AutoProcessor,+ )++ if Version(transformers.__version__) < _MIN_TRANSFORMERS_VERSION:+ msg = (+ f"Parakeet-TDT requires transformers>={_MIN_TRANSFORMERS_VERSION}, found {transformers.__version__}: "+ "earlier releases mask padded encoder frames with -inf, which can turn padded rows of a batch into NaN"+ )+ raise RuntimeError(msg)++ self._device = device+ self._dtype = self._resolve_dtype()+ revision_kwargs: dict[str, Any] = {}+ if self._revision is not None:+ revision_kwargs["revision"] = self._revision++ logger.info(+ "Loading Parakeet-TDT model %s on device=%s with dtype=%s",+ self._model_name_or_path,+ device,+ self._dtype,+ )+ processor = AutoProcessor.from_pretrained(self._model_name_or_path, **revision_kwargs)+ model = AutoModelForTDT.from_pretrained(self._model_name_or_path, dtype=self._dtype, **revision_kwargs)+ model.to(device)+ model.eval()++ config = model.config+ feature_extractor = processor.feature_extractor+ self._blank_id = int(config.blank_token_id)+ self._vocab_size = int(config.vocab_size)+ self._durations = tuple(int(value) for value in config.durations)+ self._sample_rate = int(feature_extractor.sampling_rate)+ # Feature normalization needs at least two spectrogram frames.+ self._min_samples = 2 * int(feature_extractor.hop_length)+ if getattr(processor, "decoder_type", None) is None:+ # The checkpoint's processor config predates ``decoder_type``. Left+ # unset, the processor infers it from the full vocabulary for every+ # token it decodes with timestamps: seconds of CPU time per long+ # recording, more than the transcription itself.+ processor.decoder_type = "tdt"+ self._processor = processor+ self._model = model+ self._preprocessor = AudioPreprocessor()++ def _resolve_dtype(self) -> torch.dtype:+ if not self._device or not self._device.startswith("cuda"):+ return torch.float32+ return {+ "float16": torch.float16,+ "bfloat16": torch.bfloat16,+ "float32": torch.float32,+ }.get(self._compute_precision, torch.bfloat16)++ def get_preprocessor(self) -> AudioPreprocessor | None:+ return self._preprocessor++ def encode(+ self,+ items: list[Item],+ output_types: list[str],+ *,+ instruction: str | None = None,+ is_query: bool = False,+ prepared_items: list[Any] | None = None,+ options: dict[str, Any] | None = None,+ ) -> EncodeOutput:+ raise NotImplementedError(_ERR_ENCODE_NOT_SUPPORTED)++ def extract(+ self,+ items: list[Item],+ *,+ labels: list[str] | None = None,+ output_schema: dict[str, Any] | None = None,+ instruction: str | None = None,+ options: dict[str, Any] | None = None,+ prepared_items: list[Any] | None = None,+ ) -> ExtractOutput:+ self._check_loaded()+ if self._processor is None:+ msg = "ParakeetTDTAdapter is not loaded"+ raise RuntimeError(msg)+ if labels:+ msg = "Parakeet transcription does not accept entity labels"+ raise ValueError(msg)+ if output_schema:+ msg = "Parakeet transcription does not accept output_schema"+ raise ValueError(msg)+ granularities = _parse_request(options, instruction)+ payloads = _audio_payloads(items, prepared_items)+ for payload in payloads:+ if payload.sample_rate != self._sample_rate:+ msg = f"Parakeet transcription requires {self._sample_rate} Hz audio, got {payload.sample_rate} Hz"+ raise ValueError(msg)++ waveforms = [_waveform(payload) for payload in payloads]+ transcripts = self._transcribe(waveforms)+ data = [+ self._transcript_data(transcript, payload, granularities)+ for transcript, payload in zip(transcripts, payloads, strict=True)+ ]+ return ExtractOutput(+ entities=[[] for _ in data],+ data=data,+ batch_size=len(data),+ )++ def _transcribe(self, waveforms: list[np.ndarray]) -> list[_Transcript]:+ transcripts = [_Transcript() for _ in waveforms]+ # A clip too short to normalize holds no speech; it is returned empty.+ decodable = [index for index, waveform in enumerate(waveforms) if waveform.shape[0] >= self._min_samples]+ budget = self._max_padded_batch_ms * self._sample_rate // 1_000+ for chunk in _length_chunks([waveforms[index].shape[0] for index in decodable], budget):+ rows = [decodable[position] for position in chunk]+ for row, transcript in zip(rows, self._decode([waveforms[row] for row in rows]), strict=True):+ transcripts[row] = transcript++ retry_max_samples = round(_EMPTY_RETRY_MAX_S * self._sample_rate)+ retried = 0+ for row in decodable:+ if transcripts[row].text or waveforms[row].shape[0] > retry_max_samples:+ continue+ retried += 1+ for leading_s, trailing_s in _EMPTY_RETRY_PADDING_S:+ padded = _pad(waveforms[row], self._sample_rate, leading_s, trailing_s)+ (transcript,) = self._decode([padded], offset_s=leading_s)+ if transcript.text:+ transcripts[row] = transcript+ break+ if retried:+ logger.debug("Re-decoded %d empty Parakeet transcript(s) with silence padding", retried)+ return transcripts++ def _decode(self, waveforms: list[np.ndarray], *, offset_s: float = 0.0) -> list[_Transcript]:+ with torch.inference_mode():+ inputs = self._processor(audio=waveforms, sampling_rate=self._sample_rate, return_tensors="pt")+ features = inputs["input_features"].to(self._device, dtype=self._dtype)+ attention_mask = inputs["attention_mask"].to(self._device)+ encoded = self._model.get_audio_features(+ input_features=features,+ attention_mask=attention_mask,+ output_attention_mask=True,+ )+ encoder_output = encoded.pooler_output+ if encoded.attention_mask is not None:+ lengths = encoded.attention_mask.sum(dim=-1)+ else:+ lengths = torch.full((encoder_output.shape[0],), encoder_output.shape[1], dtype=torch.long)+ hypotheses = greedy_tdt_decode(+ self._model.decoder,+ self._model.joint,+ encoder_output,+ lengths,+ blank_id=self._blank_id,+ vocab_size=self._vocab_size,+ durations=self._durations,+ )+ if len(hypotheses) != len(waveforms):+ msg = "Parakeet decoding returned a misaligned batch"+ raise RuntimeError(msg)+ with self._tokenizer_guard():+ # The processor keeps repeated tokens; the bare tokenizer would merge+ # them as if the output were CTC.+ texts = self._processor.batch_decode(+ [hypothesis.token_ids for hypothesis in hypotheses],+ skip_special_tokens=True,+ group_tokens=False,+ )+ return [+ _Transcript(text=str(text).strip(), hypothesis=hypothesis, offset_s=offset_s)+ for text, hypothesis in zip(texts, hypotheses, strict=True)+ ]++ def _transcript_data(+ self,+ transcript: _Transcript,+ payload: AudioPayload,+ granularities: frozenset[str],+ ) -> dict[str, Any]:+ data: dict[str, Any] = {+ "text": transcript.text,+ "language": None,+ "duration_ms": payload.duration_ms,+ }+ if not granularities:+ return data+ words = _words_from_tokens(self._timed_tokens(transcript, payload.duration_s))+ if "word" in granularities:+ data["words"] = words+ if "segment" in granularities:+ data["segments"] = _segments_from_words(words)+ return data++ def _timed_tokens(self, transcript: _Transcript, duration_s: float) -> list[dict[str, Any]]:+ """Token start/end seconds from each decode step's frame and duration."""+ hypothesis = transcript.hypothesis+ if not transcript.text or not hypothesis.step_ids:+ return []+ with self._tokenizer_guard():+ _, offsets = self._processor.decode(+ torch.tensor([hypothesis.step_ids], dtype=torch.long),+ durations=torch.tensor([hypothesis.step_durations], dtype=torch.long),+ skip_special_tokens=True,+ group_tokens=False,+ )+ tokens = []+ for offset in offsets[0]:+ start = min(max(float(offset["start"]) - transcript.offset_s, 0.0), duration_s)+ end = min(max(float(offset["end"]) - transcript.offset_s, start), duration_s)+ tokens.append({"token": str(offset["token"]), "start": start, "end": end})+ return tokens+++def _audio_payloads(items: list[Item], prepared_items: list[Any] | None) -> list[AudioPayload]:+ if prepared_items is None or len(prepared_items) != len(items):+ msg = "ParakeetTDTAdapter requires one Rust-prepared audio payload per item"+ raise ValueError(msg)+ payloads = []+ for prepared in prepared_items:+ payload = getattr(prepared, "payload", None)+ if not isinstance(payload, AudioPayload):+ msg = "ParakeetTDTAdapter received a non-audio prepared item"+ raise TypeError(msg)+ payloads.append(payload)+ return payloads+++def _parse_request(options: dict[str, Any] | None, instruction: str | None) -> frozenset[str]:+ """Validate the request options and return the timestamp granularities."""+ if instruction is not None and instruction.strip():+ msg = "this model does not accept a prompt (instruction); omit it"+ raise ValueError(msg)+ options = options or {}+ unknown = set(options) - _RUNTIME_OPTIONS+ if unknown:+ msg = f"unsupported Parakeet options: {', '.join(sorted(unknown))}"+ raise ValueError(msg)++ language = options.get("language")+ if language is not None and (not isinstance(language, str) or language.strip().lower() not in SUPPORTED_LANGUAGES):+ msg = (+ f"language {language!r} is not supported: this model transcribes {len(SUPPORTED_LANGUAGES)} languages "+ f"({', '.join(sorted(SUPPORTED_LANGUAGES))}; ISO 639-1 codes) and identifies the spoken one itself, "+ "so language may be omitted"+ )+ raise ValueError(msg)++ temperature = options.get("temperature")+ if temperature is not None and (+ isinstance(temperature, bool) or not isinstance(temperature, int | float) or temperature != 0+ ):+ msg = "this model decodes greedily; temperature must be 0 or omitted"+ raise ValueError(msg)++ raw_granularities = options.get("timestamp_granularities") or []+ if not isinstance(raw_granularities, list | tuple) or not all(+ isinstance(value, str) for value in raw_granularities+ ):+ msg = "timestamp_granularities must be a list of strings"+ raise ValueError(msg)+ granularities = frozenset(raw_granularities)+ unsupported = granularities - _TIMESTAMP_GRANULARITIES+ if unsupported:+ msg = f"unsupported timestamp granularities: {', '.join(sorted(unsupported))}"+ raise ValueError(msg)+ return granularities+++def _waveform(payload: AudioPayload) -> np.ndarray:+ return np.frombuffer(payload.pcm_s16le, dtype="<i2").astype(np.float32) / 32_768.0+++def _pad(waveform: np.ndarray, sample_rate: int, leading_s: float, trailing_s: float) -> np.ndarray:+ leading = np.zeros(round(leading_s * sample_rate), dtype=np.float32)+ trailing = np.zeros(round(trailing_s * sample_rate), dtype=np.float32)+ return np.concatenate([leading, waveform, trailing])+++def _length_chunks(lengths: Sequence[int], max_padded: int) -> list[list[int]]:+ """Group indices, longest first, so ``rows * longest row`` stays within ``max_padded``.++ A row longer than the budget is decoded on its own.+ """+ chunks: list[list[int]] = []+ current: list[int] = []+ for index in sorted(range(len(lengths)), key=lambda position: lengths[position], reverse=True):+ if current and (len(current) + 1) * lengths[current[0]] > max_padded:+ chunks.append(current)+ current = []+ current.append(index)+ if current:+ chunks.append(current)+ return chunks+++def _words_from_tokens(tokens: list[dict[str, Any]]) -> list[dict[str, Any]]:+ """Merge timed tokens into words: a token that starts with a space opens a new word."""+ merged: list[dict[str, Any]] = []+ for token in tokens:+ piece = token["token"]+ if not merged or piece.startswith(" "):+ merged.append({"word": piece, "start": token["start"], "end": token["end"]})+ else:+ merged[-1]["word"] += piece+ merged[-1]["end"] = max(merged[-1]["end"], token["end"])+ words = []+ for word in merged:+ text = word["word"].strip()+ if text:+ words.append({"word": text, "start": round(word["start"], 3), "end": round(word["end"], 3)})+ return words+++def _segments_from_words(words: list[dict[str, Any]]) -> list[dict[str, Any]]:+ """Split words into segments at sentence-final punctuation or after 30 seconds."""+ segments: list[dict[str, Any]] = []+ current: list[dict[str, Any]] = []+ for word in words:+ current.append(word)+ if _SENTENCE_END_RE.search(word["word"]) or word["end"] - current[0]["start"] >= _MAX_SEGMENT_S:+ segments.append(_segment(current, len(segments)))+ current = []+ if current:+ segments.append(_segment(current, len(segments)))+ return segments+++def _segment(words: list[dict[str, Any]], index: int) -> dict[str, Any]:+ return {+ "id": index,+ "start": words[0]["start"],+ "end": words[-1]["end"],+ "text": " ".join(word["word"] for word in words),+ }diff --git a/packages/sie_server/src/sie_server/adapters/parakeet/decoding.py b/packages/sie_server/src/sie_server/adapters/parakeet/decoding.pynew file mode 100644index 000000000..0cde9c186--- /dev/null+++ b/packages/sie_server/src/sie_server/adapters/parakeet/decoding.py@@ -0,0 +1,151 @@+"""Batched greedy decoding for token-and-duration transducers (TDT).++The loop follows NeMo's greedy TDT decoder. At each step the joint network+scores the current encoder frame against the prediction network's state and+predicts a token and a duration (how many encoder frames to advance):++* The token is the argmax over the vocabulary slice of the joint output (blank+ included); the duration is ``durations[argmax]`` over the remaining logits.+* A blank never stays on its frame: a blank with duration 0 advances one frame.+* Only rows that emit a token feed it to the prediction network; the other+ rows keep their state.+* A row that emits ``max_symbols_per_frame`` tokens in a row without advancing+ is forced one frame forward. This bounds the decode at+ ``max_symbols_per_frame`` steps per frame and stops a row from repeating a+ token forever.+* Each row stops at its own encoder length and never reads padded frames.+"""++from __future__ import annotations++from collections.abc import Sequence+from dataclasses import dataclass, field+from typing import Any++import torch++MAX_SYMBOLS_PER_FRAME = 10+++@dataclass(slots=True)+class TdtHypothesis:+ """One row's greedy decode.++ ``token_ids`` are the emitted (non-blank) tokens. ``step_ids`` and+ ``step_durations`` record every decode step, blanks included, with the+ number of encoder frames it advanced; their running sum is each step's+ frame index, which the processor's timestamp decoding consumes.+ """++ token_ids: list[int] = field(default_factory=list)+ step_ids: list[int] = field(default_factory=list)+ step_durations: list[int] = field(default_factory=list)++[email protected]_mode()+def greedy_tdt_decode(+ decoder: Any,+ joint: Any,+ encoder_output: torch.Tensor,+ lengths: torch.Tensor,+ *,+ blank_id: int,+ vocab_size: int,+ durations: Sequence[int],+ max_symbols_per_frame: int = MAX_SYMBOLS_PER_FRAME,+) -> list[TdtHypothesis]:+ """Decode a padded batch of encoder outputs.++ Args:+ decoder: Prediction network with ``embedding``, ``lstm`` (batch-first)+ and ``decoder_projector``.+ joint: Joint network with ``activation`` and ``head``; ``head`` returns+ ``vocab_size`` token logits followed by one logit per duration.+ encoder_output: Projected encoder states, shape ``(batch, frames, hidden)``.+ lengths: Valid encoder frames per row, shape ``(batch,)``.+ blank_id: Blank token id (inside the vocabulary slice).+ vocab_size: Width of the token slice of the joint output.+ durations: Frame advance for each duration logit.+ max_symbols_per_frame: Consecutive emissions allowed on one frame+ before the row is forced to advance.+ """+ if max_symbols_per_frame < 1:+ msg = "max_symbols_per_frame must be positive"+ raise ValueError(msg)+ batch, frames, _ = encoder_output.shape+ hypotheses = [TdtHypothesis() for _ in range(batch)]+ if batch == 0 or frames == 0:+ return hypotheses++ device = encoder_output.device+ lstm = decoder.lstm+ hidden = torch.zeros(+ (lstm.num_layers, batch, lstm.hidden_size),+ device=device,+ dtype=encoder_output.dtype,+ )+ cell = torch.zeros_like(hidden)+ # The prediction network starts from a zero state fed the blank token.+ start = torch.full((batch, 1), blank_id, device=device, dtype=torch.long)+ output, (hidden, cell) = lstm(decoder.embedding(start), (hidden, cell))+ prediction = decoder.decoder_projector(output)[:, 0]++ duration_values = torch.as_tensor(list(durations), device=device, dtype=torch.long)+ rows = torch.arange(batch, device=device)+ lengths = lengths.to(device=device, dtype=torch.long)+ frame = torch.zeros(batch, device=device, dtype=torch.long)+ symbols = torch.zeros_like(frame)+ one = torch.ones_like(frame)+ zero = torch.zeros_like(frame)+ active = frame < lengths++ step_tokens: list[torch.Tensor] = []+ step_durations: list[torch.Tensor] = []+ step_active: list[torch.Tensor] = []+ step_emitted: list[torch.Tensor] = []+ while bool(active.any()):+ encoded = encoder_output[rows, frame.clamp(max=frames - 1)]+ logits = joint.head(joint.activation(encoded + prediction)).float()+ token = logits[:, :vocab_size].argmax(dim=-1)+ duration = duration_values[logits[:, vocab_size:].argmax(dim=-1)]+ blank = token == blank_id+ duration = torch.where(blank & (duration == 0), one, duration)+ emitted = active & ~blank+ symbols = torch.where(emitted & (duration == 0), symbols + 1, zero)+ forced = symbols >= max_symbols_per_frame+ duration = torch.where(forced, one, duration)+ symbols = torch.where(forced, zero, symbols)++ step_tokens.append(token)+ step_durations.append(duration)+ step_active.append(active)+ step_emitted.append(emitted)++ if bool(emitted.any()):+ output, (next_hidden, next_cell) = lstm(decoder.embedding(token[:, None]), (hidden, cell))+ next_prediction = decoder.decoder_projector(output)[:, 0]+ state_mask = emitted.view(1, batch, 1)+ hidden = torch.where(state_mask, next_hidden, hidden)+ cell = torch.where(state_mask, next_cell, cell)+ prediction = torch.where(emitted.view(batch, 1), next_prediction, prediction)++ frame = frame + torch.where(active, duration, zero)+ active = frame < lengths++ if not step_tokens:+ return hypotheses+ tokens = torch.stack(step_tokens, dim=1).tolist()+ advances = torch.stack(step_durations, dim=1).tolist()+ was_active = torch.stack(step_active, dim=1).tolist()+ was_emitted = torch.stack(step_emitted, dim=1).tolist()+ for row, hypothesis in enumerate(hypotheses):+ for token_id, advance, row_active, row_emitted in zip(+ tokens[row], advances[row], was_active[row], was_emitted[row], strict=True+ ):+ if not row_active:+ continue+ hypothesis.step_ids.append(token_id)+ hypothesis.step_durations.append(advance)+ if row_emitted:+ hypothesis.token_ids.append(token_id)+ return hypothesesdiff --git a/packages/sie_server/tests/adapters/test_parakeet.py b/packages/sie_server/tests/adapters/test_parakeet.pynew file mode 100644index 000000000..ed79a6d7e--- /dev/null+++ b/packages/sie_server/tests/adapters/test_parakeet.py@@ -0,0 +1,748 @@+from __future__ import annotations++import dataclasses+import re+import sys+from pathlib import Path+from types import SimpleNamespace+from typing import Any+from unittest.mock import MagicMock++import numpy as np+import pytest+import torch+import yaml+from packaging.specifiers import SpecifierSet+from sie_sdk.bundle_utils import match_bundle_models+from sie_server.adapters.parakeet import adapter as parakeet_adapter+from sie_server.adapters.parakeet.adapter import (+ _EMPTY_RETRY_MAX_S,+ SUPPORTED_LANGUAGES,+ ParakeetTDTAdapter,+ _length_chunks,+ _segments_from_words,+ _Transcript,+ _words_from_tokens,+)+from sie_server.adapters.parakeet.decoding import TdtHypothesis, greedy_tdt_decode+from sie_server.bundle_requirements import resolve_bundle_requirements+from sie_server.core.loader import load_model_configs, reject_unknown_loadtime_options, resolve_adapter_class+from sie_server.core.prepared import AudioPayload, PreparedItem+from sie_server.core.preprocessor.audio import AudioPreprocessor+from sie_server.types.inputs import Item++SERVER_ROOT = Path(__file__).resolve().parents[2]+MODELS = SERVER_ROOT / "models"+BUNDLES = SERVER_ROOT / "bundles"+MODEL_ID = "nvidia/parakeet-tdt-0.6b-v3"+BLANK = 9+VOCAB = 10+DURATIONS = (0, 1, 2, 3, 4)+++# ---------------------------------------------------------------------------+# Deterministic prediction and joint networks for the greedy loop.+#+# The prediction network's state is a counter of how many times it was+# updated (1 after the start step), carried in feature 0. Encoder frame+# features carry ``row * 1000 + frame`` in feature 1, so the joint's input+# ``frame + prediction`` tells a scripted head which row, frame and decoder+# state it is scoring.+# ---------------------------------------------------------------------------+++class _CountingLSTM(torch.nn.Module):+ num_layers = 1+ hidden_size = 2++ def forward(+ self, inputs: torch.Tensor, state: tuple[torch.Tensor, torch.Tensor]+ ) -> tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor]]:+ hidden, cell = state+ updated = hidden + torch.tensor([1.0, 0.0])+ return updated[-1].unsqueeze(1), (updated, cell)+++class _FakeDecoder:+ def __init__(self) -> None:+ self.lstm = _CountingLSTM()+ self.fed: list[list[int]] = []++ def embedding(self, ids: torch.Tensor) -> torch.Tensor:+ self.fed.append(ids[:, 0].tolist())+ return torch.zeros(ids.shape[0], 1, 2)++ def decoder_projector(self, output: torch.Tensor) -> torch.Tensor:+ return output+++class _ScriptedJoint:+ """Emit ``script(row, frame, updates) -> (token, duration_index)`` as one-hot logits."""++ def __init__(self, script: Any, *, vocab_size: int = VOCAB, num_durations: int = len(DURATIONS)) -> None:+ self.script = script+ self.vocab_size = vocab_size+ self.num_durations = num_durations+ self.seen: list[tuple[int, int, int]] = []++ def activation(self, x: torch.Tensor) -> torch.Tensor:+ return x++ def head(self, x: torch.Tensor) -> torch.Tensor:+ logits = torch.zeros(x.shape[0], self.vocab_size + self.num_durations)+ for index, (updates, code) in enumerate(x.tolist()):+ row, frame = divmod(round(code), 1000)+ self.seen.append((row, frame, round(updates)))+ token, duration_index = self.script(row, frame, round(updates))+ logits[index, token] = 1.0+ logits[index, self.vocab_size + duration_index] = 1.0+ return logits+++def _encoder(frames: int, rows: int = 1) -> torch.Tensor:+ codes = torch.tensor([[row * 1000 + frame for frame in range(frames)] for row in range(rows)], dtype=torch.float32)+ return torch.stack([torch.zeros_like(codes), codes], dim=-1)+++def _decode(+ script: Any, lengths: list[int], *, frames: int | None = None, durations: tuple[int, ...] = DURATIONS+) -> tuple[list[TdtHypothesis], _FakeDecoder, _ScriptedJoint]:+ decoder = _FakeDecoder()+ joint = _ScriptedJoint(script, num_durations=len(durations))+ hypotheses = greedy_tdt_decode(+ decoder,+ joint,+ _encoder(frames or max(lengths), len(lengths)),+ torch.tensor(lengths),+ blank_id=BLANK,+ vocab_size=VOCAB,+ durations=durations,+ )+ return hypotheses, decoder, joint+++def test_greedy_decode_follows_tokens_durations_and_blanks() -> None:+ script = {+ (0, 1): (5, 0), # emit, stay on frame 0+ (0, 2): (6, 2), # emit, advance 2+ (2, 3): (BLANK, 0), # blank with duration 0 still advances one frame+ (3, 3): (7, 1),+ }+ (hypothesis,), decoder, _ = _decode(lambda row, frame, updates: script[(frame, updates)], [4])++ assert hypothesis.token_ids == [5, 6, 7]+ assert hypothesis.step_ids == [5, 6, BLANK, 7]+ assert hypothesis.step_durations == [0, 2, 1, 1]+ # The prediction network starts from blank and is fed each emitted token.+ assert decoder.fed == [[BLANK], [5], [6], [7]]+++def test_greedy_decode_maps_duration_index_to_frames() -> None:+ (hypothesis,), _, _ = _decode(lambda row, frame, updates: (3, 1), [7], durations=(0, 3))++ assert hypothesis.step_durations == [3, 3, 3]+ assert hypothesis.token_ids == [3, 3, 3]+++def test_greedy_decode_forces_progress_after_max_symbols_on_one_frame() -> None:+ (hypothesis,), _, _ = _decode(lambda row, frame, updates: (4, 0), [3])++ # Ten emissions per frame, the tenth forced one frame forward.+ assert hypothesis.token_ids == [4] * 30+ assert hypothesis.step_durations == ([0] * 9 + [1]) * 3+++def test_greedy_decode_counts_consecutive_symbols_per_frame() -> None:+ # Nine stays, an advance by 1, then nine more stays: the counter resets on advance.+ def script(row: int, frame: int, updates: int) -> tuple[int, int]:+ return (4, 1) if updates == 10 else (4, 0)++ (hypothesis,), _, _ = _decode(script, [2])++ assert hypothesis.step_durations[:10] == [0] * 9 + [1]+ assert hypothesis.step_durations[10:] == [0] * 9 + [1]+++def test_greedy_decode_stops_each_row_at_its_own_length() -> None:+ def script(row: int, frame: int, updates: int) -> tuple[int, int]:+ if row == 0:+ return (8, 1) if frame >= 2 else (2, 1) # frames >= 2 are padding for row 0+ return (3, 1)++ hypotheses, _, _ = _decode(script, [2, 5])++ assert hypotheses[0].token_ids == [2, 2]+ assert hypotheses[1].token_ids == [3] * 5+ assert sum(hypotheses[0].step_durations) == 2+ assert sum(hypotheses[1].step_durations) == 5+++def test_greedy_decode_updates_prediction_state_only_for_emitting_rows() -> None:+ def script(row: int, frame: int, updates: int) -> tuple[int, int]:+ if row == 0 and frame == 0:+ return (5, 1)+ return (BLANK, 1)++ hypotheses, decoder, joint = _decode(script, [2, 2])++ assert [hypothesis.token_ids for hypothesis in hypotheses] == [[5], []]+ # Second step: row 0 scored with its updated state, row 1 with the start state.+ assert (0, 1, 2) in joint.seen+ assert (1, 1, 1) in joint.seen+ assert decoder.fed == [[BLANK, BLANK], [5, BLANK]]+++def test_greedy_decode_handles_empty_input_and_rejects_invalid_guard() -> None:+ assert (+ greedy_tdt_decode(+ _FakeDecoder(),+ _ScriptedJoint(lambda *_: (BLANK, 1)),+ torch.zeros(0, 3, 2),+ torch.zeros(0, dtype=torch.long),+ blank_id=BLANK,+ vocab_size=VOCAB,+ durations=DURATIONS,+ )+ == []+ )+ (hypothesis,), _, _ = _decode(lambda *_: (5, 1), [0], frames=3)+ assert hypothesis == TdtHypothesis()+ with pytest.raises(ValueError, match="max_symbols_per_frame"):+ greedy_tdt_decode(+ _FakeDecoder(),+ _ScriptedJoint(lambda *_: (BLANK, 1)),+ _encoder(2),+ torch.tensor([2]),+ blank_id=BLANK,+ vocab_size=VOCAB,+ durations=DURATIONS,+ max_symbols_per_frame=0,+ )+++# ---------------------------------------------------------------------------+# Adapter+# ---------------------------------------------------------------------------+++def _payload(*, duration_ms: int = 1_000) -> AudioPayload:+ sample_count = duration_ms * 16+ return AudioPayload(+ pcm_s16le=b"\x00\x00" * sample_count,+ sample_rate=16_000,+ sample_count=sample_count,+ duration_ms=duration_ms,+ source_sample_rate=16_000,+ source_sample_count=sample_count,+ source_channels=1,+ container="wav",+ )+++def _prepared(*payloads: AudioPayload) -> list[PreparedItem[AudioPayload]]:+ return [+ PreparedItem(payload=payload, cost=payload.duration_ms, original_index=index)+ for index, payload in enumerate(payloads)+ ]+++def _loaded_adapter(**kwargs: Any) -> ParakeetTDTAdapter:+ adapter = ParakeetTDTAdapter(MODEL_ID, **kwargs)+ adapter._model = MagicMock()+ adapter._processor = MagicMock()+ adapter._preprocessor = AudioPreprocessor()+ adapter._device = "cpu"+ adapter._blank_id = BLANK+ adapter._vocab_size = VOCAB+ adapter._durations = DURATIONS+ return adapter+++def _transcript(text: str, offset_s: float = 0.0) -> _Transcript:+ hypothesis = TdtHypothesis(token_ids=[1] if text else [], step_ids=[1] if text else [], step_durations=[1])+ return _Transcript(text=text, hypothesis=hypothesis, offset_s=offset_s)+++def test_capabilities_and_encode_contract() -> None:+ adapter = ParakeetTDTAdapter(MODEL_ID)++ assert adapter.capabilities.inputs == ["audio"]+ assert adapter.capabilities.outputs == ["json"]+ assert adapter.dims.dense is None+ assert adapter.get_preprocessor() is None+ with pytest.raises(NotImplementedError, match=r"Use extract\(\) instead"):+ adapter.encode([Item(text="hello")], ["dense"])+ with pytest.raises(ValueError, match="max_padded_batch_ms"):+ ParakeetTDTAdapter(MODEL_ID, max_padded_batch_ms=0)+++def test_extract_requires_loaded_prepared_audio() -> None:+ with pytest.raises(RuntimeError, match="Model not loaded"):+ ParakeetTDTAdapter(MODEL_ID).extract([Item()])++ adapter = _loaded_adapter()+ with pytest.raises(ValueError, match="Rust-prepared audio"):+ adapter.extract([Item()])+ with pytest.raises(TypeError, match="non-audio prepared item"):+ adapter.extract([Item()], prepared_items=[SimpleNamespace(payload=object())])+ resampled = dataclasses.replace(_payload(), sample_rate=8_000)+ with pytest.raises(ValueError, match="requires 16000 Hz audio"):+ adapter.extract([Item()], prepared_items=_prepared(resampled))+++def test_extract_rejects_labels_and_schema() -> None:+ adapter = _loaded_adapter()+ prepared = _prepared(_payload())++ with pytest.raises(ValueError, match="entity labels"):+ adapter.extract([Item()], labels=["person"], prepared_items=prepared)+ with pytest.raises(ValueError, match="output_schema"):+ adapter.extract([Item()], output_schema={"type": "object"}, prepared_items=prepared)+++ ("options", "instruction", "message"),+ [+ ({"unknown": True}, None, "unsupported Parakeet options: unknown"),+ ({"language": "ja"}, None, "this model transcribes 25 languages"),+ ({"language": ""}, None, "is not supported"),+ ({"language": 7}, None, "is not supported"),+ ({}, "domain vocabulary", "does not accept a prompt"),+ ({"temperature": 0.2}, None, "temperature must be 0 or omitted"),+ ({"temperature": True}, None, "temperature must be 0 or omitted"),+ ({"temperature": "0"}, None, "temperature must be 0 or omitted"),+ ({"timestamp_granularities": "word"}, None, "must be a list"),+ ({"timestamp_granularities": ["token"]}, None, "unsupported timestamp granularities: token"),+ ],+)+def test_extract_rejects_invalid_options(options: dict[str, Any], instruction: str | None, message: str) -> None:+ adapter = _loaded_adapter()+ adapter._decode = MagicMock()++ with pytest.raises(ValueError, match=message):+ adapter.extract([Item()], options=options, instruction=instruction, prepared_items=_prepared(_payload()))+ adapter._decode.assert_not_called()+++def test_unsupported_language_error_lists_the_supported_codes() -> None:+ adapter = _loaded_adapter()++ with pytest.raises(ValueError, match="language") as error:+ adapter.extract([Item()], options={"language": "zh"}, prepared_items=_prepared(_payload()))+ assert ", ".join(sorted(SUPPORTED_LANGUAGES)) in str(error.value)+ assert len(SUPPORTED_LANGUAGES) == 25+++ ("options", "instruction"),+ [+ ({"language": "en"}, None),+ ({"language": " DE "}, None),+ ({"language": "uk", "temperature": 0}, ""),+ ({"language": None, "temperature": 0.0}, " "),+ ({"timestamp_granularities": ("segment",)}, None),+ ({"timestamp_granularities": []}, None),+ (None, None),+ ],+)+def test_extract_accepts_supported_options(options: dict[str, Any] | None, instruction: str | None) -> None:+ adapter = _loaded_adapter()+ adapter._decode = MagicMock(return_value=[_transcript("hallo")])+ adapter._processor.decode.return_value = (["hallo"], [[{"token": "hallo", "start": 0.0, "end": 0.08}]])++ output = adapter.extract([Item()], options=options, instruction=instruction, prepared_items=_prepared(_payload()))++ assert output.data is not None+ assert output.data[0]["text"] == "hallo"+ assert output.data[0]["language"] is None+++def test_extract_returns_the_transcription_contract() -> None:+ adapter = _loaded_adapter()+ adapter._decode = MagicMock()+ first, second = _payload(duration_ms=1_000), _payload(duration_ms=2_000)+ adapter._decode.side_effect = [[_transcript("Hello world."), _transcript("")], [_transcript("")], [_transcript("")]]++ output = adapter.extract([Item(), Item()], prepared_items=_prepared(first, second))++ assert output.batch_size == 2+ assert output.entities == [[], []]+ # The batch is decoded longest first; the result keeps request order.+ assert output.data == [+ {"text": "", "language": None, "duration_ms": 1_000},+ {"text": "Hello world.", "language": None, "duration_ms": 2_000},+ ]+++def test_transcribe_groups_rows_by_padded_length_budget() -> None:+ adapter = _loaded_adapter(max_padded_batch_ms=1_000)+ calls: list[list[int]] = []++ def fake_decode(waveforms: list[np.ndarray], *, offset_s: float = 0.0) -> list[_Transcript]:+ calls.append([waveform.shape[0] for waveform in waveforms])+ return [_transcript(f"row of {waveform.shape[0]}") for waveform in waveforms]++ adapter._decode = fake_decode+ lengths = [4_800, 9_600, 24_000, 3_200, 8_000] # 0.3, 0.6, 1.5, 0.2, 0.5 s+ transcripts = adapter._transcribe([np.ones(length, dtype=np.float32) for length in lengths])++ # Longest first; a chunk holds rows * longest <= 1 s; an over-budget row runs alone.+ assert calls == [[24_000], [9_600], [8_000, 4_800], [3_200]]+ assert [transcript.text for transcript in transcripts] == [f"row of {length}" for length in lengths]+++def test_length_chunks_never_returns_an_empty_chunk() -> None:+ assert _length_chunks([], 10) == []+ assert _length_chunks([5, 5, 5], 15) == [[0, 1, 2]]+ assert _length_chunks([5, 20, 5], 15) == [[1], [0, 2]]+++def test_empty_rows_are_redecoded_alone_with_end_then_both_sides_padding() -> None:+ adapter = _loaded_adapter()+ calls: list[tuple[list[int], float]] = []+ lengths = [16_000, 8_000, 4_800, 6_400, 100]+ originals = [np.full(length, 0.5, dtype=np.float32) for length in lengths]+ # Which (original length, leading pad, trailing pad) decodes to text.+ recovers = {(16_000, 0, 0), (8_000, 0, 4_000), (4_800, 4_000, 4_000)}++ def fake_decode(waveforms: list[np.ndarray], *, offset_s: float = 0.0) -> list[_Transcript]:+ calls.append(([waveform.shape[0] for waveform in waveforms], offset_s))+ results = []+ for waveform in waveforms:+ nonzero = np.flatnonzero(waveform)+ leading, trailing = int(nonzero[0]), waveform.shape[0] - int(nonzero[-1]) - 1+ original = waveform[leading : waveform.shape[0] - trailing]+ assert np.all(waveform[:leading] == 0)+ assert np.all(original == 0.5)+ key = (original.shape[0], leading, trailing)+ results.append(_transcript(f"text {original.shape[0]}" if key in recovers else "", offset_s))+ return results++ adapter._decode = fake_decode+ transcripts = adapter._transcribe(originals)++ assert calls == [+ ([16_000, 8_000, 6_400, 4_800], 0.0), # one batched pass; the 100-sample clip is too short to decode+ ([12_000], 0.0), # row 1 recovers with 0.25 s appended+ ([8_800], 0.0), # row 2: still empty with 0.25 s appended ...+ ([12_800], 0.25), # ... recovers with 0.25 s on both sides+ ([10_400], 0.0), # row 3 stays empty+ ([14_400], 0.25),+ ]+ assert [transcript.text for transcript in transcripts] == ["text 16000", "text 8000", "text 4800", "", ""]+ assert [transcript.offset_s for transcript in transcripts] == [0.0, 0.0, 0.25, 0.0, 0.0]+++def test_empty_rows_longer_than_thirty_seconds_are_not_redecoded() -> None:+ adapter = _loaded_adapter()+ calls: list[list[int]] = []++ def fake_decode(waveforms: list[np.ndarray], *, offset_s: float = 0.0) -> list[_Transcript]:+ calls.append([waveform.shape[0] for waveform in waveforms])+ return [_transcript("", offset_s) for _ in waveforms]++ adapter._decode = fake_decode+ limit = round(_EMPTY_RETRY_MAX_S * 16_000)+ transcripts = adapter._transcribe([np.ones(limit + 1, dtype=np.float32), np.ones(limit, dtype=np.float32)])++ assert _EMPTY_RETRY_MAX_S == 30.0+ # The row just over 30 s gets only the batched pass; the 30 s row is retried twice.+ assert calls == [[limit + 1, limit], [limit + 4_000], [limit + 8_000]]+ assert [transcript.text for transcript in transcripts] == ["", ""]+++def test_decode_runs_encoder_then_greedy_loop_and_processor_text(monkeypatch: pytest.MonkeyPatch) -> None:+ adapter = _loaded_adapter()+ adapter._dtype = torch.float64+ features = torch.ones(2, 6, 4)+ mask = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0]], dtype=torch.bool)+ adapter._processor.return_value = {"input_features": features, "attention_mask": mask}+ adapter._processor.batch_decode.return_value = [" Hello world. ", ""]+ encoder_output = torch.zeros(2, 3, 2)+ adapter._model.get_audio_features.return_value = SimpleNamespace(+ pooler_output=encoder_output,+ attention_mask=torch.tensor([[1, 1, 1], [1, 0, 0]], dtype=torch.int32),+ )+ hypotheses = [TdtHypothesis(token_ids=[1, 2]), TdtHypothesis()]+ greedy = MagicMock(return_value=hypotheses)+ monkeypatch.setattr(parakeet_adapter, "greedy_tdt_decode", greedy)+ waveforms = [np.zeros(960, dtype=np.float32), np.zeros(480, dtype=np.float32)]++ transcripts = adapter._decode(waveforms, offset_s=0.25)++ processor_kwargs = adapter._processor.call_args.kwargs+ assert processor_kwargs["audio"] is waveforms+ assert (processor_kwargs["sampling_rate"], processor_kwargs["return_tensors"]) == (16_000, "pt")+ encoder_kwargs = adapter._model.get_audio_features.call_args.kwargs+ assert encoder_kwargs["input_features"].dtype == torch.float64+ assert torch.equal(encoder_kwargs["attention_mask"], mask)+ assert encoder_kwargs["output_attention_mask"] is True+ args, kwargs = greedy.call_args+ assert args[0] is adapter._model.decoder+ assert args[1] is adapter._model.joint+ assert args[2] is encoder_output+ assert args[3].tolist() == [3, 1]+ assert kwargs == {"blank_id": BLANK, "vocab_size": VOCAB, "durations": DURATIONS}+ adapter._processor.batch_decode.assert_called_once_with([[1, 2], []], skip_special_tokens=True, group_tokens=False)+ assert [(t.text, t.hypothesis, t.offset_s) for t in transcripts] == [+ ("Hello world.", hypotheses[0], 0.25),+ ("", hypotheses[1], 0.25),+ ]+++def _timed(token: str, start: float, end: float) -> dict[str, Any]:+ return {"token": token, "start": start, "end": end}+++def test_word_and_segment_timestamps_come_from_processor_token_offsets() -> None:+ adapter = _loaded_adapter()+ hypothesis = TdtHypothesis(token_ids=[4, 5, 6], step_ids=[4, BLANK, 5, 6], step_durations=[1, 2, 1, 0])+ adapter._decode = MagicMock(return_value=[_Transcript(text="Hello world. Next", hypothesis=hypothesis)])+ adapter._processor.decode.return_value = (+ ["Hello world. Next"],+ [+ [+ _timed("Hel", 0.0, 0.08),+ _timed("lo", 0.08, 0.16),+ _timed(" world", 0.32, 0.48),+ _timed(".", 0.48, 0.48),+ _timed(" Next", 0.56, 0.64),+ ]+ ],+ )++ output = adapter.extract(+ [Item()],+ options={"timestamp_granularities": ["word", "segment"]},+ prepared_items=_prepared(_payload(duration_ms=1_000)),+ )++ (ids,) = adapter._processor.decode.call_args.args+ assert ids.tolist() == [[4, BLANK, 5, 6]]+ decode_kwargs = adapter._processor.decode.call_args.kwargs+ assert decode_kwargs["durations"].tolist() == [[1, 2, 1, 0]]+ assert decode_kwargs["skip_special_tokens"] is True+ assert decode_kwargs["group_tokens"] is False+ assert output.data == [+ {+ "text": "Hello world. Next",+ "language": None,+ "duration_ms": 1_000,+ "words": [+ {"word": "Hello", "start": 0.0, "end": 0.16},+ {"word": "world.", "start": 0.32, "end": 0.48},+ {"word": "Next", "start": 0.56, "end": 0.64},+ ],+ "segments": [+ {"id": 0, "start": 0.0, "end": 0.48, "text": "Hello world."},+ {"id": 1, "start": 0.56, "end": 0.64, "text": "Next"},+ ],+ }+ ]+++def test_timestamps_remove_leading_padding_and_stay_within_the_recording() -> None:+ adapter = _loaded_adapter()+ hypothesis = TdtHypothesis(token_ids=[4, 5], step_ids=[4, 5], step_durations=[4, 4])+ adapter._decode = MagicMock(+ side_effect=[+ [_Transcript()],+ [_Transcript()],+ [_Transcript(text="Oh no", hypothesis=hypothesis, offset_s=0.25)],+ ]+ )+ adapter._processor.decode.return_value = (["Oh no"], [[_timed("Oh", 0.08, 0.4), _timed(" no", 0.4, 0.72)]])++ output = adapter.extract(+ [Item()],+ options={"timestamp_granularities": ["segment"]},+ prepared_items=_prepared(_payload(duration_ms=300)),+ )++ assert output.data == [+ {+ "text": "Oh no",+ "language": None,+ "duration_ms": 300,+ "segments": [{"id": 0, "start": 0.0, "end": 0.3, "text": "Oh no"}],+ }+ ]+++def test_empty_transcript_has_empty_timestamps_without_decoding_offsets() -> None:+ adapter = _loaded_adapter()+ adapter._decode = MagicMock(return_value=[_Transcript()])++ output = adapter.extract(+ [Item()],+ options={"timestamp_granularities": ["word", "segment"]},+ prepared_items=_prepared(_payload()),+ )++ assert output.data == [{"text": "", "language": None, "duration_ms": 1_000, "words": [], "segments": []}]+ adapter._processor.decode.assert_not_called()+++def test_words_merge_tokens_at_leading_spaces() -> None:+ words = _words_from_tokens(+ [+ _timed("¿", 0.0, 0.0),+ _timed("Qué", 0.0, 0.16),+ _timed("?", 0.16, 0.16),+ _timed(" ", 0.24, 0.32),+ _timed("Bien", 0.32, 0.4),+ _timed(" 3", 0.48, 0.56),+ _timed(".5", 0.56, 0.6400000000000001),+ ]+ )++ assert words == [+ {"word": "¿Qué?", "start": 0.0, "end": 0.16},+ {"word": "Bien", "start": 0.24, "end": 0.4},+ {"word": "3.5", "start": 0.48, "end": 0.64},+ ]+++def test_segments_split_at_sentence_ends_and_after_thirty_seconds() -> None:+ def word(text: str, start: float, end: float) -> dict[str, Any]:+ return {"word": text, "start": start, "end": end}++ words = [+ word("Wait!", 0.0, 0.4),+ word('"Really?"', 0.5, 1.0),+ word("Yes…", 1.2, 1.5),+ word("long", 2.0, 20.0),+ word("talk", 20.0, 32.0),+ word("tail", 32.5, 33.0),+ ]++ assert _segments_from_words(words) == [+ {"id": 0, "start": 0.0, "end": 0.4, "text": "Wait!"},+ {"id": 1, "start": 0.5, "end": 1.0, "text": '"Really?"'},+ {"id": 2, "start": 1.2, "end": 1.5, "text": "Yes…"},+ {"id": 3, "start": 2.0, "end": 32.0, "text": "long talk"},+ {"id": 4, "start": 32.5, "end": 33.0, "text": "tail"},+ ]+ assert _segments_from_words([]) == []+++def _fake_transformers(+ monkeypatch: pytest.MonkeyPatch, *, decoder_type: str | None = None, version: str = "5.18.0"+) -> tuple[MagicMock, MagicMock, MagicMock]:+ processor = MagicMock()+ processor.feature_extractor.sampling_rate = 16_000+ processor.feature_extractor.hop_length = 160+ processor.decoder_type = decoder_type+ model = MagicMock()+ model.config = SimpleNamespace(blank_token_id=8192, vocab_size=8193, durations=[0, 1, 2, 3, 4])+ auto_processor = MagicMock()+ auto_processor.from_pretrained.return_value = processor+ auto_model = MagicMock()+ auto_model.from_pretrained.return_value = model+ # Stand-in for transformers 5.x (AutoModelForTDT); the unit environment may carry 4.x.+ monkeypatch.setitem(+ sys.modules,+ "transformers",+ SimpleNamespace(__version__=version, AutoProcessor=auto_processor, AutoModelForTDT=auto_model),+ )+ return auto_processor, auto_model, model+++ ("device", "precision", "dtype"),+ [("cpu", "bfloat16", torch.float32), ("cuda:0", "bfloat16", torch.bfloat16), ("cuda", "float16", torch.float16)],+)+def test_load_pins_revision_and_reads_the_checkpoint_layout(+ monkeypatch: pytest.MonkeyPatch, device: str, precision: str, dtype: torch.dtype+) -> None:+ auto_processor, auto_model, model = _fake_transformers(monkeypatch)+ adapter = ParakeetTDTAdapter(MODEL_ID, compute_precision=precision, revision="abc123")++ adapter.load(device)++ auto_processor.from_pretrained.assert_called_once_with(MODEL_ID, revision="abc123")+ auto_model.from_pretrained.assert_called_once_with(MODEL_ID, dtype=dtype, revision="abc123")+ model.to.assert_called_once_with(device)+ model.eval.assert_called_once_with()+ assert adapter._dtype == dtype+ assert (adapter._blank_id, adapter._vocab_size, adapter._durations) == (8192, 8193, (0, 1, 2, 3, 4))+ assert (adapter._sample_rate, adapter._min_samples) == (16_000, 320)+ # The checkpoint's processor config has no decoder type; it is pinned once.+ assert adapter._processor.decoder_type == "tdt"+ assert isinstance(adapter.get_preprocessor(), AudioPreprocessor)++ adapter.unload()+ assert adapter._model is None+ assert adapter._processor is None+++def test_load_keeps_a_decoder_type_the_processor_declares(monkeypatch: pytest.MonkeyPatch) -> None:+ auto_processor, _, _ = _fake_transformers(monkeypatch, decoder_type="rnnt")+ adapter = ParakeetTDTAdapter(MODEL_ID)++ adapter.load("cpu")++ assert auto_processor.from_pretrained.return_value.decoder_type == "rnnt"++[email protected]("version", ["5.14.1", "5.17.0", "5.18.0.dev0"])+def test_load_refuses_transformers_that_mask_padding_with_negative_infinity(+ monkeypatch: pytest.MonkeyPatch, version: str+) -> None:+ auto_processor, auto_model, _ = _fake_transformers(monkeypatch, version=version)+ adapter = ParakeetTDTAdapter(MODEL_ID)++ with pytest.raises(RuntimeError, match=rf"requires transformers>=5\.18, found {re.escape(version)}"):+ adapter.load("cpu")++ auto_processor.from_pretrained.assert_not_called()+ auto_model.from_pretrained.assert_not_called()+ assert adapter._model is None++[email protected]("version", ["5.18.0", "5.19.0"])+def test_load_accepts_transformers_with_finite_padding_mask(monkeypatch: pytest.MonkeyPatch, version: str) -> None:+ _, auto_model, model = _fake_transformers(monkeypatch, version=version)+ adapter = ParakeetTDTAdapter(MODEL_ID)++ adapter.load("cpu")++ auto_model.from_pretrained.assert_called_once()+ assert adapter._model is model+++# ---------------------------------------------------------------------------+# Catalog and bundle+# ---------------------------------------------------------------------------+++def test_model_config_is_pinned_audio_extract_and_routed_by_transformers5() -> None:+ config = load_model_configs(MODELS)[MODEL_ID]++ assert config.hf_id == MODEL_ID+ assert config.hf_revision == "541d1f99c6b0c3cd0b11a95167540bb8edefd82b"+ assert config.inputs.audio is True+ assert config.inputs.text is False+ assert config.tasks.extract is not None+ assert config.tasks.encode is None+ assert config.tasks.score is None+ profile = config.resolve_profile("default")+ assert profile.adapter_path == "sie_server.adapters.parakeet.adapter:ParakeetTDTAdapter"+ assert profile.compute_precision == "bfloat16"+ # One 12-minute request (SIE's per-request audio cap) per extract call.+ assert profile.max_batch_tokens == 12 * 60 * 1_000+ adapter_class = resolve_adapter_class(config, MODELS)+ assert adapter_class is ParakeetTDTAdapter+ reject_unknown_loadtime_options(adapter_class, profile.loadtime, model_name=MODEL_ID)+ assert MODEL_ID in match_bundle_models(BUNDLES / "transformers5.yaml", MODELS)+ assert MODEL_ID not in match_bundle_models(BUNDLES / "default.yaml", MODELS)+++def test_transformers5_bundle_carries_the_parakeet_runtime() -> None:+ bundle = yaml.safe_load((BUNDLES / "transformers5.yaml").read_text())+ requirements = resolve_bundle_requirements(bundle["deps"])++ assert "sie_server.adapters.parakeet.adapter" in bundle["adapters"]+ # The bundle's range admits a transformers release the adapter loads on.+ assert parakeet_adapter._MIN_TRANSFORMERS_VERSION in SpecifierSet(bundle["deps"]["transformers"])+ assert "librosa==1.0.0" in requirements