diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..0ef5058 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,4 @@ +# LF in every checkout, Windows included: the PTX kernels are embedded with +# include_str!, so CRLF would change what a Windows build ships to the driver +* text=auto eol=lf +*.wav binary diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 93dad0a..62b1839 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -74,6 +74,10 @@ jobs: os: ubuntu-24.04 - name: macOS arm64 os: macos-15 + # the CUDA backend and the Python bindings' CUDA path, as on Linux; there is + # no GPU here either + - name: Windows x86_64 + os: windows-2025 steps: - uses: actions/checkout@v6 diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 5df0fd7..33d7450 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -60,8 +60,9 @@ jobs: - name: Package and build as a dependency run: just package - # one abi3 wheel per platform with the backend that platform runs; the Linux - # wheel builds without a GPU or CUDA toolkit because CUDA is loaded at run time + # one abi3 wheel per platform with the backend that platform runs; the Linux and + # Windows wheels build without a GPU or CUDA toolkit because CUDA is loaded at run + # time wheels: name: Wheel (${{ matrix.name }}) runs-on: ${{ matrix.os }} @@ -73,13 +74,36 @@ jobs: os: ubuntu-24.04 target: x86_64-unknown-linux-gnu manylinux: "2_28" + rustflags: -D warnings - name: macOS arm64, Core ML os: macos-15 target: aarch64-apple-darwin manylinux: "off" + rustflags: -D warnings + # the C runtime is linked statically, so the extension runs without the + # Visual C++ redistributable whichever Python loads it. RUSTFLAGS replaces the + # per-target flags rather than adding to them, so each target spells out its + # full set + - name: Windows x86_64, CUDA + os: windows-2025 + target: x86_64-pc-windows-msvc + manylinux: "off" + rustflags: -D warnings -C target-feature=+crt-static + env: + RUSTFLAGS: ${{ matrix.rustflags }} steps: - uses: actions/checkout@v6 + # a Windows checkout without symlink support writes the LICENSE symlink as a text + # file holding its target, which maturin would ship in the wheel as the license; + # removing it first keeps cp from writing through a real symlink onto itself + - name: Resolve the LICENSE symlink + if: runner.os == 'Windows' + shell: bash + run: | + rm -f crates/scriptrs-python/LICENSE + cp LICENSE crates/scriptrs-python/LICENSE + - name: Build wheel uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1.51.0 with: @@ -102,6 +126,29 @@ jobs: test "$size" -le "$limit" done + # CUDA must load at run time and the C runtime is static, so the extension may + # import only Python's stable ABI and Windows system DLLs + - name: Check the extension's imports + if: runner.os == 'Windows' + shell: bash + run: | + set -euo pipefail + unpacked="$RUNNER_TEMP/wheel" + mkdir -p "$unpacked" + # 7-Zip ships on the runner image, and a wheel is a zip + 7z x -y dist/*.whl -o"$unpacked" > /dev/null + ext="$(find "$unpacked" -name '_native*.pyd' -print -quit)" + test -n "$ext" + rustup component add llvm-tools + readobj="$(cygpath -u "$(rustc --print sysroot)")/lib/rustlib/x86_64-pc-windows-msvc/bin/llvm-readobj.exe" + # a Windows tool's output ends lines with CRLF + imports="$("$readobj" --coff-imports "$ext" | tr -d '\r' | sed -n 's/^ *Name: //p' | sort -u)" + echo "$imports" + if grep -Eiv '^(python3|kernel32|ntdll|advapi32|bcrypt|bcryptprimitives|ws2_32|userenv|secur32|crypt32|ole32|oleaut32|shell32|user32|synchronization|api-ms-win-core-[a-z0-9-]+)\.dll$' <<< "$imports"; then + echo "the extension imports a DLL outside Python and Windows itself" >&2 + exit 1 + fi + - name: Upload wheel uses: actions/upload-artifact@v7 with: @@ -175,6 +222,10 @@ jobs: - name: macOS arm64 os: macos-15 target: aarch64-apple-darwin + # no NVIDIA driver either, so the same checks as on Linux + - name: Windows x86_64 + os: windows-2025 + target: x86_64-pc-windows-msvc # the one abi3 wheel serves every version, so test the oldest and the newest python: ["3.11", "3.14"] env: @@ -199,8 +250,14 @@ jobs: uv pip install --python .venv-wheel dist/*.whl pytest numpy - name: Run tests + shell: bash working-directory: crates/scriptrs-python - run: ../../.venv-wheel/bin/python -m pytest -rA + run: | + set -euo pipefail + # Windows venvs keep the interpreter in Scripts, not bin + python=../../.venv-wheel/bin/python + if [ "$RUNNER_OS" = Windows ]; then python=../../.venv-wheel/Scripts/python.exe; fi + "$python" -m pytest -rA # checks a tag before anything is published: the crate's version must match it, and # CHANGELOG.md needs its entry, which becomes the GitHub release notes @@ -260,7 +317,8 @@ jobs: set -euo pipefail version="${GITHUB_REF_NAME#v}" ls -la dist - test "$(find dist -type f | wc -l)" -eq 3 + # the Linux, macOS and Windows wheels and the sdist + test "$(find dist -type f | wc -l)" -eq 4 for file in dist/*; do case "$(basename "$file")" in "scriptrs-${version}"-*.whl | "scriptrs-${version}.tar.gz") ;; @@ -286,6 +344,8 @@ jobs: os: ubuntu-24.04 - name: macOS arm64 os: macos-15 + - name: Windows x86_64 + os: windows-2025 steps: - uses: actions/checkout@v6 @@ -309,10 +369,13 @@ jobs: test "$attempt" -lt 10 sleep 30 done - .venv-release/bin/python -c "import scriptrs; print(scriptrs.__version__, scriptrs.BACKEND)" + # Windows venvs keep their programs in Scripts, not bin + bin=.venv-release/bin + if [ "$RUNNER_OS" = Windows ]; then bin=.venv-release/Scripts; fi + "$bin/python" -c "import scriptrs; print(scriptrs.__version__, scriptrs.BACKEND)" # only the macOS runner has a backend that can transcribe if [ "$RUNNER_OS" = "macOS" ]; then - text="$(.venv-release/bin/scriptrs transcribe --model parakeet/v2 crates/scriptrs-python/tests/data/librispeech-1089-134686-0007.wav)" + text="$("$bin/scriptrs" transcribe --model parakeet/v2 crates/scriptrs-python/tests/data/librispeech-1089-134686-0007.wav)" printf '%s\n' "$text" grep -qi 'indifference' <<< "$text" fi diff --git a/CHANGELOG.md b/CHANGELOG.md index 40d06bd..9b16923 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions f [Semantic Versioning](https://semver.org/). The Rust crate and the Python package share one version. -## [Unreleased] +## [0.5.0] - 2026-11-07 The first release on PyPI. It has breaking changes to the Rust API; the README's "Breaking changes in 0.5.0" section lists what to write instead. @@ -13,8 +13,13 @@ The first release on PyPI. It has breaking changes to the Rust API; the README's ### Added - Python package `scriptrs` on PyPI, with one wheel for every Python version from 3.11: - Core ML on macOS arm64 and CUDA on Linux x86_64. It includes the `scriptrs transcribe` - command. + Core ML on macOS arm64 and CUDA on Linux x86_64 and Windows x86_64. It includes the + `scriptrs transcribe` command. +- Windows x86_64 builds of the `cuda` feature. They open `nvcuda.dll` from System32 + and cuBLAS 12 or 13 (`cublas64_12.dll` or `cublas64_13.dll` and its cuBLASLt) at + run time, never from the current directory. The Windows wheel installs cuBLAS from + `nvidia-cublas-cu12` and loads it before the first model. CI builds and tests them + on a runner without a GPU; they haven't run on a Windows GPU yet. - `TranscriptionScheduler` batches windows across concurrent requests on CUDA, for servers with many short requests. `SchedulerConfig::max_requests` bounds the requests held at once, and `try_submit` returns `TranscriptionError::SchedulerFull` past it. @@ -28,6 +33,11 @@ The first release on PyPI. It has breaking changes to the Rust API; the README's ### Changed +- The CUDA backend uses cudakit 0.3, which loads the CUDA libraries through its cudarc + fork. On Linux it opens versioned sonames such as `libcublas.so.12` before a + toolkit's unversioned `libcublas.so`, so it reuses the cuBLAS the Python wheel + loaded instead of opening a second copy. `low-precision` no longer depends on + upstream `cudarc`. - `TranscriptionConfig` has only `language`. The frontend settings and the input limit come from the model bundle. `from_deployment` and `validate_for` are gone. - `TranscriptionError::Control` wraps `TranscriptionControlError` in place of the three @@ -67,5 +77,5 @@ The first release on PyPI. It has breaking changes to the Rust API; the README's - VAD regions are padded by 0.2 s so soft first words are kept. - The relative-position attention runs as one fused kernel on CUDA. -[Unreleased]: https://github.com/avencera/scriptrs/compare/v0.4.0...HEAD +[0.5.0]: https://github.com/avencera/scriptrs/compare/v0.4.0...v0.5.0 [0.4.0]: https://github.com/avencera/scriptrs/releases/tag/v0.4.0 diff --git a/Cargo.lock b/Cargo.lock index 786dd5a..9251e3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,11 +96,11 @@ dependencies = [ [[package]] name = "cudakit" -version = "0.1.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87714d487777176f54c3a628bc75acd7a180146f2f809816983c08ee805e4548" +checksum = "d5d091348c1aeffd3b1807f44eaac2f3a5d7ab04c2e7fad075f1e9e1315f0ab7" dependencies = [ - "cudarc", + "cudakit-cudarc", "libloading", "sha2", "thiserror", @@ -108,10 +108,10 @@ dependencies = [ ] [[package]] -name = "cudarc" +name = "cudakit-cudarc" version = "0.19.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7359bf1de037ddada72729c3acec9b040f3468400c6e9e740b01ba0cd2e89562" +checksum = "374d375995d223cda2d64f1c3c84fff1323b594857d9aac801747cb07be53c62" dependencies = [ "libloading", ] @@ -656,7 +656,6 @@ dependencies = [ "approx", "block2", "cudakit", - "cudarc", "eyre", "hf-fetch", "hound", diff --git a/Cargo.toml b/Cargo.toml index 4a8a19a..ecd2526 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition = "2024" rust-version = "1.96" exclude = ["fixtures/", "dev-tools/", "crates/"] authors = ["Praveen Perera"] -description = "Fast NVIDIA Parakeet TDT v2 and v3 speech-to-text in Rust, on Core ML (macOS) and native CUDA (Linux)" +description = "Fast NVIDIA Parakeet TDT v2 and v3 speech-to-text in Rust, on Core ML (macOS) and native CUDA (Linux, Windows)" license = "Apache-2.0" repository = "https://github.com/avencera/scriptrs" homepage = "https://github.com/avencera/scriptrs" @@ -29,13 +29,10 @@ vad = ["long-form"] cuda = ["dep:memmap2", "dep:safetensors", "dep:cudakit", "cudakit/libraries"] # FP8, INT8 and NVFP4 encoder precisions on CUDA, and the weight files that store # them; not recommended for most users: slower than FP16 one utterance at a time -low-precision = ["cuda", "dep:cudarc"] +low-precision = ["cuda"] [dependencies] -cudakit = { version = "0.1", optional = true } -# only to turn on cuBLASLt in cudakit's cudarc, for the FP8 and NVFP4 encoder -# products; cudakit picks the CUDA version and loading -cudarc = { version = "0.19.10", optional = true, default-features = false, features = ["cublaslt"] } +cudakit = { version = "0.3", optional = true } memmap2 = { version = "0.9", optional = true } ndarray = "0.17.2" realfft = "3.5.1" diff --git a/README.md b/README.md index 16ef0ea..b4e2200 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ Fast [NVIDIA Parakeet TDT](https://huggingface.co/nvidia/parakeet-tdt-0.6b-v3) s - Parakeet TDT 0.6B v2 (English) and v3 (25 European languages) - Core ML on macOS -- Native CUDA on Linux, with no ONNX Runtime or PyTorch (`cuda` feature) +- Native CUDA on Linux, with no ONNX Runtime or PyTorch (`cuda` feature), and a + Windows build that has not run on a GPU yet (see [Windows](#windows)) - Long-form transcription of hour-long recordings, with optional VAD - Token-level timestamps and confidences @@ -28,15 +29,16 @@ The base crate exposes a single-chunk `TranscriptionPipeline`. Fast long-audio c - Optional VAD-backed long-form region planning - Scheduler for many inputs or concurrent requests of any length, batched on CUDA - Native CoreML inference on macOS -- CUDA inference on Linux (`cuda` feature) +- CUDA inference on Linux (`cuda` feature), and on Windows x86_64 untested on a GPU - Streaming transcription of many live streams at once with Nemotron Speech Streaming EN 0.6B and Nemotron 3.5 ASR Streaming 0.6B on CUDA - Hugging Face download support with optional local model loading -- Python package on PyPI, with wheels for macOS arm64 and Linux x86_64 +- Python package on PyPI, with wheels for macOS arm64, Linux x86_64 and Windows x86_64 ## What it does not do yet -- Windows support +- Tested Windows support: the Windows build passes CI, which has no GPU, and has + not transcribed on a Windows GPU yet - Other ASR models - Streaming on Core ML - Stable public guarantees around model layout or long-form behavior @@ -48,7 +50,7 @@ The base crate exposes a single-chunk `TranscriptionPipeline`. Fast long-audio c scriptrs = "0.5" ``` -For native CUDA on Linux: +For native CUDA on Linux or Windows: ```toml [dependencies] @@ -379,6 +381,31 @@ Model downloads use [`hf-fetch`](https://github.com/avencera/hf-fetch), a small blocking client with rustls and the shared Python Hugging Face cache. The `online` feature enables downloads; local model loading works without it. +### Windows + +The `cuda` feature builds for Windows x86_64 with the MSVC toolchain. CUDA loads +at run time, so building needs no GPU or CUDA toolkit. CI runs clippy and the +tests on a Windows runner and builds and tests the Windows Python wheel there, +but that runner has no GPU, and nobody has transcribed on a Windows GPU yet. +Treat Windows as untested until then, and please open an issue with what you +find. + +At run time it needs the NVIDIA driver, which installs `nvcuda.dll`, and cuBLAS +12 or 13: `cublas64_12.dll` or `cublas64_13.dll`, with the matching +`cublasLt64_*.dll` beside it. scriptrs looks for them in the executable's +directory and System32, then each absolute `PATH` entry, then +`%CUDA_PATH%\bin\x64` (where CUDA 13 keeps its DLLs) and `%CUDA_PATH%\bin` +(CUDA 12), so an installed CUDA toolkit is found without changing `PATH`. The +Python wheel installs cuBLAS 12 from the `nvidia-cublas-cu12` package instead. + +The driver loads only from System32, in the Python package's driver check too, +and no CUDA library loads from the current directory or a relative `PATH` +entry, so a DLL planted in the folder a program runs from is never picked up. +Each library found on `PATH` or under `CUDA_PATH` loads by full path, and its +own imports come from its directory, so cuBLAS binds the cuBLASLt beside it. +The exception is a process that already loaded a cuBLASLt from another install: +Windows reuses that one, and a mismatched pair can fail its first product. + ## Usage ### Short audio diff --git a/crates/scriptrs-python/Cargo.lock b/crates/scriptrs-python/Cargo.lock index 33e124c..bb0ec0d 100644 --- a/crates/scriptrs-python/Cargo.lock +++ b/crates/scriptrs-python/Cargo.lock @@ -126,11 +126,11 @@ dependencies = [ [[package]] name = "cudakit" -version = "0.1.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87714d487777176f54c3a628bc75acd7a180146f2f809816983c08ee805e4548" +checksum = "d5d091348c1aeffd3b1807f44eaac2f3a5d7ab04c2e7fad075f1e9e1315f0ab7" dependencies = [ - "cudarc", + "cudakit-cudarc", "libloading", "sha2", "thiserror", @@ -138,10 +138,10 @@ dependencies = [ ] [[package]] -name = "cudarc" +name = "cudakit-cudarc" version = "0.19.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7359bf1de037ddada72729c3acec9b040f3468400c6e9e740b01ba0cd2e89562" +checksum = "374d375995d223cda2d64f1c3c84fff1323b594857d9aac801747cb07be53c62" dependencies = [ "libloading", ] diff --git a/crates/scriptrs-python/Cargo.toml b/crates/scriptrs-python/Cargo.toml index f5e369a..db5a646 100644 --- a/crates/scriptrs-python/Cargo.toml +++ b/crates/scriptrs-python/Cargo.toml @@ -37,8 +37,8 @@ symphonia = { version = "0.6.1", default-features = false, features = [ ] } # one wheel per platform carries the backend that platform runs: Core ML on macOS -# and native CUDA on Linux x86_64, so a plain `maturin build` produces the release -# wheel. CUDA is loaded at run time, so the Linux wheel builds without a GPU or -# CUDA toolkit -[target.'cfg(all(target_os = "linux", target_arch = "x86_64"))'.dependencies] +# and native CUDA on Linux and Windows x86_64, so a plain `maturin build` produces +# the release wheel. CUDA is loaded at run time, so the Linux and Windows wheels +# build without a GPU or CUDA toolkit +[target.'cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))'.dependencies] scriptrs = { path = "../..", features = ["online", "vad", "cuda"] } diff --git a/crates/scriptrs-python/README.md b/crates/scriptrs-python/README.md index c863d8e..912dadc 100644 --- a/crates/scriptrs-python/README.md +++ b/crates/scriptrs-python/README.md @@ -12,6 +12,7 @@ pip install scriptrs | --- | --- | | macOS arm64 | Core ML | | Linux x86_64 (glibc 2.28+) | CUDA, on a Turing (RTX 20, T4) or newer NVIDIA GPU | +| Windows x86_64 | CUDA, as on Linux; not yet run on a GPU | The package needs Python 3.11 or newer. One wheel covers every Python version. @@ -113,15 +114,20 @@ uv tool install scriptrs ## CUDA -The Linux wheel needs an NVIDIA driver, but no CUDA toolkit: it installs cuBLAS -from the `nvidia-cublas-cu12` package. The default `precision="fp16"` is the -fastest. `"tf32"` keeps the weights in FP32, and `"fp32"` matches NVIDIA NeMo's -FP32 output token for token: +The Linux and Windows wheels need an NVIDIA driver, but no CUDA toolkit: they +install cuBLAS from the `nvidia-cublas-cu12` package. The default +`precision="fp16"` is the fastest. `"tf32"` keeps the weights in FP32, and +`"fp32"` matches NVIDIA NeMo's FP32 output token for token: ```python pipeline = scriptrs.Pipeline.from_pretrained("parakeet/v3", precision="fp32") ``` +The Windows wheel builds and passes its tests in CI on a runner without a GPU, +but nobody has transcribed with it on a Windows GPU yet. It loads the driver's +`nvcuda.dll` and the `nvidia-cublas-cu12` package's `cublas64_12.dll` and +`cublasLt64_12.dll`. If transcription fails there, please open an issue. + ## Models `from_pretrained()` downloads the models from Hugging Face into `cache_dir=` @@ -129,7 +135,7 @@ when given, else the Hugging Face hub cache that `HF_HUB_CACHE` or `HF_HOME` sets, else `~/.cache/huggingface/hub`. On macOS a model is about 0.6 GB of Core ML models from [`avencera/scriptrs-models`](https://huggingface.co/avencera/scriptrs-models). -On Linux it is about 2.5 GB of weights from +On Linux and Windows it is about 2.5 GB of weights from [`avencera/parakeet-tdt-0.6b-v2-cuda`](https://huggingface.co/avencera/parakeet-tdt-0.6b-v2-cuda) or [`avencera/parakeet-tdt-0.6b-v3-cuda`](https://huggingface.co/avencera/parakeet-tdt-0.6b-v3-cuda). diff --git a/crates/scriptrs-python/pyproject.toml b/crates/scriptrs-python/pyproject.toml index e98272d..ba14f33 100644 --- a/crates/scriptrs-python/pyproject.toml +++ b/crates/scriptrs-python/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "maturin" [project] name = "scriptrs" dynamic = ["version"] -description = "Fast NVIDIA Parakeet TDT v2 and v3 speech-to-text on Core ML (macOS) and CUDA (Linux)" +description = "Fast NVIDIA Parakeet TDT v2 and v3 speech-to-text on Core ML (macOS) and CUDA (Linux and Windows)" readme = "README.md" requires-python = ">=3.11" license = "Apache-2.0" @@ -17,6 +17,7 @@ classifiers = [ "Intended Audience :: Developers", "Intended Audience :: Science/Research", "Operating System :: MacOS", + "Operating System :: Microsoft :: Windows", "Operating System :: POSIX :: Linux", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", @@ -29,9 +30,10 @@ classifiers = [ "Topic :: Scientific/Engineering :: Artificial Intelligence", "Typing :: Typed", ] -# the CUDA backend runs every encoder matrix multiply through cuBLAS +# the CUDA backend runs every encoder matrix multiply through cuBLAS. Not every +# cuBLAS 12 release has a Windows wheel, and pip takes the newest one that does dependencies = [ - "nvidia-cublas-cu12>=12.4; sys_platform == 'linux' and platform_machine == 'x86_64'", + "nvidia-cublas-cu12>=12.4; (sys_platform == 'linux' and platform_machine == 'x86_64') or (sys_platform == 'win32' and platform_machine == 'AMD64')", ] [project.scripts] diff --git a/crates/scriptrs-python/python/scriptrs/__init__.py b/crates/scriptrs-python/python/scriptrs/__init__.py index 7b1ea71..3597a07 100644 --- a/crates/scriptrs-python/python/scriptrs/__init__.py +++ b/crates/scriptrs-python/python/scriptrs/__init__.py @@ -1,4 +1,4 @@ -"""Fast NVIDIA Parakeet TDT speech-to-text, on Core ML (macOS) and CUDA (Linux). +"""Fast NVIDIA Parakeet TDT speech-to-text, on Core ML (macOS) and CUDA (Linux and Windows). Pick a model when you load the pipeline, then transcribe files or 16 kHz mono samples:: @@ -133,8 +133,8 @@ def from_pretrained( """Download ``model`` from Hugging Face if needed, then load it. The first call downloads the model, about 0.6 GB of Core ML models on - macOS or 2.5 GB of weights on Linux, into ``cache_dir`` when given, else - the Hugging Face hub cache that ``HF_HUB_CACHE`` or ``HF_HOME`` sets, else + macOS or 2.5 GB of weights on Linux and Windows, into ``cache_dir`` when + given, else the Hugging Face hub cache that ``HF_HUB_CACHE`` or ``HF_HOME`` sets, else ``~/.cache/huggingface/hub``; later calls load from the cache. Setting ``SCRIPTRS_MODELS_DIR`` loads a local directory instead, as :meth:`from_dir` does. @@ -144,7 +144,7 @@ def from_pretrained( when the higher of its v5 and v6 speech probabilities is at least 0.6. Raises :class:`ScriptrsError` with code ``model_download`` when a - download fails, or ``cuda`` when Linux has no NVIDIA driver. + download fails, or ``cuda`` when Linux or Windows has no NVIDIA driver. """ model = _model(model) precision = _precision(precision) @@ -163,8 +163,8 @@ def from_dir( ) -> Pipeline: """Load ``model`` from a local directory instead of downloading it. - On macOS the directory holds Core ML models, and on Linux native weights, - in the layouts the scriptrs README describes. + On macOS the directory holds Core ML models, and on Linux and Windows + native weights, in the layouts the scriptrs README describes. """ model = _model(model) precision = _precision(precision) diff --git a/crates/scriptrs-python/python/scriptrs/_cuda.py b/crates/scriptrs-python/python/scriptrs/_cuda.py index d456440..3336cd1 100644 --- a/crates/scriptrs-python/python/scriptrs/_cuda.py +++ b/crates/scriptrs-python/python/scriptrs/_cuda.py @@ -1,24 +1,37 @@ """Find the NVIDIA driver and make pip-installed cuBLAS visible to the CUDA backend. -The native CUDA backend looks cuBLAS up by name, ``libcublas.so`` and then -``libcublas.so.12``. The ``nvidia-cublas-cu12`` wheel installs it inside -site-packages, where the dynamic loader does not look, so it is loaded here -first: glibc then resolves the ``libcublas.so.12`` lookup to the already-loaded -copy. A ``libcublas.so`` on the loader path, from a CUDA toolkit, still comes -first. +The native CUDA backend opens the driver and cuBLAS at run time by name: +``libcuda.so.1`` and ``libcublas.so.12`` on Linux, and ``nvcuda.dll`` and +``cublas64_12.dll`` on Windows, then the CUDA 13 names. The +``nvidia-cublas-cu12`` wheel installs cuBLAS inside site-packages, where neither +loader looks, so it is loaded here first. Both loaders reuse a library the +process already holds under the name they ask for: glibc matches the +``libcublas.so.12`` lookup to the loaded copy's soname, and on Windows the +backend checks for a loaded module of that name before it searches. The wheel's +copy therefore wins over a CUDA toolkit's, which ships the same names. """ from __future__ import annotations import ctypes +import sys import threading from pathlib import Path from ._errors import ScriptrsError from ._native import BACKEND -# dependencies before dependents, so each load finds what it links against -_CUBLAS = ("libcublasLt.so.12", "libcublas.so.12") +_WINDOWS = sys.platform == "win32" +# the driver's library, which the NVIDIA driver installs on the loader's own path +DRIVER = "nvcuda.dll" if _WINDOWS else "libcuda.so.1" +# the library directory inside the wheel's ``nvidia`` namespace package, and its +# libraries with dependencies before dependents, since libcublas links against +# libcublasLt by name and each load finds what it links against already loaded +_CUBLAS: tuple[tuple[str, ...], tuple[str, ...]] = ( + (("cublas", "bin"), ("cublasLt64_12.dll", "cublas64_12.dll")) + if _WINDOWS + else (("cublas", "lib"), ("libcublasLt.so.12", "libcublas.so.12")) +) _prepared = False # a second thread must wait for the first to finish loading, not skip ahead _lock = threading.Lock() @@ -40,13 +53,26 @@ def prepare() -> None: _prepared = True +def _driver_path() -> str: + """The driver to load: System32's copy on Windows, the loader's soname elsewhere.""" + if sys.platform != "win32": + return DRIVER + # a full System32 path, as the backend loads the driver, since a bare name would + # also search the interpreter's directory for a planted nvcuda.dll + buffer = ctypes.create_unicode_buffer(260) + length = ctypes.windll.kernel32.GetSystemDirectoryW(buffer, len(buffer)) + if not 0 < length < len(buffer): + raise OSError("could not find the System32 directory") + return str(Path(buffer.value) / DRIVER) + + def _check_driver() -> None: try: - ctypes.CDLL("libcuda.so.1") + ctypes.CDLL(_driver_path()) except OSError: raise ScriptrsError( "cuda", - "no NVIDIA driver found (libcuda.so.1); on Linux scriptrs runs on NVIDIA GPUs only", + f"no NVIDIA driver found ({DRIVER}); on Linux and Windows scriptrs runs on NVIDIA GPUs only", ) from None @@ -56,12 +82,15 @@ def _load_cublas() -> None: except ImportError: return + subdir, names = _CUBLAS for root in getattr(nvidia, "__path__", []): - lib_dir = Path(root) / "cublas" / "lib" - for name in _CUBLAS: + lib_dir = Path(root).joinpath(*subdir) + for name in names: path = lib_dir / name if path.is_file(): try: + # Windows ignores the mode; an absolute path there also searches the + # DLL's own directory for its imports ctypes.CDLL(str(path), mode=ctypes.RTLD_GLOBAL) except OSError: # a broken install falls back to the system search path diff --git a/crates/scriptrs-python/src/lib.rs b/crates/scriptrs-python/src/lib.rs index 463ac11..d4601bb 100644 --- a/crates/scriptrs-python/src/lib.rs +++ b/crates/scriptrs-python/src/lib.rs @@ -21,7 +21,10 @@ use results::{PyTimedToken, PyTranscriptChunk, PyTranscriptionResult}; /// Backend compiled into this build, or `None` where scriptrs has no backend const BACKEND: Option<&str> = if cfg!(target_os = "macos") { Some("coreml") -} else if cfg!(all(target_os = "linux", target_arch = "x86_64")) { +} else if cfg!(all( + any(target_os = "linux", target_os = "windows"), + target_arch = "x86_64" +)) { Some("cuda") } else { None @@ -50,7 +53,10 @@ pub(crate) fn transcription_code(error: &TranscriptionError) -> &'static str { "invalid_model" } TranscriptionError::UnsupportedPlatform => "unsupported_platform", - #[cfg(all(target_os = "linux", target_arch = "x86_64"))] + #[cfg(all( + any(target_os = "linux", target_os = "windows"), + target_arch = "x86_64" + ))] TranscriptionError::Cuda(_) => "cuda", TranscriptionError::CoreMl(_) => "coreml", TranscriptionError::Io(_) => "io", diff --git a/crates/scriptrs-python/src/pipeline.rs b/crates/scriptrs-python/src/pipeline.rs index 706da70..c1b04cb 100644 --- a/crates/scriptrs-python/src/pipeline.rs +++ b/crates/scriptrs-python/src/pipeline.rs @@ -261,7 +261,13 @@ fn load(py: Python<'_>, bundle: ModelBundle, vad: bool) -> PyResult .map_err(crate::transcription_err) } -#[cfg(any(target_os = "macos", all(target_os = "linux", target_arch = "x86_64")))] +#[cfg(any( + target_os = "macos", + all( + any(target_os = "linux", target_os = "windows"), + target_arch = "x86_64" + ) +))] fn download_err(error: impl std::fmt::Display) -> PyErr { scriptrs_err("model_download", format!("model download failed: {error}")) } @@ -300,8 +306,11 @@ mod backend { } } -/// Native CUDA weights on Linux x86_64 -#[cfg(all(target_os = "linux", target_arch = "x86_64"))] +/// Native CUDA weights on Linux and Windows x86_64 +#[cfg(all( + any(target_os = "linux", target_os = "windows"), + target_arch = "x86_64" +))] mod backend { use std::path::PathBuf; @@ -338,7 +347,13 @@ mod backend { } /// No backend: the package imports, and loading a model reports the platform -#[cfg(not(any(target_os = "macos", all(target_os = "linux", target_arch = "x86_64"))))] +#[cfg(not(any( + target_os = "macos", + all( + any(target_os = "linux", target_os = "windows"), + target_arch = "x86_64" + ) +)))] mod backend { use std::path::PathBuf; @@ -359,7 +374,7 @@ mod backend { ) -> PyResult { Err(scriptrs_err( "unsupported_platform", - "scriptrs runs on macOS (Core ML) and Linux x86_64 (CUDA) only", + "scriptrs runs on macOS (Core ML) and Linux or Windows x86_64 (CUDA) only", )) } diff --git a/crates/scriptrs-python/tests/conftest.py b/crates/scriptrs-python/tests/conftest.py index 45a231a..ce65dcb 100644 --- a/crates/scriptrs-python/tests/conftest.py +++ b/crates/scriptrs-python/tests/conftest.py @@ -12,6 +12,7 @@ import pytest import scriptrs +from scriptrs._cuda import _driver_path DATA = Path(__file__).resolve().parent / "data" # LibriSpeech test-clean 1089-134686-0007, CC BY 4.0 @@ -35,7 +36,7 @@ def normalize(text: str) -> str: def backend_available() -> bool: if scriptrs.BACKEND == "cuda": try: - ctypes.CDLL("libcuda.so.1") + ctypes.CDLL(_driver_path()) except OSError: return False return scriptrs.BACKEND is not None diff --git a/crates/scriptrs-python/tests/test_scriptrs.py b/crates/scriptrs-python/tests/test_scriptrs.py index d20ba44..ae8b17d 100644 --- a/crates/scriptrs-python/tests/test_scriptrs.py +++ b/crates/scriptrs-python/tests/test_scriptrs.py @@ -30,8 +30,8 @@ def test_model_languages(): def test_backend_matches_platform(): - expected = {"darwin": "coreml", "linux": "cuda"}.get(sys.platform) - if sys.platform == "linux" and not _is_x86_64(): + expected = {"darwin": "coreml", "linux": "cuda", "win32": "cuda"}.get(sys.platform) + if sys.platform in ("linux", "win32") and not _is_x86_64(): expected = None assert scriptrs.BACKEND == expected @@ -44,7 +44,7 @@ def _is_x86_64() -> bool: @pytest.mark.skipif( scriptrs.BACKEND != "cuda" or backend_available(), - reason="checks the error on a Linux machine without an NVIDIA driver", + reason="checks the error on a Linux or Windows machine without an NVIDIA driver", ) def test_missing_driver_fails_before_download(tmp_path): with pytest.raises(scriptrs.ScriptrsError) as raised: diff --git a/justfile b/justfile index 65a7047..7a608cb 100644 --- a/justfile +++ b/justfile @@ -149,4 +149,7 @@ python-test *args: python-build uv venv -q --python 3.12 "$venv" version=$(cargo pkgid | sed 's/.*[#@]//') uv pip install -q --python "$venv" "target/wheels/scriptrs-${version}"-*.whl pytest numpy - "$venv"/bin/python -m pytest -rA {{args}} + # Windows venvs keep the interpreter in Scripts, not bin + python="$venv/bin/python" + if [ -x "$venv/Scripts/python.exe" ]; then python="$venv/Scripts/python.exe"; fi + "$python" -m pytest -rA {{args}} diff --git a/src/cuda/lt.rs b/src/cuda/lt.rs index 42b5045..2b80e93 100644 --- a/src/cuda/lt.rs +++ b/src/cuda/lt.rs @@ -120,7 +120,7 @@ fn raw() -> Result<&'static Raw, Error> { RAW.get_or_init(|| { // SAFETY: checks for the library without loading symbols if !unsafe { sys::is_culib_present() } { - return Err("libcublasLt is not loadable".to_owned()); + return Err("cuBLASLt 12 or 13 is not loadable".to_owned()); } // SAFETY: the library is present, and both symbols have these C signatures in // every cuBLASLt from 11 on, with the enums passed as 32-bit integers diff --git a/src/lib.rs b/src/lib.rs index 856e048..ff946b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,9 @@ //! `scriptrs` is currently a narrow transcription crate: //! //! - CoreML on macOS, and CUDA on Linux behind the `cuda` feature -//! (see `ModelBundle::from_pretrained_native` and `ModelBundle::from_native_dir`) +//! (see `ModelBundle::from_pretrained_native` and `ModelBundle::from_native_dir`). +//! The `cuda` feature also builds for Windows x86_64, where CI checks it on a +//! runner without a GPU; it has not run on a Windows GPU yet //! - Parakeet TDT v2 and v3 //! - mono 16kHz `&[f32]` input //!