


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   SCREAMING_SNAKE_CASE_			(unittest.TestCase						):




       '''simple docstring'''



       def 				_lowerCAmelCase							(						self				:				str		,				__a				:				Union[str, Any]							)					->str:
          lowerCamelCase_						:   str		= 3
          lowerCamelCase_						:   Optional[int]		= 250

          lowerCamelCase_						:   Optional[int]		= ids_tensor((batch_size, length)		,				UpperCamelCase__							)
          lowerCamelCase_						:   List[str]		= torch.ones((batch_size, length)		,				device=UpperCamelCase__		,				dtype=torch.float							) / length
          return input_ids, scores



       def 				_lowerCAmelCase							(						self				:				str							)					->Optional[int]:
          lowerCamelCase_,	lowerCamelCase_						:   Tuple		= self._get_tensors(5							)

          lowerCamelCase_						:   List[Any]		= StoppingCriteriaList(
              [
                  MaxLengthCriteria(max_length=10							),
                  MaxTimeCriteria(max_time=0.1							),
              ]							)

          self.assertFalse(criteria(UpperCamelCase__		,				UpperCamelCase__							)							)

          lowerCamelCase_,	lowerCamelCase_						:   str		= self._get_tensors(9							)
          self.assertFalse(criteria(UpperCamelCase__		,				UpperCamelCase__							)							)

          lowerCamelCase_,	lowerCamelCase_						:   Union[str, Any]		= self._get_tensors(10							)
          self.assertTrue(criteria(UpperCamelCase__		,				UpperCamelCase__							)							)



       def 				_lowerCAmelCase							(						self				:				Tuple							)					->Tuple:
          lowerCamelCase_						:   str		= MaxLengthCriteria(max_length=10							)

          lowerCamelCase_,	lowerCamelCase_						:   Optional[int]		= self._get_tensors(5							)
          self.assertFalse(criteria(UpperCamelCase__		,				UpperCamelCase__							)							)

          lowerCamelCase_,	lowerCamelCase_						:   str		= self._get_tensors(9							)
          self.assertFalse(criteria(UpperCamelCase__		,				UpperCamelCase__							)							)

          lowerCamelCase_,	lowerCamelCase_						:   List[str]		= self._get_tensors(10							)
          self.assertTrue(criteria(UpperCamelCase__		,				UpperCamelCase__							)							)



       def 				_lowerCAmelCase							(						self				:				Dict							)					->Any:
          lowerCamelCase_						:   List[Any]		= MaxNewTokensCriteria(start_length=5		,				max_new_tokens=5							)

          lowerCamelCase_,	lowerCamelCase_						:   Dict		= self._get_tensors(5							)
          self.assertFalse(criteria(UpperCamelCase__		,				UpperCamelCase__							)							)

          lowerCamelCase_,	lowerCamelCase_						:   str		= self._get_tensors(9							)
          self.assertFalse(criteria(UpperCamelCase__		,				UpperCamelCase__							)							)

          lowerCamelCase_,	lowerCamelCase_						:   Any		= self._get_tensors(10							)
          self.assertTrue(criteria(UpperCamelCase__		,				UpperCamelCase__							)							)

          lowerCamelCase_						:   List[Any]		= StoppingCriteriaList([criteria]							)
          self.assertEqual(criteria_list.max_length		,				10							)



       def 				_lowerCAmelCase							(						self				:				Dict							)					->Any:
          lowerCamelCase_,	lowerCamelCase_						:   str		= self._get_tensors(5							)

          lowerCamelCase_						:   Dict		= MaxTimeCriteria(max_time=0.1							)
          self.assertFalse(criteria(UpperCamelCase__		,				UpperCamelCase__							)							)

          lowerCamelCase_						:   Optional[Any]		= MaxTimeCriteria(max_time=0.1		,				initial_timestamp=time.time() - 0.2							)
          self.assertTrue(criteria(UpperCamelCase__		,				UpperCamelCase__							)							)




       def 				_lowerCAmelCase							(						self				:				str							)					->Union[str, Any]:
          validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10							)]							)		,				10							)

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

          lowerCamelCase_						:   Optional[int]		= validate_stopping_criteria(StoppingCriteriaList()		,				11							)

          self.assertEqual(len(UpperCamelCase__							)		,				1							)






'''simple docstring'''






import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionImageVariationPipeline
from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device


_UpperCAmelCase    :     List[Any]         =       False







class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   pass







@slow
@require_torch_gpu
class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   def 	__UpperCAmelCase   (      self      :			List[str]     )  ->			Dict:
        _A    		=  VersatileDiffusionImageVariationPipeline.from_pretrained('shi-labs/versatile-diffusion'     )
        pipe.to(UpperCamelCase__     )
        pipe.set_progress_bar_config(disable=UpperCamelCase__     )

        _A    		=  load_image(
            'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'     )
        _A    		=  torch.manual_seed(0     )
        _A    		=  pipe(
            image=UpperCamelCase__, generator=UpperCamelCase__, guidance_scale=7.5, num_inference_steps=50, output_type='numpy', ).images

        _A    		=  image[0, 2_53:2_56, 2_53:2_56, -1]

        assert image.shape == (1, 5_12, 5_12, 3)
        _A    		=  np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945]     )

        assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1e-2



