
import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    FalconForCausalLM,
		    FalconForQuestionAnswering,
		    FalconForSequenceClassification,
		    FalconForTokenClassification,
		    FalconModel,
		)





class       __A:

					def __init__(	self					:     int   ,					__UpperCamelCase					:     Dict   ,					__UpperCamelCase					:     str=3   ,					__UpperCamelCase					:     List[str]=7   ,					__UpperCamelCase					:     str=True   ,					__UpperCamelCase					:     int=True   ,					__UpperCamelCase					:     Optional[Any]=False   ,					__UpperCamelCase					:     Optional[Any]=True   ,					__UpperCamelCase					:     Optional[int]=9_9   ,					__UpperCamelCase					:     Any=3_2   ,					__UpperCamelCase					:     str=5   ,					__UpperCamelCase					:     List[Any]=4   ,					__UpperCamelCase					:     List[Any]=3_7   ,					__UpperCamelCase					:     List[str]="gelu"   ,					__UpperCamelCase					:     List[Any]=0.1   ,					__UpperCamelCase					:     List[str]=0.1   ,					__UpperCamelCase					:     str=5_1_2   ,					__UpperCamelCase					:     str=1_6   ,					__UpperCamelCase					:     Dict=2   ,					__UpperCamelCase					:     Any=0.02   ,					__UpperCamelCase					:     Dict=3   ,					__UpperCamelCase					:     str=4   ,					__UpperCamelCase					:     str=None   ,					):
												lowerCamelCase_							  =					parent
												lowerCamelCase_							  =					batch_size
												lowerCamelCase_							  =					seq_length
												lowerCamelCase_							  =					is_training
												lowerCamelCase_							  =					use_input_mask
												lowerCamelCase_							  =					use_token_type_ids
												lowerCamelCase_							  =					use_labels
												lowerCamelCase_							  =					vocab_size
												lowerCamelCase_							  =					hidden_size
												lowerCamelCase_							  =					num_hidden_layers
												lowerCamelCase_							  =					num_attention_heads
												lowerCamelCase_							  =					intermediate_size
												lowerCamelCase_							  =					hidden_act
												lowerCamelCase_							  =					hidden_dropout_prob
												lowerCamelCase_							  =					attention_probs_dropout_prob
												lowerCamelCase_							  =					max_position_embeddings
												lowerCamelCase_							  =					type_vocab_size
												lowerCamelCase_							  =					type_sequence_label_size
												lowerCamelCase_							  =					initializer_range
												lowerCamelCase_							  =					num_labels
												lowerCamelCase_							  =					num_choices
												lowerCamelCase_							  =					scope

					def    lowercase__      (	self					:     List[Any]   ):
												lowerCamelCase_							  =					ids_tensor([self.batch_size, self.seq_length]   ,					self.vocab_size   )

												lowerCamelCase_							  =					None
												if self.use_input_mask:
																			lowerCamelCase_							  =					random_attention_mask([self.batch_size, self.seq_length]   )

												lowerCamelCase_							  =					None

												lowerCamelCase_							  =					None
												lowerCamelCase_							  =					None
												lowerCamelCase_							  =					None
												if self.use_labels:
																			lowerCamelCase_							  =					ids_tensor([self.batch_size]   ,					self.type_sequence_label_size   )
																			lowerCamelCase_							  =					ids_tensor([self.batch_size, self.seq_length]   ,					self.num_labels   )
																			lowerCamelCase_							  =					ids_tensor([self.batch_size]   ,					self.num_choices   )

												lowerCamelCase_							  =					self.get_config()

												return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels

					def    lowercase__      (	self					:     List[Any]   ):
												return FalconConfig(
												    vocab_size=self.vocab_size   ,					hidden_size=self.hidden_size   ,					num_hidden_layers=self.num_hidden_layers   ,					num_attention_heads=self.num_attention_heads   ,					intermediate_size=self.intermediate_size   ,					hidden_act=self.hidden_act   ,					hidden_dropout_prob=self.hidden_dropout_prob   ,					attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,					max_position_embeddings=self.max_position_embeddings   ,					type_vocab_size=self.type_vocab_size   ,					is_decoder=__UpperCamelCase   ,					initializer_range=self.initializer_range   ,					pad_token_id=1   ,					new_decoder_architecture=__UpperCamelCase   ,					)

					def    lowercase__      (	self					:     Dict   ,					__UpperCamelCase					:     Optional[Any]   ,					__UpperCamelCase					:     Dict   ,					__UpperCamelCase					:     int   ,					__UpperCamelCase					:     Tuple   ,					__UpperCamelCase					:     Dict   ,					__UpperCamelCase					:     Dict   ,					__UpperCamelCase					:     Tuple   ):
												lowerCamelCase_							  =					FalconModel(config=__UpperCamelCase   )
												model.to(__UpperCamelCase   )
												model.eval()
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					attention_mask=__UpperCamelCase   )
												lowerCamelCase_							  =					model(__UpperCamelCase   )
												self.parent.assertEqual(result.last_hidden_state.shape   ,					(self.batch_size, self.seq_length, self.hidden_size)   )

					def    lowercase__      (	self					:     List[str]   ,					__UpperCamelCase					:     Optional[int]   ,					__UpperCamelCase					:     Dict   ,					__UpperCamelCase					:     List[Any]   ,					__UpperCamelCase					:     int   ,					__UpperCamelCase					:     int   ,					__UpperCamelCase					:     int   ,					__UpperCamelCase					:     List[Any]   ,					__UpperCamelCase					:     Tuple   ,					__UpperCamelCase					:     Optional[Any]   ,					):
												lowerCamelCase_							  =					True
												lowerCamelCase_							  =					FalconModel(__UpperCamelCase   )
												model.to(__UpperCamelCase   )
												model.eval()
												lowerCamelCase_							  =					model(
												    __UpperCamelCase   ,					attention_mask=__UpperCamelCase   ,					encoder_hidden_states=__UpperCamelCase   ,					encoder_attention_mask=__UpperCamelCase   ,					)
												lowerCamelCase_							  =					model(
												    __UpperCamelCase   ,					attention_mask=__UpperCamelCase   ,					encoder_hidden_states=__UpperCamelCase   ,					)
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					attention_mask=__UpperCamelCase   )
												self.parent.assertEqual(result.last_hidden_state.shape   ,					(self.batch_size, self.seq_length, self.hidden_size)   )

					def    lowercase__      (	self					:     Any   ,					__UpperCamelCase					:     Union[str, Any]   ,					__UpperCamelCase					:     Union[str, Any]   ,					__UpperCamelCase					:     Tuple   ,					__UpperCamelCase					:     Optional[int]   ,					__UpperCamelCase					:     Union[str, Any]   ,					__UpperCamelCase					:     Tuple   ,					__UpperCamelCase					:     Dict   ,					__UpperCamelCase					:     Optional[int]   ,					__UpperCamelCase					:     int   ,					):
												lowerCamelCase_							  =					FalconForCausalLM(config=__UpperCamelCase   )
												model.to(__UpperCamelCase   )
												model.eval()
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					attention_mask=__UpperCamelCase   ,					labels=__UpperCamelCase   )
												self.parent.assertEqual(result.logits.shape   ,					(self.batch_size, self.seq_length, self.vocab_size)   )

					def    lowercase__      (	self					:     Optional[Any]   ,					__UpperCamelCase					:     Tuple   ,					__UpperCamelCase					:     Optional[Any]   ,					__UpperCamelCase					:     Any   ,					__UpperCamelCase					:     str   ,					__UpperCamelCase					:     Optional[Any]   ,					__UpperCamelCase					:     int   ,					__UpperCamelCase					:     List[str]   ,					__UpperCamelCase					:     Union[str, Any]   ,					__UpperCamelCase					:     Union[str, Any]   ,					):
												lowerCamelCase_							  =					True
												lowerCamelCase_							  =					True
												lowerCamelCase_							  =					FalconForCausalLM(config=__UpperCamelCase   )
												model.to(__UpperCamelCase   )
												model.eval()

												# first forward pass
												lowerCamelCase_							  =					model(
												    __UpperCamelCase   ,					attention_mask=__UpperCamelCase   ,					encoder_hidden_states=__UpperCamelCase   ,					encoder_attention_mask=__UpperCamelCase   ,					use_cache=__UpperCamelCase   ,					)
												lowerCamelCase_							  =					outputs.past_key_values

												# create hypothetical multiple next token and extent to next_input_ids
												lowerCamelCase_							  =					ids_tensor((self.batch_size, 3)   ,					config.vocab_size   )
												lowerCamelCase_							  =					ids_tensor((self.batch_size, 3)   ,					vocab_size=2   )

												# append to next input_ids and
												lowerCamelCase_							  =					torch.cat([input_ids, next_tokens]   ,					dim=-1   )
												lowerCamelCase_							  =					torch.cat([input_mask, next_mask]   ,					dim=-1   )

												lowerCamelCase_							  =					model(
												    __UpperCamelCase   ,					attention_mask=__UpperCamelCase   ,					encoder_hidden_states=__UpperCamelCase   ,					encoder_attention_mask=__UpperCamelCase   ,					output_hidden_states=__UpperCamelCase   ,					)["""hidden_states"""][0]
												lowerCamelCase_							  =					model(
												    __UpperCamelCase   ,					attention_mask=__UpperCamelCase   ,					encoder_hidden_states=__UpperCamelCase   ,					encoder_attention_mask=__UpperCamelCase   ,					past_key_values=__UpperCamelCase   ,					output_hidden_states=__UpperCamelCase   ,					)["""hidden_states"""][0]

												# select random slice
												lowerCamelCase_							  =					ids_tensor((1,)   ,					output_from_past.shape[-1]   ).item()
												lowerCamelCase_							  =					output_from_no_past[:, -3:, random_slice_idx].detach()
												lowerCamelCase_							  =					output_from_past[:, :, random_slice_idx].detach()

												self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]   )

												# test that outputs are equal for slice
												self.parent.assertTrue(torch.allclose(__UpperCamelCase   ,					__UpperCamelCase   ,					atol=1E-3   )   )



					def    lowercase__      (	self					:     List[Any]   ):
												lowerCamelCase_							  =					self.prepare_config_and_inputs()
												(
												    (
												    lowerCamelCase_
												)  ,   (
												    lowerCamelCase_
												)  ,   (
												    lowerCamelCase_
												)  ,   (
												    lowerCamelCase_
												)  ,   (
												    lowerCamelCase_
												)  ,   (
												    lowerCamelCase_
												)  ,   (
												    lowerCamelCase_
												)  ,   
												)							  =					config_and_inputs
												lowerCamelCase_							  =					{"""input_ids""": input_ids, """attention_mask""": input_mask}
												return config, inputs_dict





@require_torch
class       __A(				UpperCAmelCase     ,		UpperCAmelCase     ,		UpperCAmelCase     ,		unittest.TestCase       ):
					SCREAMING_SNAKE_CASE			    =    (
					    (
					        FalconModel,
					        FalconForCausalLM,
					        FalconForSequenceClassification,
					        FalconForTokenClassification,
					        FalconForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					SCREAMING_SNAKE_CASE			    =    (FalconForCausalLM,) if is_torch_available() else ()
					SCREAMING_SNAKE_CASE			    =    (
					    {
					        '''feature-extraction''': FalconModel,
					        '''text-classification''': FalconForSequenceClassification,
					        '''text-generation''': FalconForCausalLM,
					        '''question-answering''': FalconForQuestionAnswering,
					        '''token-classification''': FalconForTokenClassification,
					        '''zero-shot''': FalconForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    False

					def    lowercase__      (	self					:     Optional[Any]   ):
												lowerCamelCase_							  =					FalconModelTester(self   )
												lowerCamelCase_							  =					ConfigTester(self   ,					config_class=__UpperCamelCase   ,					hidden_size=3_7   )

					def    lowercase__      (	self					:     List[Any]   ):
												self.config_tester.run_common_tests()

					def    lowercase__      (	self					:     Tuple   ):
												lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__UpperCamelCase   )

					def    lowercase__      (	self					:     Tuple   ):
												lowerCamelCase_  ,   *lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs()
												for alibi in [True, False]:
																			lowerCamelCase_							  =					alibi
																			self.model_tester.create_and_check_model(__UpperCamelCase   ,					*__UpperCamelCase   )

					def    lowercase__      (	self					:     str   ):
												lowerCamelCase_  ,   lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs_for_common()
												lowerCamelCase_							  =					3
												lowerCamelCase_							  =					input_dict["""input_ids"""]
												lowerCamelCase_							  =					input_ids.ne(1   ).to(__UpperCamelCase   )
												lowerCamelCase_							  =					ids_tensor([self.model_tester.batch_size]   ,					self.model_tester.type_sequence_label_size   )
												lowerCamelCase_							  =					FalconForSequenceClassification(__UpperCamelCase   )
												model.to(__UpperCamelCase   )
												model.eval()
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					attention_mask=__UpperCamelCase   ,					labels=__UpperCamelCase   )
												self.assertEqual(result.logits.shape   ,					(self.model_tester.batch_size, self.model_tester.num_labels)   )

					def    lowercase__      (	self					:     Any   ):
												lowerCamelCase_  ,   lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs_for_common()
												lowerCamelCase_							  =					3
												lowerCamelCase_							  =					"""single_label_classification"""
												lowerCamelCase_							  =					input_dict["""input_ids"""]
												lowerCamelCase_							  =					input_ids.ne(1   ).to(__UpperCamelCase   )
												lowerCamelCase_							  =					ids_tensor([self.model_tester.batch_size]   ,					self.model_tester.type_sequence_label_size   )
												lowerCamelCase_							  =					FalconForSequenceClassification(__UpperCamelCase   )
												model.to(__UpperCamelCase   )
												model.eval()
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					attention_mask=__UpperCamelCase   ,					labels=__UpperCamelCase   )
												self.assertEqual(result.logits.shape   ,					(self.model_tester.batch_size, self.model_tester.num_labels)   )

					def    lowercase__      (	self					:     str   ):
												lowerCamelCase_  ,   lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs_for_common()
												lowerCamelCase_							  =					input_dict["""input_ids"""]
												lowerCamelCase_							  =					FalconForCausalLM(__UpperCamelCase   )
												model.to(__UpperCamelCase   )
												model.eval()
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					use_cache=__UpperCamelCase   )
												lowerCamelCase_							  =					input_ids.shape[0]
												lowerCamelCase_							  =					model._convert_to_rw_cache(result.past_key_values   )
												lowerCamelCase_							  =					model._convert_cache_to_standard_format(__UpperCamelCase   ,					__UpperCamelCase   )
												for layer in range(len(__UpperCamelCase   )   ):
																			for tensor_idx in range(2   ):
																										self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3   )
																										self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4   )
																										self.assertTrue(
																										    torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]   )   )

					def    lowercase__      (	self					:     Union[str, Any]   ):
												lowerCamelCase_  ,   lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs_for_common()
												lowerCamelCase_							  =					3
												lowerCamelCase_							  =					"""multi_label_classification"""
												lowerCamelCase_							  =					input_dict["""input_ids"""]
												lowerCamelCase_							  =					input_ids.ne(1   ).to(__UpperCamelCase   )
												lowerCamelCase_							  =					ids_tensor(
												    [self.model_tester.batch_size, config.num_labels]   ,					self.model_tester.type_sequence_label_size   ).to(torch.float   )
												lowerCamelCase_							  =					FalconForSequenceClassification(__UpperCamelCase   )
												model.to(__UpperCamelCase   )
												model.eval()
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					attention_mask=__UpperCamelCase   ,					labels=__UpperCamelCase   )
												self.assertEqual(result.logits.shape   ,					(self.model_tester.batch_size, self.model_tester.num_labels)   )



					def    lowercase__      (	self					:     Dict   ):
												# Falcon can have different numbers of KV-heads than the number of query heads, so we need
												# to override this test to use the right head counts.
												for model_class in self.all_generative_model_classes:
																			lowerCamelCase_  ,   lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs_for_common()

																			# If it doesn't support cache, pass the test
																			if not hasattr(__UpperCamelCase   ,					"""use_cache"""   ):
																										return

																			lowerCamelCase_							  =					model_class(__UpperCamelCase   ).to(__UpperCamelCase   )
																			if "use_cache" not in inputs:
																										lowerCamelCase_							  =					True
																			lowerCamelCase_							  =					model(**__UpperCamelCase   )

																			# If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
																			if "past_key_values" not in outputs:
																										return

																			lowerCamelCase_							  =					(
																			    getattr(__UpperCamelCase   ,					"""decoder_layers"""   ,					__UpperCamelCase   )
																			    or getattr(__UpperCamelCase   ,					"""num_decoder_layers"""   ,					__UpperCamelCase   )
																			    or config.num_hidden_layers
																			)
																			lowerCamelCase_							  =					getattr(__UpperCamelCase   ,					"""num_kv_heads"""   ,					config.num_attention_heads   )
																			lowerCamelCase_							  =					getattr(__UpperCamelCase   ,					"""d_model"""   ,					config.hidden_size   )
																			lowerCamelCase_							  =					embed_dim // num_attention_heads

																			lowerCamelCase_							  =					outputs["""past_key_values"""]
																			self.assertEqual(len(__UpperCamelCase   )   ,					__UpperCamelCase   )

																			lowerCamelCase_  ,   lowerCamelCase_							  =					inputs["""input_ids"""].shape
																			for i in range(__UpperCamelCase   ):
																										if config.new_decoder_architecture:
																																	lowerCamelCase_							  =					config.num_attention_heads
																										elif config.multi_query:
																																	lowerCamelCase_							  =					1
																										self.assertEqual(len(past_kv[0]   )   ,					2   )  # K V for the decoder = 2
																										self.assertEqual(
																										    past_kv[i][0].shape   ,					(batch_size, num_attention_heads, seq_length, per_head_embed_dim)   )
																										self.assertEqual(
																										    past_kv[i][1].shape   ,					(batch_size, num_attention_heads, seq_length, per_head_embed_dim)   )





@require_torch
class       __A(				unittest.TestCase       ):

					@slow
					def    lowercase__      (	self					:     Optional[int]   ):
												lowerCamelCase_							  =					AutoTokenizer.from_pretrained("""Rocketknight1/falcon-rw-1b"""   )
												lowerCamelCase_							  =					FalconForCausalLM.from_pretrained("""Rocketknight1/falcon-rw-1b"""   )
												model.eval()
												model.to(__UpperCamelCase   )
												lowerCamelCase_							  =					tokenizer("""My favorite food is"""   ,					return_tensors="""pt"""   ).to(__UpperCamelCase   )

												lowerCamelCase_							  =					(
												    """My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday."""
												)

												lowerCamelCase_							  =					model.generate(**__UpperCamelCase   ,					do_sample=__UpperCamelCase   ,					max_new_tokens=1_9   )
												lowerCamelCase_							  =					tokenizer.batch_decode(__UpperCamelCase   )[0]

												self.assertEqual(__UpperCamelCase   ,					__UpperCamelCase   )

					@slow
					def    lowercase__      (	self					:     Any   ):
												# The big models are way too big for the CI, so we use tiny random models that resemble their
												# architectures but with much smaller and fewer layers
												for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
																			lowerCamelCase_							  =					AutoTokenizer.from_pretrained(__UpperCamelCase   )
																			lowerCamelCase_							  =					FalconForCausalLM.from_pretrained(__UpperCamelCase   )
																			model.eval()
																			model.to(__UpperCamelCase   )
																			lowerCamelCase_							  =					tokenizer("""My favorite food is"""   ,					return_tensors="""pt"""   ).to(__UpperCamelCase   )

																			# We just test that these run without errors - the models are randomly initialized
																			# and so the actual text outputs will be garbage
																			model.generate(**__UpperCamelCase   ,					do_sample=__UpperCamelCase   ,					max_new_tokens=4   )
																			model.generate(**__UpperCamelCase   ,					do_sample=__UpperCamelCase   ,					max_new_tokens=4   )
																			model.generate(**__UpperCamelCase   ,					num_beams=2   ,					max_new_tokens=4   )



					@slow
					def    lowercase__      (	self					:     Optional[Any]   ):
												# The big models are way too big for the CI, so we use tiny random models that resemble their
												# architectures but with much smaller and fewer layers
												with torch.no_grad():
																			for repo in [
																			    "Rocketknight1/falcon-rw-1b",
																			    "Rocketknight1/tiny-random-falcon-7b",
																			    "Rocketknight1/tiny-random-falcon-40b",
																			]:
																										lowerCamelCase_							  =					AutoTokenizer.from_pretrained(__UpperCamelCase   )
																										lowerCamelCase_							  =					FalconForCausalLM.from_pretrained(__UpperCamelCase   )
																										model.eval()
																										model.to(device=__UpperCamelCase   )
																										lowerCamelCase_							  =					tokenizer("""My favorite food is"""   ,					return_tensors="""pt"""   ).to(__UpperCamelCase   )

																										# Test results are the same with and without cache
																										lowerCamelCase_							  =					model.generate(**__UpperCamelCase   ,					do_sample=__UpperCamelCase   ,					max_new_tokens=2_0   ,					use_cache=__UpperCamelCase   )
																										lowerCamelCase_							  =					model.generate(**__UpperCamelCase   ,					do_sample=__UpperCamelCase   ,					max_new_tokens=2_0   ,					use_cache=__UpperCamelCase   )
																										self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0   )







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       __A:

					def __init__(	self					:     int   ,					__UpperCamelCase					:     Union[str, Any]   ,					__UpperCamelCase					:     Union[str, Any]=3   ,					__UpperCamelCase					:     int=3_2   ,					__UpperCamelCase					:     Any=3   ,					__UpperCamelCase					:     List[str]=1_0   ,					__UpperCamelCase					:     int=[1_0, 2_0, 3_0, 4_0]   ,					__UpperCamelCase					:     List[str]=[1, 1, 2, 1]   ,					__UpperCamelCase					:     str=True   ,					__UpperCamelCase					:     List[str]=True   ,					__UpperCamelCase					:     Dict="relu"   ,					__UpperCamelCase					:     int=3   ,					__UpperCamelCase					:     Dict=None   ,					):
												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(__UpperCamelCase   )

					def    lowercase__      (	self					:     Any   ):
												lowerCamelCase_							  =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

												lowerCamelCase_							  =					None
												if self.use_labels:
																			lowerCamelCase_							  =					ids_tensor([self.batch_size]   ,					self.num_labels   )

												lowerCamelCase_							  =					self.get_config()
												return config, pixel_values, labels

					def    lowercase__      (	self					:     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   ,					)

					def    lowercase__      (	self					:     Optional[int]   ,					__UpperCamelCase					:     int   ,					__UpperCamelCase					:     Tuple   ,					__UpperCamelCase					:     Union[str, Any]   ):
												lowerCamelCase_							  =					TFRegNetModel(config=__UpperCamelCase   )
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					training=__UpperCamelCase   )
												# 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 // 3_2, self.image_size // 3_2)   ,					)

					def    lowercase__      (	self					:     Union[str, Any]   ,					__UpperCamelCase					:     int   ,					__UpperCamelCase					:     List[Any]   ,					__UpperCamelCase					:     List[str]   ):
												lowerCamelCase_							  =					self.num_labels
												lowerCamelCase_							  =					TFRegNetForImageClassification(__UpperCamelCase   )
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					labels=__UpperCamelCase   ,					training=__UpperCamelCase   )
												self.parent.assertEqual(result.logits.shape   ,					(self.batch_size, self.num_labels)   )



					def    lowercase__      (	self					:     int   ):
												lowerCamelCase_							  =					self.prepare_config_and_inputs()
												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					config_and_inputs
												lowerCamelCase_							  =					{"""pixel_values""": pixel_values}
												return config, inputs_dict





@require_tf
class       __A(				UpperCAmelCase     ,		UpperCAmelCase     ,		unittest.TestCase       ):
					SCREAMING_SNAKE_CASE			    =    (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
					SCREAMING_SNAKE_CASE			    =    (
					    {'''feature-extraction''': TFRegNetModel, '''image-classification''': TFRegNetForImageClassification}
					    if is_tf_available()
					    else {}
					)

					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    False

					def    lowercase__      (	self					:     Tuple   ):
												lowerCamelCase_							  =					TFRegNetModelTester(self   )
												lowerCamelCase_							  =					ConfigTester(self   ,					config_class=__UpperCamelCase   ,					has_text_modality=__UpperCamelCase   )

					def    lowercase__      (	self					:     Union[str, Any]   ):
												return

					@unittest.skip(reason="""RegNet does not use inputs_embeds"""   )
					def    lowercase__      (	self					:     Optional[int]   ):
												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					:     Tuple   ):
												super().test_keras_fit()

					@unittest.skip(reason="""RegNet does not support input and output embeddings"""   )
					def    lowercase__      (	self					:     Optional[Any]   ):
												pass

					def    lowercase__      (	self					:     Dict   ):
												lowerCamelCase_  ,   lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																			lowerCamelCase_							  =					model_class(__UpperCamelCase   )
																			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]   ,					__UpperCamelCase   )

					def    lowercase__      (	self					:     Any   ):
												lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__UpperCamelCase   )

					def    lowercase__      (	self					:     int   ):
												def check_hidden_states_output(__UpperCamelCase					:     Union[str, Any]   ,					__UpperCamelCase					:     str   ,					__UpperCamelCase					:     Any   ):
																			lowerCamelCase_							  =					model_class(__UpperCamelCase   )
																			lowerCamelCase_							  =					model(**self._prepare_for_class(__UpperCamelCase   ,					__UpperCamelCase   )   ,					training=__UpperCamelCase   )

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

																			lowerCamelCase_							  =					self.model_tester.num_stages
																			self.assertEqual(len(__UpperCamelCase   )   ,					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]   ,					)

												lowerCamelCase_  ,   lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs_for_common()
												lowerCamelCase_							  =					["""basic""", """bottleneck"""]
												for model_class in self.all_model_classes:
																			for layer_type in layers_type:
																										lowerCamelCase_							  =					layer_type
																										lowerCamelCase_							  =					True
																										check_hidden_states_output(__UpperCamelCase   ,					__UpperCamelCase   ,					__UpperCamelCase   )

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

																										check_hidden_states_output(__UpperCamelCase   ,					__UpperCamelCase   ,					__UpperCamelCase   )

					def    lowercase__      (	self					:     Optional[Any]   ):
												lowerCamelCase_  ,   lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs_for_common()

												def check_equivalence(__UpperCamelCase					:     List[str]   ,					__UpperCamelCase					:     Optional[Any]   ,					__UpperCamelCase					:     Tuple   ,					__UpperCamelCase					:     List[Any]={}   ):
																			lowerCamelCase_							  =					model(__UpperCamelCase   ,					return_dict=__UpperCamelCase   ,					**__UpperCamelCase   )
																			lowerCamelCase_							  =					model(__UpperCamelCase   ,					return_dict=__UpperCamelCase   ,					**__UpperCamelCase   ).to_tuple()

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

																										recursive_check(__UpperCamelCase   ,					__UpperCamelCase   )

												for model_class in self.all_model_classes:
																			lowerCamelCase_							  =					model_class(__UpperCamelCase   )

																			lowerCamelCase_							  =					self._prepare_for_class(__UpperCamelCase   ,					__UpperCamelCase   )
																			lowerCamelCase_							  =					self._prepare_for_class(__UpperCamelCase   ,					__UpperCamelCase   )
																			check_equivalence(__UpperCamelCase   ,					__UpperCamelCase   ,					__UpperCamelCase   )

																			lowerCamelCase_							  =					self._prepare_for_class(__UpperCamelCase   ,					__UpperCamelCase   ,					return_labels=__UpperCamelCase   )
																			lowerCamelCase_							  =					self._prepare_for_class(__UpperCamelCase   ,					__UpperCamelCase   ,					return_labels=__UpperCamelCase   )
																			check_equivalence(__UpperCamelCase   ,					__UpperCamelCase   ,					__UpperCamelCase   )

																			lowerCamelCase_							  =					self._prepare_for_class(__UpperCamelCase   ,					__UpperCamelCase   )
																			lowerCamelCase_							  =					self._prepare_for_class(__UpperCamelCase   ,					__UpperCamelCase   )
																			check_equivalence(__UpperCamelCase   ,					__UpperCamelCase   ,					__UpperCamelCase   ,					{"""output_hidden_states""": True}   )

																			lowerCamelCase_							  =					self._prepare_for_class(__UpperCamelCase   ,					__UpperCamelCase   ,					return_labels=__UpperCamelCase   )
																			lowerCamelCase_							  =					self._prepare_for_class(__UpperCamelCase   ,					__UpperCamelCase   ,					return_labels=__UpperCamelCase   )
																			check_equivalence(__UpperCamelCase   ,					__UpperCamelCase   ,					__UpperCamelCase   ,					{"""output_hidden_states""": True}   )

					def    lowercase__      (	self					:     Union[str, Any]   ):
												lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase   )



					@slow
					def    lowercase__      (	self					:     List[Any]   ):
												for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			lowerCamelCase_							  =					TFRegNetModel.from_pretrained(__UpperCamelCase   )
																			self.assertIsNotNone(__UpperCamelCase   )



def      __lowerCAmelCase						(							)  ->    Optional[int]:
							lowerCamelCase_							  =					Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
							return image





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

					@cached_property
					def    lowercase__      (	self					:     Union[str, Any]   ):
												return (
												    AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )
												    if is_vision_available()
												    else None
												)



					@slow
					def    lowercase__      (	self					:     List[str]   ):
												lowerCamelCase_							  =					TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )

												lowerCamelCase_							  =					self.default_image_processor
												lowerCamelCase_							  =					prepare_img()
												lowerCamelCase_							  =					image_processor(images=__UpperCamelCase   ,					return_tensors="""tf"""   )

												# forward pass
												lowerCamelCase_							  =					model(**__UpperCamelCase   ,					training=__UpperCamelCase   )

												# verify the logits
												lowerCamelCase_							  =					tf.TensorShape((1, 1_0_0_0)   )
												self.assertEqual(outputs.logits.shape   ,					__UpperCamelCase   )

												lowerCamelCase_							  =					tf.constant([-0.4180, -1.5051, -3.4836]   )

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





