




'''simple docstring'''







import os
import pickle
import tempfile
import unittest
from typing import Callable, Optional

import numpy as np

from transformers import (
    BatchEncoding,
    BertTokenizer,
    BertTokenizerFast,
    PreTrainedTokenizer,
    PreTrainedTokenizerFast,
    TensorType,
    TokenSpan,
    is_tokenizers_available,
)
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import CaptureStderr, require_flax, require_tf, require_tokenizers, require_torch, slow


if is_tokenizers_available():
						from tokenizers import Tokenizer
						from tokenizers.models import WordPiece





class      lowerCamelCase_							(       unittest.TestCase							):




					'''simple docstring'''






					def 				__SCREAMING_SNAKE_CASE	(    self,  __UpperCamelCase ):
							_snake_case	       =       list(tokenizer_class.max_model_input_sizes.keys() )
							for model_name in sa_models[:1]:
									_snake_case	       =       tokenizer_class.from_pretrained(UpperCAmelCase_ )
									self.assertIsNotNone(UpperCAmelCase_ )
									self.assertIsInstance(UpperCAmelCase_,  UpperCAmelCase_ )
									self.assertIsInstance(UpperCAmelCase_,  UpperCAmelCase_ )

									for special_tok in tokenizer.all_special_tokens:
											self.assertIsInstance(UpperCAmelCase_,  UpperCAmelCase_ )
											_snake_case	       =       tokenizer.convert_tokens_to_ids(UpperCAmelCase_ )
											self.assertIsInstance(UpperCAmelCase_,  UpperCAmelCase_ )






					def 				__SCREAMING_SNAKE_CASE	(    self,  __UpperCamelCase,  __UpperCamelCase = None ):
							_snake_case	       =       pickle.dumps(UpperCAmelCase_ )
							self.assertIsNotNone(UpperCAmelCase_ )

							_snake_case	       =       pickle.loads(UpperCAmelCase_ )

							# Ensure is_fast is correctly restored
							self.assertEqual(be_restored.is_fast,  be_original.is_fast )

							# Ensure encodings are potentially correctly restored
							if be_original.is_fast:
									self.assertIsNotNone(be_restored.encodings )
							else:
									self.assertIsNone(be_restored.encodings )

							# Ensure the keys are the same
							for original_v, restored_v in zip(be_original.values(),  be_restored.values() ):
									if equal_op:
											self.assertTrue(equal_op(UpperCAmelCase_,  UpperCAmelCase_ ) )
									else:
											self.assertEqual(UpperCAmelCase_,  UpperCAmelCase_ )






					@slow
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							self.check_tokenizer_from_pretrained(UpperCAmelCase_ )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							self.assertEqual(TensorType("tf" ),  TensorType.TENSORFLOW )
							self.assertEqual(TensorType("pt" ),  TensorType.PYTORCH )
							self.assertEqual(TensorType("np" ),  TensorType.NUMPY )






					@require_tokenizers
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							import numpy as np

							_snake_case	       =       BertTokenizer.from_pretrained("bert-base-cased" )
							_snake_case	       =       BertTokenizerFast.from_pretrained("bert-base-cased" )

							# Python no tensor
							with self.subTest("BatchEncoding (Python, return_tensors=None)" ):
									self.assert_dump_and_restore(tokenizer_p("Small example to encode" ) )

							with self.subTest("BatchEncoding (Python, return_tensors=NUMPY)" ):
									self.assert_dump_and_restore(
									    tokenizer_p("Small example to encode",  return_tensors=TensorType.NUMPY ),  np.array_equal )

							with self.subTest("BatchEncoding (Rust, return_tensors=None)" ):
									self.assert_dump_and_restore(tokenizer_r("Small example to encode" ) )

							with self.subTest("BatchEncoding (Rust, return_tensors=NUMPY)" ):
									self.assert_dump_and_restore(
									    tokenizer_r("Small example to encode",  return_tensors=TensorType.NUMPY ),  np.array_equal )






					@require_tf
					@require_tokenizers
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							import tensorflow as tf

							def tf_array_equals(__UpperCamelCase,  __UpperCamelCase ):
									return tf.reduce_all(tf.equal(UpperCAmelCase_,  UpperCAmelCase_ ) )

							_snake_case	       =       BertTokenizer.from_pretrained("bert-base-cased" )
							_snake_case	       =       BertTokenizerFast.from_pretrained("bert-base-cased" )

							with self.subTest("BatchEncoding (Python, return_tensors=TENSORFLOW)" ):
									self.assert_dump_and_restore(
									    tokenizer_p("Small example to encode",  return_tensors=TensorType.TENSORFLOW ),  UpperCAmelCase_ )

							with self.subTest("BatchEncoding (Rust, return_tensors=TENSORFLOW)" ):
									self.assert_dump_and_restore(
									    tokenizer_r("Small example to encode",  return_tensors=TensorType.TENSORFLOW ),  UpperCAmelCase_ )






					@require_torch
					@require_tokenizers
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							import torch

							_snake_case	       =       BertTokenizer.from_pretrained("bert-base-cased" )
							_snake_case	       =       BertTokenizerFast.from_pretrained("bert-base-cased" )

							with self.subTest("BatchEncoding (Python, return_tensors=PYTORCH)" ):
									self.assert_dump_and_restore(
									    tokenizer_p("Small example to encode",  return_tensors=TensorType.PYTORCH ),  torch.equal )

							with self.subTest("BatchEncoding (Rust, return_tensors=PYTORCH)" ):
									self.assert_dump_and_restore(
									    tokenizer_r("Small example to encode",  return_tensors=TensorType.PYTORCH ),  torch.equal )






					@require_tokenizers
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       BertTokenizer.from_pretrained("bert-base-cased" )
							_snake_case	       =       BertTokenizerFast.from_pretrained("bert-base-cased" )

							with self.subTest("Python Tokenizer" ):
									self.assertFalse(tokenizer_p("Small example to_encode" ).is_fast )

							with self.subTest("Rust Tokenizer" ):
									self.assertTrue(tokenizer_r("Small example to_encode" ).is_fast )






					@require_tokenizers
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       BertTokenizerFast.from_pretrained("bert-base-cased" )
							_snake_case	       =       tokenizer_r(["Test", "\xad", "test"],  is_split_into_words=UpperCAmelCase_ )

							self.assertEqual(encoded.word_to_tokens(0 ),  TokenSpan(start=1,  end=2 ) )
							self.assertEqual(encoded.word_to_tokens(1 ),  UpperCAmelCase_ )
							self.assertEqual(encoded.word_to_tokens(2 ),  TokenSpan(start=2,  end=3 ) )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       BatchEncoding({"inputs": [[1, 2, 3], [4, 5, 6]], "labels": [0, 1]} )
							_snake_case	       =       batch.convert_to_tensors(tensor_type="np" )
							self.assertEqual(tensor_batch["inputs"].shape,  (2, 3) )
							self.assertEqual(tensor_batch["labels"].shape,  (2,) )
							# test converting the converted
							with CaptureStderr() as cs:
									_snake_case	       =       batch.convert_to_tensors(tensor_type="np" )
							self.assertFalse(len(cs.err ),  msg=f'''should have no warning, but got {cs.err}''' )

							_snake_case	       =       BatchEncoding({"inputs": [1, 2, 3], "labels": 0} )
							_snake_case	       =       batch.convert_to_tensors(tensor_type="np",  prepend_batch_axis=UpperCAmelCase_ )
							self.assertEqual(tensor_batch["inputs"].shape,  (1, 3) )
							self.assertEqual(tensor_batch["labels"].shape,  (1,) )






					@require_torch
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       BatchEncoding({"inputs": [[1, 2, 3], [4, 5, 6]], "labels": [0, 1]} )
							_snake_case	       =       batch.convert_to_tensors(tensor_type="pt" )
							self.assertEqual(tensor_batch["inputs"].shape,  (2, 3) )
							self.assertEqual(tensor_batch["labels"].shape,  (2,) )
							# test converting the converted
							with CaptureStderr() as cs:
									_snake_case	       =       batch.convert_to_tensors(tensor_type="pt" )
							self.assertFalse(len(cs.err ),  msg=f'''should have no warning, but got {cs.err}''' )

							_snake_case	       =       BatchEncoding({"inputs": [1, 2, 3], "labels": 0} )
							_snake_case	       =       batch.convert_to_tensors(tensor_type="pt",  prepend_batch_axis=UpperCAmelCase_ )
							self.assertEqual(tensor_batch["inputs"].shape,  (1, 3) )
							self.assertEqual(tensor_batch["labels"].shape,  (1,) )






					@require_tf
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       BatchEncoding({"inputs": [[1, 2, 3], [4, 5, 6]], "labels": [0, 1]} )
							_snake_case	       =       batch.convert_to_tensors(tensor_type="tf" )
							self.assertEqual(tensor_batch["inputs"].shape,  (2, 3) )
							self.assertEqual(tensor_batch["labels"].shape,  (2,) )
							# test converting the converted
							with CaptureStderr() as cs:
									_snake_case	       =       batch.convert_to_tensors(tensor_type="tf" )
							self.assertFalse(len(cs.err ),  msg=f'''should have no warning, but got {cs.err}''' )

							_snake_case	       =       BatchEncoding({"inputs": [1, 2, 3], "labels": 0} )
							_snake_case	       =       batch.convert_to_tensors(tensor_type="tf",  prepend_batch_axis=UpperCAmelCase_ )
							self.assertEqual(tensor_batch["inputs"].shape,  (1, 3) )
							self.assertEqual(tensor_batch["labels"].shape,  (1,) )






					@require_flax
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       BatchEncoding({"inputs": [[1, 2, 3], [4, 5, 6]], "labels": [0, 1]} )
							_snake_case	       =       batch.convert_to_tensors(tensor_type="jax" )
							self.assertEqual(tensor_batch["inputs"].shape,  (2, 3) )
							self.assertEqual(tensor_batch["labels"].shape,  (2,) )
							# test converting the converted
							with CaptureStderr() as cs:
									_snake_case	       =       batch.convert_to_tensors(tensor_type="jax" )
							self.assertFalse(len(cs.err ),  msg=f'''should have no warning, but got {cs.err}''' )

							_snake_case	       =       BatchEncoding({"inputs": [1, 2, 3], "labels": 0} )
							_snake_case	       =       batch.convert_to_tensors(tensor_type="jax",  prepend_batch_axis=UpperCAmelCase_ )
							self.assertEqual(tensor_batch["inputs"].shape,  (1, 3) )
							self.assertEqual(tensor_batch["labels"].shape,  (1,) )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       [{'input_ids': np.array([0, 1, 2] )}, {'input_ids': np.array([0, 1, 2, 3] )}]
							_snake_case	       =       BertTokenizer.from_pretrained("bert-base-cased" )

							_snake_case	       =       tokenizer.pad(UpperCAmelCase_,  padding=UpperCAmelCase_ )
							self.assertTrue(isinstance(batch["input_ids"],  np.ndarray ) )
							self.assertEqual(batch["input_ids"].tolist(),  [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]] )
							_snake_case	       =       tokenizer.pad(UpperCAmelCase_,  padding=UpperCAmelCase_,  return_tensors="np" )
							self.assertTrue(isinstance(batch["input_ids"],  np.ndarray ) )
							self.assertEqual(batch["input_ids"].tolist(),  [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]] )






					@require_torch
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							import torch

							_snake_case	       =       [{'input_ids': torch.tensor([0, 1, 2] )}, {'input_ids': torch.tensor([0, 1, 2, 3] )}]
							_snake_case	       =       BertTokenizer.from_pretrained("bert-base-cased" )

							_snake_case	       =       tokenizer.pad(UpperCAmelCase_,  padding=UpperCAmelCase_ )
							self.assertTrue(isinstance(batch["input_ids"],  torch.Tensor ) )
							self.assertEqual(batch["input_ids"].tolist(),  [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]] )
							_snake_case	       =       tokenizer.pad(UpperCAmelCase_,  padding=UpperCAmelCase_,  return_tensors="pt" )
							self.assertTrue(isinstance(batch["input_ids"],  torch.Tensor ) )
							self.assertEqual(batch["input_ids"].tolist(),  [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]] )






					@require_tf
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							import tensorflow as tf

							_snake_case	       =       [{'input_ids': tf.constant([0, 1, 2] )}, {'input_ids': tf.constant([0, 1, 2, 3] )}]
							_snake_case	       =       BertTokenizer.from_pretrained("bert-base-cased" )

							_snake_case	       =       tokenizer.pad(UpperCAmelCase_,  padding=UpperCAmelCase_ )
							self.assertTrue(isinstance(batch["input_ids"],  tf.Tensor ) )
							self.assertEqual(batch["input_ids"].numpy().tolist(),  [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]] )
							_snake_case	       =       tokenizer.pad(UpperCAmelCase_,  padding=UpperCAmelCase_,  return_tensors="tf" )
							self.assertTrue(isinstance(batch["input_ids"],  tf.Tensor ) )
							self.assertEqual(batch["input_ids"].numpy().tolist(),  [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]] )






					@require_tokenizers
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       Tokenizer(WordPiece(unk_token="[UNK]" ) )
							PreTrainedTokenizerFast(tokenizer_object=UpperCAmelCase_ )






					@require_tokenizers
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       Tokenizer(WordPiece(unk_token="[UNK]" ) )
							with tempfile.TemporaryDirectory() as tmpdirname:
									bert_tokenizer.save(os.path.join(UpperCAmelCase_,  "tokenizer.json" ) )
									PreTrainedTokenizerFast(tokenizer_file=os.path.join(UpperCAmelCase_,  "tokenizer.json" ) )







"""simple docstring"""

import gc
import unittest

import numpy as np
import torch

from diffusers import RePaintPipeline, RePaintScheduler, UNetaDModel
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    load_image,
    load_numpy,
    nightly,
    require_torch_gpu,
    skip_mps,
    torch_device,
)

from ..pipeline_params import IMAGE_INPAINTING_BATCH_PARAMS, IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()






class        _lowercase   (				__UpperCAmelCase				,       unittest.TestCase		):
	lowercase_        =	RePaintPipeline
	lowercase_        =	IMAGE_INPAINTING_PARAMS - {'width', 'height', 'guidance_scale'}
	lowercase_        =	PipelineTesterMixin.required_optional_params - {
	    'latents',
	    'num_images_per_prompt',
	    'callback',
	    'callback_steps',
	}
	lowercase_        =	IMAGE_INPAINTING_BATCH_PARAMS
	lowercase_        =	False




	def 			_UpperCamelCase    ( self					)		->							Optional[Any]:
			torch.manual_seed(0					)
			torch.manual_seed(0					)
			lowerCamelCase     :			Optional[Any]		=    UNetaDModel(
			    block_out_channels=(32, 64)		, layers_per_block=2		, sample_size=32		, in_channels=3		, out_channels=3		, down_block_types=('DownBlock2D', 'AttnDownBlock2D')		, up_block_types=('AttnUpBlock2D', 'UpBlock2D')		, )
			lowerCamelCase     :			Union[str, Any]		=    RePaintScheduler()
			lowerCamelCase     :			Dict		=    {'unet': unet, 'scheduler': scheduler}
			return components




	def 			_UpperCamelCase    ( self		, UpperCAmelCase_		, UpperCAmelCase_=0					)		->							Optional[int]:
			if str(UpperCAmelCase_					).startswith('mps'					):
					lowerCamelCase     :			str		=    torch.manual_seed(UpperCAmelCase_					)
			else:
					lowerCamelCase     :			Tuple		=    torch.Generator(device=UpperCAmelCase_					).manual_seed(UpperCAmelCase_					)
			lowerCamelCase     :			str		=    np.random.RandomState(UpperCAmelCase_					).standard_normal((1, 3, 32, 32)					)
			lowerCamelCase     :			Tuple		=    torch.from_numpy(UpperCAmelCase_					).to(device=UpperCAmelCase_		, dtype=torch.floataa					)
			lowerCamelCase     :			Optional[int]		=    (image > 0).to(device=UpperCAmelCase_		, dtype=torch.floataa					)
			lowerCamelCase     :			Optional[Any]		=    {
			    'image': image,
			    'mask_image': mask,
			    'generator': generator,
			    'num_inference_steps': 5,
			    'eta': 0.0,
			    'jump_length': 2,
			    'jump_n_sample': 2,
			    'output_type': 'numpy',
			}
			return inputs




	def 			_UpperCamelCase    ( self					)		->							str:
			lowerCamelCase     :			Any		=    'cpu'  # ensure determinism for the device-dependent torch.Generator
			lowerCamelCase     :			Union[str, Any]		=    self.get_dummy_components()
			lowerCamelCase     :			List[Any]		=    RePaintPipeline(**UpperCAmelCase_					)
			lowerCamelCase     :			int		=    sd_pipe.to(UpperCAmelCase_					)
			sd_pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

			lowerCamelCase     :			Any		=    self.get_dummy_inputs(UpperCAmelCase_					)
			lowerCamelCase     :			Dict		=    sd_pipe(**UpperCAmelCase_					).images
			lowerCamelCase     :			List[Any]		=    image[0, -3:, -3:, -1]

			assert image.shape == (1, 32, 32, 3)
			lowerCamelCase     :			Optional[Any]		=    np.array([1.0000, 0.5426, 0.5497, 0.2200, 1.0000, 1.0000, 0.5623, 1.0000, 0.6274]					)

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




	@skip_mps
	def 			_UpperCamelCase    ( self					)		->							Union[str, Any]:
			return super().test_save_load_local()




	@unittest.skip('non-deterministic pipeline'					)
	def 			_UpperCamelCase    ( self					)		->							int:
			return super().test_inference_batch_single_identical()




	@skip_mps
	def 			_UpperCamelCase    ( self					)		->							int:
			return super().test_dict_tuple_outputs_equivalent()




	@skip_mps
	def 			_UpperCamelCase    ( self					)		->							Optional[int]:
			return super().test_save_load_optional_components()




	@skip_mps
	def 			_UpperCamelCase    ( self					)		->							Tuple:
			return super().test_attention_slicing_forward_pass()






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




	def 			_UpperCamelCase    ( self					)		->							Union[str, Any]:
			super().tearDown()
			gc.collect()
			torch.cuda.empty_cache()




	def 			_UpperCamelCase    ( self					)		->							int:
			lowerCamelCase     :			Optional[int]		=    load_image(
			    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/'
			    'repaint/celeba_hq_256.png'					)
			lowerCamelCase     :			str		=    load_image(
			    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/repaint/mask_256.png'					)
			lowerCamelCase     :			Optional[Any]		=    load_numpy(
			    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/'
			    'repaint/celeba_hq_256_result.npy'					)

			lowerCamelCase     :			Any		=    'google/ddpm-ema-celebahq-256'
			lowerCamelCase     :			List[Any]		=    UNetaDModel.from_pretrained(UpperCAmelCase_					)
			lowerCamelCase     :			Any		=    RePaintScheduler.from_pretrained(UpperCAmelCase_					)

			lowerCamelCase     :			Any		=    RePaintPipeline(unet=UpperCAmelCase_		, scheduler=UpperCAmelCase_					).to(UpperCAmelCase_					)
			repaint.set_progress_bar_config(disable=UpperCAmelCase_					)
			repaint.enable_attention_slicing()

			lowerCamelCase     :			Dict		=    torch.manual_seed(0					)
			lowerCamelCase     :			str		=    repaint(
			    UpperCAmelCase_		, UpperCAmelCase_		, num_inference_steps=250		, eta=0.0		, jump_length=10		, jump_n_sample=10		, generator=UpperCAmelCase_		, output_type='np'		, )
			lowerCamelCase     :			Optional[int]		=    output.images[0]

			assert image.shape == (256, 256, 3)
			assert np.abs(expected_image - image					).mean() < 1E-2






