






from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import ResNetConfig
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 TFResNetForImageClassification, TFResNetModel
							from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor
class  lowerCAmelCase_       :




							def __init__(   self     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   List[Any]=3 ,							SCREAMING_SNAKE_CASE_     :   Any=32 ,							SCREAMING_SNAKE_CASE_     :   Tuple=3 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=10 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=[10, 20, 30, 40] ,							SCREAMING_SNAKE_CASE_     :   List[Any]=[1, 1, 2, 1] ,							SCREAMING_SNAKE_CASE_     :   Tuple=True ,							SCREAMING_SNAKE_CASE_     :   Tuple=True ,							SCREAMING_SNAKE_CASE_     :   Any="relu" ,							SCREAMING_SNAKE_CASE_     :   Any=3 ,							SCREAMING_SNAKE_CASE_     :   List[Any]=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(A__	)




							def 						__snake_case					(   self     :   Dict	):
											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 						__snake_case					(   self     :   Union[str, Any]	):
											return ResNetConfig(
											    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     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]	):
											lowerCAmelCase__      			=						TFResNetModel(config=A__	)
											lowerCAmelCase__      			=						model(A__	)
											# 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 						__snake_case					(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[int]	):
											lowerCAmelCase__      			=						self.num_labels
											lowerCAmelCase__      			=						TFResNetForImageClassification(A__	)
											lowerCAmelCase__      			=						model(A__ ,							labels=A__	)
											self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.num_labels)	)




							def 						__snake_case					(   self     :   Dict	):
											lowerCAmelCase__      			=						self.prepare_config_and_inputs()
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						config_and_inputs
											lowerCAmelCase__      			=						{'''pixel_values''': pixel_values}
											return config, inputs_dict



@require_tf
class  lowerCAmelCase_       (				_lowerCamelCase     ,    _lowerCamelCase     ,    unittest.TestCase					):
							UpperCamelCase_		:List[str]		      =    (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
							UpperCamelCase_		:List[str]		      =    (
							    {'feature-extraction': TFResNetModel, 'image-classification': TFResNetForImageClassification}
							    if is_tf_available()
							    else {}
							)

							UpperCamelCase_		:Optional[Any]		      =    False
							UpperCamelCase_		:Tuple		      =    False
							UpperCamelCase_		:Any		      =    False
							UpperCamelCase_		:Tuple		      =    False
							UpperCamelCase_		:Dict		      =    False




							def 						__snake_case					(   self     :   Any	):
											lowerCAmelCase__      			=						TFResNetModelTester(self	)
											lowerCAmelCase__      			=						ConfigTester(self ,							config_class=A__ ,							has_text_modality=A__	)




							def 						__snake_case					(   self     :   int	):
											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     :   int	):
											return




							@unittest.skip(reason='''ResNet does not use inputs_embeds'''	)
							def 						__snake_case					(   self     :   int	):
											pass




							@unittest.skip(reason='''ResNet does not support input and output embeddings'''	)
							def 						__snake_case					(   self     :   int	):
											pass




							def 						__snake_case					(   self     :   Any	):
											lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_common()

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




							def 						__snake_case					(   self     :   Dict	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*A__	)




							def 						__snake_case					(   self     :   Optional[int]	):
											def check_hidden_states_output(SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]	):
															lowerCAmelCase__      			=						model_class(A__	)
															lowerCAmelCase__      			=						model(**self._prepare_for_class(A__ ,							A__	)	)

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

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

															# ResNet'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 // 4, self.model_tester.image_size // 4] ,							)

											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(A__ ,							A__ ,							A__	)

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

																			check_hidden_states_output(A__ ,							A__ ,							A__	)




							def 						__snake_case					(   self     :   int	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*A__	)




							@slow
							def 						__snake_case					(   self     :   List[Any]	):
											for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowerCAmelCase__      			=						TFResNetModel.from_pretrained(A__	)
															self.assertIsNotNone(A__	)





def 	lowerCAmelCase_		()		->    str:


				'''simple docstring'''




				lowerCAmelCase__      			=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
				return image




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




							@cached_property
							def 						__snake_case					(   self     :   Any	):
											return (
											    AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)
											    if is_vision_available()
											    else None
											)




							@slow
							def 						__snake_case					(   self     :   str	):
											lowerCAmelCase__      			=						TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)

											lowerCAmelCase__      			=						self.default_image_processor
											lowerCAmelCase__      			=						prepare_img()
											lowerCAmelCase__      			=						image_processor(images=A__ ,							return_tensors='''tf'''	)

											# forward pass
											lowerCAmelCase__      			=						model(**A__	)

											# verify the logits
											lowerCAmelCase__      			=						tf.TensorShape((1, 1_000)	)
											self.assertEqual(outputs.logits.shape ,							A__	)

											lowerCAmelCase__      			=						tf.constant([-11.1_069, -9.7_877, -8.3_777]	)

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













from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


_UpperCAmelCase			:		Tuple   				=    logging.get_logger(__name__)


_UpperCAmelCase			:		int   				=    {
    "Salesforce/codegen-350M-nl": "https://huggingface.co/Salesforce/codegen-350M-nl/resolve/main/config.json",
    "Salesforce/codegen-350M-multi": "https://huggingface.co/Salesforce/codegen-350M-multi/resolve/main/config.json",
    "Salesforce/codegen-350M-mono": "https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/config.json",
    "Salesforce/codegen-2B-nl": "https://huggingface.co/Salesforce/codegen-2B-nl/resolve/main/config.json",
    "Salesforce/codegen-2B-multi": "https://huggingface.co/Salesforce/codegen-2B-multi/resolve/main/config.json",
    "Salesforce/codegen-2B-mono": "https://huggingface.co/Salesforce/codegen-2B-mono/resolve/main/config.json",
    "Salesforce/codegen-6B-nl": "https://huggingface.co/Salesforce/codegen-6B-nl/resolve/main/config.json",
    "Salesforce/codegen-6B-multi": "https://huggingface.co/Salesforce/codegen-6B-multi/resolve/main/config.json",
    "Salesforce/codegen-6B-mono": "https://huggingface.co/Salesforce/codegen-6B-mono/resolve/main/config.json",
    "Salesforce/codegen-16B-nl": "https://huggingface.co/Salesforce/codegen-16B-nl/resolve/main/config.json",
    "Salesforce/codegen-16B-multi": "https://huggingface.co/Salesforce/codegen-16B-multi/resolve/main/config.json",
    "Salesforce/codegen-16B-mono": "https://huggingface.co/Salesforce/codegen-16B-mono/resolve/main/config.json",
}
class  lowerCAmelCase_       (				snake_case__					):
							UpperCamelCase_		:int		      =    'codegen'
							UpperCamelCase_		:int		      =    {
							    'max_position_embeddings': 'n_positions',
							    'hidden_size': 'n_embd',
							    'num_attention_heads': 'n_head',
							    'num_hidden_layers': 'n_layer',
							}




							def __init__(   self     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   str=50_400 ,							SCREAMING_SNAKE_CASE_     :   str=2_048 ,							SCREAMING_SNAKE_CASE_     :   int=2_048 ,							SCREAMING_SNAKE_CASE_     :   Any=4_096 ,							SCREAMING_SNAKE_CASE_     :   List[Any]=28 ,							SCREAMING_SNAKE_CASE_     :   str=16 ,							SCREAMING_SNAKE_CASE_     :   str=64 ,							SCREAMING_SNAKE_CASE_     :   Tuple=None ,							SCREAMING_SNAKE_CASE_     :   Dict="gelu_new" ,							SCREAMING_SNAKE_CASE_     :   str=0.0 ,							SCREAMING_SNAKE_CASE_     :   Any=0.0 ,							SCREAMING_SNAKE_CASE_     :   Any=0.0 ,							SCREAMING_SNAKE_CASE_     :   Any=1e-5 ,							SCREAMING_SNAKE_CASE_     :   Any=0.02 ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]=True ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=50_256 ,							SCREAMING_SNAKE_CASE_     :   Any=50_256 ,							SCREAMING_SNAKE_CASE_     :   List[str]=False ,							**SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							):
											lowerCAmelCase__      			=						vocab_size
											lowerCAmelCase__      			=						n_ctx
											lowerCAmelCase__      			=						n_positions
											lowerCAmelCase__      			=						n_embd
											lowerCAmelCase__      			=						n_layer
											lowerCAmelCase__      			=						n_head
											lowerCAmelCase__      			=						n_inner
											lowerCAmelCase__      			=						rotary_dim
											lowerCAmelCase__      			=						activation_function
											lowerCAmelCase__      			=						resid_pdrop
											lowerCAmelCase__      			=						embd_pdrop
											lowerCAmelCase__      			=						attn_pdrop
											lowerCAmelCase__      			=						layer_norm_epsilon
											lowerCAmelCase__      			=						initializer_range
											lowerCAmelCase__      			=						use_cache

											lowerCAmelCase__      			=						bos_token_id
											lowerCAmelCase__      			=						eos_token_id

											super().__init__(
											    bos_token_id=SCREAMING_SNAKE_CASE_ ,							eos_token_id=SCREAMING_SNAKE_CASE_ ,							tie_word_embeddings=SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)



class  lowerCAmelCase_       (				snake_case__					):




							def __init__(   self     :   str ,							SCREAMING_SNAKE_CASE_     :   PretrainedConfig ,							SCREAMING_SNAKE_CASE_     :   str = "default" ,							SCREAMING_SNAKE_CASE_     :   List[PatchingSpec] = None ,							SCREAMING_SNAKE_CASE_     :   bool = False ,							):
											super().__init__(SCREAMING_SNAKE_CASE_ ,							task=SCREAMING_SNAKE_CASE_ ,							patching_specs=SCREAMING_SNAKE_CASE_ ,							use_past=SCREAMING_SNAKE_CASE_	)
											if not getattr(self._config ,							'''pad_token_id''' ,							SCREAMING_SNAKE_CASE_	):
															# TODO: how to do that better?
															lowerCAmelCase__      			=						0




							@property
							def 						__snake_case					(   self     :   str	):
											lowerCAmelCase__      			=						OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}	)
											if self.use_past:
															self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE_ ,							direction='''inputs'''	)
															lowerCAmelCase__      			=						{0: '''batch''', 1: '''past_sequence + sequence'''}
											else:
															lowerCAmelCase__      			=						{0: '''batch''', 1: '''sequence'''}

											return common_inputs




							@property
							def 						__snake_case					(   self     :   Dict	):
											return self._config.n_layer




							@property
							def 						__snake_case					(   self     :   Union[str, Any]	):
											return self._config.n_head




							def 						__snake_case					(   self     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   PreTrainedTokenizer ,							SCREAMING_SNAKE_CASE_     :   int = -1 ,							SCREAMING_SNAKE_CASE_     :   int = -1 ,							SCREAMING_SNAKE_CASE_     :   bool = False ,							SCREAMING_SNAKE_CASE_     :   Optional[TensorType] = None ,							):
											lowerCAmelCase__      			=						super(SCREAMING_SNAKE_CASE_ ,							self	).generate_dummy_inputs(
											    SCREAMING_SNAKE_CASE_ ,							batch_size=SCREAMING_SNAKE_CASE_ ,							seq_length=SCREAMING_SNAKE_CASE_ ,							is_pair=SCREAMING_SNAKE_CASE_ ,							framework=SCREAMING_SNAKE_CASE_	)

											# We need to order the input in the way they appears in the forward()
											lowerCAmelCase__      			=						OrderedDict({'''input_ids''': common_inputs['''input_ids''']}	)

											# Need to add the past_keys
											if self.use_past:
															if not is_torch_available():
																			raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''	)
															else:
																			import torch

																			lowerCAmelCase__			,		lowerCAmelCase__      			=						common_inputs['''input_ids'''].shape
																			# Not using the same length for past_key_values
																			lowerCAmelCase__      			=						seqlen + 2
																			lowerCAmelCase__      			=						(
																			    batch,
																			    self.num_attention_heads,
																			    past_key_values_length,
																			    self._config.hidden_size // self.num_attention_heads,
																			)
																			lowerCAmelCase__      			=						[
																			    (torch.zeros(SCREAMING_SNAKE_CASE_	), torch.zeros(SCREAMING_SNAKE_CASE_	)) for _ in range(self.num_layers	)
																			]

											lowerCAmelCase__      			=						common_inputs['''attention_mask''']
											if self.use_past:
															lowerCAmelCase__      			=						ordered_inputs['''attention_mask'''].dtype
															lowerCAmelCase__      			=						torch.cat(
															    [ordered_inputs['''attention_mask'''], torch.ones(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							dtype=SCREAMING_SNAKE_CASE_	)] ,							dim=1	)

											return ordered_inputs




							@property
							def 						__snake_case					(   self     :   Optional[int]	):
											return 13





