Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/senselab/audio/tasks/speech_to_text/canary_qwen.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@

from senselab.audio.data_structures import Audio
from senselab.utils.data_structures import DeviceType, HFModel, ScriptLine, _select_device_and_dtype
from senselab.utils.subprocess_venv import _clean_subprocess_env, ensure_venv, parse_subprocess_result, venv_python
from senselab.utils.subprocess_venv import (
_clean_subprocess_env,
_find_uv,
ensure_venv,
parse_subprocess_result,
venv_python,
)

# Dedicated venv — kept separate from the existing nemo-diarization venv.
# Canary-Qwen needs nemo_toolkit[asr,tts] (the [tts] extra pulls
Expand Down Expand Up @@ -147,6 +153,35 @@ def transcribe_with_canary_qwen(
device_type = device or _select_device_and_dtype(compatible_devices=[DeviceType.CUDA, DeviceType.CPU])[0]

venv_dir = ensure_venv(_CANARY_VENV, _CANARY_REQUIREMENTS, python_version=_CANARY_PYTHON)

# PyPI ships torch 2.8.x as +cu129 but torchaudio 2.8.x as +cu128 — a
# CUDA-tag skew that triggers a torchaudio RuntimeError ("PyTorch has
# CUDA 12.9 whereas TorchAudio has CUDA 12.8") at import. Force-
# reinstall both from the cu128 PyTorch index so they share a CUDA
# tag; --no-deps leaves NeMo and the nvidia-*-cu12 runtime libs in
# place. cu128 is forward-compatible with cu12+ and cu13 drivers.
cuda_fixup_marker = venv_dir / ".senselab-canary-cuda-fixup"
if not cuda_fixup_marker.is_file():
subprocess.run(
[
_find_uv(),
"pip",
"install",
"--python",
venv_python(venv_dir),
"--force-reinstall",
"--no-deps",
"--index-url",
"https://download.pytorch.org/whl/cu128",
"torch>=2.8,<2.9",
"torchaudio>=2.8,<2.9",
],
check=True,
capture_output=True,
text=True,
)
cuda_fixup_marker.touch()
Comment on lines +164 to +183

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The CUDA fixup block is susceptible to a race condition. Since ensure_venv releases its lock before this block executes, multiple concurrent processes might attempt to run the uv pip install command simultaneously if the marker file hasn't been created yet. This could lead to virtual environment corruption or redundant heavy downloads in multi-process environments.

Additionally, using capture_output=True hides progress and error details for a potentially slow operation (downloading ~600MB of wheels). It is better to let the output stream to the console so the user can see progress and diagnose failures easily.

        if not cuda_fixup_marker.is_file():
            from filelock import FileLock
            lock_path = venv_dir.parent / f"{venv_dir.name}.lock"
            with FileLock(str(lock_path), timeout=600):
                # Re-check marker inside the lock to prevent race conditions
                if not cuda_fixup_marker.is_file():
                    subprocess.run(
                        [
                            _find_uv(),
                            "pip",
                            "install",
                            "--python",
                            venv_python(venv_dir),
                            "--force-reinstall",
                            "--no-deps",
                            "--index-url",
                            "https://download.pytorch.org/whl/cu128",
                            "torch>=2.8,<2.9",
                            "torchaudio>=2.8,<2.9",
                        ],
                        check=True,
                    )
                    cuda_fixup_marker.touch()


python = venv_python(venv_dir)

with tempfile.TemporaryDirectory(prefix="senselab-canary-qwen-") as tmpdir:
Expand Down
2 changes: 1 addition & 1 deletion src/senselab/utils/subprocess_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def ensure_venv(

try:
subprocess.run(
[uv, "venv", "--python", py_ver, str(venv_dir)],
[uv, "venv", "--managed-python", "--python", py_ver, str(venv_dir)],
check=True,
capture_output=True,
text=True,
Expand Down
Loading