





'''simple docstring'''




import functools
import operator

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


__UpperCAmelCase            =       logging.get_logger(__name__)

__UpperCAmelCase            =       {
    """microsoft/unispeech-large-1500h-cv""": (
        """https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json"""
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}




class      UpperCamelCase__					(					lowercase_			):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__				=	'''unispeech'''


					def __init__(						self				:			Optional[int]      ,						lowerCamelCase_				:			str=32      ,						lowerCamelCase_				:			Dict=7_68      ,						lowerCamelCase_				:			Optional[int]=12      ,						lowerCamelCase_				:			int=12      ,						lowerCamelCase_				:			str=30_72      ,						lowerCamelCase_				:			Optional[int]="gelu"      ,						lowerCamelCase_				:			Any=0.1      ,						lowerCamelCase_				:			Optional[Any]=0.1      ,						lowerCamelCase_				:			Any=0.1      ,						lowerCamelCase_				:			Tuple=0.0      ,						lowerCamelCase_				:			Optional[Any]=0.0      ,						lowerCamelCase_				:			int=0.1      ,						lowerCamelCase_				:			Optional[int]=0.1      ,						lowerCamelCase_				:			Optional[int]=0.02      ,						lowerCamelCase_				:			List[str]=1e-5      ,						lowerCamelCase_				:			str="group"      ,						lowerCamelCase_				:			Optional[Any]="gelu"      ,						lowerCamelCase_				:			Optional[Any]=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)      ,						lowerCamelCase_				:			Any=(5, 2, 2, 2, 2, 2, 2)      ,						lowerCamelCase_				:			int=(10, 3, 3, 3, 3, 2, 2)      ,						lowerCamelCase_				:			Optional[int]=False      ,						lowerCamelCase_				:			Optional[int]=1_28      ,						lowerCamelCase_				:			str=16      ,						lowerCamelCase_				:			str=False      ,						lowerCamelCase_				:			int=True      ,						lowerCamelCase_				:			List[Any]=0.05      ,						lowerCamelCase_				:			Any=10      ,						lowerCamelCase_				:			str=2      ,						lowerCamelCase_				:			Optional[Any]=0.0      ,						lowerCamelCase_				:			List[Any]=10      ,						lowerCamelCase_				:			Optional[Any]=0      ,						lowerCamelCase_				:			List[Any]=3_20      ,						lowerCamelCase_				:			Optional[int]=2      ,						lowerCamelCase_				:			Optional[Any]=0.1      ,						lowerCamelCase_				:			Any=1_00      ,						lowerCamelCase_				:			int=2_56      ,						lowerCamelCase_				:			Optional[Any]=2_56      ,						lowerCamelCase_				:			Any=0.1      ,						lowerCamelCase_				:			List[Any]="mean"      ,						lowerCamelCase_				:			Tuple=False      ,						lowerCamelCase_				:			Union[str, Any]=False      ,						lowerCamelCase_				:			List[Any]=2_56      ,						lowerCamelCase_				:			Dict=80      ,						lowerCamelCase_				:			Optional[int]=0      ,						lowerCamelCase_				:			Optional[int]=1      ,						lowerCamelCase_				:			str=2      ,						lowerCamelCase_				:			List[str]=0.5      ,						**lowerCamelCase_				:			List[Any]      ,						):

							'''simple docstring'''



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

							# parameters for pretraining with codevector quantized representations
							SCREAMING_SNAKE_CASE :				Dict		=			num_codevectors_per_group
							SCREAMING_SNAKE_CASE :				str		=			num_codevector_groups
							SCREAMING_SNAKE_CASE :				Tuple		=			contrastive_logits_temperature
							SCREAMING_SNAKE_CASE :				Dict		=			feat_quantizer_dropout
							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			num_negatives
							SCREAMING_SNAKE_CASE :				int		=			codevector_dim
							SCREAMING_SNAKE_CASE :				Tuple		=			proj_codevector_dim
							SCREAMING_SNAKE_CASE :				int		=			diversity_loss_weight

							# ctc loss
							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			ctc_loss_reduction
							SCREAMING_SNAKE_CASE :				List[Any]		=			ctc_zero_infinity

							# pretraining loss
							SCREAMING_SNAKE_CASE :				str		=			replace_prob



					@property
					def lowerCamelCase_			(						self				:			Union[str, Any]						):

							'''simple docstring'''



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










'''simple docstring'''




import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
							from PIL import Image

							from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
							import torch

if is_tf_available():
							import tensorflow as tf




@require_vision
@require_torchvision
class      UpperCamelCase__					(					unittest.TestCase			):




					"""simple docstring"""


					def lowerCamelCase_			(						self				:			Any						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			tempfile.mkdtemp()
							SCREAMING_SNAKE_CASE :				List[Any]		=			SamImageProcessor()
							SCREAMING_SNAKE_CASE :				Optional[Any]		=			SamProcessor(lowerCamelCase_						)
							processor.save_pretrained(self.tmpdirname						)


					def lowerCamelCase_			(						self				:			Dict      ,						**lowerCamelCase_				:			List[str]						):

							'''simple docstring'''



							return AutoProcessor.from_pretrained(self.tmpdirname      ,						**lowerCamelCase_						).image_processor


					def lowerCamelCase_			(						self				:			Dict						):

							'''simple docstring'''



							shutil.rmtree(self.tmpdirname						)


					def lowerCamelCase_			(						self				:			str						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Optional[int]		=			[np.random.randint(2_55      ,						size=(3, 30, 4_00)      ,						dtype=np.uinta						)]

							SCREAMING_SNAKE_CASE :				Optional[int]		=			[Image.fromarray(np.moveaxis(lowerCamelCase_      ,						0      ,						-1						)						) for x in image_inputs]

							return image_inputs


					def lowerCamelCase_			(						self				:			Any						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				List[Any]		=			SamProcessor(image_processor=self.get_image_processor()						)
							processor.save_pretrained(self.tmpdirname						)

							SCREAMING_SNAKE_CASE :				int		=			self.get_image_processor(do_normalize=lowerCamelCase_      ,						padding_value=1.0						)

							SCREAMING_SNAKE_CASE :				Tuple		=			SamProcessor.from_pretrained(self.tmpdirname      ,						do_normalize=lowerCamelCase_      ,						padding_value=1.0						)

							self.assertEqual(processor.image_processor.to_json_string()      ,						image_processor_add_kwargs.to_json_string()						)
							self.assertIsInstance(processor.image_processor      ,						lowerCamelCase_						)


					def lowerCamelCase_			(						self				:			Tuple						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Optional[Any]		=			self.get_image_processor()

							SCREAMING_SNAKE_CASE :				List[Any]		=			SamProcessor(image_processor=lowerCamelCase_						)

							SCREAMING_SNAKE_CASE :				Optional[int]		=			self.prepare_image_inputs()

							SCREAMING_SNAKE_CASE :				List[str]		=			image_processor(lowerCamelCase_      ,						return_tensors="""np"""						)
							SCREAMING_SNAKE_CASE :				Dict		=			processor(images=lowerCamelCase_      ,						return_tensors="""np"""						)

							input_feat_extract.pop("""original_sizes"""						)  # pop original_sizes as it is popped in the processor
							input_feat_extract.pop("""reshaped_input_sizes"""						)  # pop original_sizes as it is popped in the processor

							for key in input_feat_extract.keys():
									self.assertAlmostEqual(input_feat_extract[key].sum()      ,						input_processor[key].sum()      ,						delta=1e-2						)



					@require_torch
					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				List[str]		=			self.get_image_processor()

							SCREAMING_SNAKE_CASE :				Optional[Any]		=			SamProcessor(image_processor=lowerCamelCase_						)
							SCREAMING_SNAKE_CASE :				Any		=			[torch.ones((1, 3, 5, 5)						)]

							SCREAMING_SNAKE_CASE :				int		=			[[17_64, 26_46]]

							SCREAMING_SNAKE_CASE :				Any		=			[[6_83, 10_24]]
							SCREAMING_SNAKE_CASE :				str		=			processor.post_process_masks(lowerCamelCase_      ,						lowerCamelCase_      ,						lowerCamelCase_						)
							self.assertEqual(masks[0].shape      ,						(1, 3, 17_64, 26_46)						)

							SCREAMING_SNAKE_CASE :				str		=			processor.post_process_masks(
							    lowerCamelCase_      ,						torch.tensor(lowerCamelCase_						)      ,						torch.tensor(lowerCamelCase_						)						)
							self.assertEqual(masks[0].shape      ,						(1, 3, 17_64, 26_46)						)

							# should also work with np
							SCREAMING_SNAKE_CASE :				Optional[int]		=			[np.ones((1, 3, 5, 5)						)]
							SCREAMING_SNAKE_CASE :				Optional[int]		=			processor.post_process_masks(lowerCamelCase_      ,						np.array(lowerCamelCase_						)      ,						np.array(lowerCamelCase_						)						)

							self.assertEqual(masks[0].shape      ,						(1, 3, 17_64, 26_46)						)

							SCREAMING_SNAKE_CASE :				Tuple		=			[[1, 0], [0, 1]]
							with self.assertRaises(lowerCamelCase_						):
									SCREAMING_SNAKE_CASE :				Tuple		=			processor.post_process_masks(lowerCamelCase_      ,						np.array(lowerCamelCase_						)      ,						np.array(lowerCamelCase_						)						)







@require_vision
@require_tf
class      UpperCamelCase__					(					unittest.TestCase			):




					"""simple docstring"""


					def lowerCamelCase_			(						self				:			Tuple						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			tempfile.mkdtemp()
							SCREAMING_SNAKE_CASE :				Optional[Any]		=			SamImageProcessor()
							SCREAMING_SNAKE_CASE :				Optional[int]		=			SamProcessor(lowerCamelCase_						)
							processor.save_pretrained(self.tmpdirname						)


					def lowerCamelCase_			(						self				:			Any      ,						**lowerCamelCase_				:			str						):

							'''simple docstring'''



							return AutoProcessor.from_pretrained(self.tmpdirname      ,						**lowerCamelCase_						).image_processor


					def lowerCamelCase_			(						self				:			List[Any]						):

							'''simple docstring'''



							shutil.rmtree(self.tmpdirname						)


					def lowerCamelCase_			(						self				:			Union[str, Any]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Any		=			[np.random.randint(2_55      ,						size=(3, 30, 4_00)      ,						dtype=np.uinta						)]

							SCREAMING_SNAKE_CASE :				str		=			[Image.fromarray(np.moveaxis(lowerCamelCase_      ,						0      ,						-1						)						) for x in image_inputs]

							return image_inputs


					def lowerCamelCase_			(						self				:			List[Any]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			SamProcessor(image_processor=self.get_image_processor()						)
							processor.save_pretrained(self.tmpdirname						)

							SCREAMING_SNAKE_CASE :				Optional[Any]		=			self.get_image_processor(do_normalize=lowerCamelCase_      ,						padding_value=1.0						)

							SCREAMING_SNAKE_CASE :				List[Any]		=			SamProcessor.from_pretrained(self.tmpdirname      ,						do_normalize=lowerCamelCase_      ,						padding_value=1.0						)

							self.assertEqual(processor.image_processor.to_json_string()      ,						image_processor_add_kwargs.to_json_string()						)
							self.assertIsInstance(processor.image_processor      ,						lowerCamelCase_						)


					def lowerCamelCase_			(						self				:			Optional[Any]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				str		=			self.get_image_processor()

							SCREAMING_SNAKE_CASE :				Dict		=			SamProcessor(image_processor=lowerCamelCase_						)

							SCREAMING_SNAKE_CASE :				int		=			self.prepare_image_inputs()

							SCREAMING_SNAKE_CASE :				Optional[Any]		=			image_processor(lowerCamelCase_      ,						return_tensors="""np"""						)
							SCREAMING_SNAKE_CASE :				List[Any]		=			processor(images=lowerCamelCase_      ,						return_tensors="""np"""						)

							input_feat_extract.pop("""original_sizes"""						)  # pop original_sizes as it is popped in the processor
							input_feat_extract.pop("""reshaped_input_sizes"""						)  # pop reshaped_input_sizes as it is popped in the processor

							for key in input_feat_extract.keys():
									self.assertAlmostEqual(input_feat_extract[key].sum()      ,						input_processor[key].sum()      ,						delta=1e-2						)



					@require_tf
					def lowerCamelCase_			(						self				:			Dict						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Dict		=			self.get_image_processor()

							SCREAMING_SNAKE_CASE :				Tuple		=			SamProcessor(image_processor=lowerCamelCase_						)
							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			[tf.ones((1, 3, 5, 5)						)]

							SCREAMING_SNAKE_CASE :				Dict		=			[[17_64, 26_46]]

							SCREAMING_SNAKE_CASE :				Any		=			[[6_83, 10_24]]
							SCREAMING_SNAKE_CASE :				str		=			processor.post_process_masks(lowerCamelCase_      ,						lowerCamelCase_      ,						lowerCamelCase_      ,						return_tensors="""tf"""						)
							self.assertEqual(masks[0].shape      ,						(1, 3, 17_64, 26_46)						)

							SCREAMING_SNAKE_CASE :				Optional[int]		=			processor.post_process_masks(
							    lowerCamelCase_      ,						tf.convert_to_tensor(lowerCamelCase_						)      ,						tf.convert_to_tensor(lowerCamelCase_						)      ,						return_tensors="""tf"""      ,						)
							self.assertEqual(masks[0].shape      ,						(1, 3, 17_64, 26_46)						)

							# should also work with np
							SCREAMING_SNAKE_CASE :				Optional[int]		=			[np.ones((1, 3, 5, 5)						)]
							SCREAMING_SNAKE_CASE :				List[str]		=			processor.post_process_masks(
							    lowerCamelCase_      ,						np.array(lowerCamelCase_						)      ,						np.array(lowerCamelCase_						)      ,						return_tensors="""tf"""						)

							self.assertEqual(masks[0].shape      ,						(1, 3, 17_64, 26_46)						)

							SCREAMING_SNAKE_CASE :				str		=			[[1, 0], [0, 1]]
							with self.assertRaises(tf.errors.InvalidArgumentError						):
									SCREAMING_SNAKE_CASE :				Tuple		=			processor.post_process_masks(
									    lowerCamelCase_      ,						np.array(lowerCamelCase_						)      ,						np.array(lowerCamelCase_						)      ,						return_tensors="""tf"""						)







@require_vision
@require_torchvision
class      UpperCamelCase__					(					unittest.TestCase			):




					"""simple docstring"""


					def lowerCamelCase_			(						self				:			Tuple						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				int		=			tempfile.mkdtemp()
							SCREAMING_SNAKE_CASE :				int		=			SamImageProcessor()
							SCREAMING_SNAKE_CASE :				str		=			SamProcessor(lowerCamelCase_						)
							processor.save_pretrained(self.tmpdirname						)


					def lowerCamelCase_			(						self				:			Tuple      ,						**lowerCamelCase_				:			List[Any]						):

							'''simple docstring'''



							return AutoProcessor.from_pretrained(self.tmpdirname      ,						**lowerCamelCase_						).image_processor


					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							shutil.rmtree(self.tmpdirname						)


					def lowerCamelCase_			(						self				:			Dict						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Optional[int]		=			[np.random.randint(2_55      ,						size=(3, 30, 4_00)      ,						dtype=np.uinta						)]

							SCREAMING_SNAKE_CASE :				Dict		=			[Image.fromarray(np.moveaxis(lowerCamelCase_      ,						0      ,						-1						)						) for x in image_inputs]

							return image_inputs


					@is_pt_tf_cross_test
					def lowerCamelCase_			(						self				:			Optional[Any]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				str		=			self.get_image_processor()

							SCREAMING_SNAKE_CASE :				Dict		=			SamProcessor(image_processor=lowerCamelCase_						)
							SCREAMING_SNAKE_CASE :				Any		=			np.random.randint(0      ,						2      ,						size=(1, 3, 5, 5)						).astype(np.floataa						)
							SCREAMING_SNAKE_CASE :				str		=			[tf.convert_to_tensor(lowerCamelCase_						)]
							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			[torch.tensor(lowerCamelCase_						)]

							SCREAMING_SNAKE_CASE :				Tuple		=			[[17_64, 26_46]]

							SCREAMING_SNAKE_CASE :				Optional[Any]		=			[[6_83, 10_24]]
							SCREAMING_SNAKE_CASE :				Tuple		=			processor.post_process_masks(
							    lowerCamelCase_      ,						lowerCamelCase_      ,						lowerCamelCase_      ,						return_tensors="""tf"""						)
							SCREAMING_SNAKE_CASE :				Any		=			processor.post_process_masks(
							    lowerCamelCase_      ,						lowerCamelCase_      ,						lowerCamelCase_      ,						return_tensors="""pt"""						)

							self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()						)						)



					@is_pt_tf_cross_test
					def lowerCamelCase_			(						self				:			Tuple						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			self.get_image_processor()

							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			SamProcessor(image_processor=lowerCamelCase_						)

							SCREAMING_SNAKE_CASE :				Any		=			self.prepare_image_inputs()

							SCREAMING_SNAKE_CASE :				int		=			image_processor(lowerCamelCase_      ,						return_tensors="""pt"""						)["""pixel_values"""].numpy()
							SCREAMING_SNAKE_CASE :				Tuple		=			processor(images=lowerCamelCase_      ,						return_tensors="""pt"""						)["""pixel_values"""].numpy()

							SCREAMING_SNAKE_CASE :				Dict		=			image_processor(lowerCamelCase_      ,						return_tensors="""tf"""						)["""pixel_values"""].numpy()
							SCREAMING_SNAKE_CASE :				Dict		=			processor(images=lowerCamelCase_      ,						return_tensors="""tf"""						)["""pixel_values"""].numpy()

							self.assertTrue(np.allclose(lowerCamelCase_      ,						lowerCamelCase_						)						)
							self.assertTrue(np.allclose(lowerCamelCase_      ,						lowerCamelCase_						)						)
							self.assertTrue(np.allclose(lowerCamelCase_      ,						lowerCamelCase_						)						)



