





import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


_snake_case  :   Any						   =  False



class     _UpperCAmelCase   (   unittest.TestCase   ):

   """simple docstring"""


   pass





@nightly
@require_torch_gpu
class     _UpperCAmelCase   (   unittest.TestCase   ):

   """simple docstring"""


   def     lowercase		(    self			:		str					)							->						Dict:
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()






   def     lowercase		(    self			:		List[Any]					)							->						Union[str, Any]:
      __lowerCAmelCase							     =     VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' ,  torch_dtype=torch.floataa					)
      pipe.to(__lowerCamelCase					)
      pipe.set_progress_bar_config(disable=__lowerCamelCase					)

      __lowerCAmelCase							     =     load_image(
          'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'					)

      __lowerCAmelCase							     =     torch.manual_seed(0					)
      __lowerCAmelCase							     =     pipe.dual_guided(
          prompt='first prompt' ,  image=__lowerCamelCase ,  text_to_image_strength=0.75 ,  generator=__lowerCamelCase ,  guidance_scale=7.5 ,  num_inference_steps=2 ,  output_type='numpy' ,  ).images

      with tempfile.TemporaryDirectory() as tmpdirname:
         pipe.save_pretrained(__lowerCamelCase					)
         __lowerCAmelCase							     =     VersatileDiffusionPipeline.from_pretrained(__lowerCamelCase ,  torch_dtype=torch.floataa					)
      pipe.to(__lowerCamelCase					)
      pipe.set_progress_bar_config(disable=__lowerCamelCase					)

      __lowerCAmelCase							     =     generator.manual_seed(0					)
      __lowerCAmelCase							     =     pipe.dual_guided(
          prompt='first prompt' ,  image=__lowerCamelCase ,  text_to_image_strength=0.75 ,  generator=__lowerCamelCase ,  guidance_scale=7.5 ,  num_inference_steps=2 ,  output_type='numpy' ,  ).images

      assert np.abs(image - new_image					).sum() < 1e-5, "Models don't have the same forward pass"






   def     lowercase		(    self			:		Optional[Any]					)							->						str:
      __lowerCAmelCase							     =     VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion' ,  torch_dtype=torch.floataa					)
      pipe.to(__lowerCamelCase					)
      pipe.set_progress_bar_config(disable=__lowerCamelCase					)

      __lowerCAmelCase							     =     '''cyberpunk 2077'''
      __lowerCAmelCase							     =     load_image(
          'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'					)
      __lowerCAmelCase							     =     torch.manual_seed(0					)
      __lowerCAmelCase							     =     pipe.dual_guided(
          prompt=__lowerCamelCase ,  image=__lowerCamelCase ,  text_to_image_strength=0.75 ,  generator=__lowerCamelCase ,  guidance_scale=7.5 ,  num_inference_steps=5_0 ,  output_type='numpy' ,  ).images

      __lowerCAmelCase							     =     image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

      assert image.shape == (1, 5_1_2, 5_1_2, 3)
      __lowerCAmelCase							     =     np.array([0.14_48, 0.16_19, 0.17_41, 0.10_86, 0.11_47, 0.11_28, 0.11_99, 0.11_65, 0.10_01]					)

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

      __lowerCAmelCase							     =     '''A painting of a squirrel eating a burger '''
      __lowerCAmelCase							     =     torch.manual_seed(0					)
      __lowerCAmelCase							     =     pipe.text_to_image(
          prompt=__lowerCamelCase ,  generator=__lowerCamelCase ,  guidance_scale=7.5 ,  num_inference_steps=5_0 ,  output_type='numpy'					).images

      __lowerCAmelCase							     =     image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

      assert image.shape == (1, 5_1_2, 5_1_2, 3)
      __lowerCAmelCase							     =     np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78]					)

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

      __lowerCAmelCase							     =     pipe.image_variation(__lowerCamelCase ,  generator=__lowerCamelCase ,  output_type='numpy'					).images

      __lowerCAmelCase							     =     image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

      assert image.shape == (1, 5_1_2, 5_1_2, 3)
      __lowerCAmelCase							     =     np.array([0.30_76, 0.31_23, 0.32_84, 0.37_82, 0.37_70, 0.38_94, 0.42_97, 0.43_31, 0.44_56]					)

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








import unittest

import numpy as np
import torch

from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad



class     _UpperCAmelCase   (   unittest.TestCase   ):

  """simple docstring"""


  def     lowercase		(    self			:		Any					)							->						Optional[int]:
     __lowerCAmelCase							     =     1_0






  def     lowercase		(    self			:		int					)							->						Union[str, Any]:
     __lowerCAmelCase							     =     [1, 2, 3, 4]
     __lowerCAmelCase							     =     [1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
     self.assertEqual(truncate_or_pad(lowerCAmelCase_ ,  self.block_size ,  0					) ,  lowerCAmelCase_					)






  def     lowercase		(    self			:		Optional[Any]					)							->						List[str]:
     __lowerCAmelCase							     =     [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0]
     __lowerCAmelCase							     =     [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0]
     self.assertEqual(truncate_or_pad(lowerCAmelCase_ ,  self.block_size ,  0					) ,  lowerCAmelCase_					)






  def     lowercase		(    self			:		Any					)							->						Optional[Any]:
     __lowerCAmelCase							     =     [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0, 1_1, 1_2, 1_3]
     __lowerCAmelCase							     =     [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0]
     self.assertEqual(truncate_or_pad(lowerCAmelCase_ ,  self.block_size ,  0					) ,  lowerCAmelCase_					)






  def     lowercase		(    self			:		List[str]					)							->						Any:
     __lowerCAmelCase							     =     'It was the year of Our Lord one thousand seven hundred and\n        seventy-five.\n\nSpiritual revelations were conceded to England at that\n        favoured period, as at this.'
     __lowerCAmelCase     ,     __lowerCAmelCase							     =     process_story(lowerCAmelCase_					)
     self.assertEqual(lowerCAmelCase_ ,  []					)






  def     lowercase		(    self			:		Any					)							->						str:
     __lowerCAmelCase							     =     ''
     __lowerCAmelCase     ,     __lowerCAmelCase							     =     process_story(lowerCAmelCase_					)
     self.assertEqual(lowerCAmelCase_ ,  []					)
     self.assertEqual(lowerCAmelCase_ ,  []					)






  def     lowercase		(    self			:		int					)							->						int:
     __lowerCAmelCase							     =     (
         'It was the year of Our Lord one thousand seven hundred and '
         'seventy-five\n\nSpiritual revelations were conceded to England '
         'at that favoured period, as at this.\n@highlight\n\nIt was the best of times'
     )
     __lowerCAmelCase     ,     __lowerCAmelCase							     =     process_story(lowerCAmelCase_					)

     __lowerCAmelCase							     =     [
         'It was the year of Our Lord one thousand seven hundred and seventy-five.',
         'Spiritual revelations were conceded to England at that favoured period, as at this.',
     ]
     self.assertEqual(lowerCAmelCase_ ,  lowerCAmelCase_					)

     __lowerCAmelCase							     =     ['It was the best of times.']
     self.assertEqual(lowerCAmelCase_ ,  lowerCAmelCase_					)






  def     lowercase		(    self			:		Dict					)							->						Any:
     __lowerCAmelCase							     =     torch.tensor([1, 2, 3, 4]					)
     __lowerCAmelCase							     =     torch.tensor([1, 1, 1, 1]					)
     np.testing.assert_array_equal(build_mask(lowerCAmelCase_ ,  0					).numpy() ,  expected.numpy()					)






  def     lowercase		(    self			:		List[Any]					)							->						Optional[int]:
     __lowerCAmelCase							     =     torch.tensor([1, 2, 3, 4, 2_3, 2_3, 2_3]					)
     __lowerCAmelCase							     =     torch.tensor([1, 1, 1, 1, 0, 0, 0]					)
     np.testing.assert_array_equal(build_mask(lowerCAmelCase_ ,  2_3					).numpy() ,  expected.numpy()					)






  def     lowercase		(    self			:		str					)							->						List[Any]:
     __lowerCAmelCase							     =     torch.tensor([8, 2, 3, 4, 1, 1, 1]					)
     __lowerCAmelCase							     =     torch.tensor([1, 1, 1, 1, 0, 0, 0]					)
     np.testing.assert_array_equal(build_mask(lowerCAmelCase_ ,  1					).numpy() ,  expected.numpy()					)






  def     lowercase		(    self			:		Optional[Any]					)							->						Optional[int]:
     __lowerCAmelCase							     =     1_0_1
     __lowerCAmelCase							     =     torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 1_0_1, 5, 6], [1, 1_0_1, 3, 4, 1_0_1, 6]]					)
     __lowerCAmelCase							     =     torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]]					)

     __lowerCAmelCase							     =     compute_token_type_ids(lowerCAmelCase_ ,  lowerCAmelCase_					)
     np.testing.assert_array_equal(lowerCAmelCase_ ,  lowerCAmelCase_					)

