


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



    """simple docstring"""






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

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






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

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

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

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

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

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

         __UpperCamelCase   							=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 ,       A_ ,       A_ ,       A_ ,       A_ ,       A_ ,       A_ ,       A_ ,       A_   )  ->	int:
         __UpperCamelCase   							=LayoutLMvaModel(config=A_   )
         model.to(A_   )
         model.eval()

         # text + image
         __UpperCamelCase   							=model(A_ ,       pixel_values=A_   )
         __UpperCamelCase   							=model(
             A_ ,       bbox=A_ ,       pixel_values=A_ ,       attention_mask=A_ ,       token_type_ids=A_   )
         __UpperCamelCase   							=model(A_ ,       bbox=A_ ,       pixel_values=A_ ,       token_type_ids=A_   )
         __UpperCamelCase   							=model(A_ ,       bbox=A_ ,       pixel_values=A_   )

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

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

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






    def _a (      self ,       A_ ,       A_ ,       A_ ,       A_ ,       A_ ,       A_ ,       A_ ,       A_   )  ->	Tuple:
         __UpperCamelCase   							=self.num_labels
         __UpperCamelCase   							=LayoutLMvaForSequenceClassification(A_   )
         model.to(A_   )
         model.eval()
         __UpperCamelCase   							=model(
             A_ ,       bbox=A_ ,       pixel_values=A_ ,       attention_mask=A_ ,       token_type_ids=A_ ,       labels=A_ ,       )
         self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.num_labels)   )






    def _a (      self ,       A_ ,       A_ ,       A_ ,       A_ ,       A_ ,       A_ ,       A_ ,       A_   )  ->	Tuple:
         __UpperCamelCase   							=self.num_labels
         __UpperCamelCase   							=LayoutLMvaForTokenClassification(config=A_   )
         model.to(A_   )
         model.eval()
         __UpperCamelCase   							=model(
             A_ ,       bbox=A_ ,       pixel_values=A_ ,       attention_mask=A_ ,       token_type_ids=A_ ,       labels=A_ ,       )
         self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.text_seq_length, self.num_labels)   )






    def _a (      self ,       A_ ,       A_ ,       A_ ,       A_ ,       A_ ,       A_ ,       A_ ,       A_   )  ->	Any:
         __UpperCamelCase   							=LayoutLMvaForQuestionAnswering(config=A_   )
         model.to(A_   )
         model.eval()
         __UpperCamelCase   							=model(
             A_ ,       bbox=A_ ,       pixel_values=A_ ,       attention_mask=A_ ,       token_type_ids=A_ ,       start_positions=A_ ,       end_positions=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   )  ->	int:
         __UpperCamelCase   							=self.prepare_config_and_inputs()
         (
             (
             __UpperCamelCase				
         )							,				(
             __UpperCamelCase				
         )							,				(
             __UpperCamelCase				
         )							,				(
             __UpperCamelCase				
         )							,				(
             __UpperCamelCase				
         )							,				(
             __UpperCamelCase				
         )							,				(
             __UpperCamelCase				
         )							,				(
             __UpperCamelCase				
         )							,								
         )   							=config_and_inputs
         __UpperCamelCase   							={
             '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__	( A_       ,  A_       ,  unittest.TestCase		):



    """simple docstring"""






    UpperCAmelCase__						:		List[Any]              =  False
    UpperCAmelCase__						:		List[Any]              =  False
    UpperCAmelCase__						:		List[str]              =  False

    UpperCAmelCase__						:		Optional[Any]              =  (
        (
            LayoutLMvaModel,
            LayoutLMvaForSequenceClassification,
            LayoutLMvaForTokenClassification,
            LayoutLMvaForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    UpperCAmelCase__						:		int              =  (
        {"document-question-answering": LayoutLMvaForQuestionAnswering, "feature-extraction": LayoutLMvaModel}
        if is_torch_available()
        else {}
    )






    def _a (      self ,       A_ ,       A_ ,       A_ ,       A_ ,       A_   )  ->	List[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 _a (      self   )  ->	List[str]:
         __UpperCamelCase   							=LayoutLMvaModelTester(self   )
         __UpperCamelCase   							=ConfigTester(self ,       config_class=A_ ,       hidden_size=37   )






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

         return inputs_dict






    def _a (      self   )  ->	str:
         self.config_tester.run_common_tests()






    def _a (      self   )  ->	str:
         __UpperCamelCase   							=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*A_   )






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






    def _a (      self   )  ->	List[str]:
         __UpperCamelCase   							=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*A_   )






    def _a (      self   )  ->	Any:
         __UpperCamelCase   							=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*A_   )






    def _a (      self   )  ->	Tuple:
         __UpperCamelCase   							=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*A_   )






    @slow
    def _a (      self   )  ->	List[str]:
         for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              __UpperCamelCase   							=LayoutLMvaModel.from_pretrained(A_   )
              self.assertIsNotNone(A_   )

def 						_UpperCAmelCase    (      ):
     __UpperCamelCase   							=Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'    )
     return image






@require_torch
class    UpperCAmelCase__	( unittest.TestCase		):



    """simple docstring"""






    @cached_property
    def _a (      self   )  ->	Tuple:
         return LayoutLMvaImageProcessor(apply_ocr=A_   ) if is_vision_available() else None






    @slow
    def _a (      self   )  ->	Dict:
         __UpperCamelCase   							=LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'   ).to(A_   )

         __UpperCamelCase   							=self.default_image_processor
         __UpperCamelCase   							=prepare_img()
         __UpperCamelCase   							=image_processor(images=A_ ,       return_tensors='pt'   ).pixel_values.to(A_   )

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

         # forward pass
         __UpperCamelCase   							=model(
             input_ids=input_ids.to(A_   ) ,       bbox=bbox.to(A_   ) ,       pixel_values=pixel_values.to(A_   ) ,       )

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

         __UpperCamelCase   							=torch.tensor(
             [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]   ).to(A_   )

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









import inspect
import os
import unittest
from dataclasses import dataclass

import torch

from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs
from accelerate.state import AcceleratorState
from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu
from accelerate.utils import KwargsHandler






@dataclass
class    UpperCAmelCase__	( A_		):



    """simple docstring"""






    UpperCAmelCase__						:		int              =  0
    UpperCAmelCase__						:		bool              =  False
    UpperCAmelCase__						:		float              =  3.0






class    UpperCAmelCase__	( unittest.TestCase		):



    """simple docstring"""






    def _a (      self   )  ->	Any:
         # If no defaults are changed, `to_kwargs` returns an empty dict.
         self.assertDictEqual(MockClass().to_kwargs() ,       {}   )
         self.assertDictEqual(MockClass(a=2   ).to_kwargs() ,       {'a': 2}   )
         self.assertDictEqual(MockClass(a=2 ,       b=A_   ).to_kwargs() ,       {'a': 2, 'b': True}   )
         self.assertDictEqual(MockClass(a=2 ,       c=2.25   ).to_kwargs() ,       {'a': 2, 'c': 2.25}   )






    @require_cuda
    def _a (      self   )  ->	Union[str, Any]:
         # If no defaults are changed, `to_kwargs` returns an empty dict.
         __UpperCamelCase   							=GradScalerKwargs(init_scale=1024 ,       growth_factor=2   )
         AcceleratorState._reset_state()
         __UpperCamelCase   							=Accelerator(mixed_precision='fp16' ,       kwargs_handlers=[scaler_handler]   )
         print(accelerator.use_fpaa   )
         __UpperCamelCase   							=accelerator.scaler

         # Check the kwargs have been applied
         self.assertEqual(scaler._init_scale ,       1024.0   )
         self.assertEqual(scaler._growth_factor ,       2.0   )

         # Check the other values are at the default
         self.assertEqual(scaler._backoff_factor ,       0.5   )
         self.assertEqual(scaler._growth_interval ,       2000   )
         self.assertEqual(scaler._enabled ,       A_   )






    @require_multi_gpu
    def _a (      self   )  ->	Optional[int]:
         __UpperCamelCase   							=['torchrun', f'--nproc_per_node={torch.cuda.device_count()}', inspect.getfile(self.__class__   )]
         execute_subprocess_async(A_ ,       env=os.environ.copy()   )


if __name__ == "__main__":
   _A			=						DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True)
   _A			=						Accelerator(kwargs_handlers=[ddp_scaler])
   _A			=						torch.nn.Linear(100, 200)
   _A			=						accelerator.prepare(model)

   # Check the values changed in kwargs
   _A			=						''
   _A			=						model.bucket_bytes_cap // (1024 * 1024)
   if observed_bucket_cap_map != 15:
      error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n"
   if model.find_unused_parameters is not True:
      error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n"

   # Check the values of the defaults
   if model.dim != 0:
      error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n"
   if model.broadcast_buffers is not True:
      error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n"
   if model.gradient_as_bucket_view is not False:
      error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n"

   # Raise error at the end to make sure we don't stop at the first failure.
   if len(error_msg) > 0:
      raise ValueError(error_msg)





