Skip to content

build(tts): use slim CUDA 13.3.1 image - #31

Merged
volschin merged 20 commits into
mainfrom
perf/slim-cuda1331-tts
Aug 9, 2026
Merged

build(tts): use slim CUDA 13.3.1 image#31
volschin merged 20 commits into
mainfrom
perf/slim-cuda1331-tts

Conversation

@volschin

@volschin volschin commented Aug 9, 2026

Copy link
Copy Markdown
Owner

Summary

  • replace the 33.7 GB vLLM-derived TTS image with a digest-pinned CUDA 13.3.1 / Ubuntu 26.04 multi-stage ARM64 image
  • use Python 3.14, PyTorch 2.13.0+cu132 and Flash-Attention 2.8.3 for SM120 with exact dependency locks
  • retain only the hash-locked, BSD-licensed pure Kaldi fbank compatibility path required by Qwen ICL; TorchAudio is not installed
  • retain the minimal Triton first-use JIT compiler/header closure while excluding nvcc, make and build-essential

Verification

  • 346 repository tests passed
  • Ruff check and format check passed
  • Compose rendering and git diff check passed
  • real GB10 proof: Torch 2.13.0+cu132, CUDA 13.2, flash_attn_2_cuda and CUDA tensor execution
  • offline pinned-model health and private-profile 24 kHz mono WAV passed
  • cancellation and immediate recovery passed
  • 14-sample production A/B passed median and p90 equivalence gates
  • final image: 7,301,678,416 bytes, 78.34% smaller than production baseline

Deployment

Production has already been switched through Portainer to dgx-qwen3-tts:cuda1331-cu132-36181da and is healthy with zero restarts. The previous dgx-qwen3-tts:admission-08c6162 image remains available for rollback.

Remaining non-blocking risks

  • pip remains present in the copied runtime virtualenv
  • direct APT pins are exact, but the Ubuntu repository/transitive closure is not snapshot-pinned
  • perceptual speaker-identity acceptance remains an owner-listening check; no private media is committed

Summary by Sourcery

Switch the TTS service to a slim, multi-stage CUDA 13.3.1 ARM64 image with a self-contained Python 3.14 runtime and strictly locked dependencies while preserving the existing Qwen3-TTS behavior and contracts.

New Features:

  • Introduce a dedicated builder/runtime CUDA 13.3.1 ARM64 image for the TTS service using a Python 3.14 virtual environment and PyTorch 2.13.0+cu132.
  • Add a pure Kaldi fbank compatibility path extracted from Torchaudio’s compliance module without installing Torchaudio into the runtime.

Enhancements:

  • Split TTS Docker packaging into builder and runtime stages with minimal runtime tooling and explicit exclusion of CUDA compiler and build-essential tools.
  • Refine dependency management with separate slim runtime, Qwen TTS, Flash-Attention, and apt lock files that enforce hash-locked installations and forbid unwanted packages.
  • Extend deployment contract tests to validate Dockerfile stages, dependency hashes, absence of forbidden tools and Torchaudio installs, and integrity of the Kaldi compatibility shim.
  • Add design and implementation-plan documentation describing the slim CUDA 13.3.1 TTS image architecture, dependency constraints, validation steps, and rollout boundaries.

Tests:

  • Add targeted tests for TTS image dependency locks, runtime tooling absence, Kaldi compatibility extraction, forbidden Torchaudio installation detection, and tamper-resistant hash validation.

@sourcery-ai

sourcery-ai Bot commented Aug 9, 2026

Copy link
Copy Markdown

Reviewer's Guide

Replaces the vLLM-derived monolithic TTS image with a slim, multi-stage CUDA 13.3.1 ARM64 Docker image backed by tightly enforced dependency and hash-lock contracts, plus tests that guard the new build, runtime toolchain, and Kaldi compatibility path against regressions.

File-Level Changes

Change Details Files
Introduce strict lock-contract helpers and tests for TTS dependency hashes, Dockerfile runtime-tooling constraints, and absence of unintended Torchaudio installation.
  • Add regex-based helpers to parse and normalize requirement headers and enforce sha256 hash presence/shape for all locked packages.
  • Add helpers to flatten Dockerfile instructions into logical lines, isolate runtime-stage instructions, and detect forbidden build tools in runtime (nvcc, make, build-essential).
  • Add tests to verify the new CUDA 13.3.1 base images, multi-stage FROM layout, runtime requirement resolution mapping, exclusion of large inference frameworks and Torchaudio from the runtime lock, and that all lock files satisfy the hash-contract.
  • Add tests and fixtures that distinguish allowed torchaudio wheel download from forbidden pip-install patterns, and that reject tampered or comment-only hashes in lock files.
tests/test_dgx_deployment.py
Replace the vLLM-derived single-stage TTS Dockerfile with a slim two-stage CUDA 13.3.1 build/runtime image that uses a Python 3.14 virtualenv, pinned PyTorch/Flash-Attention, and a minimal Triton JIT toolchain while preserving TTS app behavior.
  • Add CUDA 13.3.1 devel/runtime ARGs and switch to a multi-stage build: builder from devel image and runtime from base image.
  • Builder stage: install pinned apt builder packages, create /opt/tts-venv, install runtime requirements, TTS packages, and Flash-Attention using hash-locked lock files in the venv, and run a build-time import plus cooperative-cancel patch verification for faster-qwen3-tts.
  • Add logic to download a hash-locked torchaudio wheel, extract only Kaldi fbank compatibility code and license into the qwen_tts package tree and /opt/tts-licenses, patch Qwen’s speech_vq to use kaldi_compat, and verify fbank output numerically.
  • Runtime stage: install exact apt runtime packages, assert absence of nvcc/make/build-essential via a tooling-absence gate, copy the venv, license directory, and Python headers from builder, set PATH/CC/PYTHONUNBUFFERED, and keep the existing TTS server entrypoint and module copies.
  • Ensure runtime stage has no direct use of forbidden tooling (nvcc, make, build-essential) and only keeps the minimal compiler/header closure needed for Triton’s first-use JIT.
dgx/tts/Dockerfile
Refactor TTS dependency locking into multiple hash-locked files for runtime, TTS app, Flash-Attention, torchaudio Kaldi compatibility, and apt builder/runtime packages, with documentation describing the slim image design and implementation plan.
  • Introduce requirements-slim-arm64.in as the explicit Python 3.14 ARM64 runtime dependency input, and generate requirements-slim-arm64.lock as its fully-resolved, hash-locked closure.
  • Add separate lock files for Qwen TTS packages, Flash-Attention, torchaudio Kaldi compatibility wheel, and apt builder/runtime packages; enforce exact pinned versions matching Ubuntu 26.04 and verified hashes.
  • Remove the old monolithic requirements-arm64.lock and apt-packages-arm64.lock that assumed a vLLM-derived base image.
  • Add design and implementation-plan docs detailing the slim CUDA 13.3.1 ARM64 TTS image architecture, dependency constraints, validation steps, and rollout boundaries.
docs/superpowers/plans/2026-08-08-slim-cuda1331-tts-image.md
docs/superpowers/specs/2026-08-08-slim-cuda1331-tts-image-design.md
dgx/tts/apt-builder-packages-arm64.lock
dgx/tts/apt-runtime-packages-arm64.lock
dgx/tts/flash-attn-arm64.lock
dgx/tts/requirements-slim-arm64.in
dgx/tts/requirements-slim-arm64.lock
dgx/tts/torchaudio-kaldi-compat-arm64.lock
dgx/tts/tts-packages-arm64.lock
dgx/tts/apt-packages-arm64.lock
dgx/tts/requirements-arm64.lock

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 2 issues, and left some high level feedback:

  • The new helper functions and regex-based assertions in test_dgx_deployment.py are quite dense; consider extracting some of the more complex logic (e.g., _assert_hash_locked, tooling gates) into a small non-test utility module to reduce test brittleness and improve readability.
  • Several tests assert exact multi-line Dockerfile snippets and full apt lock contents; using more targeted pattern checks (e.g., regex or subset assertions) would make these contracts less fragile to minor formatting or ordering changes while still enforcing the important guarantees.
  • The long inline Python one-liners in Dockerfile RUN steps (especially the Kaldi compat extraction) are hard to read and modify; moving them into standalone scripts copied into the image would make future maintenance and debugging significantly easier.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new helper functions and regex-based assertions in `test_dgx_deployment.py` are quite dense; consider extracting some of the more complex logic (e.g., `_assert_hash_locked`, tooling gates) into a small non-test utility module to reduce test brittleness and improve readability.
- Several tests assert exact multi-line Dockerfile snippets and full apt lock contents; using more targeted pattern checks (e.g., regex or subset assertions) would make these contracts less fragile to minor formatting or ordering changes while still enforcing the important guarantees.
- The long inline Python one-liners in Dockerfile `RUN` steps (especially the Kaldi compat extraction) are hard to read and modify; moving them into standalone scripts copied into the image would make future maintenance and debugging significantly easier.

## Individual Comments

### Comment 1
<location path="dgx/tts/Dockerfile" line_range="36-38" />
<code_context>
+ && python3 -c "from pathlib import Path; from zipfile import ZipFile; import sys; wheel, root, license_root = sys.argv[1:]; archive = ZipFile(wheel); source = archive.read('torchaudio/compliance/kaldi.py').decode(); license_text = archive.read('torchaudio-2.9.1.dist-info/LICENSE'); archive.close(); assert source.count('import torchaudio\\n') == 1; provenance = '# Derived from torchaudio 2.9.1, torchaudio/compliance/kaldi.py.\\n# SPDX-License-Identifier: BSD-2-Clause\\n# Full license: /usr/share/licenses/torchaudio-kaldi-compat/LICENSE\\n# Compatibility contract: only fbank is supported; mfcc is outside this runtime contract.\\n\\n'; Path(root, 'core/tokenizer_25hz/vq/kaldi_compat.py').write_text(provenance + source.replace('import torchaudio\\n', ''), encoding='utf-8'); license_path = Path(license_root) / 'LICENSE'; license_path.parent.mkdir(parents=True, exist_ok=True); license_path.write_bytes(license_text)" \
</code_context>
<issue_to_address>
**suggestion:** The inlined wheel/kaldi-compat manipulation logic is quite dense and would benefit from being factored into a dedicated script for maintainability.

The `python3 -c` snippet that rewrites `kaldi_compat.py` tightly couples filesystem and archive manipulation into a single inline command, which makes it difficult to read, debug, and safely change (e.g., path updates or behavior tweaks). Moving this logic into a small, checked-in Python module that’s copied into the image and invoked from the Dockerfile would make the build more declarative and the transformation easier to test and evolve.

Suggested implementation:

```
COPY tts/torchaudio-kaldi-compat-arm64.lock /tmp/torchaudio-kaldi-compat-arm64.lock
COPY tts/extract_kaldi_compat.py /usr/local/bin/extract_kaldi_compat.py
RUN pip download --no-cache-dir --require-hashes --no-deps \

```

```
 && kaldi_wheel="$(find /tmp/torchaudio-kaldi -name 'torchaudio-*.whl' -print -quit)" \
 && python3 /usr/local/bin/extract_kaldi_compat.py \
      "$kaldi_wheel" "$package_root" /opt/tts-licenses/torchaudio-kaldi-compat \

```

To fully implement this refactor, add a new checked-in Python module `tts/extract_kaldi_compat.py` with a `main()` that:

1. Parses `wheel_path`, `package_root`, and `license_root` from `sys.argv[1:]`.
2. Opens the wheel via `ZipFile(wheel_path)` and reads:
   - `torchaudio/compliance/kaldi.py` into `source` (decoded as UTF-8).
   - `torchaudio-2.9.1.dist-info/LICENSE` into `license_text` (bytes).
3. Asserts `source.count("import torchaudio\n") == 1` for safety.
4. Prepends the provenance header:
   ```python
   PROVENANCE = (
       "# Derived from torchaudio 2.9.1, torchaudio/compliance/kaldi.py.\n"
       "# SPDX-License-Identifier: BSD-2-Clause\n"
       "# Full license: /usr/share/licenses/torchaudio-kaldi-compat/LICENSE\n"
       "# Compatibility contract: only fbank is supported; mfcc is outside this runtime contract.\n"
       "\n"
   )
   ```
5. Writes `PROVENANCE + source.replace("import torchaudio\n", "")` to
   `Path(package_root) / "core/tokenizer_25hz/vq/kaldi_compat.py"` with UTF-8 encoding.
6. Ensures `license_root` exists (`Path(license_root).mkdir(parents=True, exist_ok=True)`) and writes `license_text` to `Path(license_root) / "LICENSE"`.

Include the usual `if __name__ == "__main__": main()` boilerplate so the script can be invoked as done in the Dockerfile.
</issue_to_address>

### Comment 2
<location path="tests/test_dgx_deployment.py" line_range="359-368" />
<code_context>
+def test_tts_runtime_stage_rejects_forbidden_tooling_instructions() -> None:
</code_context>
<issue_to_address>
**suggestion (testing):** Add a fixture where the tooling absence gate is present but a forbidden tool still appears in another runtime instruction.

One subtle case worth covering is a runtime stage that includes `_RUNTIME_TOOLING_ABSENCE_GATE` but also later runs a forbidden tooling command (e.g., `RUN make` or `apt-get install build-essential`) in the same stage. Because `_runtime_stage_uses_forbidden_tool` only skips the gate line, that fixture should still be rejected. Adding a test for this scenario would guard against future changes to `_runtime_stage_instructions` or the gate regex weakening this behavior.

Suggested implementation:

```python
def test_tts_runtime_stage_rejects_forbidden_tooling_instructions() -> None:
    builder_only = """FROM base AS builder
RUN apt-get install -y build-essential make
FROM base AS runtime
RUN echo runtime-ready
"""
    # Forbidden tooling in the builder stage should not cause the runtime stage to be rejected.
    assert not _runtime_stage_uses_forbidden_tool(builder_only)

    later_debug_stage = """FROM base AS builder
FROM base AS runtime
RUN echo runtime-ready
"""
    assert not _runtime_stage_uses_forbidden_tool(later_debug_stage)

    runtime_with_gate_and_forbidden_tooling = """FROM base AS builder
FROM base AS runtime
RUN _RUNTIME_TOOLING_ABSENCE_GATE
RUN make -j$(nproc)
"""
    # Even with the tooling-absence gate present, a later forbidden tooling command
    # in the same runtime stage must still cause rejection.
    assert _runtime_stage_has_tooling_absence_gate(runtime_with_gate_and_forbidden_tooling)
    assert _runtime_stage_uses_forbidden_tool(runtime_with_gate_and_forbidden_tooling)

```

1. If the actual gate instruction differs (e.g., it uses a shell wrapper or a different literal than `RUN _RUNTIME_TOOLING_ABSENCE_GATE`), update the `runtime_with_gate_and_forbidden_tooling` fixture to match the real gate line so that `_runtime_stage_has_tooling_absence_gate` returns `True`.
2. If your forbidden tooling matcher is keyed on specific commands (e.g., `apt-get install` rather than `make`), you may want to replace `RUN make -j$(nproc)` with a command known to be matched as forbidden in your implementation, such as `RUN apt-get install -y build-essential`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread dgx/tts/Dockerfile
Comment on lines +36 to +38
&& python3 -c "from pathlib import Path; from zipfile import ZipFile; import sys; wheel, root, license_root = sys.argv[1:]; archive = ZipFile(wheel); source = archive.read('torchaudio/compliance/kaldi.py').decode(); license_text = archive.read('torchaudio-2.9.1.dist-info/LICENSE'); archive.close(); assert source.count('import torchaudio\\n') == 1; provenance = '# Derived from torchaudio 2.9.1, torchaudio/compliance/kaldi.py.\\n# SPDX-License-Identifier: BSD-2-Clause\\n# Full license: /usr/share/licenses/torchaudio-kaldi-compat/LICENSE\\n# Compatibility contract: only fbank is supported; mfcc is outside this runtime contract.\\n\\n'; Path(root, 'core/tokenizer_25hz/vq/kaldi_compat.py').write_text(provenance + source.replace('import torchaudio\\n', ''), encoding='utf-8'); license_path = Path(license_root) / 'LICENSE'; license_path.parent.mkdir(parents=True, exist_ok=True); license_path.write_bytes(license_text)" \
"$kaldi_wheel" "$package_root" /opt/tts-licenses/torchaudio-kaldi-compat \
&& python3 -c "from pathlib import Path; import sys; path = Path(sys.argv[1]) / 'core/tokenizer_25hz/vq/speech_vq.py'; source = path.read_text(encoding='utf-8'); updated = source.replace('import torchaudio.compliance.kaldi as kaldi', 'from . import kaldi_compat as kaldi'); assert updated != source; path.write_text(updated, encoding='utf-8')" \

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: The inlined wheel/kaldi-compat manipulation logic is quite dense and would benefit from being factored into a dedicated script for maintainability.

The python3 -c snippet that rewrites kaldi_compat.py tightly couples filesystem and archive manipulation into a single inline command, which makes it difficult to read, debug, and safely change (e.g., path updates or behavior tweaks). Moving this logic into a small, checked-in Python module that’s copied into the image and invoked from the Dockerfile would make the build more declarative and the transformation easier to test and evolve.

Suggested implementation:

COPY tts/torchaudio-kaldi-compat-arm64.lock /tmp/torchaudio-kaldi-compat-arm64.lock
COPY tts/extract_kaldi_compat.py /usr/local/bin/extract_kaldi_compat.py
RUN pip download --no-cache-dir --require-hashes --no-deps \

 && kaldi_wheel="$(find /tmp/torchaudio-kaldi -name 'torchaudio-*.whl' -print -quit)" \
 && python3 /usr/local/bin/extract_kaldi_compat.py \
      "$kaldi_wheel" "$package_root" /opt/tts-licenses/torchaudio-kaldi-compat \

To fully implement this refactor, add a new checked-in Python module tts/extract_kaldi_compat.py with a main() that:

  1. Parses wheel_path, package_root, and license_root from sys.argv[1:].
  2. Opens the wheel via ZipFile(wheel_path) and reads:
    • torchaudio/compliance/kaldi.py into source (decoded as UTF-8).
    • torchaudio-2.9.1.dist-info/LICENSE into license_text (bytes).
  3. Asserts source.count("import torchaudio\n") == 1 for safety.
  4. Prepends the provenance header:
    PROVENANCE = (
        "# Derived from torchaudio 2.9.1, torchaudio/compliance/kaldi.py.\n"
        "# SPDX-License-Identifier: BSD-2-Clause\n"
        "# Full license: /usr/share/licenses/torchaudio-kaldi-compat/LICENSE\n"
        "# Compatibility contract: only fbank is supported; mfcc is outside this runtime contract.\n"
        "\n"
    )
  5. Writes PROVENANCE + source.replace("import torchaudio\n", "") to
    Path(package_root) / "core/tokenizer_25hz/vq/kaldi_compat.py" with UTF-8 encoding.
  6. Ensures license_root exists (Path(license_root).mkdir(parents=True, exist_ok=True)) and writes license_text to Path(license_root) / "LICENSE".

Include the usual if __name__ == "__main__": main() boilerplate so the script can be invoked as done in the Dockerfile.

Comment on lines +359 to +368
def test_tts_runtime_stage_rejects_forbidden_tooling_instructions() -> None:
builder_only = """FROM base AS builder
RUN apt-get install -y build-essential make
FROM base AS runtime
RUN echo runtime-ready
"""
assert not _runtime_stage_uses_forbidden_tool(builder_only)

later_debug_stage = """FROM base AS builder
FROM base AS runtime

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (testing): Add a fixture where the tooling absence gate is present but a forbidden tool still appears in another runtime instruction.

One subtle case worth covering is a runtime stage that includes _RUNTIME_TOOLING_ABSENCE_GATE but also later runs a forbidden tooling command (e.g., RUN make or apt-get install build-essential) in the same stage. Because _runtime_stage_uses_forbidden_tool only skips the gate line, that fixture should still be rejected. Adding a test for this scenario would guard against future changes to _runtime_stage_instructions or the gate regex weakening this behavior.

Suggested implementation:

def test_tts_runtime_stage_rejects_forbidden_tooling_instructions() -> None:
    builder_only = """FROM base AS builder
RUN apt-get install -y build-essential make
FROM base AS runtime
RUN echo runtime-ready
"""
    # Forbidden tooling in the builder stage should not cause the runtime stage to be rejected.
    assert not _runtime_stage_uses_forbidden_tool(builder_only)

    later_debug_stage = """FROM base AS builder
FROM base AS runtime
RUN echo runtime-ready
"""
    assert not _runtime_stage_uses_forbidden_tool(later_debug_stage)

    runtime_with_gate_and_forbidden_tooling = """FROM base AS builder
FROM base AS runtime
RUN _RUNTIME_TOOLING_ABSENCE_GATE
RUN make -j$(nproc)
"""
    # Even with the tooling-absence gate present, a later forbidden tooling command
    # in the same runtime stage must still cause rejection.
    assert _runtime_stage_has_tooling_absence_gate(runtime_with_gate_and_forbidden_tooling)
    assert _runtime_stage_uses_forbidden_tool(runtime_with_gate_and_forbidden_tooling)
  1. If the actual gate instruction differs (e.g., it uses a shell wrapper or a different literal than RUN _RUNTIME_TOOLING_ABSENCE_GATE), update the runtime_with_gate_and_forbidden_tooling fixture to match the real gate line so that _runtime_stage_has_tooling_absence_gate returns True.
  2. If your forbidden tooling matcher is keyed on specific commands (e.g., apt-get install rather than make), you may want to replace RUN make -j$(nproc) with a command known to be matched as forbidden in your implementation, such as RUN apt-get install -y build-essential.

@volschin
volschin merged commit 8282216 into main Aug 9, 2026
6 checks passed
@volschin
volschin deleted the perf/slim-cuda1331-tts branch August 9, 2026 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant