

'''simple docstring'''






import random
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
      import torch

if is_vision_available():
      import PIL

      from transformers import FlavaImageProcessor
      from transformers.image_utils import PILImageResampling
      from transformers.models.flava.image_processing_flava import (
          FLAVA_CODEBOOK_MEAN,
          FLAVA_CODEBOOK_STD,
          FLAVA_IMAGE_MEAN,
          FLAVA_IMAGE_STD,
      )
else:
      snake_case_					  =	snake_case_					  =	snake_case_					  =	snake_case_					  =	None
class      SCREAMING_SNAKE_CASE__      (    unittest.TestCase				):




    def __init__( self    ,					lowercase__    ,					lowercase__=7    ,					lowercase__=3    ,					lowercase__=30    ,					lowercase__=400    ,					lowercase__=True    ,					lowercase__=None    ,					lowercase__=True    ,					lowercase__=None    ,					lowercase__=None    ,					lowercase__=True    ,					lowercase__=1 / 255    ,					lowercase__=True    ,					lowercase__=FLAVA_IMAGE_MEAN    ,					lowercase__=FLAVA_IMAGE_STD    ,					lowercase__=14    ,					lowercase__=75    ,					lowercase__=None    ,					lowercase__=16    ,					lowercase__=0.3    ,					lowercase__=None    ,					lowercase__=True    ,					lowercase__=None    ,					lowercase__=None    ,					lowercase__=True    ,					lowercase__=None    ,					lowercase__=True    ,					lowercase__=True    ,					lowercase__=FLAVA_CODEBOOK_MEAN    ,					lowercase__=FLAVA_CODEBOOK_STD    ,					):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      List[Any]					=	size if size is not None else {"height": 224, "width": 224}
          SCREAMING_SNAKE_CASE_				:      List[str]					=	crop_size if crop_size is not None else {"height": 224, "width": 224}
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	codebook_size if codebook_size is not None else {"height": 112, "width": 112}
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	codebook_crop_size if codebook_crop_size is not None else {"height": 112, "width": 112}

          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	parent
          SCREAMING_SNAKE_CASE_				:      str					=	batch_size
          SCREAMING_SNAKE_CASE_				:      List[str]					=	num_channels
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	do_resize
          SCREAMING_SNAKE_CASE_				:      List[str]					=	do_rescale
          SCREAMING_SNAKE_CASE_				:      List[str]					=	rescale_factor
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	min_resolution
          SCREAMING_SNAKE_CASE_				:      Any					=	max_resolution
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	size
          SCREAMING_SNAKE_CASE_				:      Dict					=	resample if resample is not None else PILImageResampling.BICUBIC
          SCREAMING_SNAKE_CASE_				:      Any					=	do_normalize
          SCREAMING_SNAKE_CASE_				:      int					=	image_mean
          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	image_std
          SCREAMING_SNAKE_CASE_				:      List[str]					=	do_center_crop
          SCREAMING_SNAKE_CASE_				:      List[str]					=	crop_size

          SCREAMING_SNAKE_CASE_				:      str					=	input_size_patches
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	total_mask_patches
          SCREAMING_SNAKE_CASE_				:      Dict					=	mask_group_max_patches
          SCREAMING_SNAKE_CASE_				:      List[Any]					=	mask_group_min_patches
          SCREAMING_SNAKE_CASE_				:      Dict					=	mask_group_min_aspect_ratio
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	mask_group_max_aspect_ratio

          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	codebook_do_resize
          SCREAMING_SNAKE_CASE_				:      str					=	codebook_size
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	codebook_do_center_crop
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	codebook_crop_size
          SCREAMING_SNAKE_CASE_				:      str					=	codebook_do_map_pixels
          SCREAMING_SNAKE_CASE_				:      Tuple					=	codebook_do_normalize
          SCREAMING_SNAKE_CASE_				:      List[Any]					=	codebook_image_mean
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	codebook_image_std




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          return {
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_normalize": self.do_normalize,
              "do_resize": self.do_resize,
              "size": self.size,
              "resample": self.resample,
              "do_rescale": self.do_rescale,
              "rescale_factor": self.rescale_factor,
              "do_center_crop": self.do_center_crop,
              "crop_size": self.crop_size,
              "input_size_patches": self.input_size_patches,
              "total_mask_patches": self.total_mask_patches,
              "mask_group_max_patches": self.mask_group_max_patches,
              "mask_group_min_patches": self.mask_group_min_patches,
              "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
              "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
              "codebook_do_resize": self.codebook_do_resize,
              "codebook_size": self.codebook_size,
              "codebook_resample": self.codebook_resample,
              "codebook_do_center_crop": self.codebook_do_center_crop,
              "codebook_crop_size": self.codebook_crop_size,
              "codebook_do_map_pixels": self.codebook_do_map_pixels,
              "codebook_do_normalize": self.codebook_do_normalize,
              "codebook_image_mean": self.codebook_image_mean,
              "codebook_image_std": self.codebook_image_std,
          }




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          return (self.size["height"], self.size["width"])




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          return (
              (self.input_size_patches, self.input_size_patches)
              if not isinstance(self.input_size_patches    ,					lowercase__      )
              else self.input_size_patches
          )







    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          return (self.codebook_size["height"], self.codebook_size["width"])






@require_torch
@require_vision
class      SCREAMING_SNAKE_CASE__      (    _UpperCAmelCase,unittest.TestCase				):
    _A         =							FlavaImageProcessor if is_vision_available() else None
    _A         =							None




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      Any					=	FlavaImageProcessingTester(self      )




    @property
    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          return self.image_processor_tester.prepare_image_processor_dict()




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      Tuple					=	self.image_processing_class(**self.image_processor_dict      )
          self.assertTrue(hasattr(lowercase__    ,					"image_mean"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"image_std"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"do_normalize"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"do_resize"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"resample"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"crop_size"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"do_center_crop"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"do_rescale"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"rescale_factor"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"masking_generator"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"codebook_do_resize"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"codebook_size"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"codebook_resample"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"codebook_do_center_crop"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"codebook_crop_size"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"codebook_do_map_pixels"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"codebook_do_normalize"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"codebook_image_mean"      )      )
          self.assertTrue(hasattr(lowercase__    ,					"codebook_image_std"      )      )




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      List[Any]					=	self.image_processing_class.from_dict(self.image_processor_dict      )
          self.assertEqual(image_processor.size    ,					{"height": 224, "width": 224}      )
          self.assertEqual(image_processor.crop_size    ,					{"height": 224, "width": 224}      )
          self.assertEqual(image_processor.codebook_size    ,					{"height": 112, "width": 112}      )
          self.assertEqual(image_processor.codebook_crop_size    ,					{"height": 112, "width": 112}      )

          SCREAMING_SNAKE_CASE_				:      List[Any]					=	self.image_processing_class.from_dict(
              self.image_processor_dict    ,					size=42    ,					crop_size=84    ,					codebook_size=33    ,					codebook_crop_size=66      )
          self.assertEqual(image_processor.size    ,					{"height": 42, "width": 42}      )
          self.assertEqual(image_processor.crop_size    ,					{"height": 84, "width": 84}      )
          self.assertEqual(image_processor.codebook_size    ,					{"height": 33, "width": 33}      )
          self.assertEqual(image_processor.codebook_crop_size    ,					{"height": 66, "width": 66}      )




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          pass




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      str					=	self.image_processing_class(**self.image_processor_dict      )
          # create random PIL images
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=lowercase__      )
          for image in image_inputs:
                self.assertIsInstance(lowercase__    ,					PIL.Image.Image      )

          # Test not batched input
          SCREAMING_SNAKE_CASE_				:      str					=	image_processing(image_inputs[0]    ,					return_tensors="pt"      )

          # Test no bool masked pos
          self.assertFalse("bool_masked_pos" in encoded_images      )

          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      List[Any]					=	self.image_processor_tester.get_expected_image_size()

          self.assertEqual(
              encoded_images.pixel_values.shape    ,					(1, self.image_processor_tester.num_channels, expected_height, expected_width)    ,					)

          # Test batched
          SCREAMING_SNAKE_CASE_				:      int					=	image_processing(lowercase__    ,					return_tensors="pt"      )
          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Any					=	self.image_processor_tester.get_expected_image_size()

          # Test no bool masked pos
          self.assertFalse("bool_masked_pos" in encoded_images      )

          self.assertEqual(
              encoded_images.pixel_values.shape    ,					(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              )    ,					)




    def   __lowerCamelCase				( self    ,					lowercase__    ,					lowercase__      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      str					=	self.image_processing_class(**self.image_processor_dict      )
          # create random tensors
          SCREAMING_SNAKE_CASE_				:      Any					=	prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=lowercase__    ,					**lowercase__      )
          for image in image_inputs:
                self.assertIsInstance(lowercase__    ,					lowercase__      )

          # Test not batched input
          SCREAMING_SNAKE_CASE_				:      Any					=	image_processing(image_inputs[0]    ,					return_tensors="pt"      )

          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	self.image_processor_tester.get_expected_image_size()
          self.assertEqual(
              encoded_images.pixel_values.shape    ,					(1, self.image_processor_tester.num_channels, expected_height, expected_width)    ,					)

          SCREAMING_SNAKE_CASE_				:      Dict					=	image_processing(lowercase__    ,					return_image_mask=lowercase__    ,					return_tensors="pt"      )

          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	self.image_processor_tester.get_expected_image_size()
          self.assertEqual(
              encoded_images.pixel_values.shape    ,					(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              )    ,					)

          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Dict					=	self.image_processor_tester.get_expected_mask_size()
          self.assertEqual(
              encoded_images.bool_masked_pos.shape    ,					(
                  self.image_processor_tester.batch_size,
                  expected_height,
                  expected_width,
              )    ,					)

          # Test batched
          SCREAMING_SNAKE_CASE_				:      Tuple					=	image_processing(lowercase__    ,					return_tensors="pt"      ).pixel_values

          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Optional[int]					=	self.image_processor_tester.get_expected_image_size()
          self.assertEqual(
              encoded_images.shape    ,					(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              )    ,					)

          # Test masking
          SCREAMING_SNAKE_CASE_				:      Tuple					=	image_processing(lowercase__    ,					return_image_mask=lowercase__    ,					return_tensors="pt"      )

          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Tuple					=	self.image_processor_tester.get_expected_image_size()
          self.assertEqual(
              encoded_images.pixel_values.shape    ,					(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              )    ,					)

          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      int					=	self.image_processor_tester.get_expected_mask_size()
          self.assertEqual(
              encoded_images.bool_masked_pos.shape    ,					(
                  self.image_processor_tester.batch_size,
                  expected_height,
                  expected_width,
              )    ,					)




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          self._test_call_framework(np.ndarray    ,					prepare_kwargs={"numpify": True}      )




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          self._test_call_framework(torch.Tensor    ,					prepare_kwargs={"torchify": True}      )




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          random.seed(1234      )
          SCREAMING_SNAKE_CASE_				:      Dict					=	self.image_processing_class(**self.image_processor_dict      )
          SCREAMING_SNAKE_CASE_				:      str					=	prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=lowercase__    ,					torchify=lowercase__      )

          # Test not batched input
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	image_processing(image_inputs[0]    ,					return_image_mask=lowercase__    ,					return_tensors="pt"      )
          self.assertEqual(encoded_images.bool_masked_pos.sum().item()    ,					75      )







    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      int					=	self.image_processing_class(**self.image_processor_dict      )
          # create random PIL images
          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=lowercase__      )
          for image in image_inputs:
                self.assertIsInstance(lowercase__    ,					PIL.Image.Image      )

          # Test not batched input
          SCREAMING_SNAKE_CASE_				:      List[Any]					=	image_processing(image_inputs[0]    ,					return_codebook_pixels=lowercase__    ,					return_tensors="pt"      )
          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Tuple					=	self.image_processor_tester.get_expected_codebook_image_size()
          self.assertEqual(
              encoded_images.codebook_pixel_values.shape    ,					(1, self.image_processor_tester.num_channels, expected_height, expected_width)    ,					)

          # Test batched
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	image_processing(lowercase__    ,					return_codebook_pixels=lowercase__    ,					return_tensors="pt"      )
          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      List[Any]					=	self.image_processor_tester.get_expected_codebook_image_size()
          self.assertEqual(
              encoded_images.codebook_pixel_values.shape    ,					(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              )    ,					)




'''simple docstring'''






import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case_					  =	logging.get_logger(__name__)

snake_case_					  =	{
    'facebook/hubert-base-ls960': 'https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json',
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}
class      SCREAMING_SNAKE_CASE__      (    _UpperCAmelCase				):
    _A         =							"hubert"




    def __init__( self    ,					lowercase__=32    ,					lowercase__=768    ,					lowercase__=12    ,					lowercase__=12    ,					lowercase__=3072    ,					lowercase__="gelu"    ,					lowercase__=0.1    ,					lowercase__=0.1    ,					lowercase__=0.1    ,					lowercase__=True    ,					lowercase__=0.0    ,					lowercase__=0.1    ,					lowercase__=0.1    ,					lowercase__=0.02    ,					lowercase__=1e-5    ,					lowercase__="group"    ,					lowercase__="gelu"    ,					lowercase__=(512, 512, 512, 512, 512, 512, 512)    ,					lowercase__=(5, 2, 2, 2, 2, 2, 2)    ,					lowercase__=(10, 3, 3, 3, 3, 2, 2)    ,					lowercase__=False    ,					lowercase__=128    ,					lowercase__=16    ,					lowercase__=False    ,					lowercase__=True    ,					lowercase__=0.05    ,					lowercase__=10    ,					lowercase__=2    ,					lowercase__=0.0    ,					lowercase__=10    ,					lowercase__=0    ,					lowercase__="sum"    ,					lowercase__=False    ,					lowercase__=False    ,					lowercase__=256    ,					lowercase__=0    ,					lowercase__=1    ,					lowercase__=2    ,					**lowercase__    ,					):






          """simple docstring"""






          super().__init__(**lowercase__    ,					pad_token_id=lowercase__    ,					bos_token_id=lowercase__    ,					eos_token_id=lowercase__      )
          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	hidden_size
          SCREAMING_SNAKE_CASE_				:      List[str]					=	feat_extract_norm
          SCREAMING_SNAKE_CASE_				:      Dict					=	feat_extract_activation
          SCREAMING_SNAKE_CASE_				:      str					=	list(lowercase__      )
          SCREAMING_SNAKE_CASE_				:      int					=	list(lowercase__      )
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	list(lowercase__      )
          SCREAMING_SNAKE_CASE_				:      List[str]					=	conv_bias
          SCREAMING_SNAKE_CASE_				:      str					=	num_conv_pos_embeddings
          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	num_conv_pos_embedding_groups
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	len(self.conv_dim      )
          SCREAMING_SNAKE_CASE_				:      Any					=	num_hidden_layers
          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	intermediate_size
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	hidden_act
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	num_attention_heads
          SCREAMING_SNAKE_CASE_				:      Any					=	hidden_dropout
          SCREAMING_SNAKE_CASE_				:      List[str]					=	attention_dropout
          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	activation_dropout
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	feat_proj_layer_norm
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	feat_proj_dropout
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	final_dropout
          SCREAMING_SNAKE_CASE_				:      Dict					=	layerdrop
          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	layer_norm_eps
          SCREAMING_SNAKE_CASE_				:      Tuple					=	initializer_range
          SCREAMING_SNAKE_CASE_				:      List[str]					=	vocab_size
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	do_stable_layer_norm
          SCREAMING_SNAKE_CASE_				:      List[str]					=	use_weighted_layer_sum
          SCREAMING_SNAKE_CASE_				:      Any					=	classifier_proj_size

          if (
              (len(self.conv_stride      ) != self.num_feat_extract_layers)
              or (len(self.conv_kernel      ) != self.num_feat_extract_layers)
              or (len(self.conv_dim      ) != self.num_feat_extract_layers)
          ):
                raise ValueError(
                    "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
                    " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
                    F" {len(self.conv_dim      )}`, `len(config.conv_stride) = {len(self.conv_stride      )}`,"
                    F" `len(config.conv_kernel) = {len(self.conv_kernel      )}`."      )

          # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
          SCREAMING_SNAKE_CASE_				:      Tuple					=	apply_spec_augment
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	mask_time_prob
          SCREAMING_SNAKE_CASE_				:      List[Any]					=	mask_time_length
          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	mask_time_min_masks
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	mask_feature_prob
          SCREAMING_SNAKE_CASE_				:      List[Any]					=	mask_feature_length
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	mask_feature_min_masks

          # ctc loss
          SCREAMING_SNAKE_CASE_				:      List[Any]					=	ctc_loss_reduction
          SCREAMING_SNAKE_CASE_				:      List[Any]					=	ctc_zero_infinity







    @property
    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          return functools.reduce(operator.mul    ,					self.conv_stride    ,					1      )

