






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




 def __init__(					self		:							List[str]    ,					__lowercase		:							List[Any]    ,					__lowercase		:							List[str]=13    ,					__lowercase		:							Any=32    ,					__lowercase		:							Union[str, Any]=3    ,					__lowercase		:							Optional[int]=4    ,					__lowercase		:							Optional[Any]=[10, 20, 30, 40]    ,					__lowercase		:							Tuple=[2, 2, 3, 2]    ,					__lowercase		:							Optional[int]=True    ,					__lowercase		:							Optional[int]=True    ,					__lowercase		:							Union[str, Any]=37    ,					__lowercase		:							Optional[int]="gelu"    ,					__lowercase		:							Optional[Any]=10    ,					__lowercase		:							Tuple=0.02    ,					__lowercase		:							Any=["stage2", "stage3", "stage4"]    ,					__lowercase		:							Any=[2, 3, 4]    ,					__lowercase		:							Tuple=None    ,					)		->  Optional[Any]:
       SCREAMING_SNAKE_CASE__   : Optional[Any]													=parent
       SCREAMING_SNAKE_CASE__   : List[Any]													=batch_size
       SCREAMING_SNAKE_CASE__   : str													=image_size
       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=num_channels
       SCREAMING_SNAKE_CASE__   : List[Any]													=num_stages
       SCREAMING_SNAKE_CASE__   : Dict													=hidden_sizes
       SCREAMING_SNAKE_CASE__   : int													=depths
       SCREAMING_SNAKE_CASE__   : Tuple													=is_training
       SCREAMING_SNAKE_CASE__   : List[str]													=use_labels
       SCREAMING_SNAKE_CASE__   : Dict													=intermediate_size
       SCREAMING_SNAKE_CASE__   : int													=hidden_act
       SCREAMING_SNAKE_CASE__   : int													=num_labels
       SCREAMING_SNAKE_CASE__   : Any													=initializer_range
       SCREAMING_SNAKE_CASE__   : Tuple													=out_features
       SCREAMING_SNAKE_CASE__   : int													=out_indices
       SCREAMING_SNAKE_CASE__   : List[Any]													=scope




 def    __magic_name__		(					self		:							Dict					)		->  str:
       SCREAMING_SNAKE_CASE__   : List[Any]													=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

       SCREAMING_SNAKE_CASE__   : str													=self.get_config()

       return config, pixel_values, labels




 def    __magic_name__		(					self		:							Any					)		->  Dict:
       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=_UpperCAmelCase    ,					initializer_range=self.initializer_range    ,					out_features=self.out_features    ,					out_indices=self.out_indices    ,					num_labels=self.num_labels    ,					)




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




 def    __magic_name__		(					self		:							str    ,					__lowercase		:							List[Any]    ,					__lowercase		:							str    ,					__lowercase		:							Optional[Any]					)		->  int:
       SCREAMING_SNAKE_CASE__   : Optional[int]													=ConvNextVaBackbone(config=_UpperCAmelCase					)
       model.to(_UpperCAmelCase					)
       model.eval()
       SCREAMING_SNAKE_CASE__   : Dict													=model(_UpperCAmelCase					)

       # 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
       SCREAMING_SNAKE_CASE__   : Tuple													=None
       SCREAMING_SNAKE_CASE__   : List[Any]													=ConvNextVaBackbone(config=_UpperCAmelCase					)
       model.to(_UpperCAmelCase					)
       model.eval()
       SCREAMING_SNAKE_CASE__   : List[str]													=model(_UpperCAmelCase					)

       # 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    __magic_name__		(					self		:							Optional[Any]					)		->  Dict:
       SCREAMING_SNAKE_CASE__   : Any													=self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=config_and_inputs
       SCREAMING_SNAKE_CASE__   : Any													={'pixel_values': pixel_values}
       return config, inputs_dict




 def    __magic_name__		(					self		:							str					)		->  int:
       SCREAMING_SNAKE_CASE__   : Tuple													=self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE__   : Tuple													=config_and_inputs
       SCREAMING_SNAKE_CASE__   : List[Any]													={'pixel_values': pixel_values, 'labels': labels}
       return config, inputs_dict






@require_torch
class    __SCREAMING_SNAKE_CASE					(	_UpperCamelCase		, _UpperCamelCase		, 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    __magic_name__		(					self		:							List[Any]					)		->  Optional[Any]:
       SCREAMING_SNAKE_CASE__   : str													=ConvNextVaModelTester(self					)
       SCREAMING_SNAKE_CASE__   : Tuple													=ConfigTester(self    ,					config_class=_UpperCAmelCase    ,					has_text_modality=_UpperCAmelCase    ,					hidden_size=37					)




 def    __magic_name__		(					self		:							Optional[Any]					)		->  Optional[int]:
       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    __magic_name__		(					self		:							str					)		->  str:
       return




 @unittest.skip(reason='''ConvNextV2 does not use inputs_embeds'''					)
 def    __magic_name__		(					self		:							List[Any]					)		->  Optional[Any]:
       pass




 @unittest.skip(reason='''ConvNextV2 does not support input and output embeddings'''					)
 def    __magic_name__		(					self		:							Optional[int]					)		->  Union[str, Any]:
       pass




 @unittest.skip(reason='''ConvNextV2 does not use feedforward chunking'''					)
 def    __magic_name__		(					self		:							Any					)		->  Union[str, Any]:
       pass




 def    __magic_name__		(					self		:							List[str]					)		->  str:
       if not self.model_tester.is_training:
             return

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__   : List[Any]													=self.model_tester.prepare_config_and_inputs_with_labels()
             SCREAMING_SNAKE_CASE__   : Any													=True

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

             SCREAMING_SNAKE_CASE__   : Optional[Any]													=model_class(_UpperCAmelCase					)
             model.to(_UpperCAmelCase					)
             model.train()
             SCREAMING_SNAKE_CASE__   : str													=self._prepare_for_class(_UpperCAmelCase    ,					_UpperCAmelCase    ,					return_labels=_UpperCAmelCase					)
             SCREAMING_SNAKE_CASE__   : Optional[int]													=model(**_UpperCAmelCase					).loss
             loss.backward()




 def    __magic_name__		(					self		:							str					)		->  Tuple:
       if not self.model_tester.is_training:
             return

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__   : Optional[Any]													=self.model_tester.prepare_config_and_inputs_with_labels()
             SCREAMING_SNAKE_CASE__   : Optional[int]													=False
             SCREAMING_SNAKE_CASE__   : Tuple													=True

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

             SCREAMING_SNAKE_CASE__   : Tuple													=model_class(_UpperCAmelCase					)
             model.to(_UpperCAmelCase					)
             model.gradient_checkpointing_enable()
             model.train()
             SCREAMING_SNAKE_CASE__   : Any													=self._prepare_for_class(_UpperCAmelCase    ,					_UpperCAmelCase    ,					return_labels=_UpperCAmelCase					)
             SCREAMING_SNAKE_CASE__   : List[Any]													=model(**_UpperCAmelCase					).loss
             loss.backward()




 def    __magic_name__		(					self		:							List[Any]					)		->  int:
       SCREAMING_SNAKE_CASE__   : List[Any]													=self.model_tester.prepare_config_and_inputs_for_common()

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

             SCREAMING_SNAKE_CASE__   : int													=['pixel_values']
             self.assertListEqual(arg_names[:1]    ,					_UpperCAmelCase					)




 def    __magic_name__		(					self		:							int					)		->  Any:
       SCREAMING_SNAKE_CASE__   : Dict													=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_UpperCAmelCase					)




 def    __magic_name__		(					self		:							Any					)		->  Any:
       def check_hidden_states_output(__lowercase		:							List[Any]    ,					__lowercase		:							Tuple    ,					__lowercase		:							Dict					):
             SCREAMING_SNAKE_CASE__   : Union[str, Any]													=model_class(_UpperCAmelCase					)
             model.to(_UpperCAmelCase					)
             model.eval()

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

             SCREAMING_SNAKE_CASE__   : Any													=outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

             SCREAMING_SNAKE_CASE__   : str													=self.model_tester.num_stages
             self.assertEqual(len(_UpperCAmelCase					)    ,					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]    ,					)

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

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__   : int													=True
             check_hidden_states_output(_UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase					)

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

             check_hidden_states_output(_UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase					)




 def    __magic_name__		(					self		:							List[Any]					)		->  List[Any]:
       SCREAMING_SNAKE_CASE__   : Optional[Any]													=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase					)




 @slow
 def    __magic_name__		(					self		:							int					)		->  Union[str, Any]:
       for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE__   : Any													=ConvNextVaModel.from_pretrained(_UpperCAmelCase					)
             self.assertIsNotNone(_UpperCAmelCase					)





def        _a(     ):




      '''simple docstring'''






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







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




 @cached_property
 def    __magic_name__		(					self		:							Optional[Any]					)		->  int:
       return AutoImageProcessor.from_pretrained('''facebook/convnextv2-tiny-1k-224'''					) if is_vision_available() else None




 @slow
 def    __magic_name__		(					self		:							Any					)		->  Optional[int]:
       SCREAMING_SNAKE_CASE__   : List[str]													=ConvNextVaForImageClassification.from_pretrained('''facebook/convnextv2-tiny-1k-224'''					).to(_UpperCAmelCase					)

       SCREAMING_SNAKE_CASE__   : Optional[int]													=self.default_image_processor
       SCREAMING_SNAKE_CASE__   : str													=prepare_img()
       SCREAMING_SNAKE_CASE__   : str													=preprocessor(images=_UpperCAmelCase    ,					return_tensors='''pt'''					).to(_UpperCAmelCase					)

       # forward pass
       with torch.no_grad():
             SCREAMING_SNAKE_CASE__   : Dict													=model(**_UpperCAmelCase					)

       # verify the logits
       SCREAMING_SNAKE_CASE__   : Optional[Any]													=torch.Size((1, 10_00)					)
       self.assertEqual(outputs.logits.shape    ,					_UpperCAmelCase					)

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




'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__lowerCAmelCase									=   {
    '''configuration_lilt''': ['''LILT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LiltConfig'''],
}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__lowerCAmelCase									=   [
							    '''LILT_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''LiltForQuestionAnswering''',
							    '''LiltForSequenceClassification''',
							    '''LiltForTokenClassification''',
							    '''LiltModel''',
							    '''LiltPreTrainedModel''',
							]

if TYPE_CHECKING:
							from .configuration_lilt import LILT_PRETRAINED_CONFIG_ARCHIVE_MAP, LiltConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_lilt import (
														    LILT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    LiltForQuestionAnswering,
														    LiltForSequenceClassification,
														    LiltForTokenClassification,
														    LiltModel,
														    LiltPreTrainedModel,
														)

else:
							import sys

							__lowerCAmelCase									=   _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




