


'''simple docstring'''
from __future__ import annotations

import copy
import inspect
import unittest

import numpy as np

from transformers import is_tf_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import (
      TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
      TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
      TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
      TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
      TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
      LayoutLMvaConfig,
      TFLayoutLMvaForQuestionAnswering,
      TFLayoutLMvaForSequenceClassification,
      TFLayoutLMvaForTokenClassification,
      TFLayoutLMvaModel,
  )

if is_vision_available():
  from PIL import Image

  from transformers import LayoutLMvaImageProcessor




class      lowerCamelCase_  :






      def __init__(   self							:       Optional[int]		,		_A							:       Optional[Any]		,		_A							:       Tuple=2		,		_A							:       Tuple=3		,		_A							:       Optional[Any]=4		,		_A							:       List[Any]=2		,		_A							:       List[Any]=7		,		_A							:       int=True		,		_A							:       Dict=True		,		_A							:       int=True		,		_A							:       Dict=True		,		_A							:       Tuple=99		,		_A							:       Union[str, Any]=36		,		_A							:       int=2		,		_A							:       List[str]=4		,		_A							:       int=37		,		_A							:       List[Any]="gelu"		,		_A							:       str=0.1		,		_A							:       str=0.1		,		_A							:       Tuple=512		,		_A							:       Dict=16		,		_A							:       Tuple=2		,		_A							:       Union[str, Any]=0.0_2		,		_A							:       Any=6		,		_A							:       Union[str, Any]=6		,		_A							:       str=3		,		_A							:       str=4		,		_A							:       Tuple=None		,		_A							:       int=1_000		,		):


          '''simple docstring'''


          UpperCAmelCase__     :						int						=							parent
          UpperCAmelCase__     :						Optional[int]						=							batch_size
          UpperCAmelCase__     :						str						=							num_channels
          UpperCAmelCase__     :						str						=							image_size
          UpperCAmelCase__     :						List[str]						=							patch_size
          UpperCAmelCase__     :						Any						=							is_training
          UpperCAmelCase__     :						List[str]						=							use_input_mask
          UpperCAmelCase__     :						Tuple						=							use_token_type_ids
          UpperCAmelCase__     :						str						=							use_labels
          UpperCAmelCase__     :						int						=							vocab_size
          UpperCAmelCase__     :						List[Any]						=							hidden_size
          UpperCAmelCase__     :						Optional[int]						=							num_hidden_layers
          UpperCAmelCase__     :						List[str]						=							num_attention_heads
          UpperCAmelCase__     :						Tuple						=							intermediate_size
          UpperCAmelCase__     :						Dict						=							hidden_act
          UpperCAmelCase__     :						int						=							hidden_dropout_prob
          UpperCAmelCase__     :						Optional[int]						=							attention_probs_dropout_prob
          UpperCAmelCase__     :						List[str]						=							max_position_embeddings
          UpperCAmelCase__     :						Tuple						=							type_vocab_size
          UpperCAmelCase__     :						Any						=							type_sequence_label_size
          UpperCAmelCase__     :						List[str]						=							initializer_range
          UpperCAmelCase__     :						List[str]						=							coordinate_size
          UpperCAmelCase__     :						Tuple						=							shape_size
          UpperCAmelCase__     :						Optional[int]						=							num_labels
          UpperCAmelCase__     :						Optional[Any]						=							num_choices
          UpperCAmelCase__     :						Union[str, Any]						=							scope
          UpperCAmelCase__     :						Optional[Any]						=							range_bbox

          # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
          UpperCAmelCase__     :						str						=							text_seq_length
          UpperCAmelCase__     :						Tuple						=							(image_size // patch_size) ** 2 + 1
          UpperCAmelCase__     :						Tuple						=							self.text_seq_length + self.image_seq_length






      def lowercase_				(   self							:       Union[str, Any]					):


          '''simple docstring'''


          UpperCAmelCase__     :						Dict						=							ids_tensor([self.batch_size, self.text_seq_length]		,		self.vocab_size					)

          UpperCAmelCase__     :						List[Any]						=							ids_tensor([self.batch_size, self.text_seq_length, 4]		,		self.range_bbox					)
          UpperCAmelCase__     :						int						=							bbox.numpy()
          # Ensure that bbox is legal
          for i in range(bbox.shape[0]					):
              for j in range(bbox.shape[1]					):
                  if bbox[i, j, 3] < bbox[i, j, 1]:
                      UpperCAmelCase__     :						str						=							bbox[i, j, 3]
                      UpperCAmelCase__     :						Dict						=							bbox[i, j, 1]
                      UpperCAmelCase__     :						str						=							tmp_coordinate
                  if bbox[i, j, 2] < bbox[i, j, 0]:
                      UpperCAmelCase__     :						Optional[int]						=							bbox[i, j, 2]
                      UpperCAmelCase__     :						Any						=							bbox[i, j, 0]
                      UpperCAmelCase__     :						List[Any]						=							tmp_coordinate
          UpperCAmelCase__     :						str						=							tf.constant(_A					)

          UpperCAmelCase__     :						Optional[int]						=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

          UpperCAmelCase__     :						Any						=							None
          if self.use_input_mask:
              UpperCAmelCase__     :						Any						=							random_attention_mask([self.batch_size, self.text_seq_length]					)

          UpperCAmelCase__     :						Any						=							None
          if self.use_token_type_ids:
              UpperCAmelCase__     :						List[str]						=							ids_tensor([self.batch_size, self.text_seq_length]		,		self.type_vocab_size					)

          UpperCAmelCase__     :						Optional[int]						=							None
          UpperCAmelCase__     :						List[str]						=							None
          if self.use_labels:
              UpperCAmelCase__     :						Any						=							ids_tensor([self.batch_size]		,		self.type_sequence_label_size					)
              UpperCAmelCase__     :						List[Any]						=							ids_tensor([self.batch_size, self.text_seq_length]		,		self.num_labels					)

          UpperCAmelCase__     :						Optional[int]						=							LayoutLMvaConfig(
              vocab_size=self.vocab_size		,		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		,		max_position_embeddings=self.max_position_embeddings		,		type_vocab_size=self.type_vocab_size		,		initializer_range=self.initializer_range		,		coordinate_size=self.coordinate_size		,		shape_size=self.shape_size		,		input_size=self.image_size		,		patch_size=self.patch_size		,		)

          return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels






      def lowercase_				(   self							:       Union[str, Any]		,		_A							:       int		,		_A							:       str		,		_A							:       Optional[int]		,		_A							:       Optional[int]		,		_A							:       List[str]		,		_A							:       List[Any]					):


          '''simple docstring'''


          UpperCAmelCase__     :						int						=							TFLayoutLMvaModel(config=_A					)

          # text + image
          UpperCAmelCase__     :						Tuple						=							model(_A		,		pixel_values=_A		,		training=_A					)
          UpperCAmelCase__     :						Tuple						=							model(
              _A		,		bbox=_A		,		pixel_values=_A		,		attention_mask=_A		,		token_type_ids=_A		,		training=_A		,		)
          UpperCAmelCase__     :						Optional[Any]						=							model(_A		,		bbox=_A		,		pixel_values=_A		,		training=_A					)

          self.parent.assertEqual(result.last_hidden_state.shape		,		(self.batch_size, self.seq_length, self.hidden_size)					)

          # text only
          UpperCAmelCase__     :						Any						=							model(_A		,		training=_A					)
          self.parent.assertEqual(
              result.last_hidden_state.shape		,		(self.batch_size, self.text_seq_length, self.hidden_size)					)

          # image only
          UpperCAmelCase__     :						str						=							model({'''pixel_values''': pixel_values}		,		training=_A					)
          self.parent.assertEqual(
              result.last_hidden_state.shape		,		(self.batch_size, self.image_seq_length, self.hidden_size)					)






      def lowercase_				(   self							:       Union[str, Any]		,		_A							:       Optional[int]		,		_A							:       Optional[Any]		,		_A							:       Dict		,		_A							:       List[Any]		,		_A							:       List[Any]		,		_A							:       Any		,		_A							:       Tuple					):


          '''simple docstring'''


          UpperCAmelCase__     :						Optional[int]						=							self.num_labels
          UpperCAmelCase__     :						int						=							TFLayoutLMvaForSequenceClassification(config=_A					)
          UpperCAmelCase__     :						Union[str, Any]						=							model(
              _A		,		bbox=_A		,		pixel_values=_A		,		attention_mask=_A		,		token_type_ids=_A		,		labels=_A		,		training=_A		,		)
          self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.num_labels)					)






      def lowercase_				(   self							:       Dict		,		_A							:       List[Any]		,		_A							:       Any		,		_A							:       Dict		,		_A							:       str		,		_A							:       Optional[int]		,		_A							:       str		,		_A							:       str					):


          '''simple docstring'''


          UpperCAmelCase__     :						List[Any]						=							self.num_labels
          UpperCAmelCase__     :						Union[str, Any]						=							TFLayoutLMvaForTokenClassification(config=_A					)
          UpperCAmelCase__     :						Optional[int]						=							model(
              _A		,		bbox=_A		,		pixel_values=_A		,		attention_mask=_A		,		token_type_ids=_A		,		labels=_A		,		training=_A		,		)
          self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.text_seq_length, self.num_labels)					)






      def lowercase_				(   self							:       Dict		,		_A							:       Dict		,		_A							:       List[str]		,		_A							:       Union[str, Any]		,		_A							:       int		,		_A							:       Tuple		,		_A							:       Dict		,		_A							:       str					):


          '''simple docstring'''


          UpperCAmelCase__     :						str						=							2
          UpperCAmelCase__     :						Dict						=							TFLayoutLMvaForQuestionAnswering(config=_A					)
          UpperCAmelCase__     :						str						=							model(
              _A		,		bbox=_A		,		pixel_values=_A		,		attention_mask=_A		,		token_type_ids=_A		,		start_positions=_A		,		end_positions=_A		,		training=_A		,		)
          self.parent.assertEqual(result.start_logits.shape		,		(self.batch_size, self.seq_length)					)
          self.parent.assertEqual(result.end_logits.shape		,		(self.batch_size, self.seq_length)					)






      def lowercase_				(   self							:       Tuple					):


          '''simple docstring'''


          UpperCAmelCase__     :						int						=							self.prepare_config_and_inputs()
          ((UpperCAmelCase__)		,		(UpperCAmelCase__)		,		(UpperCAmelCase__)		,		(UpperCAmelCase__)		,		(UpperCAmelCase__)		,		(UpperCAmelCase__)		,		(UpperCAmelCase__)		,		(UpperCAmelCase__))     :						List[str]						=							config_and_inputs
          UpperCAmelCase__     :						List[Any]						=							{
              '''input_ids''': input_ids,
              '''bbox''': bbox,
              '''pixel_values''': pixel_values,
              '''token_type_ids''': token_type_ids,
              '''attention_mask''': input_mask,
          }
          return config, inputs_dict




@require_tf
class      lowerCamelCase_  (      __a	,			__a	,			unittest.TestCase ):
      lowerCAmelCase__							=	(
          (
              TFLayoutLMvaModel,
              TFLayoutLMvaForQuestionAnswering,
              TFLayoutLMvaForSequenceClassification,
              TFLayoutLMvaForTokenClassification,
          )
          if is_tf_available()
          else ()
      )
      lowerCAmelCase__							=	(
          {'document-question-answering': TFLayoutLMvaForQuestionAnswering, 'feature-extraction': TFLayoutLMvaModel}
          if is_tf_available()
          else {}
      )

      lowerCAmelCase__							=	False
      lowerCAmelCase__							=	False
      lowerCAmelCase__							=	False






      def lowercase_				(   self							:       List[Any]		,		_A							:       Union[str, Any]		,		_A							:       str		,		_A							:       List[Any]		,		_A							:       Dict		,		_A							:       List[str]					):


          '''simple docstring'''


          return True






      def lowercase_				(   self							:       Optional[Any]		,		_A							:       Tuple		,		_A							:       Any		,		_A							:       Dict=False					):


          '''simple docstring'''


          UpperCAmelCase__     :						List[Any]						=							copy.deepcopy(_A					)

          if model_class in get_values(_A					):
              UpperCAmelCase__     :						Tuple						=							{
                  k: tf.tile(tf.expand_dims(_A		,		1					)		,		(1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)					)
                  if isinstance(_A		,		tf.Tensor					) and v.ndim > 0
                  else v
                  for k, v in inputs_dict.items()
              }

          if return_labels:
              if model_class in get_values(_A					):
                  UpperCAmelCase__     :						Dict						=							tf.ones(self.model_tester.batch_size		,		dtype=tf.intaa					)
              elif model_class in get_values(_A					):
                  UpperCAmelCase__     :						Tuple						=							tf.zeros(self.model_tester.batch_size		,		dtype=tf.intaa					)
                  UpperCAmelCase__     :						Dict						=							tf.zeros(self.model_tester.batch_size		,		dtype=tf.intaa					)
              elif model_class in get_values(_A					):
                  UpperCAmelCase__     :						Dict						=							tf.zeros(self.model_tester.batch_size		,		dtype=tf.intaa					)
              elif model_class in get_values(_A					):
                  UpperCAmelCase__     :						int						=							tf.zeros(
                      (self.model_tester.batch_size, self.model_tester.text_seq_length)		,		dtype=tf.intaa					)

          return inputs_dict






      def lowercase_				(   self							:       List[str]					):


          '''simple docstring'''


          UpperCAmelCase__     :						Any						=							TFLayoutLMvaModelTester(self					)
          UpperCAmelCase__     :						Tuple						=							ConfigTester(self		,		config_class=_A		,		hidden_size=37					)






      def lowercase_				(   self							:       str					):


          '''simple docstring'''


          self.config_tester.run_common_tests()






      def lowercase_				(   self							:       Optional[int]					):


          '''simple docstring'''


          UpperCAmelCase__		,		UpperCAmelCase__     :						Any						=							self.model_tester.prepare_config_and_inputs_for_common()
          for model_class in self.all_model_classes:
              UpperCAmelCase__     :						Optional[Any]						=							model_class(_A					)
              if getattr(_A		,		'''hf_compute_loss'''		,		_A					):
                  # The number of elements in the loss should be the same as the number of elements in the label
                  UpperCAmelCase__     :						Tuple						=							self._prepare_for_class(inputs_dict.copy()		,		_A		,		return_labels=_A					)
                  UpperCAmelCase__     :						List[Any]						=							prepared_for_class[
                      sorted(prepared_for_class.keys() - inputs_dict.keys()		,		reverse=_A					)[0]
                  ]
                  UpperCAmelCase__     :						Optional[Any]						=							added_label.shape.as_list()[:1]

                  # Test that model correctly compute the loss with kwargs
                  UpperCAmelCase__     :						Any						=							self._prepare_for_class(inputs_dict.copy()		,		_A		,		return_labels=_A					)
                  UpperCAmelCase__     :						Tuple						=							prepared_for_class.pop('''input_ids'''					)

                  UpperCAmelCase__     :						List[Any]						=							model(_A		,		**_A					)[0]
                  self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]					)

                  # Test that model correctly compute the loss when we mask some positions
                  UpperCAmelCase__     :						Union[str, Any]						=							self._prepare_for_class(inputs_dict.copy()		,		_A		,		return_labels=_A					)
                  UpperCAmelCase__     :						Tuple						=							prepared_for_class.pop('''input_ids'''					)
                  if "labels" in prepared_for_class:
                      UpperCAmelCase__     :						Optional[Any]						=							prepared_for_class['''labels'''].numpy()
                      if len(labels.shape					) > 1 and labels.shape[1] != 1:
                          UpperCAmelCase__     :						Any						=							-100
                          UpperCAmelCase__     :						Union[str, Any]						=							tf.convert_to_tensor(_A					)
                          UpperCAmelCase__     :						int						=							model(_A		,		**_A					)[0]
                          self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]					)
                          self.assertTrue(not np.any(np.isnan(loss.numpy()					)					)					)

                # Test that model correctly compute the loss with a dict
                  UpperCAmelCase__     :						Optional[int]						=							self._prepare_for_class(inputs_dict.copy()		,		_A		,		return_labels=_A					)
                  UpperCAmelCase__     :						Dict						=							model(_A					)[0]
                  self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]					)

                  # Test that model correctly compute the loss with a tuple
                  UpperCAmelCase__     :						Dict						=							self._prepare_for_class(inputs_dict.copy()		,		_A		,		return_labels=_A					)

                  # Get keys that were added with the _prepare_for_class function
                  UpperCAmelCase__     :						Optional[int]						=							prepared_for_class.keys() - inputs_dict.keys()
                  UpperCAmelCase__     :						int						=							inspect.signature(model.call					).parameters
                  UpperCAmelCase__     :						Union[str, Any]						=							list(signature.keys()					)

                  # Create a dictionary holding the location of the tensors in the tuple
                  UpperCAmelCase__     :						Dict						=							{0: '''input_ids'''}
                  for label_key in label_keys:
                      UpperCAmelCase__     :						str						=							signature_names.index(_A					)
                      UpperCAmelCase__     :						List[Any]						=							label_key
                  UpperCAmelCase__     :						Dict						=							sorted(tuple_index_mapping.items()					)
                  # Initialize a list with their default values, update the values and convert to a tuple
                  UpperCAmelCase__     :						Tuple						=							[]

                  for name in signature_names:
                      if name != "kwargs":
                          list_input.append(signature[name].default					)

                  for index, value in sorted_tuple_index_mapping:
                      UpperCAmelCase__     :						Any						=							prepared_for_class[value]

                  UpperCAmelCase__     :						Tuple						=							tuple(_A					)

                  # Send to model
                  UpperCAmelCase__     :						Optional[Any]						=							model(tuple_input[:-1]					)[0]

                  self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]					)






      def lowercase_				(   self							:       int					):


          '''simple docstring'''


          (
              (
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		
          )     :						List[Any]						=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(_A		,		_A		,		_A		,		_A		,		_A		,		_A					)






      def lowercase_				(   self							:       Tuple					):


          '''simple docstring'''


          (
              (
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		
          )     :						int						=							self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
              UpperCAmelCase__     :						Union[str, Any]						=							type
              self.model_tester.create_and_check_model(_A		,		_A		,		_A		,		_A		,		_A		,		_A					)






      def lowercase_				(   self							:       List[str]					):


          '''simple docstring'''


          (
              (
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		
          )     :						Dict						=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(
              _A		,		_A		,		_A		,		_A		,		_A		,		_A		,		_A					)






      def lowercase_				(   self							:       Any					):


          '''simple docstring'''


          (
              (
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		
          )     :						List[str]						=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(
              _A		,		_A		,		_A		,		_A		,		_A		,		_A		,		_A					)






      def lowercase_				(   self							:       Optional[int]					):


          '''simple docstring'''


          (
              (
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		
          )     :						Optional[int]						=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(
              _A		,		_A		,		_A		,		_A		,		_A		,		_A		,		_A					)






      @slow
      def lowercase_				(   self							:       List[Any]					):


          '''simple docstring'''


          for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase__     :						List[str]						=							TFLayoutLMvaModel.from_pretrained(_A					)
              self.assertIsNotNone(_A					)





def    a__			(   )       ->      List[str]:
    UpperCAmelCase__     :						Dict						=							Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
    return image







@require_tf
class      lowerCamelCase_  (      unittest.TestCase ):






      @cached_property
      def lowercase_				(   self							:       Dict					):


          '''simple docstring'''


          return LayoutLMvaImageProcessor(apply_ocr=_A					) if is_vision_available() else None






      @slow
      def lowercase_				(   self							:       int					):


          '''simple docstring'''


          UpperCAmelCase__     :						str						=							TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''					)

          UpperCAmelCase__     :						Dict						=							self.default_image_processor
          UpperCAmelCase__     :						Any						=							prepare_img()
          UpperCAmelCase__     :						int						=							image_processor(images=_A		,		return_tensors='''tf'''					).pixel_values

          UpperCAmelCase__     :						str						=							tf.constant([[1, 2]]					)
          UpperCAmelCase__     :						Optional[Any]						=							tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]					)		,		axis=0					)

          # forward pass
          UpperCAmelCase__     :						int						=							model(input_ids=_A		,		bbox=_A		,		pixel_values=_A		,		training=_A					)

          # verify the logits
          UpperCAmelCase__     :						Optional[int]						=							(1, 199, 768)
          self.assertEqual(outputs.last_hidden_state.shape		,		_A					)

          UpperCAmelCase__     :						Dict						=							tf.constant(
              [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]]					)

          self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3]		,		_A		,		atol=1e-4					)					)



import json
import os
import unittest

from transformers.models.roc_bert.tokenization_roc_bert import (
    VOCAB_FILES_NAMES,
    RoCBertBasicTokenizer,
    RoCBertTokenizer,
    RoCBertWordpieceTokenizer,
    _is_control,
    _is_punctuation,
    _is_whitespace,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english



@require_tokenizers
class        _a		(__magic_name__						,  unittest.TestCase							):


					'''simple docstring'''






					UpperCAmelCase__:  List[str]    				=			RoCBertTokenizer
					UpperCAmelCase__:  Dict    				=			None
					UpperCAmelCase__:  Optional[Any]    				=			False
					UpperCAmelCase__:  Union[str, Any]    				=			True
					UpperCAmelCase__:  Union[str, Any]    				=			filter_non_english






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

						A__				:       Tuple	  =						["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """你""", """好""", """是""", """谁""", """a""", """b""", """c""", """d"""]
						A__				:       Union[str, Any]	  =						{}
						A__				:       Dict	  =						{}
						for i, value in enumerate(A__     ):
							A__				:       Optional[int]	  =						i
							A__				:       Optional[int]	  =						i
						A__				:       Dict	  =						os.path.join(self.tmpdirname       ,			VOCAB_FILES_NAMES["""vocab_file"""]     )
						A__				:       Optional[Any]	  =						os.path.join(self.tmpdirname       ,			VOCAB_FILES_NAMES["""word_shape_file"""]     )
						A__				:       int	  =						os.path.join(self.tmpdirname       ,			VOCAB_FILES_NAMES["""word_pronunciation_file"""]     )
						with open(self.vocab_file       ,			"""w"""       ,			encoding="""utf-8"""     ) as vocab_writer:
							vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]     )     )
						with open(self.word_shape_file       ,			"""w"""       ,			encoding="""utf-8"""     ) as word_shape_writer:
							json.dump(A__       ,			A__       ,			ensure_ascii=A__     )
						with open(self.word_pronunciation_file       ,			"""w"""       ,			encoding="""utf-8"""     ) as word_pronunciation_writer:
							json.dump(A__       ,			A__       ,			ensure_ascii=A__     )






					def     __A     (					self     ):
						A__				:       Dict	  =						self.tokenizer_class(self.vocab_file       ,			self.word_shape_file       ,			self.word_pronunciation_file     )

						A__				:       Optional[Any]	  =						tokenizer.tokenize("""你好[SEP]你是谁"""     )
						self.assertListEqual(A__       ,			["""你""", """好""", """[SEP]""", """你""", """是""", """谁"""]     )
						self.assertListEqual(tokenizer.convert_tokens_to_ids(A__     )       ,			[5, 6, 2, 5, 7, 8]     )
						self.assertListEqual(tokenizer.convert_tokens_to_shape_ids(A__     )       ,			[5, 6, 2, 5, 7, 8]     )
						self.assertListEqual(tokenizer.convert_tokens_to_pronunciation_ids(A__     )       ,			[5, 6, 2, 5, 7, 8]     )






					def     __A     (					self     ):
						A__				:       Dict	  =						RoCBertBasicTokenizer()

						self.assertListEqual(tokenizer.tokenize("""ah\u535A\u63A8zz"""     )       ,			["""ah""", """\u535A""", """\u63A8""", """zz"""]     )






					def     __A     (					self     ):
						A__				:       List[str]	  =						RoCBertBasicTokenizer(do_lower_case=A__     )

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHeLLo!how  \n Are yoU?  """     )       ,			["""hello""", """!""", """how""", """are""", """you""", """?"""]     )
						self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""     )       ,			["""hello"""]     )






					def     __A     (					self     ):
						A__				:       int	  =						RoCBertBasicTokenizer(do_lower_case=A__       ,			strip_accents=A__     )

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """     )       ,			["""hällo""", """!""", """how""", """are""", """you""", """?"""]     )
						self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""     )       ,			["""h\u00E9llo"""]     )






					def     __A     (					self     ):
						A__				:       Optional[Any]	  =						RoCBertBasicTokenizer(do_lower_case=A__       ,			strip_accents=A__     )

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """     )       ,			["""hallo""", """!""", """how""", """are""", """you""", """?"""]     )
						self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""     )       ,			["""hello"""]     )






					def     __A     (					self     ):
						A__				:       Union[str, Any]	  =						RoCBertBasicTokenizer(do_lower_case=A__     )

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """     )       ,			["""hallo""", """!""", """how""", """are""", """you""", """?"""]     )
						self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""     )       ,			["""hello"""]     )






					def     __A     (					self     ):
						A__				:       Union[str, Any]	  =						RoCBertBasicTokenizer(do_lower_case=A__     )

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHeLLo!how  \n Are yoU?  """     )       ,			["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]     )






					def     __A     (					self     ):
						A__				:       Dict	  =						RoCBertBasicTokenizer(do_lower_case=A__       ,			strip_accents=A__     )

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """     )       ,			["""HäLLo""", """!""", """how""", """Are""", """yoU""", """?"""]     )






					def     __A     (					self     ):
						A__				:       Optional[Any]	  =						RoCBertBasicTokenizer(do_lower_case=A__       ,			strip_accents=A__     )

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """     )       ,			["""HaLLo""", """!""", """how""", """Are""", """yoU""", """?"""]     )






					def     __A     (					self     ):
						A__				:       Dict	  =						RoCBertBasicTokenizer(do_lower_case=A__       ,			never_split=["""[UNK]"""]     )

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHeLLo!how  \n Are yoU? [UNK]"""     )       ,			["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?""", """[UNK]"""]     )






					def     __A     (					self     ):
						A__				:       List[Any]	  =						["""[UNK]""", """[CLS]""", """[SEP]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing"""]

						A__				:       Any	  =						{}
						for i, token in enumerate(A__     ):
							A__				:       Optional[int]	  =						i
						A__				:       Optional[Any]	  =						RoCBertWordpieceTokenizer(vocab=A__       ,			unk_token="""[UNK]"""     )

						self.assertListEqual(tokenizer.tokenize(""""""     )       ,			[]     )

						self.assertListEqual(tokenizer.tokenize("""unwanted running"""     )       ,			["""un""", """##want""", """##ed""", """runn""", """##ing"""]     )

						self.assertListEqual(tokenizer.tokenize("""unwantedX running"""     )       ,			["""[UNK]""", """runn""", """##ing"""]     )






					def     __A     (					self     ):
						self.assertTrue(_is_whitespace(""" """     )     )
						self.assertTrue(_is_whitespace("""\t"""     )     )
						self.assertTrue(_is_whitespace("""\r"""     )     )
						self.assertTrue(_is_whitespace("""\n"""     )     )
						self.assertTrue(_is_whitespace("""\u00A0"""     )     )

						self.assertFalse(_is_whitespace("""A"""     )     )
						self.assertFalse(_is_whitespace("""-"""     )     )






					def     __A     (					self     ):
						self.assertTrue(_is_control("""\u0005"""     )     )

						self.assertFalse(_is_control("""A"""     )     )
						self.assertFalse(_is_control(""" """     )     )
						self.assertFalse(_is_control("""\t"""     )     )
						self.assertFalse(_is_control("""\r"""     )     )






					def     __A     (					self     ):
						self.assertTrue(_is_punctuation("""-"""     )     )
						self.assertTrue(_is_punctuation("""$"""     )     )
						self.assertTrue(_is_punctuation("""`"""     )     )
						self.assertTrue(_is_punctuation("""."""     )     )

						self.assertFalse(_is_punctuation("""A"""     )     )
						self.assertFalse(_is_punctuation(""" """     )     )






					def     __A     (					self     ):
						A__				:       Optional[int]	  =						self.get_tokenizer()

						# Example taken from the issue https://github.com/huggingface/tokenizers/issues/340
						self.assertListEqual([tokenizer.tokenize(A__     ) for t in ["""Test""", """\xad""", """test"""]]       ,			[["""[UNK]"""], [], ["""[UNK]"""]]     )

						if self.test_rust_tokenizer:
							A__				:       Tuple	  =						self.get_rust_tokenizer()
							self.assertListEqual(
							    [rust_tokenizer.tokenize(A__     ) for t in ["""Test""", """\xad""", """test"""]]       ,			[["""[UNK]"""], [], ["""[UNK]"""]]     )






					def     __A     (					self     ):
						for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
							with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""     ):
								A__				:       Dict	  =						self.rust_tokenizer_class.from_pretrained(A__       ,			**A__     )

								A__				:       str	  =						F"""A, naïve {tokenizer_r.mask_token} AllenNLP sentence."""
								A__				:       Union[str, Any]	  =						tokenizer_r.encode_plus(
								    A__       ,			return_attention_mask=A__       ,			return_token_type_ids=A__       ,			return_offsets_mapping=A__       ,			add_special_tokens=A__       ,			)

								A__				:       Any	  =						tokenizer_r.do_lower_case if hasattr(A__       ,			"""do_lower_case"""     ) else False
								A__				:       List[str]	  =						(
								    [
								        ((0, 0), tokenizer_r.cls_token),
								        ((0, 1), """A"""),
								        ((1, 2), ""","""),
								        ((3, 5), """na"""),
								        ((5, 6), """##ï"""),
								        ((6, 8), """##ve"""),
								        ((9, 15), tokenizer_r.mask_token),
								        ((16, 21), """Allen"""),
								        ((21, 23), """##NL"""),
								        ((23, 24), """##P"""),
								        ((25, 33), """sentence"""),
								        ((33, 34), """."""),
								        ((0, 0), tokenizer_r.sep_token),
								    ]
								    if not do_lower_case
								    else [
								        ((0, 0), tokenizer_r.cls_token),
								        ((0, 1), """a"""),
								        ((1, 2), ""","""),
								        ((3, 8), """naive"""),
								        ((9, 15), tokenizer_r.mask_token),
								        ((16, 21), """allen"""),
								        ((21, 23), """##nl"""),
								        ((23, 24), """##p"""),
								        ((25, 33), """sentence"""),
								        ((33, 34), """."""),
								        ((0, 0), tokenizer_r.sep_token),
								    ]
								)

								self.assertEqual(
								    [e[1] for e in expected_results]       ,			tokenizer_r.convert_ids_to_tokens(tokens["""input_ids"""]     )     )
								self.assertEqual([e[0] for e in expected_results]       ,			tokens["""offset_mapping"""]     )






					def     __A     (					self     ):
						A__				:       Union[str, Any]	  =						["""的""", """人""", """有"""]
						A__				:       List[str]	  =						"""""".join(A__     )
						for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
							with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""     ):
								A__				:       Any	  =						True
								A__				:       int	  =						self.tokenizer_class.from_pretrained(A__       ,			**A__     )
								A__				:       Dict	  =						self.rust_tokenizer_class.from_pretrained(A__       ,			**A__     )

								A__				:       List[str]	  =						tokenizer_p.encode(A__       ,			add_special_tokens=A__     )
								A__				:       List[Any]	  =						tokenizer_r.encode(A__       ,			add_special_tokens=A__     )

								A__				:       Tuple	  =						tokenizer_r.convert_ids_to_tokens(A__     )
								A__				:       int	  =						tokenizer_p.convert_ids_to_tokens(A__     )

								# it is expected that each Chinese character is not preceded by "##"
								self.assertListEqual(A__       ,			A__     )
								self.assertListEqual(A__       ,			A__     )

								A__				:       Optional[int]	  =						False
								A__				:       Union[str, Any]	  =						self.rust_tokenizer_class.from_pretrained(A__       ,			**A__     )
								A__				:       str	  =						self.tokenizer_class.from_pretrained(A__       ,			**A__     )

								A__				:       Tuple	  =						tokenizer_r.encode(A__       ,			add_special_tokens=A__     )
								A__				:       List[str]	  =						tokenizer_p.encode(A__       ,			add_special_tokens=A__     )

								A__				:       Union[str, Any]	  =						tokenizer_r.convert_ids_to_tokens(A__     )
								A__				:       Tuple	  =						tokenizer_p.convert_ids_to_tokens(A__     )

								# it is expected that only the first Chinese character is not preceded by "##".
								A__				:       str	  =						[
								    F"""##{token}""" if idx != 0 else token for idx, token in enumerate(A__     )
								]
								self.assertListEqual(A__       ,			A__     )
								self.assertListEqual(A__       ,			A__     )






					@slow
					def     __A     (					self     ):
						A__				:       str	  =						self.tokenizer_class(self.vocab_file       ,			self.word_shape_file       ,			self.word_pronunciation_file     )

						A__				:       Optional[Any]	  =						tokenizer.encode("""你好"""       ,			add_special_tokens=A__     )
						A__				:       List[Any]	  =						tokenizer.encode("""你是谁"""       ,			add_special_tokens=A__     )

						A__				:       Any	  =						tokenizer.build_inputs_with_special_tokens(A__     )
						A__				:       Any	  =						tokenizer.build_inputs_with_special_tokens(A__       ,			A__     )

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






					def     __A     (					self     ):
						A__				:       List[str]	  =						self.get_tokenizers(do_lower_case=A__     )
						for tokenizer in tokenizers:
							with self.subTest(F"""{tokenizer.__class__.__name__}"""     ):
								A__				:       Optional[int]	  =						"""你好，你是谁"""
								A__				:       List[str]	  =						tokenizer.tokenize(A__     )
								A__				:       Union[str, Any]	  =						tokenizer.convert_tokens_to_ids(A__     )
								A__				:       str	  =						tokenizer.convert_tokens_to_shape_ids(A__     )
								A__				:       Optional[int]	  =						tokenizer.convert_tokens_to_pronunciation_ids(A__     )
								A__				:       Union[str, Any]	  =						tokenizer.prepare_for_model(
								    A__       ,			A__       ,			A__       ,			add_special_tokens=A__     )

								A__				:       int	  =						tokenizer.encode_plus(A__       ,			add_special_tokens=A__     )

								self.assertEqual(A__       ,			A__     )



