



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







					def __init__(							self   :  List[str]      ,		_A   :  str      ,		_A   :  List[Any]=13      ,		_A   :  Any=32      ,		_A   :  List[Any]=3      ,		_A   :  List[str]=4      ,		_A   :  Any=[10, 20, 30, 40]      ,		_A   :  List[Any]=[2, 2, 3, 2]      ,		_A   :  int=True      ,		_A   :  Union[str, Any]=True      ,		_A   :  List[Any]=37      ,		_A   :  Union[str, Any]="gelu"      ,		_A   :  Tuple=10      ,		_A   :  int=0.02      ,		_A   :  Dict=3      ,		_A   :  int=None      ,		):
											A__						:				List[Any]         =			parent
											A__						:				Optional[int]         =			batch_size
											A__						:				Tuple         =			image_size
											A__						:				Optional[int]         =			num_channels
											A__						:				Dict         =			num_stages
											A__						:				Union[str, Any]         =			hidden_sizes
											A__						:				str         =			depths
											A__						:				List[str]         =			is_training
											A__						:				List[Any]         =			use_labels
											A__						:				Dict         =			intermediate_size
											A__						:				List[Any]         =			hidden_act
											A__						:				Union[str, Any]         =			type_sequence_label_size
											A__						:				Union[str, Any]         =			initializer_range
											A__						:				List[str]         =			scope







					def 			_lowercase      (							self   :  Union[str, Any]):
											A__						:				Optional[int]         =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

											A__						:				Dict         =			None
											if self.use_labels:
																	A__						:				List[Any]         =			ids_tensor([self.batch_size]      ,		self.type_sequence_label_size)

											A__						:				Tuple         =			self.get_config()

											return config, pixel_values, labels







					def 			_lowercase      (							self   :  Optional[Any]):
											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=lowercase_      ,		initializer_range=self.initializer_range      ,		)







					def 			_lowercase      (							self   :  List[str]      ,		_A   :  str      ,		_A   :  int      ,		_A   :  Optional[int]):
											A__						:				str         =			TFConvNextModel(config=lowercase_)
											A__						:				str         =			model(lowercase_      ,		training=lowercase_)
											# expected last hidden states: B, C, H // 32, W // 32
											self.parent.assertEqual(
											    result.last_hidden_state.shape      ,		(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)      ,		)







					def 			_lowercase      (							self   :  List[str]      ,		_A   :  Optional[Any]      ,		_A   :  Any      ,		_A   :  str):
											A__						:				Optional[int]         =			self.type_sequence_label_size
											A__						:				Tuple         =			TFConvNextForImageClassification(lowercase_)
											A__						:				List[Any]         =			model(lowercase_      ,		labels=lowercase_      ,		training=lowercase_)
											self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.type_sequence_label_size))







					def 			_lowercase      (							self   :  str):
											A__						:				Union[str, Any]         =			self.prepare_config_and_inputs()
											A__     ,							A__     ,							A__						:				List[Any]         =			config_and_inputs
											A__						:				int         =			{"pixel_values": pixel_values}
											return config, inputs_dict




@require_tf
class 				lowerCAmelCase__	(						__lowerCAmelCase       ,				__lowerCAmelCase       ,				unittest.TestCase    ):
					__A  :     Any     =    (TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
					__A  :     str     =    (
					    {'feature-extraction': TFConvNextModel, 'image-classification': TFConvNextForImageClassification}
					    if is_tf_available()
					    else {}
					)

					__A  :     Any     =    False
					__A  :     Tuple     =    False
					__A  :     Union[str, Any]     =    False
					__A  :     List[Any]     =    False
					__A  :     Any     =    False







					def 			_lowercase      (							self   :  Optional[int]):
											A__						:				int         =			TFConvNextModelTester(self)
											A__						:				List[Any]         =			ConfigTester(
											    self      ,		config_class=lowercase_      ,		has_text_modality=lowercase_      ,		hidden_size=37      ,		)







					@unittest.skip(reason="ConvNext does not use inputs_embeds")
					def 			_lowercase      (							self   :  Tuple):
											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   :  str):
											super().test_keras_fit()







					@unittest.skip(reason="ConvNext does not support input and output embeddings")
					def 			_lowercase      (							self   :  Union[str, Any]):
											pass







					def 			_lowercase      (							self   :  int):
											A__     ,							A__						:				Tuple         =			self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	A__						:				int         =			model_class(lowercase_)
																	A__						:				Union[str, Any]         =			inspect.signature(model.call)
																	# signature.parameters is an OrderedDict => so arg_names order is deterministic
																	A__						:				str         =			[*signature.parameters.keys()]

																	A__						:				Dict         =			["pixel_values"]
																	self.assertListEqual(arg_names[:1]      ,		lowercase_)







					def 			_lowercase      (							self   :  Dict):
											A__						:				Tuple         =			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*lowercase_)







					@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 			_lowercase      (							self   :  Optional[Any]):
											super().test_dataset_conversion()







					def 			_lowercase      (							self   :  Optional[Any]):
											def check_hidden_states_output(_A   :  List[Any]      ,		_A   :  Optional[Any]      ,		_A   :  Optional[int]):
																	A__						:				Optional[int]         =			model_class(lowercase_)

																	A__						:				List[Any]         =			model(**self._prepare_for_class(lowercase_      ,		lowercase_))
																	A__						:				str         =			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																	A__						:				List[Any]         =			self.model_tester.num_stages
																	self.assertEqual(len(lowercase_)      ,		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]      ,		)

											A__     ,							A__						:				Optional[int]         =			self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	A__						:				Dict         =			True
																	check_hidden_states_output(lowercase_      ,		lowercase_      ,		lowercase_)

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

																	check_hidden_states_output(lowercase_      ,		lowercase_      ,		lowercase_)







					def 			_lowercase      (							self   :  Optional[Any]):
											A__     ,							A__						:				str         =			self.model_tester.prepare_config_and_inputs_for_common()

											def check_equivalence(_A   :  Optional[int]      ,		_A   :  Any      ,		_A   :  Dict      ,		_A   :  Optional[Any]={}):
																	A__						:				Any         =			model(lowercase_      ,		return_dict=lowercase_      ,		**lowercase_)
																	A__						:				List[Any]         =			model(lowercase_      ,		return_dict=lowercase_      ,		**lowercase_).to_tuple()

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

																							recursive_check(lowercase_      ,		lowercase_)

											for model_class in self.all_model_classes:
																	A__						:				List[str]         =			model_class(lowercase_)

																	A__						:				Dict         =			self._prepare_for_class(lowercase_      ,		lowercase_)
																	A__						:				Dict         =			self._prepare_for_class(lowercase_      ,		lowercase_)
																	check_equivalence(lowercase_      ,		lowercase_      ,		lowercase_)

																	A__						:				Optional[Any]         =			self._prepare_for_class(lowercase_      ,		lowercase_      ,		return_labels=lowercase_)
																	A__						:				Union[str, Any]         =			self._prepare_for_class(lowercase_      ,		lowercase_      ,		return_labels=lowercase_)
																	check_equivalence(lowercase_      ,		lowercase_      ,		lowercase_)

																	A__						:				Optional[int]         =			self._prepare_for_class(lowercase_      ,		lowercase_)
																	A__						:				Optional[int]         =			self._prepare_for_class(lowercase_      ,		lowercase_)
																	check_equivalence(lowercase_      ,		lowercase_      ,		lowercase_      ,		{"output_hidden_states": True})

																	A__						:				Dict         =			self._prepare_for_class(lowercase_      ,		lowercase_      ,		return_labels=lowercase_)
																	A__						:				Any         =			self._prepare_for_class(lowercase_      ,		lowercase_      ,		return_labels=lowercase_)
																	check_equivalence(lowercase_      ,		lowercase_      ,		lowercase_      ,		{"output_hidden_states": True})







					def 			_lowercase      (							self   :  int):
											A__						:				Tuple         =			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*lowercase_)







					@slow
					def 			_lowercase      (							self   :  Tuple):
											A__						:				Optional[Any]         =			TFConvNextModel.from_pretrained("facebook/convnext-tiny-224")
											self.assertIsNotNone(lowercase_)




def 						snake_case__							(       )		->		Optional[int]:







						"""simple docstring"""

						A__						:				Dict         =			Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
						return image




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







					@cached_property
					def 			_lowercase      (							self   :  str):
											return ConvNextImageProcessor.from_pretrained("facebook/convnext-tiny-224") if is_vision_available() else None







					@slow
					def 			_lowercase      (							self   :  List[Any]):
											A__						:				int         =			TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224")

											A__						:				List[Any]         =			self.default_image_processor
											A__						:				Union[str, Any]         =			prepare_img()
											A__						:				Tuple         =			image_processor(images=lowercase_      ,		return_tensors="tf")

											# forward pass
											A__						:				Tuple         =			model(**lowercase_)

											# verify the logits
											A__						:				Optional[int]         =			tf.TensorShape((1, 1000))
											self.assertEqual(outputs.logits.shape      ,		lowercase_)

											A__						:				Dict         =			tf.constant([-0.02_60, -0.47_39, 0.19_11])

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


'''simple docstring'''

import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor







class lowerCamelCase   (    unittest.TestCase				):

				def 					_lowerCamelCase    (       self,						lowercase_ = False )    ->							Union[str, Any]:
					snake_case							=					10

					if only_cross_attention:
						snake_case							=					12
					else:
						# when only cross attention is not set, the cross attention dim must be the same as the query dim
						snake_case							=					query_dim

					return {
					    "query_dim": query_dim,
					    "cross_attention_dim": cross_attention_dim,
					    "heads": 2,
					    "dim_head": 4,
					    "added_kv_proj_dim": 6,
					    "norm_num_groups": 1,
					    "only_cross_attention": only_cross_attention,
					    "processor": AttnAddedKVProcessor(),
					}

				def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_ )    ->							Optional[int]:
					snake_case							=					2

					snake_case							=					torch.rand(lowercase_,						lowercase_,						3,						2 )
					snake_case							=					torch.rand(lowercase_,						4,						lowercase_ )
					snake_case							=					None

					return {
					    "hidden_states": hidden_states,
					    "encoder_hidden_states": encoder_hidden_states,
					    "attention_mask": attention_mask,
					}






				def 					_lowerCamelCase    (       self )    ->							str:
					# self and cross attention

					torch.manual_seed(0 )

					snake_case							=					self.get_constructor_arguments(only_cross_attention=lowercase_ )
					snake_case							=					Attention(**lowercase_ )

					self.assertTrue(attn.to_k is not None )
					self.assertTrue(attn.to_v is not None )

					snake_case							=					self.get_forward_arguments(
					    query_dim=constructor_args['query_dim'],						added_kv_proj_dim=constructor_args['added_kv_proj_dim'] )

					snake_case							=					attn(**lowercase_ )

					# only self attention

					torch.manual_seed(0 )

					snake_case							=					self.get_constructor_arguments(only_cross_attention=lowercase_ )
					snake_case							=					Attention(**lowercase_ )

					self.assertTrue(attn.to_k is None )
					self.assertTrue(attn.to_v is None )

					snake_case							=					self.get_forward_arguments(
					    query_dim=constructor_args['query_dim'],						added_kv_proj_dim=constructor_args['added_kv_proj_dim'] )

					snake_case							=					attn(**lowercase_ )

					self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all() )







class lowerCamelCase   (    unittest.TestCase				):

				def 					_lowerCamelCase    (       self )    ->							List[Any]:
					snake_case							=					DiffusionPipeline.from_pretrained('hf-internal-testing/tiny-stable-diffusion-pipe',						safety_checker=lowercase_ )

					snake_case							=					pipe(
					    'foo',						num_inference_steps=2,						generator=torch.Generator('cpu' ).manual_seed(0 ),						output_type='np',						).images

					# the initial conversion succeeds
					snake_case							=					DiffusionPipeline.from_pretrained(
					    'hf-internal-testing/tiny-stable-diffusion-pipe',						device_map='sequential',						safety_checker=lowercase_ )

					snake_case							=					pipe(
					    'foo',						num_inference_steps=2,						generator=torch.Generator('cpu' ).manual_seed(0 ),						output_type='np',						).images

					with tempfile.TemporaryDirectory() as tmpdir:
						# save the converted model
						pipe.save_pretrained(lowercase_ )

						# can also load the converted weights
						snake_case							=					DiffusionPipeline.from_pretrained(lowercase_,						device_map='sequential',						safety_checker=lowercase_ )

					snake_case							=					pipe(
					    'foo',						num_inference_steps=2,						generator=torch.Generator('cpu' ).manual_seed(0 ),						output_type='np',						).images

					self.assertTrue(np.allclose(lowercase_,						lowercase_,						atol=1E-5 ) )
					self.assertTrue(np.allclose(lowercase_,						lowercase_,						atol=1E-5 ) )





