





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

A__:    Union[str, Any]  	=	random.Random()



def 				lowerCAmelCase_ (							A_    ,A_=1.0    ,A_=None    ,A_=None):
    if rng is None:
        UpperCamelCase__:	Any					=     global_rng

    UpperCamelCase__:	int					=     []
    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        _a    (    unittest.TestCase):

      """simple docstring"""


      def __init__(		self:			str	,    __lowerCamelCase:			Dict	,    __lowerCamelCase:			Optional[int]=7	,    __lowerCamelCase:			int=400	,    __lowerCamelCase:			Optional[Any]=2000	,    __lowerCamelCase:			Tuple=24	,    __lowerCamelCase:			Dict=24	,    __lowerCamelCase:			Union[str, Any]=0.0	,    __lowerCamelCase:			Optional[int]=1_6000	,    __lowerCamelCase:			int=True	,    __lowerCamelCase:			List[str]=True	,    ):
          '''simple docstring'''


          UpperCamelCase__:	Any					=     parent
          UpperCamelCase__:	Optional[Any]					=     batch_size
          UpperCamelCase__:	List[Any]					=     min_seq_length
          UpperCamelCase__:	int					=     max_seq_length
          UpperCamelCase__:	Union[str, Any]					=     (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
          UpperCamelCase__:	Dict					=     feature_size
          UpperCamelCase__:	Optional[Any]					=     num_mel_bins
          UpperCamelCase__:	List[Any]					=     padding_value
          UpperCamelCase__:	List[Any]					=     sampling_rate
          UpperCamelCase__:	Dict					=     return_attention_mask
          UpperCamelCase__:	List[str]					=     do_normalize






      def 			UpperCAmelCase_       (		self:			List[Any]					):
          '''simple docstring'''


          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 			UpperCAmelCase_       (		self:			Dict	,    __lowerCamelCase:			Optional[int]=False	,    __lowerCamelCase:			Any=False					):
          '''simple docstring'''


          def _flatten(__lowerCamelCase:			Union[str, Any]					):
              return list(itertools.chain(*__lowerCamelCase					)					)

          if equal_length:
              UpperCamelCase__:	Tuple					=     [floats_list((self.max_seq_length, self.feature_size)					) for _ in range(self.batch_size					)]
          else:
              # make sure that inputs increase in size
              UpperCamelCase__:	List[str]					=     [
                  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__:	List[Any]					=     [np.asarray(__lowerCamelCase					) for x in speech_inputs]
          return speech_inputs




@require_torch
@require_torchaudio
class        _a    (    UpperCamelCase__						,			unittest.TestCase):

      """simple docstring"""


      UpperCamelCase__  		=       SpeechaTextFeatureExtractor if is_speech_available() else None






      def 			UpperCAmelCase_       (		self:			Dict					):
          '''simple docstring'''


          UpperCamelCase__:	int					=     SpeechaTextFeatureExtractionTester(self					)






      def 			UpperCAmelCase_       (		self:			Tuple	,    __lowerCamelCase:			Optional[Any]					):
          '''simple docstring'''


          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 			UpperCAmelCase_       (		self:			Optional[Any]					):
          '''simple docstring'''


          UpperCamelCase__:	Tuple					=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
          # create three inputs of length 800, 1000, and 1200
          UpperCamelCase__:	Any					=     [floats_list((1, x)					)[0] for x in range(800	,    1400	,    200					)]
          UpperCamelCase__:	Union[str, Any]					=     [np.asarray(__lowerCamelCase					) for speech_input in speech_inputs]

          # Test feature size
          UpperCamelCase__:	Optional[Any]					=     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
          UpperCamelCase__:	Dict					=     feature_extractor(speech_inputs[0]	,    return_tensors="np"					).input_features
          UpperCamelCase__:	Any					=     feature_extractor(np_speech_inputs[0]	,    return_tensors="np"					).input_features
          self.assertTrue(np.allclose(__lowerCamelCase	,    __lowerCamelCase	,    atol=1e-3					)					)

          # Test batched
          UpperCamelCase__:	Dict					=     feature_extractor(__lowerCamelCase	,    return_tensors="np"					).input_features
          UpperCamelCase__:	List[Any]					=     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.
          UpperCamelCase__:	Optional[int]					=     [floats_list((1, x)					)[0] for x in (800, 800, 800)]
          UpperCamelCase__:	List[Any]					=     np.asarray(__lowerCamelCase					)
          UpperCamelCase__:	str					=     feature_extractor(__lowerCamelCase	,    return_tensors="np"					).input_features
          UpperCamelCase__:	Dict					=     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 			UpperCAmelCase_       (		self:			Tuple					):
          '''simple docstring'''


          UpperCamelCase__:	Optional[Any]					=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
          UpperCamelCase__:	List[Any]					=     [floats_list((1, x)					)[0] for x in range(800	,    1400	,    200					)]

          UpperCamelCase__:	str					=     ["longest", "max_length", "do_not_pad"]
          UpperCamelCase__:	List[str]					=     [None, 16, None]
          for max_length, padding in zip(__lowerCamelCase	,    __lowerCamelCase					):
              UpperCamelCase__:	Optional[int]					=     feature_extractor(
                  __lowerCamelCase	,    padding=__lowerCamelCase	,    max_length=__lowerCamelCase	,    return_attention_mask=__lowerCamelCase					)
              UpperCamelCase__:	Optional[int]					=     inputs.input_features
              UpperCamelCase__:	int					=     inputs.attention_mask
              UpperCamelCase__:	Optional[Any]					=     [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 			UpperCAmelCase_       (		self:			str					):
          '''simple docstring'''


          UpperCamelCase__:	List[Any]					=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
          UpperCamelCase__:	List[str]					=     [floats_list((1, x)					)[0] for x in range(800	,    1400	,    200					)]

          UpperCamelCase__:	Dict					=     ["longest", "max_length", "do_not_pad"]
          UpperCamelCase__:	Tuple					=     [None, 16, None]
          for max_length, padding in zip(__lowerCamelCase	,    __lowerCamelCase					):
              UpperCamelCase__:	List[str]					=     feature_extractor(
                  __lowerCamelCase	,    max_length=__lowerCamelCase	,    padding=__lowerCamelCase	,    return_tensors="np"	,    return_attention_mask=__lowerCamelCase					)
              UpperCamelCase__:	Union[str, Any]					=     inputs.input_features
              UpperCamelCase__:	Any					=     inputs.attention_mask
              UpperCamelCase__:	str					=     [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 			UpperCAmelCase_       (		self:			str					):
          '''simple docstring'''


          UpperCamelCase__:	List[Any]					=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
          UpperCamelCase__:	int					=     [floats_list((1, x)					)[0] for x in range(800	,    1400	,    200					)]
          UpperCamelCase__:	str					=     feature_extractor(
              __lowerCamelCase	,    padding="max_length"	,    max_length=4	,    truncation=__lowerCamelCase	,    return_tensors="np"	,    return_attention_mask=__lowerCamelCase	,    )
          UpperCamelCase__:	str					=     inputs.input_features
          UpperCamelCase__:	str					=     inputs.attention_mask
          UpperCamelCase__:	Dict					=     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 			UpperCAmelCase_       (		self:			Dict					):
          '''simple docstring'''


          UpperCamelCase__:	int					=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
          UpperCamelCase__:	int					=     [floats_list((1, x)					)[0] for x in range(800	,    1400	,    200					)]
          UpperCamelCase__:	Dict					=     feature_extractor(
              __lowerCamelCase	,    padding="longest"	,    max_length=4	,    truncation=__lowerCamelCase	,    return_tensors="np"	,    return_attention_mask=__lowerCamelCase	,    )
          UpperCamelCase__:	Tuple					=     inputs.input_features
          UpperCamelCase__:	Any					=     inputs.attention_mask
          UpperCamelCase__:	str					=     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)					)

          UpperCamelCase__:	List[Any]					=     [floats_list((1, x)					)[0] for x in range(800	,    1400	,    200					)]
          UpperCamelCase__:	Any					=     feature_extractor(
              __lowerCamelCase	,    padding="longest"	,    max_length=16	,    truncation=__lowerCamelCase	,    return_tensors="np"	,    return_attention_mask=__lowerCamelCase	,    )
          UpperCamelCase__:	Dict					=     inputs.input_features
          UpperCamelCase__:	Tuple					=     inputs.attention_mask
          UpperCamelCase__:	Dict					=     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 			UpperCAmelCase_       (		self:			List[Any]					):
          '''simple docstring'''


          import torch

          UpperCamelCase__:	Optional[int]					=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
          UpperCamelCase__:	int					=     np.random.rand(100	,    32					).astype(np.floataa					)
          UpperCamelCase__:	Optional[int]					=     np_speech_inputs.tolist()

          for inputs in [py_speech_inputs, np_speech_inputs]:
              UpperCamelCase__:	Optional[int]					=     feature_extractor.pad([{"input_features": inputs}]	,    return_tensors="np"					)
              self.assertTrue(np_processed.input_features.dtype == np.floataa					)
              UpperCamelCase__:	Optional[Any]					=     feature_extractor.pad([{"input_features": inputs}]	,    return_tensors="pt"					)
              self.assertTrue(pt_processed.input_features.dtype == torch.floataa					)






      def 			UpperCAmelCase_       (		self:			Optional[Any]	,    __lowerCamelCase:			Dict					):
          '''simple docstring'''


          from datasets import load_dataset

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

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






      def 			UpperCAmelCase_       (		self:			Optional[Any]					):
          '''simple docstring'''


          UpperCamelCase__:	Any					=     np.array([
              -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241,
              -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128,
              -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625,
          ]					)
          # fmt: on

          UpperCamelCase__:	Tuple					=     self._load_datasamples(1					)
          UpperCamelCase__:	Dict					=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
          UpperCamelCase__:	Optional[Any]					=     feature_extractor(__lowerCamelCase	,    return_tensors="pt"					).input_features
          self.assertEquals(input_features.shape	,    (1, 584, 24)					)
          self.assertTrue(np.allclose(input_features[0, 0, :30]	,    __lowerCamelCase	,    atol=1e-4					)					)








import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


A__:    str  	=	logging.get_logger(__name__)

A__:    Union[str, Any]  	=	{
    '''SenseTime/deformable-detr''': '''https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json''',
    # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr
}




class        _a    (    UpperCamelCase__):

      """simple docstring"""


      UpperCamelCase__  		=       """deformable_detr"""
      UpperCamelCase__  		=       {
          """hidden_size""": """d_model""",
          """num_attention_heads""": """encoder_attention_heads""",
      }






      def __init__(		self:			Dict	,    __lowerCamelCase:			Optional[Any]=True	,    __lowerCamelCase:			List[str]=None	,    __lowerCamelCase:			Tuple=3	,    __lowerCamelCase:			Union[str, Any]=300	,    __lowerCamelCase:			Optional[Any]=1024	,    __lowerCamelCase:			Optional[int]=6	,    __lowerCamelCase:			Optional[int]=1024	,    __lowerCamelCase:			List[str]=8	,    __lowerCamelCase:			Any=6	,    __lowerCamelCase:			Tuple=1024	,    __lowerCamelCase:			List[str]=8	,    __lowerCamelCase:			Optional[int]=0.0	,    __lowerCamelCase:			Union[str, Any]=True	,    __lowerCamelCase:			List[str]="relu"	,    __lowerCamelCase:			Tuple=256	,    __lowerCamelCase:			Dict=0.1	,    __lowerCamelCase:			Optional[Any]=0.0	,    __lowerCamelCase:			Optional[int]=0.0	,    __lowerCamelCase:			Any=0.02	,    __lowerCamelCase:			int=1.0	,    __lowerCamelCase:			Optional[Any]=True	,    __lowerCamelCase:			str=False	,    __lowerCamelCase:			Any="sine"	,    __lowerCamelCase:			Optional[Any]="resnet50"	,    __lowerCamelCase:			Optional[int]=True	,    __lowerCamelCase:			int=False	,    __lowerCamelCase:			int=4	,    __lowerCamelCase:			int=4	,    __lowerCamelCase:			Any=4	,    __lowerCamelCase:			Any=False	,    __lowerCamelCase:			List[str]=300	,    __lowerCamelCase:			Dict=False	,    __lowerCamelCase:			str=1	,    __lowerCamelCase:			int=5	,    __lowerCamelCase:			str=2	,    __lowerCamelCase:			Dict=1	,    __lowerCamelCase:			Tuple=1	,    __lowerCamelCase:			Union[str, Any]=5	,    __lowerCamelCase:			Optional[Any]=2	,    __lowerCamelCase:			List[str]=0.1	,    __lowerCamelCase:			Dict=0.25	,    __lowerCamelCase:			Dict=False	,    **__lowerCamelCase:			str	,    ):
          '''simple docstring'''


          if backbone_config is not None and use_timm_backbone:
              raise ValueError("You can't specify both `backbone_config` and `use_timm_backbone`."					)

          if not use_timm_backbone:
              if backbone_config is None:
                  logger.info("`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."					)
                  UpperCamelCase__:	Union[str, Any]					=     CONFIG_MAPPING["resnet"](out_features=["stage4"]					)
              elif isinstance(__lowerCamelCase	,    __lowerCamelCase					):
                  UpperCamelCase__:	Tuple					=     backbone_config.get("model_type"					)
                  UpperCamelCase__:	Optional[int]					=     CONFIG_MAPPING[backbone_model_type]
                  UpperCamelCase__:	Tuple					=     config_class.from_dict(__lowerCamelCase					)
          UpperCamelCase__:	Dict					=     use_timm_backbone
          UpperCamelCase__:	Any					=     backbone_config
          UpperCamelCase__:	Optional[int]					=     num_channels
          UpperCamelCase__:	int					=     num_queries
          UpperCamelCase__:	List[str]					=     max_position_embeddings
          UpperCamelCase__:	Dict					=     d_model
          UpperCamelCase__:	List[Any]					=     encoder_ffn_dim
          UpperCamelCase__:	Union[str, Any]					=     encoder_layers
          UpperCamelCase__:	Tuple					=     encoder_attention_heads
          UpperCamelCase__:	Tuple					=     decoder_ffn_dim
          UpperCamelCase__:	Optional[int]					=     decoder_layers
          UpperCamelCase__:	int					=     decoder_attention_heads
          UpperCamelCase__:	Optional[Any]					=     dropout
          UpperCamelCase__:	List[str]					=     attention_dropout
          UpperCamelCase__:	List[Any]					=     activation_dropout
          UpperCamelCase__:	List[Any]					=     activation_function
          UpperCamelCase__:	Union[str, Any]					=     init_std
          UpperCamelCase__:	List[str]					=     init_xavier_std
          UpperCamelCase__:	Optional[Any]					=     encoder_layerdrop
          UpperCamelCase__:	List[str]					=     auxiliary_loss
          UpperCamelCase__:	Optional[int]					=     position_embedding_type
          UpperCamelCase__:	Optional[Any]					=     backbone
          UpperCamelCase__:	Any					=     use_pretrained_backbone
          UpperCamelCase__:	Union[str, Any]					=     dilation
          # deformable attributes
          UpperCamelCase__:	Union[str, Any]					=     num_feature_levels
          UpperCamelCase__:	Optional[Any]					=     encoder_n_points
          UpperCamelCase__:	Tuple					=     decoder_n_points
          UpperCamelCase__:	Any					=     two_stage
          UpperCamelCase__:	Optional[int]					=     two_stage_num_proposals
          UpperCamelCase__:	str					=     with_box_refine
          if two_stage is True and with_box_refine is False:
              raise ValueError("If two_stage is True, with_box_refine must be True."					)
          # Hungarian matcher
          UpperCamelCase__:	Any					=     class_cost
          UpperCamelCase__:	str					=     bbox_cost
          UpperCamelCase__:	List[str]					=     giou_cost
          # Loss coefficients
          UpperCamelCase__:	Optional[Any]					=     mask_loss_coefficient
          UpperCamelCase__:	List[Any]					=     dice_loss_coefficient
          UpperCamelCase__:	Optional[Any]					=     bbox_loss_coefficient
          UpperCamelCase__:	List[Any]					=     giou_loss_coefficient
          UpperCamelCase__:	Dict					=     eos_coefficient
          UpperCamelCase__:	List[Any]					=     focal_alpha
          UpperCamelCase__:	Dict					=     disable_custom_kernels
          super().__init__(is_encoder_decoder=__lowerCamelCase	,    **__lowerCamelCase					)






      @property
      def 			UpperCAmelCase_       (		self:			Union[str, Any]					):
          '''simple docstring'''


          return self.encoder_attention_heads






      @property
      def 			UpperCAmelCase_       (		self:			Any					):
          '''simple docstring'''


          return self.d_model






      def 			UpperCAmelCase_       (		self:			int					):
          '''simple docstring'''


          UpperCamelCase__:	Optional[int]					=     copy.deepcopy(self.__dict__					)
          if self.backbone_config is not None:
              UpperCamelCase__:	Tuple					=     self.backbone_config.to_dict()
          UpperCamelCase__:	Optional[int]					=     self.__class__.model_type
          return output

