

'''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      UpperCamelCase :

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

     """simple docstring"""






     SCREAMING_SNAKE_CASE								=			parent
     SCREAMING_SNAKE_CASE								=			batch_size
     SCREAMING_SNAKE_CASE								=			num_channels
     SCREAMING_SNAKE_CASE								=			image_size
     SCREAMING_SNAKE_CASE								=			patch_size
     SCREAMING_SNAKE_CASE								=			is_training
     SCREAMING_SNAKE_CASE								=			use_input_mask
     SCREAMING_SNAKE_CASE								=			use_token_type_ids
     SCREAMING_SNAKE_CASE								=			use_labels
     SCREAMING_SNAKE_CASE								=			vocab_size
     SCREAMING_SNAKE_CASE								=			hidden_size
     SCREAMING_SNAKE_CASE								=			num_hidden_layers
     SCREAMING_SNAKE_CASE								=			num_attention_heads
     SCREAMING_SNAKE_CASE								=			intermediate_size
     SCREAMING_SNAKE_CASE								=			hidden_act
     SCREAMING_SNAKE_CASE								=			hidden_dropout_prob
     SCREAMING_SNAKE_CASE								=			attention_probs_dropout_prob
     SCREAMING_SNAKE_CASE								=			max_position_embeddings
     SCREAMING_SNAKE_CASE								=			type_vocab_size
     SCREAMING_SNAKE_CASE								=			type_sequence_label_size
     SCREAMING_SNAKE_CASE								=			initializer_range
     SCREAMING_SNAKE_CASE								=			coordinate_size
     SCREAMING_SNAKE_CASE								=			shape_size
     SCREAMING_SNAKE_CASE								=			num_labels
     SCREAMING_SNAKE_CASE								=			num_choices
     SCREAMING_SNAKE_CASE								=			scope
     SCREAMING_SNAKE_CASE								=			range_bbox

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

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

     """simple docstring"""






     SCREAMING_SNAKE_CASE								=			ids_tensor([self.batch_size, self.text_seq_length]							,    self.vocab_size    )

     SCREAMING_SNAKE_CASE								=			ids_tensor([self.batch_size, self.text_seq_length, 4]							,    self.range_bbox    )
     SCREAMING_SNAKE_CASE								=			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]:
        SCREAMING_SNAKE_CASE								=			bbox[i, j, 3]
        SCREAMING_SNAKE_CASE								=			bbox[i, j, 1]
        SCREAMING_SNAKE_CASE								=			tmp_coordinate
       if bbox[i, j, 2] < bbox[i, j, 0]:
        SCREAMING_SNAKE_CASE								=			bbox[i, j, 2]
        SCREAMING_SNAKE_CASE								=			bbox[i, j, 0]
        SCREAMING_SNAKE_CASE								=			tmp_coordinate
     SCREAMING_SNAKE_CASE								=			tf.constant(snake_case__    )

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

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

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

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

     SCREAMING_SNAKE_CASE								=			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 UpperCamelCase			(			self :      Dict							,    snake_case__ :      Any							,    snake_case__ :      Optional[int]							,    snake_case__ :      Tuple							,    snake_case__ :      Optional[int]							,    snake_case__ :      Optional[Any]							,    snake_case__ :      List[str]    ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE								=			TFLayoutLMvaModel(config=snake_case__    )

     # text + image
     SCREAMING_SNAKE_CASE								=			model(snake_case__							,    pixel_values=snake_case__							,    training=snake_case__    )
     SCREAMING_SNAKE_CASE								=			model(
         snake_case__							,    bbox=snake_case__							,    pixel_values=snake_case__							,    attention_mask=snake_case__							,    token_type_ids=snake_case__							,    training=snake_case__							,    )
     SCREAMING_SNAKE_CASE								=			model(snake_case__							,    bbox=snake_case__							,    pixel_values=snake_case__							,    training=snake_case__    )

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

     # text only
     SCREAMING_SNAKE_CASE								=			model(snake_case__							,    training=snake_case__    )
     self.parent.assertEqual(
         result.last_hidden_state.shape							,    (self.batch_size, self.text_seq_length, self.hidden_size)    )

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

    def UpperCamelCase			(			self :      str							,    snake_case__ :      List[str]							,    snake_case__ :      List[str]							,    snake_case__ :      Tuple							,    snake_case__ :      int							,    snake_case__ :      List[Any]							,    snake_case__ :      str							,    snake_case__ :      Union[str, Any]    ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE								=			self.num_labels
     SCREAMING_SNAKE_CASE								=			TFLayoutLMvaForSequenceClassification(config=snake_case__    )
     SCREAMING_SNAKE_CASE								=			model(
         snake_case__							,    bbox=snake_case__							,    pixel_values=snake_case__							,    attention_mask=snake_case__							,    token_type_ids=snake_case__							,    labels=snake_case__							,    training=snake_case__							,    )
     self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.num_labels)    )

    def UpperCamelCase			(			self :      Union[str, Any]							,    snake_case__ :      str							,    snake_case__ :      Optional[Any]							,    snake_case__ :      Optional[int]							,    snake_case__ :      List[str]							,    snake_case__ :      Tuple							,    snake_case__ :      int							,    snake_case__ :      str    ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE								=			self.num_labels
     SCREAMING_SNAKE_CASE								=			TFLayoutLMvaForTokenClassification(config=snake_case__    )
     SCREAMING_SNAKE_CASE								=			model(
         snake_case__							,    bbox=snake_case__							,    pixel_values=snake_case__							,    attention_mask=snake_case__							,    token_type_ids=snake_case__							,    labels=snake_case__							,    training=snake_case__							,    )
     self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.text_seq_length, self.num_labels)    )

    def UpperCamelCase			(			self :      int							,    snake_case__ :      Any							,    snake_case__ :      Tuple							,    snake_case__ :      Union[str, Any]							,    snake_case__ :      Tuple							,    snake_case__ :      int							,    snake_case__ :      int							,    snake_case__ :      str    ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE								=			2
     SCREAMING_SNAKE_CASE								=			TFLayoutLMvaForQuestionAnswering(config=snake_case__    )
     SCREAMING_SNAKE_CASE								=			model(
         snake_case__							,    bbox=snake_case__							,    pixel_values=snake_case__							,    attention_mask=snake_case__							,    token_type_ids=snake_case__							,    start_positions=snake_case__							,    end_positions=snake_case__							,    training=snake_case__							,    )
     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 UpperCamelCase			(			self :      str    ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE								=			self.prepare_config_and_inputs()
     ((SCREAMING_SNAKE_CASE)						,	(SCREAMING_SNAKE_CASE)						,	(SCREAMING_SNAKE_CASE)						,	(SCREAMING_SNAKE_CASE)						,	(SCREAMING_SNAKE_CASE)						,	(SCREAMING_SNAKE_CASE)						,	(SCREAMING_SNAKE_CASE)						,	(SCREAMING_SNAKE_CASE))								=			config_and_inputs
     SCREAMING_SNAKE_CASE								=			{
         '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 (     snake_case_	,			snake_case_	,			unittest.TestCase   ):
    __UpperCamelCase         =(
        (
            TFLayoutLMvaModel,
            TFLayoutLMvaForQuestionAnswering,
            TFLayoutLMvaForSequenceClassification,
            TFLayoutLMvaForTokenClassification,
        )
        if is_tf_available()
        else ()
    )
    __UpperCamelCase         =(
        {"""document-question-answering""": TFLayoutLMvaForQuestionAnswering, """feature-extraction""": TFLayoutLMvaModel}
        if is_tf_available()
        else {}
    )

    __UpperCamelCase         =False
    __UpperCamelCase         =False
    __UpperCamelCase         =False

    def UpperCamelCase			(			self :      str							,    snake_case__ :      Dict							,    snake_case__ :      Dict							,    snake_case__ :      str							,    snake_case__ :      List[Any]							,    snake_case__ :      Tuple    ):

     """simple docstring"""






     return True

    def UpperCamelCase			(			self :      int							,    snake_case__ :      Optional[Any]							,    snake_case__ :      List[Any]							,    snake_case__ :      Dict=False    ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE								=			copy.deepcopy(snake_case__    )

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

     if return_labels:
      if model_class in get_values(snake_case__    ):
       SCREAMING_SNAKE_CASE								=			tf.ones(self.model_tester.batch_size							,    dtype=tf.intaa    )
      elif model_class in get_values(snake_case__    ):
       SCREAMING_SNAKE_CASE								=			tf.zeros(self.model_tester.batch_size							,    dtype=tf.intaa    )
       SCREAMING_SNAKE_CASE								=			tf.zeros(self.model_tester.batch_size							,    dtype=tf.intaa    )
      elif model_class in get_values(snake_case__    ):
       SCREAMING_SNAKE_CASE								=			tf.zeros(self.model_tester.batch_size							,    dtype=tf.intaa    )
      elif model_class in get_values(snake_case__    ):
       SCREAMING_SNAKE_CASE								=			tf.zeros(
           (self.model_tester.batch_size, self.model_tester.text_seq_length)							,    dtype=tf.intaa    )

     return inputs_dict

    def UpperCamelCase			(			self :      int    ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE								=			TFLayoutLMvaModelTester(self    )
     SCREAMING_SNAKE_CASE								=			ConfigTester(self							,    config_class=snake_case__							,    hidden_size=3_7    )

    def UpperCamelCase			(			self :      str    ):

     """simple docstring"""






     self.config_tester.run_common_tests()

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

     """simple docstring"""






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

       # Test that model correctly compute the loss with kwargs
       SCREAMING_SNAKE_CASE								=			self._prepare_for_class(inputs_dict.copy()							,    snake_case__							,    return_labels=snake_case__    )
       SCREAMING_SNAKE_CASE								=			prepared_for_class.pop('input_ids'    )

       SCREAMING_SNAKE_CASE								=			model(snake_case__							,    **snake_case__    )[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
       SCREAMING_SNAKE_CASE								=			self._prepare_for_class(inputs_dict.copy()							,    snake_case__							,    return_labels=snake_case__    )
       SCREAMING_SNAKE_CASE								=			prepared_for_class.pop('input_ids'    )
       if "labels" in prepared_for_class:
        SCREAMING_SNAKE_CASE								=			prepared_for_class['labels'].numpy()
        if len(labels.shape    ) > 1 and labels.shape[1] != 1:
         SCREAMING_SNAKE_CASE								=			-1_0_0
         SCREAMING_SNAKE_CASE								=			tf.convert_to_tensor(snake_case__    )
         SCREAMING_SNAKE_CASE								=			model(snake_case__							,    **snake_case__    )[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
       SCREAMING_SNAKE_CASE								=			self._prepare_for_class(inputs_dict.copy()							,    snake_case__							,    return_labels=snake_case__    )
       SCREAMING_SNAKE_CASE								=			model(snake_case__    )[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
       SCREAMING_SNAKE_CASE								=			self._prepare_for_class(inputs_dict.copy()							,    snake_case__							,    return_labels=snake_case__    )

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

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

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

       for index, value in sorted_tuple_index_mapping:
        SCREAMING_SNAKE_CASE								=			prepared_for_class[value]

       SCREAMING_SNAKE_CASE								=			tuple(snake_case__    )

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

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

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

     """simple docstring"""






     (
         (
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	      
     )								=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__    )

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

     """simple docstring"""






     (
         (
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	      
     )								=			self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
      SCREAMING_SNAKE_CASE								=			type
      self.model_tester.create_and_check_model(snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__    )

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

     """simple docstring"""






     (
         (
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	      
     )								=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(
         snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__    )

    def UpperCamelCase			(			self :      Tuple    ):

     """simple docstring"""






     (
         (
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	      
     )								=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(
         snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__    )

    def UpperCamelCase			(			self :      List[Any]    ):

     """simple docstring"""






     (
         (
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	(
         SCREAMING_SNAKE_CASE      
     )						,	      
     )								=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(
         snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__							,    snake_case__    )




    @slow
    def UpperCamelCase			(			self :      Any    ):

     """simple docstring"""






     for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      SCREAMING_SNAKE_CASE								=			TFLayoutLMvaModel.from_pretrained(snake_case__    )
      self.assertIsNotNone(snake_case__    )


def        __lowerCAmelCase    (       )		->							int:
 '''simple docstring'''


 SCREAMING_SNAKE_CASE								=			Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'	)
 return image




@require_tf
class      UpperCamelCase (     unittest.TestCase   ):

    @cached_property
    def UpperCamelCase			(			self :      Tuple    ):

     """simple docstring"""






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




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

     """simple docstring"""






     SCREAMING_SNAKE_CASE								=			TFLayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'    )

     SCREAMING_SNAKE_CASE								=			self.default_image_processor
     SCREAMING_SNAKE_CASE								=			prepare_img()
     SCREAMING_SNAKE_CASE								=			image_processor(images=snake_case__							,    return_tensors='tf'    ).pixel_values

     SCREAMING_SNAKE_CASE								=			tf.constant([[1, 2]]    )
     SCREAMING_SNAKE_CASE								=			tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]    )							,    axis=0    )

     # forward pass
     SCREAMING_SNAKE_CASE								=			model(input_ids=snake_case__							,    bbox=snake_case__							,    pixel_values=snake_case__							,    training=snake_case__    )

     # verify the logits
     SCREAMING_SNAKE_CASE								=			(1, 1_9_9, 7_6_8)
     self.assertEqual(outputs.last_hidden_state.shape							,    snake_case__    )

     SCREAMING_SNAKE_CASE								=			tf.constant(
         [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]]    )

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




#
# This a `torch.distributed` diagnostics script that checks that all GPUs in the cluster (one or
# many nodes) can talk to each other via nccl and allocate gpu memory.
#
# To run first adjust the number of processes and nodes:
#
# python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py
#
# You may need to add --master_addr $MASTER_ADDR --master_port $MASTER_PORT if using a custom addr:port
#
# You can also use the rdzv API: --rdzv_endpoint $MASTER_ADDR:$MASTER_PORT --rdzv_backend c10d
#
# use torch.distributed.launch instead of torch.distributed.run for torch < 1.9
#
# If you get a hanging in `barrier` calls you have some network issues, you may try to debug this with:
#
# NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py
#
# which should tell you what's going on behind the scenes.
#
#
# This script can be run via `srun` in the SLURM environment as well. Here is a SLURM script that
# runs on 2 nodes of 4 gpus per node:
#
# #SBATCH --job-name=test-nodes        # name
# #SBATCH --nodes=2                    # nodes
# #SBATCH --ntasks-per-node=1          # crucial - only 1 task per dist per node!
# #SBATCH --cpus-per-task=10           # number of cores per tasks
# #SBATCH --gres=gpu:4                 # number of gpus
# #SBATCH --time 0:05:00               # maximum execution time (HH:MM:SS)
# #SBATCH --output=%x-%j.out           # output file name
#
# GPUS_PER_NODE=4
# MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1)
# MASTER_PORT=6000
#
# srun --jobid $SLURM_JOBID bash -c 'python -m torch.distributed.run \
# --nproc_per_node $GPUS_PER_NODE --nnodes $SLURM_NNODES --node_rank $SLURM_PROCID \
# --master_addr $MASTER_ADDR --master_port $MASTER_PORT \
# torch-distributed-gpu-test.py'
#

import fcntl
import os
import socket

import torch
import torch.distributed as dist


def        __lowerCAmelCase    (       *_UpperCamelCase		:  Optional[Any]	)		->							Optional[int]:
 '''simple docstring'''


 with open(_UpperCamelCase		,							'r'	) as fh:
  fcntl.flock(_UpperCamelCase		,							fcntl.LOCK_EX	)
  try:
   print(*_UpperCamelCase	)
  finally:
   fcntl.flock(_UpperCamelCase		,							fcntl.LOCK_UN	)


a_    :						int    		=      int(os.environ["LOCAL_RANK"])
torch.cuda.set_device(local_rank)
a_    :						str    		=      torch.device("cuda", local_rank)
a_    :						Optional[int]    		=      socket.gethostname()

a_    :						Union[str, Any]    		=      F"""[{hostname}-{local_rank}]"""

try:
      # test distributed
      dist.init_process_group("nccl")
      dist.all_reduce(torch.ones(1).to(device), op=dist.ReduceOp.SUM)
      dist.barrier()

      # test cuda is available and can allocate memory
      torch.cuda.is_available()
      torch.ones(1).cuda(local_rank)

      # global rank
      a_    :						Dict    		=      dist.get_rank()
      a_    :						Any    		=      dist.get_world_size()

      printflock(F"""{gpu} is OK (global rank: {rank}/{world_size})""")

      dist.barrier()
      if rank == 0:
            printflock(F"""pt={torch.__version__}, cuda={torch.version.cuda}, nccl={torch.cuda.nccl.version()}""")

except Exception:
      printflock(F"""{gpu} is broken""")
      raise

