


'''simple docstring'''






import inspect
import unittest

from transformers import RegNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
		import jax
		import jax.numpy as jnp

		from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor




class 						a__							(				unittest.TestCase    ):





							'''simple docstring'''






							def __init__(						self   ,       lowerCamelCase_   ,       lowerCamelCase_=3   ,       lowerCamelCase_=32   ,       lowerCamelCase_=3   ,       lowerCamelCase_=10   ,       lowerCamelCase_=[10, 20, 30, 40]   ,       lowerCamelCase_=[1, 1, 2, 1]   ,       lowerCamelCase_=True   ,       lowerCamelCase_=True   ,       lowerCamelCase_="relu"   ,       lowerCamelCase_=3   ,       lowerCamelCase_=None   ,       )			->				Any:
														lowerCAmelCase__			  =      parent
														lowerCAmelCase__			  =      batch_size
														lowerCAmelCase__			  =      image_size
														lowerCAmelCase__			  =      num_channels
														lowerCAmelCase__			  =      embeddings_size
														lowerCAmelCase__			  =      hidden_sizes
														lowerCAmelCase__			  =      depths
														lowerCAmelCase__			  =      is_training
														lowerCAmelCase__			  =      use_labels
														lowerCAmelCase__			  =      hidden_act
														lowerCAmelCase__			  =      num_labels
														lowerCAmelCase__			  =      scope
														lowerCAmelCase__			  =      len(lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

														lowerCAmelCase__			  =      self.get_config()

														return config, pixel_values







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														return RegNetConfig(
														    num_channels=self.num_channels   ,       embeddings_size=self.embeddings_size   ,       hidden_sizes=self.hidden_sizes   ,       depths=self.depths   ,       hidden_act=self.hidden_act   ,       num_labels=self.num_labels   ,       image_size=self.image_size   ,       )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				Optional[Any]:
														lowerCAmelCase__			  =      FlaxRegNetModel(config=lowerCamelCase_  )
														lowerCAmelCase__			  =      model(lowerCamelCase_  )

														# Output shape (b, c, h, w)
														self.parent.assertEqual(
														    result.last_hidden_state.shape   ,       (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)   ,       )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				Any:
														lowerCAmelCase__			  =      self.num_labels
														lowerCAmelCase__			  =      FlaxRegNetForImageClassification(config=lowerCamelCase_  )
														lowerCAmelCase__			  =      model(lowerCamelCase_  )
														self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.num_labels)  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				int:
														lowerCAmelCase__			  =      self.prepare_config_and_inputs()
														lowerCAmelCase__  ,   lowerCAmelCase__			  =      config_and_inputs
														lowerCAmelCase__			  =      {'''pixel_values''': pixel_values}
														return config, inputs_dict




@require_flax
class 						a__							(				a__							,							unittest.TestCase    ):





							'''simple docstring'''






							lowercase__     :  Any      		= (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

							lowercase__     :  List[Any]      		= False
							lowercase__     :  Union[str, Any]      		= False
							lowercase__     :  Optional[int]      		= False







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				None:
														lowerCAmelCase__			  =      FlaxRegNetModelTester(self  )
														lowerCAmelCase__			  =      ConfigTester(self   ,       config_class=lowerCamelCase_   ,       has_text_modality=lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														self.create_and_test_config_common_properties()
														self.config_tester.create_and_test_config_to_json_string()
														self.config_tester.create_and_test_config_to_json_file()
														self.config_tester.create_and_test_config_from_and_save_pretrained()
														self.config_tester.create_and_test_config_with_num_labels()
														self.config_tester.check_config_can_be_init_without_params()
														self.config_tester.check_config_arguments_init()







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[Any]:
														return







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[Any]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[str]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_  )







							@unittest.skip(reason='''RegNet does not use inputs_embeds'''  )
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[str]:
														pass







							@unittest.skip(reason='''RegNet does not support input and output embeddings'''  )
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														pass







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Tuple:
														lowerCAmelCase__  ,   lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCAmelCase__			  =      model_class(lowerCamelCase_  )
																					lowerCAmelCase__			  =      inspect.signature(model.__call__  )
																					# signature.parameters is an OrderedDict => so arg_names order is deterministic
																					lowerCAmelCase__			  =      [*signature.parameters.keys()]

																					lowerCAmelCase__			  =      ['''pixel_values''']
																					self.assertListEqual(arg_names[:1]   ,       lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Any:
														def check_hidden_states_output(lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  ):
																					lowerCAmelCase__			  =      model_class(lowerCamelCase_  )

																					lowerCAmelCase__			  =      model(**self._prepare_for_class(lowerCamelCase_   ,       lowerCamelCase_  )  )

																					lowerCAmelCase__			  =      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																					lowerCAmelCase__			  =      self.model_tester.num_stages
																					self.assertEqual(len(lowerCamelCase_  )   ,       expected_num_stages + 1  )

														lowerCAmelCase__  ,   lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCAmelCase__			  =      True
																					check_hidden_states_output(lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )

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

																					check_hidden_states_output(lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														lowerCAmelCase__  ,   lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					with self.subTest(model_class.__name__  ):
																												lowerCAmelCase__			  =      self._prepare_for_class(lowerCamelCase_   ,       lowerCamelCase_  )
																												lowerCAmelCase__			  =      model_class(lowerCamelCase_  )

																												@jax.jit
																												def model_jitted(lowerCamelCase_   ,       **lowerCamelCase_  ):
																																			return model(pixel_values=lowerCamelCase_   ,       **lowerCamelCase_  )

																												with self.subTest('''JIT Enabled'''  ):
																																			lowerCAmelCase__			  =      model_jitted(**lowerCamelCase_  ).to_tuple()

																												with self.subTest('''JIT Disabled'''  ):
																																			with jax.disable_jit():
																																										lowerCAmelCase__			  =      model_jitted(**lowerCamelCase_  ).to_tuple()

																												self.assertEqual(len(lowerCamelCase_  )   ,       len(lowerCamelCase_  )  )
																												for jitted_output, output in zip(lowerCamelCase_   ,       lowerCamelCase_  ):
																																			self.assertEqual(jitted_output.shape   ,       output.shape  )


def    _snake_case  (	)		->       Union[str, Any]:
							lowerCAmelCase__			  =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
							return image






@require_flax
class 						a__							(				unittest.TestCase    ):





							'''simple docstring'''






							@cached_property
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														return AutoImageProcessor.from_pretrained('''facebook/regnet-y-040'''  ) if is_vision_available() else None







							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				str:
														lowerCAmelCase__			  =      FlaxRegNetForImageClassification.from_pretrained('''facebook/regnet-y-040'''  )

														lowerCAmelCase__			  =      self.default_image_processor
														lowerCAmelCase__			  =      prepare_img()
														lowerCAmelCase__			  =      image_processor(images=lowerCamelCase_   ,       return_tensors='''np'''  )

														lowerCAmelCase__			  =      model(**lowerCamelCase_  )

														# verify the logits
														lowerCAmelCase__			  =      (1, 10_00)
														self.assertEqual(outputs.logits.shape   ,       lowerCamelCase_  )

														lowerCAmelCase__			  =      jnp.array([-0.4_180, -1.5_051, -3.4_836]  )

														self.assertTrue(jnp.allclose(outputs.logits[0, :3]   ,       lowerCamelCase_   ,       atol=1e-4  )  )

'''simple docstring'''
import functools
import operator

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


lowerCAmelCase					:    int           = logging.get_logger(__name__)

lowerCAmelCase					:    Tuple           = {
    'microsoft/unispeech-sat-base-100h-libri-ft': (
        'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}


class 			SCREAMING_SNAKE_CASE__      (     snake_case_):
				lowerCAmelCase_     			=							"""unispeech-sat"""







				def __init__(       self						,				A_=32						,				A_=768						,				A_=12						,				A_=12						,				A_=3072						,				A_="gelu"						,				A_=0.1						,				A_=0.1						,				A_=0.1						,				A_=0.0						,				A_=0.0						,				A_=0.1						,				A_=0.1						,				A_=0.02						,				A_=1e-5						,				A_="group"						,				A_="gelu"						,				A_=(512, 512, 512, 512, 512, 512, 512)						,				A_=(5, 2, 2, 2, 2, 2, 2)						,				A_=(10, 3, 3, 3, 3, 2, 2)						,				A_=False						,				A_=128						,				A_=16						,				A_=False						,				A_=True						,				A_=0.05						,				A_=10						,				A_=2						,				A_=0.0						,				A_=10						,				A_=0						,				A_=320						,				A_=2						,				A_=0.1						,				A_=100						,				A_=256						,				A_=256						,				A_=0.1						,				A_="mean"						,				A_=False						,				A_=False						,				A_=256						,				A_=(512, 512, 512, 512, 1500)						,				A_=(5, 3, 3, 1, 1)						,				A_=(1, 2, 3, 1, 1)						,				A_=512						,				A_=0						,				A_=1						,				A_=2						,				A_=504						,				**A_						,				)->		Tuple:







											'''simple docstring'''






											super().__init__(**A_						,				pad_token_id=A_						,				bos_token_id=A_						,				eos_token_id=A_     )
											UpperCamelCase   			=     hidden_size
											UpperCamelCase   			=     feat_extract_norm
											UpperCamelCase   			=     feat_extract_activation
											UpperCamelCase   			=     list(A_     )
											UpperCamelCase   			=     list(A_     )
											UpperCamelCase   			=     list(A_     )
											UpperCamelCase   			=     conv_bias
											UpperCamelCase   			=     num_conv_pos_embeddings
											UpperCamelCase   			=     num_conv_pos_embedding_groups
											UpperCamelCase   			=     len(self.conv_dim     )
											UpperCamelCase   			=     num_hidden_layers
											UpperCamelCase   			=     intermediate_size
											UpperCamelCase   			=     hidden_act
											UpperCamelCase   			=     num_attention_heads
											UpperCamelCase   			=     hidden_dropout
											UpperCamelCase   			=     attention_dropout
											UpperCamelCase   			=     activation_dropout
											UpperCamelCase   			=     feat_proj_dropout
											UpperCamelCase   			=     final_dropout
											UpperCamelCase   			=     layerdrop
											UpperCamelCase   			=     layer_norm_eps
											UpperCamelCase   			=     initializer_range
											UpperCamelCase   			=     vocab_size
											UpperCamelCase   			=     num_clusters
											UpperCamelCase   			=     do_stable_layer_norm
											UpperCamelCase   			=     use_weighted_layer_sum

											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
											UpperCamelCase   			=     apply_spec_augment
											UpperCamelCase   			=     mask_time_prob
											UpperCamelCase   			=     mask_time_length
											UpperCamelCase   			=     mask_time_min_masks
											UpperCamelCase   			=     mask_feature_prob
											UpperCamelCase   			=     mask_feature_length
											UpperCamelCase   			=     mask_feature_min_masks

											# parameters for pretraining with codevector quantized representations
											UpperCamelCase   			=     num_codevectors_per_group
											UpperCamelCase   			=     num_codevector_groups
											UpperCamelCase   			=     contrastive_logits_temperature
											UpperCamelCase   			=     feat_quantizer_dropout
											UpperCamelCase   			=     num_negatives
											UpperCamelCase   			=     codevector_dim
											UpperCamelCase   			=     proj_codevector_dim
											UpperCamelCase   			=     diversity_loss_weight

											# ctc loss
											UpperCamelCase   			=     ctc_loss_reduction
											UpperCamelCase   			=     ctc_zero_infinity

											# SequenceClassification-specific parameter. Feel free to ignore for other classes.
											UpperCamelCase   			=     classifier_proj_size

											# XVector-specific parameters. Feel free to ignore for other classes.
											UpperCamelCase   			=     list(A_     )
											UpperCamelCase   			=     list(A_     )
											UpperCamelCase   			=     list(A_     )
											UpperCamelCase   			=     xvector_output_dim







				@property
				def 						UpperCAmelCase_    (       self     )->		Optional[Any]:







											'''simple docstring'''






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




