


'''simple docstring'''





import copy
import unittest

from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
        MODEL_FOR_QUESTION_ANSWERING_MAPPING,
        MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
        MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
        LayoutLMvaConfig,
        LayoutLMvaForQuestionAnswering,
        LayoutLMvaForSequenceClassification,
        LayoutLMvaForTokenClassification,
        LayoutLMvaModel,
    )
    from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
    from PIL import Image

    from transformers import LayoutLMvaImageProcessor




class 				UpperCAmelCase			:


 def __init__(      self							:	int			,       __lowerCamelCase							:	Dict			,       __lowerCamelCase							:	Union[str, Any]=2			,       __lowerCamelCase							:	Tuple=3			,       __lowerCamelCase							:	Tuple=4			,       __lowerCamelCase							:	Any=2			,       __lowerCamelCase							:	Dict=7			,       __lowerCamelCase							:	Any=True			,       __lowerCamelCase							:	Any=True			,       __lowerCamelCase							:	int=True			,       __lowerCamelCase							:	Union[str, Any]=True			,       __lowerCamelCase							:	Optional[Any]=9_9			,       __lowerCamelCase							:	Optional[Any]=3_6			,       __lowerCamelCase							:	List[Any]=3			,       __lowerCamelCase							:	List[Any]=4			,       __lowerCamelCase							:	Dict=3_7			,       __lowerCamelCase							:	Optional[int]="gelu"			,       __lowerCamelCase							:	Any=0.1			,       __lowerCamelCase							:	Tuple=0.1			,       __lowerCamelCase							:	List[Any]=5_1_2			,       __lowerCamelCase							:	Any=1_6			,       __lowerCamelCase							:	Optional[int]=2			,       __lowerCamelCase							:	Any=0.02			,       __lowerCamelCase							:	Any=6			,       __lowerCamelCase							:	Any=6			,       __lowerCamelCase							:	List[str]=3			,       __lowerCamelCase							:	List[Any]=4			,       __lowerCamelCase							:	Optional[Any]=None			,       __lowerCamelCase							:	Optional[Any]=1_0_0_0			,       ):
    UpperCAmelCase__	:List[str]      =      parent
    UpperCAmelCase__	:Optional[Any]      =      batch_size
    UpperCAmelCase__	:Any      =      num_channels
    UpperCAmelCase__	:int      =      image_size
    UpperCAmelCase__	:int      =      patch_size
    UpperCAmelCase__	:str      =      text_seq_length
    UpperCAmelCase__	:Any      =      is_training
    UpperCAmelCase__	:Tuple      =      use_input_mask
    UpperCAmelCase__	:Any      =      use_token_type_ids
    UpperCAmelCase__	:int      =      use_labels
    UpperCAmelCase__	:List[Any]      =      vocab_size
    UpperCAmelCase__	:Optional[Any]      =      hidden_size
    UpperCAmelCase__	:List[Any]      =      num_hidden_layers
    UpperCAmelCase__	:Optional[Any]      =      num_attention_heads
    UpperCAmelCase__	:str      =      intermediate_size
    UpperCAmelCase__	:List[str]      =      hidden_act
    UpperCAmelCase__	:Optional[Any]      =      hidden_dropout_prob
    UpperCAmelCase__	:Optional[Any]      =      attention_probs_dropout_prob
    UpperCAmelCase__	:Tuple      =      max_position_embeddings
    UpperCAmelCase__	:Union[str, Any]      =      type_vocab_size
    UpperCAmelCase__	:Optional[Any]      =      type_sequence_label_size
    UpperCAmelCase__	:List[str]      =      initializer_range
    UpperCAmelCase__	:Tuple      =      coordinate_size
    UpperCAmelCase__	:Any      =      shape_size
    UpperCAmelCase__	:Tuple      =      num_labels
    UpperCAmelCase__	:int      =      num_choices
    UpperCAmelCase__	:Optional[int]      =      scope
    UpperCAmelCase__	:List[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__	:Optional[Any]      =      text_seq_length
    UpperCAmelCase__	:Union[str, Any]      =      (image_size // patch_size) ** 2 + 1
    UpperCAmelCase__	:int      =      self.text_seq_length + self.image_seq_length


 def        __SCREAMING_SNAKE_CASE					(      self							:	Union[str, Any] ):
    UpperCAmelCase__	:Tuple      =      ids_tensor([self.batch_size, self.text_seq_length]			,       self.vocab_size )

    UpperCAmelCase__	:Optional[Any]      =      ids_tensor([self.batch_size, self.text_seq_length, 4]			,       self.range_bbox )
    # 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__	:Dict      =      bbox[i, j, 3]
             UpperCAmelCase__	:List[str]      =      bbox[i, j, 1]
             UpperCAmelCase__	:Optional[Any]      =      t
          if bbox[i, j, 2] < bbox[i, j, 0]:
             UpperCAmelCase__	:str      =      bbox[i, j, 2]
             UpperCAmelCase__	:str      =      bbox[i, j, 0]
             UpperCAmelCase__	:str      =      t

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

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

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

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

    UpperCAmelCase__	:Tuple      =      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        __SCREAMING_SNAKE_CASE					(      self							:	Optional[Any]			,       __lowerCamelCase							:	int			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	Optional[int]			,       __lowerCamelCase							:	List[Any]			,       __lowerCamelCase							:	List[Any]			,       __lowerCamelCase							:	int			,       __lowerCamelCase							:	Optional[int] ):
    UpperCAmelCase__	:Any      =      LayoutLMvaModel(config=__lowerCamelCase )
    model.to(__lowerCamelCase )
    model.eval()

    # text + image
    UpperCAmelCase__	:int      =      model(__lowerCamelCase			,       pixel_values=__lowerCamelCase )
    UpperCAmelCase__	:Any      =      model(
        __lowerCamelCase			,       bbox=__lowerCamelCase			,       pixel_values=__lowerCamelCase			,       attention_mask=__lowerCamelCase			,       token_type_ids=__lowerCamelCase )
    UpperCAmelCase__	:Union[str, Any]      =      model(__lowerCamelCase			,       bbox=__lowerCamelCase			,       pixel_values=__lowerCamelCase			,       token_type_ids=__lowerCamelCase )
    UpperCAmelCase__	:List[Any]      =      model(__lowerCamelCase			,       bbox=__lowerCamelCase			,       pixel_values=__lowerCamelCase )

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

    # text only
    UpperCAmelCase__	:List[str]      =      model(__lowerCamelCase )
    self.parent.assertEqual(
        result.last_hidden_state.shape			,       (self.batch_size, self.text_seq_length, self.hidden_size) )

    # image only
    UpperCAmelCase__	:int      =      model(pixel_values=__lowerCamelCase )
    self.parent.assertEqual(
        result.last_hidden_state.shape			,       (self.batch_size, self.image_seq_length, self.hidden_size) )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict			,       __lowerCamelCase							:	List[Any]			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	Optional[int]			,       __lowerCamelCase							:	Dict			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	Dict			,       __lowerCamelCase							:	List[Any] ):
    UpperCAmelCase__	:Optional[Any]      =      self.num_labels
    UpperCAmelCase__	:Union[str, Any]      =      LayoutLMvaForSequenceClassification(__lowerCamelCase )
    model.to(__lowerCamelCase )
    model.eval()
    UpperCAmelCase__	:str      =      model(
        __lowerCamelCase			,       bbox=__lowerCamelCase			,       pixel_values=__lowerCamelCase			,       attention_mask=__lowerCamelCase			,       token_type_ids=__lowerCamelCase			,       labels=__lowerCamelCase			,       )
    self.parent.assertEqual(result.logits.shape			,       (self.batch_size, self.num_labels) )


 def        __SCREAMING_SNAKE_CASE					(      self							:	List[Any]			,       __lowerCamelCase							:	Union[str, Any]			,       __lowerCamelCase							:	List[str]			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	List[Any]			,       __lowerCamelCase							:	List[str]			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	List[Any] ):
    UpperCAmelCase__	:Dict      =      self.num_labels
    UpperCAmelCase__	:Optional[int]      =      LayoutLMvaForTokenClassification(config=__lowerCamelCase )
    model.to(__lowerCamelCase )
    model.eval()
    UpperCAmelCase__	:str      =      model(
        __lowerCamelCase			,       bbox=__lowerCamelCase			,       pixel_values=__lowerCamelCase			,       attention_mask=__lowerCamelCase			,       token_type_ids=__lowerCamelCase			,       labels=__lowerCamelCase			,       )
    self.parent.assertEqual(result.logits.shape			,       (self.batch_size, self.text_seq_length, self.num_labels) )


 def        __SCREAMING_SNAKE_CASE					(      self							:	int			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	List[Any]			,       __lowerCamelCase							:	str			,       __lowerCamelCase							:	List[str]			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	List[Any]			,       __lowerCamelCase							:	List[Any] ):
    UpperCAmelCase__	:Optional[int]      =      LayoutLMvaForQuestionAnswering(config=__lowerCamelCase )
    model.to(__lowerCamelCase )
    model.eval()
    UpperCAmelCase__	:List[Any]      =      model(
        __lowerCamelCase			,       bbox=__lowerCamelCase			,       pixel_values=__lowerCamelCase			,       attention_mask=__lowerCamelCase			,       token_type_ids=__lowerCamelCase			,       start_positions=__lowerCamelCase			,       end_positions=__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        __SCREAMING_SNAKE_CASE					(      self							:	Dict ):
    UpperCAmelCase__	:Optional[int]      =      self.prepare_config_and_inputs()
    (
        UpperCAmelCase__      
    )	:List[Any]      =      config_and_inputs
    UpperCAmelCase__	:Optional[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_torch
class 				UpperCAmelCase			(			__snake_case						, __snake_case						, unittest.TestCase						):
 UpperCAmelCase				=					False
 UpperCAmelCase				=					False
 UpperCAmelCase				=					False

 UpperCAmelCase				=					(
     (
         LayoutLMvaModel,
         LayoutLMvaForSequenceClassification,
         LayoutLMvaForTokenClassification,
         LayoutLMvaForQuestionAnswering,
     )
     if is_torch_available()
     else ()
 )
 UpperCAmelCase				=					(
     {"""document-question-answering""": LayoutLMvaForQuestionAnswering, """feature-extraction""": LayoutLMvaModel}
     if is_torch_available()
     else {}
 )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict			,       __lowerCamelCase							:	List[str]			,       __lowerCamelCase							:	Optional[int]			,       __lowerCamelCase							:	Union[str, Any]			,       __lowerCamelCase							:	Union[str, Any]			,       __lowerCamelCase							:	Union[str, Any] ):
    # `DocumentQuestionAnsweringPipeline` is expected to work with this model, but it combines the text and visual
    # embedding along the sequence dimension (dim 1), which causes an error during post-processing as `p_mask` has
    # the sequence dimension of the text embedding only.
    # (see the line `embedding_output = torch.cat([embedding_output, visual_embeddings], dim=1)`)
    return True


 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict ):
    UpperCAmelCase__	:Tuple      =      LayoutLMvaModelTester(self )
    UpperCAmelCase__	:Dict      =      ConfigTester(self			,       config_class=__lowerCamelCase			,       hidden_size=3_7 )


 def        __SCREAMING_SNAKE_CASE					(      self							:	int			,       __lowerCamelCase							:	List[str]			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	Optional[Any]=False ):
    UpperCAmelCase__	:List[str]      =      copy.deepcopy(__lowerCamelCase )
    if model_class in get_values(__lowerCamelCase ):
       UpperCAmelCase__	:str      =      {
           k: v.unsqueeze(1 ).expand(-1			,       self.model_tester.num_choices			,       -1 ).contiguous()
           if isinstance(__lowerCamelCase			,       torch.Tensor ) and v.ndim > 1
           else v
           for k, v in inputs_dict.items()
       }
    if return_labels:
       if model_class in get_values(__lowerCamelCase ):
          UpperCAmelCase__	:Any      =      torch.ones(self.model_tester.batch_size			,       dtype=torch.long			,       device=__lowerCamelCase )
       elif model_class in get_values(__lowerCamelCase ):
          UpperCAmelCase__	:Union[str, Any]      =      torch.zeros(
              self.model_tester.batch_size			,       dtype=torch.long			,       device=__lowerCamelCase )
          UpperCAmelCase__	:Union[str, Any]      =      torch.zeros(
              self.model_tester.batch_size			,       dtype=torch.long			,       device=__lowerCamelCase )
       elif model_class in [
           *get_values(__lowerCamelCase ),
       ]:
          UpperCAmelCase__	:Tuple      =      torch.zeros(
              self.model_tester.batch_size			,       dtype=torch.long			,       device=__lowerCamelCase )
       elif model_class in [
           *get_values(__lowerCamelCase ),
       ]:
          UpperCAmelCase__	:Union[str, Any]      =      torch.zeros(
              (self.model_tester.batch_size, self.model_tester.text_seq_length)			,       dtype=torch.long			,       device=__lowerCamelCase			,       )

    return inputs_dict


 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[int] ):
    self.config_tester.run_common_tests()


 def        __SCREAMING_SNAKE_CASE					(      self							:	List[Any] ):
    UpperCAmelCase__	:str      =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*__lowerCamelCase )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict ):
    UpperCAmelCase__	:Tuple      =      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(*__lowerCamelCase )


 def        __SCREAMING_SNAKE_CASE					(      self							:	List[Any] ):
    UpperCAmelCase__	:List[Any]      =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase )


 def        __SCREAMING_SNAKE_CASE					(      self							:	str ):
    UpperCAmelCase__	:List[str]      =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[Any] ):
    UpperCAmelCase__	:List[str]      =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase )





 @slow
 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[Any] ):
    for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase__	:Any      =      LayoutLMvaModel.from_pretrained(__lowerCamelCase )
       self.assertIsNotNone(__lowerCamelCase )


def        a__     (					):
   UpperCAmelCase__	:str      =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
   return image




@require_torch
class 				UpperCAmelCase			(			unittest.TestCase						):


 @cached_property
 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[int] ):
    return LayoutLMvaImageProcessor(apply_ocr=__lowerCamelCase ) if is_vision_available() else None





 @slow
 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict ):
    UpperCAmelCase__	:List[Any]      =      LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' ).to(__lowerCamelCase )

    UpperCAmelCase__	:str      =      self.default_image_processor
    UpperCAmelCase__	:List[str]      =      prepare_img()
    UpperCAmelCase__	:Optional[int]      =      image_processor(images=__lowerCamelCase			,       return_tensors='''pt''' ).pixel_values.to(__lowerCamelCase )

    UpperCAmelCase__	:Optional[Any]      =      torch.tensor([[1, 2]] )
    UpperCAmelCase__	:str      =      torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]] ).unsqueeze(0 )

    # forward pass
    UpperCAmelCase__	:Any      =      model(
        input_ids=input_ids.to(__lowerCamelCase )			,       bbox=bbox.to(__lowerCamelCase )			,       pixel_values=pixel_values.to(__lowerCamelCase )			,       )

    # verify the logits
    UpperCAmelCase__	:List[Any]      =      torch.Size((1, 1_9_9, 7_6_8) )
    self.assertEqual(outputs.last_hidden_state.shape			,       __lowerCamelCase )

    UpperCAmelCase__	:Optional[int]      =      torch.tensor(
        [[-0.05_29, 0.36_18, 0.16_32], [-0.15_87, -0.16_67, -0.04_00], [-0.15_57, -0.16_71, -0.05_05]] ).to(__lowerCamelCase )

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











"""simple docstring"""







def      UpperCamelCase       ( _A )     ->       int:
				lowercase    :      Dict       = 0
				while num > 0:
								digit_sum += num % 10
								num //= 10
				return digit_sum


def      UpperCamelCase       ( _A = 100 )     ->       int:
				lowercase    :      Union[str, Any]       = 1
				lowercase    :      Tuple       = 2

				for i in range(2  ,				max_n + 1 ):
								lowercase    :      Any       = pre_numerator
								lowercase    :      Dict       = 2 * i // 3 if i % 3 == 0 else 1
								lowercase    :      Optional[Any]       = cur_numerator
								lowercase    :      str       = e_cont * pre_numerator + temp

				return sum_digits(_A )


if __name__ == "__main__":
				print(F'{solution() = }')
