


import itertools
import random
import unittest

import numpy as np

from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaConfig, WavaVecaFeatureExtractor
from transformers.testing_utils import require_torch, slow

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


lowercase_	   =     random.Random()
def 				UpperCamelCase__					(  SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__=1.0      ,	SCREAMING_SNAKE_CASE__=None      ,	SCREAMING_SNAKE_CASE__=None						):




 if rng is None:
  __lowerCamelCase		:							Union[str, Any]		 =						global_rng

 __lowerCamelCase		:							Optional[Any]		 =						[]
 for batch_idx in range(shape[0]						):
  values.append([]						)
  for _ in range(shape[1]						):
   values[-1].append(rng.random() * scale						)

 return values


class 	A_ (	unittest.TestCase   ):




      def __init__(    self:	str						,				a:	int						,				a:	Any=7						,				a:	Optional[Any]=400						,				a:	List[str]=2000						,				a:	Union[str, Any]=1						,				a:	Union[str, Any]=0.0						,				a:	Optional[int]=1_6000						,				a:	List[Any]=True						,				a:	Any=True						,				):
       __lowerCamelCase		:							int		 =						parent
       __lowerCamelCase		:							Dict		 =						batch_size
       __lowerCamelCase		:							str		 =						min_seq_length
       __lowerCamelCase		:							List[str]		 =						max_seq_length
       __lowerCamelCase		:							str		 =						(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
       __lowerCamelCase		:							List[str]		 =						feature_size
       __lowerCamelCase		:							List[Any]		 =						padding_value
       __lowerCamelCase		:							Optional[Any]		 =						sampling_rate
       __lowerCamelCase		:							Tuple		 =						return_attention_mask
       __lowerCamelCase		:							Tuple		 =						do_normalize




      def 					_snake_case     (    self:	Dict    ):
       return {
           "feature_size": self.feature_size,
           "padding_value": self.padding_value,
           "sampling_rate": self.sampling_rate,
           "return_attention_mask": self.return_attention_mask,
           "do_normalize": self.do_normalize,
       }




      def 					_snake_case     (    self:	Tuple						,				a:	Optional[int]=False						,				a:	str=False    ):
       def _flatten(a:	List[str]    ):
        return list(itertools.chain(*__a    )    )

       if equal_length:
        __lowerCamelCase		:							int		 =						floats_list((self.batch_size, self.max_seq_length)    )
       else:
        # make sure that inputs increase in size
        __lowerCamelCase		:							Optional[Any]		 =						[
            _flatten(floats_list((x, self.feature_size)    )    )
            for x in range(self.min_seq_length						,				self.max_seq_length						,				self.seq_length_diff    )
        ]

       if numpify:
        __lowerCamelCase		:							Any		 =						[np.asarray(__a    ) for x in speech_inputs]

       return speech_inputs



class 	A_ (	UpperCamelCase__ ,				unittest.TestCase   ):
      __snake_case 	=       WavaVecaFeatureExtractor




      def 					_snake_case     (    self:	Optional[int]    ):
       __lowerCamelCase		:							Dict		 =						WavaVecaFeatureExtractionTester(self    )




      def 					_snake_case     (    self:	Any						,				a:	List[str]    ):
       self.assertTrue(np.all(np.mean(__a						,				axis=0    ) < 1e-3    )    )
       self.assertTrue(np.all(np.abs(np.var(__a						,				axis=0    ) - 1    ) < 1e-3    )    )




      def 					_snake_case     (    self:	List[str]    ):
       # Tests that all call wrap to encode_plus and batch_encode_plus
       __lowerCamelCase		:							Optional[int]		 =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
       # create three inputs of length 800, 1000, and 1200
       __lowerCamelCase		:							List[Any]		 =						[floats_list((1, x)    )[0] for x in range(800						,				1400						,				200    )]
       __lowerCamelCase		:							List[str]		 =						[np.asarray(__a    ) for speech_input in speech_inputs]

       # Test not batched input
       __lowerCamelCase		:							List[str]		 =						feat_extract(speech_inputs[0]						,				return_tensors='np'    ).input_values
       __lowerCamelCase		:							List[str]		 =						feat_extract(np_speech_inputs[0]						,				return_tensors='np'    ).input_values
       self.assertTrue(np.allclose(__a						,				__a						,				atol=1e-3    )    )

       # Test batched
       __lowerCamelCase		:							Optional[int]		 =						feat_extract(__a						,				return_tensors='np'    ).input_values
       __lowerCamelCase		:							List[Any]		 =						feat_extract(__a						,				return_tensors='np'    ).input_values
       for enc_seq_a, enc_seq_a in zip(__a						,				__a    ):
        self.assertTrue(np.allclose(__a						,				__a						,				atol=1e-3    )    )

       # Test 2-D numpy arrays are batched.
       __lowerCamelCase		:							Any		 =						[floats_list((1, x)    )[0] for x in (800, 800, 800)]
       __lowerCamelCase		:							List[str]		 =						np.asarray(__a    )
       __lowerCamelCase		:							Optional[int]		 =						feat_extract(__a						,				return_tensors='np'    ).input_values
       __lowerCamelCase		:							List[str]		 =						feat_extract(__a						,				return_tensors='np'    ).input_values
       for enc_seq_a, enc_seq_a in zip(__a						,				__a    ):
        self.assertTrue(np.allclose(__a						,				__a						,				atol=1e-3    )    )




      def 					_snake_case     (    self:	str    ):
       __lowerCamelCase		:							int		 =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
       __lowerCamelCase		:							Optional[int]		 =						[floats_list((1, x)    )[0] for x in range(800						,				1400						,				200    )]

       __lowerCamelCase		:							List[str]		 =						['longest', 'max_length', 'do_not_pad']
       __lowerCamelCase		:							Union[str, Any]		 =						[None, 1600, None]
       for max_length, padding in zip(__a						,				__a    ):
        __lowerCamelCase		:							Dict		 =						feat_extract(__a						,				padding=__a						,				max_length=__a						,				return_tensors='np'    )
        __lowerCamelCase		:							List[str]		 =						processed.input_values

        self._check_zero_mean_unit_variance(input_values[0][:800]    )
        self.assertTrue(input_values[0][800:].sum() < 1e-6    )
        self._check_zero_mean_unit_variance(input_values[1][:1000]    )
        self.assertTrue(input_values[0][1000:].sum() < 1e-6    )
        self._check_zero_mean_unit_variance(input_values[2][:1200]    )




      def 					_snake_case     (    self:	List[str]    ):
       __lowerCamelCase		:							str		 =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
       __lowerCamelCase		:							Tuple		 =						range(800						,				1400						,				200    )
       __lowerCamelCase		:							Tuple		 =						[floats_list((1, x)    )[0] for x in lengths]

       __lowerCamelCase		:							Optional[Any]		 =						['longest', 'max_length', 'do_not_pad']
       __lowerCamelCase		:							int		 =						[None, 1600, None]

       for max_length, padding in zip(__a						,				__a    ):
        __lowerCamelCase		:							int		 =						feat_extract(__a						,				max_length=__a						,				padding=__a    )
        __lowerCamelCase		:							Dict		 =						processed.input_values

        self._check_zero_mean_unit_variance(input_values[0][:800]    )
        self._check_zero_mean_unit_variance(input_values[1][:1000]    )
        self._check_zero_mean_unit_variance(input_values[2][:1200]    )




      def 					_snake_case     (    self:	int    ):
       __lowerCamelCase		:							Any		 =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
       __lowerCamelCase		:							Dict		 =						[floats_list((1, x)    )[0] for x in range(800						,				1400						,				200    )]
       __lowerCamelCase		:							Dict		 =						feat_extract(
           __a						,				truncation=__a						,				max_length=1000						,				padding='max_length'						,				return_tensors='np'    )
       __lowerCamelCase		:							int		 =						processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :800]    )
       self._check_zero_mean_unit_variance(input_values[1]    )
       self._check_zero_mean_unit_variance(input_values[2]    )




      def 					_snake_case     (    self:	List[Any]    ):
       __lowerCamelCase		:							int		 =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
       __lowerCamelCase		:							List[str]		 =						[floats_list((1, x)    )[0] for x in range(800						,				1400						,				200    )]
       __lowerCamelCase		:							Tuple		 =						feat_extract(
           __a						,				truncation=__a						,				max_length=1000						,				padding='longest'						,				return_tensors='np'    )
       __lowerCamelCase		:							Optional[int]		 =						processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :800]    )
       self._check_zero_mean_unit_variance(input_values[1, :1000]    )
       self._check_zero_mean_unit_variance(input_values[2]    )

       # make sure that if max_length < longest -> then pad to max_length
       self.assertTrue(input_values.shape == (3, 1000)    )

       __lowerCamelCase		:							Union[str, Any]		 =						[floats_list((1, x)    )[0] for x in range(800						,				1400						,				200    )]
       __lowerCamelCase		:							Optional[int]		 =						feat_extract(
           __a						,				truncation=__a						,				max_length=2000						,				padding='longest'						,				return_tensors='np'    )
       __lowerCamelCase		:							Union[str, Any]		 =						processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :800]    )
       self._check_zero_mean_unit_variance(input_values[1, :1000]    )
       self._check_zero_mean_unit_variance(input_values[2]    )

       # make sure that if max_length > longest -> then pad to longest
       self.assertTrue(input_values.shape == (3, 1200)    )




      @require_torch
      def 					_snake_case     (    self:	List[str]    ):
       import torch

       __lowerCamelCase		:							Optional[Any]		 =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
       __lowerCamelCase		:							Optional[Any]		 =						np.random.rand(100    ).astype(np.floataa    )
       __lowerCamelCase		:							Any		 =						np_speech_inputs.tolist()

       for inputs in [py_speech_inputs, np_speech_inputs]:
        __lowerCamelCase		:							int		 =						feature_extractor.pad([{'input_values': inputs}]						,				return_tensors='np'    )
        self.assertTrue(np_processed.input_values.dtype == np.floataa    )
        __lowerCamelCase		:							Dict		 =						feature_extractor.pad([{'input_values': inputs}]						,				return_tensors='pt'    )
        self.assertTrue(pt_processed.input_values.dtype == torch.floataa    )




      @slow
      @require_torch
      def 					_snake_case     (    self:	Tuple    ):
       # this test makes sure that models that are using
       # group norm don't have their feature extractor return the
       # attention_mask
       for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
        __lowerCamelCase		:							Optional[int]		 =						WavaVecaConfig.from_pretrained(__a    )
        __lowerCamelCase		:							List[Any]		 =						WavaVecaFeatureExtractor.from_pretrained(__a    )

        # only "layer" feature extraction norm should make use of
        # attention_mask
        self.assertEqual(feat_extract.return_attention_mask						,				config.feat_extract_norm == 'layer'    )





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:	int						,				a:	Optional[Any]						,				a:	Optional[Any]=3						,				a:	List[str]=32						,				a:	Optional[int]=3						,				a:	Any=10						,				a:	List[str]=[10, 20, 30, 40]						,				a:	Any=[1, 1, 2, 1]						,				a:	Optional[int]=True						,				a:	List[str]=True						,				a:	Tuple="relu"						,				a:	List[Any]=3						,				a:	List[Any]=None						,				):
       __lowerCamelCase		:							Union[str, Any]		 =						parent
       __lowerCamelCase		:							Any		 =						batch_size
       __lowerCamelCase		:							List[str]		 =						image_size
       __lowerCamelCase		:							Tuple		 =						num_channels
       __lowerCamelCase		:							int		 =						embeddings_size
       __lowerCamelCase		:							Optional[int]		 =						hidden_sizes
       __lowerCamelCase		:							Optional[Any]		 =						depths
       __lowerCamelCase		:							Optional[int]		 =						is_training
       __lowerCamelCase		:							List[str]		 =						use_labels
       __lowerCamelCase		:							Dict		 =						hidden_act
       __lowerCamelCase		:							Union[str, Any]		 =						num_labels
       __lowerCamelCase		:							Tuple		 =						scope
       __lowerCamelCase		:							Union[str, Any]		 =						len(a    )




      def 					_snake_case     (    self:	int    ):
       __lowerCamelCase		:							Optional[Any]		 =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

       __lowerCamelCase		:							List[str]		 =						self.get_config()

       return config, pixel_values




      def 					_snake_case     (    self:	List[str]    ):
       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 					_snake_case     (    self:	Tuple						,				a:	Optional[int]						,				a:	int    ):
       __lowerCamelCase		:							Optional[Any]		 =						FlaxRegNetModel(config=a    )
       __lowerCamelCase		:							List[str]		 =						model(a    )

       # 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 					_snake_case     (    self:	Optional[int]						,				a:	List[Any]						,				a:	List[Any]    ):
       __lowerCamelCase		:							Tuple		 =						self.num_labels
       __lowerCamelCase		:							Union[str, Any]		 =						FlaxRegNetForImageClassification(config=a    )
       __lowerCamelCase		:							List[Any]		 =						model(a    )
       self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_labels)    )




      def 					_snake_case     (    self:	Optional[Any]    ):
       __lowerCamelCase		:							Union[str, Any]		 =						self.prepare_config_and_inputs()
       __lowerCamelCase ,					__lowerCamelCase		:							str		 =						config_and_inputs
       __lowerCamelCase		:							Tuple		 =						{'pixel_values': pixel_values}
       return config, inputs_dict



@require_flax
class 	A_ (	__UpperCamelCase ,				unittest.TestCase   ):




      '''simple docstring'''


      __snake_case 	=       (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

      __snake_case 	=       False
      __snake_case 	=       False
      __snake_case 	=       False




      def 					_snake_case     (    self:	Tuple    ):
       __lowerCamelCase		:							Dict		 =						FlaxRegNetModelTester(self    )
       __lowerCamelCase		:							List[str]		 =						ConfigTester(self						,				config_class=a						,				has_text_modality=a    )




      def 					_snake_case     (    self:	Union[str, Any]    ):
       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 					_snake_case     (    self:	List[Any]    ):
       return




      def 					_snake_case     (    self:	Dict    ):
       __lowerCamelCase		:							Union[str, Any]		 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*a    )




      def 					_snake_case     (    self:	Optional[int]    ):
       __lowerCamelCase		:							Union[str, Any]		 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*a    )




      @unittest.skip(reason='RegNet does not use inputs_embeds'    )
      def 					_snake_case     (    self:	Tuple    ):
       pass




      @unittest.skip(reason='RegNet does not support input and output embeddings'    )
      def 					_snake_case     (    self:	str    ):
       pass




      def 					_snake_case     (    self:	List[Any]    ):
       __lowerCamelCase ,					__lowerCamelCase		:							Optional[Any]		 =						self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        __lowerCamelCase		:							Optional[Any]		 =						model_class(a    )
        __lowerCamelCase		:							Dict		 =						inspect.signature(model.__call__    )
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        __lowerCamelCase		:							Union[str, Any]		 =						[*signature.parameters.keys()]

        __lowerCamelCase		:							Union[str, Any]		 =						['pixel_values']
        self.assertListEqual(arg_names[:1]						,				a    )




      def 					_snake_case     (    self:	List[str]    ):
       def check_hidden_states_output(a:	List[Any]						,				a:	List[Any]						,				a:	Union[str, Any]    ):
        __lowerCamelCase		:							str		 =						model_class(a    )

        __lowerCamelCase		:							Optional[Any]		 =						model(**self._prepare_for_class(a						,				a    )    )

        __lowerCamelCase		:							List[str]		 =						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

        __lowerCamelCase		:							List[str]		 =						self.model_tester.num_stages
        self.assertEqual(len(a    )						,				expected_num_stages + 1    )

       __lowerCamelCase ,					__lowerCamelCase		:							Dict		 =						self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        __lowerCamelCase		:							int		 =						True
        check_hidden_states_output(a						,				a						,				a    )

        # check that output_hidden_states also work using config
        del inputs_dict["output_hidden_states"]
        __lowerCamelCase		:							List[Any]		 =						True

        check_hidden_states_output(a						,				a						,				a    )




      def 					_snake_case     (    self:	Dict    ):
       __lowerCamelCase ,					__lowerCamelCase		:							Optional[int]		 =						self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        with self.subTest(model_class.__name__    ):
         __lowerCamelCase		:							str		 =						self._prepare_for_class(a						,				a    )
         __lowerCamelCase		:							List[str]		 =						model_class(a    )

         @jax.jit
         def model_jitted(a:	Optional[int]						,				**a:	str    ):
          return model(pixel_values=a						,				**a    )

         with self.subTest('JIT Enabled'    ):
          __lowerCamelCase		:							List[str]		 =						model_jitted(**a    ).to_tuple()

         with self.subTest('JIT Disabled'    ):
          with jax.disable_jit():
           __lowerCamelCase		:							Optional[int]		 =						model_jitted(**a    ).to_tuple()

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



def 				UpperCamelCase__					(  ):
 __lowerCamelCase		:							List[str]		 =						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
 return image



@require_flax
class 	A_ (	unittest.TestCase   ):




      '''simple docstring'''


      @cached_property
      def 					_snake_case     (    self:	Tuple    ):
       return AutoImageProcessor.from_pretrained('facebook/regnet-y-040'    ) if is_vision_available() else None




      @slow
      def 					_snake_case     (    self:	Optional[Any]    ):
       __lowerCamelCase		:							Optional[Any]		 =						FlaxRegNetForImageClassification.from_pretrained('facebook/regnet-y-040'    )

       __lowerCamelCase		:							Tuple		 =						self.default_image_processor
       __lowerCamelCase		:							int		 =						prepare_img()
       __lowerCamelCase		:							Tuple		 =						image_processor(images=a						,				return_tensors='np'    )

       __lowerCamelCase		:							List[Any]		 =						model(**a    )

       # verify the logits
       __lowerCamelCase		:							str		 =						(1, 1000)
       self.assertEqual(outputs.logits.shape						,				a    )

       __lowerCamelCase		:							int		 =						jnp.array([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6]    )

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

