




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_   :	Optional[int]							   = False


if is_vision_available():
     from PIL import Image
else:


     class 						__UpperCamelCase :






            @staticmethod
            def   lowercase__  (							*lowerCAmelCase,	**lowerCAmelCase						):



                   """simple docstring"""







                   pass


@is_pipeline_test
@require_vision
class 						__UpperCamelCase (				unittest.TestCase       ):
       lowercase			:					Union[str, Any]   		=MODEL_FOR_VISION_2_SEQ_MAPPING
       lowercase			:					List[Any]   		=TF_MODEL_FOR_VISION_2_SEQ_MAPPING






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						):



              """simple docstring"""







              lowerCamelCase_							=pipeline('''image-to-text''',	model=lowerCamelCase__,	tokenizer=lowerCamelCase__,	image_processor=lowerCamelCase__						)
              lowerCamelCase_							=[
                  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						),
                  "./tests/fixtures/tests_samples/COCO/000000039769.png",
              ]
              return pipe, examples






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase						):



              """simple docstring"""







              lowerCamelCase_							=pipe(lowerCamelCase__						)
              self.assertEqual(
                  lowerCamelCase__,	[
                      [{'''generated_text''': ANY(lowerCamelCase__						)}],
                      [{'''generated_text''': ANY(lowerCamelCase__						)}],
                  ],	)






       @require_tf
       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=pipeline('''image-to-text''',	model='''hf-internal-testing/tiny-random-vit-gpt2''',	framework='''tf'''						)
              lowerCamelCase_							="./tests/fixtures/tests_samples/COCO/000000039769.png"

              lowerCamelCase_							=pipe(lowerCamelCase__						)
              self.assertEqual(
                  lowerCamelCase__,	[
                      {
                          '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                      },
                  ],	)

              lowerCamelCase_							=pipe([image, image]						)
              self.assertEqual(
                  lowerCamelCase__,	[
                      [
                          {
                              '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                          }
                      ],
                      [
                          {
                              '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                          }
                      ],
                  ],	)

              lowerCamelCase_							=pipe(lowerCamelCase__,	max_new_tokens=1						)
              self.assertEqual(
                  lowerCamelCase__,	[{'''generated_text''': '''growth'''}],	)






       @require_torch
       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=pipeline('''image-to-text''',	model='''hf-internal-testing/tiny-random-vit-gpt2'''						)
              lowerCamelCase_							="./tests/fixtures/tests_samples/COCO/000000039769.png"

              lowerCamelCase_							=pipe(lowerCamelCase__						)
              self.assertEqual(
                  lowerCamelCase__,	[
                      {
                          '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                      },
                  ],	)

              lowerCamelCase_							=pipe([image, image]						)
              self.assertEqual(
                  lowerCamelCase__,	[
                      [
                          {
                              '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                          }
                      ],
                      [
                          {
                              '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                          }
                      ],
                  ],	)






       @require_torch
       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=pipeline('''image-to-text''',	model='''hf-internal-testing/tiny-random-BlipForConditionalGeneration'''						)
              lowerCamelCase_							="./tests/fixtures/tests_samples/COCO/000000039769.png"
              lowerCamelCase_							="a photo of"

              lowerCamelCase_							=pipe(lowerCamelCase__,	prompt=lowerCamelCase__						)
              self.assertTrue(outputs[0]['''generated_text'''].startswith(lowerCamelCase__						)						)






       @slow
       @require_torch
       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=pipeline('''image-to-text''',	model='''ydshieh/vit-gpt2-coco-en'''						)
              lowerCamelCase_							="./tests/fixtures/tests_samples/COCO/000000039769.png"

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

              lowerCamelCase_							=pipe([image, image]						)
              self.assertEqual(
                  lowerCamelCase__,	[
                      [{'''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   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=pipeline('''image-to-text''',	model='''Salesforce/blip-image-captioning-base'''						)
              lowerCamelCase_							="https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
              lowerCamelCase_							=Image.open(requests.get(lowerCamelCase__,	stream=lowerCamelCase__						).raw						)

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






       @slow
       @require_torch
       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=pipeline('''image-to-text''',	model='''microsoft/git-base-coco'''						)
              lowerCamelCase_							="https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
              lowerCamelCase_							=Image.open(requests.get(lowerCamelCase__,	stream=lowerCamelCase__						).raw						)

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






       @slow
       @require_torch
       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=pipeline('''image-to-text''',	model='''Salesforce/blip-image-captioning-base'''						)
              lowerCamelCase_							="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
              lowerCamelCase_							=Image.open(requests.get(lowerCamelCase__,	stream=lowerCamelCase__						).raw						)

              lowerCamelCase_							="a photography of"

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

              with self.assertRaises(lowerCamelCase__						):
                     lowerCamelCase_							=pipe([image, image],	prompt=[prompt, prompt]						)






       @slow
       @require_torch
       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=pipeline('''image-to-text''',	model='''microsoft/git-base-coco'''						)
              lowerCamelCase_							="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
              lowerCamelCase_							=Image.open(requests.get(lowerCamelCase__,	stream=lowerCamelCase__						).raw						)

              lowerCamelCase_							="a photo of a"

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

              with self.assertRaises(lowerCamelCase__						):
                     lowerCamelCase_							=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   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=pipeline('''image-to-text''',	model='''google/pix2struct-ai2d-base'''						)
              lowerCamelCase_							="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
              lowerCamelCase_							=Image.open(requests.get(lowerCamelCase__,	stream=lowerCamelCase__						).raw						)

              lowerCamelCase_							="What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"

              lowerCamelCase_							=pipe(lowerCamelCase__,	prompt=lowerCamelCase__						)
              self.assertEqual(lowerCamelCase__,	[{'''generated_text''': '''ash cloud'''}]						)

              with self.assertRaises(lowerCamelCase__						):
                     lowerCamelCase_							=pipe([image, image],	prompt=[prompt, prompt]						)






       @slow
       @require_tf
       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=pipeline('''image-to-text''',	model='''ydshieh/vit-gpt2-coco-en''',	framework='''tf'''						)
              lowerCamelCase_							="./tests/fixtures/tests_samples/COCO/000000039769.png"

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

              lowerCamelCase_							=pipe([image, image]						)
              self.assertEqual(
                  lowerCamelCase__,	[
                      [{'''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 '''}],
                  ],	)










'''simple docstring'''




import torch

from diffusers import HeunDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest


class 						__UpperCamelCase (				lowerCamelCase__       ):
       lowercase			:					Union[str, Any]   		=(HeunDiscreteScheduler,)
       lowercase			:					int   		=10






       def   lowercase__  (							self,	**lowerCAmelCase						):



              """simple docstring"""







              lowerCamelCase_							={
                  '''num_train_timesteps''': 1_100,
                  '''beta_start''': 0.0_0_0_1,
                  '''beta_end''': 0.0_2,
                  '''beta_schedule''': '''linear''',
              }

              config.update(**lowerCAmelCase						)
              return config






       def   lowercase__  (							self						):



              """simple docstring"""







              for timesteps in [10, 50, 100, 1_000]:
                     self.check_over_configs(num_train_timesteps=lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1],	[0.0_0_0_2, 0.0_0_2, 0.0_2]						):
                     self.check_over_configs(beta_start=lowerCAmelCase,	beta_end=lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              for schedule in ["linear", "scaled_linear", "exp"]:
                     self.check_over_configs(beta_schedule=lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              for clip_sample_range in [1.0, 2.0, 3.0]:
                     self.check_over_configs(clip_sample_range=lowerCAmelCase,	clip_sample=lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              for prediction_type in ["epsilon", "v_prediction", "sample"]:
                     self.check_over_configs(prediction_type=lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.scheduler_classes[0]
              lowerCamelCase_							=self.get_scheduler_config()
              lowerCamelCase_							=scheduler_class(**lowerCAmelCase						)

              scheduler.set_timesteps(self.num_inference_steps						)

              lowerCamelCase_							=self.dummy_model()
              lowerCamelCase_							=self.dummy_sample_deter * scheduler.init_noise_sigma
              lowerCamelCase_							=sample.to(lowerCAmelCase						)

              for i, t in enumerate(scheduler.timesteps						):
                     lowerCamelCase_							=scheduler.scale_model_input(lowerCAmelCase,	lowerCAmelCase						)

                     lowerCamelCase_							=model(lowerCAmelCase,	lowerCAmelCase						)

                     lowerCamelCase_							=scheduler.step(lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						)
                     lowerCamelCase_							=output.prev_sample

              lowerCamelCase_							=torch.sum(torch.abs(lowerCAmelCase						)						)
              lowerCamelCase_							=torch.mean(torch.abs(lowerCAmelCase						)						)

              if torch_device in ["cpu", "mps"]:
                     assert abs(result_sum.item() - 0.1_2_3_3						) < 1e-2
                     assert abs(result_mean.item() - 0.0_0_0_2						) < 1e-3
              else:
                     # CUDA
                     assert abs(result_sum.item() - 0.1_2_3_3						) < 1e-2
                     assert abs(result_mean.item() - 0.0_0_0_2						) < 1e-3






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.scheduler_classes[0]
              lowerCamelCase_							=self.get_scheduler_config(prediction_type='''v_prediction'''						)
              lowerCamelCase_							=scheduler_class(**lowerCAmelCase						)

              scheduler.set_timesteps(self.num_inference_steps						)

              lowerCamelCase_							=self.dummy_model()
              lowerCamelCase_							=self.dummy_sample_deter * scheduler.init_noise_sigma
              lowerCamelCase_							=sample.to(lowerCAmelCase						)

              for i, t in enumerate(scheduler.timesteps						):
                     lowerCamelCase_							=scheduler.scale_model_input(lowerCAmelCase,	lowerCAmelCase						)

                     lowerCamelCase_							=model(lowerCAmelCase,	lowerCAmelCase						)

                     lowerCamelCase_							=scheduler.step(lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						)
                     lowerCamelCase_							=output.prev_sample

              lowerCamelCase_							=torch.sum(torch.abs(lowerCAmelCase						)						)
              lowerCamelCase_							=torch.mean(torch.abs(lowerCAmelCase						)						)

              if torch_device in ["cpu", "mps"]:
                     assert abs(result_sum.item() - 4.6_934e-07						) < 1e-2
                     assert abs(result_mean.item() - 6.1_112e-10						) < 1e-3
              else:
                     # CUDA
                     assert abs(result_sum.item() - 4.693_428_650_170_972e-07						) < 1e-2
                     assert abs(result_mean.item() - 0.0_0_0_2						) < 1e-3






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.scheduler_classes[0]
              lowerCamelCase_							=self.get_scheduler_config()
              lowerCamelCase_							=scheduler_class(**lowerCAmelCase						)

              scheduler.set_timesteps(self.num_inference_steps,	device=lowerCAmelCase						)

              lowerCamelCase_							=self.dummy_model()
              lowerCamelCase_							=self.dummy_sample_deter.to(lowerCAmelCase						) * scheduler.init_noise_sigma

              for t in scheduler.timesteps:
                     lowerCamelCase_							=scheduler.scale_model_input(lowerCAmelCase,	lowerCAmelCase						)

                     lowerCamelCase_							=model(lowerCAmelCase,	lowerCAmelCase						)

                     lowerCamelCase_							=scheduler.step(lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						)
                     lowerCamelCase_							=output.prev_sample

              lowerCamelCase_							=torch.sum(torch.abs(lowerCAmelCase						)						)
              lowerCamelCase_							=torch.mean(torch.abs(lowerCAmelCase						)						)

              if str(lowerCAmelCase						).startswith('''cpu'''						):
                     # The following sum varies between 148 and 156 on mps. Why?
                     assert abs(result_sum.item() - 0.1_2_3_3						) < 1e-2
                     assert abs(result_mean.item() - 0.0_0_0_2						) < 1e-3
              elif str(lowerCAmelCase						).startswith('''mps'''						):
                     # Larger tolerance on mps
                     assert abs(result_mean.item() - 0.0_0_0_2						) < 1e-2
              else:
                     # CUDA
                     assert abs(result_sum.item() - 0.1_2_3_3						) < 1e-2
                     assert abs(result_mean.item() - 0.0_0_0_2						) < 1e-3






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.scheduler_classes[0]
              lowerCamelCase_							=self.get_scheduler_config()
              lowerCamelCase_							=scheduler_class(**lowerCAmelCase,	use_karras_sigmas=lowerCAmelCase						)

              scheduler.set_timesteps(self.num_inference_steps,	device=lowerCAmelCase						)

              lowerCamelCase_							=self.dummy_model()
              lowerCamelCase_							=self.dummy_sample_deter.to(lowerCAmelCase						) * scheduler.init_noise_sigma
              lowerCamelCase_							=sample.to(lowerCAmelCase						)

              for t in scheduler.timesteps:
                     lowerCamelCase_							=scheduler.scale_model_input(lowerCAmelCase,	lowerCAmelCase						)

                     lowerCamelCase_							=model(lowerCAmelCase,	lowerCAmelCase						)

                     lowerCamelCase_							=scheduler.step(lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						)
                     lowerCamelCase_							=output.prev_sample

              lowerCamelCase_							=torch.sum(torch.abs(lowerCAmelCase						)						)
              lowerCamelCase_							=torch.mean(torch.abs(lowerCAmelCase						)						)

              assert abs(result_sum.item() - 0.0_0_0_1_5						) < 1e-2
              assert abs(result_mean.item() - 1.9_869_554_535_034_695e-07						) < 1e-2




