





'''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.models.esm.modeling_esmfold import EsmForProteinFolding
class 				a_       :





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





        """simple docstring"""




        lowercase_      :Union[str, Any]         =			parent
        lowercase_      :int         =			batch_size
        lowercase_      :List[Any]         =			seq_length
        lowercase_      :str         =			is_training
        lowercase_      :List[Any]         =			use_input_mask
        lowercase_      :Dict         =			use_token_type_ids
        lowercase_      :Any         =			use_labels
        lowercase_      :List[Any]         =			vocab_size
        lowercase_      :Optional[Any]         =			hidden_size
        lowercase_      :Optional[int]         =			num_hidden_layers
        lowercase_      :Dict         =			num_attention_heads
        lowercase_      :Dict         =			intermediate_size
        lowercase_      :Any         =			hidden_act
        lowercase_      :Optional[int]         =			hidden_dropout_prob
        lowercase_      :List[str]         =			attention_probs_dropout_prob
        lowercase_      :List[Any]         =			max_position_embeddings
        lowercase_      :List[Any]         =			type_vocab_size
        lowercase_      :Optional[Any]         =			type_sequence_label_size
        lowercase_      :Tuple         =			initializer_range
        lowercase_      :Any         =			num_labels
        lowercase_      :str         =			num_choices
        lowercase_      :List[str]         =			scope





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :List[str]         =			ids_tensor([self.batch_size, self.seq_length]     ,				self.vocab_size						)

        lowercase_      :str         =			None
        if self.use_input_mask:
           lowercase_      :Optional[Any]         =			random_attention_mask([self.batch_size, self.seq_length]						)

        lowercase_      :Dict         =			None
        lowercase_      :str         =			None
        lowercase_      :Tuple         =			None
        if self.use_labels:
           lowercase_      :Any         =			ids_tensor([self.batch_size]     ,				self.type_sequence_label_size						)
           lowercase_      :Dict         =			ids_tensor([self.batch_size, self.seq_length]     ,				self.num_labels						)
           lowercase_      :Tuple         =			ids_tensor([self.batch_size]     ,				self.num_choices						)

        lowercase_      :int         =			self.get_config()

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





     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        lowercase_      :Dict         =			EsmConfig(
            vocab_size=33     ,				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     ,				is_folding_model=lowercase     ,				esmfold_config={"trunk": {"num_blocks": 2}, "fp16_esm": False}     ,				)
        return config





     def 							lowercase__				(			self		: List[Any]     ,				lowercase		: Any     ,				lowercase		: Union[str, Any]     ,				lowercase		: Tuple     ,				lowercase		: List[str]     ,				lowercase		: Any     ,				lowercase		: List[str]						):





        """simple docstring"""




        lowercase_      :Optional[Any]         =			EsmForProteinFolding(config=lowercase						).float()
        model.to(lowercase						)
        model.eval()
        lowercase_      :Tuple         =			model(lowercase     ,				attention_mask=lowercase						)
        lowercase_      :Tuple         =			model(lowercase						)
        lowercase_      :Optional[int]         =			model(lowercase						)

        self.parent.assertEqual(result.positions.shape     ,				(8, self.batch_size, self.seq_length, 14, 3)						)
        self.parent.assertEqual(result.angles.shape     ,				(8, self.batch_size, self.seq_length, 7, 2)						)







     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :str         =			self.prepare_config_and_inputs()
        (
            (
            lowercase_
        )      ,							(
            lowercase_
        )      ,							(
            lowercase_
        )      ,							(
            lowercase_
        )      ,							(
            lowercase_
        )      ,							(
            lowercase_
        )      ,							
        )      :str         =			config_and_inputs
        lowercase_      :Tuple         =			{"input_ids": input_ids, "attention_mask": input_mask}
        return config, inputs_dict







@require_torch
class 				a_       (    _lowerCAmelCase     ,						_lowerCAmelCase     ,						unittest.TestCase							):
     __A								=      False

     __A								=      (EsmForProteinFolding,) if is_torch_available() else ()
     __A								=      ()
     __A								=      {} if is_torch_available() else {}
     __A								=      False





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :int         =			EsmFoldModelTester(self						)
        lowercase_      :Dict         =			ConfigTester(self     ,				config_class=lowercase     ,				hidden_size=37						)





     def 							lowercase__				(			self		: str						):





        """simple docstring"""




        self.config_tester.run_common_tests()





     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        lowercase_      :Dict         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase						)





     @unittest.skip("Does not support attention outputs"						)
     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        pass





     @unittest.skip
     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        pass





     @unittest.skip("Esm does not support embedding resizing"						)
     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        pass





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





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support passing input embeds!"						)
     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support head pruning."						)
     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support head pruning."						)
     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support head pruning."						)
     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support head pruning."						)
     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support head pruning."						)
     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not output hidden states in the normal way."						)
     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMfold does not output hidden states in the normal way."						)
     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold only has one output format."						)
     def 							lowercase__				(			self		: str						):





        """simple docstring"""




        pass





     @unittest.skip("This test doesn't work for ESMFold and doesn't test core functionality"						)
     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support input chunking."						)
     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold doesn't respect you and it certainly doesn't respect your initialization arguments."						)
     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold doesn't support torchscript compilation."						)
     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold doesn't support torchscript compilation."						)
     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold doesn't support torchscript compilation."						)
     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold doesn't support data parallel."						)
     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        pass







     @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        pass







@require_torch
class 				a_       (    _lowerCAmelCase							):





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





        """simple docstring"""




        lowercase_      :Optional[int]         =			EsmForProteinFolding.from_pretrained("facebook/esmfold_v1"						).float()
        model.eval()
        lowercase_      :List[str]         =			torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]						)
        lowercase_      :Union[str, Any]         =			model(lowercase						)["positions"]
        lowercase_      :Any         =			torch.tensor([2.58_28, 0.79_93, -10.93_34]     ,				dtype=torch.floataa						)
        self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0]     ,				lowercase     ,				atol=1e-4						)						)









'''simple docstring'''


import copy
from typing import Any, Dict, List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, logging


lowerCAmelCase :					Optional[int] 							=logging.get_logger(__name__)
class 				a_       (    _lowerCAmelCase							):
     __A								=      ["input_features"]





     def __init__(			self		: Any     ,				lowercase		: Tuple=80     ,				lowercase		: Optional[int]=16_000     ,				lowercase		: Optional[Any]=160     ,				lowercase		: Optional[int]=30     ,				lowercase		: List[Any]=400     ,				lowercase		: Dict=0.0     ,				lowercase		: Tuple=False     ,				**lowercase		: Optional[int]     ,				):





        """simple docstring"""




        super().__init__(
            feature_size=lowercase     ,				sampling_rate=lowercase     ,				padding_value=lowercase     ,				return_attention_mask=lowercase     ,				**lowercase     ,				)
        lowercase_      :Optional[int]         =			n_fft
        lowercase_      :List[Any]         =			hop_length
        lowercase_      :Tuple         =			chunk_length
        lowercase_      :List[str]         =			chunk_length * sampling_rate
        lowercase_      :Optional[Any]         =			self.n_samples // hop_length
        lowercase_      :Any         =			sampling_rate
        lowercase_      :List[Any]         =			mel_filter_bank(
            num_frequency_bins=1 + n_fft // 2     ,				num_mel_filters=lowercase     ,				min_frequency=0.0     ,				max_frequency=80_00.0     ,				sampling_rate=lowercase     ,				norm="slaney"     ,				mel_scale="slaney"     ,				)





     def 							lowercase__				(			self		: str     ,				lowercase		: np.array						):





        """simple docstring"""




        lowercase_      :Any         =			spectrogram(
            lowercase     ,				window_function(self.n_fft     ,				"hann"						)     ,				frame_length=self.n_fft     ,				hop_length=self.hop_length     ,				power=2.0     ,				mel_filters=self.mel_filters     ,				log_mel="log10"     ,				)
        lowercase_      :Any         =			log_spec[:, :-1]
        lowercase_      :List[Any]         =			np.maximum(lowercase     ,				log_spec.max() - 8.0						)
        lowercase_      :Dict         =			(log_spec + 4.0) / 4.0
        return log_spec





     @staticmethod
     # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
     def 							lowercase__				(			lowercase		: List[np.ndarray]     ,				lowercase		: List[np.ndarray]     ,				lowercase		: float = 0.0						):





        """simple docstring"""




        if attention_mask is not None:
           lowercase_      :Optional[int]         =			np.array(lowercase     ,				np.intaa						)
           lowercase_      :Any         =			[]

           for vector, length in zip(lowercase     ,				attention_mask.sum(-1						)						):
              lowercase_      :Dict         =			(vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7						)
              if length < normed_slice.shape[0]:
                 lowercase_      :List[Any]         =			padding_value

              normed_input_values.append(lowercase						)
        else:
           lowercase_      :List[Any]         =			[(x - x.mean()) / np.sqrt(x.var() + 1e-7						) for x in input_values]

        return normed_input_values





     def __call__(			self		: Tuple     ,				lowercase		: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]     ,				lowercase		: bool = True     ,				lowercase		: Optional[int] = None     ,				lowercase		: Optional[Union[str, TensorType]] = None     ,				lowercase		: Optional[bool] = None     ,				lowercase		: Optional[str] = "max_length"     ,				lowercase		: Optional[int] = None     ,				lowercase		: Optional[int] = None     ,				lowercase		: Optional[bool] = None     ,				**lowercase		: Union[str, Any]     ,				):





        """simple docstring"""




        if sampling_rate is not None:
           if sampling_rate != self.sampling_rate:
              raise ValueError(
                  F'The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a'
                  F' sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input'
                  F' was sampled with {self.sampling_rate} and not {sampling_rate}.'						)
        else:
           logger.warning(
               "It is strongly recommended to pass the `sampling_rate` argument to this function. "
               "Failing to do so can result in silent errors that might be hard to debug."						)

        lowercase_      :List[str]         =			isinstance(lowercase     ,				np.ndarray						) and len(raw_speech.shape						) > 1
        if is_batched_numpy and len(raw_speech.shape						) > 2:
           raise ValueError(F'Only mono-channel audio is supported for input to {self}'						)
        lowercase_      :Optional[Any]         =			is_batched_numpy or (
            isinstance(lowercase     ,				(list, tuple)						) and (isinstance(raw_speech[0]     ,				(np.ndarray, tuple, list)						))
        )

        if is_batched:
           lowercase_      :Any         =			[np.asarray([speech]     ,				dtype=np.floataa						).T for speech in raw_speech]
        elif not is_batched and not isinstance(lowercase     ,				np.ndarray						):
           lowercase_      :List[Any]         =			np.asarray(lowercase     ,				dtype=np.floataa						)
        elif isinstance(lowercase     ,				np.ndarray						) and raw_speech.dtype is np.dtype(np.floataa						):
           lowercase_      :Union[str, Any]         =			raw_speech.astype(np.floataa						)

        # always return batch
        if not is_batched:
           lowercase_      :Optional[int]         =			[np.asarray([raw_speech]						).T]

        lowercase_      :int         =			BatchFeature({"input_features": raw_speech}						)

        # convert into correct format for padding

        lowercase_      :Tuple         =			self.pad(
            lowercase     ,				padding=lowercase     ,				max_length=max_length if max_length else self.n_samples     ,				truncation=lowercase     ,				pad_to_multiple_of=lowercase     ,				return_attention_mask=return_attention_mask or do_normalize     ,				)

        # zero-mean and unit-variance normalization
        if do_normalize:
           lowercase_      :Union[str, Any]         =			self.zero_mean_unit_var_norm(
               padded_inputs["input_features"]     ,				attention_mask=padded_inputs["attention_mask"]     ,				padding_value=self.padding_value     ,				)
           lowercase_      :List[Any]         =			np.stack(padded_inputs["input_features"]     ,				axis=0						)

        # make sure list is in array format
        lowercase_      :Union[str, Any]         =			padded_inputs.get("input_features"						).transpose(2     ,				0     ,				1						)

        lowercase_      :List[str]         =			[self._np_extract_fbank_features(lowercase						) for waveform in input_features[0]]

        if isinstance(input_features[0]     ,				lowercase						):
           lowercase_      :Tuple         =			[np.asarray(lowercase     ,				dtype=np.floataa						) for feature in input_features]
        else:
           lowercase_      :Union[str, Any]         =			input_features

        if return_attention_mask:
           # rescale from sample (48000) to feature (3000)
           lowercase_      :Dict         =			padded_inputs["attention_mask"][:, :: self.hop_length]

        if return_tensors is not None:
           lowercase_      :Tuple         =			padded_inputs.convert_to_tensors(lowercase						)

        return padded_inputs







     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        lowercase_      :Union[str, Any]         =			copy.deepcopy(self.__dict__						)
        lowercase_      :List[str]         =			self.__class__.__name__
        if "mel_filters" in output:
           del output["mel_filters"]
        return output


