



'''simple docstring'''



import logging
import sys
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Union

import librosa
import torch
from datasets import DatasetDict, load_dataset
from packaging import version
from torch import nn

from transformers import (
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaForPreTraining,
    is_apex_available,
    trainer_utils,
)
from transformers.models.wavaveca.modeling_wavaveca import _compute_mask_indices


if is_apex_available():
							from apex import amp

if version.parse(version.parse(torch.__version__).base_version) >= version.parse('1.6'):
							lowerCAmelCase_     :       Union[str, Any]								=						True
							from torch.cuda.amp import autocast


lowerCAmelCase_     :       Union[str, Any]								=						logging.getLogger(__name__)
@dataclass
class 	lowerCamelCase_							:
						_lowerCAmelCase     :   str	     =				field(
						    metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}			)
						_lowerCAmelCase     :   Optional[str]	     =				field(
						    default=snake_case_    ,				metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}    ,				)
						_lowerCAmelCase     :   Optional[bool]	     =				field(
						    default=snake_case_    ,				metadata={'help': 'Whether to freeze the feature extractor layers of the model.'}			)
						_lowerCAmelCase     :   Optional[bool]	     =				field(
						    default=snake_case_    ,				metadata={'help': 'Whether to log verbose messages or not.'}    ,				)
						_lowerCAmelCase     :   Optional[float]	     =				field(
						    default=2.0    ,				metadata={'help': 'Maximum temperature for gumbel softmax.'}			)
						_lowerCAmelCase     :   Optional[float]	     =				field(
						    default=0.5    ,				metadata={'help': 'Minimum temperature for gumbel softmax.'}			)
						_lowerCAmelCase     :   Optional[float]	     =				field(
						    default=0.99_9995    ,				metadata={'help': 'Decay of gumbel temperature during training.'}			)


def 					UpperCAmelCase   (						A  :				ModelArguments					,		A  :				TrainingArguments				):
							logging.basicConfig(
							    format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s'''					,		datefmt='''%m/%d/%Y %H:%M:%S'''					,		handlers=[logging.StreamHandler(sys.stdout				)]					,		)
							SCREAMING_SNAKE_CASE					:      List[Any]     					=			logging.WARNING
							if model_args.verbose_logging:
														SCREAMING_SNAKE_CASE					:      Dict     					=			logging.DEBUG
							elif trainer_utils.is_main_process(training_args.local_rank				):
														SCREAMING_SNAKE_CASE					:      Any     					=			logging.INFO
							logger.setLevel(A				)
@dataclass
class 	lowerCamelCase_							:
						_lowerCAmelCase     :   str	     =				field(
						    default=snake_case_    ,				metadata={'help': 'The name of the dataset to use (via the datasets library).'}			)
						_lowerCAmelCase     :   Optional[str]	     =				field(
						    default=snake_case_    ,				metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}			)
						_lowerCAmelCase     :   Optional[str]	     =				field(
						    default='train'    ,				metadata={
						        'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\''
						    }    ,				)
						_lowerCAmelCase     :   Optional[str]	     =				field(
						    default='validation'    ,				metadata={
						        'help': (
						            'The name of the validation data set split to use (via the datasets library). Defaults to \'validation\''
						        )
						    }    ,				)
						_lowerCAmelCase     :   Optional[str]	     =				field(
						    default='file'    ,				metadata={'help': 'Column in the dataset that contains speech file path. Defaults to \'file\''}    ,				)
						_lowerCAmelCase     :   bool	     =				field(
						    default=snake_case_    ,				metadata={'help': 'Overwrite the cached preprocessed datasets or not.'}			)
						_lowerCAmelCase     :   Optional[int]	     =				field(
						    default=1    ,				metadata={
						        'help': 'The percentage of the train set used as validation set in case there\'s no validation split'
						    }    ,				)
						_lowerCAmelCase     :   Optional[int]	     =				field(
						    default=snake_case_    ,				metadata={'help': 'The number of processes to use for the preprocessing.'}    ,				)
						_lowerCAmelCase     :   Optional[float]	     =				field(
						    default=20.0    ,				metadata={'help': 'Filter audio files that are longer than `max_duration_in_seconds` seconds'}			)


@dataclass
class 	lowerCamelCase_							:
						_lowerCAmelCase     :   WavaVecaForPreTraining
						_lowerCAmelCase     :   WavaVecaFeatureExtractor
						_lowerCAmelCase     :   Union[bool, str]	     =				"longest"
						_lowerCAmelCase     :   Optional[int]	     =				None
						_lowerCAmelCase     :   Optional[int]	     =				None







						def __call__(  self   :      Any				,    lowerCAmelCase__   :      List[Dict[str, Union[List[int], torch.Tensor]]]				):

													"""simple docstring"""
													# reformat list to dict and set to pytorch format
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.feature_extractor.pad(
													    lowerCAmelCase__				,    max_length=self.max_length				,    padding=self.padding				,    pad_to_multiple_of=self.pad_to_multiple_of				,    return_tensors='''pt'''				,    )
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			self.model._get_feat_extract_output_lengths(batch['''input_values'''].shape[-1]				)

													SCREAMING_SNAKE_CASE					:      Tuple     					=			batch['''input_values'''].shape[0]

													# make sure that no loss is computed on padded inputs
													if batch["attention_mask"] is not None:
																				# compute real output lengths according to convolution formula
																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			self.model._get_feat_extract_output_lengths(batch['''attention_mask'''].sum(-1				)				).to(
																				    torch.long				)

																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			torch.zeros(
																				    (batch_size, mask_indices_seq_length)				,    dtype=torch.long				,    device=batch['''input_values'''].device				)

																				# these two operations makes sure that all values
																				# before the output lengths indices are attended to
																				SCREAMING_SNAKE_CASE					:      Optional[int]     					=			1
																				SCREAMING_SNAKE_CASE					:      Dict     					=			attention_mask.flip([-1]				).cumsum(-1				).flip([-1]				).bool()

													# sample randomly masked indices
													SCREAMING_SNAKE_CASE					:      List[str]     					=			_compute_mask_indices(
													    (batch_size, mask_indices_seq_length)				,    self.model.config.mask_time_prob				,    self.model.config.mask_time_length				,    attention_mask=lowerCAmelCase__				,    min_masks=2				,    )

													return batch


class 	lowerCamelCase_							(					snake_case_			):







						def __init__(  self   :      str				,    *lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      Any=1				,    lowerCAmelCase__   :      Dict=0				,    lowerCAmelCase__   :      Tuple=1.0				,    **lowerCAmelCase__   :      Optional[Any]				):

													"""simple docstring"""
													super().__init__(*lowerCAmelCase__				,    **lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			0
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			max_gumbel_temp
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			min_gumbel_temp
													SCREAMING_SNAKE_CASE					:      Tuple     					=			gumbel_temp_decay







						def 					__lowercase    (  self   :      str				,    lowerCAmelCase__   :      nn.Module				,    lowerCAmelCase__   :      Dict[str, Union[torch.Tensor, Any]]				):

													"""simple docstring"""
													model.train()
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			self._prepare_inputs(lowerCAmelCase__				)

													if self.use_amp:
																				with autocast():
																											SCREAMING_SNAKE_CASE					:      Any     					=			self.compute_loss(lowerCAmelCase__				,    lowerCAmelCase__				)
													else:
																				SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.compute_loss(lowerCAmelCase__				,    lowerCAmelCase__				)

													if self.args.n_gpu > 1 or self.deepspeed:
																				if model.module.config.ctc_loss_reduction == "mean":
																											SCREAMING_SNAKE_CASE					:      Any     					=			loss.mean()
																				elif model.module.config.ctc_loss_reduction == "sum":
																											SCREAMING_SNAKE_CASE					:      Tuple     					=			loss.sum() / (inputs['''mask_time_indices''']).sum()
																				else:
																											raise ValueError(F"""{model.config.ctc_loss_reduction} is not valid. Choose one of ['mean', 'sum']"""				)

													if self.args.gradient_accumulation_steps > 1:
																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			loss / self.args.gradient_accumulation_steps

													if self.use_amp:
																				self.scaler.scale(lowerCAmelCase__				).backward()
													elif self.use_apex:
																				with amp.scale_loss(lowerCAmelCase__				,    self.optimizer				) as scaled_loss:
																											scaled_loss.backward()
													elif self.deepspeed:
																				self.deepspeed.backward(lowerCAmelCase__				)
													else:
																				loss.backward()

													self.num_update_step += 1
													# make sure gumbel softmax temperature is decayed
													if self.args.n_gpu > 1 or self.deepspeed:
																				model.module.set_gumbel_temperature(
																				    max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step				,    self.min_gumbel_temp				)				)
													else:
																				model.set_gumbel_temperature(
																				    max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step				,    self.min_gumbel_temp				)				)

													return loss.detach()


def 					UpperCAmelCase   (						):
							# See all possible arguments in src/transformers/training_args.py
							# or by passing the --help flag to this script.
							# We now keep distinct sets of args, for a cleaner separation of concerns.

							SCREAMING_SNAKE_CASE					:      Optional[int]     					=			HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)				)

							SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      List[Any]     					=			parser.parse_args_into_dataclasses()
							configure_logger(A					,		A				)

							# Downloading and loading a dataset from the hub.
							SCREAMING_SNAKE_CASE					:      str     					=			load_dataset(data_args.dataset_name					,		data_args.dataset_config_name					,		cache_dir=model_args.cache_dir				)

							if "validation" not in datasets.keys():
														# make sure only "validation" and "train" keys remain"
														SCREAMING_SNAKE_CASE					:      Any     					=			DatasetDict()
														SCREAMING_SNAKE_CASE					:      Any     					=			load_dataset(
														    data_args.dataset_name					,		data_args.dataset_config_name					,		split=F"""{data_args.train_split_name}[:{data_args.validation_split_percentage}%]"""					,		cache_dir=model_args.cache_dir					,		)
														SCREAMING_SNAKE_CASE					:      List[str]     					=			load_dataset(
														    data_args.dataset_name					,		data_args.dataset_config_name					,		split=F"""{data_args.train_split_name}[{data_args.validation_split_percentage}%:]"""					,		cache_dir=model_args.cache_dir					,		)
							else:
														# make sure only "validation" and "train" keys remain"
														SCREAMING_SNAKE_CASE					:      str     					=			DatasetDict()
														SCREAMING_SNAKE_CASE					:      Any     					=			load_dataset(
														    data_args.dataset_name					,		data_args.dataset_config_name					,		split='''validation'''					,		cache_dir=model_args.cache_dir					,		)
														SCREAMING_SNAKE_CASE					:      Tuple     					=			load_dataset(
														    data_args.dataset_name					,		data_args.dataset_config_name					,		split=F"""{data_args.train_split_name}"""					,		cache_dir=model_args.cache_dir					,		)

							# only normalized-inputs-training is supported
							SCREAMING_SNAKE_CASE					:      int     					=			WavaVecaFeatureExtractor.from_pretrained(
							    model_args.model_name_or_path					,		cache_dir=model_args.cache_dir					,		do_normalize=A				)

							def prepare_dataset(A  :				str				):
														# check that all files have the correct sampling rate
														SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      List[str]     					=			librosa.load(batch[data_args.speech_file_column]					,		sr=feature_extractor.sampling_rate				)
														return batch

							# load audio files into numpy arrays
							SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			datasets.map(
							    A					,		num_proc=data_args.preprocessing_num_workers					,		remove_columns=datasets['''train'''].column_names				)

							# filter audio files that are too long
							SCREAMING_SNAKE_CASE					:      int     					=			vectorized_datasets.filter(
							    lambda A				: len(data['''speech''']				) < int(data_args.max_duration_in_seconds * feature_extractor.sampling_rate				)				)

							def normalize(A  :				List[Any]				):
														return feature_extractor(batch['''speech''']					,		sampling_rate=feature_extractor.sampling_rate				)

							# normalize and transform to `BatchFeatures`
							SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			vectorized_datasets.map(
							    A					,		batched=A					,		num_proc=data_args.preprocessing_num_workers					,		load_from_cache_file=not data_args.overwrite_cache					,		remove_columns=vectorized_datasets['''train'''].column_names					,		)

							# pretraining is only supported for "newer" stable layer norm architecture
							# apply_spec_augment has to be True, mask_feature_prob has to be 0.0
							SCREAMING_SNAKE_CASE					:      Tuple     					=			WavaVecaConfig.from_pretrained(
							    model_args.model_name_or_path					,		cache_dir=model_args.cache_dir					,		gradient_checkpointing=training_args.gradient_checkpointing					,		)

							if not config.do_stable_layer_norm or config.feat_extract_norm != "layer":
														raise ValueError(
														    '''PreTraining is only supported for ``config.do_stable_layer_norm=True`` and'''
														    ''' ``config.feat_extract_norm=\'layer\''''				)

							SCREAMING_SNAKE_CASE					:      str     					=			WavaVecaForPreTraining(A				)

							SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			DataCollatorForWavaVecaPretraining(model=A					,		feature_extractor=A				)

							SCREAMING_SNAKE_CASE					:      Dict     					=			WavaVecaPreTrainer(
							    model=A					,		data_collator=A					,		args=A					,		train_dataset=vectorized_datasets['''train''']					,		eval_dataset=vectorized_datasets['''validation''']					,		tokenizer=A					,		max_gumbel_temp=model_args.max_gumbel_temperature					,		min_gumbel_temp=model_args.min_gumbel_temperature					,		gumbel_temp_decay=model_args.gumbel_temperature_decay					,		)
							trainer.train()


if __name__ == "__main__":
							main()









'''simple docstring'''



from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import RegNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers import TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor
class 	lowerCamelCase_							:







						def __init__(  self   :      Optional[Any]				,    lowerCAmelCase__   :      Tuple				,    lowerCAmelCase__   :      int=3				,    lowerCAmelCase__   :      Tuple=32				,    lowerCAmelCase__   :      Union[str, Any]=3				,    lowerCAmelCase__   :      Dict=10				,    lowerCAmelCase__   :      List[Any]=[10, 20, 30, 40]				,    lowerCAmelCase__   :      List[Any]=[1, 1, 2, 1]				,    lowerCAmelCase__   :      Optional[int]=True				,    lowerCAmelCase__   :      Any=True				,    lowerCAmelCase__   :      List[str]="relu"				,    lowerCAmelCase__   :      int=3				,    lowerCAmelCase__   :      Tuple=None				,    ):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Tuple     					=			parent
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			batch_size
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			image_size
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			num_channels
													SCREAMING_SNAKE_CASE					:      str     					=			embeddings_size
													SCREAMING_SNAKE_CASE					:      str     					=			hidden_sizes
													SCREAMING_SNAKE_CASE					:      Tuple     					=			depths
													SCREAMING_SNAKE_CASE					:      Dict     					=			is_training
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			use_labels
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			hidden_act
													SCREAMING_SNAKE_CASE					:      str     					=			num_labels
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			scope
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			len(lowerCAmelCase__				)







						def 					__lowercase    (  self   :      str				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

													SCREAMING_SNAKE_CASE					:      List[Any]     					=			None
													if self.use_labels:
																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			ids_tensor([self.batch_size]				,    self.num_labels				)

													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			self.get_config()
													return config, pixel_values, labels







						def 					__lowercase    (  self   :      Any				):

													"""simple docstring"""
													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				,    )







						def 					__lowercase    (  self   :      Dict				,    lowerCAmelCase__   :      Tuple				,    lowerCAmelCase__   :      Union[str, Any]				,    lowerCAmelCase__   :      Optional[int]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      int     					=			TFRegNetModel(config=lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      int     					=			model(lowerCAmelCase__				,    training=lowerCAmelCase__				)
													# expected last hidden states: B, C, H // 32, W // 32
													self.parent.assertEqual(
													    result.last_hidden_state.shape				,    (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)				,    )







						def 					__lowercase    (  self   :      int				,    lowerCAmelCase__   :      str				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      List[str]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			self.num_labels
													SCREAMING_SNAKE_CASE					:      Any     					=			TFRegNetForImageClassification(lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Dict     					=			model(lowerCAmelCase__				,    labels=lowerCAmelCase__				,    training=lowerCAmelCase__				)
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)				)







						def 					__lowercase    (  self   :      List[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      int     					=			self.prepare_config_and_inputs()
													SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      List[str]     					=			config_and_inputs
													SCREAMING_SNAKE_CASE					:      Tuple     					=			{'''pixel_values''': pixel_values}
													return config, inputs_dict


@require_tf
class 	lowerCamelCase_							(					snake_case_    ,				snake_case_    ,				unittest.TestCase			):
						_lowerCAmelCase     :   str	     =				(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
						_lowerCAmelCase     :   str	     =				(
						    {'feature-extraction': TFRegNetModel, 'image-classification': TFRegNetForImageClassification}
						    if is_tf_available()
						    else {}
						)

						_lowerCAmelCase     :   Tuple	     =				False
						_lowerCAmelCase     :   List[Any]	     =				False
						_lowerCAmelCase     :   List[str]	     =				False
						_lowerCAmelCase     :   str	     =				False
						_lowerCAmelCase     :   str	     =				False







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

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Tuple     					=			TFRegNetModelTester(self				)
													SCREAMING_SNAKE_CASE					:      Dict     					=			ConfigTester(self				,    config_class=lowerCAmelCase__				,    has_text_modality=lowerCAmelCase__				)







						def 					__lowercase    (  self   :      List[str]				):

													"""simple docstring"""
													return







						@unittest.skip(reason='''RegNet does not use inputs_embeds'''				)
						def 					__lowercase    (  self   :      Optional[int]				):

													"""simple docstring"""
													pass







						@unittest.skipIf(
						    not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''				)				) == 0				,    reason='''TF does not support backprop for grouped convolutions on CPU.'''				,    )
						@slow
						def 					__lowercase    (  self   :      int				):

													"""simple docstring"""
													super().test_keras_fit()







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

													"""simple docstring"""
													pass







						def 					__lowercase    (  self   :      int				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.model_tester.prepare_config_and_inputs_for_common()

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

																				SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			['''pixel_values''']
																				self.assertListEqual(arg_names[:1]				,    lowerCAmelCase__				)







						def 					__lowercase    (  self   :      Any				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*lowerCAmelCase__				)







						def 					__lowercase    (  self   :      Dict				):

													"""simple docstring"""
													def check_hidden_states_output(lowerCAmelCase__   :      Any				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      Tuple				):
																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			model_class(lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      Dict     					=			model(**self._prepare_for_class(lowerCAmelCase__				,    lowerCAmelCase__				)				,    training=lowerCAmelCase__				)

																				SCREAMING_SNAKE_CASE					:      int     					=			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																				SCREAMING_SNAKE_CASE					:      int     					=			self.model_tester.num_stages
																				self.assertEqual(len(lowerCAmelCase__				)				,    expected_num_stages + 1				)

																				# RegNet's feature maps are of shape (batch_size, num_channels, height, width)
																				self.assertListEqual(
																				    list(hidden_states[0].shape[-2:]				)				,    [self.model_tester.image_size // 2, self.model_tester.image_size // 2]				,    )

													SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.model_tester.prepare_config_and_inputs_for_common()
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			['''basic''', '''bottleneck''']
													for model_class in self.all_model_classes:
																				for layer_type in layers_type:
																											SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			layer_type
																											SCREAMING_SNAKE_CASE					:      Tuple     					=			True
																											check_hidden_states_output(lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__				)

																											# check that output_hidden_states also work using config
																											del inputs_dict["output_hidden_states"]
																											SCREAMING_SNAKE_CASE					:      Optional[int]     					=			True

																											check_hidden_states_output(lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__				)







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

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.model_tester.prepare_config_and_inputs_for_common()

													def check_equivalence(lowerCAmelCase__   :      Tuple				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      Dict				,    lowerCAmelCase__   :      str={}				):
																				SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			model(lowerCAmelCase__				,    return_dict=lowerCAmelCase__				,    **lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      Any     					=			model(lowerCAmelCase__				,    return_dict=lowerCAmelCase__				,    **lowerCAmelCase__				).to_tuple()

																				def recursive_check(lowerCAmelCase__   :      Dict				,    lowerCAmelCase__   :      str				):
																											if isinstance(lowerCAmelCase__				,    (List, Tuple)				):
																																		for tuple_iterable_value, dict_iterable_value in zip(lowerCAmelCase__				,    lowerCAmelCase__				):
																																									recursive_check(lowerCAmelCase__				,    lowerCAmelCase__				)
																											elif tuple_object is None:
																																		return
																											else:
																																		self.assertTrue(
																																		    all(tf.equal(lowerCAmelCase__				,    lowerCAmelCase__				)				)				,    msg=(
																																		        '''Tuple and dict output are not equal. Difference:'''
																																		        F""" {tf.math.reduce_max(tf.abs(tuple_object - dict_object				)				)}"""
																																		    )				,    )

																											recursive_check(lowerCAmelCase__				,    lowerCAmelCase__				)

													for model_class in self.all_model_classes:
																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			model_class(lowerCAmelCase__				)

																				SCREAMING_SNAKE_CASE					:      int     					=			self._prepare_for_class(lowerCAmelCase__				,    lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self._prepare_for_class(lowerCAmelCase__				,    lowerCAmelCase__				)
																				check_equivalence(lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__				)

																				SCREAMING_SNAKE_CASE					:      Optional[int]     					=			self._prepare_for_class(lowerCAmelCase__				,    lowerCAmelCase__				,    return_labels=lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      str     					=			self._prepare_for_class(lowerCAmelCase__				,    lowerCAmelCase__				,    return_labels=lowerCAmelCase__				)
																				check_equivalence(lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__				)

																				SCREAMING_SNAKE_CASE					:      Tuple     					=			self._prepare_for_class(lowerCAmelCase__				,    lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      Dict     					=			self._prepare_for_class(lowerCAmelCase__				,    lowerCAmelCase__				)
																				check_equivalence(lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__				,    {'''output_hidden_states''': True}				)

																				SCREAMING_SNAKE_CASE					:      Any     					=			self._prepare_for_class(lowerCAmelCase__				,    lowerCAmelCase__				,    return_labels=lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			self._prepare_for_class(lowerCAmelCase__				,    lowerCAmelCase__				,    return_labels=lowerCAmelCase__				)
																				check_equivalence(lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__				,    {'''output_hidden_states''': True}				)







						def 					__lowercase    (  self   :      List[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Dict     					=			self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__				)







						@slow
						def 					__lowercase    (  self   :      Any				):

													"""simple docstring"""
													for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			TFRegNetModel.from_pretrained(lowerCAmelCase__				)
																				self.assertIsNotNone(lowerCAmelCase__				)


def 					UpperCAmelCase   (						):
							SCREAMING_SNAKE_CASE					:      List[Any]     					=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
							return image






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







						@cached_property
						def 					__lowercase    (  self   :      List[str]				):

													"""simple docstring"""
													return (
													    AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]				)
													    if is_vision_available()
													    else None
													)







						@slow
						def 					__lowercase    (  self   :      str				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      str     					=			TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]				)

													SCREAMING_SNAKE_CASE					:      Dict     					=			self.default_image_processor
													SCREAMING_SNAKE_CASE					:      int     					=			prepare_img()
													SCREAMING_SNAKE_CASE					:      List[str]     					=			image_processor(images=lowerCAmelCase__				,    return_tensors='''tf'''				)

													# forward pass
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			model(**lowerCAmelCase__				,    training=lowerCAmelCase__				)

													# verify the logits
													SCREAMING_SNAKE_CASE					:      List[str]     					=			tf.TensorShape((1, 10_00)				)
													self.assertEqual(outputs.logits.shape				,    lowerCAmelCase__				)

													SCREAMING_SNAKE_CASE					:      List[str]     					=			tf.constant([-0.4180, -1.5051, -3.4836]				)

													tf.debugging.assert_near(outputs.logits[0, :3]				,    lowerCAmelCase__				,    atol=1e-4				)




