





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





       '''simple docstring'''







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




       def 							UpperCAmelCase__			(  self			:   Any		):
         __snake_case:						Dict			  =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

         __snake_case:						int			  =						None
         if self.use_labels:
           __snake_case:						List[str]			  =						ids_tensor([self.batch_size]							,    self.num_labels		)

         __snake_case:						Tuple			  =						self.get_config()

         return config, pixel_values, labels




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




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




       def 							UpperCAmelCase__			(  self			:   Dict							,    A			:   Tuple							,    A			:   Optional[int]							,    A			:   int		):
         __snake_case:						str			  =						ConvNextVaBackbone(config=lowercase_		)
         model.to(lowercase_		)
         model.eval()
         __snake_case:						Optional[int]			  =						model(lowercase_		)

         # 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
         __snake_case:						int			  =						None
         __snake_case:						str			  =						ConvNextVaBackbone(config=lowercase_		)
         model.to(lowercase_		)
         model.eval()
         __snake_case:						int			  =						model(lowercase_		)

         # 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 							UpperCAmelCase__			(  self			:   Optional[Any]		):
         __snake_case:						Tuple			  =						self.prepare_config_and_inputs()
         __snake_case:						Optional[Any]			  =						config_and_inputs
         __snake_case:						Optional[int]			  =						{"pixel_values": pixel_values}
         return config, inputs_dict






       def 							UpperCAmelCase__			(  self			:   Optional[int]		):
         __snake_case:						Tuple			  =						self.prepare_config_and_inputs()
         __snake_case:						List[Any]			  =						config_and_inputs
         __snake_case:						Optional[int]			  =						{"pixel_values": pixel_values, "labels": labels}
         return config, inputs_dict







@require_torch
class    __snake_case	(							lowercase__       ,      lowercase__       ,      unittest.TestCase      ):





       '''simple docstring'''







       lowerCAmelCase__									=					(
           (
               ConvNextVaModel,
               ConvNextVaForImageClassification,
               ConvNextVaBackbone,
           )
           if is_torch_available()
           else ()
       )
       lowerCAmelCase__									=					(
           {"""feature-extraction""": ConvNextVaModel, """image-classification""": ConvNextVaForImageClassification}
           if is_torch_available()
           else {}
       )

       lowerCAmelCase__									=					False
       lowerCAmelCase__									=					False
       lowerCAmelCase__									=					False
       lowerCAmelCase__									=					False
       lowerCAmelCase__									=					False




       def 							UpperCAmelCase__			(  self			:   int		):
         __snake_case:						Tuple			  =						ConvNextVaModelTester(self		)
         __snake_case:						Any			  =						ConfigTester(self							,    config_class=lowercase_							,    has_text_modality=lowercase_							,    hidden_size=37		)




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




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




       @unittest.skip(reason="""ConvNextV2 does not support input and output embeddings"""		)
       def 							UpperCAmelCase__			(  self			:   List[str]		):
         pass




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




       def 							UpperCAmelCase__			(  self			:   Dict		):
         if not self.model_tester.is_training:
           return

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

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

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




       def 							UpperCAmelCase__			(  self			:   Optional[Any]		):
         if not self.model_tester.is_training:
           return

         for model_class in self.all_model_classes:
           __snake_case:						str			  =						self.model_tester.prepare_config_and_inputs_with_labels()
           __snake_case:						Optional[int]			  =						False
           __snake_case:						Optional[int]			  =						True

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

           __snake_case:						Optional[Any]			  =						model_class(lowercase_		)
           model.to(lowercase_		)
           model.gradient_checkpointing_enable()
           model.train()
           __snake_case:						Optional[Any]			  =						self._prepare_for_class(lowercase_							,    lowercase_							,    return_labels=lowercase_		)
           __snake_case:						int			  =						model(**lowercase_		).loss
           loss.backward()




       def 							UpperCAmelCase__			(  self			:   List[str]		):
         __snake_case:						List[str]			  =						self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           __snake_case:						str			  =						model_class(lowercase_		)
           __snake_case:						List[str]			  =						inspect.signature(model.forward		)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           __snake_case:						List[str]			  =						[*signature.parameters.keys()]

           __snake_case:						Tuple			  =						["pixel_values"]
           self.assertListEqual(arg_names[:1]							,    lowercase_		)




       def 							UpperCAmelCase__			(  self			:   List[Any]		):
         __snake_case:						Optional[int]			  =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*lowercase_		)




       def 							UpperCAmelCase__			(  self			:   List[str]		):
         def check_hidden_states_output(A			:   Dict							,    A			:   int							,    A			:   Optional[int]		):
           __snake_case:						str			  =						model_class(lowercase_		)
           model.to(lowercase_		)
           model.eval()

           with torch.no_grad():
             __snake_case:						int			  =						model(**self._prepare_for_class(lowercase_							,    lowercase_		)		)

           __snake_case:						int			  =						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

         __snake_case:						Dict			  =						self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           __snake_case:						Tuple			  =						True
           check_hidden_states_output(lowercase_							,    lowercase_							,    lowercase_		)

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

           check_hidden_states_output(lowercase_							,    lowercase_							,    lowercase_		)




       def 							UpperCAmelCase__			(  self			:   Any		):
         __snake_case:						Union[str, Any]			  =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*lowercase_		)






       @slow
       def 							UpperCAmelCase__			(  self			:   int		):
         for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __snake_case:						Any			  =						ConvNextVaModel.from_pretrained(lowercase_		)
           self.assertIsNotNone(lowercase_		)







def       A__							(   )       ->   Union[str, Any]:
  __snake_case:						Dict			  =						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
  return image







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





       '''simple docstring'''







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






       @slow
       def 							UpperCAmelCase__			(  self			:   List[str]		):
         __snake_case:						Optional[int]			  =						ConvNextVaForImageClassification.from_pretrained("""facebook/convnextv2-tiny-1k-224"""		).to(lowercase_		)

         __snake_case:						Any			  =						self.default_image_processor
         __snake_case:						Dict			  =						prepare_img()
         __snake_case:						List[str]			  =						preprocessor(images=lowercase_							,    return_tensors="""pt"""		).to(lowercase_		)

         # forward pass
         with torch.no_grad():
           __snake_case:						Dict			  =						model(**lowercase_		)

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

         __snake_case:						Dict			  =						torch.tensor([0.9996, 0.1966, -0.4386]		).to(lowercase_		)
         self.assertTrue(torch.allclose(outputs.logits[0, :3]							,    lowercase_							,    atol=1E-4		)		)













from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCAmelCase	:       str    	=							logging.get_logger(__name__)

__UpperCAmelCase	:       int    	=							{
    "RWKV/rwkv-4-169m-pile": "https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json",
    "RWKV/rwkv-4-430m-pile": "https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json",
    "RWKV/rwkv-4-1b5-pile": "https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json",
    "RWKV/rwkv-4-3b-pile": "https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json",
    "RWKV/rwkv-4-7b-pile": "https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json",
    "RWKV/rwkv-4-14b-pile": "https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json",
    "RWKV/rwkv-raven-1b5": "https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json",
    "RWKV/rwkv-raven-3b": "https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json",
    "RWKV/rwkv-raven-7b": "https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json",
    "RWKV/rwkv-raven-14b": "https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json",
}







class    __snake_case	(							__lowerCamelCase      ):





       '''simple docstring'''







       lowerCAmelCase__						=					"""rwkv"""
       lowerCAmelCase__						=					{"""max_position_embeddings""": """context_length"""}




       def __init__(  self			:   Dict							,    A			:   List[Any]=50_277							,    A			:   List[Any]=1_024							,    A			:   Union[str, Any]=4_096							,    A			:   Tuple=32							,    A			:   List[Any]=None							,    A			:   Tuple=None							,    A			:   Tuple=1E-5							,    A			:   int=0							,    A			:   Optional[int]=0							,    A			:   Dict=6							,    A			:   Dict=False							,    A			:   int=True							,    **A			:   List[Any]							,    ):
         __snake_case:						Tuple			  =						vocab_size
         __snake_case:						Any			  =						context_length
         __snake_case:						Dict			  =						hidden_size
         __snake_case:						Dict			  =						num_hidden_layers
         __snake_case:						Union[str, Any]			  =						attention_hidden_size if attention_hidden_size is not None else hidden_size
         __snake_case:						str			  =						intermediate_size if intermediate_size is not None else 4 * hidden_size
         __snake_case:						Any			  =						layer_norm_epsilon
         __snake_case:						int			  =						rescale_every
         __snake_case:						str			  =						use_cache

         __snake_case:						Dict			  =						bos_token_id
         __snake_case:						Union[str, Any]			  =						eos_token_id

         super().__init__(
             tie_word_embeddings=A							,    bos_token_id=A							,    eos_token_id=A							,    **A		)






