





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





   def __init__(			self       ,      snake_case       ,      snake_case=2       ,      snake_case=3       ,      snake_case=4       ,      snake_case=2       ,      snake_case=7       ,      snake_case=True       ,      snake_case=True       ,      snake_case=True       ,      snake_case=True       ,      snake_case=99       ,      snake_case=36       ,      snake_case=3       ,      snake_case=4       ,      snake_case=37       ,      snake_case="gelu"       ,      snake_case=0.1       ,      snake_case=0.1       ,      snake_case=512       ,      snake_case=16       ,      snake_case=2       ,      snake_case=0.02       ,      snake_case=6       ,      snake_case=6       ,      snake_case=3       ,      snake_case=4       ,      snake_case=None       ,      snake_case=1_000       ,      )					->	Dict:
      """simple docstring"""





      a__						:    Union[str, Any]   		=	parent
      a__						:    List[str]   		=	batch_size
      a__						:    str   		=	num_channels
      a__						:    Dict   		=	image_size
      a__						:    str   		=	patch_size
      a__						:    Optional[int]   		=	text_seq_length
      a__						:    int   		=	is_training
      a__						:    Dict   		=	use_input_mask
      a__						:    Union[str, Any]   		=	use_token_type_ids
      a__						:    List[str]   		=	use_labels
      a__						:    List[Any]   		=	vocab_size
      a__						:    Tuple   		=	hidden_size
      a__						:    int   		=	num_hidden_layers
      a__						:    Optional[int]   		=	num_attention_heads
      a__						:    Union[str, Any]   		=	intermediate_size
      a__						:    Dict   		=	hidden_act
      a__						:    List[str]   		=	hidden_dropout_prob
      a__						:    str   		=	attention_probs_dropout_prob
      a__						:    Dict   		=	max_position_embeddings
      a__						:    List[Any]   		=	type_vocab_size
      a__						:    Optional[Any]   		=	type_sequence_label_size
      a__						:    Union[str, Any]   		=	initializer_range
      a__						:    int   		=	coordinate_size
      a__						:    int   		=	shape_size
      a__						:    Dict   		=	num_labels
      a__						:    str   		=	num_choices
      a__						:    Optional[int]   		=	scope
      a__						:    Tuple   		=	range_bbox

      # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
      a__						:    Union[str, Any]   		=	text_seq_length
      a__						:    Any   		=	(image_size // patch_size) ** 2 + 1
      a__						:    Union[str, Any]   		=	self.text_seq_length + self.image_seq_length





   def 						_snake_case			(			self   )					->	List[str]:
      """simple docstring"""





      a__						:    Any   		=	ids_tensor([self.batch_size, self.text_seq_length]       ,      self.vocab_size   )

      a__						:    Union[str, 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]:
               a__						:    Dict   		=	bbox[i, j, 3]
               a__						:    List[Any]   		=	bbox[i, j, 1]
               a__						:    Union[str, Any]   		=	t
            if bbox[i, j, 2] < bbox[i, j, 0]:
               a__						:    List[str]   		=	bbox[i, j, 2]
               a__						:    Dict   		=	bbox[i, j, 0]
               a__						:    Optional[int]   		=	t

      a__						:    List[Any]   		=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

      a__						:    List[str]   		=	None
      if self.use_input_mask:
         a__						:    List[Any]   		=	random_attention_mask([self.batch_size, self.text_seq_length]   )

      a__						:    Union[str, Any]   		=	None
      if self.use_token_type_ids:
         a__						:    Dict   		=	ids_tensor([self.batch_size, self.text_seq_length]       ,      self.type_vocab_size   )

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

      a__						:    List[Any]   		=	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 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case   )					->	List[str]:
      """simple docstring"""





      a__						:    List[str]   		=	LayoutLMvaModel(config=snake_case   )
      model.to(snake_case   )
      model.eval()

      # text + image
      a__						:    List[Any]   		=	model(snake_case       ,      pixel_values=snake_case   )
      a__						:    Tuple   		=	model(
          snake_case       ,      bbox=snake_case       ,      pixel_values=snake_case       ,      attention_mask=snake_case       ,      token_type_ids=snake_case   )
      a__						:    Tuple   		=	model(snake_case       ,      bbox=snake_case       ,      pixel_values=snake_case       ,      token_type_ids=snake_case   )
      a__						:    str   		=	model(snake_case       ,      bbox=snake_case       ,      pixel_values=snake_case   )

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

      # text only
      a__						:    str   		=	model(snake_case   )
      self.parent.assertEqual(
          result.last_hidden_state.shape       ,      (self.batch_size, self.text_seq_length, self.hidden_size)   )

      # image only
      a__						:    Optional[int]   		=	model(pixel_values=snake_case   )
      self.parent.assertEqual(
          result.last_hidden_state.shape       ,      (self.batch_size, self.image_seq_length, self.hidden_size)   )





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case   )					->	Tuple:
      """simple docstring"""





      a__						:    int   		=	self.num_labels
      a__						:    Union[str, Any]   		=	LayoutLMvaForSequenceClassification(snake_case   )
      model.to(snake_case   )
      model.eval()
      a__						:    Dict   		=	model(
          snake_case       ,      bbox=snake_case       ,      pixel_values=snake_case       ,      attention_mask=snake_case       ,      token_type_ids=snake_case       ,      labels=snake_case       ,      )
      self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.num_labels)   )





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case   )					->	Optional[Any]:
      """simple docstring"""





      a__						:    List[str]   		=	self.num_labels
      a__						:    Optional[int]   		=	LayoutLMvaForTokenClassification(config=snake_case   )
      model.to(snake_case   )
      model.eval()
      a__						:    List[str]   		=	model(
          snake_case       ,      bbox=snake_case       ,      pixel_values=snake_case       ,      attention_mask=snake_case       ,      token_type_ids=snake_case       ,      labels=snake_case       ,      )
      self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.text_seq_length, self.num_labels)   )





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case   )					->	Optional[int]:
      """simple docstring"""





      a__						:    Union[str, Any]   		=	LayoutLMvaForQuestionAnswering(config=snake_case   )
      model.to(snake_case   )
      model.eval()
      a__						:    Optional[Any]   		=	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       ,      )
      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 						_snake_case			(			self   )					->	Optional[Any]:
      """simple docstring"""





      a__						:    Union[str, Any]   		=	self.prepare_config_and_inputs()
      (
          (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  
      )						:    Optional[int]   		=	config_and_inputs
      a__						:    List[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 	__lowerCAmelCase      (       _UpperCamelCase		,_UpperCamelCase		,unittest.TestCase	):
   _UpperCamelCase			:    List[Any]  		=					False
   _UpperCamelCase			:    List[str]  		=					False
   _UpperCamelCase			:    List[Any]  		=					False

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





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case   )					->	Union[str, Any]:
      """simple docstring"""





      return True





   def 						_snake_case			(			self   )					->	Dict:
      """simple docstring"""





      a__						:    Optional[int]   		=	LayoutLMvaModelTester(self   )
      a__						:    Any   		=	ConfigTester(self       ,      config_class=snake_case       ,      hidden_size=37   )





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case=False   )					->	Dict:
      """simple docstring"""





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

      return inputs_dict





   def 						_snake_case			(			self   )					->	Tuple:
      """simple docstring"""





      self.config_tester.run_common_tests()





   def 						_snake_case			(			self   )					->	List[str]:
      """simple docstring"""





      a__						:    Dict   		=	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*snake_case   )





   def 						_snake_case			(			self   )					->	Dict:
      """simple docstring"""





      a__						:    str   		=	self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
         a__						:    str   		=	type
         self.model_tester.create_and_check_model(*snake_case   )





   def 						_snake_case			(			self   )					->	Dict:
      """simple docstring"""





      a__						:    Optional[int]   		=	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*snake_case   )





   def 						_snake_case			(			self   )					->	Union[str, Any]:
      """simple docstring"""





      a__						:    List[Any]   		=	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*snake_case   )





   def 						_snake_case			(			self   )					->	Any:
      """simple docstring"""





      a__						:    List[str]   		=	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*snake_case   )





   @slow
   def 						_snake_case			(			self   )					->	Optional[Any]:
      """simple docstring"""





      for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a__						:    int   		=	LayoutLMvaModel.from_pretrained(snake_case   )
         self.assertIsNotNone(snake_case   )







def    _A	(   ):
   a__						:    Tuple   		=	Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
   return image







@require_torch
class 	__lowerCAmelCase      (       unittest.TestCase	):





   @cached_property
   def 						_snake_case			(			self   )					->	Union[str, Any]:
      """simple docstring"""





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





   @slow
   def 						_snake_case			(			self   )					->	Optional[int]:
      """simple docstring"""





      a__						:    List[Any]   		=	LayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base"   ).to(snake_case   )

      a__						:    Optional[int]   		=	self.default_image_processor
      a__						:    List[str]   		=	prepare_img()
      a__						:    str   		=	image_processor(images=snake_case       ,      return_tensors="pt"   ).pixel_values.to(snake_case   )

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

      # forward pass
      a__						:    Optional[int]   		=	model(
          input_ids=input_ids.to(snake_case   )       ,      bbox=bbox.to(snake_case   )       ,      pixel_values=pixel_values.to(snake_case   )       ,      )

      # verify the logits
      a__						:    Optional[Any]   		=	torch.Size((1, 199, 768)   )
      self.assertEqual(outputs.last_hidden_state.shape       ,      snake_case   )

      a__						:    List[Any]   		=	torch.tensor(
          [[-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]]   ).to(snake_case   )

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








from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL
from PIL import Image

from ...utils import (
    BaseOutput,
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_onnx_available,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)







@dataclass
class 	__lowerCAmelCase      (       _UpperCamelCase	):
   _UpperCamelCase			:    Union[List[PIL.Image.Image], np.ndarray]
   _UpperCamelCase			:    Optional[List[bool]]


try:
  if not (is_transformers_available() and is_torch_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
  from .pipeline_cycle_diffusion import CycleDiffusionPipeline
  from .pipeline_stable_diffusion import StableDiffusionPipeline
  from .pipeline_stable_diffusion_attend_and_excite import StableDiffusionAttendAndExcitePipeline
  from .pipeline_stable_diffusion_imgaimg import StableDiffusionImgaImgPipeline
  from .pipeline_stable_diffusion_inpaint import StableDiffusionInpaintPipeline
  from .pipeline_stable_diffusion_inpaint_legacy import StableDiffusionInpaintPipelineLegacy
  from .pipeline_stable_diffusion_instruct_pixapix import StableDiffusionInstructPixaPixPipeline
  from .pipeline_stable_diffusion_latent_upscale import StableDiffusionLatentUpscalePipeline
  from .pipeline_stable_diffusion_ldmad import StableDiffusionLDMaDPipeline
  from .pipeline_stable_diffusion_model_editing import StableDiffusionModelEditingPipeline
  from .pipeline_stable_diffusion_panorama import StableDiffusionPanoramaPipeline
  from .pipeline_stable_diffusion_paradigms import StableDiffusionParadigmsPipeline
  from .pipeline_stable_diffusion_sag import StableDiffusionSAGPipeline
  from .pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline
  from .pipeline_stable_unclip import StableUnCLIPPipeline
  from .pipeline_stable_unclip_imgaimg import StableUnCLIPImgaImgPipeline
  from .safety_checker import StableDiffusionSafetyChecker
  from .stable_unclip_image_normalizer import StableUnCLIPImageNormalizer

try:
  if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import StableDiffusionImageVariationPipeline
else:
  from .pipeline_stable_diffusion_image_variation import StableDiffusionImageVariationPipeline


try:
  if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.26.0""")):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import (
      StableDiffusionDepthaImgPipeline,
      StableDiffusionDiffEditPipeline,
      StableDiffusionPixaPixZeroPipeline,
  )
else:
  from .pipeline_stable_diffusion_depthaimg import StableDiffusionDepthaImgPipeline
  from .pipeline_stable_diffusion_diffedit import StableDiffusionDiffEditPipeline
  from .pipeline_stable_diffusion_pixapix_zero import StableDiffusionPixaPixZeroPipeline


try:
  if not (
      is_torch_available()
      and is_transformers_available()
      and is_k_diffusion_available()
      and is_k_diffusion_version(""">=""", """0.0.12""")
  ):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
  from .pipeline_stable_diffusion_k_diffusion import StableDiffusionKDiffusionPipeline

try:
  if not (is_transformers_available() and is_onnx_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_onnx_objects import *  # noqa F403
else:
  from .pipeline_onnx_stable_diffusion import OnnxStableDiffusionPipeline, StableDiffusionOnnxPipeline
  from .pipeline_onnx_stable_diffusion_imgaimg import OnnxStableDiffusionImgaImgPipeline
  from .pipeline_onnx_stable_diffusion_inpaint import OnnxStableDiffusionInpaintPipeline
  from .pipeline_onnx_stable_diffusion_inpaint_legacy import OnnxStableDiffusionInpaintPipelineLegacy
  from .pipeline_onnx_stable_diffusion_upscale import OnnxStableDiffusionUpscalePipeline

if is_transformers_available() and is_flax_available():
  import flax







  @flax.struct.dataclass
  class 	__lowerCAmelCase      (       _UpperCamelCase	):
     _UpperCamelCase			:    np.ndarray
     _UpperCamelCase			:    List[bool]







  from ...schedulers.scheduling_pndm_flax import PNDMSchedulerState
  from .pipeline_flax_stable_diffusion import FlaxStableDiffusionPipeline
  from .pipeline_flax_stable_diffusion_imgaimg import FlaxStableDiffusionImgaImgPipeline
  from .pipeline_flax_stable_diffusion_inpaint import FlaxStableDiffusionInpaintPipeline
  from .safety_checker_flax import FlaxStableDiffusionSafetyChecker

