


'''simple docstring'''
import unittest

from transformers import GPTSwaTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


UpperCAmelCase_ :     Tuple								=      get_tests_dir('fixtures/test_sentencepiece_with_bytefallback.model')

@require_sentencepiece
@require_tokenizers
class 						_lowerCamelCase					(    _snake_case    ,		unittest.TestCase				):




 '''simple docstring'''
 __lowercase				:		Dict    							=						GPTSwaTokenizer
 __lowercase				:		List[str]    							=						False
 __lowercase				:		List[Any]    							=						True
 __lowercase				:		List[Any]    							=						False


 def  snake_case__   (      self						):

  """simple docstring"""







  super().setUp()

  # We have a SentencePiece fixture for testing
  __A  :		Dict       =			GPTSwaTokenizer(snake_case_      ,      eos_token='<unk>'      ,      bos_token='<unk>'      ,      pad_token='<unk>'						)

  tokenizer.save_pretrained(self.tmpdirname						)


 def  snake_case__   (      self      ,      __lowercase						):

  """simple docstring"""







  __A  :		List[Any]       =			"This is a test"
  __A  :		str       =			"This is a test"
  return input_text, output_text


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Dict       =			"<s>"
  __A  :		List[Any]       =			1

  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  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Dict       =			list(self.get_tokenizer().get_vocab().keys()						)

  self.assertEqual(vocab_keys[0]      ,      '<unk>'						)
  self.assertEqual(vocab_keys[1]      ,      '<s>'						)
  self.assertEqual(vocab_keys[-1]      ,      'j'						)
  self.assertEqual(len(snake_case_						)      ,      2_000						)


 def  snake_case__   (      self						):

  """simple docstring"""







  self.assertEqual(self.get_tokenizer().vocab_size      ,      2_000						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[Any]       =			GPTSwaTokenizer(snake_case_						)

  __A  :		Optional[int]       =			tokenizer.tokenize('This is a test'						)
  self.assertListEqual(snake_case_      ,      ['▁This', '▁is', '▁a', '▁t', 'est']						)

  self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case_						)      ,      [465, 287, 265, 631, 842]						)

  __A  :		Any       =			tokenizer.tokenize('I was born in 92000, and this is falsé.'						)
  # fmt: off
  self.assertListEqual(
      snake_case_      ,      ['▁I', '▁was', '▁bor', 'n', '▁in', '▁', '<0x39>', '2', '0', '0', '0', ',', '▁and', '▁this', '▁is', '▁f', 'al', 's', '<0xC3>', '<0xA9>', '.']      ,      )
  # fmt: on

  __A  :		Optional[int]       =			tokenizer.convert_tokens_to_ids(snake_case_						)
  self.assertListEqual(
      snake_case_      ,      [262, 272, 1_525, 286, 271, 268, 60, 916, 633, 633, 633, 259, 266, 301, 287, 384, 367, 263, 198, 172, 260]      ,      )

  __A  :		Optional[int]       =			tokenizer.convert_ids_to_tokens(snake_case_						)
  # fmt: off
  self.assertListEqual(
      snake_case_      ,      ['▁I', '▁was', '▁bor', 'n', '▁in', '▁', '<0x39>', '2', '0', '0', '0', ',', '▁and', '▁this', '▁is', '▁f', 'al', 's', '<0xC3>', '<0xA9>', '.']						)
  # fmt: on


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Optional[Any]       =			GPTSwaTokenizer(snake_case_						)
  __A  :		Optional[int]       =			["This is a test", "I was born in 92000, and this is falsé."]
  __A  :		List[str]       =			[
      [465, 287, 265, 631, 842],
      [262, 272, 1_525, 286, 271, 268, 60, 916, 633, 633, 633, 259, 266, 301, 287, 384, 367, 263, 198, 172, 260],
  ]

  # Test that encode_fast returns the same as tokenize + convert_tokens_to_ids
  for text, expected_ids in zip(snake_case_      ,      snake_case_						):
   self.assertListEqual(tokenizer.encode_fast(snake_case_						)      ,      snake_case_						)

  # Test that decode_fast returns the input text
  for text, token_ids in zip(snake_case_      ,      snake_case_						):
   self.assertEqual(tokenizer.decode_fast(snake_case_						)      ,      snake_case_						)







 @slow
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Tuple       =			[
      "<|python|>def fibonacci(n)\n    if n < 0:\n        print('Incorrect input')",
      "Hey there, how are you doing this fine day?",
      "This is a text with a trailing spaces followed by a dot     .",
      "Häj sväjs lillebrör! =)",
      "Det är inget fel på Mr. Cool",
  ]

  # fmt: off
  __A  :		Optional[Any]       =			{"input_ids": [[63_423, 5, 6_811, 14_954, 282, 816, 3_821, 63_466, 63_425, 63_462, 18, 63_978, 678, 301, 1_320, 63_423, 63_455, 63_458, 18, 63_982, 4_246, 3_940, 1_901, 47_789, 5_547, 18_994], [19_630, 1_100, 63_446, 1_342, 633, 544, 4_488, 593, 5_102, 2_416, 63_495, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1_652, 428, 268, 1_936, 515, 268, 58_593, 22_413, 9_106, 546, 268, 33_213, 63_979, 698, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [55_130, 63_450, 924, 63_449, 2_249, 4_062, 1_558, 318, 63_504, 21_498, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [509, 377, 2_827, 2_559, 332, 6_575, 63_443, 26_801, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "token_type_ids": [[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]], "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, 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, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [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], [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]]}
  # fmt: on
  self.tokenizer_integration_test_util(
      expected_encoding=snake_case_      ,      model_name='AI-Sweden/gpt-sw3-126m'      ,      sequences=snake_case_      ,      )










'''simple docstring'''
import gc
import unittest

import numpy as np
import torch

from diffusers import (
    AudioDiffusionPipeline,
    AutoencoderKL,
    DDIMScheduler,
    DDPMScheduler,
    DiffusionPipeline,
    Mel,
    UNetaDConditionModel,
    UNetaDModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()

class 						_lowerCamelCase					(    unittest.TestCase				):




 '''simple docstring'''


 def  snake_case__   (      self						):

  """simple docstring"""







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


 @property
 def  snake_case__   (      self						):

  """simple docstring"""







  torch.manual_seed(0						)
  __A  :		Optional[int]       =			UNetaDModel(
      sample_size=(32, 64)      ,      in_channels=1      ,      out_channels=1      ,      layers_per_block=2      ,      block_out_channels=(128, 128)      ,      down_block_types=('AttnDownBlock2D', 'DownBlock2D')      ,      up_block_types=('UpBlock2D', 'AttnUpBlock2D')      ,      )
  return model


 @property
 def  snake_case__   (      self						):

  """simple docstring"""







  torch.manual_seed(0						)
  __A  :		Dict       =			UNetaDConditionModel(
      sample_size=(64, 32)      ,      in_channels=1      ,      out_channels=1      ,      layers_per_block=2      ,      block_out_channels=(128, 128)      ,      down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D')      ,      up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D')      ,      cross_attention_dim=10      ,      )
  return model


 @property
 def  snake_case__   (      self						):

  """simple docstring"""







  torch.manual_seed(0						)
  __A  :		Union[str, Any]       =			AutoencoderKL(
      sample_size=(128, 64)      ,      in_channels=1      ,      out_channels=1      ,      latent_channels=1      ,      layers_per_block=2      ,      block_out_channels=(128, 128)      ,      down_block_types=('DownEncoderBlock2D', 'DownEncoderBlock2D')      ,      up_block_types=('UpDecoderBlock2D', 'UpDecoderBlock2D')      ,      )
  __A  :		Union[str, Any]       =			UNetaDModel(
      sample_size=(64, 32)      ,      in_channels=1      ,      out_channels=1      ,      layers_per_block=2      ,      block_out_channels=(128, 128)      ,      down_block_types=('AttnDownBlock2D', 'DownBlock2D')      ,      up_block_types=('UpBlock2D', 'AttnUpBlock2D')      ,      )
  return vqvae, unet







 @slow
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		str       =			'cpu'  # ensure determinism for the device-dependent torch.Generator
  __A  :		int       =			Mel(
      x_res=self.dummy_unet.config.sample_size[1]      ,      y_res=self.dummy_unet.config.sample_size[0]      ,      )

  __A  :		Tuple       =			DDPMScheduler()
  __A  :		Optional[int]       =			AudioDiffusionPipeline(vqvae=__lowercase      ,      unet=self.dummy_unet      ,      mel=__lowercase      ,      scheduler=__lowercase						)
  __A  :		Dict       =			pipe.to(__lowercase						)
  pipe.set_progress_bar_config(disable=__lowercase						)

  __A  :		Tuple       =			torch.Generator(device=__lowercase						).manual_seed(42						)
  __A  :		Tuple       =			pipe(generator=__lowercase      ,      steps=4						)
  __A  :		Union[str, Any]       =			output.audios[0]
  __A  :		Dict       =			output.images[0]

  __A  :		Optional[Any]       =			torch.Generator(device=__lowercase						).manual_seed(42						)
  __A  :		Optional[int]       =			pipe(generator=__lowercase      ,      steps=4      ,      return_dict=__lowercase						)
  __A  :		int       =			output[0][0]

  assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length)
  assert (
      image.height == self.dummy_unet.config.sample_size[0]
      and image.width == self.dummy_unet.config.sample_size[1]
  )
  __A  :		Optional[int]       =			np.frombuffer(image.tobytes()      ,      dtype='uint8'						)[:10]
  __A  :		List[Any]       =			np.frombuffer(image_from_tuple.tobytes()      ,      dtype='uint8'						)[:10]
  __A  :		Optional[Any]       =			np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127]						)

  assert np.abs(image_slice.flatten() - expected_slice						).max() == 0
  assert np.abs(image_from_tuple_slice.flatten() - expected_slice						).max() == 0

  __A  :		Optional[int]       =			Mel(
      x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1]      ,      y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0]      ,      )

  __A  :		Optional[Any]       =			DDIMScheduler()
  __A  :		List[Any]       =			self.dummy_vqvae_and_unet
  __A  :		int       =			AudioDiffusionPipeline(
      vqvae=self.dummy_vqvae_and_unet[0]      ,      unet=dummy_vqvae_and_unet[1]      ,      mel=__lowercase      ,      scheduler=__lowercase						)
  __A  :		Dict       =			pipe.to(__lowercase						)
  pipe.set_progress_bar_config(disable=__lowercase						)

  np.random.seed(0						)
  __A  :		Dict       =			np.random.uniform(-1      ,      1      ,      ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)						)
  __A  :		Dict       =			torch.Generator(device=__lowercase						).manual_seed(42						)
  __A  :		List[Any]       =			pipe(raw_audio=__lowercase      ,      generator=__lowercase      ,      start_step=5      ,      steps=10						)
  __A  :		Tuple       =			output.images[0]

  assert (
      image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0]
      and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1]
  )
  __A  :		int       =			np.frombuffer(image.tobytes()      ,      dtype='uint8'						)[:10]
  __A  :		List[str]       =			np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121]						)

  assert np.abs(image_slice.flatten() - expected_slice						).max() == 0

  __A  :		Any       =			self.dummy_unet_condition
  __A  :		Optional[int]       =			AudioDiffusionPipeline(
      vqvae=self.dummy_vqvae_and_unet[0]      ,      unet=__lowercase      ,      mel=__lowercase      ,      scheduler=__lowercase						)
  __A  :		Union[str, Any]       =			pipe.to(__lowercase						)
  pipe.set_progress_bar_config(disable=__lowercase						)

  np.random.seed(0						)
  __A  :		Dict       =			torch.rand((1, 1, 10)						)
  __A  :		Any       =			pipe(generator=__lowercase      ,      encoding=__lowercase						)
  __A  :		Union[str, Any]       =			output.images[0]
  __A  :		List[Any]       =			np.frombuffer(image.tobytes()      ,      dtype='uint8'						)[:10]
  __A  :		int       =			np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111]						)

  assert np.abs(image_slice.flatten() - expected_slice						).max() == 0


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




 '''simple docstring'''


 def  snake_case__   (      self						):

  """simple docstring"""







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







 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Union[str, Any]       =			torch_device

  __A  :		Union[str, Any]       =			DiffusionPipeline.from_pretrained('teticio/audio-diffusion-ddim-256'						)
  __A  :		Dict       =			pipe.to(__lowercase						)
  pipe.set_progress_bar_config(disable=__lowercase						)

  __A  :		Any       =			torch.Generator(device=__lowercase						).manual_seed(42						)
  __A  :		Optional[Any]       =			pipe(generator=__lowercase						)
  __A  :		Union[str, Any]       =			output.audios[0]
  __A  :		List[Any]       =			output.images[0]

  assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length)
  assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1]
  __A  :		Union[str, Any]       =			np.frombuffer(image.tobytes()      ,      dtype='uint8'						)[:10]
  __A  :		Optional[Any]       =			np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26]						)

  assert np.abs(image_slice.flatten() - expected_slice						).max() == 0






