


import inspect
import unittest

from transformers import RegNetConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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 torch import nn

       from transformers import RegNetForImageClassification, RegNetModel
       from transformers.models.regnet.modeling_regnet import REGNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor





class  lowerCAmelCase__:






    '''simple docstring'''





    def __init__(	self				:   Tuple       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Optional[int]=3       ,   __snake_case				:   Tuple=32       ,   __snake_case				:   Optional[Any]=3       ,   __snake_case				:   List[Any]=10       ,   __snake_case				:   List[str]=[10, 20, 30, 40]       ,   __snake_case				:   Tuple=[1, 1, 2, 1]       ,   __snake_case				:   List[Any]=True       ,   __snake_case				:   List[str]=True       ,   __snake_case				:   Tuple="relu"       ,   __snake_case				:   Optional[Any]=3       ,   __snake_case				:   Optional[Any]=None       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      int						=   parent
       UpperCAmelCase_							:      List[str]						=   batch_size
       UpperCAmelCase_							:      List[Any]						=   image_size
       UpperCAmelCase_							:      Dict						=   num_channels
       UpperCAmelCase_							:      int						=   embeddings_size
       UpperCAmelCase_							:      Optional[Any]						=   hidden_sizes
       UpperCAmelCase_							:      Union[str, Any]						=   depths
       UpperCAmelCase_							:      Dict						=   is_training
       UpperCAmelCase_							:      Union[str, Any]						=   use_labels
       UpperCAmelCase_							:      int						=   hidden_act
       UpperCAmelCase_							:      Optional[Any]						=   num_labels
       UpperCAmelCase_							:      List[Any]						=   scope
       UpperCAmelCase_							:      Dict						=   len(__snake_case	)





    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       UpperCAmelCase_							:      str						=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

       UpperCAmelCase_							:      int						=   self.get_config()

       return config, pixel_values, labels





    def 					_lowerCamelCase (	self				:   Union[str, Any]	):





       '''simple docstring'''
       return RegNetConfig(
           num_channels=self.num_channels       ,   embeddings_size=self.embeddings_size       ,   hidden_sizes=self.hidden_sizes       ,   depths=self.depths       ,   hidden_act=self.hidden_act       ,   num_labels=self.num_labels       ,   )





    def 					_lowerCamelCase (	self				:   Optional[Any]       ,   __snake_case				:   List[Any]       ,   __snake_case				:   List[str]       ,   __snake_case				:   Any	):





       '''simple docstring'''
       UpperCAmelCase_							:      Dict						=   RegNetModel(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      int						=   model(__snake_case	)
       # 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				:   Dict       ,   __snake_case				:   Dict       ,   __snake_case				:   List[str]       ,   __snake_case				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[str]						=   self.num_labels
       UpperCAmelCase_							:      Optional[int]						=   RegNetForImageClassification(__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      int						=   model(__snake_case       ,   labels=__snake_case	)
       self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.num_labels)	)





    def 					_lowerCamelCase (	self				:   List[Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      int						=   self.prepare_config_and_inputs()
       UpperCAmelCase_		,      UpperCAmelCase_		,      UpperCAmelCase_							:      Any						=   config_and_inputs
       UpperCAmelCase_							:      Optional[Any]						=   {'''pixel_values''': pixel_values}
       return config, inputs_dict





@require_torch
class  lowerCAmelCase__(    snake_case__    ,    snake_case__    ,    unittest.TestCase						):






    '''simple docstring'''





    A_  :     List[str]			= (RegNetModel, RegNetForImageClassification) if is_torch_available() else ()
    A_  :     str			= (
        {'feature-extraction': RegNetModel, 'image-classification': RegNetForImageClassification}
        if is_torch_available()
        else {}
    )

    A_  :     List[str]			= False
    A_  :     Dict			= False
    A_  :     Tuple			= False
    A_  :     Union[str, Any]			= False





    def 					_lowerCamelCase (	self				:   Any	):





       '''simple docstring'''
       UpperCAmelCase_							:      Union[str, Any]						=   RegNetModelTester(self	)
       UpperCAmelCase_							:      Optional[int]						=   ConfigTester(self       ,   config_class=__snake_case       ,   has_text_modality=__snake_case	)





    def 					_lowerCamelCase (	self				:   Union[str, Any]	):





       '''simple docstring'''
       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				:   Any	):





       '''simple docstring'''
       return





    @unittest.skip(reason='''RegNet does not use inputs_embeds'''	)
    def 					_lowerCamelCase (	self				:   int	):





       '''simple docstring'''
       pass





    @unittest.skip(reason='''RegNet does not support input and output embeddings'''	)
    def 					_lowerCamelCase (	self				:   List[str]	):





       '''simple docstring'''
       pass





    def 					_lowerCamelCase (	self				:   str	):





       '''simple docstring'''
       UpperCAmelCase_		,      UpperCAmelCase_							:      int						=   self.model_tester.prepare_config_and_inputs_for_common()

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

          UpperCAmelCase_							:      Union[str, Any]						=   ['''pixel_values''']
          self.assertListEqual(arg_names[:1]       ,   __snake_case	)





    def 					_lowerCamelCase (	self				:   int	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[Any]						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__snake_case	)





    def 					_lowerCamelCase (	self				:   Optional[int]	):





       '''simple docstring'''
       UpperCAmelCase_		,      UpperCAmelCase_							:      str						=   self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
          UpperCAmelCase_							:      Dict						=   model_class(config=__snake_case	)
          for name, module in model.named_modules():
             if isinstance(__snake_case       ,   (nn.BatchNormad, nn.GroupNorm)	):
                self.assertTrue(
                    torch.all(module.weight == 1	)       ,   msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''       ,   )
                self.assertTrue(
                    torch.all(module.bias == 0	)       ,   msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''       ,   )





    def 					_lowerCamelCase (	self				:   int	):





       '''simple docstring'''
       def check_hidden_states_output(__snake_case				:   Dict       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Dict	):
          UpperCAmelCase_							:      str						=   model_class(__snake_case	)
          model.to(__snake_case	)
          model.eval()

          with torch.no_grad():
             UpperCAmelCase_							:      List[str]						=   model(**self._prepare_for_class(__snake_case       ,   __snake_case	)	)

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

          UpperCAmelCase_							:      str						=   self.model_tester.num_stages
          self.assertEqual(len(__snake_case	)       ,   expected_num_stages + 1	)

          # RegNet'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 // 2, self.model_tester.image_size // 2]       ,   )

       UpperCAmelCase_		,      UpperCAmelCase_							:      Any						=   self.model_tester.prepare_config_and_inputs_for_common()
       UpperCAmelCase_							:      Union[str, Any]						=   ['''basic''', '''bottleneck''']
       for model_class in self.all_model_classes:
          for layer_type in layers_type:
             UpperCAmelCase_							:      Optional[Any]						=   layer_type
             UpperCAmelCase_							:      Union[str, Any]						=   True
             check_hidden_states_output(__snake_case       ,   __snake_case       ,   __snake_case	)

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             UpperCAmelCase_							:      Union[str, Any]						=   True

             check_hidden_states_output(__snake_case       ,   __snake_case       ,   __snake_case	)





    def 					_lowerCamelCase (	self				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[Any]						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__snake_case	)





    @slow
    def 					_lowerCamelCase (	self				:   Union[str, Any]	):





       '''simple docstring'''
       for model_name in REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          UpperCAmelCase_							:      Dict						=   RegNetModel.from_pretrained(__snake_case	)
          self.assertIsNotNone(__snake_case	)





def      snake_case_						( ):
   UpperCAmelCase_							:      Union[str, Any]						=   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
   return image





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






    '''simple docstring'''





    @cached_property
    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       return (
           AutoImageProcessor.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)
           if is_vision_available()
           else None
       )





    @slow
    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       UpperCAmelCase_							:      Tuple						=   RegNetForImageClassification.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	).to(__snake_case	)

       UpperCAmelCase_							:      Optional[Any]						=   self.default_image_processor
       UpperCAmelCase_							:      Optional[int]						=   prepare_img()
       UpperCAmelCase_							:      List[str]						=   image_processor(images=__snake_case       ,   return_tensors='''pt'''	).to(__snake_case	)

       # forward pass
       with torch.no_grad():
          UpperCAmelCase_							:      List[Any]						=   model(**__snake_case	)

       # verify the logits
       UpperCAmelCase_							:      Dict						=   torch.Size((1, 1_000)	)
       self.assertEqual(outputs.logits.shape       ,   __snake_case	)

       UpperCAmelCase_							:      int						=   torch.tensor([-0.4_180, -1.5_051, -3.4_836]	).to(__snake_case	)

       self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,   __snake_case       ,   atol=1E-4	)	)



import copy

from ...configuration_utils import PretrainedConfig
from ...utils import add_start_docstrings


__UpperCamelCase				:  Dict      					=							R'\n    [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and\n    can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information.\n\n    Args:\n        title_sep (`str`, *optional*, defaults to  `" / "`):\n            Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`].\n        doc_sep (`str`, *optional*, defaults to  `" // "`):\n            Separator inserted between the text of the retrieved document and the original input when calling\n            [`RagRetriever`].\n        n_docs (`int`, *optional*, defaults to 5):\n            Number of documents to retrieve.\n        max_combined_length (`int`, *optional*, defaults to 300):\n            Max length of contextualized input returned by [`~RagRetriever.__call__`].\n        retrieval_vector_size (`int`, *optional*, defaults to 768):\n            Dimensionality of the document embeddings indexed by [`RagRetriever`].\n        retrieval_batch_size (`int`, *optional*, defaults to 8):\n            Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated\n            [`RagRetriever`].\n        dataset (`str`, *optional*, defaults to `"wiki_dpr"`):\n            A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids\n            using `datasets.list_datasets()`).\n        dataset_split (`str`, *optional*, defaults to `"train"`)\n            Which split of the `dataset` to load.\n        index_name (`str`, *optional*, defaults to `"compressed"`)\n            The index name of the index associated with the `dataset`. One can choose between `"legacy"`, `"exact"` and\n            `"compressed"`.\n        index_path (`str`, *optional*)\n            The path to the serialized faiss index on disk.\n        passages_path (`str`, *optional*):\n            A path to text passages compatible with the faiss index. Required if using\n            [`~models.rag.retrieval_rag.LegacyIndex`]\n        use_dummy_dataset (`bool`, *optional*, defaults to `False`)\n            Whether to load a "dummy" variant of the dataset specified by `dataset`.\n        label_smoothing (`float`, *optional*, defaults to 0.0):\n            Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing\n            in the loss calculation. If set to 0, no label smoothing is performed.\n        do_marginalize (`bool`, *optional*, defaults to `False`):\n            If `True`, the logits are marginalized over all documents by making use of\n            `torch.nn.functional.log_softmax`.\n        reduce_loss (`bool`, *optional*, defaults to `False`):\n            Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation.\n        do_deduplication (`bool`, *optional*, defaults to `True`):\n            Whether or not to deduplicate the generations from different context documents for a given input. Has to be\n            set to `False` if used while training with distributed backend.\n        exclude_bos_score (`bool`, *optional*, defaults to `False`):\n            Whether or not to disregard the BOS token when computing the loss.\n        output_retrieved(`bool`, *optional*, defaults to `False`):\n            If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and\n            `context_attention_mask` are returned. See returned tensors for more detail.\n        use_cache (`bool`, *optional*, defaults to `True`):\n            Whether or not the model should return the last key/values attentions (not used by all models).\n        forced_eos_token_id (`int`, *optional*):\n            The id of the token to force as the last generated token when `max_length` is reached. Usually set to\n            `eos_token_id`.\n'





@add_start_docstrings(snake_case__						)
class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    A_  :     Union[str, Any]			= 'rag'
    A_  :     Tuple			= True





    def __init__(	self				:   int       ,   __snake_case				:   List[str]=None       ,   __snake_case				:   List[Any]=True       ,   __snake_case				:   Optional[int]=None       ,   __snake_case				:   Union[str, Any]=None       ,   __snake_case				:   int=None       ,   __snake_case				:   Any=None       ,   __snake_case				:   Optional[int]=None       ,   __snake_case				:   Optional[int]=" / "       ,   __snake_case				:   Any=" // "       ,   __snake_case				:   Tuple=5       ,   __snake_case				:   Union[str, Any]=300       ,   __snake_case				:   Any=768       ,   __snake_case				:   Tuple=8       ,   __snake_case				:   int="wiki_dpr"       ,   __snake_case				:   Optional[int]="train"       ,   __snake_case				:   Tuple="compressed"       ,   __snake_case				:   Optional[int]=None       ,   __snake_case				:   List[Any]=None       ,   __snake_case				:   Optional[int]=False       ,   __snake_case				:   str=False       ,   __snake_case				:   Dict=0.0       ,   __snake_case				:   Union[str, Any]=True       ,   __snake_case				:   Union[str, Any]=False       ,   __snake_case				:   str=False       ,   __snake_case				:   str=False       ,   __snake_case				:   Optional[Any]=True       ,   __snake_case				:   int=None       ,   **__snake_case				:   str       ,   ):





       '''simple docstring'''
       super().__init__(
           bos_token_id=__snake_case       ,   pad_token_id=__snake_case       ,   eos_token_id=__snake_case       ,   decoder_start_token_id=__snake_case       ,   forced_eos_token_id=__snake_case       ,   is_encoder_decoder=__snake_case       ,   prefix=__snake_case       ,   vocab_size=__snake_case       ,   **__snake_case       ,   )
       assert (
           "question_encoder" in kwargs and "generator" in kwargs
       ), "Config has to be initialized with question_encoder and generator config"
       UpperCAmelCase_							:      Dict						=   kwargs.pop('''question_encoder'''	)
       UpperCAmelCase_							:      List[Any]						=   question_encoder_config.pop('''model_type'''	)
       UpperCAmelCase_							:      Any						=   kwargs.pop('''generator'''	)
       UpperCAmelCase_							:      Union[str, Any]						=   decoder_config.pop('''model_type'''	)

       from ..auto.configuration_auto import AutoConfig

       UpperCAmelCase_							:      Any						=   AutoConfig.for_model(__snake_case       ,   **__snake_case	)
       UpperCAmelCase_							:      int						=   AutoConfig.for_model(__snake_case       ,   **__snake_case	)

       UpperCAmelCase_							:      Any						=   reduce_loss
       UpperCAmelCase_							:      List[Any]						=   label_smoothing
       UpperCAmelCase_							:      Tuple						=   exclude_bos_score
       UpperCAmelCase_							:      int						=   do_marginalize

       UpperCAmelCase_							:      Tuple						=   title_sep
       UpperCAmelCase_							:      Union[str, Any]						=   doc_sep
       UpperCAmelCase_							:      Any						=   n_docs
       UpperCAmelCase_							:      Optional[int]						=   max_combined_length

       UpperCAmelCase_							:      Any						=   dataset
       UpperCAmelCase_							:      List[Any]						=   dataset_split
       UpperCAmelCase_							:      Union[str, Any]						=   index_name

       UpperCAmelCase_							:      List[str]						=   retrieval_vector_size
       UpperCAmelCase_							:      Optional[Any]						=   retrieval_batch_size
       UpperCAmelCase_							:      Optional[int]						=   passages_path
       UpperCAmelCase_							:      Optional[Any]						=   index_path
       UpperCAmelCase_							:      List[Any]						=   use_dummy_dataset

       UpperCAmelCase_							:      int						=   output_retrieved

       UpperCAmelCase_							:      int						=   do_deduplication

       UpperCAmelCase_							:      Optional[int]						=   use_cache

       if self.forced_eos_token_id is None:
          UpperCAmelCase_							:      int						=   getattr(self.generator       ,   '''forced_eos_token_id'''       ,   __snake_case	)





    @classmethod
    def 					_lowerCamelCase (	cls				:   Optional[Any]       ,   __snake_case				:   PretrainedConfig       ,   __snake_case				:   PretrainedConfig       ,   **__snake_case				:   str	):





       '''simple docstring'''
       return cls(question_encoder=question_encoder_config.to_dict()       ,   generator=generator_config.to_dict()       ,   **__snake_case	)





    def 					_lowerCamelCase (	self				:   int	):





       '''simple docstring'''
       UpperCAmelCase_							:      Union[str, Any]						=   copy.deepcopy(self.__dict__	)
       UpperCAmelCase_							:      Optional[int]						=   self.question_encoder.to_dict()
       UpperCAmelCase_							:      Dict						=   self.generator.to_dict()
       UpperCAmelCase_							:      Optional[Any]						=   self.__class__.model_type
       return output