'''simple docstring'''
import json
import os
import unittest

from transformers import HerbertTokenizer, HerbertTokenizerFast
from transformers.models.herbert.tokenization_herbert import VOCAB_FILES_NAMES
from transformers.testing_utils import get_tests_dir, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin






@require_tokenizers
class       __lowercase		(    _lowerCamelCase      ,				unittest.TestCase    ):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		 =    HerbertTokenizer
     SCREAMING_SNAKE_CASE__		 =    HerbertTokenizerFast
     SCREAMING_SNAKE_CASE__		 =    True







     def lowerCAmelCase  (		self     ):
         super().setUp()

         # Use a simpler test file without japanese/chinese characters
         with open(f"""{get_tests_dir()}/fixtures/sample_text_no_unicode.txt"""  ,      encoding='utf-8'     ) as f_data:
             __UpperCamelCase						:						Tuple		     =	f_data.read().replace('\n\n'  ,      '\n'     ).strip()

         __UpperCamelCase						:						List[str]		     =	[
             '<s>',
             '</s>',
             'l',
             'o',
             'w',
             'e',
             'r',
             's',
             't',
             'i',
             'd',
             'n',
             'w</w>',
             'r</w>',
             't</w>',
             'lo',
             'low',
             'er</w>',
             'low</w>',
             'lowest</w>',
             'newer</w>',
             'wider</w>',
             ',</w>',
             '<unk>',
         ]
         __UpperCamelCase						:						int		     =	dict(zip(_lowerCamelCase  ,      range(len(_lowerCamelCase     )     )     )     )
         __UpperCamelCase						:						Optional[int]		     =	['l o 123', 'lo w 1456', 'e r</w> 1789', '']

         __UpperCamelCase						:						Optional[Any]		     =	os.path.join(self.tmpdirname  ,      VOCAB_FILES_NAMES['vocab_file']     )
         __UpperCamelCase						:						Optional[Any]		     =	os.path.join(self.tmpdirname  ,      VOCAB_FILES_NAMES['merges_file']     )
         with open(self.vocab_file  ,      'w'     ) as fp:
             fp.write(json.dumps(_lowerCamelCase     )     )
         with open(self.merges_file  ,      'w'     ) as fp:
             fp.write('\n'.join(_lowerCamelCase     )     )







     def lowerCAmelCase  (		self  ,      _lowerCamelCase     ):
         __UpperCamelCase						:						int		     =	'lower newer'
         __UpperCamelCase						:						str		     =	'lower newer'
         return input_text, output_text







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						List[Any]		     =	self.tokenizer_class(vocab_file=self.vocab_file  ,      merges_file=self.merges_file     )

         __UpperCamelCase						:						Optional[Any]		     =	'lower'
         __UpperCamelCase						:						Tuple		     =	['low', 'er</w>']
         __UpperCamelCase						:						Any		     =	tokenizer.tokenize(_lowerCamelCase     )
         self.assertListEqual(_lowerCamelCase  ,      _lowerCamelCase     )

         __UpperCamelCase						:						Any		     =	tokens + ['<unk>']
         __UpperCamelCase						:						Optional[int]		     =	[1_6, 1_7, 2_3]
         self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCamelCase     )  ,      _lowerCamelCase     )







     def lowerCAmelCase  (		self     ):
         if not self.test_rust_tokenizer:
             return

         __UpperCamelCase						:						Union[str, Any]		     =	self.get_tokenizer()
         __UpperCamelCase						:						Tuple		     =	self.get_rust_tokenizer()

         __UpperCamelCase						:						Any		     =	'lower,newer'

         __UpperCamelCase						:						Optional[Any]		     =	tokenizer.tokenize(_lowerCamelCase     )
         __UpperCamelCase						:						Optional[Any]		     =	rust_tokenizer.tokenize(_lowerCamelCase     )
         self.assertListEqual(_lowerCamelCase  ,      _lowerCamelCase     )

         __UpperCamelCase						:						Tuple		     =	tokenizer.encode(_lowerCamelCase  ,      add_special_tokens=_lowerCamelCase     )
         __UpperCamelCase						:						Optional[int]		     =	rust_tokenizer.encode(_lowerCamelCase  ,      add_special_tokens=_lowerCamelCase     )
         self.assertListEqual(_lowerCamelCase  ,      _lowerCamelCase     )

         __UpperCamelCase						:						Any		     =	self.get_rust_tokenizer()
         __UpperCamelCase						:						List[Any]		     =	tokenizer.encode(_lowerCamelCase     )
         __UpperCamelCase						:						Dict		     =	rust_tokenizer.encode(_lowerCamelCase     )
         self.assertListEqual(_lowerCamelCase  ,      _lowerCamelCase     )







     @slow
     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Union[str, Any]		     =	self.tokenizer_class.from_pretrained('allegro/herbert-base-cased'     )

         __UpperCamelCase						:						Optional[Any]		     =	tokenizer.encode('konstruowanie sekwencji'  ,      add_special_tokens=_lowerCamelCase     )
         __UpperCamelCase						:						List[Any]		     =	tokenizer.encode('konstruowanie wielu sekwencji'  ,      add_special_tokens=_lowerCamelCase     )

         __UpperCamelCase						:						Tuple		     =	tokenizer.build_inputs_with_special_tokens(_lowerCamelCase     )
         __UpperCamelCase						:						List[str]		     =	tokenizer.build_inputs_with_special_tokens(_lowerCamelCase  ,      _lowerCamelCase     )

         assert encoded_sentence == [0] + text + [2]
         assert encoded_pair == [0] + text + [2] + text_a + [2]




'''simple docstring'''
import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, LDMSuperResolutionPipeline, UNetaDModel, VQModel
from diffusers.utils import PIL_INTERPOLATION, floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch


enable_full_determinism()






class       __lowercase		(    unittest.TestCase    ):







     """simple docstring"""






     @property
     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Dict		     =	1
         __UpperCamelCase						:						Union[str, Any]		     =	3
         __UpperCamelCase						:						Tuple		     =	(3_2, 3_2)

         __UpperCamelCase						:						List[Any]		     =	floats_tensor((batch_size, num_channels) + sizes  ,      rng=random.Random(0     )     ).to(_lowerCamelCase     )
         return image







     @property
     def lowerCAmelCase  (		self     ):
         torch.manual_seed(0     )
         __UpperCamelCase						:						Dict		     =	UNetaDModel(
             block_out_channels=(3_2, 6_4)  ,      layers_per_block=2  ,      sample_size=3_2  ,      in_channels=6  ,      out_channels=3  ,      down_block_types=('DownBlock2D', 'AttnDownBlock2D')  ,      up_block_types=('AttnUpBlock2D', 'UpBlock2D')  ,      )
         return model







     @property
     def lowerCAmelCase  (		self     ):
         torch.manual_seed(0     )
         __UpperCamelCase						:						List[str]		     =	VQModel(
             block_out_channels=[3_2, 6_4]  ,      in_channels=3  ,      out_channels=3  ,      down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']  ,      up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']  ,      latent_channels=3  ,      )
         return model







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Tuple		     =	'cpu'
         __UpperCamelCase						:						Union[str, Any]		     =	self.dummy_uncond_unet
         __UpperCamelCase						:						List[str]		     =	DDIMScheduler()
         __UpperCamelCase						:						Any		     =	self.dummy_vq_model

         __UpperCamelCase						:						Tuple		     =	LDMSuperResolutionPipeline(unet=_lowerCamelCase  ,      vqvae=_lowerCamelCase  ,      scheduler=_lowerCamelCase     )
         ldm.to(_lowerCamelCase     )
         ldm.set_progress_bar_config(disable=_lowerCamelCase     )

         __UpperCamelCase						:						int		     =	self.dummy_image.to(_lowerCamelCase     )

         __UpperCamelCase						:						Union[str, Any]		     =	torch.Generator(device=_lowerCamelCase     ).manual_seed(0     )
         __UpperCamelCase						:						Union[str, Any]		     =	ldm(image=_lowerCamelCase  ,      generator=_lowerCamelCase  ,      num_inference_steps=2  ,      output_type='numpy'     ).images

         __UpperCamelCase						:						str		     =	image[0, -3:, -3:, -1]

         assert image.shape == (1, 6_4, 6_4, 3)
         __UpperCamelCase						:						str		     =	np.array([0.8_6_7_8, 0.8_2_4_5, 0.6_3_8_1, 0.6_8_3_0, 0.4_3_8_5, 0.5_5_9_9, 0.4_6_4_1, 0.6_2_0_1, 0.5_1_5_0]     )

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







     @unittest.skipIf(torch_device != 'cuda'  ,      'This test requires a GPU'     )
     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Optional[Any]		     =	self.dummy_uncond_unet
         __UpperCamelCase						:						Optional[int]		     =	DDIMScheduler()
         __UpperCamelCase						:						Any		     =	self.dummy_vq_model

         # put models in fp16
         __UpperCamelCase						:						str		     =	unet.half()
         __UpperCamelCase						:						Optional[Any]		     =	vqvae.half()

         __UpperCamelCase						:						Optional[int]		     =	LDMSuperResolutionPipeline(unet=_lowerCamelCase  ,      vqvae=_lowerCamelCase  ,      scheduler=_lowerCamelCase     )
         ldm.to(_lowerCamelCase     )
         ldm.set_progress_bar_config(disable=_lowerCamelCase     )

         __UpperCamelCase						:						Dict		     =	self.dummy_image.to(_lowerCamelCase     )

         __UpperCamelCase						:						Optional[Any]		     =	ldm(_lowerCamelCase  ,      num_inference_steps=2  ,      output_type='numpy'     ).images

         assert image.shape == (1, 6_4, 6_4, 3)






@slow
@require_torch
class       __lowercase		(    unittest.TestCase    ):







     """simple docstring"""






     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						List[Any]		     =	load_image(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
             '/vq_diffusion/teddy_bear_pool.png'     )
         __UpperCamelCase						:						List[Any]		     =	init_image.resize((6_4, 6_4)  ,      resample=PIL_INTERPOLATION['lanczos']     )

         __UpperCamelCase						:						List[str]		     =	LDMSuperResolutionPipeline.from_pretrained('duongna/ldm-super-resolution'  ,      device_map='auto'     )
         ldm.set_progress_bar_config(disable=_lowerCamelCase     )

         __UpperCamelCase						:						Union[str, Any]		     =	torch.manual_seed(0     )
         __UpperCamelCase						:						Dict		     =	ldm(image=_lowerCamelCase  ,      generator=_lowerCamelCase  ,      num_inference_steps=2_0  ,      output_type='numpy'     ).images

         __UpperCamelCase						:						List[Any]		     =	image[0, -3:, -3:, -1]

         assert image.shape == (1, 2_5_6, 2_5_6, 3)
         __UpperCamelCase						:						str		     =	np.array([0.7_6_4_4, 0.7_6_7_9, 0.7_6_4_2, 0.7_6_3_3, 0.7_6_6_6, 0.7_5_6_0, 0.7_4_2_5, 0.7_2_5_7, 0.6_9_0_7]     )

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



