


'''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 _snake_case   :
							"""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=3			,				__A=None			,				)     ->Dict:



											"""simple docstring"""







											__lowerCamelCase		:      Optional[Any]											=					parent
											__lowerCamelCase		:      str											=					batch_size
											__lowerCamelCase		:      Union[str, Any]											=					image_size
											__lowerCamelCase		:      Optional[int]											=					num_channels
											__lowerCamelCase		:      Any											=					num_stages
											__lowerCamelCase		:      List[Any]											=					hidden_sizes
											__lowerCamelCase		:      Union[str, Any]											=					depths
											__lowerCamelCase		:      List[str]											=					is_training
											__lowerCamelCase		:      Dict											=					use_labels
											__lowerCamelCase		:      List[str]											=					intermediate_size
											__lowerCamelCase		:      int											=					hidden_act
											__lowerCamelCase		:      List[Any]											=					type_sequence_label_size
											__lowerCamelCase		:      List[str]											=					initializer_range
											__lowerCamelCase		:      List[Any]											=					scope







							def 		__SCREAMING_SNAKE_CASE(   self	)     ->List[str]:



											"""simple docstring"""







											__lowerCamelCase		:      str											=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

											__lowerCamelCase		:      Union[str, Any]											=					None
											if self.use_labels:
															__lowerCamelCase		:      int											=					ids_tensor([self.batch_size]			,				self.type_sequence_label_size	)

											__lowerCamelCase		:      Optional[Any]											=					self.get_config()

											return config, pixel_values, labels







							def 		__SCREAMING_SNAKE_CASE(   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=__A			,				initializer_range=self.initializer_range			,				)







							def 		__SCREAMING_SNAKE_CASE(   self			,				__A			,				__A			,				__A	)     ->Dict:



											"""simple docstring"""







											__lowerCamelCase		:      Dict											=					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 // 32, self.image_size // 32)			,				)







							def 		__SCREAMING_SNAKE_CASE(   self			,				__A			,				__A			,				__A	)     ->Tuple:



											"""simple docstring"""







											__lowerCamelCase		:      Tuple											=					self.type_sequence_label_size
											__lowerCamelCase		:      Tuple											=					TFConvNextForImageClassification(__A	)
											__lowerCamelCase		:      Dict											=					model(__A			,				labels=__A			,				training=__A	)
											self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.type_sequence_label_size)	)







							def 		__SCREAMING_SNAKE_CASE(   self	)     ->str:



											"""simple docstring"""







											__lowerCamelCase		:      Tuple											=					self.prepare_config_and_inputs()
											__lowerCamelCase						,					__lowerCamelCase						,					__lowerCamelCase		:      List[Any]											=					config_and_inputs
											__lowerCamelCase		:      Union[str, Any]											=					{"pixel_values": pixel_values}
											return config, inputs_dict





@require_tf
class _snake_case   ( a__    ,   a__    ,   unittest.TestCase   ):
							"""simple docstring"""





							__a												=     (TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
							__a												=     (
							    {'''feature-extraction''': TFConvNextModel, '''image-classification''': TFConvNextForImageClassification}
							    if is_tf_available()
							    else {}
							)

							__a												=     False
							__a												=     False
							__a												=     False
							__a												=     False
							__a												=     False







							def 		__SCREAMING_SNAKE_CASE(   self	)     ->Dict:



											"""simple docstring"""







											__lowerCamelCase		:      Dict											=					TFConvNextModelTester(self	)
											__lowerCamelCase		:      str											=					ConfigTester(
											    self			,				config_class=__A			,				has_text_modality=__A			,				hidden_size=37			,				)







							@unittest.skip(reason="ConvNext does not use inputs_embeds"	)
							def 		__SCREAMING_SNAKE_CASE(   self	)     ->Dict:



											"""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 		__SCREAMING_SNAKE_CASE(   self	)     ->Any:



											"""simple docstring"""







											super().test_keras_fit()







							@unittest.skip(reason="ConvNext does not support input and output embeddings"	)
							def 		__SCREAMING_SNAKE_CASE(   self	)     ->Tuple:



											"""simple docstring"""







											pass







							def 		__SCREAMING_SNAKE_CASE(   self	)     ->List[Any]:



											"""simple docstring"""







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

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

															__lowerCamelCase		:      Union[str, Any]											=					["pixel_values"]
															self.assertListEqual(arg_names[:1]			,				__A	)







							def 		__SCREAMING_SNAKE_CASE(   self	)     ->List[str]:



											"""simple docstring"""







											__lowerCamelCase		:      int											=					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 		__SCREAMING_SNAKE_CASE(   self	)     ->str:



											"""simple docstring"""







											super().test_dataset_conversion()







							def 		__SCREAMING_SNAKE_CASE(   self	)     ->str:



											"""simple docstring"""







											def check_hidden_states_output(__A			,				__A			,				__A	):
															__lowerCamelCase		:      Tuple											=					model_class(__A	)

															__lowerCamelCase		:      str											=					model(**self._prepare_for_class(__A			,				__A	)	)
															__lowerCamelCase		:      Any											=					outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

															__lowerCamelCase		:      Dict											=					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		:      str											=					self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															__lowerCamelCase		:      int											=					True
															check_hidden_states_output(__A			,				__A			,				__A	)

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

															check_hidden_states_output(__A			,				__A			,				__A	)







							def 		__SCREAMING_SNAKE_CASE(   self	)     ->str:



											"""simple docstring"""







											__lowerCamelCase						,					__lowerCamelCase		:      str											=					self.model_tester.prepare_config_and_inputs_for_common()

											def check_equivalence(__A			,				__A			,				__A			,				__A={}	):
															__lowerCamelCase		:      str											=					model(__A			,				return_dict=__A			,				**__A	)
															__lowerCamelCase		:      Any											=					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		:      Optional[int]											=					self._prepare_for_class(__A			,				__A	)
															__lowerCamelCase		:      Union[str, Any]											=					self._prepare_for_class(__A			,				__A	)
															check_equivalence(__A			,				__A			,				__A	)

															__lowerCamelCase		:      Union[str, Any]											=					self._prepare_for_class(__A			,				__A			,				return_labels=__A	)
															__lowerCamelCase		:      str											=					self._prepare_for_class(__A			,				__A			,				return_labels=__A	)
															check_equivalence(__A			,				__A			,				__A	)

															__lowerCamelCase		:      int											=					self._prepare_for_class(__A			,				__A	)
															__lowerCamelCase		:      Dict											=					self._prepare_for_class(__A			,				__A	)
															check_equivalence(__A			,				__A			,				__A			,				{"output_hidden_states": True}	)

															__lowerCamelCase		:      Dict											=					self._prepare_for_class(__A			,				__A			,				return_labels=__A	)
															__lowerCamelCase		:      Tuple											=					self._prepare_for_class(__A			,				__A			,				return_labels=__A	)
															check_equivalence(__A			,				__A			,				__A			,				{"output_hidden_states": True}	)







							def 		__SCREAMING_SNAKE_CASE(   self	)     ->Any:



											"""simple docstring"""







											__lowerCamelCase		:      Dict											=					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__A	)







							@slow
							def 		__SCREAMING_SNAKE_CASE(   self	)     ->int:



											"""simple docstring"""







											__lowerCamelCase		:      List[Any]											=					TFConvNextModel.from_pretrained("facebook/convnext-tiny-224"	)
											self.assertIsNotNone(__A	)





def 							a_       (						)			->str:






				'''simple docstring'''

				__lowerCamelCase		:      List[Any]											=					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
				return image





@require_tf
@require_vision
class _snake_case   ( unittest.TestCase   ):
							"""simple docstring"""





							@cached_property
							def 		__SCREAMING_SNAKE_CASE(   self	)     ->Optional[int]:



											"""simple docstring"""







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







							@slow
							def 		__SCREAMING_SNAKE_CASE(   self	)     ->Optional[int]:



											"""simple docstring"""







											__lowerCamelCase		:      Tuple											=					TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"	)

											__lowerCamelCase		:      str											=					self.default_image_processor
											__lowerCamelCase		:      List[str]											=					prepare_img()
											__lowerCamelCase		:      Dict											=					image_processor(images=__A			,				return_tensors="tf"	)

											# forward pass
											__lowerCamelCase		:      Any											=					model(**__A	)

											# verify the logits
											__lowerCamelCase		:      Tuple											=					tf.TensorShape((1, 1000)	)
											self.assertEqual(outputs.logits.shape			,				__A	)

											__lowerCamelCase		:      Dict											=					tf.constant([-0.0260, -0.4739, 0.1911]	)

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









'''simple docstring'''




import json
from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_longformer import LongformerTokenizer


__lowercase  :      str      		=				logging.get_logger(__name__)

__lowercase  :      Any      		=				{"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

__lowercase  :      Optional[Any]      		=				{
    """vocab_file""": {
        """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json""",
        """allenai/longformer-large-4096""": (
            """https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json"""
        ),
        """allenai/longformer-large-4096-finetuned-triviaqa""": (
            """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json"""
        ),
        """allenai/longformer-base-4096-extra.pos.embd.only""": (
            """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json"""
        ),
        """allenai/longformer-large-4096-extra.pos.embd.only""": (
            """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json"""
        ),
    },
    """merges_file""": {
        """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt""",
        """allenai/longformer-large-4096""": (
            """https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt"""
        ),
        """allenai/longformer-large-4096-finetuned-triviaqa""": (
            """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt"""
        ),
        """allenai/longformer-base-4096-extra.pos.embd.only""": (
            """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt"""
        ),
        """allenai/longformer-large-4096-extra.pos.embd.only""": (
            """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt"""
        ),
    },
    """tokenizer_file""": {
        """allenai/longformer-base-4096""": (
            """https://huggingface.co/allenai/longformer-base-4096/resolve/main/tokenizer.json"""
        ),
        """allenai/longformer-large-4096""": (
            """https://huggingface.co/allenai/longformer-large-4096/resolve/main/tokenizer.json"""
        ),
        """allenai/longformer-large-4096-finetuned-triviaqa""": (
            """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/tokenizer.json"""
        ),
        """allenai/longformer-base-4096-extra.pos.embd.only""": (
            """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/tokenizer.json"""
        ),
        """allenai/longformer-large-4096-extra.pos.embd.only""": (
            """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/tokenizer.json"""
        ),
    },
}

__lowercase  :      Tuple      		=				{
    """allenai/longformer-base-4096""": 40_96,
    """allenai/longformer-large-4096""": 40_96,
    """allenai/longformer-large-4096-finetuned-triviaqa""": 40_96,
    """allenai/longformer-base-4096-extra.pos.embd.only""": 40_96,
    """allenai/longformer-large-4096-extra.pos.embd.only""": 40_96,
}





class _snake_case   ( a__   ):
							"""simple docstring"""





							__a												=     VOCAB_FILES_NAMES
							__a												=     PRETRAINED_VOCAB_FILES_MAP
							__a												=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							__a												=     ['''input_ids''', '''attention_mask''']
							__a												=     LongformerTokenizer







							def __init__(   self			,				__A=None			,				__A=None			,				__A=None			,				__A="replace"			,				__A="<s>"			,				__A="</s>"			,				__A="</s>"			,				__A="<s>"			,				__A="<unk>"			,				__A="<pad>"			,				__A="<mask>"			,				__A=False			,				__A=True			,				**__A			,				)     ->Dict:



											"""simple docstring"""







											super().__init__(
											    __A			,				__A			,				tokenizer_file=__A			,				errors=__A			,				bos_token=__A			,				eos_token=__A			,				sep_token=__A			,				cls_token=__A			,				unk_token=__A			,				pad_token=__A			,				mask_token=__A			,				add_prefix_space=__A			,				trim_offsets=__A			,				**__A			,				)

											__lowerCamelCase		:      int											=					json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()	)
											if pre_tok_state.get("add_prefix_space"			,				__A	) != add_prefix_space:
															__lowerCamelCase		:      Any											=					getattr(__A			,				pre_tok_state.pop("type"	)	)
															__lowerCamelCase		:      Any											=					add_prefix_space
															__lowerCamelCase		:      Union[str, Any]											=					pre_tok_class(**__A	)

											__lowerCamelCase		:      Any											=					add_prefix_space

											__lowerCamelCase		:      int											=					"post_processor"
											__lowerCamelCase		:      Optional[int]											=					getattr(self.backend_tokenizer			,				__A			,				__A	)
											if tokenizer_component_instance:
															__lowerCamelCase		:      Tuple											=					json.loads(tokenizer_component_instance.__getstate__()	)

															# The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
															if "sep" in state:
																			__lowerCamelCase		:      Dict											=					tuple(state["sep"]	)
															if "cls" in state:
																			__lowerCamelCase		:      Union[str, Any]											=					tuple(state["cls"]	)

															__lowerCamelCase		:      Union[str, Any]											=					False

															if state.get("add_prefix_space"			,				__A	) != add_prefix_space:
																			__lowerCamelCase		:      Dict											=					add_prefix_space
																			__lowerCamelCase		:      str											=					True

															if state.get("trim_offsets"			,				__A	) != trim_offsets:
																			__lowerCamelCase		:      Tuple											=					trim_offsets
																			__lowerCamelCase		:      Optional[int]											=					True

															if changes_to_apply:
																			__lowerCamelCase		:      List[Any]											=					getattr(__A			,				state.pop("type"	)	)
																			__lowerCamelCase		:      int											=					component_class(**__A	)
																			setattr(self.backend_tokenizer			,				__A			,				__A	)







							@property
							def 		__SCREAMING_SNAKE_CASE(   self	)     ->str:



											"""simple docstring"""







											if self._mask_token is None:
															if self.verbose:
																			logger.error("Using mask_token, but it is not set yet."	)
															return None
											return str(self._mask_token	)







							@mask_token.setter
							def 		__SCREAMING_SNAKE_CASE(   self			,				__A	)     ->int:



											"""simple docstring"""







											__lowerCamelCase		:      List[Any]											=					AddedToken(__A			,				lstrip=__A			,				rstrip=__A	) if isinstance(__A			,				__A	) else value
											__lowerCamelCase		:      Optional[int]											=					value







							def 		__SCREAMING_SNAKE_CASE(   self			,				*__A			,				**__A	)     ->BatchEncoding:



											"""simple docstring"""







											__lowerCamelCase		:      Optional[int]											=					kwargs.get("is_split_into_words"			,				__A	)
											assert self.add_prefix_space or not is_split_into_words, (
											    f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
											    "to use it with pretokenized inputs."
											)

											return super()._batch_encode_plus(*__A			,				**__A	)







							def 		__SCREAMING_SNAKE_CASE(   self			,				*__A			,				**__A	)     ->BatchEncoding:



											"""simple docstring"""







											__lowerCamelCase		:      str											=					kwargs.get("is_split_into_words"			,				__A	)

											assert self.add_prefix_space or not is_split_into_words, (
											    f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
											    "to use it with pretokenized inputs."
											)

											return super()._encode_plus(*__A			,				**__A	)







							def 		__SCREAMING_SNAKE_CASE(   self			,				__A			,				__A = None	)     ->Tuple[str]:



											"""simple docstring"""







											__lowerCamelCase		:      List[str]											=					self._tokenizer.model.save(__A			,				name=__A	)
											return tuple(__A	)







							def 		__SCREAMING_SNAKE_CASE(   self			,				__A			,				__A=None	)     ->Union[str, Any]:



											"""simple docstring"""







											__lowerCamelCase		:      Any											=					[self.bos_token_id] + token_ids_a + [self.eos_token_id]
											if token_ids_a is None:
															return output

											return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]







							def 		__SCREAMING_SNAKE_CASE(   self			,				__A			,				__A = None	)     ->List[int]:



											"""simple docstring"""







											__lowerCamelCase		:      List[str]											=					[self.sep_token_id]
											__lowerCamelCase		:      Tuple											=					[self.cls_token_id]

											if token_ids_a is None:
															return len(cls + token_ids_a + sep	) * [0]
											return len(cls + token_ids_a + sep + sep + token_ids_a + sep	) * [0]





