项目文件夹

文件
wehub-resource-sync 94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:38:16 +08:00

231 行
8.7 KiB
Python

"""
Test the OpenAI-compatible /v1/audio/transcriptions endpoint with Whisper.
Usage:
python3 test_serving_transcription.py -v
"""
import io
import json
import unittest
from typing import List, Optional
import requests
from sglang.srt.utils import kill_process_tree
from sglang.test.ci.ci_register import register_cuda_ci
from sglang.test.test_utils import (
DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
DEFAULT_URL_FOR_TEST,
CustomTestCase,
popen_launch_server,
)
register_cuda_ci(est_time=60, stage="base-b", runner_config="1-gpu-small")
WHISPER_MODEL = "openai/whisper-large-v3"
AUDIO_URL = "https://raw.githubusercontent.com/sgl-project/sgl-test-files/refs/heads/main/audios/Trump_WEF_2018_10s.mp3"
def download_audio_bytes(url=AUDIO_URL):
"""Download audio file and return raw bytes."""
response = requests.get(url, timeout=30)
response.raise_for_status()
return response.content
class TestServingTranscription(CustomTestCase):
"""Test Whisper transcription via /v1/audio/transcriptions endpoint."""
@classmethod
def setUpClass(cls):
cls.model = WHISPER_MODEL
cls.base_url = DEFAULT_URL_FOR_TEST
cls.process = popen_launch_server(
cls.model,
cls.base_url,
timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
other_args=[
"--served-model-name",
"whisper",
],
)
@classmethod
def tearDownClass(cls):
if hasattr(cls, "process") and cls.process:
kill_process_tree(cls.process.pid)
def _transcribe(
self,
language: Optional[str] = "en",
response_format: Optional[str] = None,
timestamp_granularities: Optional[List[str]] = None,
):
"""Send a non-streaming transcription request and return the JSON response.
Passing ``language=None`` omits the field entirely, which exercises
the fused auto-detect path.
"""
audio_bytes = download_audio_bytes()
data = {"model": "whisper"}
if language is not None:
data["language"] = language
if response_format is not None:
data["response_format"] = response_format
if timestamp_granularities is not None:
# Form-encoded list fields repeat the key
data["timestamp_granularities[]"] = timestamp_granularities
response = requests.post(
self.base_url + "/v1/audio/transcriptions",
files={"file": ("audio.mp3", io.BytesIO(audio_bytes), "audio/mpeg")},
data=data,
)
self.assertEqual(response.status_code, 200, response.text)
return response.json()
def _transcribe_stream(self, language: Optional[str] = None) -> List[str]:
"""Send a streaming transcription request and return the delta strings."""
audio_bytes = download_audio_bytes()
data = {"model": "whisper", "stream": "true"}
if language is not None:
data["language"] = language
with requests.post(
self.base_url + "/v1/audio/transcriptions",
files={"file": ("audio.mp3", io.BytesIO(audio_bytes), "audio/mpeg")},
data=data,
stream=True,
timeout=120,
) as response:
self.assertEqual(response.status_code, 200, response.text)
deltas: List[str] = []
for raw in response.iter_lines():
if not raw:
continue
line = raw.decode("utf-8")
if not line.startswith("data: "):
continue
payload = line[len("data: ") :].strip()
if payload == "[DONE]":
break
obj = json.loads(payload)
for choice in obj.get("choices", []):
content = (choice.get("delta") or {}).get("content")
if content:
deltas.append(content)
return deltas
def test_basic_transcription(self):
"""Test that transcription returns a valid non-empty response."""
result = self._transcribe()
self.assertIn("text", result)
self.assertTrue(len(result["text"]) > 0, "Transcription should not be empty")
def test_transcription_content_quality(self):
"""Test that transcription captures key content from the audio."""
result = self._transcribe()
text = result["text"].lower()
keywords = ["privilege", "leader", "science", "art"]
matches = [kw for kw in keywords if kw in text]
self.assertGreaterEqual(
len(matches),
2,
f"Expected at least 2 of {keywords} in transcription, "
f"found {matches}. Full text: {text}",
)
def test_multiple_sequential_requests(self):
"""Test that sequential requests produce consistent results."""
results = []
for _ in range(3):
result = self._transcribe()
self.assertIn("text", result)
self.assertTrue(len(result["text"]) > 0)
results.append(result["text"])
for i in range(1, len(results)):
self.assertEqual(
results[0],
results[i],
f"Transcription {i + 1} differs from first transcription",
)
# -- fused auto-detect (language=None) ---------------------------------
# The clip is English, so the fused path must both produce a valid
# transcription AND expose "en" as the detected language. None of the
# deltas / text fields should leak Whisper special tokens.
def test_auto_detect_language_verbose_json(self):
"""language omitted + verbose_json returns detected language + clean text."""
result = self._transcribe(language=None, response_format="verbose_json")
self.assertEqual(result.get("language"), "en")
text = result.get("text", "")
self.assertTrue(len(text) > 0, "Transcription should not be empty")
self.assertNotIn("<|", text, f"Special token leaked into text: {text!r}")
# Sanity-check content against the same keywords the English test uses.
keywords = ["privilege", "leader", "science", "art"]
matches = [kw for kw in keywords if kw in text.lower()]
self.assertGreaterEqual(
len(matches),
2,
f"Expected at least 2 of {keywords} in auto-detected transcription, "
f"found {matches}. Full text: {text!r}",
)
def test_auto_detect_matches_explicit_english(self):
"""Auto-detected (language=None) text should match explicit language=en."""
auto = self._transcribe(language=None).get("text", "")
explicit = self._transcribe(language="en").get("text", "")
self.assertEqual(
auto.strip(),
explicit.strip(),
"Auto-detect should produce the same transcription as language=en "
"on an English clip.",
)
self.assertNotIn("<|", auto)
def test_auto_detect_with_segment_timestamps(self):
"""language=None + timestamp_granularities uses the timestamps fused regex."""
result = self._transcribe(
language=None,
response_format="verbose_json",
timestamp_granularities=["segment"],
)
self.assertEqual(result.get("language"), "en")
segments = result.get("segments") or []
self.assertGreater(len(segments), 0, "Expected at least one segment")
for seg in segments:
self.assertIn("start", seg)
self.assertIn("end", seg)
self.assertIn("text", seg)
self.assertGreaterEqual(seg["end"], seg["start"])
self.assertNotIn(
"<|", seg["text"], f"Special token leaked into segment: {seg!r}"
)
def test_auto_detect_streaming(self):
"""language=None + stream=True: deltas scrubbed, concat matches non-streaming.
Verified against a real server: sglang's streaming path for Whisper
produces clean deltas (complete words, no BPE fragmentation), so the
fused path only needs to hide the forced prefix — which this PR
does. Asserts both the prefix-leak guard and text equivalence.
"""
deltas = self._transcribe_stream(language=None)
self.assertTrue(len(deltas) > 0, "Expected at least one streamed delta")
for d in deltas:
self.assertNotIn(
"<|", d, f"Special token leaked into streaming delta: {d!r}"
)
streamed = "".join(deltas).strip()
reference = self._transcribe(language=None).get("text", "").strip()
self.assertEqual(
streamed,
reference,
"Streamed auto-detect text should match the non-streaming result.",
)
if __name__ == "__main__":
unittest.main()