


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

       def __init__(   self      :     int      ,							lowerCamelCase_      :     Any      ,							lowerCamelCase_      :     Optional[Any]=2      ,							lowerCamelCase_      :     Optional[Any]=3      ,							lowerCamelCase_      :     int=4      ,							lowerCamelCase_      :     Optional[int]=2      ,							lowerCamelCase_      :     List[str]=7      ,							lowerCamelCase_      :     str=True      ,							lowerCamelCase_      :     Optional[Any]=True      ,							lowerCamelCase_      :     Dict=True      ,							lowerCamelCase_      :     str=True      ,							lowerCamelCase_      :     List[Any]=9_9      ,							lowerCamelCase_      :     Tuple=3_6      ,							lowerCamelCase_      :     int=3      ,							lowerCamelCase_      :     int=4      ,							lowerCamelCase_      :     List[str]=3_7      ,							lowerCamelCase_      :     Optional[Any]="gelu"      ,							lowerCamelCase_      :     Union[str, Any]=0.1      ,							lowerCamelCase_      :     Dict=0.1      ,							lowerCamelCase_      :     Dict=5_1_2      ,							lowerCamelCase_      :     List[str]=1_6      ,							lowerCamelCase_      :     str=2      ,							lowerCamelCase_      :     str=0.0_2      ,							lowerCamelCase_      :     Optional[Any]=6      ,							lowerCamelCase_      :     Union[str, Any]=6      ,							lowerCamelCase_      :     Optional[int]=3      ,							lowerCamelCase_      :     Dict=4      ,							lowerCamelCase_      :     Tuple=None      ,							lowerCamelCase_      :     Dict=1_0_0_0      ,							):
            a_    :     str										=    parent
            a_    :     Union[str, Any]										=    batch_size
            a_    :     str										=    num_channels
            a_    :     int										=    image_size
            a_    :     List[Any]										=    patch_size
            a_    :     Union[str, Any]										=    text_seq_length
            a_    :     int										=    is_training
            a_    :     Any										=    use_input_mask
            a_    :     List[str]										=    use_token_type_ids
            a_    :     List[str]										=    use_labels
            a_    :     Optional[Any]										=    vocab_size
            a_    :     Tuple										=    hidden_size
            a_    :     int										=    num_hidden_layers
            a_    :     Optional[int]										=    num_attention_heads
            a_    :     Optional[int]										=    intermediate_size
            a_    :     Union[str, Any]										=    hidden_act
            a_    :     Union[str, Any]										=    hidden_dropout_prob
            a_    :     List[Any]										=    attention_probs_dropout_prob
            a_    :     int										=    max_position_embeddings
            a_    :     Optional[int]										=    type_vocab_size
            a_    :     Dict										=    type_sequence_label_size
            a_    :     Optional[Any]										=    initializer_range
            a_    :     int										=    coordinate_size
            a_    :     Union[str, Any]										=    shape_size
            a_    :     Optional[int]										=    num_labels
            a_    :     str										=    num_choices
            a_    :     Optional[int]										=    scope
            a_    :     Dict										=    range_bbox

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

       def 						UpperCAmelCase(   self      :     int    ):
            a_    :     Union[str, Any]										=    ids_tensor([self.batch_size, self.text_seq_length]      ,							self.vocab_size    )

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

            a_    :     str										=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

            a_    :     Optional[int]										=    None
            if self.use_input_mask:
                 a_    :     List[str]										=    random_attention_mask([self.batch_size, self.text_seq_length]    )

            a_    :     List[Any]										=    None
            if self.use_token_type_ids:
                 a_    :     Optional[Any]										=    ids_tensor([self.batch_size, self.text_seq_length]      ,							self.type_vocab_size    )

            a_    :     str										=    None
            a_    :     int										=    None
            if self.use_labels:
                 a_    :     Union[str, Any]										=    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_    :     int										=    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 						UpperCAmelCase(   self      :     Union[str, Any]      ,							lowerCamelCase_      :     Optional[int]      ,							lowerCamelCase_      :     Union[str, Any]      ,							lowerCamelCase_      :     Dict      ,							lowerCamelCase_      :     Tuple      ,							lowerCamelCase_      :     List[Any]      ,							lowerCamelCase_      :     Tuple      ,							lowerCamelCase_      :     Tuple      ,							lowerCamelCase_      :     Optional[int]    ):
            a_    :     List[str]										=    LayoutLMvaModel(config=lowerCamelCase_    )
            model.to(lowerCamelCase_    )
            model.eval()

            # text + image
            a_    :     str										=    model(lowerCamelCase_      ,							pixel_values=lowerCamelCase_    )
            a_    :     int										=    model(
                lowerCamelCase_      ,							bbox=lowerCamelCase_      ,							pixel_values=lowerCamelCase_      ,							attention_mask=lowerCamelCase_      ,							token_type_ids=lowerCamelCase_    )
            a_    :     List[Any]										=    model(lowerCamelCase_      ,							bbox=lowerCamelCase_      ,							pixel_values=lowerCamelCase_      ,							token_type_ids=lowerCamelCase_    )
            a_    :     List[str]										=    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
            a_    :     str										=    model(lowerCamelCase_    )
            self.parent.assertEqual(
                result.last_hidden_state.shape      ,							(self.batch_size, self.text_seq_length, self.hidden_size)    )

            # image only
            a_    :     Any										=    model(pixel_values=lowerCamelCase_    )
            self.parent.assertEqual(
                result.last_hidden_state.shape      ,							(self.batch_size, self.image_seq_length, self.hidden_size)    )

       def 						UpperCAmelCase(   self      :     List[str]      ,							lowerCamelCase_      :     List[str]      ,							lowerCamelCase_      :     List[Any]      ,							lowerCamelCase_      :     Any      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     List[Any]      ,							lowerCamelCase_      :     str      ,							lowerCamelCase_      :     Dict      ,							lowerCamelCase_      :     List[str]    ):
            a_    :     str										=    self.num_labels
            a_    :     Optional[Any]										=    LayoutLMvaForSequenceClassification(lowerCamelCase_    )
            model.to(lowerCamelCase_    )
            model.eval()
            a_    :     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 						UpperCAmelCase(   self      :     Optional[Any]      ,							lowerCamelCase_      :     str      ,							lowerCamelCase_      :     Optional[Any]      ,							lowerCamelCase_      :     Optional[int]      ,							lowerCamelCase_      :     List[Any]      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     str      ,							lowerCamelCase_      :     str      ,							lowerCamelCase_      :     Optional[int]    ):
            a_    :     int										=    self.num_labels
            a_    :     Optional[int]										=    LayoutLMvaForTokenClassification(config=lowerCamelCase_    )
            model.to(lowerCamelCase_    )
            model.eval()
            a_    :     Optional[int]										=    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 						UpperCAmelCase(   self      :     Union[str, Any]      ,							lowerCamelCase_      :     Union[str, Any]      ,							lowerCamelCase_      :     Optional[Any]      ,							lowerCamelCase_      :     Tuple      ,							lowerCamelCase_      :     Union[str, Any]      ,							lowerCamelCase_      :     Union[str, Any]      ,							lowerCamelCase_      :     Any      ,							lowerCamelCase_      :     Dict      ,							lowerCamelCase_      :     Optional[Any]    ):
            a_    :     Optional[Any]										=    LayoutLMvaForQuestionAnswering(config=lowerCamelCase_    )
            model.to(lowerCamelCase_    )
            model.eval()
            a_    :     Union[str, 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 						UpperCAmelCase(   self      :     str    ):
            a_    :     Tuple										=    self.prepare_config_and_inputs()
            (
                a_			
            )    :     List[str]										=    config_and_inputs
            a_    :     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   a__	(						lowerCAmelCase_						,       lowerCAmelCase_						,       unittest.TestCase    ):
       lowerCamelCase__:     Dict							    =     False
       lowerCamelCase__:     Dict							    =     False
       lowerCamelCase__:     str							    =     False

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

       def 						UpperCAmelCase(   self      :     Union[str, Any]      ,							lowerCamelCase_      :     str      ,							lowerCamelCase_      :     Optional[Any]      ,							lowerCamelCase_      :     Union[str, Any]      ,							lowerCamelCase_      :     str      ,							lowerCamelCase_      :     Optional[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 						UpperCAmelCase(   self      :     Optional[int]    ):
            a_    :     Optional[Any]										=    LayoutLMvaModelTester(self    )
            a_    :     Union[str, Any]										=    ConfigTester(self      ,							config_class=lowerCamelCase_      ,							hidden_size=3_7    )

       def 						UpperCAmelCase(   self      :     int      ,							lowerCamelCase_      :     str      ,							lowerCamelCase_      :     Any      ,							lowerCamelCase_      :     Union[str, Any]=False    ):
            a_    :     Optional[int]										=    copy.deepcopy(lowerCamelCase_    )
            if model_class in get_values(lowerCamelCase_    ):
                 a_    :     Optional[Any]										=    {
                     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_    ):
                      a_    :     Optional[int]										=    torch.ones(self.model_tester.batch_size      ,							dtype=torch.long      ,							device=lowerCamelCase_    )
                 elif model_class in get_values(lowerCamelCase_    ):
                      a_    :     Union[str, Any]										=    torch.zeros(
                          self.model_tester.batch_size      ,							dtype=torch.long      ,							device=lowerCamelCase_    )
                      a_    :     Optional[int]										=    torch.zeros(
                          self.model_tester.batch_size      ,							dtype=torch.long      ,							device=lowerCamelCase_    )
                 elif model_class in [
                     *get_values(lowerCamelCase_    ),
                 ]:
                      a_    :     List[str]										=    torch.zeros(
                          self.model_tester.batch_size      ,							dtype=torch.long      ,							device=lowerCamelCase_    )
                 elif model_class in [
                     *get_values(lowerCamelCase_    ),
                 ]:
                      a_    :     str										=    torch.zeros(
                          (self.model_tester.batch_size, self.model_tester.text_seq_length)      ,							dtype=torch.long      ,							device=lowerCamelCase_      ,							)

            return inputs_dict

       def 						UpperCAmelCase(   self      :     int    ):
            self.config_tester.run_common_tests()

       def 						UpperCAmelCase(   self      :     Dict    ):
            a_    :     Dict										=    self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     Any    ):
            a_    :     int										=    self.model_tester.prepare_config_and_inputs()
            for type in ["absolute", "relative_key", "relative_key_query"]:
                 a_    :     Any										=    type
                 self.model_tester.create_and_check_model(*lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     List[str]    ):
            a_    :     Any										=    self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     Optional[Any]    ):
            a_    :     Union[str, Any]										=    self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     Tuple    ):
            a_    :     List[Any]										=    self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_    )

       @slow
       def 						UpperCAmelCase(   self      :     Any    ):
            for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 a_    :     Dict										=    LayoutLMvaModel.from_pretrained(lowerCamelCase_    )
                 self.assertIsNotNone(lowerCamelCase_    )



def  _a			(						):
     a_    :     List[str]										=    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
     return image


@require_torch
class   a__	(						unittest.TestCase    ):

       @cached_property
       def 						UpperCAmelCase(   self      :     Dict    ):
            return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase_    ) if is_vision_available() else None

       @slow
       def 						UpperCAmelCase(   self      :     List[Any]    ):
            a_    :     Any										=    LayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""    ).to(lowerCamelCase_    )

            a_    :     List[str]										=    self.default_image_processor
            a_    :     Union[str, Any]										=    prepare_img()
            a_    :     Optional[Any]										=    image_processor(images=lowerCamelCase_      ,							return_tensors="""pt"""    ).pixel_values.to(lowerCamelCase_    )

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

            # forward pass
            a_    :     Any										=    model(
                input_ids=input_ids.to(lowerCamelCase_    )      ,							bbox=bbox.to(lowerCamelCase_    )      ,							pixel_values=pixel_values.to(lowerCamelCase_    )      ,							)

            # verify the logits
            a_    :     Union[str, Any]										=    torch.Size((1, 1_9_9, 7_6_8)    )
            self.assertEqual(outputs.last_hidden_state.shape      ,							lowerCamelCase_    )

            a_    :     int										=    torch.tensor(
                [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]]    ).to(lowerCamelCase_    )

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







import tempfile
import unittest

import numpy as np

from diffusers import (
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionPipeline,
    PNDMScheduler,
)
from diffusers.utils.testing_utils import is_onnx_available, nightly, require_onnxruntime, require_torch_gpu

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
       import onnxruntime as ort


class   a__	(						lowerCAmelCase_						,       unittest.TestCase    ):
       lowerCamelCase__:     int							    =     """hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline"""

       def 						UpperCAmelCase(   self      :     int      ,							lowerCamelCase_      :     Union[str, Any]=0    ):
            a_    :     Any										=    np.random.RandomState(lowerCamelCase_    )
            a_    :     Optional[Any]										=    {
                """prompt""": """A painting of a squirrel eating a burger""",
                """generator""": generator,
                """num_inference_steps""": 2,
                """guidance_scale""": 7.5,
                """output_type""": """numpy""",
            }
            return inputs

       def 						UpperCAmelCase(   self      :     List[Any]    ):
            a_    :     List[Any]										=    OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint      ,							provider="""CPUExecutionProvider"""    )
            pipe.set_progress_bar_config(disable=lowerCamelCase_    )

            a_    :     Optional[int]										=    self.get_dummy_inputs()
            a_    :     Optional[Any]										=    pipe(**lowerCamelCase_    ).images
            a_    :     Tuple										=    image[0, -3:, -3:, -1]

            assert image.shape == (1, 1_2_8, 1_2_8, 3)
            a_    :     str										=    np.array([0.6_5_0_7_2, 0.5_8_4_9_2, 0.4_8_2_1_9, 0.5_5_5_2_1, 0.5_3_1_8_0, 0.5_5_9_3_9, 0.5_0_6_9_7, 0.3_9_8_0_0, 0.4_6_4_5_5]    )

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

       def 						UpperCAmelCase(   self      :     List[str]    ):
            a_    :     str										=    OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint      ,							provider="""CPUExecutionProvider"""    )
            a_    :     Any										=    PNDMScheduler.from_config(pipe.scheduler.config      ,							skip_prk_steps=lowerCamelCase_    )
            pipe.set_progress_bar_config(disable=lowerCamelCase_    )

            a_    :     Dict										=    self.get_dummy_inputs()
            a_    :     Optional[int]										=    pipe(**lowerCamelCase_    ).images
            a_    :     Optional[Any]										=    image[0, -3:, -3:, -1]

            assert image.shape == (1, 1_2_8, 1_2_8, 3)
            a_    :     Tuple										=    np.array([0.6_5_8_6_3, 0.5_9_4_2_5, 0.4_9_3_2_6, 0.5_6_3_1_3, 0.5_3_8_7_5, 0.5_6_6_2_7, 0.5_1_0_6_5, 0.3_9_7_7_7, 0.4_6_3_3_0]    )

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

       def 						UpperCAmelCase(   self      :     List[Any]    ):
            a_    :     Optional[Any]										=    OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint      ,							provider="""CPUExecutionProvider"""    )
            a_    :     Optional[int]										=    LMSDiscreteScheduler.from_config(pipe.scheduler.config    )
            pipe.set_progress_bar_config(disable=lowerCamelCase_    )

            a_    :     Dict										=    self.get_dummy_inputs()
            a_    :     Union[str, Any]										=    pipe(**lowerCamelCase_    ).images
            a_    :     int										=    image[0, -3:, -3:, -1]

            assert image.shape == (1, 1_2_8, 1_2_8, 3)
            a_    :     Optional[int]										=    np.array([0.5_3_7_5_5, 0.6_0_7_8_6, 0.4_7_4_0_2, 0.4_9_4_8_8, 0.5_1_8_6_9, 0.4_9_8_1_9, 0.4_7_9_8_5, 0.3_8_9_5_7, 0.4_4_2_7_9]    )

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

       def 						UpperCAmelCase(   self      :     Any    ):
            a_    :     int										=    OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint      ,							provider="""CPUExecutionProvider"""    )
            a_    :     Optional[Any]										=    EulerDiscreteScheduler.from_config(pipe.scheduler.config    )
            pipe.set_progress_bar_config(disable=lowerCamelCase_    )

            a_    :     Optional[Any]										=    self.get_dummy_inputs()
            a_    :     Optional[Any]										=    pipe(**lowerCamelCase_    ).images
            a_    :     Optional[Any]										=    image[0, -3:, -3:, -1]

            assert image.shape == (1, 1_2_8, 1_2_8, 3)
            a_    :     Optional[int]										=    np.array([0.5_3_7_5_5, 0.6_0_7_8_6, 0.4_7_4_0_2, 0.4_9_4_8_8, 0.5_1_8_6_9, 0.4_9_8_1_9, 0.4_7_9_8_5, 0.3_8_9_5_7, 0.4_4_2_7_9]    )

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

       def 						UpperCAmelCase(   self      :     List[str]    ):
            a_    :     Any										=    OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint      ,							provider="""CPUExecutionProvider"""    )
            a_    :     str										=    EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config    )
            pipe.set_progress_bar_config(disable=lowerCamelCase_    )

            a_    :     Dict										=    self.get_dummy_inputs()
            a_    :     Union[str, Any]										=    pipe(**lowerCamelCase_    ).images
            a_    :     Any										=    image[0, -3:, -3:, -1]

            assert image.shape == (1, 1_2_8, 1_2_8, 3)
            a_    :     Tuple										=    np.array([0.5_3_8_1_7, 0.6_0_8_1_2, 0.4_7_3_8_4, 0.4_9_5_3_0, 0.5_1_8_9_4, 0.4_9_8_1_4, 0.4_7_9_8_4, 0.3_8_9_5_8, 0.4_4_2_7_1]    )

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

       def 						UpperCAmelCase(   self      :     Optional[Any]    ):
            a_    :     Tuple										=    OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint      ,							provider="""CPUExecutionProvider"""    )
            a_    :     Tuple										=    DPMSolverMultistepScheduler.from_config(pipe.scheduler.config    )
            pipe.set_progress_bar_config(disable=lowerCamelCase_    )

            a_    :     Tuple										=    self.get_dummy_inputs()
            a_    :     Optional[int]										=    pipe(**lowerCamelCase_    ).images
            a_    :     str										=    image[0, -3:, -3:, -1]

            assert image.shape == (1, 1_2_8, 1_2_8, 3)
            a_    :     List[Any]										=    np.array([0.5_3_8_9_5, 0.6_0_8_0_8, 0.4_7_9_3_3, 0.4_9_6_0_8, 0.5_1_8_8_6, 0.4_9_9_5_0, 0.4_8_0_5_3, 0.3_8_9_5_7, 0.4_4_2_0_0]    )

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

       def 						UpperCAmelCase(   self      :     Optional[Any]    ):
            a_    :     int										=    OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint      ,							provider="""CPUExecutionProvider"""    )
            pipe.set_progress_bar_config(disable=lowerCamelCase_    )

            a_    :     List[str]										=    self.get_dummy_inputs()
            a_    :     Optional[int]										=    3 * [inputs["""prompt"""]]

            # forward
            a_    :     Optional[int]										=    pipe(**lowerCamelCase_    )
            a_    :     Union[str, Any]										=    output.images[0, -3:, -3:, -1]

            a_    :     Tuple										=    self.get_dummy_inputs()
            a_    :     List[Any]										=    3 * [inputs.pop("""prompt"""    )]

            a_    :     List[Any]										=    pipe.tokenizer(
                lowerCamelCase_      ,							padding="""max_length"""      ,							max_length=pipe.tokenizer.model_max_length      ,							truncation=lowerCamelCase_      ,							return_tensors="""np"""      ,							)
            a_    :     str										=    text_inputs["""input_ids"""]

            a_    :     Dict										=    pipe.text_encoder(input_ids=text_inputs.astype(np.intaa    )    )[0]

            a_    :     List[Any]										=    prompt_embeds

            # forward
            a_    :     Tuple										=    pipe(**lowerCamelCase_    )
            a_    :     Union[str, Any]										=    output.images[0, -3:, -3:, -1]

            assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()    ).max() < 1E-4

       def 						UpperCAmelCase(   self      :     List[str]    ):
            a_    :     Any										=    OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint      ,							provider="""CPUExecutionProvider"""    )
            pipe.set_progress_bar_config(disable=lowerCamelCase_    )

            a_    :     int										=    self.get_dummy_inputs()
            a_    :     str										=    3 * ["""this is a negative prompt"""]
            a_    :     int										=    negative_prompt
            a_    :     Dict										=    3 * [inputs["""prompt"""]]

            # forward
            a_    :     Tuple										=    pipe(**lowerCamelCase_    )
            a_    :     Union[str, Any]										=    output.images[0, -3:, -3:, -1]

            a_    :     Tuple										=    self.get_dummy_inputs()
            a_    :     Optional[int]										=    3 * [inputs.pop("""prompt"""    )]

            a_    :     str										=    []
            for p in [prompt, negative_prompt]:
                 a_    :     Optional[int]										=    pipe.tokenizer(
                     lowerCamelCase_      ,							padding="""max_length"""      ,							max_length=pipe.tokenizer.model_max_length      ,							truncation=lowerCamelCase_      ,							return_tensors="""np"""      ,							)
                 a_    :     int										=    text_inputs["""input_ids"""]

                 embeds.append(pipe.text_encoder(input_ids=text_inputs.astype(np.intaa    )    )[0]    )

            a_			,			a_    :     Union[str, Any]										=    embeds

            # forward
            a_    :     Optional[Any]										=    pipe(**lowerCamelCase_    )
            a_    :     int										=    output.images[0, -3:, -3:, -1]

            assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()    ).max() < 1E-4


@nightly
@require_onnxruntime
@require_torch_gpu
class   a__	(						unittest.TestCase    ):

       @property
       def 						UpperCAmelCase(   self      :     Tuple    ):
            return (
                "CUDAExecutionProvider",
                {
                    "gpu_mem_limit": "15000000000",  # 15GB
                    "arena_extend_strategy": "kSameAsRequested",
                },
            )

       @property
       def 						UpperCAmelCase(   self      :     List[Any]    ):
            a_    :     List[str]										=    ort.SessionOptions()
            a_    :     List[str]										=    False
            return options

       def 						UpperCAmelCase(   self      :     Optional[Any]    ):
            # using the PNDM scheduler by default
            a_    :     int										=    OnnxStableDiffusionPipeline.from_pretrained(
                """CompVis/stable-diffusion-v1-4"""      ,							revision="""onnx"""      ,							safety_checker=lowerCamelCase_      ,							feature_extractor=lowerCamelCase_      ,							provider=self.gpu_provider      ,							sess_options=self.gpu_options      ,							)
            sd_pipe.set_progress_bar_config(disable=lowerCamelCase_    )

            a_    :     Tuple										=    """A painting of a squirrel eating a burger"""
            np.random.seed(0    )
            a_    :     int										=    sd_pipe([prompt]      ,							guidance_scale=6.0      ,							num_inference_steps=1_0      ,							output_type="""np"""    )
            a_    :     Optional[int]										=    output.images

            a_    :     Tuple										=    image[0, -3:, -3:, -1]

            assert image.shape == (1, 5_1_2, 5_1_2, 3)
            a_    :     Optional[int]										=    np.array([0.0_4_5_2, 0.0_3_9_0, 0.0_0_8_7, 0.0_3_5_0, 0.0_6_1_7, 0.0_3_6_4, 0.0_5_4_4, 0.0_5_2_3, 0.0_7_2_0]    )

            assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1E-3

       def 						UpperCAmelCase(   self      :     str    ):
            a_    :     str										=    DDIMScheduler.from_pretrained(
                """runwayml/stable-diffusion-v1-5"""      ,							subfolder="""scheduler"""      ,							revision="""onnx"""    )
            a_    :     Any										=    OnnxStableDiffusionPipeline.from_pretrained(
                """runwayml/stable-diffusion-v1-5"""      ,							revision="""onnx"""      ,							scheduler=lowerCamelCase_      ,							safety_checker=lowerCamelCase_      ,							feature_extractor=lowerCamelCase_      ,							provider=self.gpu_provider      ,							sess_options=self.gpu_options      ,							)
            sd_pipe.set_progress_bar_config(disable=lowerCamelCase_    )

            a_    :     Union[str, Any]										=    """open neural network exchange"""
            a_    :     str										=    np.random.RandomState(0    )
            a_    :     str										=    sd_pipe([prompt]      ,							guidance_scale=7.5      ,							num_inference_steps=1_0      ,							generator=lowerCamelCase_      ,							output_type="""np"""    )
            a_    :     str										=    output.images
            a_    :     Optional[Any]										=    image[0, -3:, -3:, -1]

            assert image.shape == (1, 5_1_2, 5_1_2, 3)
            a_    :     List[str]										=    np.array([0.2_8_6_7, 0.1_9_7_4, 0.1_4_8_1, 0.7_2_9_4, 0.7_2_5_1, 0.6_6_6_7, 0.4_1_9_4, 0.5_6_4_2, 0.6_4_8_6]    )

            assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1E-3

       def 						UpperCAmelCase(   self      :     Optional[int]    ):
            a_    :     Optional[int]										=    LMSDiscreteScheduler.from_pretrained(
                """runwayml/stable-diffusion-v1-5"""      ,							subfolder="""scheduler"""      ,							revision="""onnx"""    )
            a_    :     List[str]										=    OnnxStableDiffusionPipeline.from_pretrained(
                """runwayml/stable-diffusion-v1-5"""      ,							revision="""onnx"""      ,							scheduler=lowerCamelCase_      ,							safety_checker=lowerCamelCase_      ,							feature_extractor=lowerCamelCase_      ,							provider=self.gpu_provider      ,							sess_options=self.gpu_options      ,							)
            sd_pipe.set_progress_bar_config(disable=lowerCamelCase_    )

            a_    :     Optional[int]										=    """open neural network exchange"""
            a_    :     Tuple										=    np.random.RandomState(0    )
            a_    :     Tuple										=    sd_pipe([prompt]      ,							guidance_scale=7.5      ,							num_inference_steps=1_0      ,							generator=lowerCamelCase_      ,							output_type="""np"""    )
            a_    :     Tuple										=    output.images
            a_    :     str										=    image[0, -3:, -3:, -1]

            assert image.shape == (1, 5_1_2, 5_1_2, 3)
            a_    :     str										=    np.array([0.2_3_0_6, 0.1_9_5_9, 0.1_5_9_3, 0.6_5_4_9, 0.6_3_9_4, 0.5_4_0_8, 0.5_0_6_5, 0.6_0_1_0, 0.6_1_6_1]    )

            assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1E-3

       def 						UpperCAmelCase(   self      :     Optional[Any]    ):
            a_    :     List[str]										=    0

            def test_callback_fn(lowerCamelCase_      :     int      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     np.ndarray    ) -> None:
                 a_    :     List[Any]										=    True
                 nonlocal number_of_steps
                 number_of_steps += 1
                 if step == 0:
                      assert latents.shape == (1, 4, 6_4, 6_4)
                      a_    :     Union[str, Any]										=    latents[0, -3:, -3:, -1]
                      a_    :     Optional[Any]										=    np.array(
                          [-0.6_7_7_2, -0.3_8_3_5, -1.2_4_5_6, 0.1_9_0_5, -1.0_9_7_4, 0.6_9_6_7, -1.9_3_5_3, 0.0_1_7_8, 1.0_1_6_7]    )

                      assert np.abs(latents_slice.flatten() - expected_slice    ).max() < 1E-3
                 elif step == 5:
                      assert latents.shape == (1, 4, 6_4, 6_4)
                      a_    :     List[str]										=    latents[0, -3:, -3:, -1]
                      a_    :     int										=    np.array(
                          [-0.3_3_5_1, 0.2_2_4_1, -0.1_8_3_7, -0.2_3_2_5, -0.6_5_7_7, 0.3_3_9_3, -0.0_2_4_1, 0.5_8_9_9, 1.3_8_7_5]    )

                      assert np.abs(latents_slice.flatten() - expected_slice    ).max() < 1E-3

            a_    :     Optional[int]										=    False

            a_    :     List[Any]										=    OnnxStableDiffusionPipeline.from_pretrained(
                """runwayml/stable-diffusion-v1-5"""      ,							revision="""onnx"""      ,							safety_checker=lowerCamelCase_      ,							feature_extractor=lowerCamelCase_      ,							provider=self.gpu_provider      ,							sess_options=self.gpu_options      ,							)
            pipe.set_progress_bar_config(disable=lowerCamelCase_    )

            a_    :     List[str]										=    """Andromeda galaxy in a bottle"""

            a_    :     Union[str, Any]										=    np.random.RandomState(0    )
            pipe(
                prompt=lowerCamelCase_      ,							num_inference_steps=5      ,							guidance_scale=7.5      ,							generator=lowerCamelCase_      ,							callback=lowerCamelCase_      ,							callback_steps=1      ,							)
            assert test_callback_fn.has_been_called
            assert number_of_steps == 6

       def 						UpperCAmelCase(   self      :     Dict    ):
            a_    :     Tuple										=    OnnxStableDiffusionPipeline.from_pretrained(
                """runwayml/stable-diffusion-v1-5"""      ,							revision="""onnx"""      ,							safety_checker=lowerCamelCase_      ,							feature_extractor=lowerCamelCase_      ,							provider=self.gpu_provider      ,							sess_options=self.gpu_options      ,							)
            assert isinstance(lowerCamelCase_      ,							lowerCamelCase_    )
            assert pipe.safety_checker is None

            a_    :     Union[str, Any]										=    pipe("""example prompt"""      ,							num_inference_steps=2    ).images[0]
            assert image is not None

            # check that there's no error when saving a pipeline with one of the models being None
            with tempfile.TemporaryDirectory() as tmpdirname:
                 pipe.save_pretrained(lowerCamelCase_    )
                 a_    :     Tuple										=    OnnxStableDiffusionPipeline.from_pretrained(lowerCamelCase_    )

            # sanity check that the pipeline still works
            assert pipe.safety_checker is None
            a_    :     Any										=    pipe("""example prompt"""      ,							num_inference_steps=2    ).images[0]
            assert image is not None



