



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



       def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_=2  ,UpperCAmelCase_=3  ,UpperCAmelCase_=4  ,UpperCAmelCase_=2  ,UpperCAmelCase_=7  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=99  ,UpperCAmelCase_=36  ,UpperCAmelCase_=3  ,UpperCAmelCase_=4  ,UpperCAmelCase_=37  ,UpperCAmelCase_="gelu"  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=512  ,UpperCAmelCase_=16  ,UpperCAmelCase_=2  ,UpperCAmelCase_=0.02  ,UpperCAmelCase_=6  ,UpperCAmelCase_=6  ,UpperCAmelCase_=3  ,UpperCAmelCase_=4  ,UpperCAmelCase_=None  ,UpperCAmelCase_=1_000  ,)	->				Tuple:
           lowercase__									=    parent
           lowercase__									=    batch_size
           lowercase__									=    num_channels
           lowercase__									=    image_size
           lowercase__									=    patch_size
           lowercase__									=    text_seq_length
           lowercase__									=    is_training
           lowercase__									=    use_input_mask
           lowercase__									=    use_token_type_ids
           lowercase__									=    use_labels
           lowercase__									=    vocab_size
           lowercase__									=    hidden_size
           lowercase__									=    num_hidden_layers
           lowercase__									=    num_attention_heads
           lowercase__									=    intermediate_size
           lowercase__									=    hidden_act
           lowercase__									=    hidden_dropout_prob
           lowercase__									=    attention_probs_dropout_prob
           lowercase__									=    max_position_embeddings
           lowercase__									=    type_vocab_size
           lowercase__									=    type_sequence_label_size
           lowercase__									=    initializer_range
           lowercase__									=    coordinate_size
           lowercase__									=    shape_size
           lowercase__									=    num_labels
           lowercase__									=    num_choices
           lowercase__									=    scope
           lowercase__									=    range_bbox

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



       def 				_a   (					self   )	->				Any:
           lowercase__									=    ids_tensor([self.batch_size, self.text_seq_length]  ,self.vocab_size   )

           lowercase__									=    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]:
                       lowercase__									=    bbox[i, j, 3]
                       lowercase__									=    bbox[i, j, 1]
                       lowercase__									=    t
                   if bbox[i, j, 2] < bbox[i, j, 0]:
                       lowercase__									=    bbox[i, j, 2]
                       lowercase__									=    bbox[i, j, 0]
                       lowercase__									=    t

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

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

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

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

           lowercase__									=    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  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Tuple:
           lowercase__									=    LayoutLMvaModel(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()

           # text + image
           lowercase__									=    model(UpperCAmelCase_  ,pixel_values=UpperCAmelCase_   )
           lowercase__									=    model(
               UpperCAmelCase_  ,bbox=UpperCAmelCase_  ,pixel_values=UpperCAmelCase_  ,attention_mask=UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_   )
           lowercase__									=    model(UpperCAmelCase_  ,bbox=UpperCAmelCase_  ,pixel_values=UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_   )
           lowercase__									=    model(UpperCAmelCase_  ,bbox=UpperCAmelCase_  ,pixel_values=UpperCAmelCase_   )

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

           # text only
           lowercase__									=    model(UpperCAmelCase_   )
           self.parent.assertEqual(
               result.last_hidden_state.shape  ,(self.batch_size, self.text_seq_length, self.hidden_size)   )

           # image only
           lowercase__									=    model(pixel_values=UpperCAmelCase_   )
           self.parent.assertEqual(
               result.last_hidden_state.shape  ,(self.batch_size, self.image_seq_length, self.hidden_size)   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Union[str, Any]:
           lowercase__									=    self.num_labels
           lowercase__									=    LayoutLMvaForSequenceClassification(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    model(
               UpperCAmelCase_  ,bbox=UpperCAmelCase_  ,pixel_values=UpperCAmelCase_  ,attention_mask=UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,labels=UpperCAmelCase_  ,)
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.num_labels)   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				List[Any]:
           lowercase__									=    self.num_labels
           lowercase__									=    LayoutLMvaForTokenClassification(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    model(
               UpperCAmelCase_  ,bbox=UpperCAmelCase_  ,pixel_values=UpperCAmelCase_  ,attention_mask=UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,labels=UpperCAmelCase_  ,)
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.text_seq_length, self.num_labels)   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Optional[int]:
           lowercase__									=    LayoutLMvaForQuestionAnswering(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    model(
               UpperCAmelCase_  ,bbox=UpperCAmelCase_  ,pixel_values=UpperCAmelCase_  ,attention_mask=UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,start_positions=UpperCAmelCase_  ,end_positions=UpperCAmelCase_  ,)
           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   )	->				Tuple:
           lowercase__									=    self.prepare_config_and_inputs()
           (
               (
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							
           )									=    config_and_inputs
           lowercase__									=    {
               "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 					snake_case			(UpperCamelCase		,   UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:Optional[Any]     =       False
       lowerCAmelCase__	:Optional[Any]     =       False
       lowerCAmelCase__	:List[str]     =       False

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



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Optional[int]:
           # `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 				_a   (					self   )	->				Dict:
           lowercase__									=    LayoutLMvaModelTester(self   )
           lowercase__									=    ConfigTester(self  ,config_class=UpperCAmelCase_  ,hidden_size=37   )



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

           return inputs_dict



       def 				_a   (					self   )	->				List[Any]:
           self.config_tester.run_common_tests()



       def 				_a   (					self   )	->				Optional[int]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				Any:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
               lowercase__									=    type
               self.model_tester.create_and_check_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				int:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase_   )







       @slow
       def 				_a   (					self   )	->				Optional[Any]:
           for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__									=    LayoutLMvaModel.from_pretrained(UpperCAmelCase_   )
               self.assertIsNotNone(UpperCAmelCase_   )





def 							lowerCamelCase	(					):





    '''simple docstring'''


    lowercase__									=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
    return image



@require_torch
class 					snake_case			(unittest.TestCase ):



       @cached_property
       def 				_a   (					self   )	->				List[Any]:
           return LayoutLMvaImageProcessor(apply_ocr=UpperCAmelCase_   ) if is_vision_available() else None







       @slow
       def 				_a   (					self   )	->				Any:
           lowercase__									=    LayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base"   ).to(UpperCAmelCase_   )

           lowercase__									=    self.default_image_processor
           lowercase__									=    prepare_img()
           lowercase__									=    image_processor(images=UpperCAmelCase_  ,return_tensors="pt"   ).pixel_values.to(UpperCAmelCase_   )

           lowercase__									=    torch.tensor([[1, 2]]   )
           lowercase__									=    torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]   ).unsqueeze(0   )

           # forward pass
           lowercase__									=    model(
               input_ids=input_ids.to(UpperCAmelCase_   )  ,bbox=bbox.to(UpperCAmelCase_   )  ,pixel_values=pixel_values.to(UpperCAmelCase_   )  ,)

           # verify the logits
           lowercase__									=    torch.Size((1, 199, 768)   )
           self.assertEqual(outputs.last_hidden_state.shape  ,UpperCAmelCase_   )

           lowercase__									=    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(UpperCAmelCase_   )

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









'''simple docstring'''





from __future__ import annotations




def 							lowerCamelCase	(					_snake_case	:    int  ,_snake_case	:    int							):





    '''simple docstring'''


    if b == 0:
        return (1, 0)
    ((lowercase__)							,							(lowercase__))									=    extended_euclid(_snake_case  ,a % b							)
    lowercase__									=    a // b
    return (y, x - k * y)




def 							lowerCamelCase	(					_snake_case	:    int  ,_snake_case	:    int  ,_snake_case	:    int  ,_snake_case	:    int							):





    '''simple docstring'''


    ((lowercase__)							,							(lowercase__))									=    extended_euclid(_snake_case  ,_snake_case							)
    lowercase__									=    na * na
    lowercase__									=    ra * x * na + ra * y * na
    return (n % m + m) % m




def 							lowerCamelCase	(					_snake_case	:    int  ,_snake_case	:    int							):





    '''simple docstring'''


    ((lowercase__)							,							(lowercase__))									=    extended_euclid(_snake_case  ,_snake_case							)
    if b < 0:
        lowercase__									=    (b % n + n) % n
    return b




def 							lowerCamelCase	(					_snake_case	:    int  ,_snake_case	:    int  ,_snake_case	:    int  ,_snake_case	:    int							):





    '''simple docstring'''


    lowercase__							,							lowercase__									=    invert_modulo(_snake_case  ,_snake_case							), invert_modulo(_snake_case  ,_snake_case							)
    lowercase__									=    na * na
    lowercase__									=    ra * x * na + ra * y * na
    return (n % m + m) % m


if __name__ == "__main__":
 from doctest import testmod

 testmod(name="chinese_remainder_theorem", verbose=True)
 testmod(name="chinese_remainder_theorem2", verbose=True)
 testmod(name="invert_modulo", verbose=True)
 testmod(name="extended_euclid", verbose=True)




