






'''simple docstring'''

import itertools
import random
import unittest

import numpy as np

from transformers import BatchFeature, SpeechTaFeatureExtractor
from transformers.testing_utils import require_torch
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
     import torch


SCREAMING_SNAKE_CASE__     =       random.Random()







def    lowercase__			(				__UpperCamelCase		,						__UpperCamelCase=1.0		,						__UpperCamelCase=None		,						__UpperCamelCase=None   )->			Any:
      if rng is None:
            UpperCamelCase      = global_rng

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

      return values






@require_torch
class     a_						(      unittest.TestCase    ):

  def __init__(				self      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE=7      ,		_SCREAMING_SNAKE_CASE=400      ,		_SCREAMING_SNAKE_CASE=2000      ,		_SCREAMING_SNAKE_CASE=1      ,		_SCREAMING_SNAKE_CASE=0.0      ,		_SCREAMING_SNAKE_CASE=16000      ,		_SCREAMING_SNAKE_CASE=True      ,		_SCREAMING_SNAKE_CASE=80      ,		_SCREAMING_SNAKE_CASE=16      ,		_SCREAMING_SNAKE_CASE=64      ,		_SCREAMING_SNAKE_CASE="hann_window"      ,		_SCREAMING_SNAKE_CASE=80      ,		_SCREAMING_SNAKE_CASE=7600      ,		_SCREAMING_SNAKE_CASE=1e-10      ,		_SCREAMING_SNAKE_CASE=True      ,		)  ->				Tuple:



        """simple docstring"""






        UpperCamelCase      = parent
        UpperCamelCase      = batch_size
        UpperCamelCase      = min_seq_length
        UpperCamelCase      = max_seq_length
        UpperCamelCase      = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
        UpperCamelCase      = feature_size
        UpperCamelCase      = padding_value
        UpperCamelCase      = sampling_rate
        UpperCamelCase      = do_normalize
        UpperCamelCase      = num_mel_bins
        UpperCamelCase      = hop_length
        UpperCamelCase      = win_length
        UpperCamelCase      = win_function
        UpperCamelCase      = fmin
        UpperCamelCase      = fmax
        UpperCamelCase      = mel_floor
        UpperCamelCase      = return_attention_mask

  def   A__						(				self				)  ->				str:



        """simple docstring"""






        return {
            "feature_size": self.feature_size,
            "padding_value": self.padding_value,
            "sampling_rate": self.sampling_rate,
            "do_normalize": self.do_normalize,
            "num_mel_bins": self.num_mel_bins,
            "hop_length": self.hop_length,
            "win_length": self.win_length,
            "win_function": self.win_function,
            "fmin": self.fmin,
            "fmax": self.fmax,
            "mel_floor": self.mel_floor,
            "return_attention_mask": self.return_attention_mask,
        }

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE=False      ,		_SCREAMING_SNAKE_CASE=False				)  ->				List[str]:



        """simple docstring"""






        def _flatten(_SCREAMING_SNAKE_CASE				):
              return list(itertools.chain(*_SCREAMING_SNAKE_CASE				)				)

        if equal_length:
              UpperCamelCase      = floats_list((self.batch_size, self.max_seq_length)				)
        else:
              # make sure that inputs increase in size
              UpperCamelCase      = [
                  _flatten(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      = [np.asarray(_SCREAMING_SNAKE_CASE				) for x in speech_inputs]

        return speech_inputs


  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE=False      ,		_SCREAMING_SNAKE_CASE=False				)  ->				Union[str, Any]:



        """simple docstring"""






        if equal_length:
              UpperCamelCase      = [floats_list((self.max_seq_length, self.num_mel_bins)				) for _ in range(self.batch_size				)]
        else:
              # make sure that inputs increase in size
              UpperCamelCase      = [
                  floats_list((x, self.num_mel_bins)				)
                  for x in range(self.min_seq_length      ,		self.max_seq_length      ,		self.seq_length_diff				)
              ]

        if numpify:
              UpperCamelCase      = [np.asarray(_SCREAMING_SNAKE_CASE				) for x in speech_inputs]

        return speech_inputs






@require_torch
class     a_						(      lowerCamelCase     ,	unittest.TestCase    ):
  lowercase							=				SpeechTaFeatureExtractor

  def   A__						(				self				)  ->				List[Any]:



        """simple docstring"""






        UpperCamelCase      = SpeechTaFeatureExtractionTester(self				)

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE				)  ->				Tuple:



        """simple docstring"""






        self.assertTrue(np.all(np.mean(_SCREAMING_SNAKE_CASE      ,		axis=0				) < 1e-3				)				)
        self.assertTrue(np.all(np.abs(np.var(_SCREAMING_SNAKE_CASE      ,		axis=0				) - 1				) < 1e-3				)				)

  def   A__						(				self				)  ->				Dict:



        """simple docstring"""






        UpperCamelCase      = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
        # create three inputs of length 800, 1000, and 1200
        UpperCamelCase      = [floats_list((1, x)				)[0] for x in range(800      ,		1400      ,		200				)]
        UpperCamelCase      = [np.asarray(_SCREAMING_SNAKE_CASE				) for speech_input in speech_inputs]

        # Test not batched input
        UpperCamelCase      = feat_extract(speech_inputs[0]      ,		return_tensors="""np"""				).input_values
        UpperCamelCase      = feat_extract(np_speech_inputs[0]      ,		return_tensors="""np"""				).input_values
        self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		atol=1e-3				)				)

        # Test batched
        UpperCamelCase      = feat_extract(_SCREAMING_SNAKE_CASE      ,		return_tensors="""np"""				).input_values
        UpperCamelCase      = feat_extract(_SCREAMING_SNAKE_CASE      ,		return_tensors="""np"""				).input_values
        for enc_seq_a, enc_seq_a in zip(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				):
              self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		atol=1e-3				)				)

  def   A__						(				self				)  ->				str:



        """simple docstring"""






        UpperCamelCase      = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
        UpperCamelCase      = [floats_list((1, x)				)[0] for x in range(800      ,		1400      ,		200				)]

        UpperCamelCase      = ["""longest""", """max_length""", """do_not_pad"""]
        UpperCamelCase      = [None, 1600, None]
        for max_length, padding in zip(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				):
              UpperCamelCase      = feat_extract(_SCREAMING_SNAKE_CASE      ,		padding=_SCREAMING_SNAKE_CASE      ,		max_length=_SCREAMING_SNAKE_CASE      ,		return_tensors="""np"""				)
              UpperCamelCase      = processed.input_values

              self._check_zero_mean_unit_variance(input_values[0][:800]				)
              self.assertTrue(input_values[0][800:].sum() < 1e-6				)
              self._check_zero_mean_unit_variance(input_values[1][:1000]				)
              self.assertTrue(input_values[0][1000:].sum() < 1e-6				)
              self._check_zero_mean_unit_variance(input_values[2][:1200]				)

  def   A__						(				self				)  ->				List[str]:



        """simple docstring"""






        UpperCamelCase      = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
        UpperCamelCase      = range(800      ,		1400      ,		200				)
        UpperCamelCase      = [floats_list((1, x)				)[0] for x in lengths]

        UpperCamelCase      = ["""longest""", """max_length""", """do_not_pad"""]
        UpperCamelCase      = [None, 1600, None]

        for max_length, padding in zip(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				):
              UpperCamelCase      = feat_extract(_SCREAMING_SNAKE_CASE      ,		max_length=_SCREAMING_SNAKE_CASE      ,		padding=_SCREAMING_SNAKE_CASE				)
              UpperCamelCase      = processed.input_values

              self._check_zero_mean_unit_variance(input_values[0][:800]				)
              self._check_zero_mean_unit_variance(input_values[1][:1000]				)
              self._check_zero_mean_unit_variance(input_values[2][:1200]				)

  def   A__						(				self				)  ->				str:



        """simple docstring"""






        UpperCamelCase      = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
        UpperCamelCase      = [floats_list((1, x)				)[0] for x in range(800      ,		1400      ,		200				)]
        UpperCamelCase      = feat_extract(
            _SCREAMING_SNAKE_CASE      ,		truncation=_SCREAMING_SNAKE_CASE      ,		max_length=1000      ,		padding="""max_length"""      ,		return_tensors="""np"""				)
        UpperCamelCase      = processed.input_values

        self._check_zero_mean_unit_variance(input_values[0, :800]				)
        self._check_zero_mean_unit_variance(input_values[1]				)
        self._check_zero_mean_unit_variance(input_values[2]				)

  def   A__						(				self				)  ->				Tuple:



        """simple docstring"""






        UpperCamelCase      = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
        UpperCamelCase      = [floats_list((1, x)				)[0] for x in range(800      ,		1400      ,		200				)]
        UpperCamelCase      = feat_extract(
            _SCREAMING_SNAKE_CASE      ,		truncation=_SCREAMING_SNAKE_CASE      ,		max_length=1000      ,		padding="""longest"""      ,		return_tensors="""np"""				)
        UpperCamelCase      = processed.input_values

        self._check_zero_mean_unit_variance(input_values[0, :800]				)
        self._check_zero_mean_unit_variance(input_values[1, :1000]				)
        self._check_zero_mean_unit_variance(input_values[2]				)

        # make sure that if max_length < longest -> then pad to max_length
        self.assertTrue(input_values.shape == (3, 1000)				)

        UpperCamelCase      = [floats_list((1, x)				)[0] for x in range(800      ,		1400      ,		200				)]
        UpperCamelCase      = feat_extract(
            _SCREAMING_SNAKE_CASE      ,		truncation=_SCREAMING_SNAKE_CASE      ,		max_length=2000      ,		padding="""longest"""      ,		return_tensors="""np"""				)
        UpperCamelCase      = processed.input_values

        self._check_zero_mean_unit_variance(input_values[0, :800]				)
        self._check_zero_mean_unit_variance(input_values[1, :1000]				)
        self._check_zero_mean_unit_variance(input_values[2]				)

        # make sure that if max_length > longest -> then pad to longest
        self.assertTrue(input_values.shape == (3, 1200)				)

  def   A__						(				self				)  ->				Union[str, Any]:



        """simple docstring"""






        UpperCamelCase      = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
        UpperCamelCase      = np.random.rand(100				).astype(np.floataa				)
        UpperCamelCase      = np_speech_inputs.tolist()

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

  def   A__						(				self				)  ->				Tuple:



        """simple docstring"""






        UpperCamelCase      = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
        # create three inputs of length 800, 1000, and 1200
        UpperCamelCase      = [floats_list((1, x)				)[0] for x in range(800      ,		1400      ,		200				)]
        UpperCamelCase      = [np.asarray(_SCREAMING_SNAKE_CASE				) for speech_input in speech_inputs]

        # Test feature size
        UpperCamelCase      = feature_extractor(audio_target=_SCREAMING_SNAKE_CASE      ,		padding=_SCREAMING_SNAKE_CASE      ,		return_tensors="""np"""				).input_values
        self.assertTrue(input_values.ndim == 3				)
        self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins				)

        # Test not batched input
        UpperCamelCase      = feature_extractor(speech_inputs[0]      ,		return_tensors="""np"""				).input_values
        UpperCamelCase      = feature_extractor(np_speech_inputs[0]      ,		return_tensors="""np"""				).input_values
        self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		atol=1e-3				)				)

        # Test batched
        UpperCamelCase      = feature_extractor(_SCREAMING_SNAKE_CASE      ,		return_tensors="""np"""				).input_values
        UpperCamelCase      = feature_extractor(_SCREAMING_SNAKE_CASE      ,		return_tensors="""np"""				).input_values
        for enc_seq_a, enc_seq_a in zip(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				):
              self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		atol=1e-3				)				)

        # Test 2-D numpy arrays are batched.
        UpperCamelCase      = [floats_list((1, x)				)[0] for x in (800, 800, 800)]
        UpperCamelCase      = np.asarray(_SCREAMING_SNAKE_CASE				)
        UpperCamelCase      = feature_extractor(_SCREAMING_SNAKE_CASE      ,		return_tensors="""np"""				).input_values
        UpperCamelCase      = feature_extractor(_SCREAMING_SNAKE_CASE      ,		return_tensors="""np"""				).input_values
        for enc_seq_a, enc_seq_a in zip(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				):
              self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		atol=1e-3				)				)

  def   A__						(				self				)  ->				Any:



        """simple docstring"""






        UpperCamelCase      = self.feat_extract_tester.prepare_inputs_for_target()
        UpperCamelCase      = self.feature_extraction_class(**self.feat_extract_dict				)
        UpperCamelCase      = feat_extract.model_input_names[0]

        UpperCamelCase      = BatchFeature({input_name: speech_inputs}				)

        self.assertTrue(all(len(_SCREAMING_SNAKE_CASE				) == len(_SCREAMING_SNAKE_CASE				) for x, y in zip(_SCREAMING_SNAKE_CASE      ,		processed_features[input_name]				)				)				)

        UpperCamelCase      = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_SCREAMING_SNAKE_CASE				)
        UpperCamelCase      = BatchFeature({input_name: speech_inputs}      ,		tensor_type="""np"""				)

        UpperCamelCase      = processed_features[input_name]

        if len(batch_features_input.shape				) < 3:
              UpperCamelCase      = batch_features_input[:, :, None]

        self.assertTrue(
            batch_features_input.shape
            == (self.feat_extract_tester.batch_size, len(speech_inputs[0]				), self.feat_extract_tester.num_mel_bins)				)

  @require_torch
  def   A__						(				self				)  ->				Any:



        """simple docstring"""






        UpperCamelCase      = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_SCREAMING_SNAKE_CASE				)
        UpperCamelCase      = self.feature_extraction_class(**self.feat_extract_dict				)
        UpperCamelCase      = feat_extract.model_input_names[0]

        UpperCamelCase      = BatchFeature({input_name: speech_inputs}      ,		tensor_type="""pt"""				)

        UpperCamelCase      = processed_features[input_name]

        if len(batch_features_input.shape				) < 3:
              UpperCamelCase      = batch_features_input[:, :, None]

        self.assertTrue(
            batch_features_input.shape
            == (self.feat_extract_tester.batch_size, len(speech_inputs[0]				), self.feat_extract_tester.num_mel_bins)				)

  @require_torch
  def   A__						(				self				)  ->				List[Any]:



        """simple docstring"""






        UpperCamelCase      = self.feature_extraction_class(**self.feat_extract_dict				)
        UpperCamelCase      = self.feat_extract_tester.prepare_inputs_for_target()
        UpperCamelCase      = feat_extract.model_input_names[0]

        UpperCamelCase      = BatchFeature({input_name: speech_inputs}				)

        UpperCamelCase      = feat_extract.num_mel_bins  # hack!

        UpperCamelCase      = feat_extract.pad(_SCREAMING_SNAKE_CASE      ,		padding="""longest"""      ,		return_tensors="""np"""				)[input_name]
        UpperCamelCase      = feat_extract.pad(_SCREAMING_SNAKE_CASE      ,		padding="""longest"""      ,		return_tensors="""pt"""				)[input_name]

        self.assertTrue(abs(input_np.astype(np.floataa				).sum() - input_pt.numpy().astype(np.floataa				).sum()				) < 1e-2				)

  def   A__						(				self				)  ->				Dict:



        """simple docstring"""






        UpperCamelCase      = self.feat_extract_dict
        UpperCamelCase      = True
        UpperCamelCase      = self.feature_extraction_class(**_SCREAMING_SNAKE_CASE				)
        UpperCamelCase      = self.feat_extract_tester.prepare_inputs_for_target()
        UpperCamelCase      = [len(_SCREAMING_SNAKE_CASE				) for x in speech_inputs]
        UpperCamelCase      = feat_extract.model_input_names[0]

        UpperCamelCase      = BatchFeature({input_name: speech_inputs}				)

        UpperCamelCase      = feat_extract.num_mel_bins  # hack!

        UpperCamelCase      = feat_extract.pad(_SCREAMING_SNAKE_CASE      ,		padding="""longest"""      ,		return_tensors="""np"""				)
        self.assertIn("""attention_mask"""      ,		_SCREAMING_SNAKE_CASE				)
        self.assertListEqual(list(processed.attention_mask.shape				)      ,		list(processed[input_name].shape[:2]				)				)
        self.assertListEqual(processed.attention_mask.sum(-1				).tolist()      ,		_SCREAMING_SNAKE_CASE				)

  def   A__						(				self				)  ->				Dict:



        """simple docstring"""






        UpperCamelCase      = self.feat_extract_dict
        UpperCamelCase      = True
        UpperCamelCase      = self.feature_extraction_class(**_SCREAMING_SNAKE_CASE				)
        UpperCamelCase      = self.feat_extract_tester.prepare_inputs_for_target()
        UpperCamelCase      = [len(_SCREAMING_SNAKE_CASE				) for x in speech_inputs]
        UpperCamelCase      = feat_extract.model_input_names[0]

        UpperCamelCase      = BatchFeature({input_name: speech_inputs}				)
        UpperCamelCase      = min(_SCREAMING_SNAKE_CASE				)

        UpperCamelCase      = feat_extract.num_mel_bins  # hack!

        UpperCamelCase      = feat_extract.pad(
            _SCREAMING_SNAKE_CASE      ,		padding="""max_length"""      ,		max_length=_SCREAMING_SNAKE_CASE      ,		truncation=_SCREAMING_SNAKE_CASE      ,		return_tensors="""np"""				)
        self.assertIn("""attention_mask"""      ,		_SCREAMING_SNAKE_CASE				)
        self.assertListEqual(
            list(processed_pad.attention_mask.shape				)      ,		[processed_pad[input_name].shape[0], max_length]				)
        self.assertListEqual(
            processed_pad.attention_mask[:, :max_length].sum(-1				).tolist()      ,		[max_length for x in speech_inputs]				)

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE				)  ->				int:



        """simple docstring"""






        from datasets import load_dataset

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

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

  def   A__						(				self				)  ->				Union[str, Any]:



        """simple docstring"""






        UpperCamelCase      = torch.tensor(
            [2.38_04e-03, 2.07_52e-03, 1.98_36e-03, 2.10_57e-03, 1.61_74e-03,
             3.05_18e-04, 9.15_53e-05, 3.35_69e-04, 9.76_56e-04, 1.83_11e-03,
             2.01_42e-03, 2.10_57e-03, 1.73_95e-03, 4.57_76e-04, -3.96_73e-04,
             4.57_76e-04, 1.00_71e-03, 9.15_53e-05, 4.88_28e-04, 1.15_97e-03,
             7.32_42e-04, 9.46_04e-04, 1.80_05e-03, 1.83_11e-03, 8.85_01e-04,
             4.27_25e-04, 4.88_28e-04, 7.32_42e-04, 1.09_86e-03, 2.10_57e-03]				)
        # fmt: on

        UpperCamelCase      = self._load_datasamples(1				)
        UpperCamelCase      = SpeechTaFeatureExtractor()
        UpperCamelCase      = feature_extractor(_SCREAMING_SNAKE_CASE      ,		return_tensors="""pt"""				).input_values
        self.assertEquals(input_values.shape      ,		(1, 93680)				)
        self.assertTrue(torch.allclose(input_values[0, :30]      ,		_SCREAMING_SNAKE_CASE      ,		atol=1e-6				)				)


  def   A__						(				self				)  ->				Any:



        """simple docstring"""






        UpperCamelCase      = torch.tensor(
            [-2.6_8_7_0, -3.0_1_0_4, -3.1_3_5_6, -3.5_3_5_2, -3.0_0_4_4, -3.0_3_5_3, -3.4_7_1_9, -3.6_7_7_7,
             -3.1_5_2_0, -2.9_4_3_5, -2.6_5_5_3, -2.8_7_9_5, -2.9_9_4_4, -2.5_9_2_1, -3.0_2_7_9, -3.0_3_8_6,
             -3.0_8_6_4, -3.1_2_9_1, -3.2_3_5_3, -2.7_4_4_4, -2.6_8_3_1, -2.7_2_8_7, -3.1_7_6_1, -3.1_5_7_1,
             -3.2_7_2_6, -3.0_5_8_2, -3.1_0_0_7, -3.4_5_3_3, -3.4_6_9_5, -3.0_9_9_8]				)
        # fmt: on

        UpperCamelCase      = self._load_datasamples(1				)
        UpperCamelCase      = SpeechTaFeatureExtractor()
        UpperCamelCase      = feature_extractor(audio_target=_SCREAMING_SNAKE_CASE      ,		return_tensors="""pt"""				).input_values
        self.assertEquals(input_values.shape      ,		(1, 366, 80)				)
        self.assertTrue(torch.allclose(input_values[0, 0, :30]      ,		_SCREAMING_SNAKE_CASE      ,		atol=1e-4				)				)








'''simple docstring'''

import collections
import inspect
import unittest

from transformers import FocalNetConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch
     from torch import nn

     from transformers import (
         FocalNetBackbone,
         FocalNetForImageClassification,
         FocalNetForMaskedImageModeling,
         FocalNetModel,
     )
     from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
     from PIL import Image

     from transformers import AutoImageProcessor






class     a_						:

  def __init__(				self      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE=13      ,		_SCREAMING_SNAKE_CASE=32      ,		_SCREAMING_SNAKE_CASE=2      ,		_SCREAMING_SNAKE_CASE=3      ,		_SCREAMING_SNAKE_CASE=16      ,		_SCREAMING_SNAKE_CASE=[32, 64, 128]      ,		_SCREAMING_SNAKE_CASE=[1, 2, 1]      ,		_SCREAMING_SNAKE_CASE=[2, 2, 4]      ,		_SCREAMING_SNAKE_CASE=2      ,		_SCREAMING_SNAKE_CASE=2.0      ,		_SCREAMING_SNAKE_CASE=True      ,		_SCREAMING_SNAKE_CASE=0.0      ,		_SCREAMING_SNAKE_CASE=0.0      ,		_SCREAMING_SNAKE_CASE=0.1      ,		_SCREAMING_SNAKE_CASE="gelu"      ,		_SCREAMING_SNAKE_CASE=False      ,		_SCREAMING_SNAKE_CASE=True      ,		_SCREAMING_SNAKE_CASE=0.0_2      ,		_SCREAMING_SNAKE_CASE=1e-5      ,		_SCREAMING_SNAKE_CASE=True      ,		_SCREAMING_SNAKE_CASE=None      ,		_SCREAMING_SNAKE_CASE=True      ,		_SCREAMING_SNAKE_CASE=10      ,		_SCREAMING_SNAKE_CASE=8      ,		_SCREAMING_SNAKE_CASE=["stage1", "stage2"]      ,		_SCREAMING_SNAKE_CASE=[1, 2]      ,		)  ->				Optional[Any]:



        """simple docstring"""






        UpperCamelCase      = parent
        UpperCamelCase      = batch_size
        UpperCamelCase      = image_size
        UpperCamelCase      = patch_size
        UpperCamelCase      = num_channels
        UpperCamelCase      = embed_dim
        UpperCamelCase      = hidden_sizes
        UpperCamelCase      = depths
        UpperCamelCase      = num_heads
        UpperCamelCase      = window_size
        UpperCamelCase      = mlp_ratio
        UpperCamelCase      = qkv_bias
        UpperCamelCase      = hidden_dropout_prob
        UpperCamelCase      = attention_probs_dropout_prob
        UpperCamelCase      = drop_path_rate
        UpperCamelCase      = hidden_act
        UpperCamelCase      = use_absolute_embeddings
        UpperCamelCase      = patch_norm
        UpperCamelCase      = layer_norm_eps
        UpperCamelCase      = initializer_range
        UpperCamelCase      = is_training
        UpperCamelCase      = scope
        UpperCamelCase      = use_labels
        UpperCamelCase      = type_sequence_label_size
        UpperCamelCase      = encoder_stride
        UpperCamelCase      = out_features
        UpperCamelCase      = out_indices

  def   A__						(				self				)  ->				Any:



        """simple docstring"""






        UpperCamelCase      = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

        UpperCamelCase      = None
        if self.use_labels:
              UpperCamelCase      = ids_tensor([self.batch_size]      ,		self.type_sequence_label_size				)

        UpperCamelCase      = self.get_config()

        return config, pixel_values, labels

  def   A__						(				self				)  ->				str:



        """simple docstring"""






        return FocalNetConfig(
            image_size=self.image_size      ,		patch_size=self.patch_size      ,		num_channels=self.num_channels      ,		embed_dim=self.embed_dim      ,		hidden_sizes=self.hidden_sizes      ,		depths=self.depths      ,		num_heads=self.num_heads      ,		window_size=self.window_size      ,		mlp_ratio=self.mlp_ratio      ,		qkv_bias=self.qkv_bias      ,		hidden_dropout_prob=self.hidden_dropout_prob      ,		attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,		drop_path_rate=self.drop_path_rate      ,		hidden_act=self.hidden_act      ,		use_absolute_embeddings=self.use_absolute_embeddings      ,		path_norm=self.patch_norm      ,		layer_norm_eps=self.layer_norm_eps      ,		initializer_range=self.initializer_range      ,		encoder_stride=self.encoder_stride      ,		out_features=self.out_features      ,		out_indices=self.out_indices      ,		)

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)  ->				Tuple:



        """simple docstring"""






        UpperCamelCase      = FocalNetModel(config=_SCREAMING_SNAKE_CASE				)
        model.to(_SCREAMING_SNAKE_CASE				)
        model.eval()
        UpperCamelCase      = model(_SCREAMING_SNAKE_CASE				)

        UpperCamelCase      = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths				) - 1))
        UpperCamelCase      = int(config.embed_dim * 2 ** (len(config.depths				) - 1)				)

        self.parent.assertEqual(result.last_hidden_state.shape      ,		(self.batch_size, expected_seq_len, expected_dim)				)

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)  ->				List[Any]:



        """simple docstring"""






        UpperCamelCase      = FocalNetBackbone(config=_SCREAMING_SNAKE_CASE				)
        model.to(_SCREAMING_SNAKE_CASE				)
        model.eval()
        UpperCamelCase      = model(_SCREAMING_SNAKE_CASE				)

        # verify feature maps
        self.parent.assertEqual(len(result.feature_maps				)      ,		len(config.out_features				)				)
        self.parent.assertListEqual(list(result.feature_maps[0].shape				)      ,		[self.batch_size, self.image_size, 8, 8]				)

        # verify channels
        self.parent.assertEqual(len(model.channels				)      ,		len(config.out_features				)				)
        self.parent.assertListEqual(model.channels      ,		config.hidden_sizes[:-1]				)

        # verify backbone works with out_features=None
        UpperCamelCase      = None
        UpperCamelCase      = FocalNetBackbone(config=_SCREAMING_SNAKE_CASE				)
        model.to(_SCREAMING_SNAKE_CASE				)
        model.eval()
        UpperCamelCase      = model(_SCREAMING_SNAKE_CASE				)

        # verify feature maps
        self.parent.assertEqual(len(result.feature_maps				)      ,		1				)
        self.parent.assertListEqual(list(result.feature_maps[0].shape				)      ,		[self.batch_size, self.image_size * 2, 4, 4]				)

        # verify channels
        self.parent.assertEqual(len(model.channels				)      ,		1				)
        self.parent.assertListEqual(model.channels      ,		[config.hidden_sizes[-1]]				)

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)  ->				List[Any]:



        """simple docstring"""






        UpperCamelCase      = FocalNetForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE				)
        model.to(_SCREAMING_SNAKE_CASE				)
        model.eval()
        UpperCamelCase      = model(_SCREAMING_SNAKE_CASE				)
        self.parent.assertEqual(
            result.reconstruction.shape      ,		(self.batch_size, self.num_channels, self.image_size, self.image_size)				)

        # test greyscale images
        UpperCamelCase      = 1
        UpperCamelCase      = FocalNetForMaskedImageModeling(_SCREAMING_SNAKE_CASE				)
        model.to(_SCREAMING_SNAKE_CASE				)
        model.eval()

        UpperCamelCase      = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
        UpperCamelCase      = model(_SCREAMING_SNAKE_CASE				)
        self.parent.assertEqual(result.reconstruction.shape      ,		(self.batch_size, 1, self.image_size, self.image_size)				)

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)  ->				List[str]:



        """simple docstring"""






        UpperCamelCase      = self.type_sequence_label_size
        UpperCamelCase      = FocalNetForImageClassification(_SCREAMING_SNAKE_CASE				)
        model.to(_SCREAMING_SNAKE_CASE				)
        model.eval()
        UpperCamelCase      = model(_SCREAMING_SNAKE_CASE      ,		labels=_SCREAMING_SNAKE_CASE				)
        self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.type_sequence_label_size)				)

        # test greyscale images
        UpperCamelCase      = 1
        UpperCamelCase      = FocalNetForImageClassification(_SCREAMING_SNAKE_CASE				)
        model.to(_SCREAMING_SNAKE_CASE				)
        model.eval()

        UpperCamelCase      = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
        UpperCamelCase      = model(_SCREAMING_SNAKE_CASE				)
        self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.type_sequence_label_size)				)


  def   A__						(				self				)  ->				List[str]:



        """simple docstring"""






        UpperCamelCase      = self.prepare_config_and_inputs()

        UpperCamelCase  ,UpperCamelCase  ,UpperCamelCase      = config_and_inputs
        UpperCamelCase      = {"""pixel_values""": pixel_values}
        return config, inputs_dict






@require_torch
class     a_						(      lowerCamelCase     ,	lowerCamelCase     ,	unittest.TestCase    ):
  lowercase							=				(
      (
          FocalNetModel,
          FocalNetForImageClassification,
          FocalNetForMaskedImageModeling,
          FocalNetBackbone,
      )
      if is_torch_available()
      else ()
  )
  lowercase							=				(
      {"""feature-extraction""": FocalNetModel, """image-classification""": FocalNetForImageClassification}
      if is_torch_available()
      else {}
  )
  lowercase							=				False

  lowercase							=				False
  lowercase							=				False
  lowercase							=				False
  lowercase							=				False

  def   A__						(				self				)  ->				int:



        """simple docstring"""






        UpperCamelCase      = FocalNetModelTester(self				)
        UpperCamelCase      = ConfigTester(self      ,		config_class=_SCREAMING_SNAKE_CASE      ,		embed_dim=37      ,		has_text_modality=_SCREAMING_SNAKE_CASE				)

  def   A__						(				self				)  ->				List[Any]:



        """simple docstring"""






        self.create_and_test_config_common_properties()
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.create_and_test_config_with_num_labels()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()

  def   A__						(				self				)  ->				Tuple:



        """simple docstring"""






        return

  def   A__						(				self				)  ->				str:



        """simple docstring"""






        UpperCamelCase      = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE				)

  def   A__						(				self				)  ->				List[str]:



        """simple docstring"""






        UpperCamelCase      = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_backbone(*_SCREAMING_SNAKE_CASE				)

  def   A__						(				self				)  ->				Optional[Any]:



        """simple docstring"""






        UpperCamelCase      = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*_SCREAMING_SNAKE_CASE				)

  def   A__						(				self				)  ->				Union[str, Any]:



        """simple docstring"""






        UpperCamelCase      = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE				)

  @unittest.skip(reason="""FocalNet does not use inputs_embeds"""				)
  def   A__						(				self				)  ->				int:



        """simple docstring"""






        pass

  @unittest.skip(reason="""FocalNet does not use feedforward chunking"""				)
  def   A__						(				self				)  ->				int:



        """simple docstring"""






        pass

  def   A__						(				self				)  ->				str:



        """simple docstring"""






        UpperCamelCase  ,UpperCamelCase      = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes[:-1]:
              UpperCamelCase      = model_class(_SCREAMING_SNAKE_CASE				)
              self.assertIsInstance(model.get_input_embeddings()      ,		(nn.Module)				)
              UpperCamelCase      = model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE      ,		nn.Linear				)				)

  def   A__						(				self				)  ->				List[Any]:



        """simple docstring"""






        UpperCamelCase  ,UpperCamelCase      = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes[:-1]:
              UpperCamelCase      = model_class(_SCREAMING_SNAKE_CASE				)
              UpperCamelCase      = inspect.signature(model.forward				)
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              UpperCamelCase      = [*signature.parameters.keys()]

              UpperCamelCase      = ["""pixel_values"""]
              self.assertListEqual(arg_names[:1]      ,		_SCREAMING_SNAKE_CASE				)

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)  ->				Dict:



        """simple docstring"""






        UpperCamelCase      = model_class(_SCREAMING_SNAKE_CASE				)
        model.to(_SCREAMING_SNAKE_CASE				)
        model.eval()

        with torch.no_grad():
              UpperCamelCase      = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)				)

        UpperCamelCase      = outputs.hidden_states

        UpperCamelCase      = getattr(
            self.model_tester      ,		"""expected_num_hidden_layers"""      ,		len(self.model_tester.depths				) + 1				)
        self.assertEqual(len(_SCREAMING_SNAKE_CASE				)      ,		_SCREAMING_SNAKE_CASE				)

        # FocalNet has a different seq_length
        UpperCamelCase      = (
            config.patch_size
            if isinstance(config.patch_size      ,		collections.abc.Iterable				)
            else (config.patch_size, config.patch_size)
        )

        UpperCamelCase      = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

        self.assertListEqual(
            list(hidden_states[0].shape[-2:]				)      ,		[num_patches, self.model_tester.embed_dim]      ,		)

        UpperCamelCase      = outputs.reshaped_hidden_states
        self.assertEqual(len(_SCREAMING_SNAKE_CASE				)      ,		_SCREAMING_SNAKE_CASE				)

        UpperCamelCase  ,UpperCamelCase  ,UpperCamelCase  ,UpperCamelCase      = reshaped_hidden_states[0].shape
        UpperCamelCase      = (
            reshaped_hidden_states[0].view(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		height * width				).permute(0      ,		2      ,		1				)
        )
        self.assertListEqual(
            list(reshaped_hidden_states.shape[-2:]				)      ,		[num_patches, self.model_tester.embed_dim]      ,		)

  def   A__						(				self				)  ->				Optional[Any]:



        """simple docstring"""






        UpperCamelCase  ,UpperCamelCase      = self.model_tester.prepare_config_and_inputs_for_common()

        UpperCamelCase      = (
            self.model_tester.image_size
            if isinstance(self.model_tester.image_size      ,		collections.abc.Iterable				)
            else (self.model_tester.image_size, self.model_tester.image_size)
        )

        for model_class in self.all_model_classes[:-1]:
              UpperCamelCase      = True
              self.check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              UpperCamelCase      = True

              self.check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)

  def   A__						(				self				)  ->				List[Any]:



        """simple docstring"""






        UpperCamelCase  ,UpperCamelCase      = self.model_tester.prepare_config_and_inputs_for_common()
        UpperCamelCase      = 3

        UpperCamelCase      = (
            self.model_tester.image_size
            if isinstance(self.model_tester.image_size      ,		collections.abc.Iterable				)
            else (self.model_tester.image_size, self.model_tester.image_size)
        )
        UpperCamelCase      = (
            config.patch_size
            if isinstance(config.patch_size      ,		collections.abc.Iterable				)
            else (config.patch_size, config.patch_size)
        )

        UpperCamelCase      = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
        UpperCamelCase      = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

        for model_class in self.all_model_classes[:-1]:
              UpperCamelCase      = True
              self.check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		(padded_height, padded_width)				)

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              UpperCamelCase      = True
              self.check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		(padded_height, padded_width)				)

  @slow
  def   A__						(				self				)  ->				Union[str, Any]:



        """simple docstring"""






        for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCamelCase      = FocalNetModel.from_pretrained(_SCREAMING_SNAKE_CASE				)
              self.assertIsNotNone(_SCREAMING_SNAKE_CASE				)


  def   A__						(				self				)  ->				Any:



        """simple docstring"""






        UpperCamelCase  ,UpperCamelCase      = self.model_tester.prepare_config_and_inputs_for_common()

        UpperCamelCase      = _config_zero_init(_SCREAMING_SNAKE_CASE				)
        for model_class in self.all_model_classes:
              UpperCamelCase      = model_class(config=_SCREAMING_SNAKE_CASE				)
              for name, param in model.named_parameters():
                    if "embeddings" not in name and param.requires_grad:
                          self.assertIn(
                              ((param.data.mean() * 1e9).round() / 1e9).item()      ,		[0.0, 1.0]      ,		msg=F"Parameter {name} of model {model_class} seems not properly initialized"      ,		)






@require_vision
@require_torch
class     a_						(      unittest.TestCase    ):

  @cached_property
  def   A__						(				self				)  ->				List[str]:



        """simple docstring"""






        return AutoImageProcessor.from_pretrained("""microsoft/focalnet-tiny"""				) if is_vision_available() else None


  @slow
  def   A__						(				self				)  ->				str:



        """simple docstring"""






        UpperCamelCase      = FocalNetForImageClassification.from_pretrained("""microsoft/focalnet-tiny"""				).to(_SCREAMING_SNAKE_CASE				)
        UpperCamelCase      = self.default_image_processor

        UpperCamelCase      = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""				)
        UpperCamelCase      = image_processor(images=_SCREAMING_SNAKE_CASE      ,		return_tensors="""pt"""				).to(_SCREAMING_SNAKE_CASE				)

        # forward pass
        with torch.no_grad():
              UpperCamelCase      = model(**_SCREAMING_SNAKE_CASE				)

        # verify the logits
        UpperCamelCase      = torch.Size((1, 1000)				)
        self.assertEqual(outputs.logits.shape      ,		_SCREAMING_SNAKE_CASE				)
        UpperCamelCase      = torch.tensor([0.2_1_6_6, -0.4_3_6_8, 0.2_1_9_1]				).to(_SCREAMING_SNAKE_CASE				)
        self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,		_SCREAMING_SNAKE_CASE      ,		atol=1e-4				)				)
        self.assertTrue(outputs.logits.argmax(dim=-1				).item()      ,		281				)






@require_torch
class     a_						(      lowerCamelCase     ,	unittest.TestCase    ):
  lowercase							=				(FocalNetBackbone,) if is_torch_available() else ()
  lowercase							=				FocalNetConfig

  lowercase							=				False

  def   A__						(				self				)  ->				Union[str, Any]:



        """simple docstring"""






        UpperCamelCase      = FocalNetModelTester(self				)
