modeling_encodec.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. # coding=utf-8
  2. # Copyright 2023 Meta Platforms, Inc. and affiliates, and the HuggingFace Inc. team. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """PyTorch EnCodec model."""
  16. import math
  17. from dataclasses import dataclass
  18. from typing import Optional, Union
  19. import torch
  20. from torch import nn
  21. from ...modeling_utils import PreTrainedAudioTokenizerBase
  22. from ...utils import (
  23. ModelOutput,
  24. auto_docstring,
  25. logging,
  26. )
  27. from .configuration_encodec import EncodecConfig
  28. logger = logging.get_logger(__name__)
  29. # General docstring
  30. @dataclass
  31. @auto_docstring
  32. class EncodecOutput(ModelOutput):
  33. r"""
  34. audio_codes (`torch.LongTensor` of shape `(nb_frames, batch_size, nb_quantizers, frame_len)`, *optional*):
  35. Discrete code embeddings computed using `model.encode`.
  36. audio_values (`torch.FloatTensor` of shape `(batch_size, segment_length)`, *optional*):
  37. Decoded audio values, obtained using the decoder part of Encodec.
  38. """
  39. audio_codes: Optional[torch.LongTensor] = None
  40. audio_values: Optional[torch.FloatTensor] = None
  41. @dataclass
  42. @auto_docstring
  43. class EncodecEncoderOutput(ModelOutput):
  44. r"""
  45. audio_codes (`torch.LongTensor` of shape `(nb_frames, batch_size, nb_quantizers, frame_len)`, *optional*):
  46. Discrete code embeddings computed using `model.encode`.
  47. audio_scales (list of length `nb_frames` of `torch.Tensor` of shape `(batch_size, 1)`, *optional*):
  48. Scaling factor for each `audio_codes` input. This is used to unscale each chunk of audio when decoding.
  49. last_frame_pad_length (`int`, *optional*):
  50. The length of the padding in the last frame, if any. This is used to ensure that the encoded frames can be
  51. outputted as a tensor. This value should be passed during decoding to ensure padding is removed from the
  52. encoded frames.
  53. """
  54. audio_codes: Optional[torch.LongTensor] = None
  55. audio_scales: Optional[torch.FloatTensor] = None
  56. last_frame_pad_length: Optional[int] = None
  57. @dataclass
  58. @auto_docstring
  59. class EncodecDecoderOutput(ModelOutput):
  60. r"""
  61. audio_values (`torch.FloatTensor` of shape `(batch_size, segment_length)`, *optional*):
  62. Decoded audio values, obtained using the decoder part of Encodec.
  63. """
  64. audio_values: Optional[torch.FloatTensor] = None
  65. class EncodecConv1d(nn.Module):
  66. """Conv1d with asymmetric or causal padding and normalization."""
  67. def __init__(
  68. self, config, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, dilation: int = 1
  69. ):
  70. super().__init__()
  71. self.causal = config.use_causal_conv
  72. self.pad_mode = config.pad_mode
  73. self.norm_type = config.norm_type
  74. if self.norm_type not in ["weight_norm", "time_group_norm"]:
  75. raise ValueError(
  76. f'self.norm_type must be one of `"weight_norm"`, `"time_group_norm"`), got {self.norm_type}'
  77. )
  78. # warn user on unusual setup between dilation and stride
  79. if stride > 1 and dilation > 1:
  80. logger.warning(
  81. "EncodecConv1d has been initialized with stride > 1 and dilation > 1"
  82. f" (kernel_size={kernel_size} stride={stride}, dilation={dilation})."
  83. )
  84. self.conv = nn.Conv1d(in_channels, out_channels, kernel_size, stride, dilation=dilation)
  85. weight_norm = nn.utils.weight_norm
  86. if hasattr(nn.utils.parametrizations, "weight_norm"):
  87. weight_norm = nn.utils.parametrizations.weight_norm
  88. if self.norm_type == "weight_norm":
  89. self.conv = weight_norm(self.conv)
  90. elif self.norm_type == "time_group_norm":
  91. self.norm = nn.GroupNorm(1, out_channels)
  92. kernel_size = self.conv.kernel_size[0]
  93. stride = torch.tensor(self.conv.stride[0], dtype=torch.int64)
  94. dilation = self.conv.dilation[0]
  95. # Effective kernel size with dilations.
  96. kernel_size = torch.tensor((kernel_size - 1) * dilation + 1, dtype=torch.int64)
  97. self.register_buffer("stride", stride, persistent=False)
  98. self.register_buffer("kernel_size", kernel_size, persistent=False)
  99. self.register_buffer("padding_total", kernel_size - stride, persistent=False)
  100. def _get_extra_padding_for_conv1d(
  101. self,
  102. hidden_states: torch.Tensor,
  103. ) -> torch.Tensor:
  104. """See `pad_for_conv1d`."""
  105. length = hidden_states.shape[-1]
  106. n_frames = (length - self.kernel_size + self.padding_total) / self.stride + 1
  107. n_frames = torch.ceil(n_frames).to(torch.int64) - 1
  108. ideal_length = n_frames * self.stride + self.kernel_size - self.padding_total
  109. return ideal_length - length
  110. @staticmethod
  111. def _pad1d(hidden_states: torch.Tensor, paddings: tuple[int, int], mode: str = "zero", value: float = 0.0):
  112. """Tiny wrapper around torch.nn.functional.pad, just to allow for reflect padding on small input.
  113. If this is the case, we insert extra 0 padding to the right before the reflection happens.
  114. """
  115. length = hidden_states.shape[-1]
  116. padding_left, padding_right = paddings
  117. if mode != "reflect":
  118. return nn.functional.pad(hidden_states, paddings, mode, value)
  119. max_pad = max(padding_left, padding_right)
  120. extra_pad = 0
  121. if length <= max_pad:
  122. extra_pad = max_pad - length + 1
  123. hidden_states = nn.functional.pad(hidden_states, (0, extra_pad))
  124. padded = nn.functional.pad(hidden_states, paddings, mode, value)
  125. end = padded.shape[-1] - extra_pad
  126. return padded[..., :end]
  127. def forward(self, hidden_states):
  128. extra_padding = self._get_extra_padding_for_conv1d(hidden_states)
  129. if self.causal:
  130. # Left padding for causal
  131. hidden_states = self._pad1d(hidden_states, (self.padding_total, extra_padding), mode=self.pad_mode)
  132. else:
  133. # Asymmetric padding required for odd strides
  134. padding_right = self.padding_total // 2
  135. padding_left = self.padding_total - padding_right
  136. hidden_states = self._pad1d(
  137. hidden_states, (padding_left, padding_right + extra_padding), mode=self.pad_mode
  138. )
  139. hidden_states = self.conv(hidden_states)
  140. if self.norm_type == "time_group_norm":
  141. hidden_states = self.norm(hidden_states)
  142. return hidden_states
  143. class EncodecConvTranspose1d(nn.Module):
  144. """ConvTranspose1d with asymmetric or causal padding and normalization."""
  145. def __init__(self, config, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1):
  146. super().__init__()
  147. self.causal = config.use_causal_conv
  148. self.trim_right_ratio = config.trim_right_ratio
  149. self.norm_type = config.norm_type
  150. if self.norm_type not in ["weight_norm", "time_group_norm"]:
  151. raise ValueError(
  152. f'self.norm_type must be one of `"weight_norm"`, `"time_group_norm"`), got {self.norm_type}'
  153. )
  154. self.conv = nn.ConvTranspose1d(in_channels, out_channels, kernel_size, stride)
  155. weight_norm = nn.utils.weight_norm
  156. if hasattr(nn.utils.parametrizations, "weight_norm"):
  157. weight_norm = nn.utils.parametrizations.weight_norm
  158. if config.norm_type == "weight_norm":
  159. self.conv = weight_norm(self.conv)
  160. elif config.norm_type == "time_group_norm":
  161. self.norm = nn.GroupNorm(1, out_channels)
  162. if not (self.causal or self.trim_right_ratio == 1.0):
  163. raise ValueError("`trim_right_ratio` != 1.0 only makes sense for causal convolutions")
  164. def forward(self, hidden_states):
  165. kernel_size = self.conv.kernel_size[0]
  166. stride = self.conv.stride[0]
  167. padding_total = kernel_size - stride
  168. hidden_states = self.conv(hidden_states)
  169. if self.norm_type == "time_group_norm":
  170. hidden_states = self.norm(hidden_states)
  171. # We will only trim fixed padding. Extra padding from `pad_for_conv1d` would be
  172. # removed at the very end, when keeping only the right length for the output,
  173. # as removing it here would require also passing the length at the matching layer
  174. # in the encoder.
  175. if self.causal:
  176. # Trim the padding on the right according to the specified ratio
  177. # if trim_right_ratio = 1.0, trim everything from right
  178. padding_right = math.ceil(padding_total * self.trim_right_ratio)
  179. else:
  180. # Asymmetric padding required for odd strides
  181. padding_right = padding_total // 2
  182. padding_left = padding_total - padding_right
  183. # unpad
  184. end = hidden_states.shape[-1] - padding_right
  185. hidden_states = hidden_states[..., padding_left:end]
  186. return hidden_states
  187. class EncodecLSTM(nn.Module):
  188. """
  189. LSTM without worrying about the hidden state, nor the layout of the data. Expects input as convolutional layout.
  190. """
  191. def __init__(self, config: EncodecConfig, dimension: int):
  192. super().__init__()
  193. self.lstm = nn.LSTM(dimension, dimension, config.num_lstm_layers)
  194. def forward(self, hidden_states):
  195. hidden_states = hidden_states.permute(2, 0, 1)
  196. hidden_states = self.lstm(hidden_states)[0] + hidden_states
  197. hidden_states = hidden_states.permute(1, 2, 0)
  198. return hidden_states
  199. class EncodecResnetBlock(nn.Module):
  200. """
  201. Residual block from SEANet model as used by EnCodec.
  202. """
  203. def __init__(self, config: EncodecConfig, dim: int, dilations: list[int]):
  204. super().__init__()
  205. kernel_sizes = (config.residual_kernel_size, 1)
  206. if len(kernel_sizes) != len(dilations):
  207. raise ValueError("Number of kernel sizes should match number of dilations")
  208. hidden = dim // config.compress
  209. block = []
  210. for i, (kernel_size, dilation) in enumerate(zip(kernel_sizes, dilations)):
  211. in_chs = dim if i == 0 else hidden
  212. out_chs = dim if i == len(kernel_sizes) - 1 else hidden
  213. block += [nn.ELU()]
  214. block += [EncodecConv1d(config, in_chs, out_chs, kernel_size, dilation=dilation)]
  215. self.block = nn.ModuleList(block)
  216. if config.use_conv_shortcut:
  217. self.shortcut = EncodecConv1d(config, dim, dim, kernel_size=1)
  218. else:
  219. self.shortcut = nn.Identity()
  220. def forward(self, hidden_states):
  221. residual = hidden_states
  222. for layer in self.block:
  223. hidden_states = layer(hidden_states)
  224. return self.shortcut(residual) + hidden_states
  225. class EncodecEncoder(nn.Module):
  226. """SEANet encoder as used by EnCodec."""
  227. def __init__(self, config: EncodecConfig):
  228. super().__init__()
  229. model = [EncodecConv1d(config, config.audio_channels, config.num_filters, config.kernel_size)]
  230. scaling = 1
  231. # Downsample to raw audio scale
  232. for ratio in reversed(config.upsampling_ratios):
  233. current_scale = scaling * config.num_filters
  234. # Add residual layers
  235. for j in range(config.num_residual_layers):
  236. model += [EncodecResnetBlock(config, current_scale, [config.dilation_growth_rate**j, 1])]
  237. # Add downsampling layers
  238. model += [nn.ELU()]
  239. model += [EncodecConv1d(config, current_scale, current_scale * 2, kernel_size=ratio * 2, stride=ratio)]
  240. scaling *= 2
  241. model += [EncodecLSTM(config, scaling * config.num_filters)]
  242. model += [nn.ELU()]
  243. model += [EncodecConv1d(config, scaling * config.num_filters, config.hidden_size, config.last_kernel_size)]
  244. self.layers = nn.ModuleList(model)
  245. def forward(self, hidden_states):
  246. for layer in self.layers:
  247. hidden_states = layer(hidden_states)
  248. return hidden_states
  249. class EncodecDecoder(nn.Module):
  250. """SEANet decoder as used by EnCodec."""
  251. def __init__(self, config: EncodecConfig):
  252. super().__init__()
  253. scaling = int(2 ** len(config.upsampling_ratios))
  254. model = [EncodecConv1d(config, config.hidden_size, scaling * config.num_filters, config.kernel_size)]
  255. model += [EncodecLSTM(config, scaling * config.num_filters)]
  256. # Upsample to raw audio scale
  257. for ratio in config.upsampling_ratios:
  258. current_scale = scaling * config.num_filters
  259. # Add upsampling layers
  260. model += [nn.ELU()]
  261. model += [
  262. EncodecConvTranspose1d(config, current_scale, current_scale // 2, kernel_size=ratio * 2, stride=ratio)
  263. ]
  264. # Add residual layers
  265. for j in range(config.num_residual_layers):
  266. model += [EncodecResnetBlock(config, current_scale // 2, (config.dilation_growth_rate**j, 1))]
  267. scaling //= 2
  268. # Add final layers
  269. model += [nn.ELU()]
  270. model += [EncodecConv1d(config, config.num_filters, config.audio_channels, config.last_kernel_size)]
  271. self.layers = nn.ModuleList(model)
  272. def forward(self, hidden_states):
  273. for layer in self.layers:
  274. hidden_states = layer(hidden_states)
  275. return hidden_states
  276. class EncodecEuclideanCodebook(nn.Module):
  277. """Codebook with Euclidean distance."""
  278. def __init__(self, config: EncodecConfig):
  279. super().__init__()
  280. embed = torch.zeros(config.codebook_size, config.codebook_dim)
  281. self.codebook_size = config.codebook_size
  282. self.register_buffer("inited", torch.Tensor([True]))
  283. self.register_buffer("cluster_size", torch.zeros(config.codebook_size))
  284. self.register_buffer("embed", embed)
  285. self.register_buffer("embed_avg", embed.clone())
  286. def quantize(self, hidden_states):
  287. embed = self.embed.t()
  288. scaled_states = hidden_states.pow(2).sum(1, keepdim=True)
  289. dist = -(scaled_states - 2 * hidden_states @ embed + embed.pow(2).sum(0, keepdim=True))
  290. embed_ind = dist.max(dim=-1).indices
  291. return embed_ind
  292. def encode(self, hidden_states):
  293. shape = hidden_states.shape
  294. # pre-process
  295. hidden_states = hidden_states.reshape((-1, shape[-1]))
  296. # quantize
  297. embed_ind = self.quantize(hidden_states)
  298. # post-process
  299. embed_ind = embed_ind.view(*shape[:-1])
  300. return embed_ind
  301. def decode(self, embed_ind):
  302. quantize = nn.functional.embedding(embed_ind, self.embed)
  303. return quantize
  304. class EncodecVectorQuantization(nn.Module):
  305. """
  306. Vector quantization implementation. Currently supports only euclidean distance.
  307. """
  308. def __init__(self, config: EncodecConfig):
  309. super().__init__()
  310. self.codebook = EncodecEuclideanCodebook(config)
  311. def encode(self, hidden_states):
  312. hidden_states = hidden_states.permute(0, 2, 1)
  313. embed_in = self.codebook.encode(hidden_states)
  314. return embed_in
  315. def decode(self, embed_ind):
  316. quantize = self.codebook.decode(embed_ind)
  317. quantize = quantize.permute(0, 2, 1)
  318. return quantize
  319. class EncodecResidualVectorQuantizer(nn.Module):
  320. """Residual Vector Quantizer."""
  321. def __init__(self, config: EncodecConfig):
  322. super().__init__()
  323. self.codebook_size = config.codebook_size
  324. self.frame_rate = config.frame_rate
  325. self.num_quantizers = config.num_quantizers
  326. self.layers = nn.ModuleList([EncodecVectorQuantization(config) for _ in range(config.num_quantizers)])
  327. def get_num_quantizers_for_bandwidth(self, bandwidth: Optional[float] = None) -> int:
  328. """Return num_quantizers based on specified target bandwidth."""
  329. bw_per_q = math.log2(self.codebook_size) * self.frame_rate
  330. num_quantizers = self.num_quantizers
  331. if bandwidth is not None and bandwidth > 0.0:
  332. num_quantizers = int(max(1, math.floor(bandwidth * 1000 / bw_per_q)))
  333. return num_quantizers
  334. def encode(self, embeddings: torch.Tensor, bandwidth: Optional[float] = None) -> torch.Tensor:
  335. """
  336. Encode a given input tensor with the specified frame rate at the given bandwidth. The RVQ encode method sets
  337. the appropriate number of quantizers to use and returns indices for each quantizer.
  338. """
  339. num_quantizers = self.get_num_quantizers_for_bandwidth(bandwidth)
  340. residual = embeddings
  341. all_indices = []
  342. for layer in self.layers[:num_quantizers]:
  343. indices = layer.encode(residual)
  344. quantized = layer.decode(indices)
  345. residual = residual - quantized
  346. all_indices.append(indices)
  347. out_indices = torch.stack(all_indices)
  348. return out_indices
  349. def decode(self, codes: torch.Tensor) -> torch.Tensor:
  350. """Decode the given codes to the quantized representation."""
  351. quantized_out = torch.tensor(0.0, device=codes.device)
  352. for i, indices in enumerate(codes):
  353. layer = self.layers[i]
  354. quantized = layer.decode(indices)
  355. quantized_out = quantized_out + quantized
  356. return quantized_out
  357. @auto_docstring
  358. class EncodecPreTrainedModel(PreTrainedAudioTokenizerBase):
  359. config: EncodecConfig
  360. base_model_prefix = "encodec"
  361. main_input_name = "input_values"
  362. def _init_weights(self, module):
  363. """Initialize the weights"""
  364. if isinstance(module, nn.GroupNorm):
  365. module.bias.data.zero_()
  366. module.weight.data.fill_(1.0)
  367. elif isinstance(module, nn.Conv1d):
  368. nn.init.kaiming_normal_(module.weight)
  369. if module.bias is not None:
  370. k = math.sqrt(module.groups / (module.in_channels * module.kernel_size[0]))
  371. nn.init.uniform_(module.bias, a=-k, b=k)
  372. elif isinstance(module, nn.ConvTranspose1d):
  373. module.reset_parameters()
  374. elif isinstance(module, nn.LSTM):
  375. for name, param in module.named_parameters():
  376. if "weight" in name:
  377. nn.init.xavier_uniform_(param)
  378. elif "bias" in name:
  379. nn.init.constant_(param, 0.0)
  380. @auto_docstring(
  381. custom_intro="""
  382. The EnCodec neural audio codec model.
  383. """
  384. )
  385. class EncodecModel(EncodecPreTrainedModel):
  386. def __init__(self, config: EncodecConfig):
  387. super().__init__(config)
  388. self.config = config
  389. self.encoder = EncodecEncoder(config)
  390. self.decoder = EncodecDecoder(config)
  391. self.quantizer = EncodecResidualVectorQuantizer(config)
  392. self.bits_per_codebook = int(math.log2(self.config.codebook_size))
  393. if 2**self.bits_per_codebook != self.config.codebook_size:
  394. raise ValueError("The codebook_size must be a power of 2.")
  395. # Initialize weights and apply final processing
  396. self.post_init()
  397. def get_encoder(self):
  398. return self.encoder
  399. def _encode_frame(
  400. self, input_values: torch.Tensor, bandwidth: float
  401. ) -> tuple[torch.Tensor, Optional[torch.Tensor]]:
  402. """
  403. Encodes the given input using the underlying VQVAE. If `config.normalize` is set to `True` the input is first
  404. normalized. The padding mask is required to compute the correct scale.
  405. """
  406. length = input_values.shape[-1]
  407. duration = length / self.config.sampling_rate
  408. if self.config.chunk_length_s is not None and duration > 1e-5 + self.config.chunk_length_s:
  409. raise RuntimeError(f"Duration of frame ({duration}) is longer than chunk {self.config.chunk_length_s}")
  410. scale = None
  411. if self.config.normalize:
  412. mono = torch.sum(input_values, 1, keepdim=True) / input_values.shape[1]
  413. scale = mono.pow(2).mean(dim=-1, keepdim=True).sqrt() + 1e-8
  414. input_values = input_values / scale
  415. scale = scale.view(-1, 1)
  416. embeddings = self.encoder(input_values)
  417. codes = self.quantizer.encode(embeddings, bandwidth)
  418. codes = codes.transpose(0, 1)
  419. return codes, scale
  420. def encode(
  421. self,
  422. input_values: torch.Tensor,
  423. padding_mask: Optional[torch.Tensor] = None,
  424. bandwidth: Optional[float] = None,
  425. return_dict: Optional[bool] = None,
  426. ) -> Union[tuple[torch.Tensor, Optional[torch.Tensor], int], EncodecEncoderOutput]:
  427. """
  428. Encodes the input audio waveform into discrete codes of shape
  429. `(nb_frames, batch_size, nb_quantizers, frame_len)`.
  430. - `nb_frames=1` if `self.config.chunk_length=None` (as the encoder is applied on the full audio), which is the
  431. case for the 24kHz model. Otherwise, `nb_frames=ceil(input_length/self.config.chunk_stride)`, which is the case
  432. for the 48kHz model.
  433. - `frame_len` is the length of each frame, which is equal to `ceil(input_length/self.config.hop_length)` if
  434. `self.config.chunk_length=None` (e.g., for the 24kHz model). Otherwise, if `self.config.chunk_length` is
  435. defined, `frame_len=self.config.chunk_length/self.config.hop_length`, e.g., the case for the 48kHz model with
  436. `frame_len=150`.
  437. Args:
  438. input_values (`torch.Tensor` of shape `(batch_size, channels, sequence_length)`):
  439. Float values of the input audio waveform.
  440. padding_mask (`torch.Tensor` of shape `(batch_size, channels, sequence_length)`):
  441. Padding mask used to pad the `input_values`.
  442. bandwidth (`float`, *optional*):
  443. The target bandwidth. Must be one of `config.target_bandwidths`. If `None`, uses the smallest possible
  444. bandwidth. bandwidth is represented as a thousandth of what it is, e.g. 6kbps bandwidth is represented
  445. as bandwidth == 6.0
  446. Returns:
  447. EncodecEncoderOutput dict or a tuple containing:
  448. - audio_codes (`torch.LongTensor` of shape `(nb_frames, batch_size, nb_quantizers, frame_len)`, *optional*),
  449. - audio_scales (list of length `nb_frames` of `torch.Tensor` of shape `(batch_size, 1)`, *optional*),
  450. - last_frame_pad_length (`int`, *optional*).
  451. """
  452. return_dict = return_dict if return_dict is not None else self.config.return_dict
  453. if bandwidth is None:
  454. bandwidth = self.config.target_bandwidths[0]
  455. if bandwidth not in self.config.target_bandwidths:
  456. raise ValueError(
  457. f"This model doesn't support the bandwidth {bandwidth}. Select one of {self.config.target_bandwidths}."
  458. )
  459. _, channels, input_length = input_values.shape
  460. if channels < 1 or channels > 2:
  461. raise ValueError(f"Number of audio channels must be 1 or 2, but got {channels}")
  462. chunk_length = self.config.chunk_length
  463. if chunk_length is None:
  464. chunk_length = input_length
  465. stride = input_length
  466. else:
  467. stride = self.config.chunk_stride
  468. if padding_mask is None:
  469. padding_mask = torch.ones_like(input_values).bool()
  470. else:
  471. padding_mask = padding_mask.view(padding_mask.shape[0], -1, padding_mask.shape[-1])
  472. encoded_frames = []
  473. scales = []
  474. for offset in range(0, input_length, stride):
  475. mask = padding_mask[..., offset : offset + chunk_length].bool()
  476. frame = mask * input_values[..., offset : offset + chunk_length]
  477. encoded_frame, scale = self._encode_frame(frame, bandwidth)
  478. encoded_frames.append(encoded_frame)
  479. scales.append(scale)
  480. # pad last frame (if necessary) to be able to apply `torch.stack`
  481. last_frame_pad_length = encoded_frames[0].shape[-1] - encoded_frames[-1].shape[-1]
  482. if last_frame_pad_length > 0:
  483. last_frame = nn.functional.pad(encoded_frames[-1], (0, last_frame_pad_length), value=0)
  484. encoded_frames[-1] = last_frame
  485. encoded_frames = torch.stack(encoded_frames)
  486. if not return_dict:
  487. return (encoded_frames, scales, last_frame_pad_length)
  488. return EncodecEncoderOutput(encoded_frames, scales, last_frame_pad_length)
  489. @staticmethod
  490. def _linear_overlap_add(frames: list[torch.Tensor], stride: int):
  491. # Generic overlap add, with linear fade-in/fade-out, supporting complex scenario
  492. # e.g., more than 2 frames per position.
  493. # The core idea is to use a weight function that is a triangle,
  494. # with a maximum value at the middle of the chunk.
  495. # We use this weighting when summing the frames, and divide by the sum of weights
  496. # for each positions at the end. Thus:
  497. # - if a frame is the only one to cover a position, the weighting is a no-op.
  498. # - if 2 frames cover a position:
  499. # ... ...
  500. # / \/ \
  501. # / /\ \
  502. # S T , i.e. S offset of second frame starts, T end of first frame.
  503. # Then the weight function for each one is: (t - S), (T - t), with `t` a given offset.
  504. # After the final normalization, the weight of the second frame at position `t` is
  505. # (t - S) / (t - S + (T - t)) = (t - S) / (T - S), which is exactly what we want.
  506. #
  507. # - if more than 2 frames overlap at a given point, we hope that by induction
  508. # something sensible happens.
  509. if len(frames) == 0:
  510. raise ValueError("`frames` cannot be an empty list.")
  511. device = frames[0].device
  512. dtype = frames[0].dtype
  513. shape = frames[0].shape[:-1]
  514. total_size = stride * (len(frames) - 1) + frames[-1].shape[-1]
  515. frame_length = frames[0].shape[-1]
  516. time_vec = torch.linspace(0, 1, frame_length + 2, device=device, dtype=dtype)[1:-1]
  517. weight = 0.5 - (time_vec - 0.5).abs()
  518. sum_weight = torch.zeros(total_size, device=device, dtype=dtype)
  519. out = torch.zeros(*shape, total_size, device=device, dtype=dtype)
  520. offset: int = 0
  521. for frame in frames:
  522. frame_length = frame.shape[-1]
  523. out[..., offset : offset + frame_length] += weight[:frame_length] * frame
  524. sum_weight[offset : offset + frame_length] += weight[:frame_length]
  525. offset += stride
  526. if sum_weight.min() == 0:
  527. raise ValueError(f"`sum_weight` minimum element must be bigger than zero: {sum_weight}`")
  528. return out / sum_weight
  529. def _decode_frame(self, codes: torch.Tensor, scale: Optional[torch.Tensor] = None) -> torch.Tensor:
  530. codes = codes.transpose(0, 1)
  531. embeddings = self.quantizer.decode(codes)
  532. outputs = self.decoder(embeddings)
  533. if scale is not None:
  534. outputs = outputs * scale.view(-1, 1, 1)
  535. return outputs
  536. def decode(
  537. self,
  538. audio_codes: torch.LongTensor,
  539. audio_scales: torch.Tensor,
  540. padding_mask: Optional[torch.Tensor] = None,
  541. return_dict: Optional[bool] = None,
  542. last_frame_pad_length: Optional[int] = 0,
  543. ) -> Union[tuple[torch.Tensor, torch.Tensor], EncodecDecoderOutput]:
  544. """
  545. Decodes the given frames into an output audio waveform.
  546. Note that the output might be a bit bigger than the input. In that case, any extra steps at the end can be
  547. trimmed.
  548. Args:
  549. audio_codes (`torch.LongTensor` of shape `(nb_frames, batch_size, nb_quantizers, frame_len)`, *optional*):
  550. Discrete code embeddings computed using `model.encode`.
  551. audio_scales (list of length `nb_frames` of `torch.Tensor` of shape `(batch_size, 1)`, *optional*):
  552. Scaling factor for each `audio_codes` input.
  553. padding_mask (`torch.Tensor` of shape `(channels, sequence_length)`):
  554. Padding mask used to pad the `input_values`.
  555. return_dict (`bool`, *optional*):
  556. Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
  557. last_frame_pad_length (`int`, *optional*):
  558. Integer representing the length of the padding in the last frame, which is removed during decoding.
  559. """
  560. return_dict = return_dict if return_dict is not None else self.config.return_dict
  561. chunk_length = self.config.chunk_length
  562. if chunk_length is None:
  563. if len(audio_codes) != 1:
  564. raise ValueError(f"Expected one frame, got {len(audio_codes)}")
  565. frame = audio_codes[0]
  566. if last_frame_pad_length > 0:
  567. frame = frame[..., :-last_frame_pad_length]
  568. audio_values = self._decode_frame(frame, audio_scales[0])
  569. else:
  570. decoded_frames = []
  571. for i, (frame, scale) in enumerate(zip(audio_codes, audio_scales)):
  572. if i == len(audio_codes) - 1 and last_frame_pad_length > 0:
  573. frame = frame[..., :-last_frame_pad_length]
  574. frames = self._decode_frame(frame, scale)
  575. decoded_frames.append(frames)
  576. audio_values = self._linear_overlap_add(decoded_frames, self.config.chunk_stride or 1)
  577. # truncate based on padding mask
  578. if padding_mask is not None and padding_mask.shape[-1] < audio_values.shape[-1]:
  579. audio_values = audio_values[..., : padding_mask.shape[-1]]
  580. if not return_dict:
  581. return (audio_values,)
  582. return EncodecDecoderOutput(audio_values)
  583. @auto_docstring
  584. def forward(
  585. self,
  586. input_values: torch.FloatTensor,
  587. padding_mask: Optional[torch.BoolTensor] = None,
  588. bandwidth: Optional[float] = None,
  589. audio_codes: Optional[torch.LongTensor] = None,
  590. audio_scales: Optional[torch.Tensor] = None,
  591. return_dict: Optional[bool] = None,
  592. last_frame_pad_length: Optional[int] = 0,
  593. ) -> Union[tuple[torch.Tensor, torch.Tensor], EncodecOutput]:
  594. r"""
  595. input_values (`torch.FloatTensor` of shape `(batch_size, channels, sequence_length)`, *optional*):
  596. Raw audio input converted to Float and padded to the appropriate length in order to be encoded using chunks
  597. of length self.chunk_length and a stride of `config.chunk_stride`.
  598. padding_mask (`torch.BoolTensor` of shape `(batch_size, channels, sequence_length)`, *optional*):
  599. Mask to avoid computing scaling factors on padding token indices (can we avoid computing conv on these+).
  600. Mask values selected in `[0, 1]`:
  601. - 1 for tokens that are **not masked**,
  602. - 0 for tokens that are **masked**.
  603. <Tip warning={true}>
  604. `padding_mask` should always be passed, unless the input was truncated or not padded. This is because in
  605. order to process tensors effectively, the input audio should be padded so that `input_length % stride =
  606. step` with `step = chunk_length-stride`. This ensures that all chunks are of the same shape
  607. </Tip>
  608. bandwidth (`float`, *optional*):
  609. The target bandwidth. Must be one of `config.target_bandwidths`. If `None`, uses the smallest possible
  610. bandwidth. bandwidth is represented as a thousandth of what it is, e.g. 6kbps bandwidth is represented as
  611. `bandwidth == 6.0`
  612. audio_codes (`torch.LongTensor` of shape `(nb_frames, batch_size, nb_quantizers, frame_len)`, *optional*):
  613. Discrete code embeddings computed using `model.encode`.
  614. audio_scales (list of length `nb_frames` of `torch.Tensor` of shape `(batch_size, 1)`, *optional*):
  615. Scaling factor for each `audio_codes` input.
  616. return_dict (`bool`, *optional*):
  617. Whether to return outputs as a dict.
  618. last_frame_pad_length (`int`, *optional*):
  619. The length of the padding in the last frame, if any. This is used to ensure that the encoded frames can be
  620. outputted as a tensor. This value should be passed during decoding to ensure padding is removed from the
  621. encoded frames.
  622. Examples:
  623. ```python
  624. >>> from datasets import load_dataset
  625. >>> from transformers import AutoProcessor, EncodecModel
  626. >>> dataset = load_dataset("hf-internal-testing/ashraq-esc50-1-dog-example")
  627. >>> audio_sample = dataset["train"]["audio"][0]["array"]
  628. >>> model_id = "facebook/encodec_24khz"
  629. >>> model = EncodecModel.from_pretrained(model_id)
  630. >>> processor = AutoProcessor.from_pretrained(model_id)
  631. >>> inputs = processor(raw_audio=audio_sample, return_tensors="pt")
  632. >>> outputs = model(**inputs)
  633. >>> audio_codes = outputs.audio_codes
  634. >>> audio_values = outputs.audio_values
  635. ```"""
  636. return_dict = return_dict if return_dict is not None else self.config.return_dict
  637. if padding_mask is None:
  638. padding_mask = torch.ones_like(input_values).bool()
  639. else:
  640. # ensure that channel dimension is present
  641. padding_mask = padding_mask.view(padding_mask.shape[0], -1, padding_mask.shape[-1])
  642. if audio_codes is not None and audio_scales is None:
  643. raise ValueError("You specified `audio_codes` but did not specify the `audio_scales`")
  644. if audio_scales is not None and audio_codes is None:
  645. raise ValueError("You specified `audio_scales` but did not specify the `audio_codes`")
  646. if audio_scales is None and audio_codes is None:
  647. audio_codes, audio_scales, last_frame_pad_length = self.encode(
  648. input_values, padding_mask, bandwidth, False
  649. )
  650. audio_values = self.decode(
  651. audio_codes,
  652. audio_scales,
  653. padding_mask,
  654. return_dict=return_dict,
  655. last_frame_pad_length=last_frame_pad_length,
  656. )[0]
  657. if not return_dict:
  658. return (audio_codes, audio_values)
  659. return EncodecOutput(audio_codes=audio_codes, audio_values=audio_values)
  660. __all__ = ["EncodecModel", "EncodecPreTrainedModel"]