
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 UpperCAmelCase   :





   '''simple docstring'''


   def __init__(					self     :							List[Any]			,			lowerCAmelCase_     :							List[Any]			,			lowerCAmelCase_     :							Any=2			,			lowerCAmelCase_     :							Optional[int]=3			,			lowerCAmelCase_     :							List[Any]=4			,			lowerCAmelCase_     :							Optional[Any]=2			,			lowerCAmelCase_     :							List[Any]=7			,			lowerCAmelCase_     :							int=True			,			lowerCAmelCase_     :							Tuple=True			,			lowerCAmelCase_     :							str=True			,			lowerCAmelCase_     :							int=True			,			lowerCAmelCase_     :							Optional[Any]=9_9			,			lowerCAmelCase_     :							Dict=3_6			,			lowerCAmelCase_     :							List[Any]=2			,			lowerCAmelCase_     :							List[Any]=4			,			lowerCAmelCase_     :							str=3_7			,			lowerCAmelCase_     :							int="gelu"			,			lowerCAmelCase_     :							Optional[int]=0.1			,			lowerCAmelCase_     :							Optional[Any]=0.1			,			lowerCAmelCase_     :							int=5_1_2			,			lowerCAmelCase_     :							List[Any]=1_6			,			lowerCAmelCase_     :							str=2			,			lowerCAmelCase_     :							List[str]=0.02			,			lowerCAmelCase_     :							List[Any]=6			,			lowerCAmelCase_     :							int=6			,			lowerCAmelCase_     :							Optional[Any]=3			,			lowerCAmelCase_     :							Any=4			,			lowerCAmelCase_     :							List[Any]=None			,			lowerCAmelCase_     :							Union[str, Any]=1_0_0_0			,			):

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

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


   def  __magic_name__				(					self     :							int     ):

      """simple docstring"""
      _A:  str					=       ids_tensor([self.batch_size, self.text_seq_length]			,			self.vocab_size     )

      _A:  Optional[int]					=       ids_tensor([self.batch_size, self.text_seq_length, 4]			,			self.range_bbox     )
      _A:  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]:
               _A:  List[str]					=       bbox[i, j, 3]
               _A:  int					=       bbox[i, j, 1]
               _A:  List[str]					=       tmp_coordinate
            if bbox[i, j, 2] < bbox[i, j, 0]:
               _A:  Union[str, Any]					=       bbox[i, j, 2]
               _A:  Dict					=       bbox[i, j, 0]
               _A:  str					=       tmp_coordinate
      _A:  Optional[int]					=       tf.constant(lowerCAmelCase_     )

      _A:  Union[str, Any]					=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

      _A:  Dict					=       None
      if self.use_input_mask:
         _A:  int					=       random_attention_mask([self.batch_size, self.text_seq_length]     )

      _A:  Union[str, Any]					=       None
      if self.use_token_type_ids:
         _A:  Optional[Any]					=       ids_tensor([self.batch_size, self.text_seq_length]			,			self.type_vocab_size     )

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

      _A:  Optional[Any]					=       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  __magic_name__				(					self     :							Optional[Any]			,			lowerCAmelCase_     :							List[str]			,			lowerCAmelCase_     :							List[Any]			,			lowerCAmelCase_     :							str			,			lowerCAmelCase_     :							List[Any]			,			lowerCAmelCase_     :							Union[str, Any]			,			lowerCAmelCase_     :							Optional[Any]     ):

      """simple docstring"""
      _A:  str					=       TFLayoutLMvaModel(config=lowerCAmelCase_     )

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

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

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

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


   def  __magic_name__				(					self     :							Union[str, Any]			,			lowerCAmelCase_     :							Optional[Any]			,			lowerCAmelCase_     :							List[Any]			,			lowerCAmelCase_     :							Union[str, Any]			,			lowerCAmelCase_     :							str			,			lowerCAmelCase_     :							Union[str, Any]			,			lowerCAmelCase_     :							Optional[int]			,			lowerCAmelCase_     :							List[Any]     ):

      """simple docstring"""
      _A:  Dict					=       self.num_labels
      _A:  Tuple					=       TFLayoutLMvaForSequenceClassification(config=lowerCAmelCase_     )
      _A:  Union[str, Any]					=       model(
          lowerCAmelCase_			,			bbox=lowerCAmelCase_			,			pixel_values=lowerCAmelCase_			,			attention_mask=lowerCAmelCase_			,			token_type_ids=lowerCAmelCase_			,			labels=lowerCAmelCase_			,			training=lowerCAmelCase_			,			)
      self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.num_labels)     )


   def  __magic_name__				(					self     :							Any			,			lowerCAmelCase_     :							int			,			lowerCAmelCase_     :							Tuple			,			lowerCAmelCase_     :							Union[str, Any]			,			lowerCAmelCase_     :							Dict			,			lowerCAmelCase_     :							Any			,			lowerCAmelCase_     :							str			,			lowerCAmelCase_     :							Dict     ):

      """simple docstring"""
      _A:  List[Any]					=       self.num_labels
      _A:  List[str]					=       TFLayoutLMvaForTokenClassification(config=lowerCAmelCase_     )
      _A:  List[str]					=       model(
          lowerCAmelCase_			,			bbox=lowerCAmelCase_			,			pixel_values=lowerCAmelCase_			,			attention_mask=lowerCAmelCase_			,			token_type_ids=lowerCAmelCase_			,			labels=lowerCAmelCase_			,			training=lowerCAmelCase_			,			)
      self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.text_seq_length, self.num_labels)     )


   def  __magic_name__				(					self     :							int			,			lowerCAmelCase_     :							Any			,			lowerCAmelCase_     :							int			,			lowerCAmelCase_     :							str			,			lowerCAmelCase_     :							int			,			lowerCAmelCase_     :							List[str]			,			lowerCAmelCase_     :							Optional[Any]			,			lowerCAmelCase_     :							Union[str, Any]     ):

      """simple docstring"""
      _A:  Union[str, Any]					=       2
      _A:  Tuple					=       TFLayoutLMvaForQuestionAnswering(config=lowerCAmelCase_     )
      _A:  Any					=       model(
          lowerCAmelCase_			,			bbox=lowerCAmelCase_			,			pixel_values=lowerCAmelCase_			,			attention_mask=lowerCAmelCase_			,			token_type_ids=lowerCAmelCase_			,			start_positions=lowerCAmelCase_			,			end_positions=lowerCAmelCase_			,			training=lowerCAmelCase_			,			)
      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  __magic_name__				(					self     :							Optional[int]     ):

      """simple docstring"""
      _A:  Tuple					=       self.prepare_config_and_inputs()
      ((_A)			,  (_A)			,  (_A)			,  (_A)			,  (_A)			,  (_A)			,  (_A)			,  (_A)):  List[Any]					=       config_and_inputs
      _A:  List[str]					=       {
          '''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 UpperCAmelCase   (						SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__							,							unittest.TestCase   ):





   '''simple docstring'''
   __UpperCamelCase		:    List[str] 				=    (
       (
           TFLayoutLMvaModel,
           TFLayoutLMvaForQuestionAnswering,
           TFLayoutLMvaForSequenceClassification,
           TFLayoutLMvaForTokenClassification,
       )
       if is_tf_available()
       else ()
   )
   __UpperCamelCase		:    Any 				=    (
       {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel}
       if is_tf_available()
       else {}
   )

   __UpperCamelCase		:    Optional[Any] 				=    False
   __UpperCamelCase		:    List[Any] 				=    False
   __UpperCamelCase		:    Dict 				=    False


   def  __magic_name__				(					self     :							Any			,			lowerCAmelCase_     :							Optional[Any]			,			lowerCAmelCase_     :							int			,			lowerCAmelCase_     :							Optional[int]			,			lowerCAmelCase_     :							Tuple			,			lowerCAmelCase_     :							Union[str, Any]     ):

      """simple docstring"""
      return True


   def  __magic_name__				(					self     :							Union[str, Any]			,			lowerCAmelCase_     :							str			,			lowerCAmelCase_     :							Tuple			,			lowerCAmelCase_     :							Tuple=False     ):

      """simple docstring"""
      _A:  List[Any]					=       copy.deepcopy(lowerCAmelCase_     )

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

      if return_labels:
         if model_class in get_values(lowerCAmelCase_     ):
            _A:  Optional[Any]					=       tf.ones(self.model_tester.batch_size			,			dtype=tf.intaa     )
         elif model_class in get_values(lowerCAmelCase_     ):
            _A:  Optional[Any]					=       tf.zeros(self.model_tester.batch_size			,			dtype=tf.intaa     )
            _A:  List[Any]					=       tf.zeros(self.model_tester.batch_size			,			dtype=tf.intaa     )
         elif model_class in get_values(lowerCAmelCase_     ):
            _A:  int					=       tf.zeros(self.model_tester.batch_size			,			dtype=tf.intaa     )
         elif model_class in get_values(lowerCAmelCase_     ):
            _A:  Optional[Any]					=       tf.zeros(
                (self.model_tester.batch_size, self.model_tester.text_seq_length)			,			dtype=tf.intaa     )

      return inputs_dict


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

      """simple docstring"""
      _A:  str					=       TFLayoutLMvaModelTester(self     )
      _A:  Optional[int]					=       ConfigTester(self			,			config_class=lowerCAmelCase_			,			hidden_size=3_7     )


   def  __magic_name__				(					self     :							Optional[int]     ):

      """simple docstring"""
      self.config_tester.run_common_tests()


   def  __magic_name__				(					self     :							int     ):

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

            # Test that model correctly compute the loss with kwargs
            _A:  List[str]					=       self._prepare_for_class(inputs_dict.copy()			,			lowerCAmelCase_			,			return_labels=lowerCAmelCase_     )
            _A:  Union[str, Any]					=       prepared_for_class.pop('''input_ids'''     )

            _A:  List[Any]					=       model(lowerCAmelCase_			,			**lowerCAmelCase_     )[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
            _A:  List[str]					=       self._prepare_for_class(inputs_dict.copy()			,			lowerCAmelCase_			,			return_labels=lowerCAmelCase_     )
            _A:  Tuple					=       prepared_for_class.pop('''input_ids'''     )
            if "labels" in prepared_for_class:
               _A:  Dict					=       prepared_for_class['''labels'''].numpy()
               if len(labels.shape     ) > 1 and labels.shape[1] != 1:
                  _A:  Optional[int]					=       -1_0_0
                  _A:  Tuple					=       tf.convert_to_tensor(lowerCAmelCase_     )
                  _A:  Dict					=       model(lowerCAmelCase_			,			**lowerCAmelCase_     )[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
            _A:  Optional[int]					=       self._prepare_for_class(inputs_dict.copy()			,			lowerCAmelCase_			,			return_labels=lowerCAmelCase_     )
            _A:  Optional[Any]					=       model(lowerCAmelCase_     )[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
            _A:  str					=       self._prepare_for_class(inputs_dict.copy()			,			lowerCAmelCase_			,			return_labels=lowerCAmelCase_     )

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

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

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

            for index, value in sorted_tuple_index_mapping:
               _A:  Optional[int]					=       prepared_for_class[value]

            _A:  Any					=       tuple(lowerCAmelCase_     )

            # Send to model
            _A:  Dict					=       model(tuple_input[:-1]     )[0]

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


   def  __magic_name__				(					self     :							Dict     ):

      """simple docstring"""
      (
          (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  
      ):  List[Any]					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_     )


   def  __magic_name__				(					self     :							Dict     ):

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


   def  __magic_name__				(					self     :							List[str]     ):

      """simple docstring"""
      (
          (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  
      ):  Optional[Any]					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(
          lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_     )


   def  __magic_name__				(					self     :							Optional[Any]     ):

      """simple docstring"""
      (
          (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  
      ):  List[str]					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(
          lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_     )


   def  __magic_name__				(					self     :							Optional[Any]     ):

      """simple docstring"""
      (
          (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  
      ):  List[str]					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(
          lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_     )


   @slow
   def  __magic_name__				(					self     :							int     ):

      """simple docstring"""
      for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _A:  Union[str, Any]					=       TFLayoutLMvaModel.from_pretrained(lowerCAmelCase_     )
         self.assertIsNotNone(lowerCAmelCase_     )






def   lowerCamelCase__							(					)      ->   int:
   _A:  Optional[Any]					=       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
   return image






@require_tf
class UpperCAmelCase   (						unittest.TestCase   ):





   '''simple docstring'''


   @cached_property
   def  __magic_name__				(					self     :							Optional[int]     ):

      """simple docstring"""
      return LayoutLMvaImageProcessor(apply_ocr=lowerCAmelCase_     ) if is_vision_available() else None


   @slow
   def  __magic_name__				(					self     :							Union[str, Any]     ):

      """simple docstring"""
      _A:  Optional[Any]					=       TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''     )

      _A:  Optional[int]					=       self.default_image_processor
      _A:  Union[str, Any]					=       prepare_img()
      _A:  str					=       image_processor(images=lowerCAmelCase_			,			return_tensors='''tf'''     ).pixel_values

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

      # forward pass
      _A:  Optional[Any]					=       model(input_ids=lowerCAmelCase_			,			bbox=lowerCAmelCase_			,			pixel_values=lowerCAmelCase_			,			training=lowerCAmelCase_     )

      # verify the logits
      _A:  str					=       (1, 1_9_9, 7_6_8)
      self.assertEqual(outputs.last_hidden_state.shape			,			lowerCAmelCase_     )

      _A:  Union[str, Any]					=       tf.constant(
          [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]     )

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





import os

import pytest
import yaml

from datasets.features.features import Features, Value
from datasets.info import DatasetInfo, DatasetInfosDict






@pytest.mark.parametrize(
    '''files'''		,		[
        ['''full:README.md''', '''dataset_infos.json'''],
        ['''empty:README.md''', '''dataset_infos.json'''],
        ['''dataset_infos.json'''],
        ['''full:README.md'''],
    ]		,		)
def   lowerCamelCase__							(					a		,		a      )      ->   Any:
   _A:  Any					=       tmp_path_factory.mktemp('''dset_infos_dir'''      )
   if "full:README.md" in files:
      with open(dataset_infos_dir / '''README.md'''		,		'''w'''      ) as f:
         f.write('''---\ndataset_info:\n  dataset_size: 42\n---'''      )
   if "empty:README.md" in files:
      with open(dataset_infos_dir / '''README.md'''		,		'''w'''      ) as f:
         f.write(''''''      )
    # we want to support dataset_infos.json for backward compatibility
   if "dataset_infos.json" in files:
      with open(dataset_infos_dir / '''dataset_infos.json'''		,		'''w'''      ) as f:
         f.write('''{"default": {"dataset_size": 42}}'''      )
   _A:  Optional[int]					=       DatasetInfosDict.from_directory(a      )
   assert dataset_infos
   assert dataset_infos["default"].dataset_size == 42






@pytest.mark.parametrize(
    '''dataset_info'''		,		[
        DatasetInfo(),
        DatasetInfo(
            description='''foo'''		,		features=Features({'''a''': Value('''int32'''      )}      )		,		builder_name='''builder'''		,		config_name='''config'''		,		version='''1.0.0'''		,		splits=[{'''name''': '''train'''}]		,		download_size=42		,		),
    ]		,		)
def   lowerCamelCase__							(					a		,		a      )      ->   Any:
   _A:  int					=       str(a      )
   dataset_info.write_to_directory(a      )
   _A:  str					=       DatasetInfo.from_directory(a      )
   assert dataset_info == reloaded
   assert os.path.exists(os.path.join(a		,		'''dataset_info.json'''      )      )






def   lowerCamelCase__							(					)      ->   Any:
   _A:  int					=       DatasetInfo(
       description='''foo'''		,		citation='''bar'''		,		homepage='''https://foo.bar'''		,		license='''CC0'''		,		features=Features({'''a''': Value('''int32'''      )}      )		,		post_processed={}		,		supervised_keys=()		,		task_templates=[]		,		builder_name='''builder'''		,		config_name='''config'''		,		version='''1.0.0'''		,		splits=[{'''name''': '''train''', '''num_examples''': 42}]		,		download_checksums={}		,		download_size=13_37		,		post_processing_size=4_42		,		dataset_size=12_34		,		size_in_bytes=13_37 + 4_42 + 12_34		,		)
   _A:  Optional[Any]					=       dataset_info._to_yaml_dict()
   assert sorted(a      ) == sorted(DatasetInfo._INCLUDED_INFO_IN_YAML      )
   for key in DatasetInfo._INCLUDED_INFO_IN_YAML:
      assert key in dataset_info_yaml_dict
      assert isinstance(dataset_info_yaml_dict[key]		,		(list, dict, int, str)      )
   _A:  str					=       yaml.safe_dump(a      )
   _A:  Optional[int]					=       yaml.safe_load(a      )
   assert dataset_info_yaml_dict == reloaded






def   lowerCamelCase__							(					)      ->   int:
   _A:  Union[str, Any]					=       DatasetInfo()
   _A:  Union[str, Any]					=       dataset_info._to_yaml_dict()
   assert dataset_info_yaml_dict == {}






@pytest.mark.parametrize(
    '''dataset_infos_dict'''		,		[
        DatasetInfosDict(),
        DatasetInfosDict({'''default''': DatasetInfo()}      ),
        DatasetInfosDict({'''my_config_name''': DatasetInfo()}      ),
        DatasetInfosDict(
            {
                '''default''': DatasetInfo(
                    description='''foo'''		,		features=Features({'''a''': Value('''int32'''      )}      )		,		builder_name='''builder'''		,		config_name='''config'''		,		version='''1.0.0'''		,		splits=[{'''name''': '''train'''}]		,		download_size=42		,		)
            }      ),
        DatasetInfosDict(
            {
                '''v1''': DatasetInfo(dataset_size=42      ),
                '''v2''': DatasetInfo(dataset_size=13_37      ),
            }      ),
    ]		,		)
def   lowerCamelCase__							(					a		,		a      )      ->   Optional[int]:
   _A:  Optional[int]					=       str(a      )
   dataset_infos_dict.write_to_directory(a      )
   _A:  Union[str, Any]					=       DatasetInfosDict.from_directory(a      )

   # the config_name of the dataset_infos_dict take over the attribute
   for config_name, dataset_info in dataset_infos_dict.items():
      _A:  Optional[Any]					=       config_name
      # the yaml representation doesn't include fields like description or citation
      # so we just test that we can recover what we can from the yaml
      _A:  Any					=       DatasetInfo._from_yaml_dict(dataset_info._to_yaml_dict()      )
   assert dataset_infos_dict == reloaded

   if dataset_infos_dict:
      assert os.path.exists(os.path.join(a		,		'''README.md'''      )      )



