





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



	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		,   ):
			lowerCAmelCase  :     int       						= parent
			lowerCAmelCase  :     List[str]       						= batch_size
			lowerCAmelCase  :     Dict       						= image_size
			lowerCAmelCase  :     str       						= num_channels
			lowerCAmelCase  :     Tuple       						= num_stages
			lowerCAmelCase  :     int       						= hidden_sizes
			lowerCAmelCase  :     Optional[int]       						= depths
			lowerCAmelCase  :     List[Any]       						= is_training
			lowerCAmelCase  :     List[Any]       						= use_labels
			lowerCAmelCase  :     Optional[int]       						= intermediate_size
			lowerCAmelCase  :     Any       						= hidden_act
			lowerCAmelCase  :     List[Any]       						= num_labels
			lowerCAmelCase  :     List[Any]       						= initializer_range
			lowerCAmelCase  :     List[Any]       						= out_features
			lowerCAmelCase  :     List[str]       						= out_indices
			lowerCAmelCase  :     List[str]       						= scope



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     List[Any]       						= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

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

			return config, pixel_values, labels



	def 	_lowerCamelCase     ( self						):
			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 	_lowerCamelCase     ( self		,   a_		,   a_		,   a_						):
			lowerCAmelCase  :     List[str]       						= ConvNextVaModel(config=a_						)
			model.to(a_						)
			model.eval()
			lowerCAmelCase  :     Optional[int]       						= 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 	_lowerCamelCase     ( self		,   a_		,   a_		,   a_						):
			lowerCAmelCase  :     Union[str, Any]       						= ConvNextVaForImageClassification(a_						)
			model.to(a_						)
			model.eval()
			lowerCAmelCase  :     Optional[Any]       						= model(a_		,   labels=a_						)
			self.parent.assertEqual(result.logits.shape		,   (self.batch_size, self.num_labels)						)



	def 	_lowerCamelCase     ( self		,   a_		,   a_		,   a_						):
			lowerCAmelCase  :     List[str]       						= ConvNextVaBackbone(config=a_						)
			model.to(a_						)
			model.eval()
			lowerCAmelCase  :     Optional[Any]       						= 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
			lowerCAmelCase  :     List[str]       						= None
			lowerCAmelCase  :     Optional[Any]       						= ConvNextVaBackbone(config=a_						)
			model.to(a_						)
			model.eval()
			lowerCAmelCase  :     Optional[int]       						= 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 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Optional[int]       						= self.prepare_config_and_inputs()
			lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase  :     Any       						= config_and_inputs
			lowerCAmelCase  :     List[str]       						= {"pixel_values": pixel_values}
			return config, inputs_dict





	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Any       						= self.prepare_config_and_inputs()
			lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase  :     List[str]       						= config_and_inputs
			lowerCAmelCase  :     List[Any]       						= {"pixel_values": pixel_values, "labels": labels}
			return config, inputs_dict






@require_torch
class 		lowerCamelCase	(   _A   , _A   , unittest.TestCase							):
	snake_case_   = (
	    (
	        ConvNextVaModel,
	        ConvNextVaForImageClassification,
	        ConvNextVaBackbone,
	    )
	    if is_torch_available()
	    else ()
	)
	snake_case_   = (
	    {"feature-extraction": ConvNextVaModel, "image-classification": ConvNextVaForImageClassification}
	    if is_torch_available()
	    else {}
	)

	snake_case_   = False
	snake_case_   = False
	snake_case_   = False
	snake_case_   = False
	snake_case_   = False



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     List[str]       						= ConvNextVaModelTester(self						)
			lowerCAmelCase  :     str       						= ConfigTester(self		,   config_class=a_		,   has_text_modality=a_		,   hidden_size=37						)



	def 	_lowerCamelCase     ( self						):
			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 	_lowerCamelCase     ( self						):
			return



	@unittest.skip(reason="ConvNextV2 does not use inputs_embeds"						)
	def 	_lowerCamelCase     ( self						):
			pass



	@unittest.skip(reason="ConvNextV2 does not support input and output embeddings"						)
	def 	_lowerCamelCase     ( self						):
			pass



	@unittest.skip(reason="ConvNextV2 does not use feedforward chunking"						)
	def 	_lowerCamelCase     ( self						):
			pass



	def 	_lowerCamelCase     ( self						):
			if not self.model_tester.is_training:
					return

			for model_class in self.all_model_classes:
					lowerCAmelCase						,		lowerCAmelCase  :     List[str]       						= self.model_tester.prepare_config_and_inputs_with_labels()
					lowerCAmelCase  :     Optional[Any]       						= True

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

					lowerCAmelCase  :     Optional[int]       						= model_class(a_						)
					model.to(a_						)
					model.train()
					lowerCAmelCase  :     List[Any]       						= self._prepare_for_class(a_		,   a_		,   return_labels=a_						)
					lowerCAmelCase  :     List[Any]       						= model(**a_						).loss
					loss.backward()



	def 	_lowerCamelCase     ( self						):
			if not self.model_tester.is_training:
					return

			for model_class in self.all_model_classes:
					lowerCAmelCase						,		lowerCAmelCase  :     List[str]       						= self.model_tester.prepare_config_and_inputs_with_labels()
					lowerCAmelCase  :     Optional[Any]       						= False
					lowerCAmelCase  :     Optional[int]       						= True

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

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



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase						,		lowerCAmelCase  :     Tuple       						= self.model_tester.prepare_config_and_inputs_for_common()

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

					lowerCAmelCase  :     Tuple       						= ["pixel_values"]
					self.assertListEqual(arg_names[:1]		,   a_						)



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Union[str, Any]       						= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*a_						)



	def 	_lowerCamelCase     ( self						):
			def check_hidden_states_output(a_		,   a_		,   a_						):
					lowerCAmelCase  :     Dict       						= model_class(a_						)
					model.to(a_						)
					model.eval()

					with torch.no_grad():
							lowerCAmelCase  :     List[Any]       						= model(**self._prepare_for_class(a_		,   a_						)						)

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

					lowerCAmelCase  :     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]		,   )

			lowerCAmelCase						,		lowerCAmelCase  :     List[str]       						= self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					lowerCAmelCase  :     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  :     int       						= True

					check_hidden_states_output(a_		,   a_		,   a_						)



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     List[Any]       						= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*a_						)





	@slow
	def 	_lowerCamelCase     ( self						):
			for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCAmelCase  :     str       						= ConvNextVaModel.from_pretrained(a_						)
					self.assertIsNotNone(a_						)






def   __A    (				):
		lowerCAmelCase  :     Optional[Any]       						= Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
		return image

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



	@cached_property
	def 	_lowerCamelCase     ( self						):
			return AutoImageProcessor.from_pretrained("facebook/convnextv2-tiny-1k-224"						) if is_vision_available() else None





	@slow
	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Optional[int]       						= ConvNextVaForImageClassification.from_pretrained("facebook/convnextv2-tiny-1k-224"						).to(a_						)

			lowerCAmelCase  :     Optional[Any]       						= self.default_image_processor
			lowerCAmelCase  :     Dict       						= prepare_img()
			lowerCAmelCase  :     Tuple       						= preprocessor(images=a_		,   return_tensors="pt"						).to(a_						)

			# forward pass
			with torch.no_grad():
					lowerCAmelCase  :     Dict       						= model(**a_						)

			# verify the logits
			lowerCAmelCase  :     Optional[int]       						= torch.Size((1, 1_000)						)
			self.assertEqual(outputs.logits.shape		,   a_						)

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










'''simple docstring'''






import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType
class 		lowerCamelCase	(   _A							):
	snake_case_   = ["image_processor", "tokenizer"]
	snake_case_   = "LayoutLMv3ImageProcessor"
	snake_case_   = ("LayoutLMv3Tokenizer", "LayoutLMv3TokenizerFast")



	def __init__( self		,   a_=None		,   a_=None		,   **a_						):
			lowerCAmelCase  :     Union[str, Any]       						= None
			if "feature_extractor" in kwargs:
					warnings.warn(
					    "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
					    " instead."		,   a_		,   )
					lowerCAmelCase  :     Tuple       						= kwargs.pop("feature_extractor"						)

			lowerCAmelCase  :     str       						= image_processor if image_processor is not None else feature_extractor
			if image_processor is None:
					raise ValueError("You need to specify an `image_processor`."						)
			if tokenizer is None:
					raise ValueError("You need to specify a `tokenizer`."						)

			super().__init__(a_		,   a_						)



	def __call__( self		,   a_		,   a_ = None		,   a_ = None		,   a_ = None		,   a_ = None		,   a_ = True		,   a_ = False		,   a_ = None		,   a_ = None		,   a_ = 0		,   a_ = None		,   a_ = None		,   a_ = None		,   a_ = False		,   a_ = False		,   a_ = False		,   a_ = False		,   a_ = True		,   a_ = None		,   **a_		,   ):
			# verify input
			if self.image_processor.apply_ocr and (boxes is not None):
					raise ValueError(
					    "You cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True."						)

			if self.image_processor.apply_ocr and (word_labels is not None):
					raise ValueError(
					    "You cannot provide word labels if you initialized the image processor with apply_ocr set to True."						)

			# first, apply the image processor
			lowerCAmelCase  :     str       						= self.image_processor(images=a_		,   return_tensors=a_						)

			# second, apply the tokenizer
			if text is not None and self.image_processor.apply_ocr and text_pair is None:
					if isinstance(a_		,   a_						):
							lowerCAmelCase  :     Union[str, Any]       						= [text]  # add batch dimension (as the image processor always adds a batch dimension)
					lowerCAmelCase  :     int       						= features["words"]

			lowerCAmelCase  :     List[Any]       						= self.tokenizer(
			    text=text if text is not None else features["words"]		,   text_pair=text_pair if text_pair is not None else None		,   boxes=boxes if boxes is not None else features["boxes"]		,   word_labels=a_		,   add_special_tokens=a_		,   padding=a_		,   truncation=a_		,   max_length=a_		,   stride=a_		,   pad_to_multiple_of=a_		,   return_token_type_ids=a_		,   return_attention_mask=a_		,   return_overflowing_tokens=a_		,   return_special_tokens_mask=a_		,   return_offsets_mapping=a_		,   return_length=a_		,   verbose=a_		,   return_tensors=a_		,   **a_		,   )

			# add pixel values
			lowerCAmelCase  :     Tuple       						= features.pop("pixel_values"						)
			if return_overflowing_tokens is True:
					lowerCAmelCase  :     Optional[int]       						= self.get_overflowing_images(a_		,   encoded_inputs["overflow_to_sample_mapping"]						)
			lowerCAmelCase  :     str       						= images

			return encoded_inputs



	def 	_lowerCamelCase     ( self		,   a_		,   a_						):
			# in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
			lowerCAmelCase  :     Optional[Any]       						= []
			for sample_idx in overflow_to_sample_mapping:
					images_with_overflow.append(images[sample_idx]						)

			if len(a_						) != len(a_						):
					raise ValueError(
					    "Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got"
					    F''' {len(a_						)} and {len(a_						)}'''						)

			return images_with_overflow



	def 	_lowerCamelCase     ( self		,   *a_		,   **a_						):
			return self.tokenizer.batch_decode(*a_		,   **a_						)



	def 	_lowerCamelCase     ( self		,   *a_		,   **a_						):
			return self.tokenizer.decode(*a_		,   **a_						)



	@property
	def 	_lowerCamelCase     ( self						):
			return ["input_ids", "bbox", "attention_mask", "pixel_values"]



	@property
	def 	_lowerCamelCase     ( self						):
			warnings.warn(
			    "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."		,   a_		,   )
			return self.image_processor_class





	@property
	def 	_lowerCamelCase     ( self						):
			warnings.warn(
			    "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."		,   a_		,   )
			return self.image_processor



