

"""simple docstring"""




import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
      import torch

if is_datasets_available():
      from datasets import load_dataset

__UpperCAmelCase			=					random.Random()
def 	lowerCAmelCase    (       __UpperCamelCase				,     __UpperCamelCase=1.0				,     __UpperCamelCase=None				,     __UpperCamelCase=None   ):
 '''simple docstring'''





 if rng is None:
  UpperCAmelCase__       :	int        =       global_rng

 UpperCAmelCase__       :	Dict        =       []
 for batch_idx in range(shape[0]   ):
  values.append([]   )
  for _ in range(shape[1]   ):
   values[-1].append(rng.random() * scale   )

 return values







class   __lowercase      (     unittest.TestCase       ):

  def __init__(	self		:      List[Any]       ,A		:      List[Any]       ,A		:      Optional[int]=7       ,A		:      int=400       ,A		:      Optional[int]=2_000       ,A		:      Dict=2_048       ,A		:      Optional[Any]=128       ,A		:      str=1       ,A		:      str=512       ,A		:      Dict=30       ,A		:      Any=44_100       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       parent
   UpperCAmelCase__       :	Optional[int]        =       batch_size
   UpperCAmelCase__       :	str        =       min_seq_length
   UpperCAmelCase__       :	int        =       max_seq_length
   UpperCAmelCase__       :	int        =       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
   UpperCAmelCase__       :	Tuple        =       spectrogram_length
   UpperCAmelCase__       :	str        =       feature_size
   UpperCAmelCase__       :	str        =       num_audio_channels
   UpperCAmelCase__       :	Any        =       hop_length
   UpperCAmelCase__       :	int        =       chunk_length
   UpperCAmelCase__       :	int        =       sampling_rate

  def  __lowercase							(	self		:      List[Any]     ):






   '''simple docstring'''
   return {
       "spectrogram_length": self.spectrogram_length,
       "feature_size": self.feature_size,
       "num_audio_channels": self.num_audio_channels,
       "hop_length": self.hop_length,
       "chunk_length": self.chunk_length,
       "sampling_rate": self.sampling_rate,
   }




  def  __lowercase							(	self		:      List[Any]       ,A		:      Dict=False       ,A		:      Any=False     ):






   '''simple docstring'''
   def _flatten(A		:      Dict     ):
    return list(itertools.chain(*A     )     )

   if equal_length:
    UpperCAmelCase__       :	Any        =       [floats_list((self.max_seq_length, self.feature_size)     ) for _ in range(self.batch_size     )]
   else:
    # make sure that inputs increase in size
    UpperCAmelCase__       :	Union[str, Any]        =       [
        floats_list((x, self.feature_size)     )
        for x in range(self.min_seq_length       ,self.max_seq_length       ,self.seq_length_diff     )
    ]
   if numpify:
    UpperCAmelCase__       :	int        =       [np.asarray(A     ) for x in speech_inputs]
   return speech_inputs







@require_torch
@require_torchaudio
class   __lowercase      (     __lowerCamelCase						,      unittest.TestCase       ):
  snake_case_												=							TvltFeatureExtractor

  def  __lowercase							(	self		:      Tuple     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[int]        =       TvltFeatureExtractionTester(self     )

  def  __lowercase							(	self		:      Any     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Tuple        =       self.feature_extraction_class(**self.feat_extract_dict     )
   self.assertTrue(hasattr(A       ,"""spectrogram_length"""     )     )
   self.assertTrue(hasattr(A       ,"""feature_size"""     )     )
   self.assertTrue(hasattr(A       ,"""num_audio_channels"""     )     )
   self.assertTrue(hasattr(A       ,"""hop_length"""     )     )
   self.assertTrue(hasattr(A       ,"""chunk_length"""     )     )
   self.assertTrue(hasattr(A       ,"""sampling_rate"""     )     )

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






   '''simple docstring'''
   UpperCAmelCase__       :	Union[str, Any]        =       self.feature_extraction_class(**self.feat_extract_dict     )

   with tempfile.TemporaryDirectory() as tmpdirname:
    UpperCAmelCase__       :	Any        =       feat_extract_first.save_pretrained(A     )[0]
    check_json_file_has_correct_format(A     )
    UpperCAmelCase__       :	Optional[Any]        =       self.feature_extraction_class.from_pretrained(A     )

   UpperCAmelCase__       :	List[Any]        =       feat_extract_first.to_dict()
   UpperCAmelCase__       :	Union[str, Any]        =       feat_extract_second.to_dict()
   UpperCAmelCase__       :	List[Any]        =       dict_first.pop("""mel_filters"""     )
   UpperCAmelCase__       :	Union[str, Any]        =       dict_second.pop("""mel_filters"""     )
   self.assertTrue(np.allclose(A       ,A     )     )
   self.assertEqual(A       ,A     )

  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[int]        =       self.feature_extraction_class(**self.feat_extract_dict     )

   with tempfile.TemporaryDirectory() as tmpdirname:
    UpperCAmelCase__       :	Optional[int]        =       os.path.join(A       ,"""feat_extract.json"""     )
    feat_extract_first.to_json_file(A     )
    UpperCAmelCase__       :	List[str]        =       self.feature_extraction_class.from_json_file(A     )

   UpperCAmelCase__       :	Dict        =       feat_extract_first.to_dict()
   UpperCAmelCase__       :	Optional[Any]        =       feat_extract_second.to_dict()
   UpperCAmelCase__       :	int        =       dict_first.pop("""mel_filters"""     )
   UpperCAmelCase__       :	List[str]        =       dict_second.pop("""mel_filters"""     )
   self.assertTrue(np.allclose(A       ,A     )     )
   self.assertEqual(A       ,A     )

  def  __lowercase							(	self		:      Tuple     ):






   '''simple docstring'''
   # Initialize feature_extractor
   UpperCAmelCase__       :	Optional[Any]        =       self.feature_extraction_class(**self.feat_extract_dict     )

   # create three inputs of length 800, 1000, and 1200
   UpperCAmelCase__       :	Tuple        =       [floats_list((1, x)     )[0] for x in range(800       ,1_400       ,200     )]
   UpperCAmelCase__       :	List[str]        =       [np.asarray(A     ) for speech_input in speech_inputs]

   # Test not batched input
   UpperCAmelCase__       :	Union[str, Any]        =       feature_extractor(np_speech_inputs[0]       ,return_tensors="""np"""       ,sampling_rate=44_100     ).audio_values

   self.assertTrue(encoded_audios.ndim == 4     )
   self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size     )
   self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length     )
   self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels     )

   # Test batched
   UpperCAmelCase__       :	Optional[Any]        =       feature_extractor(A       ,return_tensors="""np"""       ,sampling_rate=44_100     ).audio_values

   self.assertTrue(encoded_audios.ndim == 4     )
   self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size     )
   self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length     )
   self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels     )

   # Test audio masking
   UpperCAmelCase__       :	Optional[Any]        =       feature_extractor(
       A       ,return_tensors="""np"""       ,sampling_rate=44_100       ,mask_audio=A     ).audio_values

   self.assertTrue(encoded_audios.ndim == 4     )
   self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size     )
   self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length     )
   self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels     )

   # Test 2-D numpy arrays are batched.
   UpperCAmelCase__       :	Tuple        =       [floats_list((1, x)     )[0] for x in (800, 800, 800)]
   UpperCAmelCase__       :	str        =       np.asarray(A     )
   UpperCAmelCase__       :	List[Any]        =       feature_extractor(A       ,return_tensors="""np"""       ,sampling_rate=44_100     ).audio_values
   self.assertTrue(encoded_audios.ndim == 4     )
   self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size     )
   self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length     )
   self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels     )

  def  __lowercase							(	self		:      str       ,A		:      Dict     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Union[str, Any]        =       load_dataset("""hf-internal-testing/librispeech_asr_dummy"""       ,"""clean"""       ,split="""validation"""     )
   # automatic decoding with librispeech
   UpperCAmelCase__       :	List[str]        =       ds.sort("""id"""     ).select(range(A     )     )[:num_samples]["""audio"""]

   return [x["array"] for x in speech_samples]




  def  __lowercase							(	self		:      str     ):






   '''simple docstring'''
   UpperCAmelCase__       :	str        =       self._load_datasamples(1     )
   UpperCAmelCase__       :	str        =       TvltFeatureExtractor()
   UpperCAmelCase__       :	Dict        =       feature_extractor(A       ,return_tensors="""pt"""     ).audio_values

   self.assertEquals(audio_values.shape       ,(1, 1, 192, 128)     )

   UpperCAmelCase__       :	Dict        =       torch.tensor([[-0.3_0_3_2, -0.2_7_0_8], [-0.4_4_3_4, -0.4_0_0_7]]     )
   self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]       ,A       ,atol=1e-4     )     )









'''simple docstring'''





import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__lowerCamelCase												=						logging.get_logger(__name__)

__lowerCamelCase												=						{
    '''nvidia/segformer-b0-finetuned-ade-512-512''': (
        '''https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json'''
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}




class 				UpperCAmelCase			(			_snake_case						):
 UpperCAmelCase				=					"segformer"


 def __init__(      self							:	Dict			,       __lowerCamelCase							:	Dict=3			,       __lowerCamelCase							:	List[str]=4			,       __lowerCamelCase							:	List[Any]=[2, 2, 2, 2]			,       __lowerCamelCase							:	List[str]=[8, 4, 2, 1]			,       __lowerCamelCase							:	Optional[Any]=[3_2, 6_4, 1_6_0, 2_5_6]			,       __lowerCamelCase							:	Tuple=[7, 3, 3, 3]			,       __lowerCamelCase							:	Tuple=[4, 2, 2, 2]			,       __lowerCamelCase							:	Any=[1, 2, 5, 8]			,       __lowerCamelCase							:	Optional[Any]=[4, 4, 4, 4]			,       __lowerCamelCase							:	Union[str, Any]="gelu"			,       __lowerCamelCase							:	Dict=0.0			,       __lowerCamelCase							:	Union[str, Any]=0.0			,       __lowerCamelCase							:	Tuple=0.1			,       __lowerCamelCase							:	Optional[Any]=0.02			,       __lowerCamelCase							:	Optional[int]=0.1			,       __lowerCamelCase							:	str=1e-6			,       __lowerCamelCase							:	List[Any]=2_5_6			,       __lowerCamelCase							:	int=2_5_5			,       **__lowerCamelCase							:	str			,       ):
    super().__init__(**__lowerCamelCase )

    if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
       warnings.warn(
           '''Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'''
           ''' removed, as the behaviour will default to that of reshape_last_stage = True.'''			,       __lowerCamelCase			,       )

    UpperCAmelCase__	:Dict      =      num_channels
    UpperCAmelCase__	:List[Any]      =      num_encoder_blocks
    UpperCAmelCase__	:List[str]      =      depths
    UpperCAmelCase__	:str      =      sr_ratios
    UpperCAmelCase__	:Optional[Any]      =      hidden_sizes
    UpperCAmelCase__	:List[str]      =      patch_sizes
    UpperCAmelCase__	:List[str]      =      strides
    UpperCAmelCase__	:List[str]      =      mlp_ratios
    UpperCAmelCase__	:Dict      =      num_attention_heads
    UpperCAmelCase__	:Tuple      =      hidden_act
    UpperCAmelCase__	:List[str]      =      hidden_dropout_prob
    UpperCAmelCase__	:List[Any]      =      attention_probs_dropout_prob
    UpperCAmelCase__	:int      =      classifier_dropout_prob
    UpperCAmelCase__	:str      =      initializer_range
    UpperCAmelCase__	:List[str]      =      drop_path_rate
    UpperCAmelCase__	:Dict      =      layer_norm_eps
    UpperCAmelCase__	:List[str]      =      decoder_hidden_size
    UpperCAmelCase__	:Optional[int]      =      kwargs.get('''reshape_last_stage'''			,       __lowerCamelCase )
    UpperCAmelCase__	:str      =      semantic_loss_ignore_index




class 				UpperCAmelCase			(			_snake_case						):
 UpperCAmelCase				=					version.parse("1.11"						)


 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[Any] ):
    return OrderedDict(
        [
            ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
        ] )


 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[int] ):
    return 1e-4





 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	List[str] ):
    return 1_2



