


import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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 (
      MODEL_MAPPING,
      BeitForImageClassification,
      BeitForMaskedImageModeling,
      BeitForSemanticSegmentation,
      BeitModel,
  )
  from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  import PIL
  from PIL import Image

  from transformers import BeitImageProcessor



class    UpperCAmelCase_	:
       '''simple docstring'''






       def __init__(     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE=100 ,					__SCREAMING_SNAKE_CASE=13 ,					__SCREAMING_SNAKE_CASE=30 ,					__SCREAMING_SNAKE_CASE=2 ,					__SCREAMING_SNAKE_CASE=3 ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=32 ,					__SCREAMING_SNAKE_CASE=4 ,					__SCREAMING_SNAKE_CASE=4 ,					__SCREAMING_SNAKE_CASE=37 ,					__SCREAMING_SNAKE_CASE="gelu" ,					__SCREAMING_SNAKE_CASE=0.1 ,					__SCREAMING_SNAKE_CASE=0.1 ,					__SCREAMING_SNAKE_CASE=10 ,					__SCREAMING_SNAKE_CASE=0.02 ,					__SCREAMING_SNAKE_CASE=3 ,					__SCREAMING_SNAKE_CASE=None ,					__SCREAMING_SNAKE_CASE=[0, 1, 2, 3] ,					):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		parent
              UpperCamelCase    :	Dict   	=		100
              UpperCamelCase    :	Tuple   	=		batch_size
              UpperCamelCase    :	Union[str, Any]   	=		image_size
              UpperCamelCase    :	Tuple   	=		patch_size
              UpperCamelCase    :	Union[str, Any]   	=		num_channels
              UpperCamelCase    :	Dict   	=		is_training
              UpperCamelCase    :	List[Any]   	=		use_labels
              UpperCamelCase    :	Union[str, Any]   	=		hidden_size
              UpperCamelCase    :	List[Any]   	=		num_hidden_layers
              UpperCamelCase    :	Tuple   	=		num_attention_heads
              UpperCamelCase    :	List[Any]   	=		intermediate_size
              UpperCamelCase    :	Dict   	=		hidden_act
              UpperCamelCase    :	str   	=		hidden_dropout_prob
              UpperCamelCase    :	List[Any]   	=		attention_probs_dropout_prob
              UpperCamelCase    :	Optional[int]   	=		type_sequence_label_size
              UpperCamelCase    :	int   	=		initializer_range
              UpperCamelCase    :	str   	=		scope
              UpperCamelCase    :	List[Any]   	=		out_indices
              UpperCamelCase    :	Tuple   	=		num_labels

              # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
              UpperCamelCase    :	List[Any]   	=		(image_size // patch_size) ** 2
              UpperCamelCase    :	List[Any]   	=		num_patches + 1






       def  _lowercase   (     self						):




              """simple docstring"""




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

              UpperCamelCase    :	Dict   	=		None
              UpperCamelCase    :	Optional[Any]   	=		None
              if self.use_labels:
                     UpperCamelCase    :	List[str]   	=		ids_tensor([self.batch_size] ,					self.type_sequence_label_size						)
                     UpperCamelCase    :	Dict   	=		ids_tensor([self.batch_size, self.image_size, self.image_size] ,					self.num_labels						)

              UpperCamelCase    :	List[Any]   	=		self.get_config()

              return config, pixel_values, labels, pixel_labels






       def  _lowercase   (     self						):




              """simple docstring"""




              return BeitConfig(
                  vocab_size=self.vocab_size ,					image_size=self.image_size ,					patch_size=self.patch_size ,					num_channels=self.num_channels ,					hidden_size=self.hidden_size ,					num_hidden_layers=self.num_hidden_layers ,					num_attention_heads=self.num_attention_heads ,					intermediate_size=self.intermediate_size ,					hidden_act=self.hidden_act ,					hidden_dropout_prob=self.hidden_dropout_prob ,					attention_probs_dropout_prob=self.attention_probs_dropout_prob ,					is_decoder=__SCREAMING_SNAKE_CASE ,					initializer_range=self.initializer_range ,					out_indices=self.out_indices ,					)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	int   	=		BeitModel(config=__SCREAMING_SNAKE_CASE						)
              model.to(__SCREAMING_SNAKE_CASE						)
              model.eval()
              UpperCamelCase    :	List[str]   	=		model(__SCREAMING_SNAKE_CASE						)
              self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		BeitForMaskedImageModeling(config=__SCREAMING_SNAKE_CASE						)
              model.to(__SCREAMING_SNAKE_CASE						)
              model.eval()
              UpperCamelCase    :	Optional[Any]   	=		model(__SCREAMING_SNAKE_CASE						)
              self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length - 1, self.vocab_size)						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		self.type_sequence_label_size
              UpperCamelCase    :	Optional[Any]   	=		BeitForImageClassification(__SCREAMING_SNAKE_CASE						)
              model.to(__SCREAMING_SNAKE_CASE						)
              model.eval()
              UpperCamelCase    :	str   	=		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    :	Any   	=		1
              UpperCamelCase    :	Optional[int]   	=		BeitForImageClassification(__SCREAMING_SNAKE_CASE						)
              model.to(__SCREAMING_SNAKE_CASE						)
              model.eval()

              UpperCamelCase    :	List[Any]   	=		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
              UpperCamelCase    :	Optional[int]   	=		model(__SCREAMING_SNAKE_CASE ,					labels=__SCREAMING_SNAKE_CASE						)
              self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.type_sequence_label_size)						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	str   	=		self.num_labels
              UpperCamelCase    :	Dict   	=		BeitForSemanticSegmentation(__SCREAMING_SNAKE_CASE						)
              model.to(__SCREAMING_SNAKE_CASE						)
              model.eval()
              UpperCamelCase    :	List[str]   	=		model(__SCREAMING_SNAKE_CASE						)
              self.parent.assertEqual(
                  result.logits.shape ,					(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)						)
              UpperCamelCase    :	List[str]   	=		model(__SCREAMING_SNAKE_CASE ,					labels=__SCREAMING_SNAKE_CASE						)
              self.parent.assertEqual(
                  result.logits.shape ,					(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[int]   	=		self.prepare_config_and_inputs()
              UpperCamelCase    :	str   	=		config_and_inputs
              UpperCamelCase    :	List[str]   	=		{'''pixel_values''': pixel_values}
              return config, inputs_dict




@require_torch
class    UpperCAmelCase_	(							_a,     _a,     unittest.TestCase):
       '''simple docstring'''
       __UpperCamelCase      :     Union[str, Any]							=					(
           (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
           if is_torch_available()
           else ()
       )
       __UpperCamelCase      :     Any							=					(
           {
               "feature-extraction": BeitModel,
               "image-classification": BeitForImageClassification,
               "image-segmentation": BeitForSemanticSegmentation,
           }
           if is_torch_available()
           else {}
       )

       __UpperCamelCase      :     List[str]							=					False
       __UpperCamelCase      :     Dict							=					False
       __UpperCamelCase      :     Tuple							=					False






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[Any]   	=		BeitModelTester(self						)
              UpperCamelCase    :	Dict   	=		ConfigTester(self ,					config_class=__SCREAMING_SNAKE_CASE ,					has_text_modality=__SCREAMING_SNAKE_CASE ,					hidden_size=37						)






       def  _lowercase   (     self						):




              """simple docstring"""




              self.config_tester.run_common_tests()






       @unittest.skip(reason='''BEiT does not use inputs_embeds'''						)
       def  _lowercase   (     self						):




              """simple docstring"""




              pass






       @require_torch_multi_gpu
       @unittest.skip(reason='''BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''						)
       def  _lowercase   (     self						):




              """simple docstring"""




              pass






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	int   	=		self.model_tester.prepare_config_and_inputs_for_common()

              for model_class in self.all_model_classes:
                     UpperCamelCase    :	Union[str, Any]   	=		model_class(__SCREAMING_SNAKE_CASE						)
                     self.assertIsInstance(model.get_input_embeddings() ,					(nn.Module)						)
                     UpperCamelCase    :	List[str]   	=		model.get_output_embeddings()
                     self.assertTrue(x is None or isinstance(__SCREAMING_SNAKE_CASE ,					nn.Linear						)						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	List[str]   	=		self.model_tester.prepare_config_and_inputs_for_common()

              for model_class in self.all_model_classes:
                     UpperCamelCase    :	Union[str, Any]   	=		model_class(__SCREAMING_SNAKE_CASE						)
                     UpperCamelCase    :	Tuple   	=		inspect.signature(model.forward						)
                     # signature.parameters is an OrderedDict => so arg_names order is deterministic
                     UpperCamelCase    :	List[Any]   	=		[*signature.parameters.keys()]

                     UpperCamelCase    :	str   	=		['''pixel_values''']
                     self.assertListEqual(arg_names[:1] ,					__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	List[Any]   	=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Tuple   	=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	int   	=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	List[Any]   	=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_semantic_segmentation(*__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              if not self.model_tester.is_training:
                     return

              UpperCamelCase    :	Union[str, Any]   	=		self.model_tester.prepare_config_and_inputs_for_common()
              UpperCamelCase    :	Tuple   	=		True

              for model_class in self.all_model_classes:
                     # we don't test BeitForMaskedImageModeling
                     if model_class in [*get_values(__SCREAMING_SNAKE_CASE						), BeitForMaskedImageModeling]:
                            continue

                     UpperCamelCase    :	str   	=		model_class(__SCREAMING_SNAKE_CASE						)
                     model.to(__SCREAMING_SNAKE_CASE						)
                     model.train()
                     UpperCamelCase    :	Dict   	=		self._prepare_for_class(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					return_labels=__SCREAMING_SNAKE_CASE						)
                     UpperCamelCase    :	int   	=		model(**__SCREAMING_SNAKE_CASE						).loss
                     loss.backward()






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	List[Any]   	=		self.model_tester.prepare_config_and_inputs_for_common()
              if not self.model_tester.is_training:
                     return

              UpperCamelCase    :	int   	=		False
              UpperCamelCase    :	Optional[Any]   	=		True

              for model_class in self.all_model_classes:
                     # we don't test BeitForMaskedImageModeling
                     if (
                         model_class in [*get_values(__SCREAMING_SNAKE_CASE						), BeitForMaskedImageModeling]
                         or not model_class.supports_gradient_checkpointing
                     ):
                            continue

                     UpperCamelCase    :	str   	=		model_class(__SCREAMING_SNAKE_CASE						)
                     model.gradient_checkpointing_enable()
                     model.to(__SCREAMING_SNAKE_CASE						)
                     model.train()
                     UpperCamelCase    :	Optional[int]   	=		self._prepare_for_class(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					return_labels=__SCREAMING_SNAKE_CASE						)
                     UpperCamelCase    :	str   	=		model(**__SCREAMING_SNAKE_CASE						).loss
                     loss.backward()






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	str   	=		self.model_tester.prepare_config_and_inputs_for_common()

              UpperCamelCase    :	Any   	=		_config_zero_init(__SCREAMING_SNAKE_CASE						)
              for model_class in self.all_model_classes:
                     UpperCamelCase    :	Optional[Any]   	=		model_class(config=__SCREAMING_SNAKE_CASE						)
                     for name, param in model.named_parameters():
                            # we skip lambda parameters as these require special initial values
                            # determined by config.layer_scale_init_value
                            if "lambda" in name:
                                   continue
                            if 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""" ,					)






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     UpperCamelCase    :	Union[str, Any]   	=		BeitModel.from_pretrained(__SCREAMING_SNAKE_CASE						)
                     self.assertIsNotNone(__SCREAMING_SNAKE_CASE						)




def      a	(							):






       """simple docstring"""





       UpperCamelCase    :	str   	=		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
       return image



@require_torch
@require_vision
class    UpperCAmelCase_	(							unittest.TestCase):
       '''simple docstring'''






       @cached_property
       def  _lowercase   (     self						):




              """simple docstring"""




              return BeitImageProcessor.from_pretrained('''microsoft/beit-base-patch16-224'''						) if is_vision_available() else None






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		BeitForMaskedImageModeling.from_pretrained('''microsoft/beit-base-patch16-224-pt22k'''						).to(__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	List[str]   	=		self.default_image_processor
              UpperCamelCase    :	Any   	=		prepare_img()
              UpperCamelCase    :	int   	=		image_processor(images=__SCREAMING_SNAKE_CASE ,					return_tensors='''pt'''						).pixel_values.to(__SCREAMING_SNAKE_CASE						)

              # prepare bool_masked_pos
              UpperCamelCase    :	Tuple   	=		torch.ones((1, 196) ,					dtype=torch.bool						).to(__SCREAMING_SNAKE_CASE						)

              # forward pass
              with torch.no_grad():
                     UpperCamelCase    :	List[Any]   	=		model(pixel_values=__SCREAMING_SNAKE_CASE ,					bool_masked_pos=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Any   	=		outputs.logits

              # verify the logits
              UpperCamelCase    :	int   	=		torch.Size((1, 196, 8_192)						)
              self.assertEqual(logits.shape ,					__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	List[str]   	=		torch.tensor(
                  [[-3.2_437, 0.5_072, -13.9_174], [-3.2_456, 0.4_948, -13.9_401], [-3.2_033, 0.5_121, -13.8_550]]						).to(__SCREAMING_SNAKE_CASE						)

              self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] ,					__SCREAMING_SNAKE_CASE ,					atol=1e-2						)						)






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Tuple   	=		BeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224'''						).to(__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	Dict   	=		self.default_image_processor
              UpperCamelCase    :	List[str]   	=		prepare_img()
              UpperCamelCase    :	List[Any]   	=		image_processor(images=__SCREAMING_SNAKE_CASE ,					return_tensors='''pt'''						).to(__SCREAMING_SNAKE_CASE						)

              # forward pass
              with torch.no_grad():
                     UpperCamelCase    :	Dict   	=		model(**__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	int   	=		outputs.logits

              # verify the logits
              UpperCamelCase    :	Dict   	=		torch.Size((1, 1_000)						)
              self.assertEqual(logits.shape ,					__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	Optional[Any]   	=		torch.tensor([-1.2_385, -1.0_987, -1.0_108]						).to(__SCREAMING_SNAKE_CASE						)

              self.assertTrue(torch.allclose(logits[0, :3] ,					__SCREAMING_SNAKE_CASE ,					atol=1e-4						)						)

              UpperCamelCase    :	Union[str, Any]   	=		281
              self.assertEqual(logits.argmax(-1						).item() ,					__SCREAMING_SNAKE_CASE						)






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[int]   	=		BeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k'''						).to(
                  __SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	Tuple   	=		self.default_image_processor
              UpperCamelCase    :	Dict   	=		prepare_img()
              UpperCamelCase    :	Tuple   	=		image_processor(images=__SCREAMING_SNAKE_CASE ,					return_tensors='''pt'''						).to(__SCREAMING_SNAKE_CASE						)

              # forward pass
              with torch.no_grad():
                     UpperCamelCase    :	Any   	=		model(**__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Union[str, Any]   	=		outputs.logits

              # verify the logits
              UpperCamelCase    :	Union[str, Any]   	=		torch.Size((1, 21_841)						)
              self.assertEqual(logits.shape ,					__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	Union[str, Any]   	=		torch.tensor([1.6_881, -0.2_787, 0.5_901]						).to(__SCREAMING_SNAKE_CASE						)

              self.assertTrue(torch.allclose(logits[0, :3] ,					__SCREAMING_SNAKE_CASE ,					atol=1e-4						)						)

              UpperCamelCase    :	List[Any]   	=		2_396
              self.assertEqual(logits.argmax(-1						).item() ,					__SCREAMING_SNAKE_CASE						)






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[int]   	=		BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''						)
              UpperCamelCase    :	List[str]   	=		model.to(__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	List[str]   	=		BeitImageProcessor(do_resize=__SCREAMING_SNAKE_CASE ,					size=640 ,					do_center_crop=__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	int   	=		load_dataset('''hf-internal-testing/fixtures_ade20k''' ,					split='''test'''						)
              UpperCamelCase    :	Any   	=		Image.open(ds[0]['''file''']						)
              UpperCamelCase    :	List[str]   	=		image_processor(images=__SCREAMING_SNAKE_CASE ,					return_tensors='''pt'''						).to(__SCREAMING_SNAKE_CASE						)

              # forward pass
              with torch.no_grad():
                     UpperCamelCase    :	List[Any]   	=		model(**__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Optional[int]   	=		outputs.logits

              # verify the logits
              UpperCamelCase    :	List[str]   	=		torch.Size((1, 150, 160, 160)						)
              self.assertEqual(logits.shape ,					__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	Optional[Any]   	=		version.parse(PIL.__version__						) < version.parse('''9.0.0'''						)

              if is_pillow_less_than_a:
                     UpperCamelCase    :	Optional[Any]   	=		torch.tensor(
                         [
                             [[-4.9_225, -2.3_954, -3.0_522], [-2.8_822, -1.0_046, -1.7_561], [-2.9_549, -1.3_228, -2.1_347]],
                             [[-5.8_168, -3.4_129, -4.0_778], [-3.8_651, -2.2_214, -3.0_277], [-3.8_356, -2.4_643, -3.3_535]],
                             [[-0.0_078, 3.9_952, 4.0_754], [2.9_856, 4.6_944, 5.0_035], [3.2_413, 4.7_813, 4.9_969]],
                         ] ,					device=__SCREAMING_SNAKE_CASE ,					)
              else:
                     UpperCamelCase    :	Any   	=		torch.tensor(
                         [
                             [[-4.8_960, -2.3_688, -3.0_355], [-2.8_478, -0.9_836, -1.7_418], [-2.9_449, -1.3_332, -2.1_456]],
                             [[-5.8_081, -3.4_124, -4.1_006], [-3.8_561, -2.2_081, -3.0_323], [-3.8_365, -2.4_601, -3.3_669]],
                             [[-0.0_309, 3.9_868, 4.0_540], [2.9_640, 4.6_877, 4.9_976], [3.2_081, 4.7_690, 4.9_942]],
                         ] ,					device=__SCREAMING_SNAKE_CASE ,					)

              self.assertTrue(torch.allclose(logits[0, :3, :3, :3] ,					__SCREAMING_SNAKE_CASE ,					atol=1e-4						)						)






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	str   	=		BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''						)
              UpperCamelCase    :	str   	=		model.to(__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	Any   	=		BeitImageProcessor(do_resize=__SCREAMING_SNAKE_CASE ,					size=640 ,					do_center_crop=__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	Optional[Any]   	=		load_dataset('''hf-internal-testing/fixtures_ade20k''' ,					split='''test'''						)
              UpperCamelCase    :	Dict   	=		Image.open(ds[0]['''file''']						)
              UpperCamelCase    :	Optional[int]   	=		image_processor(images=__SCREAMING_SNAKE_CASE ,					return_tensors='''pt'''						).to(__SCREAMING_SNAKE_CASE						)

              # forward pass
              with torch.no_grad():
                     UpperCamelCase    :	Optional[Any]   	=		model(**__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	Optional[int]   	=		outputs.logits.detach().cpu()

              UpperCamelCase    :	Dict   	=		image_processor.post_process_semantic_segmentation(outputs=__SCREAMING_SNAKE_CASE ,					target_sizes=[(500, 300)]						)
              UpperCamelCase    :	Tuple   	=		torch.Size((500, 300)						)
              self.assertEqual(segmentation[0].shape ,					__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	int   	=		image_processor.post_process_semantic_segmentation(outputs=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Dict   	=		torch.Size((160, 160)						)
              self.assertEqual(segmentation[0].shape ,					__SCREAMING_SNAKE_CASE						)





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

import numpy as np

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


__UpperCAmelCase : Dict 			=				logging.get_logger(__name__)



class    UpperCAmelCase_	(							_a):
       '''simple docstring'''
       __UpperCamelCase      :     Any							=					["input_features"]






       def __init__(     self ,					__SCREAMING_SNAKE_CASE=80 ,					__SCREAMING_SNAKE_CASE=16_000 ,					__SCREAMING_SNAKE_CASE=160 ,					__SCREAMING_SNAKE_CASE=30 ,					__SCREAMING_SNAKE_CASE=400 ,					__SCREAMING_SNAKE_CASE=0.0 ,					__SCREAMING_SNAKE_CASE=False ,					**__SCREAMING_SNAKE_CASE ,					):




              """simple docstring"""




              super().__init__(
                  feature_size=__SCREAMING_SNAKE_CASE ,					sampling_rate=__SCREAMING_SNAKE_CASE ,					padding_value=__SCREAMING_SNAKE_CASE ,					return_attention_mask=__SCREAMING_SNAKE_CASE ,					**__SCREAMING_SNAKE_CASE ,					)
              UpperCamelCase    :	List[str]   	=		n_fft
              UpperCamelCase    :	Dict   	=		hop_length
              UpperCamelCase    :	Dict   	=		chunk_length
              UpperCamelCase    :	List[str]   	=		chunk_length * sampling_rate
              UpperCamelCase    :	Dict   	=		self.n_samples // hop_length
              UpperCamelCase    :	str   	=		sampling_rate
              UpperCamelCase    :	Union[str, Any]   	=		mel_filter_bank(
                  num_frequency_bins=1 + n_fft // 2 ,					num_mel_filters=__SCREAMING_SNAKE_CASE ,					min_frequency=0.0 ,					max_frequency=8_000.0 ,					sampling_rate=__SCREAMING_SNAKE_CASE ,					norm='''slaney''' ,					mel_scale='''slaney''' ,					)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	List[str]   	=		spectrogram(
                  __SCREAMING_SNAKE_CASE ,					window_function(self.n_fft ,					'''hann'''						) ,					frame_length=self.n_fft ,					hop_length=self.hop_length ,					power=2.0 ,					mel_filters=self.mel_filters ,					log_mel='''log10''' ,					)
              UpperCamelCase    :	int   	=		log_spec[:, :-1]
              UpperCamelCase    :	int   	=		np.maximum(__SCREAMING_SNAKE_CASE ,					log_spec.max() - 8.0						)
              UpperCamelCase    :	Any   	=		(log_spec + 4.0) / 4.0
              return log_spec






       @staticmethod
       # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
       def  _lowercase   (     __SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE = 0.0						):




              """simple docstring"""




              if attention_mask is not None:
                     UpperCamelCase    :	List[Any]   	=		np.array(__SCREAMING_SNAKE_CASE ,					np.intaa						)
                     UpperCamelCase    :	Optional[Any]   	=		[]

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

                            normed_input_values.append(__SCREAMING_SNAKE_CASE						)
              else:
                     UpperCamelCase    :	Union[str, Any]   	=		[(x - x.mean()) / np.sqrt(x.var() + 1e-7						) for x in input_values]

              return normed_input_values






       def __call__(     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE = True ,					__SCREAMING_SNAKE_CASE = None ,					__SCREAMING_SNAKE_CASE = None ,					__SCREAMING_SNAKE_CASE = None ,					__SCREAMING_SNAKE_CASE = "max_length" ,					__SCREAMING_SNAKE_CASE = None ,					__SCREAMING_SNAKE_CASE = None ,					__SCREAMING_SNAKE_CASE = None ,					**__SCREAMING_SNAKE_CASE ,					):




              """simple docstring"""




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

              UpperCamelCase    :	Tuple   	=		isinstance(__SCREAMING_SNAKE_CASE ,					np.ndarray						) and len(raw_speech.shape						) > 1
              if is_batched_numpy and len(raw_speech.shape						) > 2:
                     raise ValueError(f"""Only mono-channel audio is supported for input to {self}"""						)
              UpperCamelCase    :	Union[str, Any]   	=		is_batched_numpy or (
                  isinstance(__SCREAMING_SNAKE_CASE ,					(list, tuple)						) and (isinstance(raw_speech[0] ,					(np.ndarray, tuple, list)						))
              )

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

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

              UpperCamelCase    :	Optional[int]   	=		BatchFeature({'''input_features''': raw_speech}						)

              # convert into correct format for padding

              UpperCamelCase    :	Optional[Any]   	=		self.pad(
                  __SCREAMING_SNAKE_CASE ,					padding=__SCREAMING_SNAKE_CASE ,					max_length=max_length if max_length else self.n_samples ,					truncation=__SCREAMING_SNAKE_CASE ,					pad_to_multiple_of=__SCREAMING_SNAKE_CASE ,					return_attention_mask=return_attention_mask or do_normalize ,					)

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

              # make sure list is in array format
              UpperCamelCase    :	Dict   	=		padded_inputs.get('''input_features'''						).transpose(2 ,					0 ,					1						)

              UpperCamelCase    :	Tuple   	=		[self._np_extract_fbank_features(__SCREAMING_SNAKE_CASE						) for waveform in input_features[0]]

              if isinstance(input_features[0] ,					__SCREAMING_SNAKE_CASE						):
                     UpperCamelCase    :	Optional[int]   	=		[np.asarray(__SCREAMING_SNAKE_CASE ,					dtype=np.floataa						) for feature in input_features]
              else:
                     UpperCamelCase    :	Dict   	=		input_features

              if return_attention_mask:
                     # rescale from sample (48000) to feature (3000)
                     UpperCamelCase    :	Union[str, Any]   	=		padded_inputs['''attention_mask'''][:, :: self.hop_length]

              if return_tensors is not None:
                     UpperCamelCase    :	Dict   	=		padded_inputs.convert_to_tensors(__SCREAMING_SNAKE_CASE						)

              return padded_inputs






       def  _lowercase   (     self						):




              """simple docstring"""




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

