




import time
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, torch_device

from ..test_modeling_common import ids_tensor


if is_torch_available():
    import torch

    from transformers.generation import (
        MaxLengthCriteria,
        MaxNewTokensCriteria,
        MaxTimeCriteria,
        StoppingCriteriaList,
        validate_stopping_criteria,
    )
@require_torch
class 			_UpperCamelCase      (				unittest.TestCase		):
      def 	UpperCAmelCase_						(			self   :Optional[int] ,      lowerCamelCase   :int      )     ->			List[str]:
       UpperCAmelCase__							 =						3
       UpperCAmelCase__							 =						250

       UpperCAmelCase__							 =						ids_tensor((batch_size, length) ,      lowerCamelCase      )
       UpperCAmelCase__							 =						torch.ones((batch_size, length) ,      device=lowerCamelCase ,      dtype=torch.float      ) / length
       return input_ids, scores
      def 	UpperCAmelCase_						(			self   :str      )     ->			Optional[int]:
       UpperCAmelCase__ , UpperCAmelCase__							 =						self._get_tensors(5      )

       UpperCAmelCase__							 =						StoppingCriteriaList(
           [
               MaxLengthCriteria(max_length=10      ),
               MaxTimeCriteria(max_time=0.1      ),
           ]      )

       self.assertFalse(criteria(lowerCamelCase ,      lowerCamelCase      )      )

       UpperCAmelCase__ , UpperCAmelCase__							 =						self._get_tensors(9      )
       self.assertFalse(criteria(lowerCamelCase ,      lowerCamelCase      )      )

       UpperCAmelCase__ , UpperCAmelCase__							 =						self._get_tensors(10      )
       self.assertTrue(criteria(lowerCamelCase ,      lowerCamelCase      )      )
      def 	UpperCAmelCase_						(			self   :int      )     ->			str:
       UpperCAmelCase__							 =						MaxLengthCriteria(max_length=10      )

       UpperCAmelCase__ , UpperCAmelCase__							 =						self._get_tensors(5      )
       self.assertFalse(criteria(lowerCamelCase ,      lowerCamelCase      )      )

       UpperCAmelCase__ , UpperCAmelCase__							 =						self._get_tensors(9      )
       self.assertFalse(criteria(lowerCamelCase ,      lowerCamelCase      )      )

       UpperCAmelCase__ , UpperCAmelCase__							 =						self._get_tensors(10      )
       self.assertTrue(criteria(lowerCamelCase ,      lowerCamelCase      )      )
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			List[str]:
       UpperCAmelCase__							 =						MaxNewTokensCriteria(start_length=5 ,      max_new_tokens=5      )

       UpperCAmelCase__ , UpperCAmelCase__							 =						self._get_tensors(5      )
       self.assertFalse(criteria(lowerCamelCase ,      lowerCamelCase      )      )

       UpperCAmelCase__ , UpperCAmelCase__							 =						self._get_tensors(9      )
       self.assertFalse(criteria(lowerCamelCase ,      lowerCamelCase      )      )

       UpperCAmelCase__ , UpperCAmelCase__							 =						self._get_tensors(10      )
       self.assertTrue(criteria(lowerCamelCase ,      lowerCamelCase      )      )

       UpperCAmelCase__							 =						StoppingCriteriaList([criteria]      )
       self.assertEqual(criteria_list.max_length ,      10      )
      def 	UpperCAmelCase_						(			self   :Any      )     ->			int:
       UpperCAmelCase__ , UpperCAmelCase__							 =						self._get_tensors(5      )

       UpperCAmelCase__							 =						MaxTimeCriteria(max_time=0.1      )
       self.assertFalse(criteria(lowerCamelCase ,      lowerCamelCase      )      )

       UpperCAmelCase__							 =						MaxTimeCriteria(max_time=0.1 ,      initial_timestamp=time.time() - 0.2      )
       self.assertTrue(criteria(lowerCamelCase ,      lowerCamelCase      )      )







      def 	UpperCAmelCase_						(			self   :int      )     ->			Dict:
       validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10      )]      ) ,      10      )

       with self.assertWarns(lowerCamelCase      ):
        validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10      )]      ) ,      11      )

       UpperCAmelCase__							 =						validate_stopping_criteria(StoppingCriteriaList() ,      11      )

       self.assertEqual(len(lowerCamelCase      ) ,      1      )











import random
import unittest

import torch

from diffusers import IFInpaintingSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin
@skip_mps
class 			_UpperCamelCase      (				lowerCAmelCase	,       lowerCAmelCase	,       unittest.TestCase		):
      UpperCAmelCase_        =		IFInpaintingSuperResolutionPipeline
      UpperCAmelCase_        =		TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""}
      UpperCAmelCase_        =		TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"""original_image"""}		)
      UpperCAmelCase_        =		PipelineTesterMixin.required_optional_params - {"""latents"""}
      def 	UpperCAmelCase_						(			self   :str      )     ->			Optional[Any]:
       return self._get_superresolution_dummy_components()
      def 	UpperCAmelCase_						(			self   :List[str] ,      lowerCamelCase   :Dict ,      lowerCamelCase   :Optional[Any]=0      )     ->			int:
       if str(lowerCamelCase      ).startswith("mps"      ):
        UpperCAmelCase__							 =						torch.manual_seed(lowerCamelCase      )
       else:
        UpperCAmelCase__							 =						torch.Generator(device=lowerCamelCase      ).manual_seed(lowerCamelCase      )

       UpperCAmelCase__							 =						floats_tensor((1, 3, 16, 16) ,      rng=random.Random(lowerCamelCase      )      ).to(lowerCamelCase      )
       UpperCAmelCase__							 =						floats_tensor((1, 3, 32, 32) ,      rng=random.Random(lowerCamelCase      )      ).to(lowerCamelCase      )
       UpperCAmelCase__							 =						floats_tensor((1, 3, 32, 32) ,      rng=random.Random(lowerCamelCase      )      ).to(lowerCamelCase      )

       UpperCAmelCase__							 =						{
           "prompt": "A painting of a squirrel eating a burger",
           "image": image,
           "original_image": original_image,
           "mask_image": mask_image,
           "generator": generator,
           "num_inference_steps": 2,
           "output_type": "numpy",
       }

       return inputs
      @unittest.skipIf(
          torch_device != "cuda" or not is_xformers_available() ,      reason="XFormers attention is only available with CUDA and `xformers` installed" ,      )
      def 	UpperCAmelCase_						(			self   :Union[str, Any]      )     ->			Dict:
       self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3      )
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			Dict:
       self._test_save_load_optional_components()
      @unittest.skipIf(torch_device != "cuda" ,      reason="float16 requires CUDA"      )
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			Dict:
       # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
       super().test_save_load_floataa(expected_max_diff=1e-1      )
      def 	UpperCAmelCase_						(			self   :Dict      )     ->			str:
       self._test_attention_slicing_forward_pass(expected_max_diff=1e-2      )
      def 	UpperCAmelCase_						(			self   :Dict      )     ->			str:
       self._test_save_load_local()







      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			List[str]:
       self._test_inference_batch_single_identical(
           expected_max_diff=1e-2 ,      )





