






import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer
from ...utils import logging


__A					=	logging.get_logger(__name__)

__A					=	"▁"

__A					=	{"vocab_file": "sentencepiece.bpe.model"}

__A					=	{
    "vocab_file": {
        "facebook/nllb-200-distilled-600M": (
            "https://huggingface.co/facebook/nllb-200-distilled-600M/blob/main/sentencepiece.bpe.model"
        ),
    }
}

__A					=	{
    "facebook/nllb-200-distilled-600M": 10_24,
}

# fmt: off
__A					=	["ace_Arab", "ace_Latn", "acm_Arab", "acq_Arab", "aeb_Arab", "afr_Latn", "ajp_Arab", "aka_Latn", "amh_Ethi", "apc_Arab", "arb_Arab", "ars_Arab", "ary_Arab", "arz_Arab", "asm_Beng", "ast_Latn", "awa_Deva", "ayr_Latn", "azb_Arab", "azj_Latn", "bak_Cyrl", "bam_Latn", "ban_Latn", "bel_Cyrl", "bem_Latn", "ben_Beng", "bho_Deva", "bjn_Arab", "bjn_Latn", "bod_Tibt", "bos_Latn", "bug_Latn", "bul_Cyrl", "cat_Latn", "ceb_Latn", "ces_Latn", "cjk_Latn", "ckb_Arab", "crh_Latn", "cym_Latn", "dan_Latn", "deu_Latn", "dik_Latn", "dyu_Latn", "dzo_Tibt", "ell_Grek", "eng_Latn", "epo_Latn", "est_Latn", "eus_Latn", "ewe_Latn", "fao_Latn", "pes_Arab", "fij_Latn", "fin_Latn", "fon_Latn", "fra_Latn", "fur_Latn", "fuv_Latn", "gla_Latn", "gle_Latn", "glg_Latn", "grn_Latn", "guj_Gujr", "hat_Latn", "hau_Latn", "heb_Hebr", "hin_Deva", "hne_Deva", "hrv_Latn", "hun_Latn", "hye_Armn", "ibo_Latn", "ilo_Latn", "ind_Latn", "isl_Latn", "ita_Latn", "jav_Latn", "jpn_Jpan", "kab_Latn", "kac_Latn", "kam_Latn", "kan_Knda", "kas_Arab", "kas_Deva", "kat_Geor", "knc_Arab", "knc_Latn", "kaz_Cyrl", "kbp_Latn", "kea_Latn", "khm_Khmr", "kik_Latn", "kin_Latn", "kir_Cyrl", "kmb_Latn", "kon_Latn", "kor_Hang", "kmr_Latn", "lao_Laoo", "lvs_Latn", "lij_Latn", "lim_Latn", "lin_Latn", "lit_Latn", "lmo_Latn", "ltg_Latn", "ltz_Latn", "lua_Latn", "lug_Latn", "luo_Latn", "lus_Latn", "mag_Deva", "mai_Deva", "mal_Mlym", "mar_Deva", "min_Latn", "mkd_Cyrl", "plt_Latn", "mlt_Latn", "mni_Beng", "khk_Cyrl", "mos_Latn", "mri_Latn", "zsm_Latn", "mya_Mymr", "nld_Latn", "nno_Latn", "nob_Latn", "npi_Deva", "nso_Latn", "nus_Latn", "nya_Latn", "oci_Latn", "gaz_Latn", "ory_Orya", "pag_Latn", "pan_Guru", "pap_Latn", "pol_Latn", "por_Latn", "prs_Arab", "pbt_Arab", "quy_Latn", "ron_Latn", "run_Latn", "rus_Cyrl", "sag_Latn", "san_Deva", "sat_Beng", "scn_Latn", "shn_Mymr", "sin_Sinh", "slk_Latn", "slv_Latn", "smo_Latn", "sna_Latn", "snd_Arab", "som_Latn", "sot_Latn", "spa_Latn", "als_Latn", "srd_Latn", "srp_Cyrl", "ssw_Latn", "sun_Latn", "swe_Latn", "swh_Latn", "szl_Latn", "tam_Taml", "tat_Cyrl", "tel_Telu", "tgk_Cyrl", "tgl_Latn", "tha_Thai", "tir_Ethi", "taq_Latn", "taq_Tfng", "tpi_Latn", "tsn_Latn", "tso_Latn", "tuk_Latn", "tum_Latn", "tur_Latn", "twi_Latn", "tzm_Tfng", "uig_Arab", "ukr_Cyrl", "umb_Latn", "urd_Arab", "uzn_Latn", "vec_Latn", "vie_Latn", "war_Latn", "wol_Latn", "xho_Latn", "ydd_Hebr", "yor_Latn", "yue_Hant", "zho_Hans", "zho_Hant", "zul_Latn"]



class 				_A			(     UpperCamelCase				):
    """simple docstring"""

    lowerCamelCase	:							Tuple				= VOCAB_FILES_NAMES
    lowerCamelCase	:							List[str]				= PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    lowerCamelCase	:							Optional[Any]				= PRETRAINED_VOCAB_FILES_MAP
    lowerCamelCase	:							List[Any]				= ['input_ids', 'attention_mask']

    lowerCamelCase	:							List[int]				= []
    lowerCamelCase	:							List[int]				= []



    def __init__(				self      :     Any ,     __SCREAMING_SNAKE_CASE      :     int ,     __SCREAMING_SNAKE_CASE      :     Any="<s>" ,     __SCREAMING_SNAKE_CASE      :     Tuple="</s>" ,     __SCREAMING_SNAKE_CASE      :     List[Any]="</s>" ,     __SCREAMING_SNAKE_CASE      :     Optional[int]="<s>" ,     __SCREAMING_SNAKE_CASE      :     Dict="<unk>" ,     __SCREAMING_SNAKE_CASE      :     Optional[int]="<pad>" ,     __SCREAMING_SNAKE_CASE      :     Any="<mask>" ,     __SCREAMING_SNAKE_CASE      :     int=None ,     __SCREAMING_SNAKE_CASE      :     Union[str, Any]=None ,     __SCREAMING_SNAKE_CASE      :     Optional[Any]=None ,     __SCREAMING_SNAKE_CASE      :     Optional[Dict[str, Any]] = None ,     __SCREAMING_SNAKE_CASE      :     Tuple=None ,     __SCREAMING_SNAKE_CASE      :     Optional[int]=False ,     **__SCREAMING_SNAKE_CASE      :     Any ,     )      ->							Optional[Any]:
           # Mask token behave like a normal word, i.e. include the space before it
           __UpperCAmelCase     				=AddedToken(__SCREAMING_SNAKE_CASE ,     lstrip=__SCREAMING_SNAKE_CASE ,     rstrip=__SCREAMING_SNAKE_CASE      ) if isinstance(__SCREAMING_SNAKE_CASE ,     __SCREAMING_SNAKE_CASE      ) else mask_token

           __UpperCAmelCase     				={} if sp_model_kwargs is None else sp_model_kwargs
           __UpperCAmelCase     				=legacy_behaviour

           super().__init__(
               bos_token=__SCREAMING_SNAKE_CASE ,     eos_token=__SCREAMING_SNAKE_CASE ,     unk_token=__SCREAMING_SNAKE_CASE ,     sep_token=__SCREAMING_SNAKE_CASE ,     cls_token=__SCREAMING_SNAKE_CASE ,     pad_token=__SCREAMING_SNAKE_CASE ,     mask_token=__SCREAMING_SNAKE_CASE ,     tokenizer_file=__SCREAMING_SNAKE_CASE ,     src_lang=__SCREAMING_SNAKE_CASE ,     tgt_lang=__SCREAMING_SNAKE_CASE ,     additional_special_tokens=__SCREAMING_SNAKE_CASE ,     sp_model_kwargs=self.sp_model_kwargs ,     legacy_behaviour=__SCREAMING_SNAKE_CASE ,     **__SCREAMING_SNAKE_CASE ,     )

           __UpperCAmelCase     				=spm.SentencePieceProcessor(**self.sp_model_kwargs      )
           self.sp_model.Load(str(__SCREAMING_SNAKE_CASE      )      )
           __UpperCAmelCase     				=vocab_file
           # Original fairseq vocab and spm vocab must be "aligned":
           # Vocab    |    0    |    1    |   2    |    3    |  4   |  5   |  6   |   7  |   8  |  9
           # -------- | ------- | ------- | ------ | ------- | ---- | ---- | ---- | ---- | ---- | ----
           # fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | 'an' | '▁n' | '▁m' | '▁t' | '▁k' | '▁a'
           # spm      | '<unk>' | '<s>'   | '</s>' | 'an'    | '▁n' | '▁m' | '▁t' | '▁k' | '▁a' | '▁s'

           # Mimic fairseq token-to-id alignment for the first 4 token
           __UpperCAmelCase     				={"""<s>""": 0, """<pad>""": 1, """</s>""": 2, """<unk>""": 3}

           # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
           __UpperCAmelCase     				=1

           __UpperCAmelCase     				=len(self.sp_model      )
           __UpperCAmelCase     				={
               code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(__SCREAMING_SNAKE_CASE      )
           }
           __UpperCAmelCase     				={v: k for k, v in self.lang_code_to_id.items()}
           __UpperCAmelCase     				=len(self.sp_model      ) + len(self.lang_code_to_id      ) + self.fairseq_offset

           self.fairseq_tokens_to_ids.update(self.lang_code_to_id      )
           __UpperCAmelCase     				={v: k for k, v in self.fairseq_tokens_to_ids.items()}
           __UpperCAmelCase     				=list(self.lang_code_to_id.keys()      )

           if additional_special_tokens is not None:
                  # Only add those special tokens if they are not already there.
                  self._additional_special_tokens.extend(
                      [t for t in additional_special_tokens if t not in self._additional_special_tokens]      )

           __UpperCAmelCase     				=src_lang if src_lang is not None else """eng_Latn"""
           __UpperCAmelCase     				=self.lang_code_to_id[self._src_lang]
           __UpperCAmelCase     				=tgt_lang
           self.set_src_lang_special_tokens(self._src_lang      )



    def __getstate__(				self      :     Tuple      )      ->							Optional[Any]:
           __UpperCAmelCase     				=self.__dict__.copy()
           __UpperCAmelCase     				=None
           __UpperCAmelCase     				=self.sp_model.serialized_model_proto()
           return state



    def __setstate__(				self      :     Dict ,     __SCREAMING_SNAKE_CASE      :     Dict      )      ->							Tuple:
           __UpperCAmelCase     				=d

           # for backward compatibility
           if not hasattr(self ,     """sp_model_kwargs"""      ):
                  __UpperCAmelCase     				={}

           __UpperCAmelCase     				=spm.SentencePieceProcessor(**self.sp_model_kwargs      )
           self.sp_model.LoadFromSerializedProto(self.sp_model_proto      )



    @property
    def _a							(				self      :     Any      )      ->							int:
           return len(self.sp_model      ) + len(self.lang_code_to_id      ) + self.fairseq_offset + 1  # Plus 1 for the mask token



    @property
    def _a							(				self      :     str      )      ->							str:
           return self._src_lang



    @src_lang.setter
    def _a							(				self      :     Any ,     __SCREAMING_SNAKE_CASE      :     str      )      ->							None:
           __UpperCAmelCase     				=new_src_lang
           self.set_src_lang_special_tokens(self._src_lang      )



    def _a							(				self      :     Any ,     __SCREAMING_SNAKE_CASE      :     List[int] ,     __SCREAMING_SNAKE_CASE      :     Optional[List[int]] = None ,     __SCREAMING_SNAKE_CASE      :     bool = False      )      ->							List[int]:

           if already_has_special_tokens:
                  return super().get_special_tokens_mask(
                      token_ids_a=__SCREAMING_SNAKE_CASE ,     token_ids_a=__SCREAMING_SNAKE_CASE ,     already_has_special_tokens=__SCREAMING_SNAKE_CASE      )

           __UpperCAmelCase     				=[1] * len(self.prefix_tokens      )
           __UpperCAmelCase     				=[1] * len(self.suffix_tokens      )
           if token_ids_a is None:
                  return prefix_ones + ([0] * len(__SCREAMING_SNAKE_CASE      )) + suffix_ones
           return prefix_ones + ([0] * len(__SCREAMING_SNAKE_CASE      )) + ([0] * len(__SCREAMING_SNAKE_CASE      )) + suffix_ones



    def _a							(				self      :     Tuple ,     __SCREAMING_SNAKE_CASE      :     List[int] ,     __SCREAMING_SNAKE_CASE      :     Optional[List[int]] = None      )      ->							List[int]:
           if token_ids_a is None:
                  return self.prefix_tokens + token_ids_a + self.suffix_tokens
           # We don't expect to process pairs, but leave the pair logic for API consistency
           return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens



    def _a							(				self      :     List[str] ,     __SCREAMING_SNAKE_CASE      :     List[int] ,     __SCREAMING_SNAKE_CASE      :     Optional[List[int]] = None      )      ->							List[int]:
           __UpperCAmelCase     				=[self.sep_token_id]
           __UpperCAmelCase     				=[self.cls_token_id]

           if token_ids_a is None:
                  return len(cls + token_ids_a + sep      ) * [0]
           return len(cls + token_ids_a + sep + sep + token_ids_a + sep      ) * [0]



    def _a							(				self      :     Tuple ,     __SCREAMING_SNAKE_CASE      :     Optional[int] ,     __SCREAMING_SNAKE_CASE      :     str ,     __SCREAMING_SNAKE_CASE      :     Optional[str] ,     __SCREAMING_SNAKE_CASE      :     Optional[str] ,     **__SCREAMING_SNAKE_CASE      :     Tuple      )      ->							Dict:
           if src_lang is None or tgt_lang is None:
                  raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model"""      )
           __UpperCAmelCase     				=src_lang
           __UpperCAmelCase     				=self(__SCREAMING_SNAKE_CASE ,     add_special_tokens=__SCREAMING_SNAKE_CASE ,     return_tensors=__SCREAMING_SNAKE_CASE ,     **__SCREAMING_SNAKE_CASE      )
           __UpperCAmelCase     				=self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE      )
           __UpperCAmelCase     				=tgt_lang_id
           return inputs



    def _a							(				self      :     Any      )      ->							List[Any]:
           __UpperCAmelCase     				={self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE      ): i for i in range(self.vocab_size      )}
           vocab.update(self.added_tokens_encoder      )
           return vocab



    def _a							(				self      :     List[str] ,     __SCREAMING_SNAKE_CASE      :     str      )      ->							List[str]:
           return self.sp_model.encode(__SCREAMING_SNAKE_CASE ,     out_type=__SCREAMING_SNAKE_CASE      )



    def _a							(				self      :     Dict ,     __SCREAMING_SNAKE_CASE      :     str      )      ->							int:
           if token in self.fairseq_tokens_to_ids:
                  return self.fairseq_tokens_to_ids[token]
           __UpperCAmelCase     				=self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE      )

           # Need to return unknown token if the SP model returned 0
           return spm_id + self.fairseq_offset if spm_id else self.unk_token_id



    def _a							(				self      :     List[str] ,     __SCREAMING_SNAKE_CASE      :     Union[str, Any]      )      ->							Dict:
           if index in self.fairseq_ids_to_tokens:
                  return self.fairseq_ids_to_tokens[index]
           return self.sp_model.IdToPiece(index - self.fairseq_offset      )



    def _a							(				self      :     str ,     __SCREAMING_SNAKE_CASE      :     Tuple      )      ->							int:
           __UpperCAmelCase     				="""""".join(__SCREAMING_SNAKE_CASE      ).replace(__SCREAMING_SNAKE_CASE ,     """ """      ).strip()
           return out_string



    def _a							(				self      :     int ,     __SCREAMING_SNAKE_CASE      :     str ,     __SCREAMING_SNAKE_CASE      :     Optional[str] = None      )      ->							Tuple[str]:
           if not os.path.isdir(__SCREAMING_SNAKE_CASE      ):
                  logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''      )
                  return
           __UpperCAmelCase     				=os.path.join(
               __SCREAMING_SNAKE_CASE ,     (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]      )

           if os.path.abspath(self.vocab_file      ) != os.path.abspath(__SCREAMING_SNAKE_CASE      ) and os.path.isfile(self.vocab_file      ):
                  copyfile(self.vocab_file ,     __SCREAMING_SNAKE_CASE      )
           elif not os.path.isfile(self.vocab_file      ):
                  with open(__SCREAMING_SNAKE_CASE ,     """wb"""      ) as fi:
                         __UpperCAmelCase     				=self.sp_model.serialized_model_proto()
                         fi.write(__SCREAMING_SNAKE_CASE      )

           return (out_vocab_file,)



    def _a							(				self      :     Optional[Any] ,     __SCREAMING_SNAKE_CASE      :     List[str] ,     __SCREAMING_SNAKE_CASE      :     str = "eng_Latn" ,     __SCREAMING_SNAKE_CASE      :     Optional[List[str]] = None ,     __SCREAMING_SNAKE_CASE      :     str = "fra_Latn" ,     **__SCREAMING_SNAKE_CASE      :     Dict ,     )      ->							BatchEncoding:
           __UpperCAmelCase     				=src_lang
           __UpperCAmelCase     				=tgt_lang
           return super().prepare_seqaseq_batch(__SCREAMING_SNAKE_CASE ,     __SCREAMING_SNAKE_CASE ,     **__SCREAMING_SNAKE_CASE      )



    def _a							(				self      :     Optional[int]      )      ->							Any:
           return self.set_src_lang_special_tokens(self.src_lang      )



    def _a							(				self      :     Any      )      ->							Any:
           return self.set_tgt_lang_special_tokens(self.tgt_lang      )



    def _a							(				self      :     Any ,     __SCREAMING_SNAKE_CASE      :     List[str]      )      ->							None:
           __UpperCAmelCase     				=self.lang_code_to_id[src_lang]
           if self.legacy_behaviour:
                  __UpperCAmelCase     				=[]
                  __UpperCAmelCase     				=[self.eos_token_id, self.cur_lang_code]
           else:
                  __UpperCAmelCase     				=[self.cur_lang_code]
                  __UpperCAmelCase     				=[self.eos_token_id]



    def _a							(				self      :     Optional[int] ,     __SCREAMING_SNAKE_CASE      :     str      )      ->							None:
           __UpperCAmelCase     				=self.lang_code_to_id[lang]
           if self.legacy_behaviour:
                  __UpperCAmelCase     				=[]
                  __UpperCAmelCase     				=[self.eos_token_id, self.cur_lang_code]
           else:
                  __UpperCAmelCase     				=[self.cur_lang_code]
                  __UpperCAmelCase     				=[self.eos_token_id]









'''simple docstring'''





import math
from typing import Any, Callable, List, Optional, Tuple, Union

import numpy as np
import torch

from ...models import TaFilmDecoder
from ...schedulers import DDPMScheduler
from ...utils import is_onnx_available, logging, randn_tensor


if is_onnx_available():
   from ..onnx_utils import OnnxRuntimeModel

from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline
from .continous_encoder import SpectrogramContEncoder
from .notes_encoder import SpectrogramNotesEncoder


_UpperCamelCase											=		logging.get_logger(__name__)  # pylint: disable=invalid-name

_UpperCamelCase											=		256






class    lowerCamelCase_       (							SCREAMING_SNAKE_CASE_	):
  """simple docstring"""




  a_				   =["""melgan"""]




  def __init__(					self :      Dict     ,      _a :      SpectrogramNotesEncoder     ,      _a :      SpectrogramContEncoder     ,      _a :      TaFilmDecoder     ,      _a :      DDPMScheduler     ,      _a :      OnnxRuntimeModel if is_onnx_available() else Any     ,      )   ->   None:
    super().__init__()

    # From MELGAN
    __lowerCamelCase			:			Any       			=					math.log(1e-5  )  # Matches MelGAN training.
    __lowerCamelCase			:			List[Any]       			=					4.0  # Largest value for most examples
    __lowerCamelCase			:			Tuple       			=					128

    self.register_modules(
        notes_encoder=_a     ,      continuous_encoder=_a     ,      decoder=_a     ,      scheduler=_a     ,      melgan=_a     ,      )




  def 		_lowercase	(					self :      Tuple     ,      _a :      int     ,      _a :      List[Any]=(-1.0, 1.0)     ,      _a :      Any=False  )   ->   Dict:
    __lowerCamelCase			,__lowerCamelCase			:			Any       			=					output_range
    if clip:
      __lowerCamelCase			:			List[Any]       			=					torch.clip(_a     ,      self.min_value     ,      self.max_value  )
    # Scale to [0, 1].
    __lowerCamelCase			:			Union[str, Any]       			=					(features - self.min_value) / (self.max_value - self.min_value)
    # Scale to [min_out, max_out].
    return zero_one * (max_out - min_out) + min_out




  def 		_lowercase	(					self :      Dict     ,      _a :      List[str]     ,      _a :      int=(-1.0, 1.0)     ,      _a :      Dict=False  )   ->   List[str]:
    __lowerCamelCase			,__lowerCamelCase			:			List[Any]       			=					input_range
    __lowerCamelCase			:			Optional[Any]       			=					torch.clip(_a     ,      _a     ,      _a  ) if clip else outputs
    # Scale to [0, 1].
    __lowerCamelCase			:			str       			=					(outputs - min_out) / (max_out - min_out)
    # Scale to [self.min_value, self.max_value].
    return zero_one * (self.max_value - self.min_value) + self.min_value




  def 		_lowercase	(					self :      int     ,      _a :      Dict     ,      _a :      List[str]     ,      _a :      Tuple  )   ->   Any:
    __lowerCamelCase			:			Tuple       			=					input_tokens > 0
    __lowerCamelCase			,__lowerCamelCase			:			int       			=					self.notes_encoder(
        encoder_input_tokens=_a     ,      encoder_inputs_mask=_a  )

    __lowerCamelCase			,__lowerCamelCase			:			Tuple       			=					self.continuous_encoder(
        encoder_inputs=_a     ,      encoder_inputs_mask=_a  )

    return [(tokens_encoded, tokens_mask), (continuous_encoded, continuous_mask)]




  def 		_lowercase	(					self :      Tuple     ,      _a :      Tuple     ,      _a :      List[Any]     ,      _a :      int  )   ->   Dict:
    __lowerCamelCase			:			Any       			=					noise_time
    if not torch.is_tensor(_a  ):
      __lowerCamelCase			:			Union[str, Any]       			=					torch.tensor([timesteps]     ,      dtype=torch.long     ,      device=input_tokens.device  )
    elif torch.is_tensor(_a  ) and len(timesteps.shape  ) == 0:
      __lowerCamelCase			:			List[str]       			=					timesteps[None].to(input_tokens.device  )

    # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
    __lowerCamelCase			:			Tuple       			=					timesteps * torch.ones(input_tokens.shape[0]     ,      dtype=timesteps.dtype     ,      device=timesteps.device  )

    __lowerCamelCase			:			int       			=					self.decoder(
        encodings_and_masks=_a     ,      decoder_input_tokens=_a     ,      decoder_noise_time=_a  )
    return logits




  @torch.no_grad()
  def __call__(					self :      Optional[int]     ,      _a :      List[List[int]]     ,      _a :      Optional[torch.Generator] = None     ,      _a :      int = 100     ,      _a :      bool = True     ,      _a :      str = "numpy"     ,      _a :      Optional[Callable[[int, int, torch.FloatTensor], None]] = None     ,      _a :      int = 1     ,      )   ->   Union[AudioPipelineOutput, Tuple]:
    if (callback_steps is None) or (
        callback_steps is not None and (not isinstance(_a     ,      _a  ) or callback_steps <= 0)
    ):
      raise ValueError(
          f'`callback_steps` has to be a positive integer but is {callback_steps} of type'
          f' {type(_a  )}.'  )

    __lowerCamelCase			:			Optional[int]       			=					np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims]     ,      dtype=np.floataa  )
    __lowerCamelCase			:			Dict       			=					np.zeros([1, 0, self.n_dims]     ,      np.floataa  )
    __lowerCamelCase			:			List[Any]       			=					torch.ones((1, TARGET_FEATURE_LENGTH)     ,      dtype=_a     ,      device=self.device  )

    for i, encoder_input_tokens in enumerate(_a  ):
      if i == 0:
        __lowerCamelCase			:			List[str]       			=					torch.from_numpy(pred_mel[:1].copy()  ).to(
            device=self.device     ,      dtype=self.decoder.dtype  )
        # The first chunk has no previous context.
        __lowerCamelCase			:			List[Any]       			=					torch.zeros((1, TARGET_FEATURE_LENGTH)     ,      dtype=_a     ,      device=self.device  )
      else:
        # The full song pipeline does not feed in a context feature, so the mask
        # will be all 0s after the feature converter. Because we know we're
        # feeding in a full context chunk from the previous prediction, set it
        # to all 1s.
        __lowerCamelCase			:			int       			=					ones

      __lowerCamelCase			:			int       			=					self.scale_features(
          _a     ,      output_range=[-1.0, 1.0]     ,      clip=_a  )

      __lowerCamelCase			:			Tuple       			=					self.encode(
          input_tokens=torch.IntTensor([encoder_input_tokens]  ).to(device=self.device  )     ,      continuous_inputs=_a     ,      continuous_mask=_a     ,      )

      # Sample encoder_continuous_inputs shaped gaussian noise to begin loop
      __lowerCamelCase			:			Optional[int]       			=					randn_tensor(
          shape=encoder_continuous_inputs.shape     ,      generator=_a     ,      device=self.device     ,      dtype=self.decoder.dtype     ,      )

      # set step values
      self.scheduler.set_timesteps(_a  )

      # Denoising diffusion loop
      for j, t in enumerate(self.progress_bar(self.scheduler.timesteps  )  ):
        __lowerCamelCase			:			List[Any]       			=					self.decode(
            encodings_and_masks=_a     ,      input_tokens=_a     ,      noise_time=t / self.scheduler.config.num_train_timesteps     ,      )

        # Compute previous output: x_t -> x_t-1
        __lowerCamelCase			:			Optional[int]       			=					self.scheduler.step(_a     ,      _a     ,      _a     ,      generator=_a  ).prev_sample

      __lowerCamelCase			:			List[Any]       			=					self.scale_to_features(_a     ,      input_range=[-1.0, 1.0]  )
      __lowerCamelCase			:			Union[str, Any]       			=					mel[:1]
      __lowerCamelCase			:			Union[str, Any]       			=					mel.cpu().float().numpy()

      __lowerCamelCase			:			Dict       			=					np.concatenate([full_pred_mel, pred_mel[:1]]     ,      axis=1  )

      # call the callback, if provided
      if callback is not None and i % callback_steps == 0:
        callback(_a     ,      _a  )

      logger.info('Generated segment'     ,      _a  )

    if output_type == "numpy" and not is_onnx_available():
      raise ValueError(
          'Cannot return output in \'np\' format if ONNX is not available. Make sure to have ONNX installed or set \'output_type\' to \'mel\'.'  )
    elif output_type == "numpy" and self.melgan is None:
      raise ValueError(
          'Cannot return output in \'np\' format if melgan component is not defined. Make sure to define `self.melgan` or set \'output_type\' to \'mel\'.'  )

    if output_type == "numpy":
      __lowerCamelCase			:			Tuple       			=					self.melgan(input_features=full_pred_mel.astype(np.floataa  )  )
    else:
      __lowerCamelCase			:			List[str]       			=					full_pred_mel

    if not return_dict:
      return (output,)

    return AudioPipelineOutput(audios=_a  )






