"""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 			UpperCamelCase						:
   def __init__(     self	,__UpperCamelCase	,__UpperCamelCase=13	,__UpperCamelCase=32	,__UpperCamelCase=3	,__UpperCamelCase=4	,__UpperCamelCase=[10, 20, 30, 40]	,__UpperCamelCase=[2, 2, 3, 2]	,__UpperCamelCase=True	,__UpperCamelCase=True	,__UpperCamelCase=37	,__UpperCamelCase="gelu"	,__UpperCamelCase=10	,__UpperCamelCase=0.02	,__UpperCamelCase=["stage2", "stage3", "stage4"]	,__UpperCamelCase=[2, 3, 4]	,__UpperCamelCase=None	,)      -> int:


        '''simple docstring'''


        lowercase_      :							int						=      parent
        lowercase_      :							Optional[Any]						=      batch_size
        lowercase_      :							int						=      image_size
        lowercase_      :							Any						=      num_channels
        lowercase_      :							Tuple						=      num_stages
        lowercase_      :							List[str]						=      hidden_sizes
        lowercase_      :							int						=      depths
        lowercase_      :							Any						=      is_training
        lowercase_      :							Dict						=      use_labels
        lowercase_      :							Dict						=      intermediate_size
        lowercase_      :							Tuple						=      hidden_act
        lowercase_      :							Optional[Any]						=      num_labels
        lowercase_      :							Optional[Any]						=      initializer_range
        lowercase_      :							str						=      out_features
        lowercase_      :							List[Any]						=      out_indices
        lowercase_      :							str						=      scope
   def      _UpperCAmelCase  (     self     )      -> List[Any]:


        '''simple docstring'''


        lowercase_      :							int						=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

        lowercase_      :							Any						=      None
        if self.use_labels:
             lowercase_      :							Any						=      ids_tensor([self.batch_size]	,self.num_labels     )

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

        return config, pixel_values, labels
   def      _UpperCAmelCase  (     self     )      -> List[Any]:


        '''simple docstring'''


        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      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> List[str]:


        '''simple docstring'''


        lowercase_      :							Optional[int]						=      ConvNextVaModel(config=__UpperCamelCase     )
        model.to(__UpperCamelCase     )
        model.eval()
        lowercase_      :							int						=      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      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> str:


        '''simple docstring'''


        lowercase_      :							Tuple						=      ConvNextVaForImageClassification(__UpperCamelCase     )
        model.to(__UpperCamelCase     )
        model.eval()
        lowercase_      :							List[Any]						=      model(__UpperCamelCase	,labels=__UpperCamelCase     )
        self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.num_labels)     )
   def      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> Tuple:


        '''simple docstring'''


        lowercase_      :							Optional[Any]						=      ConvNextVaBackbone(config=__UpperCamelCase     )
        model.to(__UpperCamelCase     )
        model.eval()
        lowercase_      :							Tuple						=      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
        lowercase_      :							Any						=      None
        lowercase_      :							Tuple						=      ConvNextVaBackbone(config=__UpperCamelCase     )
        model.to(__UpperCamelCase     )
        model.eval()
        lowercase_      :							Optional[Any]						=      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      _UpperCAmelCase  (     self     )      -> List[Any]:


        '''simple docstring'''


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







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


        '''simple docstring'''


        lowercase_      :							Tuple						=      self.prepare_config_and_inputs()
        lowercase_      :							List[Any]						=      config_and_inputs
        lowercase_      :							Optional[int]						=      {'pixel_values': pixel_values, 'labels': labels}
        return config, inputs_dict







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

   lowercase				=    False
   lowercase				=    False
   lowercase				=    False
   lowercase				=    False
   lowercase				=    False
   def      _UpperCAmelCase  (     self     )      -> List[str]:


        '''simple docstring'''


        lowercase_      :							str						=      ConvNextVaModelTester(self     )
        lowercase_      :							List[Any]						=      ConfigTester(self	,config_class=__UpperCamelCase	,has_text_modality=__UpperCamelCase	,hidden_size=37     )
   def      _UpperCAmelCase  (     self     )      -> List[str]:


        '''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      _UpperCAmelCase  (     self     )      -> Tuple:


        '''simple docstring'''


        return
   @unittest.skip(reason='ConvNextV2 does not use inputs_embeds'     )
   def      _UpperCAmelCase  (     self     )      -> Optional[int]:


        '''simple docstring'''


        pass
   @unittest.skip(reason='ConvNextV2 does not support input and output embeddings'     )
   def      _UpperCAmelCase  (     self     )      -> Tuple:


        '''simple docstring'''


        pass
   @unittest.skip(reason='ConvNextV2 does not use feedforward chunking'     )
   def      _UpperCAmelCase  (     self     )      -> Optional[int]:


        '''simple docstring'''


        pass
   def      _UpperCAmelCase  (     self     )      -> Tuple:


        '''simple docstring'''


        if not self.model_tester.is_training:
             return

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

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

             lowercase_      :							Tuple						=      model_class(__UpperCamelCase     )
             model.to(__UpperCamelCase     )
             model.train()
             lowercase_      :							List[str]						=      self._prepare_for_class(__UpperCamelCase	,__UpperCamelCase	,return_labels=__UpperCamelCase     )
             lowercase_      :							Dict						=      model(**__UpperCamelCase     ).loss
             loss.backward()
   def      _UpperCAmelCase  (     self     )      -> Dict:


        '''simple docstring'''


        if not self.model_tester.is_training:
             return

        for model_class in self.all_model_classes:
             lowercase_      :							Dict						=      self.model_tester.prepare_config_and_inputs_with_labels()
             lowercase_      :							Tuple						=      False
             lowercase_      :							Tuple						=      True

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

             lowercase_      :							str						=      model_class(__UpperCamelCase     )
             model.to(__UpperCamelCase     )
             model.gradient_checkpointing_enable()
             model.train()
             lowercase_      :							Any						=      self._prepare_for_class(__UpperCamelCase	,__UpperCamelCase	,return_labels=__UpperCamelCase     )
             lowercase_      :							Optional[int]						=      model(**__UpperCamelCase     ).loss
             loss.backward()
   def      _UpperCAmelCase  (     self     )      -> Tuple:


        '''simple docstring'''


        lowercase_      :							Tuple						=      self.model_tester.prepare_config_and_inputs_for_common()

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

             lowercase_      :							List[str]						=      ['pixel_values']
             self.assertListEqual(arg_names[:1]	,__UpperCamelCase     )
   def      _UpperCAmelCase  (     self     )      -> Dict:


        '''simple docstring'''


        lowercase_      :							Union[str, Any]						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__UpperCamelCase     )
   def      _UpperCAmelCase  (     self     )      -> Optional[Any]:


        '''simple docstring'''


        def check_hidden_states_output(__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     ):
             lowercase_      :							List[Any]						=      model_class(__UpperCamelCase     )
             model.to(__UpperCamelCase     )
             model.eval()

             with torch.no_grad():
                  lowercase_      :							Optional[int]						=      model(**self._prepare_for_class(__UpperCamelCase	,__UpperCamelCase     )     )

             lowercase_      :							Dict						=      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

             lowercase_      :							Any						=      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]	,)

        lowercase_      :							Tuple						=      self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase_      :							Optional[Any]						=      True
             check_hidden_states_output(__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )

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

             check_hidden_states_output(__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )
   def      _UpperCAmelCase  (     self     )      -> Any:


        '''simple docstring'''


        lowercase_      :							Dict						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase     )







   @slow
   def      _UpperCAmelCase  (     self     )      -> Union[str, Any]:


        '''simple docstring'''


        for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowercase_      :							Optional[int]						=      ConvNextVaModel.from_pretrained(__UpperCamelCase     )
             self.assertIsNotNone(__UpperCamelCase     )






def      lowercase__(			):
     lowercase_      :							str						=      Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' )
     return image







@require_torch
@require_vision
class 			UpperCamelCase						(     unittest.TestCase    ):
   @cached_property
   def      _UpperCAmelCase  (     self     )      -> int:


        '''simple docstring'''


        return AutoImageProcessor.from_pretrained('facebook/convnextv2-tiny-1k-224'     ) if is_vision_available() else None







   @slow
   def      _UpperCAmelCase  (     self     )      -> Union[str, Any]:


        '''simple docstring'''


        lowercase_      :							Optional[Any]						=      ConvNextVaForImageClassification.from_pretrained('facebook/convnextv2-tiny-1k-224'     ).to(__UpperCamelCase     )

        lowercase_      :							List[Any]						=      self.default_image_processor
        lowercase_      :							Optional[int]						=      prepare_img()
        lowercase_      :							Tuple						=      preprocessor(images=__UpperCamelCase	,return_tensors='pt'     ).to(__UpperCamelCase     )

        # forward pass
        with torch.no_grad():
             lowercase_      :							Tuple						=      model(**__UpperCamelCase     )

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

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









"""simple docstring"""


# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ..models.auto import AutoModelForVisionaSeq
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
		from PIL import Image




class 						__UpperCamelCase     (     UpperCamelCase						):
					lowercase_    :				Optional[Any]      =  """Salesforce/blip-image-captioning-base"""
					lowercase_    :				Optional[int]      =  (
					    """This is a tool that generates a description of an image. It takes an input named `image` which should be the """
					    """image to caption, and returns a text that contains the description in English."""
					)
					lowercase_    :				int      =  """image_captioner"""
					lowercase_    :				int      =  AutoModelForVisionaSeq

					lowercase_    :				List[str]      =  ["""image"""]
					lowercase_    :				int      =  ["""text"""]
					def __init__(							self							:	Any     , *UpperCAmelCase							:	str     , **UpperCAmelCase							:	List[str]   )	->       Union[str, Any]:
											requires_backends(self     , ['vision']   )
											super().__init__(*UpperCAmelCase     , **UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	int     , UpperCAmelCase							:	"Image"   )	->       Union[str, Any]:
											return self.pre_processor(images=UpperCAmelCase     , return_tensors='pt'   )
					def        UpperCAmelCase__ (							self							:	int     , UpperCAmelCase							:	Optional[int]   )	->       Optional[int]:
											return self.model.generate(**UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	Dict     , UpperCAmelCase							:	int   )	->       Dict:
											return self.pre_processor.batch_decode(UpperCAmelCase     , skip_special_tokens=UpperCAmelCase   )[0].strip()