

import unittest

import requests

from transformers import MODEL_FOR_VISION_2_SEQ_MAPPING, TF_MODEL_FOR_VISION_2_SEQ_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_torch_available():
  from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
  _a			=       False


if is_vision_available():
  from PIL import Image
else:






  class 							A_			:


       @staticmethod
       def   UpperCAmelCase  (							*UpperCAmelCase      :		Tuple      ,  **UpperCAmelCase      :		Dict )							->     Optional[Any]:
        pass






@is_pipeline_test
@require_vision
class 							A_			(		unittest.TestCase ):
     _lowercase      :					Any       =     MODEL_FOR_VISION_2_SEQ_MAPPING
     _lowercase      :					Dict       =     TF_MODEL_FOR_VISION_2_SEQ_MAPPING


     def   UpperCAmelCase  (							self      :		int      ,  UpperCAmelCase      :		List[Any]      ,  UpperCAmelCase      :		Union[str, Any]      ,  UpperCAmelCase      :		str )							->     List[Any]:
      __lowerCAmelCase:       str            =				pipeline('image-to-text'      ,  model=UpperCAmelCase      ,  tokenizer=UpperCAmelCase      ,  image_processor=UpperCAmelCase )
      __lowerCAmelCase:       Any            =				[
          Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ),
          './tests/fixtures/tests_samples/COCO/000000039769.png',
      ]
      return pipe, examples


     def   UpperCAmelCase  (							self      :		str      ,  UpperCAmelCase      :		List[Any]      ,  UpperCAmelCase      :		Tuple )							->     List[Any]:
      __lowerCAmelCase:       str            =				pipe(UpperCAmelCase )
      self.assertEqual(
          UpperCAmelCase      ,  [
              [{'generated_text': ANY(UpperCAmelCase )}],
              [{'generated_text': ANY(UpperCAmelCase )}],
          ]      ,  )


     @require_tf
     def   UpperCAmelCase  (							self      :		Optional[Any] )							->     List[str]:
      __lowerCAmelCase:       Tuple            =				pipeline('image-to-text'      ,  model='hf-internal-testing/tiny-random-vit-gpt2'      ,  framework='tf' )
      __lowerCAmelCase:       List[Any]            =				'./tests/fixtures/tests_samples/COCO/000000039769.png'

      __lowerCAmelCase:       Optional[int]            =				pipe(UpperCAmelCase )
      self.assertEqual(
          UpperCAmelCase      ,  [
              {
                  'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
              },
          ]      ,  )

      __lowerCAmelCase:       Optional[int]            =				pipe([image, image] )
      self.assertEqual(
          UpperCAmelCase      ,  [
              [
                  {
                      'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                  }
              ],
              [
                  {
                      'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                  }
              ],
          ]      ,  )

      __lowerCAmelCase:       int            =				pipe(UpperCAmelCase      ,  max_new_tokens=1 )
      self.assertEqual(
          UpperCAmelCase      ,  [{'generated_text': 'growth'}]      ,  )


     @require_torch
     def   UpperCAmelCase  (							self      :		List[Any] )							->     str:
      __lowerCAmelCase:       int            =				pipeline('image-to-text'      ,  model='hf-internal-testing/tiny-random-vit-gpt2' )
      __lowerCAmelCase:       List[Any]            =				'./tests/fixtures/tests_samples/COCO/000000039769.png'

      __lowerCAmelCase:       Any            =				pipe(UpperCAmelCase )
      self.assertEqual(
          UpperCAmelCase      ,  [
              {
                  'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
              },
          ]      ,  )

      __lowerCAmelCase:       Dict            =				pipe([image, image] )
      self.assertEqual(
          UpperCAmelCase      ,  [
              [
                  {
                      'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                  }
              ],
              [
                  {
                      'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                  }
              ],
          ]      ,  )


     @require_torch
     def   UpperCAmelCase  (							self      :		Union[str, Any] )							->     Tuple:
      __lowerCAmelCase:       int            =				pipeline('image-to-text'      ,  model='hf-internal-testing/tiny-random-BlipForConditionalGeneration' )
      __lowerCAmelCase:       Optional[Any]            =				'./tests/fixtures/tests_samples/COCO/000000039769.png'
      __lowerCAmelCase:       List[Any]            =				'a photo of'

      __lowerCAmelCase:       Dict            =				pipe(UpperCAmelCase      ,  prompt=UpperCAmelCase )
      self.assertTrue(outputs[0]['generated_text'].startswith(UpperCAmelCase ) )


     @slow
     @require_torch
     def   UpperCAmelCase  (							self      :		Tuple )							->     List[Any]:
      __lowerCAmelCase:       int            =				pipeline('image-to-text'      ,  model='ydshieh/vit-gpt2-coco-en' )
      __lowerCAmelCase:       Optional[int]            =				'./tests/fixtures/tests_samples/COCO/000000039769.png'

      __lowerCAmelCase:       str            =				pipe(UpperCAmelCase )
      self.assertEqual(UpperCAmelCase      ,  [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}] )

      __lowerCAmelCase:       Dict            =				pipe([image, image] )
      self.assertEqual(
          UpperCAmelCase      ,  [
              [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
              [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
          ]      ,  )


     @slow
     @require_torch
     def   UpperCAmelCase  (							self      :		Any )							->     List[str]:
      __lowerCAmelCase:       Any            =				pipeline('image-to-text'      ,  model='Salesforce/blip-image-captioning-base' )
      __lowerCAmelCase:       Optional[Any]            =				'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
      __lowerCAmelCase:       str            =				Image.open(requests.get(UpperCAmelCase      ,  stream=UpperCAmelCase ).raw )

      __lowerCAmelCase:       Tuple            =				pipe(UpperCAmelCase )
      self.assertEqual(UpperCAmelCase      ,  [{'generated_text': 'a pink pokemon pokemon with a blue shirt and a blue shirt'}] )


     @slow
     @require_torch
     def   UpperCAmelCase  (							self      :		List[str] )							->     Dict:
      __lowerCAmelCase:       List[Any]            =				pipeline('image-to-text'      ,  model='microsoft/git-base-coco' )
      __lowerCAmelCase:       List[Any]            =				'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
      __lowerCAmelCase:       int            =				Image.open(requests.get(UpperCAmelCase      ,  stream=UpperCAmelCase ).raw )

      __lowerCAmelCase:       Dict            =				pipe(UpperCAmelCase )
      self.assertEqual(UpperCAmelCase      ,  [{'generated_text': 'a cartoon of a purple character.'}] )


     @slow
     @require_torch
     def   UpperCAmelCase  (							self      :		Union[str, Any] )							->     int:
      __lowerCAmelCase:       Optional[Any]            =				pipeline('image-to-text'      ,  model='Salesforce/blip-image-captioning-base' )
      __lowerCAmelCase:       Optional[Any]            =				'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
      __lowerCAmelCase:       Any            =				Image.open(requests.get(UpperCAmelCase      ,  stream=UpperCAmelCase ).raw )

      __lowerCAmelCase:       Optional[int]            =				'a photography of'

      __lowerCAmelCase:       int            =				pipe(UpperCAmelCase      ,  prompt=UpperCAmelCase )
      self.assertEqual(UpperCAmelCase      ,  [{'generated_text': 'a photography of a volcano'}] )

      with self.assertRaises(UpperCAmelCase ):
       __lowerCAmelCase:       List[str]            =				pipe([image, image]      ,  prompt=[prompt, prompt] )


     @slow
     @require_torch
     def   UpperCAmelCase  (							self      :		Union[str, Any] )							->     Optional[int]:
      __lowerCAmelCase:       str            =				pipeline('image-to-text'      ,  model='microsoft/git-base-coco' )
      __lowerCAmelCase:       Dict            =				'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
      __lowerCAmelCase:       Optional[int]            =				Image.open(requests.get(UpperCAmelCase      ,  stream=UpperCAmelCase ).raw )

      __lowerCAmelCase:       Tuple            =				'a photo of a'

      __lowerCAmelCase:       Dict            =				pipe(UpperCAmelCase      ,  prompt=UpperCAmelCase )
      self.assertEqual(UpperCAmelCase      ,  [{'generated_text': 'a photo of a tent with a tent and a tent in the background.'}] )

      with self.assertRaises(UpperCAmelCase ):
       __lowerCAmelCase:       Union[str, Any]            =				pipe([image, image]      ,  prompt=[prompt, prompt] )


     @unittest.skipIf(
         not is_torch_greater_or_equal_than_1_11      ,  reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.' )
     @slow
     @require_torch
     def   UpperCAmelCase  (							self      :		List[Any] )							->     List[str]:
      __lowerCAmelCase:       int            =				pipeline('image-to-text'      ,  model='google/pix2struct-ai2d-base' )
      __lowerCAmelCase:       Optional[Any]            =				'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
      __lowerCAmelCase:       List[str]            =				Image.open(requests.get(UpperCAmelCase      ,  stream=UpperCAmelCase ).raw )

      __lowerCAmelCase:       List[str]            =				'What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'

      __lowerCAmelCase:       Union[str, Any]            =				pipe(UpperCAmelCase      ,  prompt=UpperCAmelCase )
      self.assertEqual(UpperCAmelCase      ,  [{'generated_text': 'ash cloud'}] )

      with self.assertRaises(UpperCAmelCase ):
       __lowerCAmelCase:       Tuple            =				pipe([image, image]      ,  prompt=[prompt, prompt] )


     @slow
     @require_tf
     def   UpperCAmelCase  (							self      :		int )							->     Tuple:
      __lowerCAmelCase:       Union[str, Any]            =				pipeline('image-to-text'      ,  model='ydshieh/vit-gpt2-coco-en'      ,  framework='tf' )
      __lowerCAmelCase:       int            =				'./tests/fixtures/tests_samples/COCO/000000039769.png'

      __lowerCAmelCase:       int            =				pipe(UpperCAmelCase )
      self.assertEqual(UpperCAmelCase      ,  [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}] )

      __lowerCAmelCase:       Dict            =				pipe([image, image] )
      self.assertEqual(
          UpperCAmelCase      ,  [
              [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
              [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
          ]      ,  )









import inspect
import unittest

from transformers import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, 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 transformers import MODEL_MAPPING, PoolFormerConfig, PoolFormerForImageClassification, PoolFormerModel
  from transformers.models.poolformer.modeling_poolformer import POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import PoolFormerImageProcessor






class 							A_			(		snake_case__ ):


     def   UpperCAmelCase  (							self      :		int )							->     Tuple:
      __lowerCAmelCase:       Optional[Any]            =				self.config_class(**self.inputs_dict )
      self.parent.assertTrue(hasattr(UpperCAmelCase      ,  'hidden_sizes' ) )
      self.parent.assertTrue(hasattr(UpperCAmelCase      ,  'num_encoder_blocks' ) )







class 							A_			:


     def __init__(							self      :		Optional[int]      ,  UpperCAmelCase      :		Any      ,  UpperCAmelCase      :		List[str]=1_3      ,  UpperCAmelCase      :		Optional[Any]=6_4      ,  UpperCAmelCase      :		Union[str, Any]=3      ,  UpperCAmelCase      :		Optional[Any]=4      ,  UpperCAmelCase      :		Any=[2, 2, 2, 2]      ,  UpperCAmelCase      :		Optional[Any]=[8, 4, 2, 1]      ,  UpperCAmelCase      :		str=[1_6, 3_2, 6_4, 1_2_8]      ,  UpperCAmelCase      :		str=[1, 4, 8, 1_6]      ,  UpperCAmelCase      :		Optional[Any]=False      ,  UpperCAmelCase      :		str=True      ,  UpperCAmelCase      :		Optional[int]="gelu"      ,  UpperCAmelCase      :		Union[str, Any]=0.1      ,  UpperCAmelCase      :		Optional[Any]=0.02      ,  UpperCAmelCase      :		List[Any]=3      ,  UpperCAmelCase      :		Optional[int]=None      ,  )							->     Any:
      __lowerCAmelCase:       List[str]            =				parent
      __lowerCAmelCase:       Union[str, Any]            =				batch_size
      __lowerCAmelCase:       Union[str, Any]            =				image_size
      __lowerCAmelCase:       str            =				num_channels
      __lowerCAmelCase:       Tuple            =				num_encoder_blocks
      __lowerCAmelCase:       Optional[int]            =				sr_ratios
      __lowerCAmelCase:       Union[str, Any]            =				depths
      __lowerCAmelCase:       int            =				hidden_sizes
      __lowerCAmelCase:       List[Any]            =				downsampling_rates
      __lowerCAmelCase:       List[Any]            =				is_training
      __lowerCAmelCase:       Union[str, Any]            =				use_labels
      __lowerCAmelCase:       Union[str, Any]            =				hidden_act
      __lowerCAmelCase:       Any            =				hidden_dropout_prob
      __lowerCAmelCase:       List[Any]            =				initializer_range
      __lowerCAmelCase:       Dict            =				num_labels
      __lowerCAmelCase:       List[Any]            =				scope


     def   UpperCAmelCase  (							self      :		Optional[int] )							->     List[str]:
      __lowerCAmelCase:       Dict            =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

      __lowerCAmelCase:       Dict            =				PoolFormerConfig(
          image_size=self.image_size      ,  num_channels=self.num_channels      ,  num_encoder_blocks=self.num_encoder_blocks      ,  depths=self.depths      ,  hidden_sizes=self.hidden_sizes      ,  hidden_act=self.hidden_act      ,  hidden_dropout_prob=self.hidden_dropout_prob      ,  initializer_range=self.initializer_range      ,  )

      return config, pixel_values, labels


     def   UpperCAmelCase  (							self      :		str      ,  UpperCAmelCase      :		Tuple      ,  UpperCAmelCase      :		int      ,  UpperCAmelCase      :		List[Any] )							->     str:
      __lowerCAmelCase:       Any            =				PoolFormerModel(config=UpperCAmelCase )
      model.to(UpperCAmelCase )
      model.eval()
      __lowerCAmelCase:       List[str]            =				model(UpperCAmelCase )
      __lowerCAmelCase:       Union[str, Any]            =				self.image_size // 32.0
      self.parent.assertEqual(
          result.last_hidden_state.shape      ,  (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width) )


     def   UpperCAmelCase  (							self      :		Any )							->     int:
      __lowerCAmelCase:       List[str]            =				self.prepare_config_and_inputs()
      __lowerCAmelCase     ,							__lowerCAmelCase     ,							__lowerCAmelCase:       Union[str, Any]            =				config_and_inputs
      __lowerCAmelCase:       List[str]            =				{'pixel_values': pixel_values}
      return config, inputs_dict







@require_torch
class 							A_			(		snake_case__							,     snake_case__							,     unittest.TestCase ):
     _lowercase      :					Optional[Any]       =     (PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
     _lowercase      :					List[Any]       =     (
         {'feature-extraction': PoolFormerModel, 'image-classification': PoolFormerForImageClassification}
         if is_torch_available()
         else {}
     )

     _lowercase      :					Tuple       =     False
     _lowercase      :					Tuple       =     False
     _lowercase      :					List[Any]       =     False
     _lowercase      :					Any       =     False
     _lowercase      :					List[Any]       =     False


     def   UpperCAmelCase  (							self      :		str )							->     Any:
      __lowerCAmelCase:       int            =				PoolFormerModelTester(self )
      __lowerCAmelCase:       int            =				PoolFormerConfigTester(self      ,  config_class=UpperCAmelCase )


     def   UpperCAmelCase  (							self      :		Optional[Any] )							->     int:
      self.config_tester.run_common_tests()


     def   UpperCAmelCase  (							self      :		int )							->     int:
      __lowerCAmelCase:       Union[str, Any]            =				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*UpperCAmelCase )


     @unittest.skip('PoolFormer does not use inputs_embeds' )
     def   UpperCAmelCase  (							self      :		Optional[int] )							->     Union[str, Any]:
      pass


     @unittest.skip('PoolFormer does not have get_input_embeddings method and get_output_embeddings methods' )
     def   UpperCAmelCase  (							self      :		int )							->     List[str]:
      pass


     def   UpperCAmelCase  (							self      :		List[str] )							->     List[str]:
      def check_hidden_states_output(UpperCAmelCase      :		List[str]      ,  UpperCAmelCase      :		int      ,  UpperCAmelCase      :		List[Any] ):
       __lowerCAmelCase:       Union[str, Any]            =				model_class(UpperCAmelCase )
       model.to(UpperCAmelCase )
       model.eval()

       with torch.no_grad():
        __lowerCAmelCase:       Dict            =				model(**self._prepare_for_class(UpperCAmelCase      ,  UpperCAmelCase ) )

       __lowerCAmelCase:       int            =				outputs.hidden_states

       __lowerCAmelCase:       Optional[int]            =				self.model_tester.num_encoder_blocks
       self.assertEqual(len(UpperCAmelCase )      ,  UpperCAmelCase )

       # verify the first hidden states (first block)
       self.assertListEqual(
           list(hidden_states[0].shape[-3:] )      ,  [
               self.model_tester.hidden_sizes[0],
               self.model_tester.image_size // 4,
               self.model_tester.image_size // 4,
           ]      ,  )

      __lowerCAmelCase     ,							__lowerCAmelCase:       Dict            =				self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
       __lowerCAmelCase:       Optional[int]            =				True
       check_hidden_states_output(UpperCAmelCase      ,  UpperCAmelCase      ,  UpperCAmelCase )

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

       check_hidden_states_output(UpperCAmelCase      ,  UpperCAmelCase      ,  UpperCAmelCase )


     def   UpperCAmelCase  (							self      :		List[Any] )							->     Optional[int]:
      if not self.model_tester.is_training:
       return

      __lowerCAmelCase     ,							__lowerCAmelCase:       Union[str, Any]            =				self.model_tester.prepare_config_and_inputs_for_common()
      __lowerCAmelCase:       Optional[int]            =				True

      for model_class in self.all_model_classes:
       if model_class in get_values(UpperCAmelCase ):
        continue
       __lowerCAmelCase:       str            =				model_class(UpperCAmelCase )
       model.to(UpperCAmelCase )
       model.train()
       __lowerCAmelCase:       Any            =				self._prepare_for_class(UpperCAmelCase      ,  UpperCAmelCase      ,  return_labels=UpperCAmelCase )
       __lowerCAmelCase:       str            =				model(**UpperCAmelCase ).loss
       loss.backward()


     def   UpperCAmelCase  (							self      :		Dict )							->     Tuple:
      __lowerCAmelCase     ,							__lowerCAmelCase:       Union[str, Any]            =				self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
       __lowerCAmelCase:       Union[str, Any]            =				model_class(UpperCAmelCase )
       __lowerCAmelCase:       Dict            =				inspect.signature(model.forward )
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       __lowerCAmelCase:       List[str]            =				[*signature.parameters.keys()]

       __lowerCAmelCase:       List[Any]            =				['pixel_values']
       self.assertListEqual(arg_names[:1]      ,  UpperCAmelCase )


     @slow
     def   UpperCAmelCase  (							self      :		int )							->     str:
      for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowerCAmelCase:       List[str]            =				PoolFormerModel.from_pretrained(UpperCAmelCase )
       self.assertIsNotNone(UpperCAmelCase )







def _a			(	)   ->   Any:

 """simple docstring"""






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






@require_torch
class 							A_			(		unittest.TestCase ):


     @slow
     def   UpperCAmelCase  (							self      :		Optional[Any] )							->     int:
      __lowerCAmelCase:       Optional[int]            =				PoolFormerImageProcessor()
      __lowerCAmelCase:       Any            =				PoolFormerForImageClassification.from_pretrained('sail/poolformer_s12' ).to(UpperCAmelCase )

      __lowerCAmelCase:       int            =				image_processor(images=prepare_img()      ,  return_tensors='pt' ).to(UpperCAmelCase )

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

      # verify the logits
      __lowerCAmelCase:       List[Any]            =				torch.Size((1, 1_0_0_0) )
      self.assertEqual(outputs.logits.shape      ,  UpperCAmelCase )

      __lowerCAmelCase:       Union[str, Any]            =				torch.tensor([-0.6113, 0.1685, -0.0492] ).to(UpperCAmelCase )
      self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,  UpperCAmelCase      ,  atol=1E-4 ) )





