Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

fix(whisper): restore SDPA attention after word-timestamp requests (#711)

Shai Shmuel44b51dc9e329
diff --git a/packages/sie_server/src/sie_server/adapters/whisper/adapter.py b/packages/sie_server/src/sie_server/adapters/whisper/adapter.py
index 3fbcc818c..04a53c6c8 100644
--- a/packages/sie_server/src/sie_server/adapters/whisper/adapter.py
+++ b/packages/sie_server/src/sie_server/adapters/whisper/adapter.py
@@ -192,15 +192,22 @@ class WhisperAdapter(BaseAdapter):
timestamp_mode = "word"
else:
timestamp_mode = long_form or "segment" in granularities
- group_outputs = self._pipeline(
- [pipeline_inputs[index] for index in indices],
- # Long-form features keep each recording's full frame count, and
- # the pipeline cannot pad different lengths into one batch.
- batch_size=1 if long_form else self._pipeline_batch_size,
- return_timestamps=timestamp_mode,
- return_language=True,
- generate_kwargs=generation_kwargs,
- )
+ attn_implementation = self._model.config._attn_implementation
+ try:
+ group_outputs = self._pipeline(
+ [pipeline_inputs[index] for index in indices],
+ # Long-form features keep each recording's full frame count, and
+ # the pipeline cannot pad different lengths into one batch.
+ batch_size=1 if long_form else self._pipeline_batch_size,
+ return_timestamps=timestamp_mode,
+ return_language=True,
+ generate_kwargs=generation_kwargs,
+ )
+ finally:
+ # Word timestamps make Whisper's generate() switch the shared
+ # model config to eager attention, and it never switches back,
+ # so every later batch in this process would run eager too.
+ self._model.config._attn_implementation = attn_implementation
if isinstance(group_outputs, dict):
group_outputs = [group_outputs]
if len(group_outputs) != len(indices):
diff --git a/packages/sie_server/tests/adapters/test_whisper.py b/packages/sie_server/tests/adapters/test_whisper.py
index 9b9258960..c5356e4d9 100644
--- a/packages/sie_server/tests/adapters/test_whisper.py
+++ b/packages/sie_server/tests/adapters/test_whisper.py
@@ -216,6 +216,61 @@ def test_extract_requests_segment_timestamps_for_short_audio() -> None:
assert output.data[0]["segments"] == [{"id": 0, "start": 0.0, "end": 0.9, "text": "hello"}]
+def _switch_to_eager_like_whisper_generate(
+ adapter: WhisperAdapter,
+ seen: list[str],
+ *,
+ error: Exception | None = None,
+) -> Any:
+ """Pipeline stand-in that changes the config the way Whisper's generate() does for token timestamps."""
+
+ def run(inputs: list[dict[str, Any]], **kwargs: Any) -> list[dict[str, Any]]:
+ config = adapter._model.config
+ seen.append(config._attn_implementation)
+ if kwargs["return_timestamps"] == "word":
+ config._attn_implementation = "eager"
+ if error is not None:
+ raise error
+ return [{"text": "hello", "chunks": []} for _ in inputs]
+
+ return run
+
+
+def test_extract_restores_attention_after_word_timestamps() -> None:
+ adapter, pipeline = _loaded_adapter(None)
+ adapter._model = SimpleNamespace(config=SimpleNamespace(_attn_implementation="sdpa"))
+ seen: list[str] = []
+ pipeline.side_effect = _switch_to_eager_like_whisper_generate(adapter, seen)
+ short, long = _payload(duration_ms=1_000), _payload(duration_ms=31_000)
+ prepared = [
+ PreparedItem(payload=short, cost=short.duration_ms, original_index=0),
+ PreparedItem(payload=long, cost=long.duration_ms, original_index=1),
+ ]
+
+ adapter.extract([Item(), Item()], options={"timestamp_granularities": ["word"]}, prepared_items=prepared)
+ assert adapter._model.config._attn_implementation == "sdpa"
+ adapter.extract([Item(), Item()], prepared_items=prepared)
+
+ # Each word-timestamp call starts on sdpa, and so do the plain calls after them.
+ assert seen == ["sdpa", "sdpa", "sdpa", "sdpa"]
+ assert adapter._model.config._attn_implementation == "sdpa"
+
+
+def test_extract_restores_attention_when_word_timestamps_fail() -> None:
+ adapter, pipeline = _loaded_adapter(None)
+ adapter._model = SimpleNamespace(config=SimpleNamespace(_attn_implementation="sdpa"))
+ pipeline.side_effect = _switch_to_eager_like_whisper_generate(adapter, [], error=RuntimeError("decode failed"))
+
+ with pytest.raises(RuntimeError, match="decode failed"):
+ adapter.extract(
+ [Item()],
+ options={"timestamp_granularities": ["word"]},
+ prepared_items=[PreparedItem(payload=_payload(), cost=1, original_index=0)],
+ )
+
+ assert adapter._model.config._attn_implementation == "sdpa"
+
+
@pytest.mark.parametrize(
("options", "message"),
[