


'''simple docstring'''

import unittest

from transformers import EsmConfig, is_torch_available
from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import EsmForMaskedLM, EsmForSequenceClassification, EsmForTokenClassification, EsmModel
 from transformers.models.esm.modeling_esm import (
     ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
     EsmEmbeddings,
     create_position_ids_from_input_ids,
 )
class     __lowerCAmelCase		:





      '''simple docstring'''






      def __init__(self       :       str  ,		UpperCamelCase       :       List[Any]  ,		UpperCamelCase       :       Optional[int]=13  ,		UpperCamelCase       :       Optional[Any]=7  ,		UpperCamelCase       :       List[str]=False  ,		UpperCamelCase       :       Dict=True  ,		UpperCamelCase       :       Optional[Any]=False  ,		UpperCamelCase       :       Tuple=True  ,		UpperCamelCase       :       List[Any]=33  ,		UpperCamelCase       :       int=32  ,		UpperCamelCase       :       int=5  ,		UpperCamelCase       :       int=4  ,		UpperCamelCase       :       List[str]=37  ,		UpperCamelCase       :       str="gelu"  ,		UpperCamelCase       :       Union[str, Any]=0.1  ,		UpperCamelCase       :       Any=0.1  ,		UpperCamelCase       :       str=512  ,		UpperCamelCase       :       List[str]=16  ,		UpperCamelCase       :       Dict=2  ,		UpperCamelCase       :       Any=0.02  ,		UpperCamelCase       :       int=3  ,		UpperCamelCase       :       str=4  ,		UpperCamelCase       :       Any=None  ,		):

             '''simple docstring'''







             lowercase__      =					parent
             lowercase__      =					batch_size
             lowercase__      =					seq_length
             lowercase__      =					is_training
             lowercase__      =					use_input_mask
             lowercase__      =					use_token_type_ids
             lowercase__      =					use_labels
             lowercase__      =					vocab_size
             lowercase__      =					hidden_size
             lowercase__      =					num_hidden_layers
             lowercase__      =					num_attention_heads
             lowercase__      =					intermediate_size
             lowercase__      =					hidden_act
             lowercase__      =					hidden_dropout_prob
             lowercase__      =					attention_probs_dropout_prob
             lowercase__      =					max_position_embeddings
             lowercase__      =					type_vocab_size
             lowercase__      =					type_sequence_label_size
             lowercase__      =					initializer_range
             lowercase__      =					num_labels
             lowercase__      =					num_choices
             lowercase__      =					scope



      def  UpperCamelCase__						(self       :       Tuple					):

             '''simple docstring'''







             lowercase__      =					ids_tensor([self.batch_size, self.seq_length]  ,		self.vocab_size					)

             lowercase__      =					None
             if self.use_input_mask:
                    lowercase__      =					random_attention_mask([self.batch_size, self.seq_length]					)

             lowercase__      =					None
             lowercase__      =					None
             lowercase__      =					None
             if self.use_labels:
                    lowercase__      =					ids_tensor([self.batch_size]  ,		self.type_sequence_label_size					)
                    lowercase__      =					ids_tensor([self.batch_size, self.seq_length]  ,		self.num_labels					)
                    lowercase__      =					ids_tensor([self.batch_size]  ,		self.num_choices					)

             lowercase__      =					self.get_config()

             return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels



      def  UpperCamelCase__						(self       :       str					):

             '''simple docstring'''







             return EsmConfig(
                 vocab_size=self.vocab_size  ,		hidden_size=self.hidden_size  ,		pad_token_id=1  ,		num_hidden_layers=self.num_hidden_layers  ,		num_attention_heads=self.num_attention_heads  ,		intermediate_size=self.intermediate_size  ,		hidden_act=self.hidden_act  ,		hidden_dropout_prob=self.hidden_dropout_prob  ,		attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,		max_position_embeddings=self.max_position_embeddings  ,		type_vocab_size=self.type_vocab_size  ,		initializer_range=self.initializer_range  ,		)



      def  UpperCamelCase__						(self       :       str  ,		UpperCamelCase       :       List[Any]  ,		UpperCamelCase       :       str  ,		UpperCamelCase       :       Optional[int]  ,		UpperCamelCase       :       Any  ,		UpperCamelCase       :       Tuple  ,		UpperCamelCase       :       Optional[int]					):

             '''simple docstring'''







             lowercase__      =					EsmModel(config=_a					)
             model.to(_a					)
             model.eval()
             lowercase__      =					model(_a  ,		attention_mask=_a					)
             lowercase__      =					model(_a					)
             lowercase__      =					model(_a					)

             self.parent.assertEqual(result.last_hidden_state.shape  ,		(self.batch_size, self.seq_length, self.hidden_size)					)
             self.parent.assertEqual(result.pooler_output.shape  ,		(self.batch_size, self.hidden_size)					)



      def  UpperCamelCase__						(self       :       Any  ,		UpperCamelCase       :       Optional[int]  ,		UpperCamelCase       :       Any  ,		UpperCamelCase       :       List[str]  ,		UpperCamelCase       :       Dict  ,		UpperCamelCase       :       List[str]  ,		UpperCamelCase       :       int					):

             '''simple docstring'''







             lowercase__      =					EsmForMaskedLM(config=_a					)
             model.to(_a					)
             model.eval()
             lowercase__      =					model(_a  ,		attention_mask=_a  ,		labels=_a					)
             self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.seq_length, self.vocab_size)					)



      def  UpperCamelCase__						(self       :       str  ,		UpperCamelCase       :       str  ,		UpperCamelCase       :       Union[str, Any]  ,		UpperCamelCase       :       Dict  ,		UpperCamelCase       :       List[Any]  ,		UpperCamelCase       :       Any  ,		UpperCamelCase       :       str					):

             '''simple docstring'''







             lowercase__      =					self.num_labels
             lowercase__      =					EsmForTokenClassification(config=_a					)
             model.to(_a					)
             model.eval()
             lowercase__      =					model(_a  ,		attention_mask=_a  ,		labels=_a					)
             self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.seq_length, self.num_labels)					)






      def  UpperCamelCase__						(self       :       Dict					):

             '''simple docstring'''







             lowercase__      =					self.prepare_config_and_inputs()
             (
                 lowercase__
             )      =					config_and_inputs
             lowercase__      =					{'input_ids': input_ids, 'attention_mask': input_mask}
             return config, inputs_dict






@require_torch
class     __lowerCAmelCase		(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_    ,	unittest.TestCase	):





      '''simple docstring'''






      lowerCAmelCase__   :	Dict       =				False

      lowerCAmelCase__   :	List[Any]       =				(
          (
              EsmForMaskedLM,
              EsmModel,
              EsmForSequenceClassification,
              EsmForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      lowerCAmelCase__   :	List[str]       =				()
      lowerCAmelCase__   :	Any       =				(
          {
              """feature-extraction""": EsmModel,
              """fill-mask""": EsmForMaskedLM,
              """text-classification""": EsmForSequenceClassification,
              """token-classification""": EsmForTokenClassification,
              """zero-shot""": EsmForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      lowerCAmelCase__   :	Tuple       =				True



      def  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







             lowercase__      =					EsmModelTester(self					)
             lowercase__      =					ConfigTester(self  ,		config_class=_a  ,		hidden_size=37					)



      def  UpperCamelCase__						(self       :       Optional[Any]					):

             '''simple docstring'''







             self.config_tester.run_common_tests()



      def  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*_a					)



      def  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             for type in ["absolute", "relative_key", "relative_key_query"]:
                    lowercase__      =					type
                    self.model_tester.create_and_check_model(*_a					)



      def  UpperCamelCase__						(self       :       List[Any]					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_lm(*_a					)



      def  UpperCamelCase__						(self       :       Tuple					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*_a					)



      @slow
      def  UpperCamelCase__						(self       :       Optional[int]					):

             '''simple docstring'''







             for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    lowercase__      =					EsmModel.from_pretrained(_a					)
                    self.assertIsNotNone(_a					)



      def  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()[0]
             lowercase__      =					EsmEmbeddings(config=_a					)

             lowercase__      =					torch.as_tensor([[12, 31, 13, model.padding_idx]]					)
             lowercase__      =					torch.as_tensor(
                 [
                     [
                         0 + model.padding_idx + 1,
                         1 + model.padding_idx + 1,
                         2 + model.padding_idx + 1,
                         model.padding_idx,
                     ]
                 ]					)
             lowercase__      =					create_position_ids_from_input_ids(_a  ,		model.padding_idx					)
             self.assertEqual(position_ids.shape  ,		expected_positions.shape					)
             self.assertTrue(torch.all(torch.eq(_a  ,		_a					)					)					)



      def  UpperCamelCase__						(self       :       List[Any]					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()[0]
             lowercase__      =					EsmEmbeddings(config=_a					)

             lowercase__      =					torch.empty(2  ,		4  ,		30					)
             lowercase__      =					[
                 0 + embeddings.padding_idx + 1,
                 1 + embeddings.padding_idx + 1,
                 2 + embeddings.padding_idx + 1,
                 3 + embeddings.padding_idx + 1,
             ]
             lowercase__      =					torch.as_tensor([expected_single_positions, expected_single_positions]					)
             lowercase__      =					embeddings.create_position_ids_from_inputs_embeds(_a					)
             self.assertEqual(position_ids.shape  ,		expected_positions.shape					)
             self.assertTrue(torch.all(torch.eq(_a  ,		_a					)					)					)



      @unittest.skip('''Esm does not support embedding resizing'''					)
      def  UpperCamelCase__						(self       :       str					):

             '''simple docstring'''







             pass



      @unittest.skip('''Esm does not support embedding resizing'''					)
      def  UpperCamelCase__						(self       :       List[Any]					):

             '''simple docstring'''







             pass






      @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''					)
      def  UpperCamelCase__						(self       :       Optional[Any]					):

             '''simple docstring'''







             pass






@require_torch
class     __lowerCAmelCase		(SCREAMING_SNAKE_CASE_	):





      '''simple docstring'''






      @slow
      def  UpperCamelCase__						(self       :       Union[str, Any]					):

             '''simple docstring'''







             with torch.no_grad():
                    lowercase__      =					EsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D'''					)
                    model.eval()
                    lowercase__      =					torch.tensor([[0, 1, 2, 3, 4, 5]]					)
                    lowercase__      =					model(_a					)[0]

                    lowercase__      =					33

                    lowercase__      =					torch.Size((1, 6, vocab_size)					)
                    self.assertEqual(output.shape  ,		_a					)

                    lowercase__      =					torch.tensor(
                        [[[8.92_15, -10.58_98, -6.46_71], [-6.39_67, -13.91_14, -1.12_12], [-7.78_12, -13.95_16, -3.74_06]]]					)
                    self.assertTrue(torch.allclose(output[:, :3, :3]  ,		_a  ,		atol=1E-4					)					)






      @slow
      def  UpperCamelCase__						(self       :       int					):

             '''simple docstring'''







             with torch.no_grad():
                    lowercase__      =					EsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D'''					)
                    model.eval()

                    lowercase__      =					torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]					)
                    lowercase__      =					model(_a					)[0]
                    # compare the actual values for a slice.
                    lowercase__      =					torch.tensor(
                        [[[0.14_44, 0.54_13, 0.32_48], [0.30_34, 0.00_53, 0.31_08], [0.32_28, -0.24_99, 0.34_15]]]					)
                    self.assertTrue(torch.allclose(output[:, :3, :3]  ,		_a  ,		atol=1E-4					)					)









'''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  )






