






import unittest

from transformers import AlbertTokenizer, AlbertTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


lowerCamelCase     :							Dict          =get_tests_dir('''fixtures/spiece.model''')





@require_sentencepiece
@require_tokenizers
class       __a  (   A__	,       unittest.TestCase	):
 _lowerCAmelCase	:	Tuple				=			AlbertTokenizer
 _lowerCAmelCase	:	Union[str, Any]				=			AlbertTokenizerFast
 _lowerCAmelCase	:	List[Any]				=			True
 _lowerCAmelCase	:	List[Any]				=			True
 _lowerCAmelCase	:	Optional[Any]				=			True







 def  __lowercase (		self :       Union[str, Any]						):


      '''simple docstring'''






      super().setUp()

      # We have a SentencePiece fixture for testing
      UpperCamelCase__      :       int	    =  AlbertTokenizer(_snake_case						)
      tokenizer.save_pretrained(self.tmpdirname						)







 def  __lowercase (		self :       Dict							,       SCREAMING_SNAKE_CASE :       Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__      :       Union[str, Any]	    =  "this is a test"
      UpperCamelCase__      :       Union[str, Any]	    =  "this is a test"
      return input_text, output_text







 def  __lowercase (		self :       str						):


      '''simple docstring'''






      UpperCamelCase__      :       int	    =  "<pad>"
      UpperCamelCase__      :       str	    =  0

      self.assertEqual(self.get_tokenizer()._convert_token_to_id(_snake_case						)							,       _snake_case						)
      self.assertEqual(self.get_tokenizer()._convert_id_to_token(_snake_case						)							,       _snake_case						)







 def  __lowercase (		self :       int						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[int]	    =  list(self.get_tokenizer().get_vocab().keys()						)

      self.assertEqual(vocab_keys[0]							,       "<pad>"						)
      self.assertEqual(vocab_keys[1]							,       "<unk>"						)
      self.assertEqual(vocab_keys[-1]							,       "▁eloquent"						)
      self.assertEqual(len(_snake_case						)							,       3_00_00						)







 def  __lowercase (		self :       Any						):


      '''simple docstring'''






      self.assertEqual(self.get_tokenizer().vocab_size							,       3_00_00						)







 def  __lowercase (		self :       Optional[Any]						):


      '''simple docstring'''






      if not self.test_rust_tokenizer:
           return

      UpperCamelCase__      :       List[str]	    =  self.get_tokenizer()
      UpperCamelCase__      :       Union[str, Any]	    =  self.get_rust_tokenizer()

      UpperCamelCase__      :       Any	    =  "I was born in 92000, and this is falsé."

      UpperCamelCase__      :       List[str]	    =  tokenizer.tokenize(_snake_case						)
      UpperCamelCase__      :       List[Any]	    =  rust_tokenizer.tokenize(_snake_case						)
      self.assertListEqual(_snake_case							,       _snake_case						)

      UpperCamelCase__      :       Dict	    =  tokenizer.encode(_snake_case							,       add_special_tokens=_snake_case						)
      UpperCamelCase__      :       int	    =  rust_tokenizer.encode(_snake_case							,       add_special_tokens=_snake_case						)
      self.assertListEqual(_snake_case							,       _snake_case						)

      UpperCamelCase__      :       List[Any]	    =  self.get_rust_tokenizer()
      UpperCamelCase__      :       List[str]	    =  tokenizer.encode(_snake_case						)
      UpperCamelCase__      :       Optional[int]	    =  rust_tokenizer.encode(_snake_case						)
      self.assertListEqual(_snake_case							,       _snake_case						)







 def  __lowercase (		self :       Tuple						):


      '''simple docstring'''






      UpperCamelCase__      :       int	    =  AlbertTokenizer(_snake_case							,       keep_accents=_snake_case						)

      UpperCamelCase__      :       int	    =  tokenizer.tokenize("This is a test"						)
      self.assertListEqual(_snake_case							,       ["▁this", "▁is", "▁a", "▁test"]						)

      self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case						)							,       [48, 25, 21, 12_89]						)

      UpperCamelCase__      :       List[Any]	    =  tokenizer.tokenize("I was born in 92000, and this is falsé."						)
      self.assertListEqual(
          _snake_case							,       ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", "."]						)
      UpperCamelCase__      :       Optional[int]	    =  tokenizer.convert_tokens_to_ids(_snake_case						)
      self.assertListEqual(_snake_case							,       [31, 23, 3_86, 19, 5_61, 30_50, 15, 17, 48, 25, 82_56, 18, 1, 9]						)

      UpperCamelCase__      :       List[str]	    =  tokenizer.convert_ids_to_tokens(_snake_case						)
      self.assertListEqual(
          _snake_case							,       ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "."]							,       )







 def  __lowercase (		self :       Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__      :       str	    =  AlbertTokenizer(_snake_case						)

      UpperCamelCase__      :       List[str]	    =  tokenizer.encode("sequence builders"						)
      UpperCamelCase__      :       int	    =  tokenizer.encode("multi-sequence build"						)

      UpperCamelCase__      :       List[Any]	    =  tokenizer.build_inputs_with_special_tokens(_snake_case						)
      UpperCamelCase__      :       Any	    =  tokenizer.build_inputs_with_special_tokens(_snake_case							,       _snake_case						)

      assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
      assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [
          tokenizer.sep_token_id
      ]







 @slow
 def  __lowercase (		self :       List[Any]						):


      '''simple docstring'''






      UpperCamelCase__      :       List[str]	    =  {"attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "input_ids": [[2, 2_19_70, 13, 5, 60_92, 1_67, 28, 71_03, 21_53, 6_73, 8, 70_28, 1_20_51, 18, 17, 71_03, 21_53, 6_73, 8, 35_15, 1_86_84, 8, 44_61, 6, 19_27, 2_97, 8, 1_20_60, 26_07, 18, 13, 5, 44_61, 15, 1_05_38, 38, 8, 1_35, 15, 8_22, 58, 15, 9_93, 1_03_63, 15, 14_60, 80_05, 44_61, 15, 9_93, 2_55, 23_28, 9, 9, 9, 6, 26, 11_12, 8_16, 32_60, 13, 5, 1_03, 23_77, 6, 17, 11_12, 8_16, 27_82, 13, 5, 1_03, 1_06_41, 6, 29, 84, 25_12, 24_30, 7_82, 1_86_84, 27_61, 19, 8_08, 24_30, 25_56, 17, 8_55, 14_80, 94_77, 40_91, 1_28, 1_17_12, 15, 71_03, 21_53, 6_73, 17, 2_48_83, 99_90, 9, 3], [2, 1_15_02, 25, 10_06, 20, 7_82, 8, 1_18_09, 8_55, 17_32, 1_93_93, 1_86_67, 37, 3_67, 2_10_18, 69, 18_54, 34, 1_18_60, 1_91_24, 27, 1_56, 2_25, 17, 1_93, 41_41, 19, 65, 91_24, 9, 3, 0, 0, 0, 0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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
      # fmt: on

      self.tokenizer_integration_test_util(
          expected_encoding=_snake_case							,       model_name="albert-base-v2"							,       revision="6b6560eaf5ff2e250b00c50f380c5389a9c2d82e"							,       )







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


lowerCamelCase     :							Optional[Any]    =False





class       __a  (   unittest.TestCase	):
 pass







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







 def  __lowercase (		self :       Any						):


      '''simple docstring'''






      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()







 def  __lowercase (		self :       Dict						):


      '''simple docstring'''






      UpperCamelCase__      :       int	    =  VersatileDiffusionPipeline.from_pretrained("shi-labs/versatile-diffusion"							,       torch_dtype=torch.floataa						)
      pipe.to(SCREAMING_SNAKE_CASE						)
      pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE						)

      UpperCamelCase__      :       Optional[int]	    =  load_image(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"						)

      UpperCamelCase__      :       Dict	    =  torch.manual_seed(0						)
      UpperCamelCase__      :       str	    =  pipe.dual_guided(
          prompt="first prompt"							,       image=SCREAMING_SNAKE_CASE							,       text_to_image_strength=0.7_5							,       generator=SCREAMING_SNAKE_CASE							,       guidance_scale=7.5							,       num_inference_steps=2							,       output_type="numpy"							,       ).images

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

      UpperCamelCase__      :       int	    =  generator.manual_seed(0						)
      UpperCamelCase__      :       Dict	    =  pipe.dual_guided(
          prompt="first prompt"							,       image=SCREAMING_SNAKE_CASE							,       text_to_image_strength=0.7_5							,       generator=SCREAMING_SNAKE_CASE							,       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[int]						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[int]	    =  VersatileDiffusionPipeline.from_pretrained("shi-labs/versatile-diffusion"							,       torch_dtype=torch.floataa						)
      pipe.to(SCREAMING_SNAKE_CASE						)
      pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE						)

      UpperCamelCase__      :       Tuple	    =  "cyberpunk 2077"
      UpperCamelCase__      :       Any	    =  load_image(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"						)
      UpperCamelCase__      :       int	    =  torch.manual_seed(0						)
      UpperCamelCase__      :       Any	    =  pipe.dual_guided(
          prompt=SCREAMING_SNAKE_CASE							,       image=SCREAMING_SNAKE_CASE							,       text_to_image_strength=0.7_5							,       generator=SCREAMING_SNAKE_CASE							,       guidance_scale=7.5							,       num_inference_steps=50							,       output_type="numpy"							,       ).images

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

      assert image.shape == (1, 5_12, 5_12, 3)
      UpperCamelCase__      :       Optional[int]	    =  np.array([0.1_4_4_8, 0.1_6_1_9, 0.1_7_4_1, 0.1_0_8_6, 0.1_1_4_7, 0.1_1_2_8, 0.1_1_9_9, 0.1_1_6_5, 0.1_0_0_1]						)

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

      UpperCamelCase__      :       List[str]	    =  "A painting of a squirrel eating a burger "
      UpperCamelCase__      :       List[Any]	    =  torch.manual_seed(0						)
      UpperCamelCase__      :       Dict	    =  pipe.text_to_image(
          prompt=SCREAMING_SNAKE_CASE							,       generator=SCREAMING_SNAKE_CASE							,       guidance_scale=7.5							,       num_inference_steps=50							,       output_type="numpy"						).images

      UpperCamelCase__      :       Union[str, Any]	    =  image[0, 2_53:2_56, 2_53:2_56, -1]

      assert image.shape == (1, 5_12, 5_12, 3)
      UpperCamelCase__      :       Dict	    =  np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8]						)

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

      UpperCamelCase__      :       Any	    =  pipe.image_variation(SCREAMING_SNAKE_CASE							,       generator=SCREAMING_SNAKE_CASE							,       output_type="numpy"						).images

      UpperCamelCase__      :       Optional[Any]	    =  image[0, 2_53:2_56, 2_53:2_56, -1]

      assert image.shape == (1, 5_12, 5_12, 3)
      UpperCamelCase__      :       int	    =  np.array([0.3_0_7_6, 0.3_1_2_3, 0.3_2_8_4, 0.3_7_8_2, 0.3_7_7_0, 0.3_8_9_4, 0.4_2_9_7, 0.4_3_3_1, 0.4_4_5_6]						)

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