

import functools
import operator

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


lowerCamelCase						 =	logging.get_logger(__name__)

lowerCamelCase						 =	{
    '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 A  (       lowerCamelCase_					):
      UpperCamelCase__							:  Optional[Any]				="""hubert"""






      def __init__( self						:					Optional[int]      ,       lowercase_						:					str=32      ,       lowercase_						:					str=768      ,       lowercase_						:					List[str]=12      ,       lowercase_						:					Union[str, Any]=12      ,       lowercase_						:					int=3072      ,       lowercase_						:					Any="gelu"      ,       lowercase_						:					Tuple=0.1      ,       lowercase_						:					Optional[int]=0.1      ,       lowercase_						:					List[str]=0.1      ,       lowercase_						:					Tuple=True      ,       lowercase_						:					Optional[Any]=0.0      ,       lowercase_						:					List[str]=0.1      ,       lowercase_						:					Tuple=0.1      ,       lowercase_						:					Optional[int]=0.02      ,       lowercase_						:					List[Any]=1E-5      ,       lowercase_						:					List[str]="group"      ,       lowercase_						:					int="gelu"      ,       lowercase_						:					str=(512, 512, 512, 512, 512, 512, 512)      ,       lowercase_						:					int=(5, 2, 2, 2, 2, 2, 2)      ,       lowercase_						:					Tuple=(10, 3, 3, 3, 3, 2, 2)      ,       lowercase_						:					Optional[Any]=False      ,       lowercase_						:					Tuple=128      ,       lowercase_						:					str=16      ,       lowercase_						:					Optional[Any]=False      ,       lowercase_						:					Optional[int]=True      ,       lowercase_						:					List[Any]=0.05      ,       lowercase_						:					Any=10      ,       lowercase_						:					Any=2      ,       lowercase_						:					Any=0.0      ,       lowercase_						:					Any=10      ,       lowercase_						:					Dict=0      ,       lowercase_						:					Optional[Any]="sum"      ,       lowercase_						:					str=False      ,       lowercase_						:					List[str]=False      ,       lowercase_						:					Optional[int]=256      ,       lowercase_						:					Optional[Any]=0      ,       lowercase_						:					Optional[Any]=1      ,       lowercase_						:					Tuple=2      ,       **lowercase_						:					List[str]      ,       ) ->						str:
             """simple docstring"""




             super().__init__(**lowercase_      ,       pad_token_id=lowercase_      ,       bos_token_id=lowercase_      ,       eos_token_id=lowercase_			)
             _lowerCamelCase	:		Optional[Any]          =hidden_size
             _lowerCamelCase	:		Dict          =feat_extract_norm
             _lowerCamelCase	:		Tuple          =feat_extract_activation
             _lowerCamelCase	:		Dict          =list(lowercase_			)
             _lowerCamelCase	:		Optional[Any]          =list(lowercase_			)
             _lowerCamelCase	:		List[str]          =list(lowercase_			)
             _lowerCamelCase	:		Any          =conv_bias
             _lowerCamelCase	:		Optional[Any]          =num_conv_pos_embeddings
             _lowerCamelCase	:		Optional[Any]          =num_conv_pos_embedding_groups
             _lowerCamelCase	:		int          =len(self.conv_dim			)
             _lowerCamelCase	:		Any          =num_hidden_layers
             _lowerCamelCase	:		Tuple          =intermediate_size
             _lowerCamelCase	:		str          =hidden_act
             _lowerCamelCase	:		Union[str, Any]          =num_attention_heads
             _lowerCamelCase	:		Union[str, Any]          =hidden_dropout
             _lowerCamelCase	:		List[Any]          =attention_dropout
             _lowerCamelCase	:		Optional[int]          =activation_dropout
             _lowerCamelCase	:		List[Any]          =feat_proj_layer_norm
             _lowerCamelCase	:		int          =feat_proj_dropout
             _lowerCamelCase	:		Optional[int]          =final_dropout
             _lowerCamelCase	:		Any          =layerdrop
             _lowerCamelCase	:		Optional[Any]          =layer_norm_eps
             _lowerCamelCase	:		Any          =initializer_range
             _lowerCamelCase	:		Tuple          =vocab_size
             _lowerCamelCase	:		Any          =do_stable_layer_norm
             _lowerCamelCase	:		List[Any]          =use_weighted_layer_sum
             _lowerCamelCase	:		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
             _lowerCamelCase	:		int          =apply_spec_augment
             _lowerCamelCase	:		Union[str, Any]          =mask_time_prob
             _lowerCamelCase	:		Optional[Any]          =mask_time_length
             _lowerCamelCase	:		int          =mask_time_min_masks
             _lowerCamelCase	:		Dict          =mask_feature_prob
             _lowerCamelCase	:		Tuple          =mask_feature_length
             _lowerCamelCase	:		Union[str, Any]          =mask_feature_min_masks

             # ctc loss
             _lowerCamelCase	:		Union[str, Any]          =ctc_loss_reduction
             _lowerCamelCase	:		Dict          =ctc_zero_infinity






      @property
      def 	lowerCamelCase	( self						:					Union[str, Any]			) ->						Any:
             """simple docstring"""




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






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


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import CLIPImageProcessor




class A  (       unittest.TestCase					):






      def __init__( self						:					Union[str, Any]      ,       lowercase_						:					Any      ,       lowercase_						:					Optional[Any]=7      ,       lowercase_						:					Union[str, Any]=3      ,       lowercase_						:					Any=18      ,       lowercase_						:					Optional[int]=30      ,       lowercase_						:					int=400      ,       lowercase_						:					List[Any]=True      ,       lowercase_						:					List[str]=None      ,       lowercase_						:					Optional[Any]=True      ,       lowercase_						:					Tuple=None      ,       lowercase_						:					str=True      ,       lowercase_						:					Any=[0.48145466, 0.4578275, 0.40821073]      ,       lowercase_						:					Optional[int]=[0.26862954, 0.26130258, 0.27577711]      ,       lowercase_						:					Dict=True      ,       ) ->						str:
             """simple docstring"""




             _lowerCamelCase	:		List[str]          =size if size is not None else {'shortest_edge': 20}
             _lowerCamelCase	:		Optional[Any]          =crop_size if crop_size is not None else {'height': 18, 'width': 18}
             _lowerCamelCase	:		Any          =parent
             _lowerCamelCase	:		List[Any]          =batch_size
             _lowerCamelCase	:		Union[str, Any]          =num_channels
             _lowerCamelCase	:		str          =image_size
             _lowerCamelCase	:		str          =min_resolution
             _lowerCamelCase	:		Optional[int]          =max_resolution
             _lowerCamelCase	:		str          =do_resize
             _lowerCamelCase	:		Tuple          =size
             _lowerCamelCase	:		Any          =do_center_crop
             _lowerCamelCase	:		Any          =crop_size
             _lowerCamelCase	:		Optional[Any]          =do_normalize
             _lowerCamelCase	:		List[Any]          =image_mean
             _lowerCamelCase	:		int          =image_std
             _lowerCamelCase	:		Any          =do_convert_rgb






      def 	lowerCamelCase	( self						:					int			) ->						Tuple:
             """simple docstring"""




             return {
                 "do_resize": self.do_resize,
                 "size": self.size,
                 "do_center_crop": self.do_center_crop,
                 "crop_size": self.crop_size,
                 "do_normalize": self.do_normalize,
                 "image_mean": self.image_mean,
                 "image_std": self.image_std,
                 "do_convert_rgb": self.do_convert_rgb,
             }






      def 	lowerCamelCase	( self						:					List[str]      ,       lowercase_						:					Any=False      ,       lowercase_						:					str=False      ,       lowercase_						:					Union[str, Any]=False			) ->						Tuple:
             """simple docstring"""




             assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

             if equal_resolution:
                    _lowerCamelCase	:		int          =[]
                    for i in range(self.batch_size			):
                           image_inputs.append(
                               np.random.randint(
                                   255      ,       size=(self.num_channels, self.max_resolution, self.max_resolution)      ,       dtype=np.uinta			)			)
             else:
                    _lowerCamelCase	:		Dict          =[]
                    for i in range(self.batch_size			):
                           _lowerCamelCase     ,					_lowerCamelCase	:		str          =np.random.choice(np.arange(self.min_resolution      ,       self.max_resolution			)      ,       2			)
                           image_inputs.append(np.random.randint(255      ,       size=(self.num_channels, width, height)      ,       dtype=np.uinta			)			)

             if not numpify and not torchify:
                    # PIL expects the channel dimension as last dimension
                    _lowerCamelCase	:		Optional[int]          =[Image.fromarray(np.moveaxis(lowercase_      ,       0      ,       -1			)			) for x in image_inputs]

             if torchify:
                    _lowerCamelCase	:		Tuple          =[torch.from_numpy(lowercase_			) for x in image_inputs]

             return image_inputs




@require_torch
@require_vision
class A  (       UpperCamelCase_   ,							unittest.TestCase					):
      UpperCamelCase__							:  Tuple				=CLIPImageProcessor if is_vision_available() else None






      def 	lowerCamelCase	( self						:					Optional[Any]			) ->						Dict:
             """simple docstring"""




             _lowerCamelCase	:		Tuple          =CLIPImageProcessingTester(self			)






      @property
      def 	lowerCamelCase	( self						:					Tuple			) ->						Dict:
             """simple docstring"""




             return self.image_processor_tester.prepare_image_processor_dict()






      def 	lowerCamelCase	( self						:					int			) ->						str:
             """simple docstring"""




             _lowerCamelCase	:		Optional[int]          =self.image_processing_class(**self.image_processor_dict			)
             self.assertTrue(hasattr(lowercase_      ,       'do_resize'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'size'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'do_center_crop'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'center_crop'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'do_normalize'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'image_mean'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'image_std'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'do_convert_rgb'			)			)






      def 	lowerCamelCase	( self						:					Any			) ->						Tuple:
             """simple docstring"""




             _lowerCamelCase	:		int          =self.image_processing_class.from_dict(self.image_processor_dict			)
             self.assertEqual(image_processor.size      ,       {'shortest_edge': 20}			)
             self.assertEqual(image_processor.crop_size      ,       {'height': 18, 'width': 18}			)

             _lowerCamelCase	:		Optional[Any]          =self.image_processing_class.from_dict(self.image_processor_dict      ,       size=42      ,       crop_size=84			)
             self.assertEqual(image_processor.size      ,       {'shortest_edge': 42}			)
             self.assertEqual(image_processor.crop_size      ,       {'height': 84, 'width': 84}			)






      def 	lowerCamelCase	( self						:					Tuple			) ->						Optional[Any]:
             """simple docstring"""




             pass






      def 	lowerCamelCase	( self						:					List[Any]			) ->						Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase	:		str          =self.image_processing_class(**self.image_processor_dict			)
             # create random PIL images
             _lowerCamelCase	:		Any          =self.image_processor_tester.prepare_inputs(equal_resolution=lowercase_			)
             for image in image_inputs:
                    self.assertIsInstance(lowercase_      ,       Image.Image			)

             # Test not batched input
             _lowerCamelCase	:		Optional[Any]          =image_processing(image_inputs[0]      ,       return_tensors='pt'			).pixel_values
             self.assertEqual(
                 encoded_images.shape      ,       (
                     1,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size['height'],
                     self.image_processor_tester.crop_size['width'],
                 )      ,       )

             # Test batched
             _lowerCamelCase	:		Optional[Any]          =image_processing(lowercase_      ,       return_tensors='pt'			).pixel_values
             self.assertEqual(
                 encoded_images.shape      ,       (
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size['height'],
                     self.image_processor_tester.crop_size['width'],
                 )      ,       )






      def 	lowerCamelCase	( self						:					List[Any]			) ->						Optional[int]:
             """simple docstring"""




             _lowerCamelCase	:		Tuple          =self.image_processing_class(**self.image_processor_dict			)
             # create random numpy tensors
             _lowerCamelCase	:		Tuple          =self.image_processor_tester.prepare_inputs(equal_resolution=lowercase_      ,       numpify=lowercase_			)
             for image in image_inputs:
                    self.assertIsInstance(lowercase_      ,       np.ndarray			)

             # Test not batched input
             _lowerCamelCase	:		Any          =image_processing(image_inputs[0]      ,       return_tensors='pt'			).pixel_values
             self.assertEqual(
                 encoded_images.shape      ,       (
                     1,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size['height'],
                     self.image_processor_tester.crop_size['width'],
                 )      ,       )

             # Test batched
             _lowerCamelCase	:		str          =image_processing(lowercase_      ,       return_tensors='pt'			).pixel_values
             self.assertEqual(
                 encoded_images.shape      ,       (
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size['height'],
                     self.image_processor_tester.crop_size['width'],
                 )      ,       )






      def 	lowerCamelCase	( self						:					List[str]			) ->						Tuple:
             """simple docstring"""




             _lowerCamelCase	:		int          =self.image_processing_class(**self.image_processor_dict			)
             # create random PyTorch tensors
             _lowerCamelCase	:		Dict          =self.image_processor_tester.prepare_inputs(equal_resolution=lowercase_      ,       torchify=lowercase_			)
             for image in image_inputs:
                    self.assertIsInstance(lowercase_      ,       torch.Tensor			)

             # Test not batched input
             _lowerCamelCase	:		List[str]          =image_processing(image_inputs[0]      ,       return_tensors='pt'			).pixel_values
             self.assertEqual(
                 encoded_images.shape      ,       (
                     1,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size['height'],
                     self.image_processor_tester.crop_size['width'],
                 )      ,       )

             # Test batched
             _lowerCamelCase	:		str          =image_processing(lowercase_      ,       return_tensors='pt'			).pixel_values
             self.assertEqual(
                 encoded_images.shape      ,       (
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size['height'],
                     self.image_processor_tester.crop_size['width'],
                 )      ,       )




@require_torch
@require_vision
class A  (       UpperCamelCase_   ,							unittest.TestCase					):
      UpperCamelCase__							:  List[str]				=CLIPImageProcessor if is_vision_available() else None






      def 	lowerCamelCase	( self						:					Union[str, Any]			) ->						str:
             """simple docstring"""




             _lowerCamelCase	:		int          =CLIPImageProcessingTester(self      ,       num_channels=4			)
             _lowerCamelCase	:		Any          =3






      @property
      def 	lowerCamelCase	( self						:					Union[str, Any]			) ->						Tuple:
             """simple docstring"""




             return self.image_processor_tester.prepare_image_processor_dict()






      def 	lowerCamelCase	( self						:					Optional[Any]			) ->						Any:
             """simple docstring"""




             _lowerCamelCase	:		Tuple          =self.image_processing_class(**self.image_processor_dict			)
             self.assertTrue(hasattr(lowercase_      ,       'do_resize'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'size'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'do_center_crop'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'center_crop'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'do_normalize'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'image_mean'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'image_std'			)			)
             self.assertTrue(hasattr(lowercase_      ,       'do_convert_rgb'			)			)






      def 	lowerCamelCase	( self						:					Optional[int]			) ->						Optional[int]:
             """simple docstring"""




             pass






      def 	lowerCamelCase	( self						:					Any			) ->						Any:
             """simple docstring"""




             _lowerCamelCase	:		Union[str, Any]          =self.image_processing_class(**self.image_processor_dict			)
             # create random PIL images
             _lowerCamelCase	:		Optional[Any]          =self.image_processor_tester.prepare_inputs(equal_resolution=lowercase_			)
             for image in image_inputs:
                    self.assertIsInstance(lowercase_      ,       Image.Image			)

             # Test not batched input
             _lowerCamelCase	:		Any          =image_processing(image_inputs[0]      ,       return_tensors='pt'			).pixel_values
             self.assertEqual(
                 encoded_images.shape      ,       (
                     1,
                     self.expected_encoded_image_num_channels,
                     self.image_processor_tester.crop_size['height'],
                     self.image_processor_tester.crop_size['width'],
                 )      ,       )

             # Test batched
             _lowerCamelCase	:		Optional[Any]          =image_processing(lowercase_      ,       return_tensors='pt'			).pixel_values
             self.assertEqual(
                 encoded_images.shape      ,       (
                     self.image_processor_tester.batch_size,
                     self.expected_encoded_image_num_channels,
                     self.image_processor_tester.crop_size['height'],
                     self.image_processor_tester.crop_size['width'],
                 )      ,       )


