API Reference

torchstreamer

Streamers for core PyTorch modules

BaseStream

Base class for all streamers

forward(x, final=False) abstractmethod

Processes an input tensor through the stream

Parameters:
  • x (Tensor) –

    input tensor, shaped [C, T]

  • final (bool, default: False ) –

    true if this is the final input, used to flush internal buffers

Returns:
  • y( Tensor | None ) –

    streaming output, if enough inputs have been buffered, null otherwise

Sequential1dStream

Streamer for torch.nn.Sequential networks over 1D [B, C, T] tensors

Parameters:
  • net (Sequential) –

    sequential network to stream

Example
net = nn.Sequential(
   nn.Conv1d(1, 128, 1),
   nn.Conv1d(128, 1, 1),
)
stream = pts.Sequential1dStream(net)

Residual1dStream

Streamer for ResNets

This streamer streams over the inner layers of a residual network (the part without the residual connection). It buffers the inputs so that they can be added (without padding) to the outputs after processing through the network's receptive field.

Parameters:
  • net (Module) –

    inner part of the residual network to stream

Example
net = pts.Residual1d(
    nn.Conv1d(1, 128, 1),
    nn.Conv1d(128, 1, 1),
)
stream = pts.Residual1dStream(net)

Residual1d

PyTorch module for wrapping a network with a residual connection

Note this is not a streamer, just a utility module to make it easier to build streamable residual networks.

Parameters:
  • net (Module) –

    inner network, which will be wrapped with a residual connection

Raises:
  • ValueError

    if the inner network contains strided convolutions, which are not supported

Example
net = nn.Sequential(
    nn.Conv1d(1, 128, 1),
    pts.Residual1d(
        nn.GELU(),
        nn.Conv1d(128, 256, 1),
        nn.GELU(),
        nn.Conv1d(256, 128, 1),
    ),
    nn.Conv1d(128, 1, 1),
)

Conv1dStream

Streamer for Conv1d modules

Note that the stream ignores any padding configuration for the convolution, performing an exact (valid) convolution instead.

Parameters:
  • net (Conv1d) –

    Conv1d layer to stream

ConvTranspose1dStream

Streamer for ConvTranspose1d modules

Note that the stream ignores any padding configuration for the convolution, performing an exact (valid) transposed convolution instead.

Parameters:
  • net (ConvTranspose1d) –

    ConvTranspose1d layer to stream

Pool1dStream

Streamer for PyTorch pooling modules (AvgPool1d/MaxPool1d)

Parameters:
  • net (AvgPool1d | MaxPool1d) –

    pooling layer to stream

Raises:
  • ValueError

    an invalid pooling layer was passed

Elementwise1dStream

Streamer for simple pointwise/elementwise PyTorch modules

Elementwise modules are modules that do not have a spatial receptive field, so operations do not span or stride the final dimension.

Parameters:
  • net (Module) –

    PyTorch layer to stream

register_streamer(module, streamer)

Registers a new stream for a PyTorch module type

Note that custom streamers are not needed for simple elementwise modules, these are wrapped automatically with Elementwise1dStream instances. Custom streamers are only needed for custom modules that process more than one spatial element at a time.

Parameters:
  • module (type) –

    the type of the PyTorch module to register for

  • streamer (Callable[[Module], BaseStream]) –

    a callable that constructs a streamer for this module type

Example
class MyModule(nn.Module):
    ...

class MyModuleStream(pts.BaseStream):
    def __init__(self, net: MyModule):
    ...

register_streamer(MyModule, lambda net: MyModuleStream(net))

create_stream(net)

Finds and creates a stream handler for a registered or built-in module

Parameters:
  • net (Module) –

    the module to stream

Returns:
  • stream( BaseStream ) –

    a streamer that wraps the specified module

Example
stream = create_stream(nn.Conv1d(1, 128, 1))

torchstreamer.audio

Streamers for audio signals and TorchAudio modules

ResampleStream

Audio sample rate conversion streamer

This streamer performs the same operation as torchaudio.functional.resample in a streaming fashion, buffering inputs as needed to match the non-streamed outputs exactly. Inputs can be shaped [T] or [C, T], and the tensor rank will be preserved in the outputs.

Parameters:
  • source_fs (int) –

    input audio sample rate, in Hz

  • target_fs (int) –

    output audio sample rate, in Hz

  • resampling_method (str, default: 'sinc_interp_hann' ) –

    the resampling window to use

  • lowpass_filter_width (int, default: 6 ) –

    resampling filter sharpness

  • rolloff (float, default: 0.99 ) –

    lowpass filter roll-of, as a fraction of Nyquist frequency

  • beta (float, default: 14.769656459379492 ) –

    kaiser window shape parameter, ignored for other resampling methods