





'''simple docstring'''

from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
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 TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
			from PIL import Image

			from transformers import ConvNextImageProcessor


class      SCREAMING_SNAKE_CASE__      :







					"""simple docstring"""






					def __init__(   self       :      Union[str, Any]		,	UpperCAmelCase_       :      int		,	UpperCAmelCase_       :      Dict=13		,	UpperCAmelCase_       :      Any=32		,	UpperCAmelCase_       :      Union[str, Any]=3		,	UpperCAmelCase_       :      Dict=4		,	UpperCAmelCase_       :      List[Any]=[10, 20, 30, 40]		,	UpperCAmelCase_       :      Tuple=[2, 2, 3, 2]		,	UpperCAmelCase_       :      Optional[Any]=True		,	UpperCAmelCase_       :      Optional[int]=True		,	UpperCAmelCase_       :      int=37		,	UpperCAmelCase_       :      Dict="gelu"		,	UpperCAmelCase_       :      List[str]=10		,	UpperCAmelCase_       :      List[str]=0.02		,	UpperCAmelCase_       :      Optional[int]=3		,	UpperCAmelCase_       :      List[Any]=None		,	):







							"""simple docstring"""
							__UpperCAmelCase	:							Any     =     parent
							__UpperCAmelCase	:							Optional[Any]     =     batch_size
							__UpperCAmelCase	:							Union[str, Any]     =     image_size
							__UpperCAmelCase	:							Dict     =     num_channels
							__UpperCAmelCase	:							List[Any]     =     num_stages
							__UpperCAmelCase	:							Dict     =     hidden_sizes
							__UpperCAmelCase	:							List[str]     =     depths
							__UpperCAmelCase	:							Optional[Any]     =     is_training
							__UpperCAmelCase	:							Dict     =     use_labels
							__UpperCAmelCase	:							List[str]     =     intermediate_size
							__UpperCAmelCase	:							str     =     hidden_act
							__UpperCAmelCase	:							Any     =     type_sequence_label_size
							__UpperCAmelCase	:							str     =     initializer_range
							__UpperCAmelCase	:							int     =     scope
					def 		lowerCamelCase_   (   self       :      Optional[int]		):







							"""simple docstring"""
							__UpperCAmelCase	:							List[str]     =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

							__UpperCAmelCase	:							Optional[int]     =     None
							if self.use_labels:
									__UpperCAmelCase	:							Optional[Any]     =     ids_tensor([self.batch_size]		,	self.type_sequence_label_size		)

							__UpperCAmelCase	:							List[Any]     =     self.get_config()

							return config, pixel_values, labels
					def 		lowerCamelCase_   (   self       :      int		):







							"""simple docstring"""
							return ConvNextConfig(
							    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=UpperCAmelCase_		,	initializer_range=self.initializer_range		,	)
					def 		lowerCamelCase_   (   self       :      Optional[int]		,	UpperCAmelCase_       :      Optional[Any]		,	UpperCAmelCase_       :      str		,	UpperCAmelCase_       :      str		):







							"""simple docstring"""
							__UpperCAmelCase	:							Any     =     TFConvNextModel(config=UpperCAmelCase_		)
							__UpperCAmelCase	:							List[str]     =     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 // 32, self.image_size // 32)		,	)
					def 		lowerCamelCase_   (   self       :      int		,	UpperCAmelCase_       :      Dict		,	UpperCAmelCase_       :      str		,	UpperCAmelCase_       :      Optional[int]		):







							"""simple docstring"""
							__UpperCAmelCase	:							Optional[Any]     =     self.type_sequence_label_size
							__UpperCAmelCase	:							str     =     TFConvNextForImageClassification(UpperCAmelCase_		)
							__UpperCAmelCase	:							Any     =     model(UpperCAmelCase_		,	labels=UpperCAmelCase_		,	training=UpperCAmelCase_		)
							self.parent.assertEqual(result.logits.shape		,	(self.batch_size, self.type_sequence_label_size)		)






					def 		lowerCamelCase_   (   self       :      Any		):







							"""simple docstring"""
							__UpperCAmelCase	:							Optional[Any]     =     self.prepare_config_and_inputs()
							__UpperCAmelCase  ,			__UpperCAmelCase  ,			__UpperCAmelCase	:							int     =     config_and_inputs
							__UpperCAmelCase	:							List[str]     =     {"pixel_values": pixel_values}
							return config, inputs_dict






@require_tf
class      SCREAMING_SNAKE_CASE__      (   snake_case__							,snake_case__							,unittest.TestCase		):







					"""simple docstring"""






					SCREAMING_SNAKE_CASE							=	(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
					SCREAMING_SNAKE_CASE							=	(
					    {'''feature-extraction''': TFConvNextModel, '''image-classification''': TFConvNextForImageClassification}
					    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 		lowerCamelCase_   (   self       :      Optional[Any]		):







							"""simple docstring"""
							__UpperCAmelCase	:							str     =     TFConvNextModelTester(self		)
							__UpperCAmelCase	:							Optional[int]     =     ConfigTester(
							    self		,	config_class=UpperCAmelCase_		,	has_text_modality=UpperCAmelCase_		,	hidden_size=37		,	)
					@unittest.skip(reason="ConvNext does not use inputs_embeds"		)
					def 		lowerCamelCase_   (   self       :      Any		):







							"""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 		lowerCamelCase_   (   self       :      Union[str, Any]		):







							"""simple docstring"""
							super().test_keras_fit()
					@unittest.skip(reason="ConvNext does not support input and output embeddings"		)
					def 		lowerCamelCase_   (   self       :      int		):







							"""simple docstring"""
							pass
					def 		lowerCamelCase_   (   self       :      Union[str, Any]		):







							"""simple docstring"""
							__UpperCAmelCase  ,			__UpperCAmelCase	:							int     =     self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									__UpperCAmelCase	:							Dict     =     model_class(UpperCAmelCase_		)
									__UpperCAmelCase	:							List[str]     =     inspect.signature(model.call		)
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									__UpperCAmelCase	:							List[str]     =     [*signature.parameters.keys()]

									__UpperCAmelCase	:							List[Any]     =     ["pixel_values"]
									self.assertListEqual(arg_names[:1]		,	UpperCAmelCase_		)
					def 		lowerCamelCase_   (   self       :      Tuple		):







							"""simple docstring"""
							__UpperCAmelCase	:							Dict     =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*UpperCAmelCase_		)
					@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."		,	)
					def 		lowerCamelCase_   (   self       :      Dict		):







							"""simple docstring"""
							super().test_dataset_conversion()
					def 		lowerCamelCase_   (   self       :      Any		):







							"""simple docstring"""
							def check_hidden_states_output(UpperCAmelCase_       :      Any		,	UpperCAmelCase_       :      List[str]		,	UpperCAmelCase_       :      str		):
									__UpperCAmelCase	:							List[Any]     =     model_class(UpperCAmelCase_		)

									__UpperCAmelCase	:							Dict     =     model(**self._prepare_for_class(UpperCAmelCase_		,	UpperCAmelCase_		)		)
									__UpperCAmelCase	:							int     =     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

									__UpperCAmelCase	:							Union[str, Any]     =     self.model_tester.num_stages
									self.assertEqual(len(UpperCAmelCase_		)		,	expected_num_stages + 1		)

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

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

							for model_class in self.all_model_classes:
									__UpperCAmelCase	:							Tuple     =     True
									check_hidden_states_output(UpperCAmelCase_		,	UpperCAmelCase_		,	UpperCAmelCase_		)

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

									check_hidden_states_output(UpperCAmelCase_		,	UpperCAmelCase_		,	UpperCAmelCase_		)
					def 		lowerCamelCase_   (   self       :      str		):







							"""simple docstring"""
							__UpperCAmelCase  ,			__UpperCAmelCase	:							Tuple     =     self.model_tester.prepare_config_and_inputs_for_common()

							def check_equivalence(UpperCAmelCase_       :      Tuple		,	UpperCAmelCase_       :      str		,	UpperCAmelCase_       :      int		,	UpperCAmelCase_       :      List[Any]={}		):
									__UpperCAmelCase	:							Dict     =     model(UpperCAmelCase_		,	return_dict=UpperCAmelCase_		,	**UpperCAmelCase_		)
									__UpperCAmelCase	:							List[Any]     =     model(UpperCAmelCase_		,	return_dict=UpperCAmelCase_		,	**UpperCAmelCase_		).to_tuple()

									def recursive_check(UpperCAmelCase_       :      List[Any]		,	UpperCAmelCase_       :      Optional[int]		):
											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:
									__UpperCAmelCase	:							List[Any]     =     model_class(UpperCAmelCase_		)

									__UpperCAmelCase	:							int     =     self._prepare_for_class(UpperCAmelCase_		,	UpperCAmelCase_		)
									__UpperCAmelCase	:							List[Any]     =     self._prepare_for_class(UpperCAmelCase_		,	UpperCAmelCase_		)
									check_equivalence(UpperCAmelCase_		,	UpperCAmelCase_		,	UpperCAmelCase_		)

									__UpperCAmelCase	:							List[Any]     =     self._prepare_for_class(UpperCAmelCase_		,	UpperCAmelCase_		,	return_labels=UpperCAmelCase_		)
									__UpperCAmelCase	:							List[str]     =     self._prepare_for_class(UpperCAmelCase_		,	UpperCAmelCase_		,	return_labels=UpperCAmelCase_		)
									check_equivalence(UpperCAmelCase_		,	UpperCAmelCase_		,	UpperCAmelCase_		)

									__UpperCAmelCase	:							Any     =     self._prepare_for_class(UpperCAmelCase_		,	UpperCAmelCase_		)
									__UpperCAmelCase	:							Dict     =     self._prepare_for_class(UpperCAmelCase_		,	UpperCAmelCase_		)
									check_equivalence(UpperCAmelCase_		,	UpperCAmelCase_		,	UpperCAmelCase_		,	{"output_hidden_states": True}		)

									__UpperCAmelCase	:							Optional[int]     =     self._prepare_for_class(UpperCAmelCase_		,	UpperCAmelCase_		,	return_labels=UpperCAmelCase_		)
									__UpperCAmelCase	:							List[Any]     =     self._prepare_for_class(UpperCAmelCase_		,	UpperCAmelCase_		,	return_labels=UpperCAmelCase_		)
									check_equivalence(UpperCAmelCase_		,	UpperCAmelCase_		,	UpperCAmelCase_		,	{"output_hidden_states": True}		)
					def 		lowerCamelCase_   (   self       :      int		):







							"""simple docstring"""
							__UpperCAmelCase	:							Optional[Any]     =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_		)






					@slow
					def 		lowerCamelCase_   (   self       :      Dict		):







							"""simple docstring"""
							__UpperCAmelCase	:							Optional[int]     =     TFConvNextModel.from_pretrained("facebook/convnext-tiny-224"		)
							self.assertIsNotNone(UpperCAmelCase_		)






def 						__UpperCamelCase						(			):
		__UpperCAmelCase	:							Optional[int]     =     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"  )
		return image






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







					"""simple docstring"""






					@cached_property
					def 		lowerCamelCase_   (   self       :      List[Any]		):







							"""simple docstring"""
							return ConvNextImageProcessor.from_pretrained("facebook/convnext-tiny-224"		) if is_vision_available() else None






					@slow
					def 		lowerCamelCase_   (   self       :      int		):







							"""simple docstring"""
							__UpperCAmelCase	:							Tuple     =     TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"		)

							__UpperCAmelCase	:							Tuple     =     self.default_image_processor
							__UpperCAmelCase	:							Optional[int]     =     prepare_img()
							__UpperCAmelCase	:							str     =     image_processor(images=UpperCAmelCase_		,	return_tensors="tf"		)

							# forward pass
							__UpperCAmelCase	:							int     =     model(**UpperCAmelCase_		)

							# verify the logits
							__UpperCAmelCase	:							Any     =     tf.TensorShape((1, 1_000)		)
							self.assertEqual(outputs.logits.shape		,	UpperCAmelCase_		)

							__UpperCAmelCase	:							Optional[int]     =     tf.constant([-0.0260, -0.4739, 0.1911]		)

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











'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCAmelCase__      :   Any      	=			{
    "configuration_open_llama": ["OPEN_LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP", "OpenLlamaConfig"],
}

try:
			if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCAmelCase__      :   Optional[int]      	=			["LlamaTokenizer"]

try:
			if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCAmelCase__      :   int      	=			["LlamaTokenizerFast"]

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCAmelCase__      :   int      	=			[
			    "OpenLlamaForCausalLM",
			    "OpenLlamaModel",
			    "OpenLlamaPreTrainedModel",
			    "OpenLlamaForSequenceClassification",
			]


if TYPE_CHECKING:
			from .configuration_open_llama import OPEN_LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP, OpenLlamaConfig

			try:
						if not is_sentencepiece_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from transformers import LlamaTokenizer

			try:
						if not is_tokenizers_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from transformers import LlamaTokenizerFast

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_open_llama import (
						    OpenLlamaForCausalLM,
						    OpenLlamaForSequenceClassification,
						    OpenLlamaModel,
						    OpenLlamaPreTrainedModel,
						)


else:
			import sys

			lowerCAmelCase__      :   Dict      	=			_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)




