import random
import unittest

import numpy as np
import torch
from PIL import Image
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyVaaPriorEmbaEmbPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()




class  _UpperCAmelCase       (			lowerCAmelCase,       unittest.TestCase   ):



 '''simple docstring'''




 __A           = KandinskyVaaPriorEmbaEmbPipeline
 __A           = ['''prompt''', '''image''']
 __A           = ['''prompt''', '''image''']
 __A           = [
     '''num_images_per_prompt''',
     '''strength''',
     '''generator''',
     '''num_inference_steps''',
     '''latents''',
     '''negative_prompt''',
     '''guidance_scale''',
     '''output_type''',
     '''return_dict''',
 ]
 __A           = False







 @property
 def 						__UpperCAmelCase							(						self							:					List[str])		->     List[str]:


   """simple docstring"""







   return 32







 @property
 def 						__UpperCAmelCase							(						self							:					List[Any])		->     Union[str, Any]:


   """simple docstring"""







   return 32







 @property
 def 						__UpperCAmelCase							(						self							:					Any)		->     Optional[int]:


   """simple docstring"""







   return self.time_input_dim







 @property
 def 						__UpperCAmelCase							(						self							:					Optional[Any])		->     int:


   """simple docstring"""







   return self.time_input_dim * 4







 @property
 def 						__UpperCAmelCase							(						self							:					int)		->     str:


   """simple docstring"""







   return 100







 @property
 def 						__UpperCAmelCase							(						self							:					List[Any])		->     List[str]:


   """simple docstring"""







   _UpperCamelCase				=  CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")
   return tokenizer







 @property
 def 						__UpperCAmelCase							(						self							:					Optional[Any])		->     Optional[int]:


   """simple docstring"""







   torch.manual_seed(0)
   _UpperCamelCase				=  CLIPTextConfig(
       bos_token_id=0				,      eos_token_id=2				,      hidden_size=self.text_embedder_hidden_size				,      projection_dim=self.text_embedder_hidden_size				,      intermediate_size=37				,      layer_norm_eps=1e-0_5				,      num_attention_heads=4				,      num_hidden_layers=5				,      pad_token_id=1				,      vocab_size=1000				,      )
   return CLIPTextModelWithProjection(lowercase_)







 @property
 def 						__UpperCAmelCase							(						self							:					Optional[Any])		->     List[str]:


   """simple docstring"""







   torch.manual_seed(0)

   _UpperCamelCase				=  {
       "num_attention_heads": 2,
       "attention_head_dim": 12,
       "embedding_dim": self.text_embedder_hidden_size,
       "num_layers": 1,
   }

   _UpperCamelCase				=  PriorTransformer(**lowercase_)
   # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
   _UpperCamelCase				=  nn.Parameter(torch.ones(model.clip_std.shape))
   return model







 @property
 def 						__UpperCAmelCase							(						self							:					str)		->     Union[str, Any]:


   """simple docstring"""







   torch.manual_seed(0)
   _UpperCamelCase				=  CLIPVisionConfig(
       hidden_size=self.text_embedder_hidden_size				,      image_size=224				,      projection_dim=self.text_embedder_hidden_size				,      intermediate_size=37				,      num_attention_heads=4				,      num_channels=3				,      num_hidden_layers=5				,      patch_size=14				,      )

   _UpperCamelCase				=  CLIPVisionModelWithProjection(lowercase_)
   return model







 @property
 def 						__UpperCAmelCase							(						self							:					Optional[Any])		->     Union[str, Any]:


   """simple docstring"""







   _UpperCamelCase				=  CLIPImageProcessor(
       crop_size=224				,      do_center_crop=lowercase_				,      do_normalize=lowercase_				,      do_resize=lowercase_				,      image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]				,      image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]				,      resample=3				,      size=224				,      )

   return image_processor







 def 						__UpperCAmelCase							(						self							:					List[Any])		->     Optional[Any]:


   """simple docstring"""







   _UpperCamelCase				=  self.dummy_prior
   _UpperCamelCase				=  self.dummy_image_encoder
   _UpperCamelCase				=  self.dummy_text_encoder
   _UpperCamelCase				=  self.dummy_tokenizer
   _UpperCamelCase				=  self.dummy_image_processor

   _UpperCamelCase				=  UnCLIPScheduler(
       variance_type="fixed_small_log"				,      prediction_type="sample"				,      num_train_timesteps=1000				,      clip_sample=lowercase_				,      clip_sample_range=10.0				,      )

   _UpperCamelCase				=  {
       "prior": prior,
       "image_encoder": image_encoder,
       "text_encoder": text_encoder,
       "tokenizer": tokenizer,
       "scheduler": scheduler,
       "image_processor": image_processor,
   }

   return components







 def 						__UpperCAmelCase							(						self							:					List[str]				,      lowercase_							:					Optional[int]				,      lowercase_							:					Union[str, Any]=0)		->     Tuple:


   """simple docstring"""







   if str(lowercase_).startswith("mps"):
     _UpperCamelCase				=  torch.manual_seed(lowercase_)
   else:
     _UpperCamelCase				=  torch.Generator(device=lowercase_).manual_seed(lowercase_)

   _UpperCamelCase				=  floats_tensor((1, 3, 64, 64)				,      rng=random.Random(lowercase_)).to(lowercase_)
   _UpperCamelCase				=  image.cpu().permute(0				,      2				,      3				,      1)[0]
   _UpperCamelCase				=  Image.fromarray(np.uinta(lowercase_)).convert("RGB").resize((256, 256))

   _UpperCamelCase				=  {
       "prompt": "horse",
       "image": init_image,
       "strength": 0.5,
       "generator": generator,
       "guidance_scale": 4.0,
       "num_inference_steps": 2,
       "output_type": "np",
   }
   return inputs







 def 						__UpperCAmelCase							(						self							:					List[str])		->     Union[str, Any]:


   """simple docstring"""







   _UpperCamelCase				=  "cpu"

   _UpperCamelCase				=  self.get_dummy_components()

   _UpperCamelCase				=  self.pipeline_class(**lowercase_)
   _UpperCamelCase				=  pipe.to(lowercase_)

   pipe.set_progress_bar_config(disable=lowercase_)

   _UpperCamelCase				=  pipe(**self.get_dummy_inputs(lowercase_))
   _UpperCamelCase				=  output.image_embeds

   _UpperCamelCase				=  pipe(
       **self.get_dummy_inputs(lowercase_)				,      return_dict=lowercase_				,      )[0]

   _UpperCamelCase				=  image[0, -10:]
   _UpperCamelCase				=  image_from_tuple[0, -10:]

   assert image.shape == (1, 32)

   _UpperCamelCase				=  np.array(
       [
           0.1_07_12_84,
           1.3_33_02_71,
           0.61_26_02_23,
           -0.6_69_10_65,
           -0.3_84_68_52,
           -1.0_30_36_61,
           0.22_71_61_11,
           0.03_34_89_01,
           0.30_04_06_75,
           -0.24_80_50_29,
       ])

   assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2
   assert np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < 1e-2







 @skip_mps
 def 						__UpperCAmelCase							(						self							:					Optional[int])		->     Optional[int]:


   """simple docstring"""







   _UpperCamelCase				=  torch_device == "cpu"
   _UpperCamelCase				=  True
   _UpperCamelCase				=  False

   self._test_inference_batch_single_identical(
       test_max_difference=lowercase_				,      relax_max_difference=lowercase_				,      test_mean_pixel_difference=lowercase_				,      )







 @skip_mps
 def 						__UpperCAmelCase							(						self							:					List[Any])		->     Union[str, Any]:


   """simple docstring"""







   _UpperCamelCase				=  torch_device == "cpu"
   _UpperCamelCase				=  False

   self._test_attention_slicing_forward_pass(
       test_max_difference=lowercase_				,      test_mean_pixel_difference=lowercase_				,      )





import unittest

from transformers import LayoutLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
   import torch

   from transformers import (
       LayoutLMForMaskedLM,
       LayoutLMForQuestionAnswering,
       LayoutLMForSequenceClassification,
       LayoutLMForTokenClassification,
       LayoutLMModel,
   )




class  _UpperCAmelCase       :



 '''simple docstring'''




 def __init__(						self							:					Tuple				,      lowercase_							:					int				,      lowercase_							:					Union[str, Any]=13				,      lowercase_							:					Optional[int]=7				,      lowercase_							:					Optional[Any]=True				,      lowercase_							:					Tuple=True				,      lowercase_							:					str=True				,      lowercase_							:					Union[str, Any]=True				,      lowercase_							:					int=99				,      lowercase_							:					Dict=32				,      lowercase_							:					Optional[int]=5				,      lowercase_							:					List[Any]=4				,      lowercase_							:					List[str]=37				,      lowercase_							:					Dict="gelu"				,      lowercase_							:					Union[str, Any]=0.1				,      lowercase_							:					List[Any]=0.1				,      lowercase_							:					int=512				,      lowercase_							:					int=16				,      lowercase_							:					Dict=2				,      lowercase_							:					Union[str, Any]=0.02				,      lowercase_							:					List[Any]=3				,      lowercase_							:					List[Any]=4				,      lowercase_							:					Optional[Any]=None				,      lowercase_							:					List[Any]=1000				,      )		->     Optional[int]:


   """simple docstring"""







   _UpperCamelCase				=  parent
   _UpperCamelCase				=  batch_size
   _UpperCamelCase				=  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				=  num_labels
   _UpperCamelCase				=  num_choices
   _UpperCamelCase				=  scope
   _UpperCamelCase				=  range_bbox







 def 						__UpperCAmelCase							(						self							:					Union[str, Any])		->     Optional[Any]:


   """simple docstring"""







   _UpperCamelCase				=  ids_tensor([self.batch_size, self.seq_length]				,      self.vocab_size)

   _UpperCamelCase				=  ids_tensor([self.batch_size, self.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				=  None
   if self.use_input_mask:
     _UpperCamelCase				=  ids_tensor([self.batch_size, self.seq_length]				,      vocab_size=2)

   _UpperCamelCase				=  None
   if self.use_token_type_ids:
     _UpperCamelCase				=  ids_tensor([self.batch_size, self.seq_length]				,      self.type_vocab_size)

   _UpperCamelCase				=  None
   _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.seq_length]				,      self.num_labels)
     _UpperCamelCase				=  ids_tensor([self.batch_size]				,      self.num_choices)

   _UpperCamelCase				=  self.get_config()

   return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







 def 						__UpperCAmelCase							(						self							:					Union[str, Any])		->     List[Any]:


   """simple docstring"""







   return LayoutLMConfig(
       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				,      )







 def 						__UpperCAmelCase							(						self							:					List[str]				,      lowercase_							:					Optional[Any]				,      lowercase_							:					str				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Any				,      lowercase_							:					Dict				,      lowercase_							:					List[str]				,      lowercase_							:					Dict				,      lowercase_							:					List[str])		->     List[Any]:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMModel(config=lowercase_)
   model.to(lowercase_)
   model.eval()
   _UpperCamelCase				=  model(lowercase_				,      lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_)
   _UpperCamelCase				=  model(lowercase_				,      lowercase_				,      token_type_ids=lowercase_)
   _UpperCamelCase				=  model(lowercase_				,      lowercase_)
   self.parent.assertEqual(result.last_hidden_state.shape				,      (self.batch_size, self.seq_length, self.hidden_size))
   self.parent.assertEqual(result.pooler_output.shape				,      (self.batch_size, self.hidden_size))







 def 						__UpperCAmelCase							(						self							:					Dict				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Dict				,      lowercase_							:					Tuple				,      lowercase_							:					int				,      lowercase_							:					List[Any]				,      lowercase_							:					int				,      lowercase_							:					Optional[int]				,      lowercase_							:					Tuple)		->     Tuple:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMForMaskedLM(config=lowercase_)
   model.to(lowercase_)
   model.eval()
   _UpperCamelCase				=  model(lowercase_				,      lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      labels=lowercase_)
   self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.seq_length, self.vocab_size))







 def 						__UpperCAmelCase							(						self							:					Union[str, Any]				,      lowercase_							:					List[Any]				,      lowercase_							:					Optional[int]				,      lowercase_							:					Union[str, Any]				,      lowercase_							:					Optional[int]				,      lowercase_							:					Dict				,      lowercase_							:					int				,      lowercase_							:					Dict				,      lowercase_							:					List[str])		->     Any:


   """simple docstring"""







   _UpperCamelCase				=  self.num_labels
   _UpperCamelCase				=  LayoutLMForSequenceClassification(lowercase_)
   model.to(lowercase_)
   model.eval()
   _UpperCamelCase				=  model(
       lowercase_				,      lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      labels=lowercase_)
   self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.num_labels))







 def 						__UpperCAmelCase							(						self							:					List[Any]				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Dict				,      lowercase_							:					Dict				,      lowercase_							:					Union[str, Any]				,      lowercase_							:					str				,      lowercase_							:					Tuple)		->     List[Any]:


   """simple docstring"""







   _UpperCamelCase				=  self.num_labels
   _UpperCamelCase				=  LayoutLMForTokenClassification(config=lowercase_)
   model.to(lowercase_)
   model.eval()
   _UpperCamelCase				=  model(lowercase_				,      lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      labels=lowercase_)
   self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.seq_length, self.num_labels))







 def 						__UpperCAmelCase							(						self							:					str				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Tuple				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Tuple				,      lowercase_							:					str				,      lowercase_							:					Optional[Any]				,      lowercase_							:					Optional[Any])		->     Tuple:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMForQuestionAnswering(config=lowercase_)
   model.to(lowercase_)
   model.eval()
   _UpperCamelCase				=  model(
       lowercase_				,      bbox=lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      start_positions=lowercase_				,      end_positions=lowercase_				,      )
   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							:					Dict)		->     List[str]:


   """simple docstring"""







   _UpperCamelCase				=  self.prepare_config_and_inputs()
   (
       (
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		(
       _UpperCamelCase
   )	,		
   )				=  config_and_inputs
   _UpperCamelCase				=  {
       "input_ids": input_ids,
       "bbox": bbox,
       "token_type_ids": token_type_ids,
       "attention_mask": input_mask,
   }
   return config, inputs_dict




@require_torch
class  _UpperCAmelCase       (			lowerCAmelCase,       lowerCAmelCase,       unittest.TestCase   ):



 '''simple docstring'''




 __A           = (
     (
         LayoutLMModel,
         LayoutLMForMaskedLM,
         LayoutLMForSequenceClassification,
         LayoutLMForTokenClassification,
         LayoutLMForQuestionAnswering,
     )
     if is_torch_available()
     else None
 )
 __A           = (
     {
         '''document-question-answering''': LayoutLMForQuestionAnswering,
         '''feature-extraction''': LayoutLMModel,
         '''fill-mask''': LayoutLMForMaskedLM,
         '''text-classification''': LayoutLMForSequenceClassification,
         '''token-classification''': LayoutLMForTokenClassification,
         '''zero-shot''': LayoutLMForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __A           = True







 def 						__UpperCAmelCase							(						self							:					List[Any])		->     List[str]:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMModelTester(self)
   _UpperCamelCase				=  ConfigTester(self				,      config_class=lowercase_				,      hidden_size=37)







 def 						__UpperCAmelCase							(						self							:					List[Any])		->     Union[str, Any]:


   """simple docstring"""







   self.config_tester.run_common_tests()







 def 						__UpperCAmelCase							(						self							:					Optional[int])		->     str:


   """simple docstring"""







   _UpperCamelCase				=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*lowercase_)







 def 						__UpperCAmelCase							(						self							:					List[Any])		->     Dict:


   """simple docstring"""







   _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(*lowercase_)







 def 						__UpperCAmelCase							(						self							:					Any)		->     Optional[Any]:


   """simple docstring"""







   _UpperCamelCase				=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_lm(*lowercase_)







 def 						__UpperCAmelCase							(						self							:					Dict)		->     str:


   """simple docstring"""







   _UpperCamelCase				=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_sequence_classification(*lowercase_)







 def 						__UpperCAmelCase							(						self							:					Tuple)		->     Dict:


   """simple docstring"""







   _UpperCamelCase				=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_token_classification(*lowercase_)







 def 						__UpperCAmelCase							(						self							:					List[str])		->     Optional[int]:


   """simple docstring"""







   _UpperCamelCase				=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_question_answering(*lowercase_)







 @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests.")
 def 						__UpperCAmelCase							(						self							:					Tuple)		->     List[str]:


   """simple docstring"""







   pass


def 							lowerCAmelCase__    (       )	->Union[str, Any]:




  '''simple docstring'''

  _UpperCamelCase				=  torch.tensor([[101,1_019,1_014,1_016,1_037,12_849,4_747,1_004,14_246,2_278,5_439,4_524,5_002,2_930,2_193,2_930,4_341,3_208,1_005,1_055,2_171,2_848,11_300,3_531,102],[101,4_070,4_034,7_020,1_024,3_058,1_015,1_013,2_861,1_013,6_070,19_274,2_772,6_205,27_814,16_147,16_147,4_343,2_047,10_283,10_969,14_389,1_012,2_338,102]]  ,			device=a__      )  # noqa: E231
  _UpperCamelCase				=  torch.tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],]  ,			device=a__      )  # noqa: E231
  _UpperCamelCase				=  torch.tensor([[[0,0,0,0],[423,237,440,251],[427,272,441,287],[419,115,437,129],[961,885,992,912],[256,38,330,58],[256,38,330,58],[336,42,353,57],[360,39,401,56],[360,39,401,56],[411,39,471,59],[479,41,528,59],[533,39,630,60],[67,113,134,131],[141,115,209,132],[68,149,133,166],[141,149,187,164],[195,148,287,165],[195,148,287,165],[195,148,287,165],[295,148,349,165],[441,149,492,166],[497,149,546,164],[64,201,125,218],[1_000,1_000,1_000,1_000]],[[0,0,0,0],[662,150,754,166],[665,199,742,211],[519,213,554,228],[519,213,554,228],[134,433,187,454],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[314,469,376,482],[504,684,582,706],[941,825,973,900],[941,825,973,900],[941,825,973,900],[941,825,973,900],[610,749,652,765],[130,659,168,672],[176,657,237,672],[238,657,312,672],[443,653,628,672],[443,653,628,672],[716,301,825,317],[1_000,1_000,1_000,1_000]]]  ,			device=a__      )  # noqa: E231
  _UpperCamelCase				=  torch.tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]  ,			device=a__      )  # noqa: E231
  # these are sequence labels (i.e. at the token level)
  _UpperCamelCase				=  torch.tensor([[-100,10,10,10,9,1,-100,7,7,-100,7,7,4,2,5,2,8,8,-100,-100,5,0,3,2,-100],[-100,12,12,12,-100,12,10,-100,-100,-100,-100,10,12,9,-100,-100,-100,10,10,10,9,12,-100,10,-100]]  ,			device=a__      )  # noqa: E231
  # fmt: on

  return input_ids, attention_mask, bbox, token_type_ids, labels







@require_torch
class  _UpperCAmelCase       (			unittest.TestCase   ):



 '''simple docstring'''




 @slow
 def 						__UpperCAmelCase							(						self							:					Any)		->     Union[str, Any]:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMModel.from_pretrained("microsoft/layoutlm-base-uncased").to(lowercase_)

   _UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase				=  prepare_layoutlm_batch_inputs()

   # forward pass
   _UpperCamelCase				=  model(input_ids=lowercase_				,      bbox=lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_)

   # test the sequence output on [0, :3, :3]
   _UpperCamelCase				=  torch.tensor(
       [[0.17_85, -0.19_47, -0.04_25], [-0.32_54, -0.28_07, 0.25_53], [-0.53_91, -0.33_22, 0.33_64]]				,      device=lowercase_				,      )

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

   # test the pooled output on [1, :3]
   _UpperCamelCase				=  torch.tensor([-0.65_80, -0.02_14, 0.85_52]				,      device=lowercase_)

   self.assertTrue(torch.allclose(outputs.pooler_output[1, :3]				,      lowercase_				,      atol=1e-3))







 @slow
 def 						__UpperCAmelCase							(						self							:					Optional[int])		->     List[str]:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMForSequenceClassification.from_pretrained("microsoft/layoutlm-base-uncased"				,      num_labels=2).to(
       lowercase_)

   _UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase				=  prepare_layoutlm_batch_inputs()

   # forward pass
   _UpperCamelCase				=  model(
       input_ids=lowercase_				,      bbox=lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      labels=torch.tensor([1, 1]				,      device=lowercase_)				,      )

   # test whether we get a loss as a scalar
   _UpperCamelCase				=  outputs.loss
   _UpperCamelCase				=  torch.Size([])
   self.assertEqual(loss.shape				,      lowercase_)

   # test the shape of the logits
   _UpperCamelCase				=  outputs.logits
   _UpperCamelCase				=  torch.Size((2, 2))
   self.assertEqual(logits.shape				,      lowercase_)







 @slow
 def 						__UpperCAmelCase							(						self							:					Tuple)		->     Any:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-base-uncased"				,      num_labels=13).to(
       lowercase_)

   _UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase				=  prepare_layoutlm_batch_inputs()

   # forward pass
   _UpperCamelCase				=  model(
       input_ids=lowercase_				,      bbox=lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      labels=lowercase_)

   # test the loss calculation to be around 2.65
   # expected_loss = torch.tensor(2.65, device=torch_device)

   # The loss is currently somewhat random and can vary between 0.1-0.3 atol.
   # self.assertTrue(torch.allclose(outputs.loss, expected_loss, atol=0.1))

   # test the shape of the logits
   _UpperCamelCase				=  outputs.logits
   _UpperCamelCase				=  torch.Size((2, 25, 13))
   self.assertEqual(logits.shape				,      lowercase_)







 @slow
 def 						__UpperCAmelCase							(						self							:					Any)		->     int:


   """simple docstring"""







   _UpperCamelCase				=  LayoutLMForQuestionAnswering.from_pretrained("microsoft/layoutlm-base-uncased").to(lowercase_)

   _UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase				=  prepare_layoutlm_batch_inputs()

   # forward pass
   _UpperCamelCase				=  model(input_ids=lowercase_				,      bbox=lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_)

   # test the shape of the logits
   _UpperCamelCase				=  torch.Size((2, 25))
   self.assertEqual(outputs.start_logits.shape				,      lowercase_)
   self.assertEqual(outputs.end_logits.shape				,      lowercase_)




