pretrained.encodec
Defines a simple API for interacting with Meta’s pretrained Encodec model.
This API only supports the causal version of the model, which is better suited for streaming applications.
from pretrained.encodec import pretrained_encodec
model = pretrained_encodec("24khz")
encoder, decoder = model.get_encoder(), model.get_decoder()
# Get the tokens for a waveform.
tokens = encoder.encode(torch.randn(1, 24_000))
# Reconstructs the waveform from the tokens.
reconstructed = decoder.decode(tokens)
- pretrained.encodec.get_extra_padding_for_conv1d(x: Tensor, kernel_size: int, stride: int, padding_total: int = 0) int [source]
- pretrained.encodec.pad1d(x: Tensor, paddings: tuple[int, int], mode: Literal['reflect', 'replicate', 'circular', 'constant'] = 'constant', value: float = 0.0) Tensor [source]
- class pretrained.encodec.NormConv1d(conv: Conv1d, norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'] | Literal['no_norm', 'weight', 'spectral'], *, groups: int = 1)[source]
Bases:
Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(x: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class pretrained.encodec.NormConvTranspose1d(conv: ConvTranspose1d, norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'] | Literal['no_norm', 'weight', 'spectral'], *, groups: int = 1)[source]
Bases:
Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(x: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class pretrained.encodec.SConv1d(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, dilation: int = 1, groups: int = 1, bias: bool = True, causal: bool = False, norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'] | Literal['no_norm', 'weight', 'spectral'] = 'no_norm', norm_groups: int = 1, pad_mode: Literal['reflect', 'replicate', 'circular', 'constant'] = 'reflect')[source]
Bases:
Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(x: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class pretrained.encodec.SConvTranspose1d(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, causal: bool = False, norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'] | Literal['no_norm', 'weight', 'spectral'] = 'no_norm', norm_groups: int = 1, trim_right_ratio: float = 1.0)[source]
Bases:
Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(x: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class pretrained.encodec.SLSTM(dimension: int, num_layers: int = 2, skip: bool = True)[source]
Bases:
Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(x: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class pretrained.encodec.SEANetResnetBlock(dim: int, kernel_sizes: Sequence[int] = [3, 1], dilations: Sequence[int] = [1, 1], activation: Literal['no_act', 'relu', 'relu6', 'relu2', 'clamp6', 'leaky_relu', 'elu', 'celu', 'selu', 'gelu', 'gelu_fast', 'sigmoid', 'log_sigmoid', 'hard_sigomid', 'tanh', 'softsign', 'softplus', 'silu', 'mish', 'swish', 'hard_swish', 'soft_shrink', 'hard_shrink', 'tanh_shrink', 'soft_sign', 'relu_squared', 'laplace'] = 'elu', norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'] | Literal['no_norm', 'weight', 'spectral'] = 'weight', causal: bool = False, pad_mode: Literal['reflect', 'replicate', 'circular', 'constant'] = 'reflect', compress: int = 2, true_skip: bool = True)[source]
Bases:
Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(x: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class pretrained.encodec.SEANetEncoder(channels: int = 1, dimension: int = 128, n_filters: int = 32, n_residual_layers: int = 1, ratios: Sequence[int] = [8, 5, 4, 2], activation: Literal['no_act', 'relu', 'relu6', 'relu2', 'clamp6', 'leaky_relu', 'elu', 'celu', 'selu', 'gelu', 'gelu_fast', 'sigmoid', 'log_sigmoid', 'hard_sigomid', 'tanh', 'softsign', 'softplus', 'silu', 'mish', 'swish', 'hard_swish', 'soft_shrink', 'hard_shrink', 'tanh_shrink', 'soft_sign', 'relu_squared', 'laplace'] = 'elu', norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'] | Literal['no_norm', 'weight', 'spectral'] = 'weight', kernel_size: int = 7, last_kernel_size: int = 7, residual_kernel_size: int = 3, dilation_base: int = 2, causal: bool = False, pad_mode: Literal['reflect', 'replicate', 'circular', 'constant'] = 'reflect', true_skip: bool = False, compress: int = 2, lstm: int = 2)[source]
Bases:
Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(x: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class pretrained.encodec.SEANetDecoder(channels: int = 1, dimension: int = 128, n_filters: int = 32, n_residual_layers: int = 1, ratios: Sequence[int] = [8, 5, 4, 2], activation: Literal['no_act', 'relu', 'relu6', 'relu2', 'clamp6', 'leaky_relu', 'elu', 'celu', 'selu', 'gelu', 'gelu_fast', 'sigmoid', 'log_sigmoid', 'hard_sigomid', 'tanh', 'softsign', 'softplus', 'silu', 'mish', 'swish', 'hard_swish', 'soft_shrink', 'hard_shrink', 'tanh_shrink', 'soft_sign', 'relu_squared', 'laplace'] = 'elu', final_activation: Literal['no_act', 'relu', 'relu6', 'relu2', 'clamp6', 'leaky_relu', 'elu', 'celu', 'selu', 'gelu', 'gelu_fast', 'sigmoid', 'log_sigmoid', 'hard_sigomid', 'tanh', 'softsign', 'softplus', 'silu', 'mish', 'swish', 'hard_swish', 'soft_shrink', 'hard_shrink', 'tanh_shrink', 'soft_sign', 'relu_squared', 'laplace'] = 'no_act', norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'] | Literal['no_norm', 'weight', 'spectral'] = 'weight', kernel_size: int = 7, last_kernel_size: int = 7, residual_kernel_size: int = 3, dilation_base: int = 2, causal: bool = False, pad_mode: Literal['reflect', 'replicate', 'circular', 'constant'] = 'reflect', true_skip: bool = False, compress: int = 2, lstm: int = 2, trim_right_ratio: float = 1.0)[source]
Bases:
Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(z: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class pretrained.encodec.Encodec(encoder: SEANetEncoder, decoder: SEANetDecoder, quantizer: ResidualVectorQuantization, channels: int = 1, sample_rate: int | None = None)[source]
Bases:
Module
EnCodec model operating on the raw waveform.
- Parameters:
encoder – Encoder network.
decoder – Decoder network.
quantizer – The residual quantizer module.
channels – Number of audio channels.
sample_rate – The sample rate of the input audio.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(x: Tensor) tuple[torch.Tensor, torch.Tensor] [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class pretrained.encodec.Encoder(encodec: Encodec)[source]
Bases:
Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(waveform: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class pretrained.encodec.Decoder(encodec: Encodec)[source]
Bases:
Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(waveform: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class pretrained.encodec.EncodecConfig(num_quantizers: int, channels: int, causal: bool, norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'] | Literal['no_norm', 'weight', 'spectral'], sample_rate: int | None = None)[source]
Bases:
object
- num_quantizers: int
- channels: int
- causal: bool
- norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'] | Literal['no_norm', 'weight', 'spectral']
- sample_rate: int | None = None