'''simple docstring'''




import inspect
import unittest

from transformers import DataaVecVisionConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch
						from torch import nn

						from transformers import (
						    MODEL_MAPPING,
						    DataaVecVisionForImageClassification,
						    DataaVecVisionForSemanticSegmentation,
						    DataaVecVisionModel,
						)
						from transformers.models.dataavec.modeling_dataavec_vision import DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import BeitImageProcessor






class     a__:


			'''simple docstring'''






			def __init__(       self	,   __lowerCAmelCase	,   __lowerCAmelCase=100	,   __lowerCAmelCase=13	,   __lowerCAmelCase=30	,   __lowerCAmelCase=2	,   __lowerCAmelCase=3	,   __lowerCAmelCase=True	,   __lowerCAmelCase=True	,   __lowerCAmelCase=32	,   __lowerCAmelCase=4	,   __lowerCAmelCase=4	,   __lowerCAmelCase=37	,   __lowerCAmelCase="gelu"	,   __lowerCAmelCase=0.1	,   __lowerCAmelCase=0.1	,   __lowerCAmelCase=10	,   __lowerCAmelCase=0.02	,   __lowerCAmelCase=3	,   __lowerCAmelCase=None	,   __lowerCAmelCase=[0, 1, 2, 3]	,   ):




					"""simple docstring"""


					lowerCAmelCase        =  parent
					lowerCAmelCase        =  100
					lowerCAmelCase        =  batch_size
					lowerCAmelCase        =  image_size
					lowerCAmelCase        =  patch_size
					lowerCAmelCase        =  num_channels
					lowerCAmelCase        =  is_training
					lowerCAmelCase        =  use_labels
					lowerCAmelCase        =  hidden_size
					lowerCAmelCase        =  num_hidden_layers
					lowerCAmelCase        =  num_attention_heads
					lowerCAmelCase        =  intermediate_size
					lowerCAmelCase        =  hidden_act
					lowerCAmelCase        =  hidden_dropout_prob
					lowerCAmelCase        =  attention_probs_dropout_prob
					lowerCAmelCase        =  type_sequence_label_size
					lowerCAmelCase        =  initializer_range
					lowerCAmelCase        =  scope
					lowerCAmelCase        =  out_indices
					lowerCAmelCase        =  num_labels

					# in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
					lowerCAmelCase        =  (image_size // patch_size) ** 2
					lowerCAmelCase        =  num_patches + 1



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

					lowerCAmelCase        =  None
					lowerCAmelCase        =  None
					if self.use_labels:
							lowerCAmelCase        =  ids_tensor([self.batch_size]	,   self.type_sequence_label_size)
							lowerCAmelCase        =  ids_tensor([self.batch_size, self.image_size, self.image_size]	,   self.num_labels)

					lowerCAmelCase        =  self.get_config()

					return config, pixel_values, labels, pixel_labels



			def 							a_	(       self):




					"""simple docstring"""


					return DataaVecVisionConfig(
					    vocab_size=self.vocab_size	,   image_size=self.image_size	,   patch_size=self.patch_size	,   num_channels=self.num_channels	,   hidden_size=self.hidden_size	,   num_hidden_layers=self.num_hidden_layers	,   num_attention_heads=self.num_attention_heads	,   intermediate_size=self.intermediate_size	,   hidden_act=self.hidden_act	,   hidden_dropout_prob=self.hidden_dropout_prob	,   attention_probs_dropout_prob=self.attention_probs_dropout_prob	,   is_decoder=__lowerCAmelCase	,   initializer_range=self.initializer_range	,   out_indices=self.out_indices	,   )



			def 							a_	(       self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase        =  DataaVecVisionModel(config=__lowerCAmelCase)
					model.to(__lowerCAmelCase)
					model.eval()
					lowerCAmelCase        =  model(__lowerCAmelCase)
					# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
					lowerCAmelCase        =  (self.image_size // self.patch_size) ** 2
					self.parent.assertEqual(result.last_hidden_state.shape	,   (self.batch_size, num_patches + 1, self.hidden_size))



			def 							a_	(       self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase        =  self.type_sequence_label_size
					lowerCAmelCase        =  DataaVecVisionForImageClassification(__lowerCAmelCase)
					model.to(__lowerCAmelCase)
					model.eval()
					lowerCAmelCase        =  model(__lowerCAmelCase	,   labels=__lowerCAmelCase)
					self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.type_sequence_label_size))



			def 							a_	(       self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase        =  self.num_labels
					lowerCAmelCase        =  DataaVecVisionForSemanticSegmentation(__lowerCAmelCase)
					model.to(__lowerCAmelCase)
					model.eval()
					lowerCAmelCase        =  model(__lowerCAmelCase)
					self.parent.assertEqual(
					    result.logits.shape	,   (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2))
					lowerCAmelCase        =  model(__lowerCAmelCase	,   labels=__lowerCAmelCase)
					self.parent.assertEqual(
					    result.logits.shape	,   (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2))







			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  self.prepare_config_and_inputs()
					lowerCAmelCase ,					lowerCAmelCase ,					lowerCAmelCase ,					lowerCAmelCase        =  config_and_inputs
					lowerCAmelCase        =  {"""pixel_values""": pixel_values}
					return config, inputs_dict







@require_torch
class     a__( lowerCAmelCase__			,						lowerCAmelCase__			,						unittest.TestCase     ):


			'''simple docstring'''






			UpperCAmelCase_      :					Optional[int]								=	(
			    (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
			    if is_torch_available()
			    else ()
			)
			UpperCAmelCase_      :					Dict								=	(
			    {
			        '''feature-extraction''': DataaVecVisionModel,
			        '''image-classification''': DataaVecVisionForImageClassification,
			        '''image-segmentation''': DataaVecVisionForSemanticSegmentation,
			    }
			    if is_torch_available()
			    else {}
			)

			UpperCAmelCase_      :					List[Any]								=	False
			UpperCAmelCase_      :					Optional[int]								=	False
			UpperCAmelCase_      :					str								=	False



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  DataaVecVisionModelTester(self)
					lowerCAmelCase        =  ConfigTester(
					    self	,   config_class=__lowerCAmelCase	,   has_text_modality=__lowerCAmelCase	,   hidden_size=37)



			def 							a_	(       self):




					"""simple docstring"""


					self.config_tester.run_common_tests()



			def 							a_	(       self):




					"""simple docstring"""


					pass



			@require_torch_multi_gpu
			@unittest.skip(
			    reason="""Data2VecVision has some layers using `add_module` which doesn't work well with `nn.DataParallel`""")
			def 							a_	(       self):




					"""simple docstring"""


					pass



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase ,					lowerCAmelCase        =  self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							lowerCAmelCase        =  model_class(__lowerCAmelCase)
							self.assertIsInstance(model.get_input_embeddings()	,   (nn.Module))
							lowerCAmelCase        =  model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(__lowerCAmelCase	,   nn.Linear))



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase ,					lowerCAmelCase        =  self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							lowerCAmelCase        =  model_class(__lowerCAmelCase)
							lowerCAmelCase        =  inspect.signature(model.forward)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							lowerCAmelCase        =  [*signature.parameters.keys()]

							lowerCAmelCase        =  ["""pixel_values"""]
							self.assertListEqual(arg_names[:1]	,   __lowerCAmelCase)



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__lowerCAmelCase)



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_segmentation(*__lowerCAmelCase)



			def 							a_	(       self):




					"""simple docstring"""


					if not self.model_tester.is_training:
							return

					lowerCAmelCase ,					lowerCAmelCase        =  self.model_tester.prepare_config_and_inputs_for_common()
					lowerCAmelCase        =  True

					for model_class in self.all_model_classes:
							if model_class in [*get_values(__lowerCAmelCase)]:
									continue

							lowerCAmelCase        =  model_class(__lowerCAmelCase)
							model.to(__lowerCAmelCase)
							model.train()
							lowerCAmelCase        =  self._prepare_for_class(__lowerCAmelCase	,   __lowerCAmelCase	,   return_labels=__lowerCAmelCase)
							lowerCAmelCase        =  model(**__lowerCAmelCase).loss
							loss.backward()



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase ,					lowerCAmelCase        =  self.model_tester.prepare_config_and_inputs_for_common()
					if not self.model_tester.is_training:
							return

					lowerCAmelCase        =  False
					lowerCAmelCase        =  True

					for model_class in self.all_model_classes:
							if model_class in [*get_values(__lowerCAmelCase)] or not model_class.supports_gradient_checkpointing:
									continue
							# TODO: remove the following 3 lines once we have a MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING
							# this can then be incorporated into _prepare_for_class in test_modeling_common.py
							elif model_class.__name__ == "Data2VecVisionForSemanticSegmentation":
									lowerCAmelCase ,					lowerCAmelCase ,					lowerCAmelCase ,					lowerCAmelCase        =  inputs_dict["""pixel_values"""].shape
									lowerCAmelCase        =  torch.zeros(
									    [self.model_tester.batch_size, height, width]	,   device=__lowerCAmelCase).long()
							lowerCAmelCase        =  model_class(__lowerCAmelCase)
							model.gradient_checkpointing_enable()
							model.to(__lowerCAmelCase)
							model.train()
							lowerCAmelCase        =  self._prepare_for_class(__lowerCAmelCase	,   __lowerCAmelCase	,   return_labels=__lowerCAmelCase)
							lowerCAmelCase        =  model(**__lowerCAmelCase).loss
							loss.backward()



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase ,					lowerCAmelCase        =  self.model_tester.prepare_config_and_inputs_for_common()

					lowerCAmelCase        =  _config_zero_init(__lowerCAmelCase)
					for model_class in self.all_model_classes:
							lowerCAmelCase        =  model_class(config=__lowerCAmelCase)
							for name, param in model.named_parameters():
									# we skip lambda parameters as these require special initial values
									# determined by config.layer_scale_init_value
									if "lambda" in name:
											continue
									if param.requires_grad:
											self.assertIn(
											    ((param.data.mean() * 1E9).round() / 1E9).item()	,   [0.0, 1.0]	,   msg=f"Parameter {name} of model {model_class} seems not properly initialized"	,   )



			def 							a_	(       self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase=2E-4	,   __lowerCAmelCase="outputs"	,   __lowerCAmelCase=None):




					"""simple docstring"""


					super().check_pt_tf_outputs(__lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase)



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase)







			@slow
			def 							a_	(       self):




					"""simple docstring"""


					for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCAmelCase        =  DataaVecVisionModel.from_pretrained(__lowerCAmelCase)
							self.assertIsNotNone(__lowerCAmelCase)







def     snake_case__							(      )   ->     Any:

		'''simple docstring'''


		lowerCAmelCase        =  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
		return image






@require_torch
@require_vision
class     a__( unittest.TestCase     ):


			'''simple docstring'''






			@cached_property
			def 							a_	(       self):




					"""simple docstring"""


					return (
					    BeitImageProcessor.from_pretrained("""facebook/data2vec-vision-base-ft1k""") if is_vision_available() else None
					)







			@slow
			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  DataaVecVisionForImageClassification.from_pretrained("""facebook/data2vec-vision-base-ft1k""").to(
					    __lowerCAmelCase)

					lowerCAmelCase        =  self.default_image_processor
					lowerCAmelCase        =  prepare_img()
					lowerCAmelCase        =  image_processor(images=__lowerCAmelCase	,   return_tensors="""pt""").to(__lowerCAmelCase)

					# forward pass
					with torch.no_grad():
							lowerCAmelCase        =  model(**__lowerCAmelCase)
					lowerCAmelCase        =  outputs.logits

					# verify the logits
					lowerCAmelCase        =  torch.Size((1, 1000))
					self.assertEqual(logits.shape	,   __lowerCAmelCase)

					lowerCAmelCase        =  torch.tensor([0.3277, -0.1395, 0.0911]).to(__lowerCAmelCase)

					self.assertTrue(torch.allclose(logits[0, :3]	,   __lowerCAmelCase	,   atol=1E-4))

					lowerCAmelCase        =  [model.config.labelaid[i] for i in ["""remote control, remote""", """tabby, tabby cat"""]]
					self.assertEqual(logits[0].topk(2).indices.cpu().tolist()	,   __lowerCAmelCase)





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


			'''simple docstring'''






			def 							a_	(       self	,   __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase        =  list(tokenizer_class.max_model_input_sizes.keys())
					for model_name in sa_models[:1]:
							lowerCAmelCase        =  tokenizer_class.from_pretrained(__lowerCAmelCase)
							self.assertIsNotNone(__lowerCAmelCase)
							self.assertIsInstance(__lowerCAmelCase	,   __lowerCAmelCase)
							self.assertIsInstance(__lowerCAmelCase	,   __lowerCAmelCase)

							for special_tok in tokenizer.all_special_tokens:
									self.assertIsInstance(__lowerCAmelCase	,   __lowerCAmelCase)
									lowerCAmelCase        =  tokenizer.convert_tokens_to_ids(__lowerCAmelCase)
									self.assertIsInstance(__lowerCAmelCase	,   __lowerCAmelCase)



			def 							a_	(       self	,   __lowerCAmelCase	,   __lowerCAmelCase = None):




					"""simple docstring"""


					lowerCAmelCase        =  pickle.dumps(__lowerCAmelCase)
					self.assertIsNotNone(__lowerCAmelCase)

					lowerCAmelCase        =  pickle.loads(__lowerCAmelCase)

					# 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(__lowerCAmelCase	,   __lowerCAmelCase))
							else:
									self.assertEqual(__lowerCAmelCase	,   __lowerCAmelCase)



			@slow
			def 							a_	(       self):




					"""simple docstring"""


					self.check_tokenizer_from_pretrained(__lowerCAmelCase)



			def 							a_	(       self):




					"""simple docstring"""


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



			@require_tokenizers
			def 							a_	(       self):




					"""simple docstring"""


					import numpy as np

					lowerCAmelCase        =  BertTokenizer.from_pretrained("""bert-base-cased""")
					lowerCAmelCase        =  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 							a_	(       self):




					"""simple docstring"""


					import tensorflow as tf

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

					lowerCAmelCase        =  BertTokenizer.from_pretrained("""bert-base-cased""")
					lowerCAmelCase        =  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)	,   __lowerCAmelCase)

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



			@require_torch
			@require_tokenizers
			def 							a_	(       self):




					"""simple docstring"""


					import torch

					lowerCAmelCase        =  BertTokenizer.from_pretrained("""bert-base-cased""")
					lowerCAmelCase        =  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 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  BertTokenizer.from_pretrained("""bert-base-cased""")
					lowerCAmelCase        =  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 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  BertTokenizerFast.from_pretrained("""bert-base-cased""")
					lowerCAmelCase        =  tokenizer_r(["""Test""", """\xad""", """test"""]	,   is_split_into_words=__lowerCAmelCase)

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



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  BatchEncoding({"""inputs""": [[1, 2, 3], [4, 5, 6]], """labels""": [0, 1]})
					lowerCAmelCase        =  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:
							lowerCAmelCase        =  batch.convert_to_tensors(tensor_type="""np""")
					self.assertFalse(len(cs.err)	,   msg=f"should have no warning, but got {cs.err}")

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



			@require_torch
			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  BatchEncoding({"""inputs""": [[1, 2, 3], [4, 5, 6]], """labels""": [0, 1]})
					lowerCAmelCase        =  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:
							lowerCAmelCase        =  batch.convert_to_tensors(tensor_type="""pt""")
					self.assertFalse(len(cs.err)	,   msg=f"should have no warning, but got {cs.err}")

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



			@require_tf
			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  BatchEncoding({"""inputs""": [[1, 2, 3], [4, 5, 6]], """labels""": [0, 1]})
					lowerCAmelCase        =  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:
							lowerCAmelCase        =  batch.convert_to_tensors(tensor_type="""tf""")
					self.assertFalse(len(cs.err)	,   msg=f"should have no warning, but got {cs.err}")

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



			@require_flax
			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  BatchEncoding({"""inputs""": [[1, 2, 3], [4, 5, 6]], """labels""": [0, 1]})
					lowerCAmelCase        =  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:
							lowerCAmelCase        =  batch.convert_to_tensors(tensor_type="""jax""")
					self.assertFalse(len(cs.err)	,   msg=f"should have no warning, but got {cs.err}")

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



			def 							a_	(       self):




					"""simple docstring"""


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

					lowerCAmelCase        =  tokenizer.pad(__lowerCAmelCase	,   padding=__lowerCAmelCase)
					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]])
					lowerCAmelCase        =  tokenizer.pad(__lowerCAmelCase	,   padding=__lowerCAmelCase	,   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 							a_	(       self):




					"""simple docstring"""


					import torch

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

					lowerCAmelCase        =  tokenizer.pad(__lowerCAmelCase	,   padding=__lowerCAmelCase)
					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]])
					lowerCAmelCase        =  tokenizer.pad(__lowerCAmelCase	,   padding=__lowerCAmelCase	,   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 							a_	(       self):




					"""simple docstring"""


					import tensorflow as tf

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

					lowerCAmelCase        =  tokenizer.pad(__lowerCAmelCase	,   padding=__lowerCAmelCase)
					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]])
					lowerCAmelCase        =  tokenizer.pad(__lowerCAmelCase	,   padding=__lowerCAmelCase	,   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 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  Tokenizer(WordPiece(unk_token="""[UNK]"""))
					PreTrainedTokenizerFast(tokenizer_object=__lowerCAmelCase)







			@require_tokenizers
			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  Tokenizer(WordPiece(unk_token="""[UNK]"""))
					with tempfile.TemporaryDirectory() as tmpdirname:
							bert_tokenizer.save(os.path.join(__lowerCAmelCase	,   """tokenizer.json"""))
							PreTrainedTokenizerFast(tokenizer_file=os.path.join(__lowerCAmelCase	,   """tokenizer.json"""))




