


"""simple docstring"""
import tempfile
import unittest

import numpy as np

import transformers
from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


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

			from transformers.modeling_flax_pytorch_utils import (
			    convert_pytorch_state_dict_to_flax,
			    load_flax_weights_in_pytorch_model,
			)
			from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel

if is_torch_available():
			import torch



class 			_UpperCAmelCase :






		'''simple docstring'''

		def __init__(self				,       a_				,       a_=14				,       a_=7				,       a_=True				,       a_=True				,       a_=False				,       a_=True				,       a_=99				,       a_=32				,       a_=4				,       a_=4				,       a_=4				,       a_=37				,       a_="gelu"				,       a_=0.1				,       a_=0.1				,       a_=5_12				,       a_=0.02				,       ):





			'''simple docstring'''

			__snake_case	:						Any						=				parent
			__snake_case	:						List[str]						=				batch_size
			__snake_case	:						Union[str, Any]						=				seq_length
			__snake_case	:						Tuple						=				is_training
			__snake_case	:						List[Any]						=				use_input_mask
			__snake_case	:						List[Any]						=				use_token_type_ids
			__snake_case	:						Union[str, Any]						=				use_labels
			__snake_case	:						Union[str, Any]						=				vocab_size
			__snake_case	:						Dict						=				hidden_size
			__snake_case	:						int						=				rotary_dim
			__snake_case	:						Any						=				num_hidden_layers
			__snake_case	:						Union[str, Any]						=				num_attention_heads
			__snake_case	:						Dict						=				intermediate_size
			__snake_case	:						Union[str, Any]						=				hidden_act
			__snake_case	:						List[str]						=				hidden_dropout_prob
			__snake_case	:						Any						=				attention_probs_dropout_prob
			__snake_case	:						Optional[int]						=				max_position_embeddings
			__snake_case	:						Tuple						=				initializer_range
			__snake_case	:						int						=				None
			__snake_case	:						Optional[Any]						=				vocab_size - 1
			__snake_case	:						List[str]						=				vocab_size - 1
			__snake_case	:						Tuple						=				vocab_size - 1



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Dict						=				ids_tensor([self.batch_size, self.seq_length]				,       self.vocab_size					)

			__snake_case	:						int						=				None
			if self.use_input_mask:
				__snake_case	:						Union[str, Any]						=				random_attention_mask([self.batch_size, self.seq_length]					)

			__snake_case	:						int						=				GPTJConfig(
			    vocab_size=self.vocab_size				,       n_embd=self.hidden_size				,       n_layer=self.num_hidden_layers				,       n_head=self.num_attention_heads				,       n_positions=self.max_position_embeddings				,       use_cache=a_				,       bos_token_id=self.bos_token_id				,       eos_token_id=self.eos_token_id				,       pad_token_id=self.pad_token_id				,       rotary_dim=self.rotary_dim				,       )

			return (config, input_ids, input_mask)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[Any]						=				self.prepare_config_and_inputs()
			__snake_case	,							__snake_case	,							__snake_case	:						int						=				config_and_inputs
			__snake_case	:						Tuple						=				{'''input_ids''': input_ids, '''attention_mask''': attention_mask}
			return config, inputs_dict



		def 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_				,       a_					):





			'''simple docstring'''

			__snake_case	:						Any						=				20
			__snake_case	:						int						=				model_class_name(a_					)

			__snake_case	:						Tuple						=				model.init_cache(input_ids.shape[0]				,       a_					)
			__snake_case	:						Dict						=				jnp.ones((input_ids.shape[0], max_decoder_length)				,       dtype='''i4'''					)

			__snake_case	:						List[Any]						=				jnp.broadcast_to(
			    jnp.arange(input_ids.shape[-1] - 1					)[None, :]				,       (input_ids.shape[0], input_ids.shape[-1] - 1)					)
			__snake_case	:						Optional[Any]						=				model(
			    input_ids[:, :-1]				,       attention_mask=a_				,       past_key_values=a_				,       position_ids=a_				,       )

			__snake_case	:						List[str]						=				jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]				,       dtype='''i4'''					)
			__snake_case	:						List[str]						=				model(
			    input_ids[:, -1:]				,       attention_mask=a_				,       past_key_values=outputs_cache.past_key_values				,       position_ids=a_				,       )

			__snake_case	:						Union[str, Any]						=				model(a_					)

			__snake_case	:						Any						=				np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])					)					)
			self.parent.assertTrue(diff < 1E-3				,       msg=f"""Max diff is {diff}"""					)



		def 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_				,       a_					):





			'''simple docstring'''

			__snake_case	:						List[str]						=				20
			__snake_case	:						str						=				model_class_name(a_					)

			__snake_case	:						Tuple						=				jnp.concatenate(
			    [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1])					)]				,       axis=-1				,       )

			__snake_case	:						Union[str, Any]						=				model.init_cache(input_ids.shape[0]				,       a_					)
			__snake_case	:						Any						=				jnp.broadcast_to(
			    jnp.arange(input_ids.shape[-1] - 1					)[None, :]				,       (input_ids.shape[0], input_ids.shape[-1] - 1)					)

			__snake_case	:						List[str]						=				model(
			    input_ids[:, :-1]				,       attention_mask=a_				,       past_key_values=a_				,       position_ids=a_				,       )
			__snake_case	:						Union[str, Any]						=				jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]				,       dtype='''i4'''					)
			__snake_case	:						Optional[int]						=				model(
			    input_ids[:, -1:]				,       past_key_values=outputs_cache.past_key_values				,       attention_mask=a_				,       position_ids=a_				,       )

			__snake_case	:						Optional[int]						=				model(a_				,       attention_mask=a_					)

			__snake_case	:						List[Any]						=				np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])					)					)
			self.parent.assertTrue(diff < 1E-3				,       msg=f"""Max diff is {diff}"""					)



@require_flax
class 			_UpperCAmelCase (							__snake_case,				__snake_case,				unittest.TestCase				):






		'''simple docstring'''

		lowerCamelCase__				=(FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else ()
		lowerCamelCase__				=(FlaxGPTJForCausalLM,) if is_flax_available() else ()



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						str						=				FlaxGPTJModelTester(self					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_class_name in self.all_model_classes:
				__snake_case	,							__snake_case	,							__snake_case	:						Union[str, Any]						=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.check_use_cache_forward(a_				,       a_				,       a_				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_class_name in self.all_model_classes:
				__snake_case	,							__snake_case	,							__snake_case	:						List[Any]						=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.check_use_cache_forward_with_attn_mask(
				    a_				,       a_				,       a_				,       a_					)



		@tooslow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Tuple						=				GPTaTokenizer.from_pretrained('''gpt2'''				,       pad_token='''<|endoftext|>'''				,       padding_side='''left'''					)
			__snake_case	:						str						=				tokenizer(['''Hello this is a long string''', '''Hey''']				,       return_tensors='''np'''				,       padding=a_				,       truncation=a_					)

			__snake_case	:						Optional[int]						=				FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B'''					)
			__snake_case	:						Any						=				False
			__snake_case	:						List[str]						=				model.config.eos_token_id

			__snake_case	:						Dict						=				jax.jit(model.generate					)

			__snake_case	:						Dict						=				jit_generate(
			    inputs['''input_ids''']				,       attention_mask=inputs['''attention_mask''']				,       pad_token_id=tokenizer.pad_token_id					).sequences

			__snake_case	:						Union[str, Any]						=				tokenizer.batch_decode(a_				,       skip_special_tokens=a_					)

			__snake_case	:						int						=				[
			    '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''',
			    '''Hey, I\'m a little late to the party. I\'m going to''',
			]

			self.assertListEqual(a_				,       a_					)



		@is_pt_flax_cross_test
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	,							__snake_case	:						Tuple						=				self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				with self.subTest(model_class.__name__					):
					# prepare inputs
					__snake_case	:						Tuple						=				self._prepare_for_class(a_				,       a_					)
					__snake_case	:						str						=				{k: torch.tensor(v.tolist()					) for k, v in prepared_inputs_dict.items()}

					# load corresponding PyTorch class
					__snake_case	:						int						=				model_class.__name__[4:]  # Skip the "Flax" at the beginning
					__snake_case	:						List[Any]						=				getattr(a_				,       a_					)

					__snake_case	,							__snake_case	:						Dict						=				pt_inputs['''input_ids'''].shape
					__snake_case	:						Optional[Any]						=				np.random.randint(0				,       seq_length - 1				,       size=(batch_size,)					)
					for batch_idx, start_index in enumerate(a_					):
						__snake_case	:						List[str]						=				0
						__snake_case	:						Tuple						=				1
						__snake_case	:						int						=				0
						__snake_case	:						Optional[Any]						=				1
					__snake_case	:						Any						=				pt_model_class(a_					).eval()
					__snake_case	:						int						=				model_class(a_				,       dtype=jnp.floataa					)

					__snake_case	:						Dict						=				convert_pytorch_state_dict_to_flax(pt_model.state_dict()				,       a_					)
					__snake_case	:						str						=				fx_state

					with torch.no_grad():
						__snake_case	:						int						=				pt_model(**a_					).to_tuple()

					__snake_case	:						str						=				fx_model(**a_					).to_tuple()
					self.assertEqual(len(a_					)				,       len(a_					)				,       '''Output lengths differ between Flax and PyTorch'''					)
					for fx_output, pt_output in zip(a_				,       a_					):
						self.assert_almost_equals(fx_output[:, -1]				,       pt_output[:, -1].numpy()				,       4E-2					)

					with tempfile.TemporaryDirectory() as tmpdirname:
						pt_model.save_pretrained(a_					)
						__snake_case	:						List[str]						=				model_class.from_pretrained(a_				,       from_pt=a_					)

					__snake_case	:						int						=				fx_model_loaded(**a_					).to_tuple()
					self.assertEqual(
					    len(a_					)				,       len(a_					)				,       '''Output lengths differ between Flax and PyTorch'''					)
					for fx_output_loaded, pt_output in zip(a_				,       a_					):
						self.assert_almost_equals(fx_output_loaded[:, -1]				,       pt_output[:, -1].numpy()				,       4E-2					)



		@is_pt_flax_cross_test
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	,							__snake_case	:						int						=				self.model_tester.prepare_config_and_inputs_for_common()
			for model_class in self.all_model_classes:
				with self.subTest(model_class.__name__					):
					# prepare inputs
					__snake_case	:						List[Any]						=				self._prepare_for_class(a_				,       a_					)
					__snake_case	:						Dict						=				{k: torch.tensor(v.tolist()					) for k, v in prepared_inputs_dict.items()}

					# load corresponding PyTorch class
					__snake_case	:						str						=				model_class.__name__[4:]  # Skip the "Flax" at the beginning
					__snake_case	:						List[Any]						=				getattr(a_				,       a_					)

					__snake_case	:						List[str]						=				pt_model_class(a_					).eval()
					__snake_case	:						int						=				model_class(a_				,       dtype=jnp.floataa					)

					__snake_case	:						Dict						=				load_flax_weights_in_pytorch_model(a_				,       fx_model.params					)
					__snake_case	,							__snake_case	:						Union[str, Any]						=				pt_inputs['''input_ids'''].shape
					__snake_case	:						Optional[Any]						=				np.random.randint(0				,       seq_length - 1				,       size=(batch_size,)					)
					for batch_idx, start_index in enumerate(a_					):
						__snake_case	:						Any						=				0
						__snake_case	:						int						=				1
						__snake_case	:						int						=				0
						__snake_case	:						int						=				1

					# make sure weights are tied in PyTorch
					pt_model.tie_weights()

					with torch.no_grad():
						__snake_case	:						List[Any]						=				pt_model(**a_					).to_tuple()

					__snake_case	:						List[str]						=				fx_model(**a_					).to_tuple()
					self.assertEqual(len(a_					)				,       len(a_					)				,       '''Output lengths differ between Flax and PyTorch'''					)
					for fx_output, pt_output in zip(a_				,       a_					):
						self.assert_almost_equals(fx_output[:, -1]				,       pt_output[:, -1].numpy()				,       4E-2					)

					with tempfile.TemporaryDirectory() as tmpdirname:
						fx_model.save_pretrained(a_					)
						__snake_case	:						Any						=				pt_model_class.from_pretrained(a_				,       from_flax=a_					)

					with torch.no_grad():
						__snake_case	:						Union[str, Any]						=				pt_model_loaded(**a_					).to_tuple()

					self.assertEqual(
					    len(a_					)				,       len(a_					)				,       '''Output lengths differ between Flax and PyTorch'''					)
					for fx_output, pt_output in zip(a_				,       a_					):
						self.assert_almost_equals(fx_output[:, -1]				,       pt_output[:, -1].numpy()				,       4E-2					)



		@tooslow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_class_name in self.all_model_classes:
				__snake_case	:						List[str]						=				model_class_name.from_pretrained('''EleutherAI/gpt-j-6B'''					)
				__snake_case	:						int						=				model(np.ones((1, 1)					)					)
				self.assertIsNotNone(a_					)









"""simple docstring"""
import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
			from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import AutoImageProcessor



class 			_UpperCAmelCase :






		'''simple docstring'''

		def __init__(self				,       a_				,       a_=13				,       a_=32				,       a_=3				,       a_=4				,       a_=[10, 20, 30, 40]				,       a_=[2, 2, 3, 2]				,       a_=True				,       a_=True				,       a_=37				,       a_="gelu"				,       a_=10				,       a_=0.02				,       a_=["stage2", "stage3", "stage4"]				,       a_=[2, 3, 4]				,       a_=None				,       ):





			'''simple docstring'''

			__snake_case	:						List[str]						=				parent
			__snake_case	:						str						=				batch_size
			__snake_case	:						List[Any]						=				image_size
			__snake_case	:						List[Any]						=				num_channels
			__snake_case	:						str						=				num_stages
			__snake_case	:						Any						=				hidden_sizes
			__snake_case	:						Optional[int]						=				depths
			__snake_case	:						Dict						=				is_training
			__snake_case	:						Tuple						=				use_labels
			__snake_case	:						str						=				intermediate_size
			__snake_case	:						Optional[int]						=				hidden_act
			__snake_case	:						Dict						=				num_labels
			__snake_case	:						Tuple						=				initializer_range
			__snake_case	:						Dict						=				out_features
			__snake_case	:						Optional[int]						=				out_indices
			__snake_case	:						str						=				scope



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[Any]						=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

			__snake_case	:						Dict						=				self.get_config()

			return config, pixel_values, labels



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			return ConvNextVaConfig(
			    num_channels=self.num_channels				,       hidden_sizes=self.hidden_sizes				,       depths=self.depths				,       num_stages=self.num_stages				,       hidden_act=self.hidden_act				,       is_decoder=a_				,       initializer_range=self.initializer_range				,       out_features=self.out_features				,       out_indices=self.out_indices				,       num_labels=self.num_labels				,       )



		def 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_					):





			'''simple docstring'''

			__snake_case	:						Optional[int]						=				ConvNextVaModel(config=a_					)
			model.to(a_					)
			model.eval()
			__snake_case	:						Any						=				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 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_					):





			'''simple docstring'''

			__snake_case	:						Any						=				ConvNextVaForImageClassification(a_					)
			model.to(a_					)
			model.eval()
			__snake_case	:						Union[str, Any]						=				model(a_				,       labels=a_					)
			self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)					)



		def 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_					):





			'''simple docstring'''

			__snake_case	:						Dict						=				ConvNextVaBackbone(config=a_					)
			model.to(a_					)
			model.eval()
			__snake_case	:						Tuple						=				model(a_					)

			# verify hidden states
			self.parent.assertEqual(len(result.feature_maps					)				,       len(config.out_features					)					)
			self.parent.assertListEqual(list(result.feature_maps[0].shape					)				,       [self.batch_size, self.hidden_sizes[1], 4, 4]					)

			# verify channels
			self.parent.assertEqual(len(model.channels					)				,       len(config.out_features					)					)
			self.parent.assertListEqual(model.channels				,       config.hidden_sizes[1:]					)

			# verify backbone works with out_features=None
			__snake_case	:						str						=				None
			__snake_case	:						Optional[Any]						=				ConvNextVaBackbone(config=a_					)
			model.to(a_					)
			model.eval()
			__snake_case	:						Tuple						=				model(a_					)

			# verify feature maps
			self.parent.assertEqual(len(result.feature_maps					)				,       1					)
			self.parent.assertListEqual(list(result.feature_maps[0].shape					)				,       [self.batch_size, self.hidden_sizes[-1], 1, 1]					)

			# verify channels
			self.parent.assertEqual(len(model.channels					)				,       1					)
			self.parent.assertListEqual(model.channels				,       [config.hidden_sizes[-1]]					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						int						=				self.prepare_config_and_inputs()
			__snake_case	,							__snake_case	,							__snake_case	:						Union[str, Any]						=				config_and_inputs
			__snake_case	:						Any						=				{'''pixel_values''': pixel_values}
			return config, inputs_dict



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						int						=				self.prepare_config_and_inputs()
			__snake_case	,							__snake_case	,							__snake_case	:						Union[str, Any]						=				config_and_inputs
			__snake_case	:						Optional[int]						=				{'''pixel_values''': pixel_values, '''labels''': labels}
			return config, inputs_dict



@require_torch
class 			_UpperCAmelCase (							__snake_case,				__snake_case,				unittest.TestCase				):






		'''simple docstring'''

		lowerCamelCase__				=(
		    (
		        ConvNextVaModel,
		        ConvNextVaForImageClassification,
		        ConvNextVaBackbone,
		    )
		    if is_torch_available()
		    else ()
		)
		lowerCamelCase__				=(
		    {'feature-extraction': ConvNextVaModel, 'image-classification': ConvNextVaForImageClassification}
		    if is_torch_available()
		    else {}
		)

		lowerCamelCase__				=False
		lowerCamelCase__				=False
		lowerCamelCase__				=False
		lowerCamelCase__				=False
		lowerCamelCase__				=False



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Dict						=				ConvNextVaModelTester(self					)
			__snake_case	:						Union[str, Any]						=				ConfigTester(self				,       config_class=a_				,       has_text_modality=a_				,       hidden_size=37					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			self.create_and_test_config_common_properties()
			self.config_tester.create_and_test_config_to_json_string()
			self.config_tester.create_and_test_config_to_json_file()
			self.config_tester.create_and_test_config_from_and_save_pretrained()
			self.config_tester.create_and_test_config_with_num_labels()
			self.config_tester.check_config_can_be_init_without_params()
			self.config_tester.check_config_arguments_init()



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			return



		@unittest.skip(reason='''ConvNextV2 does not use inputs_embeds'''					)
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			pass



		@unittest.skip(reason='''ConvNextV2 does not support input and output embeddings'''					)
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			pass



		@unittest.skip(reason='''ConvNextV2 does not use feedforward chunking'''					)
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			pass



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			if not self.model_tester.is_training:
				return

			for model_class in self.all_model_classes:
				__snake_case	,							__snake_case	:						str						=				self.model_tester.prepare_config_and_inputs_with_labels()
				__snake_case	:						int						=				True

				if model_class.__name__ in [
				    *get_values(a_					),
				    *get_values(a_					),
				]:
					continue

				__snake_case	:						Dict						=				model_class(a_					)
				model.to(a_					)
				model.train()
				__snake_case	:						Tuple						=				self._prepare_for_class(a_				,       a_				,       return_labels=a_					)
				__snake_case	:						List[str]						=				model(**a_					).loss
				loss.backward()



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			if not self.model_tester.is_training:
				return

			for model_class in self.all_model_classes:
				__snake_case	,							__snake_case	:						Optional[Any]						=				self.model_tester.prepare_config_and_inputs_with_labels()
				__snake_case	:						Optional[Any]						=				False
				__snake_case	:						Tuple						=				True

				if (
				    model_class.__name__
				    in [*get_values(a_					), *get_values(a_					)]
				    or not model_class.supports_gradient_checkpointing
				):
					continue

				__snake_case	:						Union[str, Any]						=				model_class(a_					)
				model.to(a_					)
				model.gradient_checkpointing_enable()
				model.train()
				__snake_case	:						str						=				self._prepare_for_class(a_				,       a_				,       return_labels=a_					)
				__snake_case	:						Union[str, Any]						=				model(**a_					).loss
				loss.backward()



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	,							__snake_case	:						Optional[Any]						=				self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				__snake_case	:						Optional[Any]						=				model_class(a_					)
				__snake_case	:						int						=				inspect.signature(model.forward					)
				# signature.parameters is an OrderedDict => so arg_names order is deterministic
				__snake_case	:						int						=				[*signature.parameters.keys()]

				__snake_case	:						Any						=				['''pixel_values''']
				self.assertListEqual(arg_names[:1]				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						str						=				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			def check_hidden_states_output(a_				,       a_				,       a_					):
				__snake_case	:						Tuple						=				model_class(a_					)
				model.to(a_					)
				model.eval()

				with torch.no_grad():
					__snake_case	:						Union[str, Any]						=				model(**self._prepare_for_class(a_				,       a_					)					)

				__snake_case	:						Union[str, Any]						=				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

				__snake_case	:						List[Any]						=				self.model_tester.num_stages
				self.assertEqual(len(a_					)				,       expected_num_stages + 1					)

				# ConvNextV2'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]				,       )

			__snake_case	,							__snake_case	:						List[Any]						=				self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				__snake_case	:						str						=				True
				check_hidden_states_output(a_				,       a_				,       a_					)

				# check that output_hidden_states also work using config
				del inputs_dict["output_hidden_states"]
				__snake_case	:						str						=				True

				check_hidden_states_output(a_				,       a_				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						str						=				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__snake_case	:						str						=				ConvNextVaModel.from_pretrained(a_					)
				self.assertIsNotNone(a_					)






def 		lowercase							(     )     ->Any:
	"""simple docstring"""





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



@require_torch
@require_vision
class 			_UpperCAmelCase (							unittest.TestCase				):






		'''simple docstring'''

		@cached_property
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			return AutoImageProcessor.from_pretrained('''facebook/convnextv2-tiny-1k-224'''					) if is_vision_available() else None



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						str						=				ConvNextVaForImageClassification.from_pretrained('''facebook/convnextv2-tiny-1k-224'''					).to(a_					)

			__snake_case	:						Dict						=				self.default_image_processor
			__snake_case	:						Tuple						=				prepare_img()
			__snake_case	:						Tuple						=				preprocessor(images=a_				,       return_tensors='''pt'''					).to(a_					)

			# forward pass
			with torch.no_grad():
				__snake_case	:						Union[str, Any]						=				model(**a_					)

			# verify the logits
			__snake_case	:						Tuple						=				torch.Size((1, 10_00)					)
			self.assertEqual(outputs.logits.shape				,       a_					)

			__snake_case	:						Any						=				torch.tensor([0.9996, 0.1966, -0.4386]					).to(a_					)
			self.assertTrue(torch.allclose(outputs.logits[0, :3]				,       a_				,       atol=1E-4					)					)





