
"""simple docstring"""





import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModelWithProjection

from diffusers import (
    AutoencoderKL,
    DPMSolverMultistepScheduler,
    PNDMScheduler,
    StableDiffusionImageVariationPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, nightly, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


class A_      (UpperCAmelCase_							,UpperCAmelCase_							,UpperCAmelCase_							,unittest.TestCase ):




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__     :		Any    	=						StableDiffusionImageVariationPipeline
  SCREAMING_SNAKE_CASE__     :		Optional[Any]    	=						IMAGE_VARIATION_PARAMS
  SCREAMING_SNAKE_CASE__     :		Tuple    	=						IMAGE_VARIATION_BATCH_PARAMS
  SCREAMING_SNAKE_CASE__     :		int    	=						frozenset([] )
  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
  SCREAMING_SNAKE_CASE__     :		Optional[Any]    	=						frozenset([] )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     torch.manual_seed(0    )
     UpperCAmelCase_   :   Optional[Any]     	=       UNetaDConditionModel(
         block_out_channels=(32, 64)							,  layers_per_block=2							,  sample_size=32							,  in_channels=4							,  out_channels=4							,  down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")							,  up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")							,  cross_attention_dim=32							,  )
     UpperCAmelCase_   :   List[Any]     	=       PNDMScheduler(skip_prk_steps=_lowercase    )
     torch.manual_seed(0    )
     UpperCAmelCase_   :   Tuple     	=       AutoencoderKL(
         block_out_channels=[32, 64]							,  in_channels=3							,  out_channels=3							,  down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]							,  up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]							,  latent_channels=4							,  )
     torch.manual_seed(0    )
     UpperCAmelCase_   :   List[str]     	=       CLIPVisionConfig(
         hidden_size=32							,  projection_dim=32							,  intermediate_size=37							,  layer_norm_eps=1E-0_5							,  num_attention_heads=4							,  num_hidden_layers=5							,  image_size=32							,  patch_size=4							,  )
     UpperCAmelCase_   :   Optional[int]     	=       CLIPVisionModelWithProjection(_lowercase    )
     UpperCAmelCase_   :   int     	=       CLIPImageProcessor(crop_size=32							,  size=32    )

     UpperCAmelCase_   :   str     	=       {
         'unet': unet,
         'scheduler': scheduler,
         'vae': vae,
         'image_encoder': image_encoder,
         'feature_extractor': feature_extractor,
         'safety_checker': None,
     }
     return components







  def 			UpperCamelCase__   (						self							,  lowercase_							,  lowercase_=0    ):

     """simple docstring"""
     UpperCAmelCase_   :   Tuple     	=       floats_tensor((1, 3, 32, 32)							,  rng=random.Random(_lowercase    )    )
     UpperCAmelCase_   :   str     	=       image.cpu().permute(0							,  2							,  3							,  1    )[0]
     UpperCAmelCase_   :   str     	=       Image.fromarray(np.uinta(_lowercase    )    ).convert("RGB"    ).resize((32, 32)    )
     if str(_lowercase    ).startswith("mps"    ):
        UpperCAmelCase_   :   int     	=       torch.manual_seed(_lowercase    )
     else:
        UpperCAmelCase_   :   Dict     	=       torch.Generator(device=_lowercase    ).manual_seed(_lowercase    )
     UpperCAmelCase_   :   int     	=       {
         'image': image,
         'generator': generator,
         'num_inference_steps': 2,
         'guidance_scale': 6.0,
         'output_type': 'numpy',
     }
     return inputs







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Tuple     	=       'cpu'  # ensure determinism for the device-dependent torch.Generator
     UpperCAmelCase_   :   List[Any]     	=       self.get_dummy_components()
     UpperCAmelCase_   :   str     	=       StableDiffusionImageVariationPipeline(**_lowercase    )
     UpperCAmelCase_   :   List[Any]     	=       sd_pipe.to(_lowercase    )
     sd_pipe.set_progress_bar_config(disable=_lowercase    )

     UpperCAmelCase_   :   Optional[int]     	=       self.get_dummy_inputs(_lowercase    )
     UpperCAmelCase_   :   str     	=       sd_pipe(**_lowercase    ).images
     UpperCAmelCase_   :   Optional[int]     	=       image[0, -3:, -3:, -1]

     assert image.shape == (1, 64, 64, 3)
     UpperCAmelCase_   :   int     	=       np.array([0.52_39, 0.57_23, 0.47_96, 0.50_49, 0.55_50, 0.46_85, 0.53_29, 0.48_91, 0.49_21]    )

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







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[str]     	=       'cpu'  # ensure determinism for the device-dependent torch.Generator
     UpperCAmelCase_   :   Optional[Any]     	=       self.get_dummy_components()
     UpperCAmelCase_   :   Any     	=       StableDiffusionImageVariationPipeline(**_lowercase    )
     UpperCAmelCase_   :   Dict     	=       sd_pipe.to(_lowercase    )
     sd_pipe.set_progress_bar_config(disable=_lowercase    )

     UpperCAmelCase_   :   List[Any]     	=       self.get_dummy_inputs(_lowercase    )
     UpperCAmelCase_   :   Optional[int]     	=       2 * [inputs['image']]
     UpperCAmelCase_   :   Tuple     	=       sd_pipe(**_lowercase    )

     UpperCAmelCase_   :   List[str]     	=       output.images

     UpperCAmelCase_   :   Tuple     	=       image[-1, -3:, -3:, -1]

     assert image.shape == (2, 64, 64, 3)
     UpperCAmelCase_   :   Any     	=       np.array([0.68_92, 0.56_37, 0.58_36, 0.57_71, 0.62_54, 0.64_09, 0.55_80, 0.55_69, 0.52_89]    )

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







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     super().test_inference_batch_single_identical(expected_max_diff=3E-3    )




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




  '''simple docstring'''







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()







  def 			UpperCamelCase__   (						self							,  lowercase_							,  lowercase_="cpu"							,  lowercase_=torch.floataa							,  lowercase_=0    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[Any]     	=       torch.Generator(device=_lowercase    ).manual_seed(_lowercase    )
     UpperCAmelCase_   :   Union[str, Any]     	=       load_image(
         "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
         "/stable_diffusion_imgvar/input_image_vermeer.png"    )
     UpperCAmelCase_   :   Optional[Any]     	=       np.random.RandomState(_lowercase    ).standard_normal((1, 4, 64, 64)    )
     UpperCAmelCase_   :   Optional[Any]     	=       torch.from_numpy(_lowercase    ).to(device=_lowercase							,  dtype=_lowercase    )
     UpperCAmelCase_   :   Dict     	=       {
         'image': init_image,
         'latents': latents,
         'generator': generator,
         'num_inference_steps': 3,
         'guidance_scale': 7.5,
         'output_type': 'numpy',
     }
     return inputs







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   int     	=       StableDiffusionImageVariationPipeline.from_pretrained(
         "lambdalabs/sd-image-variations-diffusers"							,  safety_checker=_lowercase    )
     UpperCAmelCase_   :   Optional[int]     	=       sd_pipe.to(_lowercase    )
     sd_pipe.set_progress_bar_config(disable=_lowercase    )

     UpperCAmelCase_   :   Tuple     	=       self.get_inputs(_lowercase    )
     UpperCAmelCase_   :   Any     	=       sd_pipe(**_lowercase    ).images
     UpperCAmelCase_   :   List[Any]     	=       image[0, -3:, -3:, -1].flatten()

     assert image.shape == (1, 512, 512, 3)
     UpperCAmelCase_   :   Dict     	=       np.array([0.8_44_91, 0.9_07_89, 0.7_57_08, 0.7_87_34, 0.8_34_85, 0.7_00_99, 0.6_69_38, 0.6_87_27, 0.6_13_79]    )
     assert np.abs(image_slice - expected_slice    ).max() < 6E-3







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Tuple     	=       0

     def callback_fn(lowercase_							,  lowercase_							,  lowercase_    ) -> None:
        UpperCAmelCase_   :   Any     	=       True
        nonlocal number_of_steps
        number_of_steps += 1
        if step == 1:
           UpperCAmelCase_   :   List[Any]     	=       latents.detach().cpu().numpy()
           assert latents.shape == (1, 4, 64, 64)
           UpperCAmelCase_   :   List[str]     	=       latents[0, -3:, -3:, -1]
           UpperCAmelCase_   :   Dict     	=       np.array(
               [-0.16_21, 0.28_37, -0.79_79, -0.12_21, -1.30_57, 0.76_81, -2.11_91, 0.04_64, 1.63_09]    )

           assert np.abs(latents_slice.flatten() - expected_slice    ).max() < 5E-2
        elif step == 2:
           UpperCAmelCase_   :   Union[str, Any]     	=       latents.detach().cpu().numpy()
           assert latents.shape == (1, 4, 64, 64)
           UpperCAmelCase_   :   List[Any]     	=       latents[0, -3:, -3:, -1]
           UpperCAmelCase_   :   Dict     	=       np.array([0.62_99, 1.75_00, 1.19_92, -2.15_82, -1.89_94, 0.73_34, -0.70_90, 1.01_37, 1.52_73]    )

           assert np.abs(latents_slice.flatten() - expected_slice    ).max() < 5E-2

     UpperCAmelCase_   :   Dict     	=       False

     UpperCAmelCase_   :   str     	=       StableDiffusionImageVariationPipeline.from_pretrained(
         "fusing/sd-image-variations-diffusers"							,  safety_checker=_lowercase							,  torch_dtype=torch.floataa							,  )
     pipe.to(_lowercase    )
     pipe.set_progress_bar_config(disable=_lowercase    )
     pipe.enable_attention_slicing()

     UpperCAmelCase_   :   Any     	=       self.get_inputs(_lowercase							,  dtype=torch.floataa    )
     pipe(**_lowercase							,  callback=_lowercase							,  callback_steps=1    )
     assert callback_fn.has_been_called
     assert number_of_steps == inputs["num_inference_steps"]







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     torch.cuda.empty_cache()
     torch.cuda.reset_max_memory_allocated()
     torch.cuda.reset_peak_memory_stats()

     UpperCAmelCase_   :   str     	=       'fusing/sd-image-variations-diffusers'
     UpperCAmelCase_   :   Any     	=       StableDiffusionImageVariationPipeline.from_pretrained(
         _lowercase							,  safety_checker=_lowercase							,  torch_dtype=torch.floataa    )
     UpperCAmelCase_   :   int     	=       pipe.to(_lowercase    )
     pipe.set_progress_bar_config(disable=_lowercase    )
     pipe.enable_attention_slicing(1    )
     pipe.enable_sequential_cpu_offload()

     UpperCAmelCase_   :   List[Any]     	=       self.get_inputs(_lowercase							,  dtype=torch.floataa    )
     UpperCAmelCase_   :   str     	=       pipe(**_lowercase    )

     UpperCAmelCase_   :   Dict     	=       torch.cuda.max_memory_allocated()
     # make sure that less than 2.6 GB is allocated
     assert mem_bytes < 2.6 * 10**9




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




  '''simple docstring'''







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()







  def 			UpperCamelCase__   (						self							,  lowercase_							,  lowercase_="cpu"							,  lowercase_=torch.floataa							,  lowercase_=0    ):

     """simple docstring"""
     UpperCAmelCase_   :   Tuple     	=       torch.Generator(device=_lowercase    ).manual_seed(_lowercase    )
     UpperCAmelCase_   :   List[str]     	=       load_image(
         "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
         "/stable_diffusion_imgvar/input_image_vermeer.png"    )
     UpperCAmelCase_   :   Union[str, Any]     	=       np.random.RandomState(_lowercase    ).standard_normal((1, 4, 64, 64)    )
     UpperCAmelCase_   :   List[Any]     	=       torch.from_numpy(_lowercase    ).to(device=_lowercase							,  dtype=_lowercase    )
     UpperCAmelCase_   :   Tuple     	=       {
         'image': init_image,
         'latents': latents,
         'generator': generator,
         'num_inference_steps': 50,
         'guidance_scale': 7.5,
         'output_type': 'numpy',
     }
     return inputs







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[Any]     	=       StableDiffusionImageVariationPipeline.from_pretrained("fusing/sd-image-variations-diffusers"    )
     sd_pipe.to(_lowercase    )
     sd_pipe.set_progress_bar_config(disable=_lowercase    )

     UpperCAmelCase_   :   str     	=       self.get_inputs(_lowercase    )
     UpperCAmelCase_   :   Any     	=       sd_pipe(**_lowercase    ).images[0]

     UpperCAmelCase_   :   str     	=       load_numpy(
         "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
         "/stable_diffusion_imgvar/lambdalabs_variations_pndm.npy"    )
     UpperCAmelCase_   :   Optional[int]     	=       np.abs(expected_image - image    ).max()
     assert max_diff < 1E-3







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   str     	=       StableDiffusionImageVariationPipeline.from_pretrained("fusing/sd-image-variations-diffusers"    )
     UpperCAmelCase_   :   Any     	=       DPMSolverMultistepScheduler.from_config(sd_pipe.scheduler.config    )
     sd_pipe.to(_lowercase    )
     sd_pipe.set_progress_bar_config(disable=_lowercase    )

     UpperCAmelCase_   :   str     	=       self.get_inputs(_lowercase    )
     UpperCAmelCase_   :   Any     	=       25
     UpperCAmelCase_   :   List[Any]     	=       sd_pipe(**_lowercase    ).images[0]

     UpperCAmelCase_   :   str     	=       load_numpy(
         "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
         "/stable_diffusion_imgvar/lambdalabs_variations_dpm_multi.npy"    )
     UpperCAmelCase_   :   List[Any]     	=       np.abs(expected_image - image    ).max()
     assert max_diff < 1E-3










'''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 				__magic_name__  (						unittest.TestCase					):







	"""simple docstring"""



	def 							lowerCamelCase__		(				self	,   _lowercase							)    ->	Optional[int]:
							lowercase_   : Optional[int]   	=      list(tokenizer_class.max_model_input_sizes.keys()							)
							for model_name in sa_models[:1]:
													lowercase_   : List[str]   	=      tokenizer_class.from_pretrained(_lowercase							)
													self.assertIsNotNone(_lowercase							)
													self.assertIsInstance(_lowercase	,   _lowercase							)
													self.assertIsInstance(_lowercase	,   _lowercase							)

													for special_tok in tokenizer.all_special_tokens:
																			self.assertIsInstance(_lowercase	,   _lowercase							)
																			lowercase_   : Optional[int]   	=      tokenizer.convert_tokens_to_ids(_lowercase							)
																			self.assertIsInstance(_lowercase	,   _lowercase							)
	def 							lowerCamelCase__		(				self	,   _lowercase	,   _lowercase = None							)    ->	str:
							lowercase_   : int   	=      pickle.dumps(_lowercase							)
							self.assertIsNotNone(_lowercase							)

							lowercase_   : Any   	=      pickle.loads(_lowercase							)

							# 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(_lowercase	,   _lowercase							)							)
													else:
																			self.assertEqual(_lowercase	,   _lowercase							)
	@slow
	def 							lowerCamelCase__		(				self							)    ->	List[str]:
							self.check_tokenizer_from_pretrained(_lowercase							)
	def 							lowerCamelCase__		(				self							)    ->	str:
							self.assertEqual(TensorType('tf'							)	,   TensorType.TENSORFLOW							)
							self.assertEqual(TensorType('pt'							)	,   TensorType.PYTORCH							)
							self.assertEqual(TensorType('np'							)	,   TensorType.NUMPY							)
	@require_tokenizers
	def 							lowerCamelCase__		(				self							)    ->	Dict:
							import numpy as np

							lowercase_   : Any   	=      BertTokenizer.from_pretrained('bert-base-cased'							)
							lowercase_   : str   	=      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 							lowerCamelCase__		(				self							)    ->	Optional[int]:
							import tensorflow as tf

							def tf_array_equals(_lowercase	,   _lowercase							):
													return tf.reduce_all(tf.equal(_lowercase	,   _lowercase							)							)

							lowercase_   : Optional[Any]   	=      BertTokenizer.from_pretrained('bert-base-cased'							)
							lowercase_   : str   	=      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							)	,   _lowercase							)

							with self.subTest('BatchEncoding (Rust, return_tensors=TENSORFLOW)'							):
													self.assert_dump_and_restore(
													    tokenizer_r('Small example to encode'	,   return_tensors=TensorType.TENSORFLOW							)	,   _lowercase							)
	@require_torch
	@require_tokenizers
	def 							lowerCamelCase__		(				self							)    ->	List[str]:
							import torch

							lowercase_   : Dict   	=      BertTokenizer.from_pretrained('bert-base-cased'							)
							lowercase_   : Optional[int]   	=      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 							lowerCamelCase__		(				self							)    ->	Tuple:
							lowercase_   : Dict   	=      BertTokenizer.from_pretrained('bert-base-cased'							)
							lowercase_   : Tuple   	=      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 							lowerCamelCase__		(				self							)    ->	List[Any]:
							lowercase_   : Optional[Any]   	=      BertTokenizerFast.from_pretrained('bert-base-cased'							)
							lowercase_   : Dict   	=      tokenizer_r(['Test', '\xad', 'test']	,   is_split_into_words=_lowercase							)

							self.assertEqual(encoded.word_to_tokens(0							)	,   TokenSpan(start=1	,   end=2							)							)
							self.assertEqual(encoded.word_to_tokens(1							)	,   _lowercase							)
							self.assertEqual(encoded.word_to_tokens(2							)	,   TokenSpan(start=2	,   end=3							)							)
	def 							lowerCamelCase__		(				self							)    ->	str:
							lowercase_   : Dict   	=      BatchEncoding({'inputs': [[1, 2, 3], [4, 5, 6]], 'labels': [0, 1]}							)
							lowercase_   : Any   	=      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:
													lowercase_   : Dict   	=      batch.convert_to_tensors(tensor_type='np'							)
							self.assertFalse(len(cs.err							)	,   msg=f"should have no warning, but got {cs.err}"							)

							lowercase_   : Union[str, Any]   	=      BatchEncoding({'inputs': [1, 2, 3], 'labels': 0}							)
							lowercase_   : Optional[int]   	=      batch.convert_to_tensors(tensor_type='np'	,   prepend_batch_axis=_lowercase							)
							self.assertEqual(tensor_batch['inputs'].shape	,   (1, 3)							)
							self.assertEqual(tensor_batch['labels'].shape	,   (1,)							)
	@require_torch
	def 							lowerCamelCase__		(				self							)    ->	Union[str, Any]:
							lowercase_   : Dict   	=      BatchEncoding({'inputs': [[1, 2, 3], [4, 5, 6]], 'labels': [0, 1]}							)
							lowercase_   : Optional[Any]   	=      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:
													lowercase_   : Optional[Any]   	=      batch.convert_to_tensors(tensor_type='pt'							)
							self.assertFalse(len(cs.err							)	,   msg=f"should have no warning, but got {cs.err}"							)

							lowercase_   : int   	=      BatchEncoding({'inputs': [1, 2, 3], 'labels': 0}							)
							lowercase_   : Union[str, Any]   	=      batch.convert_to_tensors(tensor_type='pt'	,   prepend_batch_axis=_lowercase							)
							self.assertEqual(tensor_batch['inputs'].shape	,   (1, 3)							)
							self.assertEqual(tensor_batch['labels'].shape	,   (1,)							)
	@require_tf
	def 							lowerCamelCase__		(				self							)    ->	Tuple:
							lowercase_   : Dict   	=      BatchEncoding({'inputs': [[1, 2, 3], [4, 5, 6]], 'labels': [0, 1]}							)
							lowercase_   : List[Any]   	=      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:
													lowercase_   : Tuple   	=      batch.convert_to_tensors(tensor_type='tf'							)
							self.assertFalse(len(cs.err							)	,   msg=f"should have no warning, but got {cs.err}"							)

							lowercase_   : int   	=      BatchEncoding({'inputs': [1, 2, 3], 'labels': 0}							)
							lowercase_   : Optional[Any]   	=      batch.convert_to_tensors(tensor_type='tf'	,   prepend_batch_axis=_lowercase							)
							self.assertEqual(tensor_batch['inputs'].shape	,   (1, 3)							)
							self.assertEqual(tensor_batch['labels'].shape	,   (1,)							)
	@require_flax
	def 							lowerCamelCase__		(				self							)    ->	Union[str, Any]:
							lowercase_   : int   	=      BatchEncoding({'inputs': [[1, 2, 3], [4, 5, 6]], 'labels': [0, 1]}							)
							lowercase_   : Optional[Any]   	=      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:
													lowercase_   : Dict   	=      batch.convert_to_tensors(tensor_type='jax'							)
							self.assertFalse(len(cs.err							)	,   msg=f"should have no warning, but got {cs.err}"							)

							lowercase_   : List[Any]   	=      BatchEncoding({'inputs': [1, 2, 3], 'labels': 0}							)
							lowercase_   : Dict   	=      batch.convert_to_tensors(tensor_type='jax'	,   prepend_batch_axis=_lowercase							)
							self.assertEqual(tensor_batch['inputs'].shape	,   (1, 3)							)
							self.assertEqual(tensor_batch['labels'].shape	,   (1,)							)
	def 							lowerCamelCase__		(				self							)    ->	List[str]:
							lowercase_   : Any   	=      [{'input_ids': np.array([0, 1, 2]							)}, {'input_ids': np.array([0, 1, 2, 3]							)}]
							lowercase_   : str   	=      BertTokenizer.from_pretrained('bert-base-cased'							)

							lowercase_   : Any   	=      tokenizer.pad(_lowercase	,   padding=_lowercase							)
							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]]							)
							lowercase_   : List[str]   	=      tokenizer.pad(_lowercase	,   padding=_lowercase	,   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 							lowerCamelCase__		(				self							)    ->	List[Any]:
							import torch

							lowercase_   : Union[str, Any]   	=      [{'input_ids': torch.tensor([0, 1, 2]							)}, {'input_ids': torch.tensor([0, 1, 2, 3]							)}]
							lowercase_   : Optional[int]   	=      BertTokenizer.from_pretrained('bert-base-cased'							)

							lowercase_   : str   	=      tokenizer.pad(_lowercase	,   padding=_lowercase							)
							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]]							)
							lowercase_   : Tuple   	=      tokenizer.pad(_lowercase	,   padding=_lowercase	,   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 							lowerCamelCase__		(				self							)    ->	Any:
							import tensorflow as tf

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

							lowercase_   : Any   	=      tokenizer.pad(_lowercase	,   padding=_lowercase							)
							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]]							)
							lowercase_   : Dict   	=      tokenizer.pad(_lowercase	,   padding=_lowercase	,   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 							lowerCamelCase__		(				self							)    ->	str:
							lowercase_   : Optional[int]   	=      Tokenizer(WordPiece(unk_token='[UNK]'							)							)
							PreTrainedTokenizerFast(tokenizer_object=_lowercase							)







	@require_tokenizers
	def 							lowerCamelCase__		(				self							)    ->	int:
							lowercase_   : Union[str, Any]   	=      Tokenizer(WordPiece(unk_token='[UNK]'							)							)
							with tempfile.TemporaryDirectory() as tmpdirname:
													bert_tokenizer.save(os.path.join(_lowercase	,   'tokenizer.json'							)							)
													PreTrainedTokenizerFast(tokenizer_file=os.path.join(_lowercase	,   'tokenizer.json'							)							)
