






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







				"""simple docstring"""




				def __init__( self     :List[str]  ,     lowerCamelCase__     :Union[str, Any]  ,     lowerCamelCase__     :str=2  ,     lowerCamelCase__     :Union[str, Any]=3  ,     lowerCamelCase__     :Dict=4  ,     lowerCamelCase__     :Optional[Any]=2  ,     lowerCamelCase__     :Any=7  ,     lowerCamelCase__     :Any=True  ,     lowerCamelCase__     :List[str]=True  ,     lowerCamelCase__     :Tuple=True  ,     lowerCamelCase__     :Optional[int]=True  ,     lowerCamelCase__     :Dict=99  ,     lowerCamelCase__     :int=36  ,     lowerCamelCase__     :Dict=2  ,     lowerCamelCase__     :Tuple=4  ,     lowerCamelCase__     :List[Any]=37  ,     lowerCamelCase__     :int="gelu"  ,     lowerCamelCase__     :Tuple=0.1  ,     lowerCamelCase__     :Union[str, Any]=0.1  ,     lowerCamelCase__     :Optional[int]=5_12  ,     lowerCamelCase__     :Dict=16  ,     lowerCamelCase__     :Dict=2  ,     lowerCamelCase__     :int=0.02  ,     lowerCamelCase__     :Dict=6  ,     lowerCamelCase__     :Dict=6  ,     lowerCamelCase__     :int=3  ,     lowerCamelCase__     :str=4  ,     lowerCamelCase__     :List[Any]=None  ,     lowerCamelCase__     :str=10_00  ,     ):
					UpperCamelCase__ :Dict						=      parent
					UpperCamelCase__ :Union[str, Any]						=      batch_size
					UpperCamelCase__ :List[Any]						=      num_channels
					UpperCamelCase__ :Tuple						=      image_size
					UpperCamelCase__ :int						=      patch_size
					UpperCamelCase__ :List[Any]						=      is_training
					UpperCamelCase__ :Any						=      use_input_mask
					UpperCamelCase__ :List[str]						=      use_token_type_ids
					UpperCamelCase__ :Any						=      use_labels
					UpperCamelCase__ :Any						=      vocab_size
					UpperCamelCase__ :Tuple						=      hidden_size
					UpperCamelCase__ :List[str]						=      num_hidden_layers
					UpperCamelCase__ :Optional[int]						=      num_attention_heads
					UpperCamelCase__ :List[str]						=      intermediate_size
					UpperCamelCase__ :Union[str, Any]						=      hidden_act
					UpperCamelCase__ :Union[str, Any]						=      hidden_dropout_prob
					UpperCamelCase__ :List[Any]						=      attention_probs_dropout_prob
					UpperCamelCase__ :int						=      max_position_embeddings
					UpperCamelCase__ :Union[str, Any]						=      type_vocab_size
					UpperCamelCase__ :List[Any]						=      type_sequence_label_size
					UpperCamelCase__ :str						=      initializer_range
					UpperCamelCase__ :List[Any]						=      coordinate_size
					UpperCamelCase__ :Dict						=      shape_size
					UpperCamelCase__ :List[Any]						=      num_labels
					UpperCamelCase__ :Any						=      num_choices
					UpperCamelCase__ :List[str]						=      scope
					UpperCamelCase__ :Dict						=      range_bbox

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







				def 				__a   ( self     :Union[str, Any]     ):
					UpperCamelCase__ :str						=      ids_tensor([self.batch_size, self.text_seq_length]  ,     self.vocab_size     )

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

					UpperCamelCase__ :List[Any]						=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

					UpperCamelCase__ :int						=      None
					if self.use_input_mask:
						UpperCamelCase__ :List[str]						=      random_attention_mask([self.batch_size, self.text_seq_length]     )

					UpperCamelCase__ :int						=      None
					if self.use_token_type_ids:
						UpperCamelCase__ :Optional[int]						=      ids_tensor([self.batch_size, self.text_seq_length]  ,     self.type_vocab_size     )

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

					UpperCamelCase__ :Union[str, 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 				__a   ( self     :List[str]  ,     lowerCamelCase__     :Optional[int]  ,     lowerCamelCase__     :Dict  ,     lowerCamelCase__     :Optional[Any]  ,     lowerCamelCase__     :Optional[int]  ,     lowerCamelCase__     :Optional[int]  ,     lowerCamelCase__     :Tuple     ):
					UpperCamelCase__ :List[str]						=      TFLayoutLMvaModel(config=A_     )

					# text + image
					UpperCamelCase__ :int						=      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__ :Tuple						=      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__ :str						=      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__ :Optional[Any]						=      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 				__a   ( self     :int  ,     lowerCamelCase__     :List[Any]  ,     lowerCamelCase__     :Any  ,     lowerCamelCase__     :str  ,     lowerCamelCase__     :str  ,     lowerCamelCase__     :List[str]  ,     lowerCamelCase__     :Optional[Any]  ,     lowerCamelCase__     :List[str]     ):
					UpperCamelCase__ :List[str]						=      self.num_labels
					UpperCamelCase__ :Optional[Any]						=      TFLayoutLMvaForSequenceClassification(config=A_     )
					UpperCamelCase__ :List[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 				__a   ( self     :List[str]  ,     lowerCamelCase__     :List[str]  ,     lowerCamelCase__     :Any  ,     lowerCamelCase__     :List[Any]  ,     lowerCamelCase__     :List[str]  ,     lowerCamelCase__     :List[Any]  ,     lowerCamelCase__     :Union[str, Any]  ,     lowerCamelCase__     :List[Any]     ):
					UpperCamelCase__ :Optional[Any]						=      self.num_labels
					UpperCamelCase__ :List[str]						=      TFLayoutLMvaForTokenClassification(config=A_     )
					UpperCamelCase__ :List[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.text_seq_length, self.num_labels)     )







				def 				__a   ( self     :List[Any]  ,     lowerCamelCase__     :int  ,     lowerCamelCase__     :List[str]  ,     lowerCamelCase__     :Tuple  ,     lowerCamelCase__     :List[str]  ,     lowerCamelCase__     :Optional[int]  ,     lowerCamelCase__     :Optional[int]  ,     lowerCamelCase__     :Optional[Any]     ):
					UpperCamelCase__ :List[Any]						=      2
					UpperCamelCase__ :str						=      TFLayoutLMvaForQuestionAnswering(config=A_     )
					UpperCamelCase__ :Dict						=      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 				__a   ( self     :List[str]     ):
					UpperCamelCase__ :Tuple						=      self.prepare_config_and_inputs()
					((UpperCamelCase__)				,		(UpperCamelCase__)				,		(UpperCamelCase__)				,		(UpperCamelCase__)				,		(UpperCamelCase__)				,		(UpperCamelCase__)				,		(UpperCamelCase__)				,		(UpperCamelCase__)) :str						=      config_and_inputs
					UpperCamelCase__ :Union[str, 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_				(      _lowercase						,      _lowercase						,      unittest.TestCase	):







				"""simple docstring"""




				_snake_case				:   str        =			(
				    (
				        TFLayoutLMvaModel,
				        TFLayoutLMvaForQuestionAnswering,
				        TFLayoutLMvaForSequenceClassification,
				        TFLayoutLMvaForTokenClassification,
				    )
				    if is_tf_available()
				    else ()
				)
				_snake_case				:   int        =			(
				    {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel}
				    if is_tf_available()
				    else {}
				)

				_snake_case				:   int        =			False
				_snake_case				:   Any        =			False
				_snake_case				:   Any        =			False







				def 				__a   ( self     :Any  ,     lowerCamelCase__     :Tuple  ,     lowerCamelCase__     :Any  ,     lowerCamelCase__     :Optional[int]  ,     lowerCamelCase__     :Dict  ,     lowerCamelCase__     :Dict     ):
					return True







				def 				__a   ( self     :Union[str, Any]  ,     lowerCamelCase__     :int  ,     lowerCamelCase__     :Tuple  ,     lowerCamelCase__     :Optional[int]=False     ):
					UpperCamelCase__ :Dict						=      copy.deepcopy(A_     )

					if model_class in get_values(A_     ):
						UpperCamelCase__ :Optional[int]						=      {
						    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__ :Optional[int]						=      tf.ones(self.model_tester.batch_size  ,     dtype=tf.intaa     )
						elif model_class in get_values(A_     ):
							UpperCamelCase__ :List[str]						=      tf.zeros(self.model_tester.batch_size  ,     dtype=tf.intaa     )
							UpperCamelCase__ :Any						=      tf.zeros(self.model_tester.batch_size  ,     dtype=tf.intaa     )
						elif model_class in get_values(A_     ):
							UpperCamelCase__ :List[Any]						=      tf.zeros(self.model_tester.batch_size  ,     dtype=tf.intaa     )
						elif model_class in get_values(A_     ):
							UpperCamelCase__ :str						=      tf.zeros(
							    (self.model_tester.batch_size, self.model_tester.text_seq_length)  ,     dtype=tf.intaa     )

					return inputs_dict







				def 				__a   ( self     :List[Any]     ):
					UpperCamelCase__ :List[str]						=      TFLayoutLMvaModelTester(self     )
					UpperCamelCase__ :int						=      ConfigTester(self  ,     config_class=A_  ,     hidden_size=37     )







				def 				__a   ( self     :Dict     ):
					self.config_tester.run_common_tests()







				def 				__a   ( self     :Any     ):
					UpperCamelCase__				,		UpperCamelCase__ :Optional[int]						=      self.model_tester.prepare_config_and_inputs_for_common()
					for model_class in self.all_model_classes:
						UpperCamelCase__ :int						=      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__ :str						=      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__ :str						=      added_label.shape.as_list()[:1]

							# Test that model correctly compute the loss with kwargs
							UpperCamelCase__ :str						=      self._prepare_for_class(inputs_dict.copy()  ,     A_  ,     return_labels=A_     )
							UpperCamelCase__ :Optional[int]						=      prepared_for_class.pop("""input_ids"""     )

							UpperCamelCase__ :List[str]						=      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__ :int						=      self._prepare_for_class(inputs_dict.copy()  ,     A_  ,     return_labels=A_     )
							UpperCamelCase__ :str						=      prepared_for_class.pop("""input_ids"""     )
							if "labels" in prepared_for_class:
								UpperCamelCase__ :int						=      prepared_for_class["""labels"""].numpy()
								if len(labels.shape     ) > 1 and labels.shape[1] != 1:
									UpperCamelCase__ :Union[str, Any]						=      -1_00
									UpperCamelCase__ :List[str]						=      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[Any]						=      self._prepare_for_class(inputs_dict.copy()  ,     A_  ,     return_labels=A_     )
							UpperCamelCase__ :List[str]						=      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__ :str						=      self._prepare_for_class(inputs_dict.copy()  ,     A_  ,     return_labels=A_     )

							# Get keys that were added with the _prepare_for_class function
							UpperCamelCase__ :Any						=      prepared_for_class.keys() - inputs_dict.keys()
							UpperCamelCase__ :int						=      inspect.signature(model.call     ).parameters
							UpperCamelCase__ :int						=      list(signature.keys()     )

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

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

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

							UpperCamelCase__ :Dict						=      tuple(A_     )

							# Send to model
							UpperCamelCase__ :str						=      model(tuple_input[:-1]     )[0]

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







				def 				__a   ( self     :List[str]     ):
					(
					    (
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		   
					) :Dict						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(A_  ,     A_  ,     A_  ,     A_  ,     A_  ,     A_     )







				def 				__a   ( self     :Optional[Any]     ):
					(
					    (
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		   
					) :Optional[int]						=      self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
						UpperCamelCase__ :Optional[Any]						=      type
						self.model_tester.create_and_check_model(A_  ,     A_  ,     A_  ,     A_  ,     A_  ,     A_     )







				def 				__a   ( self     :Tuple     ):
					(
					    (
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		   
					) :List[Any]						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(
					    A_  ,     A_  ,     A_  ,     A_  ,     A_  ,     A_  ,     A_     )







				def 				__a   ( self     :Dict     ):
					(
					    (
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		   
					) :Union[str, Any]						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(
					    A_  ,     A_  ,     A_  ,     A_  ,     A_  ,     A_  ,     A_     )







				def 				__a   ( self     :str     ):
					(
					    (
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		(
					    UpperCamelCase__   
					)				,		   
					) :List[Any]						=      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 				__a   ( self     :int     ):
					for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						UpperCamelCase__ :Tuple						=      TFLayoutLMvaModel.from_pretrained(A_     )
						self.assertIsNotNone(A_     )




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




@require_tf
class        lowerCAmelCase_				(      unittest.TestCase	):







				"""simple docstring"""




				@cached_property
				def 				__a   ( self     :int     ):
					return LayoutLMvaImageProcessor(apply_ocr=A_     ) if is_vision_available() else None







				@slow
				def 				__a   ( self     :Tuple     ):
					UpperCamelCase__ :int						=      TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""     )

					UpperCamelCase__ :List[Any]						=      self.default_image_processor
					UpperCamelCase__ :Optional[Any]						=      prepare_img()
					UpperCamelCase__ :int						=      image_processor(images=A_  ,     return_tensors="""tf"""     ).pixel_values

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

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

					# verify the logits
					UpperCamelCase__ :Dict						=      (1, 1_99, 7_68)
					self.assertEqual(outputs.last_hidden_state.shape  ,     A_     )

					UpperCamelCase__ :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]  ,     A_  ,     atol=1e-4     )     )

"""simple docstring"""


# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from accelerate.utils import ComputeEnvironment

from .cluster import get_cluster_input
from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file  # noqa: F401
from .config_utils import _ask_field, _ask_options, _convert_compute_environment  # noqa: F401
from .sagemaker import get_sagemaker_input


_lowercase				 =					'''Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine'''






def    _snake_case					(		):
	A     = _ask_options(
	    'In which compute environment are you running?'						,   ['This machine', 'AWS (Amazon SageMaker)']						,   _convert_compute_environment						,   )
	if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
		A     = get_sagemaker_input()
	else:
		A     = get_cluster_input()
	return config






def    _snake_case					(		snake_case__ :  Any=None  ):
	if subparsers is not None:
		A     = subparsers.add_parser('config'						,   description=snake_case__  )
	else:
		A     = argparse.ArgumentParser('Accelerate config command'						,   description=snake_case__  )

	parser.add_argument(
	    '--config_file'						,   default=snake_case__						,   help=(
	        'The path to use to store the config file. Will default to a file named default_config.yaml in the cache '
	        'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '
	        'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '
	        'with \'huggingface\'.'
	    )						,   )

	if subparsers is not None:
		parser.set_defaults(func=snake_case__  )
	return parser






def    _snake_case					(		snake_case__ :  Tuple  ):
	A     = get_user_input()
	if args.config_file is not None:
		A     = args.config_file
	else:
		if not os.path.isdir(snake_case__  ):
			os.makedirs(snake_case__  )
		A     = default_yaml_config_file

	if config_file.endswith('.json'  ):
		config.to_json_file(snake_case__  )
	else:
		config.to_yaml_file(snake_case__  )
	print(F'accelerate configuration saved at {config_file}'  )






def    _snake_case					(		):
	A     = config_command_parser()
	A     = parser.parse_args()
	config_command(snake_case__  )


if __name__ == "__main__":
		main()