
'''simple docstring'''


import inspect
from typing import Callable, List, Optional, Set, Tuple, Union

import torch
from packaging import version
from safetensors.torch import storage_ptr, storage_size
from torch import nn

from .utils import logging


__lowercase							:				Union[str, Any]					=       [nn.LayerNorm]

__lowercase							:				List[str]					=       logging.get_logger(__name__)

__lowercase							:				Union[str, Any]					=       version.parse(version.parse(torch.__version__).base_version)

__lowercase							:				Optional[int]					=       parsed_torch_version_base >= version.parse('''2.0''')
__lowercase							:				Tuple					=       parsed_torch_version_base >= version.parse('''1.12''')
__lowercase							:				Union[str, Any]					=       parsed_torch_version_base >= version.parse('''1.11''')
__lowercase							:				str					=       parsed_torch_version_base < version.parse('''1.11''')




def lowercase_		(				_lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase     ,    self			)       ->							List[str]:
					'''simple docstring'''

					from torch import _softmax_backward_data

					if is_torch_less_than_1_11:
										return _softmax_backward_data(_lowercase     ,    _lowercase     ,    parent.dim     ,    self			)
					else:
										return _softmax_backward_data(_lowercase     ,    _lowercase     ,    parent.dim     ,    self.dtype			)




def lowercase_		(				_lowercase     ,    _lowercase     ,    _lowercase = 0			)       ->							nn.Linear:
					'''simple docstring'''

					lowerCamelCase_   : Tuple       =						index.to(layer.weight.device			)
					lowerCamelCase_   : List[Any]       =						layer.weight.index_select(_lowercase     ,    _lowercase			).clone().detach()
					if layer.bias is not None:
										if dim == 1:
															lowerCamelCase_   : Optional[int]       =						layer.bias.clone().detach()
										else:
															lowerCamelCase_   : List[str]       =						layer.bias[index].clone().detach()
					lowerCamelCase_   : Optional[Any]       =						list(layer.weight.size()			)
					lowerCamelCase_   : int       =						len(_lowercase			)
					lowerCamelCase_   : Union[str, Any]       =						nn.Linear(new_size[1]     ,    new_size[0]     ,    bias=layer.bias is not None			).to(layer.weight.device			)
					lowerCamelCase_   : Optional[int]       =						False
					new_layer.weight.copy_(W.contiguous()			)
					lowerCamelCase_   : Dict       =						True
					if layer.bias is not None:
										lowerCamelCase_   : List[str]       =						False
										new_layer.bias.copy_(b.contiguous()			)
										lowerCamelCase_   : Dict       =						True
					return new_layer

class 		__lowercase			(					nn.Module      ):

			def __init__(self     ,				A     ,				A ):
								super().__init__()
								lowerCamelCase_   : Dict       =						nf
								lowerCamelCase_   : Union[str, Any]       =						nn.Parameter(torch.empty(A     ,				A ) )
								lowerCamelCase_   : str       =						nn.Parameter(torch.zeros(A ) )
								nn.init.normal_(self.weight     ,				std=0.02 )






			def     UpperCAmelCase__	(self     ,				A ):
								lowerCamelCase_   : Tuple       =						x.size()[:-1] + (self.nf,)
								lowerCamelCase_   : str       =						torch.addmm(self.bias     ,				x.view(-1     ,				x.size(-1 ) )     ,				self.weight )
								lowerCamelCase_   : List[str]       =						x.view(A )
								return x




def lowercase_		(				_lowercase     ,    _lowercase     ,    _lowercase = 1			)       ->							ConvaD:
					'''simple docstring'''

					lowerCamelCase_   : str       =						index.to(layer.weight.device			)
					lowerCamelCase_   : str       =						layer.weight.index_select(_lowercase     ,    _lowercase			).clone().detach()
					if dim == 0:
										lowerCamelCase_   : Union[str, Any]       =						layer.bias.clone().detach()
					else:
										lowerCamelCase_   : List[str]       =						layer.bias[index].clone().detach()
					lowerCamelCase_   : str       =						list(layer.weight.size()			)
					lowerCamelCase_   : List[Any]       =						len(_lowercase			)
					lowerCamelCase_   : Tuple       =						ConvaD(new_size[1]     ,    new_size[0]			).to(layer.weight.device			)
					lowerCamelCase_   : Any       =						False
					new_layer.weight.copy_(W.contiguous()			)
					lowerCamelCase_   : str       =						True
					lowerCamelCase_   : Tuple       =						False
					new_layer.bias.copy_(b.contiguous()			)
					lowerCamelCase_   : List[str]       =						True
					return new_layer




def lowercase_		(				_lowercase     ,    _lowercase     ,    _lowercase = None			)       ->							Union[nn.Linear, ConvaD]:
					'''simple docstring'''

					if isinstance(_lowercase     ,    nn.Linear			):
										return prune_linear_layer(_lowercase     ,    _lowercase     ,    dim=0 if dim is None else dim			)
					elif isinstance(_lowercase     ,    _lowercase			):
										return prune_convad_layer(_lowercase     ,    _lowercase     ,    dim=1 if dim is None else dim			)
					else:
										raise ValueError(F"""Can't prune layer of class {layer.__class__}"""			)




def lowercase_		(				_lowercase     ,    _lowercase     ,    _lowercase     ,    *_lowercase			)       ->							torch.Tensor:
					'''simple docstring'''

					assert len(_lowercase			) > 0, F"""{input_tensors} has to be a tuple/list of tensors"""

					# inspect.signature exist since python 3.5 and is a python method -> no problem with backward compatibility
					lowerCamelCase_   : Any       =						len(inspect.signature(_lowercase			).parameters			)
					if num_args_in_forward_chunk_fn != len(_lowercase			):
										raise ValueError(
										    F"""forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(_lowercase			)} input """
										    '''tensors are given'''			)

					if chunk_size > 0:
										lowerCamelCase_   : Union[str, Any]       =						input_tensors[0].shape[chunk_dim]
										for input_tensor in input_tensors:
															if input_tensor.shape[chunk_dim] != tensor_shape:
																				raise ValueError(
																				    F"""All input tenors have to be of the same shape: {tensor_shape}, """
																				    F"""found shape {input_tensor.shape[chunk_dim]}"""			)

										if input_tensors[0].shape[chunk_dim] % chunk_size != 0:
															raise ValueError(
															    F"""The dimension to be chunked {input_tensors[0].shape[chunk_dim]} has to be a multiple of the chunk """
															    F"""size {chunk_size}"""			)

										lowerCamelCase_   : List[Any]       =						input_tensors[0].shape[chunk_dim] // chunk_size

										# chunk input tensor into tuples
										lowerCamelCase_   : Optional[Any]       =						tuple(input_tensor.chunk(_lowercase     ,    dim=_lowercase			) for input_tensor in input_tensors			)
										# apply forward fn to every tuple
										lowerCamelCase_   : Dict       =						tuple(forward_fn(*_lowercase			) for input_tensors_chunk in zip(*_lowercase			)			)
										# concatenate output at same dimension
										return torch.cat(_lowercase     ,    dim=_lowercase			)

					return forward_fn(*_lowercase			)




def lowercase_		(				_lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase			)       ->							Tuple[Set[int], torch.LongTensor]:
					'''simple docstring'''

					lowerCamelCase_   : str       =						torch.ones(_lowercase     ,    _lowercase			)
					lowerCamelCase_   : int       =						set(_lowercase			) - already_pruned_heads  # Convert to set and remove already pruned heads
					for head in heads:
										# Compute how many pruned heads are before the head and move the index accordingly
										lowerCamelCase_   : Dict       =						head - sum(1 if h < head else 0 for h in already_pruned_heads			)
										lowerCamelCase_   : List[Any]       =						0
					lowerCamelCase_   : List[Any]       =						mask.view(-1			).contiguous().eq(1			)
					lowerCamelCase_   : torch.LongTensor       =						torch.arange(len(_lowercase			)			)[mask].long()
					return heads, index




def lowercase_		(				*_lowercase     ,    _lowercase = None			)       ->							Tuple[torch.Tensor, ...]:
					'''simple docstring'''

					return torch.meshgrid(*_lowercase     ,    indexing=_lowercase			)






def lowercase_		(				_lowercase			)       ->							Tuple[torch.device, int, int]:
					'''simple docstring'''

					return tensor.device, storage_ptr(_lowercase			), storage_size(_lowercase			)




'''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 		__lowercase			:

			def __init__(self     ,				A     ,				A=1_3     ,				A=3_2     ,				A=3     ,				A=4     ,				A=[1_0, 2_0, 3_0, 4_0]     ,				A=[2, 2, 3, 2]     ,				A=True     ,				A=True     ,				A=3_7     ,				A="gelu"     ,				A=1_0     ,				A=0.02     ,				A=3     ,				A=None     ,				):
								lowerCamelCase_   : List[str]       =						parent
								lowerCamelCase_   : List[Any]       =						batch_size
								lowerCamelCase_   : List[str]       =						image_size
								lowerCamelCase_   : List[str]       =						num_channels
								lowerCamelCase_   : Union[str, Any]       =						num_stages
								lowerCamelCase_   : Optional[Any]       =						hidden_sizes
								lowerCamelCase_   : Optional[int]       =						depths
								lowerCamelCase_   : Optional[Any]       =						is_training
								lowerCamelCase_   : Dict       =						use_labels
								lowerCamelCase_   : Optional[int]       =						intermediate_size
								lowerCamelCase_   : Union[str, Any]       =						hidden_act
								lowerCamelCase_   : Tuple       =						type_sequence_label_size
								lowerCamelCase_   : Optional[Any]       =						initializer_range
								lowerCamelCase_   : str       =						scope

			def     UpperCAmelCase__	(self ):
								lowerCamelCase_   : Optional[int]       =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

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

								return config, pixel_values, labels

			def     UpperCAmelCase__	(self ):
								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=A     ,				initializer_range=self.initializer_range     ,				)

			def     UpperCAmelCase__	(self     ,				A     ,				A     ,				A ):
								lowerCamelCase_   : List[str]       =						TFConvNextModel(config=A )
								lowerCamelCase_   : Tuple       =						model(A     ,				training=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 // 3_2, self.image_size // 3_2)     ,				)

			def     UpperCAmelCase__	(self     ,				A     ,				A     ,				A ):
								lowerCamelCase_   : Union[str, Any]       =						self.type_sequence_label_size
								lowerCamelCase_   : Dict       =						TFConvNextForImageClassification(A )
								lowerCamelCase_   : Optional[Any]       =						model(A     ,				labels=A     ,				training=A )
								self.parent.assertEqual(result.logits.shape     ,				(self.batch_size, self.type_sequence_label_size) )






			def     UpperCAmelCase__	(self ):
								lowerCamelCase_   : str       =						self.prepare_config_and_inputs()
								lowerCamelCase_, lowerCamelCase_, lowerCamelCase_   : List[str]       =						config_and_inputs
								lowerCamelCase_   : Tuple       =						{'''pixel_values''': pixel_values}
								return config, inputs_dict

@require_tf
class 		__lowercase			(					_lowercase ,       _lowercase ,       unittest.TestCase      ):
			lowerCamelCase     :						Tuple		 =     (TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
			lowerCamelCase     :						List[str]		 =     (
			    {"feature-extraction": TFConvNextModel, "image-classification": TFConvNextForImageClassification}
			    if is_tf_available()
			    else {}
			)

			lowerCamelCase     :						List[Any]		 =     False
			lowerCamelCase     :						List[str]		 =     False
			lowerCamelCase     :						Optional[int]		 =     False
			lowerCamelCase     :						List[Any]		 =     False
			lowerCamelCase     :						List[Any]		 =     False

			def     UpperCAmelCase__	(self ):
								lowerCamelCase_   : List[str]       =						TFConvNextModelTester(self )
								lowerCamelCase_   : int       =						ConfigTester(
								    self     ,				config_class=A     ,				has_text_modality=A     ,				hidden_size=3_7     ,				)

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

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

			def     UpperCAmelCase__	(self ):
								lowerCamelCase_, lowerCamelCase_   : Tuple       =						self.model_tester.prepare_config_and_inputs_for_common()

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

													lowerCamelCase_   : str       =						['''pixel_values''']
													self.assertListEqual(arg_names[:1]     ,				A )

			def     UpperCAmelCase__	(self ):
								lowerCamelCase_   : List[Any]       =						self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*A )

			@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     UpperCAmelCase__	(self ):
								super().test_dataset_conversion()

			def     UpperCAmelCase__	(self ):
								def check_hidden_states_output(A     ,				A     ,				A ):
													lowerCamelCase_   : str       =						model_class(A )

													lowerCamelCase_   : Dict       =						model(**self._prepare_for_class(A     ,				A ) )
													lowerCamelCase_   : List[str]       =						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

								lowerCamelCase_, lowerCamelCase_   : Union[str, Any]       =						self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													lowerCamelCase_   : Optional[Any]       =						True
													check_hidden_states_output(A     ,				A     ,				A )

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

													check_hidden_states_output(A     ,				A     ,				A )

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

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

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

																		recursive_check(A     ,				A )

								for model_class in self.all_model_classes:
													lowerCamelCase_   : Optional[Any]       =						model_class(A )

													lowerCamelCase_   : Any       =						self._prepare_for_class(A     ,				A )
													lowerCamelCase_   : Any       =						self._prepare_for_class(A     ,				A )
													check_equivalence(A     ,				A     ,				A )

													lowerCamelCase_   : Optional[Any]       =						self._prepare_for_class(A     ,				A     ,				return_labels=A )
													lowerCamelCase_   : int       =						self._prepare_for_class(A     ,				A     ,				return_labels=A )
													check_equivalence(A     ,				A     ,				A )

													lowerCamelCase_   : Union[str, Any]       =						self._prepare_for_class(A     ,				A )
													lowerCamelCase_   : List[str]       =						self._prepare_for_class(A     ,				A )
													check_equivalence(A     ,				A     ,				A     ,				{'''output_hidden_states''': True} )

													lowerCamelCase_   : Optional[int]       =						self._prepare_for_class(A     ,				A     ,				return_labels=A )
													lowerCamelCase_   : List[str]       =						self._prepare_for_class(A     ,				A     ,				return_labels=A )
													check_equivalence(A     ,				A     ,				A     ,				{'''output_hidden_states''': True} )

			def     UpperCAmelCase__	(self ):
								lowerCamelCase_   : str       =						self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*A )






			@slow
			def     UpperCAmelCase__	(self ):
								lowerCamelCase_   : List[str]       =						TFConvNextModel.from_pretrained('''facebook/convnext-tiny-224''' )
								self.assertIsNotNone(A )




def lowercase_		(				)       ->							Optional[Any]:
					'''simple docstring'''

					lowerCamelCase_   : Optional[Any]       =						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
					return image






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

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






			@slow
			def     UpperCAmelCase__	(self ):
								lowerCamelCase_   : List[str]       =						TFConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224''' )

								lowerCamelCase_   : Tuple       =						self.default_image_processor
								lowerCamelCase_   : List[str]       =						prepare_img()
								lowerCamelCase_   : Optional[Any]       =						image_processor(images=A     ,				return_tensors='''tf''' )

								# forward pass
								lowerCamelCase_   : Optional[int]       =						model(**A )

								# verify the logits
								lowerCamelCase_   : List[str]       =						tf.TensorShape((1, 1_0_0_0) )
								self.assertEqual(outputs.logits.shape     ,				A )

								lowerCamelCase_   : Optional[int]       =						tf.constant([-0.02_60, -0.47_39, 0.19_11] )

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


