


'''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					)					)




'''simple docstring'''
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow


if is_torch_available():
		import torch

		from transformers import XLMRobertaModel
@require_sentencepiece
@require_tokenizers
@require_torch
class   lowercase      (				unittest.TestCase   ):

		"""simple docstring"""

		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Optional[Any]				   =					XLMRobertaModel.from_pretrained('''xlm-roberta-base'''				)
							UpperCamelCase__  :List[str]				   =					torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]				)
							# The dog is cute and lives in the garden house

							UpperCamelCase__  :Union[str, Any]				   =					torch.Size((1, 12, 768)				)  # batch_size, sequence_length, embedding_vector_dim
							UpperCamelCase__  :Union[str, Any]				   =					torch.tensor(
							    [[-0.0101, 0.1218, -0.0803, 0.0801, 0.1327, 0.0776, -0.1215, 0.2383, 0.3338, 0.3106, 0.0300, 0.0252]]				)
							#  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base')
							#  xlmr.eval()
							#  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
							with torch.no_grad():
												UpperCamelCase__  :Any				   =					model(UpperCamelCase_				)['''last_hidden_state'''].detach()
							self.assertEqual(output.shape       ,  UpperCamelCase_				)
							# compare the actual values for a slice of last dim
							self.assertTrue(torch.allclose(output[:, :, -1]       ,  UpperCamelCase_       ,  atol=1e-3				)				)




		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :int				   =					XLMRobertaModel.from_pretrained('''xlm-roberta-large'''				)
							UpperCamelCase__  :List[str]				   =					torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]				)
							# The dog is cute and lives in the garden house

							UpperCamelCase__  :Any				   =					torch.Size((1, 12, 1024)				)  # batch_size, sequence_length, embedding_vector_dim
							UpperCamelCase__  :List[Any]				   =					torch.tensor(
							    [[-0.0699, -0.0318, 0.0705, -0.1241, 0.0999, -0.0520, 0.1004, -0.1838, -0.4704, 0.1437, 0.0821, 0.0126]]				)
							#  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large')
							#  xlmr.eval()
							#  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
							with torch.no_grad():
												UpperCamelCase__  :Optional[int]				   =					model(UpperCamelCase_				)['''last_hidden_state'''].detach()
							self.assertEqual(output.shape       ,  UpperCamelCase_				)
							# compare the actual values for a slice of last dim
							self.assertTrue(torch.allclose(output[:, :, -1]       ,  UpperCamelCase_       ,  atol=1e-3				)				)