Skip to main content

TorchStream

TorchStream is a library to help ML developers stream PyTorch models without retraining nor rewriting them, in order to reduce their latency or use them in live applications.

TorchStream comes with a website of live examples.

Installation

Install as a package (any OS, CUDA optional):

(uv) pip install torchstream-lib

Install as a project, to run the streamlit examples yourself (set "--extra cpu" for a cpu only install):

git clone https://github.com/CorentinJ/TorchStream
cd TorchStream
uv run --group demos streamlit run examples --extra cuda

If you don't have uv yet:

# On Windows:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
# On Linux
curl -LsSf https://astral.sh/uv/install.sh | sh

# Alternatively, on any platform if you have pip installed you can do
pip install -U uv

Overview

TorchStream offers a set of tools to help you stream complex neural networks and other sequence to sequence transforms.

The example below requires cloning the project and installing the demos dependencies (uv sync --group demos). It streams BigVGAN, a state of the art neural vocoder:

import logging

import librosa
import torch

from examples.resources.bigvgan.bigvgan import BigVGAN
from examples.resources.bigvgan.meldataset import get_mel_spectrogram
from torchstream import SeqSpec, SlidingWindowStream, find_sliding_window_params

logging.basicConfig(level=logging.INFO)

device = "cuda" if torch.cuda.is_available() else "cpu"
model = BigVGAN.from_pretrained("nvidia/bigvgan_v2_24khz_100band_256x").eval().to(device)
model.remove_weight_norm()

# Get a sample mel spectrogram input
wave, sample_rate = librosa.load(librosa.ex("libri1"), sr=model.h.sampling_rate)
mel = get_mel_spectrogram(torch.from_numpy(wave).unsqueeze(0), model.h).to(device)

# Specify the model's input format: a melspectrogram
in_spec = SeqSpec(1, model.h.num_mels, -1, device=device)
# Output format: an audio waveform
out_spec = SeqSpec(1, 1, -1, device=device)

# Use TorchStream's solver to find the sliding window parameters of BigVGAN
sli_params = find_sliding_window_params(
    trsfm=model,
    in_spec=in_spec,
    out_spec=out_spec,
    max_in_out_seq_size=1_000_000,
)[0]

# Perform streaming inference
stream = SlidingWindowStream(model, sli_params, in_spec, out_spec)
for audio_chunk in stream.forward_in_chunks_iter(mel, chunk_size=80):
    print(f"Got a {tuple(audio_chunk.shapes[0])} shaped audio chunk")

Disclaimer

TorchStream is developed by myself. It is not affiliated with, endorsed by, or sponsored by the PyTorch team or Meta.

Release files for torchstream-lib 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torchstream-lib 1.0.2
File Size Uploaded
torchstream_lib-1.0.2.tar.gz 57.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torchstream-lib 1.0.2
File Interpreter ABI Platform
torchstream_lib-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 121.1 kB

Release files / torchstream_lib-1.0.2.tar.gz

Download URL torchstream_lib-1.0.2.tar.gz
Size 57.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a0f78101f8b810943a2b2aa929e36c91eff25f99c5b1d9a8e5ca521d2fe814f4
BLAKE2b-256 checksum
How to use checksums
28a477b3623233366194bd19bad6daaa5e955c877ee865786a0b1077287b3099
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.5

Release files / torchstream_lib-1.0.2-py3-none-any.whl

Download URL torchstream_lib-1.0.2-py3-none-any.whl
Size 63.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d8814dc3272f457c177c673e541ffdaddb91cfd1a73af661b6fe5f7c377667a1
BLAKE2b-256 checksum
How to use checksums
c13ec759e546570c05bd28d9e8c533743818785133b1a100791c1e323bd806f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.5

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page