Skip to content

Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.

fix(runtime): include Torch runtime sources and required dependencies in distributions

zhifu gaob19438821654
diff --git a/.github/workflows/test-funasr-torch-package.yml b/.github/workflows/test-funasr-torch-package.yml
new file mode 100644
index 000000000..2287de3b7
--- /dev/null
+++ b/.github/workflows/test-funasr-torch-package.yml
@@ -0,0 +1,73 @@
+name: Validate funasr-torch package
+
+on:
+ pull_request:
+ paths:
+ - "runtime/python/libtorch/**"
+ - "tests/test_funasr_torch_package.py"
+ - ".github/workflows/test-funasr-torch-package.yml"
+ push:
+ branches: [main]
+ paths:
+ - "runtime/python/libtorch/**"
+ - "tests/test_funasr_torch_package.py"
+ - ".github/workflows/test-funasr-torch-package.yml"
+
+permissions:
+ contents: read
+
+jobs:
+ torch-package:
+ runs-on: ubuntu-latest
+ timeout-minutes: 15
+ strategy:
+ fail-fast: false
+ matrix:
+ python: ["3.10", "3.12"]
+ env:
+ HF_HUB_OFFLINE: "1"
+ MODELSCOPE_OFFLINE: "1"
+ steps:
+ - uses: actions/checkout@v4
+ with:
+ ref: ${{ github.event.pull_request.head.sha || github.sha }}
+ - uses: actions/setup-python@v5
+ with:
+ python-version: ${{ matrix.python }}
+ - name: Install build and CPU import dependencies
+ run: |
+ python -m pip install torch==2.8.0 --index-url https://download.pytorch.org/whl/cpu
+ python -m pip install build setuptools wheel twine pytest packaging librosa scipy "numpy<2" kaldi-native-fbank PyYAML sentencepiece
+ - name: Verify actual distributions
+ run: |
+ git rev-parse HEAD > source-commit.txt
+ python -m pip freeze > resolved-dependencies.txt
+ python -m pytest -q tests/test_funasr_torch_package.py --junitxml=torch-package-results.xml
+ python -m build --no-isolation runtime/python/libtorch --outdir dist/torch-runtime
+ python -m twine check dist/torch-runtime/*
+ - name: Verify installed wheel outside the checkout
+ run: |
+ python -m pip install --no-deps --target "$RUNNER_TEMP/torch-runtime-installed" dist/torch-runtime/*.whl
+ cd "$RUNNER_TEMP"
+ PYTHONPATH="$RUNNER_TEMP/torch-runtime-installed" python - <<'PY'
+ import importlib.util
+ import os
+ from pathlib import Path
+ import funasr_torch
+ root = Path(os.environ["RUNNER_TEMP"]) / "torch-runtime-installed"
+ assert Path(funasr_torch.__file__).is_relative_to(root)
+ assert callable(funasr_torch.Paraformer)
+ assert callable(funasr_torch.SenseVoiceSmall)
+ assert importlib.util.find_spec("onnxruntime") is None
+ assert importlib.util.find_spec("torch_quant") is None
+ print("Installed Torch runtime imports passed without ONNX Runtime or torch-quant")
+ PY
+ - uses: actions/upload-artifact@v4
+ if: always()
+ with:
+ name: torch-package-${{ matrix.python }}
+ path: |
+ source-commit.txt
+ resolved-dependencies.txt
+ torch-package-results.xml
+ dist/torch-runtime/*
diff --git a/runtime/python/libtorch/setup.py b/runtime/python/libtorch/setup.py
index b7cca4c91..94004ccf4 100644
--- a/runtime/python/libtorch/setup.py
+++ b/runtime/python/libtorch/setup.py
@@ -27,14 +27,14 @@ setuptools.setup(
include_package_data=True,
install_requires=[
"librosa",
- "onnxruntime>=1.7.0",
+ "torch",
"scipy",
"numpy>=1.19.3",
"kaldi-native-fbank",
"PyYAML>=5.1.2",
- "torch-quant >= 0.4.0",
+ "sentencepiece",
],
- packages=find_packages(include=["torch_paraformer*"]),
+ packages=find_packages(include=["funasr_torch", "funasr_torch.*"]),
keywords=["funasr, paraformer, funasr_torch"],
classifiers=[
"Programming Language :: Python :: 3.6",
diff --git a/tests/test_funasr_torch_package.py b/tests/test_funasr_torch_package.py
new file mode 100644
index 000000000..664cbb282
--- /dev/null
+++ b/tests/test_funasr_torch_package.py
@@ -0,0 +1,120 @@
+"""Verify real distributions, not just a successful setuptools invocation."""
+
+from email.parser import BytesParser
+from pathlib import Path
+import shutil
+import subprocess
+import sys
+import tarfile
+import zipfile
+
+import pytest
+from packaging.requirements import Requirement
+
+
+ROOT = Path(__file__).resolve().parents[1]
+RUNTIME = ROOT / "runtime/python/libtorch"
+
+
[email protected](scope="module")
+def distributions(tmp_path_factory):
+ directory = tmp_path_factory.mktemp("torch-dist")
+ source = directory / "source"
+ shutil.copytree(
+ RUNTIME,
+ source,
+ ignore=shutil.ignore_patterns("__pycache__", "*.egg-info", "build", "dist"),
+ )
+ result = subprocess.run(
+ [
+ sys.executable,
+ "-m",
+ "build",
+ "--no-isolation",
+ "--outdir",
+ str(directory / "dist"),
+ str(source),
+ ],
+ text=True,
+ capture_output=True,
+ timeout=120,
+ )
+ assert result.returncode == 0, result.stdout + result.stderr
+ wheels = list((directory / "dist").glob("*.whl"))
+ sdists = list((directory / "dist").glob("*.tar.gz"))
+ assert len(wheels) == len(sdists) == 1
+ return wheels[0], sdists[0]
+
+
+def test_wheel_contains_all_runtime_sources(distributions):
+ wheel, _ = distributions
+ expected = {
+ str(path.relative_to(RUNTIME)): path.read_bytes()
+ for path in (RUNTIME / "funasr_torch").rglob("*.py")
+ }
+ assert "funasr_torch/__init__.py" in expected
+ assert "funasr_torch/utils/frontend.py" in expected
+ with zipfile.ZipFile(wheel) as archive:
+ assert archive.testzip() is None
+ actual = {name for name in archive.namelist() if name.endswith(".py")}
+ assert actual == set(expected)
+ for name, content in expected.items():
+ assert archive.read(name) == content
+
+
+def test_sdist_contains_runtime_sources_and_build_files(distributions):
+ _, sdist = distributions
+ with tarfile.open(sdist, "r:gz") as archive:
+ files = {
+ str(Path(member.name).relative_to(Path(member.name).parts[0])): member
+ for member in archive.getmembers()
+ if member.isfile()
+ }
+ for path in (RUNTIME / "funasr_torch").rglob("*.py"):
+ name = str(path.relative_to(RUNTIME))
+ assert name in files, name
+ assert archive.extractfile(files[name]).read() == path.read_bytes()
+ for name in ["setup.py", "README.md"]:
+ assert name in files
+ assert (
+ archive.extractfile(files[name]).read() == (RUNTIME / name).read_bytes()
+ )
+
+
+def metadata(wheel):
+ with zipfile.ZipFile(wheel) as archive:
+ paths = [
+ name for name in archive.namelist() if name.endswith(".dist-info/METADATA")
+ ]
+ assert len(paths) == 1
+ return BytesParser().parsebytes(archive.read(paths[0]))
+
+
+ "dependency",
+ [
+ "torch",
+ "sentencepiece",
+ "librosa",
+ "numpy",
+ "scipy",
+ "kaldi-native-fbank",
+ "pyyaml",
+ ],
+)
+def test_declares_direct_runtime_dependencies(distributions, dependency):
+ wheel, _ = distributions
+ names = {
+ Requirement(item).name.lower().replace("_", "-")
+ for item in metadata(wheel).get_all("Requires-Dist", [])
+ }
+ assert dependency in names
+
+
+def test_torch_package_does_not_require_unrelated_runtime_engines(distributions):
+ wheel, _ = distributions
+ names = {
+ Requirement(item).name.lower().replace("_", "-")
+ for item in metadata(wheel).get_all("Requires-Dist", [])
+ }
+ assert not names & {"torch-quant", "onnxruntime", "onnxruntime-gpu"}