codingutf8 2023 meta platforms inc and affiliates and the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license encodec model configuration import math from typing import optional import numpy as np from configurationutils import pretrainedconfig from utils import logging logger logging getloggername encodecpretrainedconfigarchivemap facebookencodec24khz https huggingface cofacebookencodec24khzresolvemainconfig json facebookencodec48khz https huggingface cofacebookencodec48khzresolvemainconfig json class encodecconfigpretrainedconfig r this is the configuration class to store the configuration of an encodecmodel it is used to instantiate a encodec model according to the specified arguments defining the model architecture instantiating a configuration with the defaults will yield a similar configuration to that of the facebookencodec24khzhttps huggingface cofacebookencodec24khz architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args targetbandwidths listfloat optional defaults to 1 5 3 0 6 0 12 0 24 0 the range of diffent bandwiths the model can encode audio with samplingrate int optional defaults to 24000 the sampling rate at which the audio waveform should be digitalized expressed in hertz hz audiochannels int optional defaults to 1 number of channels in the audio data either 1 for mono or 2 for stereo normalize bool optional defaults to false whether the audio shall be normalized when passed chunklengths float optional if defined the audio is preprocessed into chunks of lengths chunklengths and then encoded overlap float optional defines the overlap between each chunk it is used to compute the chunkstride using the following formulae int1 0 self overlap self chunklength hiddensize int optional defaults to 128 intermediate representation dimension numfilters int optional defaults to 32 number of convolution kernels of first encodecconv1d down sampling layer numresiduallayers int optional defaults to 1 number of residual layers upsamplingratios sequenceint optional defaults to 8 5 4 2 kernel size and stride ratios the encoder uses downsampling ratios instead of upsampling ratios hence it will use the ratios in the reverse order to the ones specified here that must match the decoder order normtype str optional defaults to weightnorm normalization method should be in weightnorm timegroupnorm kernelsize int optional defaults to 7 kernel size for the initial convolution lastkernelsize int optional defaults to 7 kernel size for the last convolution layer residualkernelsize int optional defaults to 3 kernel size for the residual layers dilationgrowthrate int optional defaults to 2 how much to increase the dilation with each layer usecausalconv bool optional defaults to true whether to use fully causal convolution padmode str optional defaults to reflect padding mode for the convolutions compress int optional defaults to 2 reduced dimensionality in residual branches from demucs v3 numlstmlayers int optional defaults to 2 number of lstm layers at the end of the encoder trimrightratio float optional defaults to 1 0 ratio for trimming at the right of the transposed convolution under the usecausalconv true setup if equal to 1 0 it means that all the trimming is done at the right codebooksize int optional defaults to 1024 number of discret codes that make up vqvae codebookdim int optional dimension of the codebook vectors if not defined uses hiddensize useconvshortcut bool optional defaults to true whether to use a convolutional layer as the skip connection in the encodecresnetblock block if false an identity function will be used giving a generic residual connection example python from transformers import encodecmodel encodecconfig initializing a facebookencodec24khz style configuration configuration encodecconfig initializing a model with random weights from the facebookencodec24khz style configuration model encodecmodelconfiguration accessing the model configuration configuration model config modeltype encodec def init self targetbandwidths1 5 3 0 6 0 12 0 24 0 samplingrate24000 audiochannels1 normalizefalse chunklengthsnone overlapnone hiddensize128 numfilters32 numresiduallayers1 upsamplingratios8 5 4 2 normtypeweightnorm kernelsize7 lastkernelsize7 residualkernelsize3 dilationgrowthrate2 usecausalconvtrue padmodereflect compress2 numlstmlayers2 trimrightratio1 0 codebooksize1024 codebookdimnone useconvshortcuttrue kwargs self targetbandwidths targetbandwidths self samplingrate samplingrate self audiochannels audiochannels self normalize normalize self chunklengths chunklengths self overlap overlap self hiddensize hiddensize self numfilters numfilters self numresiduallayers numresiduallayers self upsamplingratios upsamplingratios self normtype normtype self kernelsize kernelsize self lastkernelsize lastkernelsize self residualkernelsize residualkernelsize self dilationgrowthrate dilationgrowthrate self usecausalconv usecausalconv self padmode padmode self compress compress self numlstmlayers numlstmlayers self trimrightratio trimrightratio self codebooksize codebooksize self codebookdim codebookdim if codebookdim is not none else hiddensize self useconvshortcut useconvshortcut if self normtype not in weightnorm timegroupnorm raise valueerror f self normtype must be one of weightnorm timegroupnorm got self normtype super initkwargs this is a property because you might want to change the chunklengths on the fly property def chunklengthself optionalint if self chunklengths is none return none else return intself chunklengths self samplingrate this is a property because you might want to change the chunklengths on the fly property def chunkstrideself optionalint if self chunklengths is none or self overlap is none return none else return max1 int1 0 self overlap self chunklength property def framerateself int hoplength np prodself upsamplingratios return math ceilself samplingrate hoplength property def numquantizersself int return int1000 self targetbandwidths1 self framerate 10 coding utf 8 2023 meta platforms inc and affiliates and the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license encodec model configuration this is the configuration class to store the configuration of an encodecmodel it is used to instantiate a encodec model according to the specified arguments defining the model architecture instantiating a configuration with the defaults will yield a similar configuration to that of the facebook encodec_24khz https huggingface co facebook encodec_24khz architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args target_bandwidths list float optional defaults to 1 5 3 0 6 0 12 0 24 0 the range of diffent bandwiths the model can encode audio with sampling_rate int optional defaults to 24000 the sampling rate at which the audio waveform should be digitalized expressed in hertz hz audio_channels int optional defaults to 1 number of channels in the audio data either 1 for mono or 2 for stereo normalize bool optional defaults to false whether the audio shall be normalized when passed chunk_length_s float optional if defined the audio is pre processed into chunks of lengths chunk_length_s and then encoded overlap float optional defines the overlap between each chunk it is used to compute the chunk_stride using the following formulae int 1 0 self overlap self chunk_length hidden_size int optional defaults to 128 intermediate representation dimension num_filters int optional defaults to 32 number of convolution kernels of first encodecconv1d down sampling layer num_residual_layers int optional defaults to 1 number of residual layers upsampling_ratios sequence int optional defaults to 8 5 4 2 kernel size and stride ratios the encoder uses downsampling ratios instead of upsampling ratios hence it will use the ratios in the reverse order to the ones specified here that must match the decoder order norm_type str optional defaults to weight_norm normalization method should be in weight_norm time_group_norm kernel_size int optional defaults to 7 kernel size for the initial convolution last_kernel_size int optional defaults to 7 kernel size for the last convolution layer residual_kernel_size int optional defaults to 3 kernel size for the residual layers dilation_growth_rate int optional defaults to 2 how much to increase the dilation with each layer use_causal_conv bool optional defaults to true whether to use fully causal convolution pad_mode str optional defaults to reflect padding mode for the convolutions compress int optional defaults to 2 reduced dimensionality in residual branches from demucs v3 num_lstm_layers int optional defaults to 2 number of lstm layers at the end of the encoder trim_right_ratio float optional defaults to 1 0 ratio for trimming at the right of the transposed convolution under the use_causal_conv true setup if equal to 1 0 it means that all the trimming is done at the right codebook_size int optional defaults to 1024 number of discret codes that make up vqvae codebook_dim int optional dimension of the codebook vectors if not defined uses hidden_size use_conv_shortcut bool optional defaults to true whether to use a convolutional layer as the skip connection in the encodecresnetblock block if false an identity function will be used giving a generic residual connection example python from transformers import encodecmodel encodecconfig initializing a facebook encodec_24khz style configuration configuration encodecconfig initializing a model with random weights from the facebook encodec_24khz style configuration model encodecmodel configuration accessing the model configuration configuration model config this is a property because you might want to change the chunk_length_s on the fly this is a property because you might want to change the chunk_length_s on the fly
import math
from typing import Optional

import numpy as np

from ...configuration_utils import PretrainedConfig
from ...utils import logging


logger = logging.get_logger(__name__)

ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "facebook/encodec_24khz": "https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json",
    "facebook/encodec_48khz": "https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json",
}


class EncodecConfig(PretrainedConfig):
    r

    model_type = "encodec"

    def __init__(
        self,
        target_bandwidths=[1.5, 3.0, 6.0, 12.0, 24.0],
        sampling_rate=24_000,
        audio_channels=1,
        normalize=False,
        chunk_length_s=None,
        overlap=None,
        hidden_size=128,
        num_filters=32,
        num_residual_layers=1,
        upsampling_ratios=[8, 5, 4, 2],
        norm_type="weight_norm",
        kernel_size=7,
        last_kernel_size=7,
        residual_kernel_size=3,
        dilation_growth_rate=2,
        use_causal_conv=True,
        pad_mode="reflect",
        compress=2,
        num_lstm_layers=2,
        trim_right_ratio=1.0,
        codebook_size=1024,
        codebook_dim=None,
        use_conv_shortcut=True,
        **kwargs,
    ):
        self.target_bandwidths = target_bandwidths
        self.sampling_rate = sampling_rate
        self.audio_channels = audio_channels
        self.normalize = normalize
        self.chunk_length_s = chunk_length_s
        self.overlap = overlap
        self.hidden_size = hidden_size
        self.num_filters = num_filters
        self.num_residual_layers = num_residual_layers
        self.upsampling_ratios = upsampling_ratios
        self.norm_type = norm_type
        self.kernel_size = kernel_size
        self.last_kernel_size = last_kernel_size
        self.residual_kernel_size = residual_kernel_size
        self.dilation_growth_rate = dilation_growth_rate
        self.use_causal_conv = use_causal_conv
        self.pad_mode = pad_mode
        self.compress = compress
        self.num_lstm_layers = num_lstm_layers
        self.trim_right_ratio = trim_right_ratio
        self.codebook_size = codebook_size
        self.codebook_dim = codebook_dim if codebook_dim is not None else hidden_size
        self.use_conv_shortcut = use_conv_shortcut

        if self.norm_type not in ["weight_norm", "time_group_norm"]:
            raise ValueError(
                f'self.norm_type must be one of `"weight_norm"`, `"time_group_norm"`), got {self.norm_type}'
            )

        super().__init__(**kwargs)

    
    @property
    def chunk_length(self) -> Optional[int]:
        if self.chunk_length_s is None:
            return None
        else:
            return int(self.chunk_length_s * self.sampling_rate)

    
    @property
    def chunk_stride(self) -> Optional[int]:
        if self.chunk_length_s is None or self.overlap is None:
            return None
        else:
            return max(1, int((1.0 - self.overlap) * self.chunk_length))

    @property
    def frame_rate(self) -> int:
        hop_length = np.prod(self.upsampling_ratios)
        return math.ceil(self.sampling_rate / hop_length)

    @property
    def num_quantizers(self) -> int:
        return int(1000 * self.target_bandwidths[-1] // (self.frame_rate * 10))