




import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
       from transformers import SpeechaTextFeatureExtractor

UpperCAmelCase_									=	random.Random()




def 					lowerCamelCase__       (						A__       :					Union[str, Any]  ,			A__       :					Union[str, Any]=1.0  ,			A__       :					int=None  ,			A__       :					Union[str, Any]=None	):





       '''simple docstring'''



       if rng is None:
              __lowerCamelCase   =						global_rng

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

       return values





@require_torch
@require_torchaudio
class     lowerCamelCase__(					unittest.TestCase):
      def __init__(				self: str    ,       UpperCamelCase_: Union[str, Any]    ,       UpperCamelCase_: List[str]=7    ,       UpperCamelCase_: int=4_00    ,       UpperCamelCase_: Optional[Any]=20_00    ,       UpperCamelCase_: Tuple=24    ,       UpperCamelCase_: Optional[int]=24    ,       UpperCamelCase_: Dict=0.0    ,       UpperCamelCase_: Optional[int]=1_60_00    ,       UpperCamelCase_: Any=True    ,       UpperCamelCase_: Optional[Any]=True    ,       ):
             __lowerCamelCase   =						parent
             __lowerCamelCase   =						batch_size
             __lowerCamelCase   =						min_seq_length
             __lowerCamelCase   =						max_seq_length
             __lowerCamelCase   =						(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
             __lowerCamelCase   =						feature_size
             __lowerCamelCase   =						num_mel_bins
             __lowerCamelCase   =						padding_value
             __lowerCamelCase   =						sampling_rate
             __lowerCamelCase   =						return_attention_mask
             __lowerCamelCase   =						do_normalize
      def 			lowerCAmelCase__						(				self: Dict   ):
             return {
                 "feature_size": self.feature_size,
                 "num_mel_bins": self.num_mel_bins,
                 "padding_value": self.padding_value,
                 "sampling_rate": self.sampling_rate,
                 "return_attention_mask": self.return_attention_mask,
                 "do_normalize": self.do_normalize,
             }




      def 			lowerCAmelCase__						(				self: Union[str, Any]    ,       UpperCamelCase_: List[Any]=False    ,       UpperCamelCase_: str=False   ):







             def _flatten(UpperCamelCase_: str   ):
                    return list(itertools.chain(*__lowerCAmelCase   )   )

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





@require_torch
@require_torchaudio
class     lowerCamelCase__(					__lowerCamelCase ,     unittest.TestCase):
      UpperCAmelCase__      :	int    		=   SpeechaTextFeatureExtractor if is_speech_available() else None
      def 			lowerCAmelCase__						(				self: List[Any]   ):
             __lowerCamelCase   =						SpeechaTextFeatureExtractionTester(self   )
      def 			lowerCAmelCase__						(				self: Union[str, Any]    ,       UpperCamelCase_: Optional[int]   ):
             self.assertTrue(np.all(np.mean(__lowerCAmelCase    ,       axis=0   ) < 1E-3   )   )
             self.assertTrue(np.all(np.abs(np.var(__lowerCAmelCase    ,       axis=0   ) - 1   ) < 1E-3   )   )
      def 			lowerCAmelCase__						(				self: List[str]   ):
             __lowerCamelCase   =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
             # create three inputs of length 800, 1000, and 1200
             __lowerCamelCase   =						[floats_list((1, x)   )[0] for x in range(8_00    ,       14_00    ,       2_00   )]
             __lowerCamelCase   =						[np.asarray(__lowerCAmelCase   ) for speech_input in speech_inputs]

             # Test feature size
             __lowerCamelCase   =						feature_extractor(__lowerCAmelCase    ,       padding=__lowerCAmelCase    ,       return_tensors="""np"""   ).input_features
             self.assertTrue(input_features.ndim == 3   )
             self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size   )

             # Test not batched input
             __lowerCamelCase   =						feature_extractor(speech_inputs[0]    ,       return_tensors="""np"""   ).input_features
             __lowerCamelCase   =						feature_extractor(np_speech_inputs[0]    ,       return_tensors="""np"""   ).input_features
             self.assertTrue(np.allclose(__lowerCAmelCase    ,       __lowerCAmelCase    ,       atol=1E-3   )   )

             # Test batched
             __lowerCamelCase   =						feature_extractor(__lowerCAmelCase    ,       return_tensors="""np"""   ).input_features
             __lowerCamelCase   =						feature_extractor(__lowerCAmelCase    ,       return_tensors="""np"""   ).input_features
             for enc_seq_a, enc_seq_a in zip(__lowerCAmelCase    ,       __lowerCAmelCase   ):
                    self.assertTrue(np.allclose(__lowerCAmelCase    ,       __lowerCAmelCase    ,       atol=1E-3   )   )

             # Test 2-D numpy arrays are batched.
             __lowerCamelCase   =						[floats_list((1, x)   )[0] for x in (8_00, 8_00, 8_00)]
             __lowerCamelCase   =						np.asarray(__lowerCAmelCase   )
             __lowerCamelCase   =						feature_extractor(__lowerCAmelCase    ,       return_tensors="""np"""   ).input_features
             __lowerCamelCase   =						feature_extractor(__lowerCAmelCase    ,       return_tensors="""np"""   ).input_features
             for enc_seq_a, enc_seq_a in zip(__lowerCAmelCase    ,       __lowerCAmelCase   ):
                    self.assertTrue(np.allclose(__lowerCAmelCase    ,       __lowerCAmelCase    ,       atol=1E-3   )   )
      def 			lowerCAmelCase__						(				self: Union[str, Any]   ):
             __lowerCamelCase   =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
             __lowerCamelCase   =						[floats_list((1, x)   )[0] for x in range(8_00    ,       14_00    ,       2_00   )]

             __lowerCamelCase   =						["""longest""", """max_length""", """do_not_pad"""]
             __lowerCamelCase   =						[None, 16, None]
             for max_length, padding in zip(__lowerCAmelCase    ,       __lowerCAmelCase   ):
                    __lowerCamelCase   =						feature_extractor(
                        __lowerCAmelCase    ,       padding=__lowerCAmelCase    ,       max_length=__lowerCAmelCase    ,       return_attention_mask=__lowerCAmelCase   )
                    __lowerCamelCase   =						inputs.input_features
                    __lowerCamelCase   =						inputs.attention_mask
                    __lowerCamelCase   =						[np.sum(__lowerCAmelCase   ) for x in attention_mask]

                    self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]   )
                    self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]   )
                    self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]   )
      def 			lowerCAmelCase__						(				self: Dict   ):
             __lowerCamelCase   =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
             __lowerCamelCase   =						[floats_list((1, x)   )[0] for x in range(8_00    ,       14_00    ,       2_00   )]

             __lowerCamelCase   =						["""longest""", """max_length""", """do_not_pad"""]
             __lowerCamelCase   =						[None, 16, None]
             for max_length, padding in zip(__lowerCAmelCase    ,       __lowerCAmelCase   ):
                    __lowerCamelCase   =						feature_extractor(
                        __lowerCAmelCase    ,       max_length=__lowerCAmelCase    ,       padding=__lowerCAmelCase    ,       return_tensors="""np"""    ,       return_attention_mask=__lowerCAmelCase   )
                    __lowerCamelCase   =						inputs.input_features
                    __lowerCamelCase   =						inputs.attention_mask
                    __lowerCamelCase   =						[np.sum(__lowerCAmelCase   ) for x in attention_mask]

                    self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]   )
                    self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1E-6   )
                    self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]   )
                    self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1E-6   )
                    self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]   )
      def 			lowerCAmelCase__						(				self: Tuple   ):
             __lowerCamelCase   =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
             __lowerCamelCase   =						[floats_list((1, x)   )[0] for x in range(8_00    ,       14_00    ,       2_00   )]
             __lowerCamelCase   =						feature_extractor(
                 __lowerCAmelCase    ,       padding="""max_length"""    ,       max_length=4    ,       truncation=__lowerCAmelCase    ,       return_tensors="""np"""    ,       return_attention_mask=__lowerCAmelCase    ,       )
             __lowerCamelCase   =						inputs.input_features
             __lowerCamelCase   =						inputs.attention_mask
             __lowerCamelCase   =						np.sum(attention_mask == 1    ,       axis=1   )

             self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]   )
             self._check_zero_mean_unit_variance(input_features[1]   )
             self._check_zero_mean_unit_variance(input_features[2]   )
      def 			lowerCAmelCase__						(				self: Optional[int]   ):
             __lowerCamelCase   =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
             __lowerCamelCase   =						[floats_list((1, x)   )[0] for x in range(8_00    ,       14_00    ,       2_00   )]
             __lowerCamelCase   =						feature_extractor(
                 __lowerCAmelCase    ,       padding="""longest"""    ,       max_length=4    ,       truncation=__lowerCAmelCase    ,       return_tensors="""np"""    ,       return_attention_mask=__lowerCAmelCase    ,       )
             __lowerCamelCase   =						inputs.input_features
             __lowerCamelCase   =						inputs.attention_mask
             __lowerCamelCase   =						np.sum(attention_mask == 1    ,       axis=1   )

             self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]   )
             self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]   )
             self._check_zero_mean_unit_variance(input_features[2]   )

             # make sure that if max_length < longest -> then pad to max_length
             self.assertEqual(input_features.shape    ,       (3, 4, 24)   )

             __lowerCamelCase   =						[floats_list((1, x)   )[0] for x in range(8_00    ,       14_00    ,       2_00   )]
             __lowerCamelCase   =						feature_extractor(
                 __lowerCAmelCase    ,       padding="""longest"""    ,       max_length=16    ,       truncation=__lowerCAmelCase    ,       return_tensors="""np"""    ,       return_attention_mask=__lowerCAmelCase    ,       )
             __lowerCamelCase   =						inputs.input_features
             __lowerCamelCase   =						inputs.attention_mask
             __lowerCamelCase   =						np.sum(attention_mask == 1    ,       axis=1   )

             self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]   )
             self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]   )
             self._check_zero_mean_unit_variance(input_features[2]   )

             # make sure that if max_length < longest -> then pad to max_length
             self.assertEqual(input_features.shape    ,       (3, 6, 24)   )
      def 			lowerCAmelCase__						(				self: int   ):
             import torch

             __lowerCamelCase   =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
             __lowerCamelCase   =						np.random.rand(1_00    ,       32   ).astype(np.floataa   )
             __lowerCamelCase   =						np_speech_inputs.tolist()

             for inputs in [py_speech_inputs, np_speech_inputs]:
                    __lowerCamelCase   =						feature_extractor.pad([{"""input_features""": inputs}]    ,       return_tensors="""np"""   )
                    self.assertTrue(np_processed.input_features.dtype == np.floataa   )
                    __lowerCamelCase   =						feature_extractor.pad([{"""input_features""": inputs}]    ,       return_tensors="""pt"""   )
                    self.assertTrue(pt_processed.input_features.dtype == torch.floataa   )
      def 			lowerCAmelCase__						(				self: List[Any]    ,       UpperCamelCase_: Tuple   ):
             from datasets import load_dataset

             __lowerCamelCase   =						load_dataset("""hf-internal-testing/librispeech_asr_dummy"""    ,       """clean"""    ,       split="""validation"""   )
             # automatic decoding with librispeech
             __lowerCamelCase   =						ds.sort("""id"""   ).select(range(__lowerCAmelCase   )   )[:num_samples]["""audio"""]

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




      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						np.array([
                 -1.5745, -1.7713, -1.7020, -1.6069, -1.2250, -1.1105, -0.9072, -0.8241,
                 -1.2310, -0.8098, -0.3320, -0.4101, -0.7985, -0.4996, -0.8213, -0.9128,
                 -1.0420, -1.1286, -1.0440, -0.7999, -0.8405, -1.2275, -1.5443, -1.4625,
             ]   )
             # fmt: on

             __lowerCamelCase   =						self._load_datasamples(1   )
             __lowerCamelCase   =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
             __lowerCamelCase   =						feature_extractor(__lowerCAmelCase    ,       return_tensors="""pt"""   ).input_features
             self.assertEquals(input_features.shape    ,       (1, 5_84, 24)   )
             self.assertTrue(np.allclose(input_features[0, 0, :30]    ,       __lowerCAmelCase    ,       atol=1E-4   )   )








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


_a 						=      logging.get_logger(__name__)

_a 						=      {
    "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json",
    # See all YOLOS models at https://huggingface.co/models?filter=yolos
}






class 					__A    (    lowerCAmelCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						"""yolos"""





			def __init__(    self       ,  __lowerCAmelCase=7_6_8       ,  __lowerCAmelCase=1_2       ,  __lowerCAmelCase=1_2       ,  __lowerCAmelCase=3_0_7_2       ,  __lowerCAmelCase="gelu"       ,  __lowerCAmelCase=0.0       ,  __lowerCAmelCase=0.0       ,  __lowerCAmelCase=0.02       ,  __lowerCAmelCase=1E-12       ,  __lowerCAmelCase=[5_1_2, 8_6_4]       ,  __lowerCAmelCase=1_6       ,  __lowerCAmelCase=3       ,  __lowerCAmelCase=True       ,  __lowerCAmelCase=1_0_0       ,  __lowerCAmelCase=True       ,  __lowerCAmelCase=False       ,  __lowerCAmelCase=1       ,  __lowerCAmelCase=5       ,  __lowerCAmelCase=2       ,  __lowerCAmelCase=5       ,  __lowerCAmelCase=2       ,  __lowerCAmelCase=0.1       ,  **__lowerCAmelCase       ,  ):



				'''simple docstring'''







				super().__init__(**__lowerCAmelCase							)

				lowerCamelCase__								=		hidden_size
				lowerCamelCase__								=		num_hidden_layers
				lowerCamelCase__								=		num_attention_heads
				lowerCamelCase__								=		intermediate_size
				lowerCamelCase__								=		hidden_act
				lowerCamelCase__								=		hidden_dropout_prob
				lowerCamelCase__								=		attention_probs_dropout_prob
				lowerCamelCase__								=		initializer_range
				lowerCamelCase__								=		layer_norm_eps
				lowerCamelCase__								=		image_size
				lowerCamelCase__								=		patch_size
				lowerCamelCase__								=		num_channels
				lowerCamelCase__								=		qkv_bias
				lowerCamelCase__								=		num_detection_tokens
				lowerCamelCase__								=		use_mid_position_embeddings
				lowerCamelCase__								=		auxiliary_loss
				# Hungarian matcher
				lowerCamelCase__								=		class_cost
				lowerCamelCase__								=		bbox_cost
				lowerCamelCase__								=		giou_cost
				# Loss coefficients
				lowerCamelCase__								=		bbox_loss_coefficient
				lowerCamelCase__								=		giou_loss_coefficient
				lowerCamelCase__								=		eos_coefficient






class 					__A    (    lowerCAmelCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						version.parse("""1.11""" )





			@property
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				return OrderedDict(
				    [
				        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
				    ]							)





			@property
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				return 1E-4





			@property
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				return 1_2



