class_docstr: An wrapper for the HuggingFace encodec model
  
  Arguments
  ---------
  source : str
      A HuggingFace repository identifier or a path
  save_path : str
      The location where the pretrained model will be saved
  sample_rate : int
      The audio sampling rate
  bandwidth : float
      The encoding bandwidth, in kbps (optional)
      Supported bandwidths:
      1.5, 3.0, 6.0, 12.0, 24.0
  flat_embeddings : bool
      If set to True, embeddings will be flattened into
      (Batch x Length x (Heads * Embedding))
  freeze : bool
      whether the model will be frozen (e.g. not trainable if used
      as part of training another model)
  renorm_embeddings : bool
      whether embeddings should be renormalized. In the original
      model.
  
  Example
  -------
  >>> model_hub = "facebook/encodec_24khz"
  >>> save_path = "savedir"
  >>> model = Encodec(model_hub, save_path)
  >>> audio = torch.randn(4, 1000)
  >>> length = torch.tensor([1.0, .5, .75, 1.0])
  >>> tokens, emb = model.encode(audio, length)
  >>> tokens.shape
  torch.Size([4, 4, 2])
  >>> emb.shape
  torch.Size([4, 4, 2, 128])
  >>> rec = model.decode(tokens, length)
  >>> rec.shape
  torch.Size([4, 1, 1280])
  >>> rec_emb = model.decode_emb(emb, length)
  >>> rec_emb.shape
  torch.Size([4, 1, 1280])
  >>> rec_tokens = model.tokens(emb, length)
  >>> rec_tokens.shape
  torch.Size([4, 4, 2])
  >>> model = Encodec(model_hub, save_path, flat_embeddings=True)
  >>> _, emb = model.encode(audio, length)
  >>> emb.shape
  torch.Size([4, 4, 256])
class_docstr_tok: ["An","wrapper","for","the","HuggingFace","encodec","model","Arguments","--","--","--","--","-","source",":","str","A","HuggingFace","repository","identifier","or","a","path","save_path",":","str","The","location","where","the","pretrained","model","will","be","saved","sample_rate",":","int","The","audio","sampling","rate","bandwidth",":","float","The","encoding","bandwidth",",","in","kbps","(","optional",")","Supported","bandwidths",":","1.5",",","3.0",",","6.0",",","12.0",",","24.0","flat_embeddings",":","bool","If","set","to","True",",","embeddings","will","be","flattened","into","(","Batch","x","Length","x","(","Heads","*","Embedding",")",")","freeze",":","bool","whether","the","model","will","be","frozen","(","e.g",".","not","trainable","if","used","as","part","of","training","another","model",")","renorm_embeddings",":","bool","whether","embeddings","should","be","renormalized",".","In","the","original","model",".","Example","--","--","--","-",">",">",">","model_hub","=","``","facebook\/encodec_24khz","''",">",">",">","save_path","=","``","savedir","''",">",">",">","model","=","Encodec","(","model_hub",",","save_path",")",">",">",">","audio","=","torch.randn","(","4",",","1000",")",">",">",">","length","=","torch.tensor","(","[","1.0",",",".5",",",".75",",","1.0","]",")",">",">",">","tokens",",","emb","=","model.encode","(","audio",",","length",")",">",">",">","tokens.shape","torch.Size","(","[","4",",","4",",","2","]",")",">",">",">","emb.shape","torch.Size","(","[","4",",","4",",","2",",","128","]",")",">",">",">","rec","=","model.decode","(","tokens",",","length",")",">",">",">","rec.shape","torch.Size","(","[","4",",","1",",","1280","]",")",">",">",">","rec_emb","=","model.decode_emb","(","emb",",","length",")",">",">",">","rec_emb.shape","torch.Size","(","[","4",",","1",",","1280","]",")",">",">",">","rec_tokens","=","model.tokens","(","emb",",","length",")",">",">",">","rec_tokens.shape","torch.Size","(","[","4",",","4",",","2","]",")",">",">",">","model","=","Encodec","(","model_hub",",","save_path",",","flat_embeddings=True",")",">",">",">","_",",","emb","=","model.encode","(","audio",",","length",")",">",">",">","emb.shape","torch.Size","(","[","4",",","4",",","256","]",")"]
code: def __init__(
      self,
      source,
      save_path=None,
      sample_rate=None,
      bandwidth=1.5,
      flat_embeddings=False,
      freeze=True,
      renorm_embeddings=True,
  ):
      super().__init__(
          source=source, save_path=save_path, freeze=freeze
      )
      if not sample_rate:
          sample_rate = DEFAULT_SAMPLE_RATE
      self.sample_rate = sample_rate
      self.bandwidth = bandwidth
      self.flat_embeddings = flat_embeddings
      self.num_heads = (
          self.model.quantizer.get_num_quantizers_for_bandwidth(
              bandwidth
          )
      )
      self.num_tokens = self.model.config.codebook_size
      quantizer_layers = self.model.quantizer.layers[: self.num_heads]
      vocabulary = torch.stack(
          [layer.codebook.embed for layer in quantizer_layers]
      )
      self.register_buffer("vocabulary", vocabulary)
      _, self.num_tokens, self.emb_dim = self.vocabulary.shape
      vocabulary_flat = self.vocabulary.reshape(
          self.num_heads * self.num_tokens, self.emb_dim
      )
      self.register_buffer("vocabulary_flat", vocabulary_flat)
      token_index_offsets = (
          torch.arange(self.num_heads)[None, None, :] * self.num_tokens
      )
      self.register_buffer("token_index_offsets", token_index_offsets)
      self.renorm_embeddings = renorm_embeddings
      if self.renorm_embeddings:
          emb_mean, emb_std = self._precalibrate()
          self.register_buffer("emb_mean", emb_mean)
          self.register_buffer("emb_std", emb_std)
      if self.freeze:
          logger.warning("huggingface_Encodec - Encodec is frozen.")
          for param in self.model.parameters():
              param.requires_grad = False
  
file_path: speechbrain/speechbrain/lobes/models/huggingface_transformers/encodec.py
imports: import torch
  import logging
  from torch.nn import functional
  from speechbrain.dataio.dataio import length_to_mask, clean_padding_
  from speechbrain.lobes.models.huggingface_transformers.huggingface import HFTransformersInterface
label_desc: Use image node_id 1 to create a new Encodec object from inherited base classes: HFTransformersInterface with example: obj = Encodec(source, save_path, sample_rate, bandwidth, flat_embeddings, freeze, renorm_embeddings)